"Fossies" - the Fresh Open Source Software Archive

Member "dmidecode-3.3/vpdopt.c" (14 Oct 2020, 3906 Bytes) of package /linux/privat/dmidecode-3.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "vpdopt.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.12_vs_3.0.

    1 /*
    2  * Command line handling of vpddecode
    3  * This file is part of the dmidecode project.
    4  *
    5  *   Copyright (C) 2005-2007 Jean Delvare <jdelvare@suse.de>
    6  *
    7  *   This program is free software; you can redistribute it and/or modify
    8  *   it under the terms of the GNU General Public License as published by
    9  *   the Free Software Foundation; either version 2 of the License, or
   10  *   (at your option) any later version.
   11  *
   12  *   This program is distributed in the hope that it will be useful,
   13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  *   GNU General Public License for more details.
   16  *
   17  *   You should have received a copy of the GNU General Public License
   18  *   along with this program; if not, write to the Free Software
   19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   20  */
   21 
   22 #include <stdio.h>
   23 #include <strings.h>
   24 #include <stdlib.h>
   25 #include <getopt.h>
   26 
   27 #include "config.h"
   28 #include "util.h"
   29 #include "vpdopt.h"
   30 
   31 
   32 /* Options are global */
   33 struct opt opt;
   34 
   35 
   36 /*
   37  * Handling of option --string
   38  */
   39 
   40 /* This lookup table could admittedly be reworked for improved performance.
   41    Due to the low count of items in there at the moment, it did not seem
   42    worth the additional code complexity though. */
   43 static const struct string_keyword opt_string_keyword[] = {
   44     { "bios-build-id", 0x0D, 9 },
   45     { "box-serial-number", 0x16, 7 },
   46     { "motherboard-serial-number", 0x1D, 11 },
   47     { "machine-type-model", 0x28, 7 },
   48     { "bios-release-date", 0x30, 8 },
   49 };
   50 
   51 static void print_opt_string_list(void)
   52 {
   53     unsigned int i;
   54 
   55     fprintf(stderr, "Valid string keywords are:\n");
   56     for (i = 0; i < ARRAY_SIZE(opt_string_keyword); i++)
   57     {
   58         fprintf(stderr, "  %s\n", opt_string_keyword[i].keyword);
   59     }
   60 }
   61 
   62 static int parse_opt_string(const char *arg)
   63 {
   64     unsigned int i;
   65 
   66     if (opt.string)
   67     {
   68         fprintf(stderr, "Only one string can be specified\n");
   69         return -1;
   70     }
   71 
   72     for (i = 0; i<ARRAY_SIZE(opt_string_keyword); i++)
   73     {
   74         if (!strcasecmp(arg, opt_string_keyword[i].keyword))
   75         {
   76             opt.string = &opt_string_keyword[i];
   77             return 0;
   78         }
   79     }
   80 
   81     fprintf(stderr, "Invalid string keyword: %s\n", arg);
   82     print_opt_string_list();
   83     return -1;
   84 }
   85 
   86 
   87 /*
   88  * Command line options handling
   89  */
   90 
   91 /* Return -1 on error, 0 on success */
   92 int parse_command_line(int argc, char * const argv[])
   93 {
   94     int option;
   95     const char *optstring = "d:hs:uV";
   96     struct option longopts[] = {
   97         { "dev-mem", required_argument, NULL, 'd' },
   98         { "help", no_argument, NULL, 'h' },
   99         { "string", required_argument, NULL, 's' },
  100         { "dump", no_argument, NULL, 'u' },
  101         { "version", no_argument, NULL, 'V' },
  102         { NULL, 0, NULL, 0 }
  103     };
  104 
  105     while ((option = getopt_long(argc, argv, optstring, longopts, NULL)) != -1)
  106         switch (option)
  107         {
  108             case 'd':
  109                 opt.devmem = optarg;
  110                 break;
  111             case 'h':
  112                 opt.flags |= FLAG_HELP;
  113                 break;
  114             case 's':
  115                 if (parse_opt_string(optarg) < 0)
  116                     return -1;
  117                 opt.flags |= FLAG_QUIET;
  118                 break;
  119             case 'u':
  120                 opt.flags |= FLAG_DUMP;
  121                 break;
  122             case 'V':
  123                 opt.flags |= FLAG_VERSION;
  124                 break;
  125             case '?':
  126                 switch (optopt)
  127                 {
  128                     case 's':
  129                         fprintf(stderr, "String keyword expected\n");
  130                         print_opt_string_list();
  131                         break;
  132                 }
  133                 return -1;
  134         }
  135 
  136     if ((opt.flags & FLAG_DUMP) && opt.string != NULL)
  137     {
  138         fprintf(stderr, "Options --string and --dump are mutually exclusive\n");
  139         return -1;
  140     }
  141 
  142     return 0;
  143 }
  144 
  145 void print_help(void)
  146 {
  147     static const char *help =
  148         "Usage: vpddecode [OPTIONS]\n"
  149         "Options are:\n"
  150         " -d, --dev-mem FILE     Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n"
  151         " -h, --help             Display this help text and exit\n"
  152         " -s, --string KEYWORD   Only display the value of the given VPD string\n"
  153         " -u, --dump             Do not decode the VPD records\n"
  154         " -V, --version          Display the version and exit\n";
  155 
  156     printf("%s", help);
  157 }