summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkhali <khali>2005-06-15 18:22:35 +0000
committerkhali <khali>2005-06-15 18:22:35 +0000
commitfa2ae2c9a22633ff913d70de2a08efbb76c6addc (patch)
tree9396d9fc0a9e4d8220e4f1dd79ed3c5cfba7bcc0
parent729277961858b4e5410a52572cf81f7cc04ca4fd (diff)
downloaddmidecode-fa2ae2c9a22633ff913d70de2a08efbb76c6addc.tar.gz
Move the command line handling of dmidecode to a separate source file.
-rw-r--r--Makefile9
-rw-r--r--dmidecode.c114
-rw-r--r--dmiopt.c131
-rw-r--r--dmiopt.h35
4 files changed, 173 insertions, 116 deletions
diff --git a/Makefile b/Makefile
index 57f498d..0cd4dd7 100644
--- a/Makefile
+++ b/Makefile
@@ -40,8 +40,8 @@ all : dmidecode biosdecode ownership vpddecode
# Programs
#
-dmidecode : dmidecode.o util.o
- $(CC) $(LDFLAGS) dmidecode.o util.o -o $@
+dmidecode : dmidecode.o dmiopt.o util.o
+ $(CC) $(LDFLAGS) dmidecode.o dmiopt.o util.o -o $@
biosdecode : biosdecode.o util.o
$(CC) $(LDFLAGS) biosdecode.o util.o -o $@
@@ -56,7 +56,10 @@ vpddecode : vpddecode.o util.o
# Objects
#
-dmidecode.o : dmidecode.c version.h types.h util.h config.h
+dmidecode.o : dmidecode.c version.h types.h util.h config.h dmiopt.h
+ $(CC) $(CFLAGS) -c $< -o $@
+
+dmiopt.o : dmiopt.c config.h types.h dmiopt.h
$(CC) $(CFLAGS) -c $< -o $@
biosdecode.o : biosdecode.c version.h types.h util.h config.h
diff --git a/dmidecode.c b/dmidecode.c
index cb018c2..6be915e 100644
--- a/dmidecode.c
+++ b/dmidecode.c
@@ -51,7 +51,6 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
-#include <getopt.h>
#ifdef __ia64__
#define USE_EFI
@@ -61,19 +60,7 @@
#include "config.h"
#include "types.h"
#include "util.h"
-
-/* Options are global */
-struct opt
-{
- const char* devmem;
- unsigned int flags;
- u8 *type;
-};
-static struct opt opt;
-
-#define FLAG_VERSION (1<<0)
-#define FLAG_HELP (1<<1)
-#define FLAG_DUMP (1<<2)
+#include "dmiopt.h"
static const char *out_of_spec = "<OUT OF SPEC>";
static const char *bad_index = "<BAD INDEX>";
@@ -3871,105 +3858,6 @@ static int legacy_decode(u8 *buf, const char *devmem)
}
#endif /* USE_EFI */
-static u8 *parse_opt_type(u8* p, const char *arg)
-{
- /* Allocate memory on first call only */
- if(p==NULL)
- {
- p=(u8 *)calloc(256, sizeof(u8));
- if(p==NULL)
- {
- perror("calloc");
- return NULL;
- }
- }
-
- while(*arg!='\0')
- {
- unsigned long val;
- char *next;
-
- val=strtoul(arg, &next, 0);
- if(next==arg)
- {
- fprintf(stderr, "Invalid type: %s\n", arg);
- goto exit_free;
- }
- if(val>0xff)
- {
- fprintf(stderr, "Invalid type: %lu\n", val);
- goto exit_free;
- }
-
- p[val]=1;
- arg=next;
- while(*arg==',' || *arg==' ')
- arg++;
- }
-
- return p;
-
-exit_free:
- free(p);
- return NULL;
-}
-
-/* Return -1 on error, 0 on success */
-static int parse_command_line(int argc, char * const argv[])
-{
- int option;
- const char *optstring = "d:ht:uV";
- struct option longopts[]={
- { "dev-mem", required_argument, NULL, 'd' },
- { "help", no_argument, NULL, 'h' },
- { "type", required_argument, NULL, 't' },
- { "dump", no_argument, NULL, 'u' },
- { "version", no_argument, NULL, 'V' },
- { 0, 0, 0, 0 }
- };
-
- while((option=getopt_long(argc, argv, optstring, longopts, NULL))!=-1)
- switch(option)
- {
- case 'd':
- opt.devmem=optarg;
- break;
- case 'h':
- opt.flags|=FLAG_HELP;
- break;
- case 't':
- opt.type=parse_opt_type(opt.type, optarg);
- if(opt.type==NULL)
- return -1;
- break;
- case 'u':
- opt.flags|=FLAG_DUMP;
- break;
- case 'V':
- opt.flags|=FLAG_VERSION;
- break;
- case ':':
- case '?':
- return -1;
- }
-
- return 0;
-}
-
-static void print_help(void)
-{
- static const char *help=
- "Usage: dmidecode [OPTIONS]\n"
- "Options are:\n"
- " -d, --dev-mem FILE Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n"
- " -h, --help Display this help text and exit\n"
- " -t, --type T1[,T2...] Only display the entries of given type(s)\n"
- " -u, --dump Do not decode the entries\n"
- " -V, --version Display the version and exit\n";
-
- printf("%s", help);
-}
-
int main(int argc, char * const argv[])
{
int ret=0; /* Returned value */
diff --git a/dmiopt.c b/dmiopt.c
new file mode 100644
index 0000000..9e03abf
--- /dev/null
+++ b/dmiopt.c
@@ -0,0 +1,131 @@
+/*
+ * Command line handling of dmidecode
+ * This file is part of the dmidecode project.
+ *
+ * (C) 2005 Jean Delvare <khali@linux-fr.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
+
+#include "config.h"
+#include "types.h"
+#include "dmiopt.h"
+
+/* Options are global */
+struct opt opt;
+
+static u8 *parse_opt_type(u8 *p, const char *arg)
+{
+ /* Allocate memory on first call only */
+ if(p==NULL)
+ {
+ p=(u8 *)calloc(256, sizeof(u8));
+ if(p==NULL)
+ {
+ perror("calloc");
+ return NULL;
+ }
+ }
+
+ while(*arg!='\0')
+ {
+ unsigned long val;
+ char *next;
+
+ val=strtoul(arg, &next, 0);
+ if(next==arg)
+ {
+ fprintf(stderr, "Invalid type: %s\n", arg);
+ goto exit_free;
+ }
+ if(val>0xff)
+ {
+ fprintf(stderr, "Invalid type: %lu\n", val);
+ goto exit_free;
+ }
+
+ p[val]=1;
+ arg=next;
+ while(*arg==',' || *arg==' ')
+ arg++;
+ }
+
+ return p;
+
+exit_free:
+ free(p);
+ return NULL;
+}
+
+/* Return -1 on error, 0 on success */
+int parse_command_line(int argc, char * const argv[])
+{
+ int option;
+ const char *optstring = "d:ht:uV";
+ struct option longopts[]={
+ { "dev-mem", required_argument, NULL, 'd' },
+ { "help", no_argument, NULL, 'h' },
+ { "type", required_argument, NULL, 't' },
+ { "dump", no_argument, NULL, 'u' },
+ { "version", no_argument, NULL, 'V' },
+ { 0, 0, 0, 0 }
+ };
+
+ while((option=getopt_long(argc, argv, optstring, longopts, NULL))!=-1)
+ switch(option)
+ {
+ case 'd':
+ opt.devmem=optarg;
+ break;
+ case 'h':
+ opt.flags|=FLAG_HELP;
+ break;
+ case 't':
+ opt.type=parse_opt_type(opt.type, optarg);
+ if(opt.type==NULL)
+ return -1;
+ break;
+ case 'u':
+ opt.flags|=FLAG_DUMP;
+ break;
+ case 'V':
+ opt.flags|=FLAG_VERSION;
+ break;
+ case ':':
+ case '?':
+ return -1;
+ }
+
+ return 0;
+}
+
+void print_help(void)
+{
+ static const char *help=
+ "Usage: dmidecode [OPTIONS]\n"
+ "Options are:\n"
+ " -d, --dev-mem FILE Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n"
+ " -h, --help Display this help text and exit\n"
+ " -t, --type T1[,T2...] Only display the entries of given type(s)\n"
+ " -u, --dump Do not decode the entries\n"
+ " -V, --version Display the version and exit\n";
+
+ printf("%s", help);
+}
diff --git a/dmiopt.h b/dmiopt.h
new file mode 100644
index 0000000..7ab0b70
--- /dev/null
+++ b/dmiopt.h
@@ -0,0 +1,35 @@
+/*
+ * Command line handling of dmidecode
+ * This file is part of the dmidecode project.
+ *
+ * (C) 2005 Jean Delvare <khali@linux-fr.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+struct opt
+{
+ const char* devmem;
+ unsigned int flags;
+ u8 *type;
+};
+extern struct opt opt;
+
+#define FLAG_VERSION (1<<0)
+#define FLAG_HELP (1<<1)
+#define FLAG_DUMP (1<<2)
+
+int parse_command_line(int argc, char * const argv[]);
+void print_help(void);