1 /* 2 * (c) Copyright 1990, Kim Fabricius Storm. All rights reserved. 3 * Copyright (c) 1996-2003 Michael T Pins. All rights reserved. 4 * 5 * Include file for generic option parsing 6 */ 7 8 /* 9 * To use this routine, you must a table called an Option_Description. 10 * Each element in this table describes one possible option: 11 * Its option letter 12 * Its argument type (if any) 13 * Whether an argument is mandatory or optional 14 * The address of the variable holding the option value 15 * The defualt value if argument is optional 16 * 17 * Example: 18 * 19 * A program accepts the following options: 20 * -a [no value] 21 * -b N [a numeric value] 22 * -p [N] [an optional numeric value] 23 * -t S [a string value] 24 * 25 * The corresponding option description table would then look like: 26 * 27 * #include <options.h> 28 * int a_flg = 1, b_value = 0, p_value = 0; 29 * char *t_string = "default"; 30 * 31 * Option_Description( options ) { 32 * 'a', Bool_Option(a_flg), 33 * 'b', Int_Option(b_value), 34 * 'p', Int_Option_Optional(p_value, -1), 35 * 't', String_Option(t_string), 36 * '\0', 37 * } 38 * To parse the argument list - and the contents of the environment variable 39 * XXINIT, all that has to be done is to issue the following call: 40 * 41 * files = parse_options(argc, argv, "XXINIT", options, NULL); 42 * 43 * If no environment variable is associated with the program, use NULL as 44 * the third parameter. 45 * 46 * Upon return, the elements argv[1] .. argv[files] will contain 47 * the file names (and other 'non-options') that occur in the argument list. 48 * 49 * The last NULL argument may be replaced by your own 'usage routine' 50 * which will be called in the following way: 51 * 52 * usage(pname) 53 * char *pname; /+ argv[0] without path +/ 54 * 55 * 56 * char *program_name(argv) 57 * 58 * return a pointer to the last component of argv[0] (the program name with 59 * with the path deleted). 60 * 61 62 */ 63 64 #ifndef _NN_OPTIONS_H 65 #define _NN_OPTIONS_H 1 66 67 struct option_descr { 68 char option_letter; 69 char option_type; 70 char **option_address; 71 char *option_default; 72 }; 73 74 75 #define Option_Description(name) \ 76 struct option_descr name[] = 77 78 #define Bool_Option(addr) \ 79 1, (char **)(&addr), (char *)0 80 81 #define String_Option(addr) \ 82 2, &addr, (char *)0 83 84 #define String_Option_Optional(addr, default) \ 85 3, &addr, default 86 87 #define Int_Option(addr) \ 88 4, (char **)(&addr), (char *)0 89 90 #define Int_Option_Optional(addr, default) \ 91 5, (char **)(&addr), (char *)default 92 93 94 char *program_name(char **); 95 void parseargv_variables(char **, int); 96 int parse_options(int, char **, char *, struct option_descr[], char *); 97 #endif /* _NN_OPTIONS_H */ 98