| 1 |
/* |
| 2 |
getlopt: command line option/parameter parsing |
| 3 |
|
| 4 |
copyright ?-2006 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written Oliver Fromme |
| 7 |
old timestamp: Tue Apr 8 07:13:39 MET DST 1997 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
|
| 14 |
#ifndef _MPG123_GETLOPT_H_ |
| 15 |
#define _MPG123_GETLOPT_H_ |
| 16 |
|
| 17 |
extern int loptind; /* index in argv[] */ |
| 18 |
extern int loptchr; /* index in argv[loptind] */ |
| 19 |
extern char *loptarg; /* points to argument if present, else to option */ |
| 20 |
|
| 21 |
struct topt; |
| 22 |
typedef struct topt topt; |
| 23 |
struct topt { |
| 24 |
char sname; /* short option name, can be 0 */ |
| 25 |
char *lname; /* long option name, can be 0 */ |
| 26 |
int flags; /* see below */ |
| 27 |
void (*func)(char *, topt *); /* called if != 0 (after setting of var) */ |
| 28 |
void *var; /* type is *long, *char or **char, see below */ |
| 29 |
long value; |
| 30 |
}; |
| 31 |
|
| 32 |
/* ThOr: make this clear; distict long from int (since this is != on my Alpha) and really use a flag for every case (spare the 0 case |
| 33 |
for .... no flag) */ |
| 34 |
#define GLO_ARG 1 |
| 35 |
#define GLO_CHAR 2 |
| 36 |
#define GLO_INT 4 |
| 37 |
#define GLO_LONG 8 |
| 38 |
#define GLO_DOUBLE 16 |
| 39 |
/* This is set if getlopt allocates memory for var. */ |
| 40 |
#define GLO_VAR_MEM 32 |
| 41 |
|
| 42 |
/* flags: |
| 43 |
* bit 0 = 0 - no argument |
| 44 |
* if var != NULL |
| 45 |
* *var := value or (char)value [see bit 1] |
| 46 |
* else |
| 47 |
* loptarg = &option |
| 48 |
* return ((value != 0) ? value : sname) |
| 49 |
* bit 0 = 1 - argument required |
| 50 |
* if var != NULL |
| 51 |
* *var := atoi(arg) or strdup(arg) [see bit 1] |
| 52 |
* else |
| 53 |
* loptarg = &arg |
| 54 |
* return ((value != 0) ? value : sname) |
| 55 |
* |
| 56 |
* bit 1 = 1 - var is a pointer to a char (or string), |
| 57 |
* and value is interpreted as char |
| 58 |
* bit 2 = 1 - var is a pointer to int |
| 59 |
* bit 3 = 1 - var is a pointer to long |
| 60 |
* |
| 61 |
* Note: The options definition is terminated by a topt |
| 62 |
* containing only zeroes. |
| 63 |
*/ |
| 64 |
|
| 65 |
#define GLO_END 0 |
| 66 |
#define GLO_UNKNOWN -1 |
| 67 |
#define GLO_NOARG -2 |
| 68 |
#define GLO_CONTINUE -3 |
| 69 |
#define GLO_BADARG -4 |
| 70 |
|
| 71 |
int getlopt (int argc, char *argv[], topt *opts); |
| 72 |
// Helper to set a char parameter, avoiding memory leaks. |
| 73 |
void getlopt_set_char(topt *opts, char *name, char *value); |
| 74 |
/* return values: |
| 75 |
* GLO_END (0) end of options |
| 76 |
* GLO_UNKNOWN (-1) unknown option *loptarg |
| 77 |
* GLO_NOARG (-2) missing argument |
| 78 |
* GLO_CONTINUE (-3) (reserved for internal use) |
| 79 |
* else - return value according to flags (see above) |
| 80 |
*/ |
| 81 |
|
| 82 |
|
| 83 |
#endif |