| 1 | /* |
| 2 | ** petopt.h - Command line argument parser header file |
| 3 | ** |
| 4 | ** Copyright (c) 1999 Peter Eriksson <[email protected]> |
| 5 | ** |
| 6 | ** This program is free software; you can redistribute it and/or |
| 7 | ** modify it as you wish - as long as you don't claim that you wrote |
| 8 | ** it. |
| 9 | ** |
| 10 | ** This program is distributed in the hope that it will be useful, |
| 11 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | */ |
| 14 | |
| 15 | #ifndef PETOPT_H |
| 16 | #define PETOPT_H |
| 17 | |
| 18 | /* Option types and flags */ |
| 19 | #define POF_TYPEMASK 0x00FF |
| 20 | |
| 21 | #define POF_NONE 0x0000 |
| 22 | #define POF_INT 0x0001 /* Argument is an 'int' number */ |
| 23 | #define POF_STR 0x0002 /* Argument is a 'char *' string */ |
| 24 | #define POF_BOOL 0x0003 /* Argument is a 'int' boolean */ |
| 25 | |
| 26 | #define POF_OPT 0x0100 /* Argument is optional */ |
| 27 | #define POF_DUP 0x0200 /* strdup() the assigned var (if string) */ |
| 28 | |
| 29 | /* Global flags */ |
| 30 | #define POF_SYSLOG 0x1000 /* Send errors to syslog */ |
| 31 | #define POF_NOERRPRINT 0x2000 /* Do not print errors to stderr */ |
| 32 | #define POF_PRDEFAULT 0x4000 /* Print default values in usage text */ |
| 33 | |
| 34 | /* Error returns */ |
| 35 | #define POE_EOF -1 |
| 36 | |
| 37 | #define POE_OPTION -2 /* Invalid option */ |
| 38 | #define POE_MULTI -3 /* Option is not unique */ |
| 39 | #define POE_MISSING -4 /* Required argument to option is missing */ |
| 40 | #define POE_INVALID -5 /* Invalid argument to option */ |
| 41 | #define POE_INTERNAL -6 /* Internal error */ |
| 42 | |
| 43 | |
| 44 | typedef struct petopt_option |
| 45 | { |
| 46 | int s; /* Short option character */ |
| 47 | int f; /* Argument flags */ |
| 48 | char *l; /* Long option name */ |
| 49 | void *v; /* Variable pointer */ |
| 50 | char *h; /* Help/usage string */ |
| 51 | } PETOPTS; |
| 52 | |
| 53 | |
| 54 | typedef struct petopt |
| 55 | { |
| 56 | int f; |
| 57 | int ai; |
| 58 | int ci; |
| 59 | int saved_ai; |
| 60 | int saved_ci; |
| 61 | int argc; |
| 62 | const char **argv; |
| 63 | PETOPTS *pov; |
| 64 | int (*parse)(struct petopt *pp, PETOPTS *pov, const char *arg); |
| 65 | int (*errpr)(struct petopt *pp, PETOPTS *pov, int err, FILE *fp); |
| 66 | int oac; |
| 67 | char **oav; |
| 68 | } PETOPT; |
| 69 | |
| 70 | |
| 71 | extern int |
| 72 | petopt_setup(PETOPT **popp, |
| 73 | int f, |
| 74 | int argc, |
| 75 | char **argv, |
| 76 | PETOPTS *pov, |
| 77 | int (*parse)(PETOPT *pop, PETOPTS *pov, const char *arg), |
| 78 | int (*errpr)(PETOPT *pop, PETOPTS *pov, int err, FILE *fp)); |
| 79 | |
| 80 | |
| 81 | extern int |
| 82 | petopt_parse(PETOPT *pop, |
| 83 | int *o_argc, |
| 84 | char ***o_argv); |
| 85 | |
| 86 | extern int |
| 87 | petopt_print_error(PETOPT *pop, |
| 88 | PETOPTS *pov, |
| 89 | int err, |
| 90 | FILE *fp); |
| 91 | |
| 92 | extern int |
| 93 | petopt_print_usage(PETOPT *pop, |
| 94 | FILE *fp); |
| 95 | |
| 96 | extern int |
| 97 | petopt_cleanup(PETOPT *pop); |
| 98 | |
| 99 | #endif |