2 Routines to simplify argument handling
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
13 extern int args_callback_option(char*,char*);
14 extern int args_callback_longoption(char*,char*);
15 extern int args_callback_command(char*,char*);
16 extern void args_callback_usage(char*name);
21 void processargs(int argn2,char**argv2)
26 args_callback_usage(argv2[0]);
32 if(t<argn2-1) next=argv2[t+1];
39 if(!strcmp(&argv2[t][2],"help"))
41 args_callback_usage(argv2[0]);
44 t+=args_callback_longoption(&argv2[t][2],next);
48 if(strchr("?h",argv2[t][1]))
50 args_callback_usage(argv2[0]);
53 if(argv2[t][1]) // this might be something like e.g. -xvf
62 ret = args_callback_option(buf,&argv2[t][s+1]);
65 t+= args_callback_option(&argv2[t][s], next);
71 else // - usually means "read stdout"
73 t+=args_callback_option(&argv2[t][1],next);
79 t+=args_callback_command(argv2[t],next);
90 int args_long2shortoption(struct options_t*options, char*name, char*val)
92 char*equal = strchr(name,'=');
97 while(options->shortoption) {
98 if(!strcmp(options->longoption, name)) {
99 char*tmp = (char*)malloc(strlen(options->shortoption)
100 +(equal?strlen(equal)+2:2));
101 strcpy(tmp, options->shortoption);
103 //strcpy(&tmp[strlen(tmp)], equal);
104 int ret = args_callback_option(tmp, equal);
106 fprintf(stderr, "Warning: Option --%s takes no parameter.\n", name);
110 return args_callback_option(tmp,val);
114 fprintf(stderr, "Unknown option: --%s\n", name);
118 /* check whether the value t is in a given range.
119 examples: 3 is in range 1-10: true
120 7 is in range 2-4,6,8-10: false
121 9 is in range 1,2,3-12: true
123 char is_in_range(int t, char*irange)
132 if(!irange) // no range resembles (-OO,OO)
137 while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
141 while(*digits>='0' && *digits<='9')
144 fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange);
148 tmp=*digits;*digits=0;
153 while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
156 if(range && last<=t && num>=t)
168 fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange);
179 /* if it isn't a '-', we assume it is a seperator like
180 ',', ';', ':', whatever. */