added NO_ARGPARSER option
[swftools.git] / lib / args.h
1 /* args.h
2    Routines to simplify argument handling
3
4    Part of the swftools package.
5
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #ifndef __args_h__
23 #define __args_h__
24
25 #ifndef NO_ARGPARSER
26
27 extern int args_callback_option(char*,char*);
28 extern int args_callback_longoption(char*,char*);
29 extern int args_callback_command(char*,char*);
30 extern void args_callback_usage(char*name);
31
32 //int argn;
33 //char**argv;
34
35 static void processargs(int argn2,char**argv2)
36 {
37     int t;
38     if(argn2==1)
39     {
40         args_callback_usage(argv2[0]);
41         exit(1);
42     }
43     for(t=1;t<argn2;t++)
44     {
45         char*next;
46         if(t<argn2-1) next=argv2[t+1];
47         else        next=0;
48
49         if(argv2[t][0]=='-')
50         {
51             if(argv2[t][1]=='-')
52             {
53                 if(!strcmp(&argv2[t][2],"help")) 
54                 {
55                     args_callback_usage(argv2[0]);
56                     exit(1);
57                 }
58                 t+=args_callback_longoption(&argv2[t][2],next);
59             }
60             else
61             {
62                 if(strchr("?h",argv2[t][1]))
63                 {
64                     args_callback_usage(argv2[0]);
65                     exit(1);
66                 }
67                 if(argv2[t][1]) // this might be something like e.g. -xvf
68                 {
69                     char buf[2];
70                     int s=1;
71                     int ret;
72                     buf[1]=0;
73                     do{
74                         if(argv2[t][s+1]) {
75                           buf[0] = argv2[t][s];
76                           ret = args_callback_option(buf,&argv2[t][s+1]);
77                         }
78                         else {
79                           t+= args_callback_option(&argv2[t][s], next);
80                           break;
81                         }
82                         s++;
83                     } while(!ret);
84                 }
85                 else // - usually means "read stdout"
86                 {
87                     t+=args_callback_option(&argv2[t][1],next);
88                 }
89             }
90         }
91         else
92         {
93             t+=args_callback_command(argv2[t],next);
94         }
95     }
96 }
97
98 struct options_t
99 {
100     char*shortoption;
101     char*longoption;
102 };
103
104 static int args_long2shortoption(struct options_t*options, char*name, char*val)
105 {
106     char*equal = strchr(name,'=');
107     if (equal) {
108         *equal = 0;
109         equal++;
110     }
111     while(options->shortoption) {
112         if(!strcmp(options->longoption, name)) {
113                 char*tmp = (char*)malloc(strlen(options->shortoption)
114                         +(equal?strlen(equal)+2:2));
115                 strcpy(tmp, options->shortoption);
116                 if(equal) {
117                     //strcpy(&tmp[strlen(tmp)], equal);
118                     int ret = args_callback_option(tmp, equal);
119                     if(!ret) {
120                         fprintf(stderr, "Warning: Option --%s takes no parameter.\n", name);
121                     }
122                     return 0;
123                 }
124                 return args_callback_option(tmp,val);
125         }
126         options++;
127     }
128     fprintf(stderr, "Unknown option: --%s\n", name);
129     exit(1);
130 }
131
132 #endif
133
134 /* check whether the value t is in a given range.
135   examples: 3 is in range 1-10: true
136             7 is in range 2-4,6,8-10: false
137             9 is in range 1,2,3-12: true
138 */
139 static char is_in_range(int t, char*irange)
140 {
141     char*pos = irange;
142     char*digits;
143     int num;
144     char range = 0;
145     int last=0;
146     char tmp;
147
148     if(!irange)  // no range resembles (-OO,OO)
149         return 1;
150
151     while(*pos)
152     {
153         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
154             pos++;
155
156         digits = pos;
157         while(*digits>='0' && *digits<='9')
158             digits++;
159         if(digits == pos) {
160             fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange);
161             exit(1);
162         }
163         
164         tmp=*digits;*digits=0;
165         num = atoi(pos);
166         *digits=tmp;
167         pos = digits;
168
169         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
170             pos++;
171
172         if(range && last<=t && num>=t)
173             return 1;
174         if(range) {
175             range = 0;
176             if(*pos)
177              pos ++;
178             continue;
179         }
180
181         if(*pos=='-')
182         {
183             if(range) {
184                 fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange);
185                 exit(1);
186             }
187             last = num;
188             range = 1;
189             if(*pos)
190              pos ++;
191             continue;
192         } 
193         else 
194         {
195             /* if it isn't a '-', we assume it is a seperator like
196                ',', ';', ':', whatever. */
197             if(t == num)
198                 return 1;
199             if(*pos)
200              pos ++;
201             continue;
202         }
203     }
204     if(range && last<=t)
205         return 1;
206     return 0;
207 }
208
209 #endif //__args_h__