Blame vpdopt.c

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