Blame vpdopt.c

Packit Service e18529
/*
Packit Service e18529
 * Command line handling of vpddecode
Packit Service e18529
 * This file is part of the dmidecode project.
Packit Service e18529
 *
Packit Service e18529
 *   Copyright (C) 2005-2007 Jean Delvare <jdelvare@suse.de>
Packit Service e18529
 *
Packit Service e18529
 *   This program is free software; you can redistribute it and/or modify
Packit Service e18529
 *   it under the terms of the GNU General Public License as published by
Packit Service e18529
 *   the Free Software Foundation; either version 2 of the License, or
Packit Service e18529
 *   (at your option) any later version.
Packit Service e18529
 *
Packit Service e18529
 *   This program is distributed in the hope that it will be useful,
Packit Service e18529
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service e18529
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service e18529
 *   GNU General Public License for more details.
Packit Service e18529
 *
Packit Service e18529
 *   You should have received a copy of the GNU General Public License
Packit Service e18529
 *   along with this program; if not, write to the Free Software
Packit Service e18529
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Packit Service e18529
 */
Packit Service e18529
Packit Service e18529
#include <stdio.h>
Packit Service e18529
#include <strings.h>
Packit Service e18529
#include <stdlib.h>
Packit Service e18529
#include <getopt.h>
Packit Service e18529
Packit Service e18529
#include "config.h"
Packit Service e18529
#include "util.h"
Packit Service e18529
#include "vpdopt.h"
Packit Service e18529
Packit Service e18529
Packit Service e18529
/* Options are global */
Packit Service e18529
struct opt opt;
Packit Service e18529
Packit Service e18529
Packit Service e18529
/*
Packit Service e18529
 * Handling of option --string
Packit Service e18529
 */
Packit Service e18529
Packit Service e18529
/* This lookup table could admittedly be reworked for improved performance.
Packit Service e18529
   Due to the low count of items in there at the moment, it did not seem
Packit Service e18529
   worth the additional code complexity though. */
Packit Service e18529
static const struct string_keyword opt_string_keyword[] = {
Packit Service e18529
	{ "bios-build-id", 0x0D, 9 },
Packit Service e18529
	{ "box-serial-number", 0x16, 7 },
Packit Service e18529
	{ "motherboard-serial-number", 0x1D, 11 },
Packit Service e18529
	{ "machine-type-model", 0x28, 7 },
Packit Service e18529
	{ "bios-release-date", 0x30, 8 },
Packit Service e18529
};
Packit Service e18529
Packit Service e18529
static void print_opt_string_list(void)
Packit Service e18529
{
Packit Service e18529
	unsigned int i;
Packit Service e18529
Packit Service e18529
	fprintf(stderr, "Valid string keywords are:\n");
Packit Service e18529
	for (i = 0; i < ARRAY_SIZE(opt_string_keyword); i++)
Packit Service e18529
	{
Packit Service e18529
		fprintf(stderr, "  %s\n", opt_string_keyword[i].keyword);
Packit Service e18529
	}
Packit Service e18529
}
Packit Service e18529
Packit Service e18529
static int parse_opt_string(const char *arg)
Packit Service e18529
{
Packit Service e18529
	unsigned int i;
Packit Service e18529
Packit Service e18529
	if (opt.string)
Packit Service e18529
	{
Packit Service e18529
		fprintf(stderr, "Only one string can be specified\n");
Packit Service e18529
		return -1;
Packit Service e18529
	}
Packit Service e18529
Packit Service e18529
	for (i = 0; i
Packit Service e18529
	{
Packit Service e18529
		if (!strcasecmp(arg, opt_string_keyword[i].keyword))
Packit Service e18529
		{
Packit Service e18529
			opt.string = &opt_string_keyword[i];
Packit Service e18529
			return 0;
Packit Service e18529
		}
Packit Service e18529
	}
Packit Service e18529
Packit Service e18529
	fprintf(stderr, "Invalid string keyword: %s\n", arg);
Packit Service e18529
	print_opt_string_list();
Packit Service e18529
	return -1;
Packit Service e18529
}
Packit Service e18529
Packit Service e18529
Packit Service e18529
/*
Packit Service e18529
 * Command line options handling
Packit Service e18529
 */
Packit Service e18529
Packit Service e18529
/* Return -1 on error, 0 on success */
Packit Service e18529
int parse_command_line(int argc, char * const argv[])
Packit Service e18529
{
Packit Service e18529
	int option;
Packit Service e18529
	const char *optstring = "d:hs:uV";
Packit Service e18529
	struct option longopts[] = {
Packit Service e18529
		{ "dev-mem", required_argument, NULL, 'd' },
Packit Service e18529
		{ "help", no_argument, NULL, 'h' },
Packit Service e18529
		{ "string", required_argument, NULL, 's' },
Packit Service e18529
		{ "dump", no_argument, NULL, 'u' },
Packit Service e18529
		{ "version", no_argument, NULL, 'V' },
Packit Service e18529
		{ NULL, 0, NULL, 0 }
Packit Service e18529
	};
Packit Service e18529
Packit Service e18529
	while ((option = getopt_long(argc, argv, optstring, longopts, NULL)) != -1)
Packit Service e18529
		switch (option)
Packit Service e18529
		{
Packit Service e18529
			case 'd':
Packit Service e18529
				opt.devmem = optarg;
Packit Service e18529
				break;
Packit Service e18529
			case 'h':
Packit Service e18529
				opt.flags |= FLAG_HELP;
Packit Service e18529
				break;
Packit Service e18529
			case 's':
Packit Service e18529
				if (parse_opt_string(optarg) < 0)
Packit Service e18529
					return -1;
Packit Service e18529
				opt.flags |= FLAG_QUIET;
Packit Service e18529
				break;
Packit Service e18529
			case 'u':
Packit Service e18529
				opt.flags |= FLAG_DUMP;
Packit Service e18529
				break;
Packit Service e18529
			case 'V':
Packit Service e18529
				opt.flags |= FLAG_VERSION;
Packit Service e18529
				break;
Packit Service e18529
			case '?':
Packit Service e18529
				switch (optopt)
Packit Service e18529
				{
Packit Service e18529
					case 's':
Packit Service e18529
						fprintf(stderr, "String keyword expected\n");
Packit Service e18529
						print_opt_string_list();
Packit Service e18529
						break;
Packit Service e18529
				}
Packit Service e18529
				return -1;
Packit Service e18529
		}
Packit Service e18529
Packit Service e18529
	if ((opt.flags & FLAG_DUMP) && opt.string != NULL)
Packit Service e18529
	{
Packit Service e18529
		fprintf(stderr, "Options --string and --dump are mutually exclusive\n");
Packit Service e18529
		return -1;
Packit Service e18529
	}
Packit Service e18529
Packit Service e18529
	return 0;
Packit Service e18529
}
Packit Service e18529
Packit Service e18529
void print_help(void)
Packit Service e18529
{
Packit Service e18529
	static const char *help =
Packit Service e18529
		"Usage: vpddecode [OPTIONS]\n"
Packit Service e18529
		"Options are:\n"
Packit Service e18529
		" -d, --dev-mem FILE     Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n"
Packit Service e18529
		" -h, --help             Display this help text and exit\n"
Packit Service e18529
		" -s, --string KEYWORD   Only display the value of the given VPD string\n"
Packit Service e18529
		" -u, --dump             Do not decode the VPD records\n"
Packit Service e18529
		" -V, --version          Display the version and exit\n";
Packit Service e18529
Packit Service e18529
	printf("%s", help);
Packit Service e18529
}