00001 /* 00002 File: ArgParse.h 00003 00004 Function: Argument parsing package 00005 00006 Author: Paul Heckbert 00007 00008 Copyright: 00009 */ 00010 00011 #ifndef __ArgParse__ 00012 #define __ArgParse__ 00013 00014 #include <stdio.h> 00015 00016 struct ArgForm 00017 { 00018 /* a "form" contains the format, doc string, and miscellaneous internal */ 00019 /* info about an argument. It's an argument descriptor, basically */ 00020 00021 ArgForm *next; /* next in linked list */ 00022 char *format; /* scanf-style format: "-size %d %F" */ 00023 char *flag; /* flag portion of format:"-size" */ 00024 char *code; /* just the format codes: "dF" */ 00025 char *doc; /* documentation string: "set widget size" */ 00026 short type; /* REGULAR | SIMPFLAG | PARAMFLAG | 00027 SUBRFLAG | SUBLISTFLAG | NOP */ 00028 short nparam; /* number of parameters to flag */ 00029 int parammask; /* bit i says ok to stop before param i, i=0..*/ 00030 int **param; /* parameter pointer list */ 00031 int (*subr)(int argc, char *argv[]); /* subroutine to call for action (if any) */ 00032 ArgForm *sublist; /* subordinate list (if any) */ 00033 short rep; /* # times this flag repeated in arglist */ 00034 }; 00035 00036 typedef ArgForm Arg_form; 00037 00038 /* form type values */ 00039 enum ArgTypes 00040 { 00041 ARG_REGULAR = 1, /* a regular argument */ 00042 ARG_SIMPFLAG = 2, /* a simple flag (no parameters) */ 00043 ARG_PARAMFLAG = 3, /* a flag with parameters */ 00044 ARG_SUBRFLAG = 4, /* a flag with subroutine action */ 00045 ARG_SUBLISTFLAG = 5, /* a sub-formlist */ 00046 ARG_NOP = 6, /* no arg or flag, just a doc string */ 00047 }; 00048 00049 /* the following must be impossible pointer values (note: machine-dependent) */ 00050 enum ArgNextTypes 00051 { 00052 ARG_MASKNEXT = 0x80000000, /* mask for these NEXT flags */ 00053 ARG_FLAGNEXT = 0x80000001, 00054 ARG_SUBRNEXT = 0x80000002, 00055 ARG_LISTNEXT = 0x80000003, 00056 }; 00057 00058 /* varargs tricks */ 00059 #define ARG_FLAG(ptr) ARG_FLAGNEXT, (ptr) /* for SIMPFLAG */ 00060 #define ARG_SUBR(ptr) ARG_SUBRNEXT, (ptr) /* for SUBRFLAG */ 00061 #define ARG_SUBLIST(ptr) ARG_LISTNEXT, (ptr) /* for SUBLISTFLAG */ 00062 00063 /* error codes: BADCALL is a programmer error, the others are user errors */ 00064 enum ArgError 00065 { 00066 ARG_BADCALL = -1, /* arg_parse call itself is bad */ 00067 ARG_BADARG = -2, /* bad argument given */ 00068 ARG_MISSING = -3, /* argument or parameter missing */ 00069 ARG_EXTRA = -4, /* extra argument given */ 00070 }; 00071 00072 #define ARG_NARGMAX 10000 /* max number of allowed args */ 00073 00074 extern int arg_debug, arg_doccol; 00075 extern int arg_warning; /* print warnings about repeated flags? */ 00076 00077 int arg_parse(int ac, char **av, ...); 00078 int arg_parse_argv(int ac, char **av, ArgForm *form); 00079 int arg_parse_stream(FILE *fp, ArgForm *form); 00080 ArgForm *arg_to_form(int fish, ...); 00081 /* due to limitations of stdarg.h, you must pass in the 00082 * number of fish you own as the first argument to 00083 * arg_to_form */ 00084 void arg_form_print(ArgForm *form); 00085 void arg_form_append(ArgForm *form, ArgForm *additionalForm); 00086 00087 #endif