Generated from configure.in
[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 extern int args_callback_option(char*,char*);
26 extern int args_callback_longoption(char*,char*);
27 extern int args_callback_command(char*,char*);
28 extern void args_callback_usage(char*name);
29
30 //int argn;
31 //char**argv;
32
33 void processargs(int argn2,char**argv2)
34 {
35     int t;
36     if(argn2==1)
37     {
38         args_callback_usage(argv2[0]);
39         exit(1);
40     }
41     for(t=1;t<argn2;t++)
42     {
43         char*next;
44         if(t<argn2-1) next=argv2[t+1];
45         else        next=0;
46
47         if(argv2[t][0]=='-')
48         {
49             if(argv2[t][1]=='-')
50             {
51                 if(!strcmp(&argv2[t][2],"help")) 
52                 {
53                     args_callback_usage(argv2[0]);
54                     exit(1);
55                 }
56                 t+=args_callback_longoption(&argv2[t][2],next);
57             }
58             else
59             {
60                 if(strchr("?h",argv2[t][1]))
61                 {
62                     args_callback_usage(argv2[0]);
63                     exit(1);
64                 }
65                 if(argv2[t][1]) // this might be something like e.g. -xvf
66                 {
67                     char buf[2];
68                     int s=1;
69                     int ret;
70                     buf[1]=0;
71                     do{
72                         if(argv2[t][s+1]) {
73                           buf[0] = argv2[t][s];
74                           ret = args_callback_option(buf,&argv2[t][s+1]);
75                         }
76                         else {
77                           t+= args_callback_option(&argv2[t][s], next);
78                           break;
79                         }
80                         s++;
81                     } while(!ret);
82                 }
83                 else // - usually means "read stdout"
84                 {
85                     t+=args_callback_option(&argv2[t][1],next);
86                 }
87             }
88         }
89         else
90         {
91             t+=args_callback_command(argv2[t],next);
92         }
93     }
94 }
95
96 struct options_t
97 {
98     char*shortoption;
99     char*longoption;
100 };
101
102 int args_long2shortoption(struct options_t*options, char*name, char*val)
103 {
104     char*equal = strchr(name,'=');
105     if (equal) {
106         *equal = 0;
107         equal++;
108     }
109     while(options->shortoption) {
110         if(!strcmp(options->longoption, name)) {
111                 char*tmp = (char*)malloc(strlen(options->shortoption)
112                         +(equal?strlen(equal)+2:2));
113                 strcpy(tmp, options->shortoption);
114                 if(equal) {
115                     //strcpy(&tmp[strlen(tmp)], equal);
116                     int ret = args_callback_option(tmp, equal);
117                     if(!ret) {
118                         fprintf(stderr, "Warning: Option --%s takes no parameter.\n", name);
119                     }
120                     return 0;
121                 }
122                 return args_callback_option(tmp,val);
123         }
124         options++;
125     }
126     fprintf(stderr, "Unknown option: --%s\n", name);
127     exit(1);
128 }
129
130 /* check whether the value t is in a given range.
131   examples: 3 is in range 1-10: true
132             7 is in range 2-4,6,8-10: false
133             9 is in range 1,2,3-12: true
134 */
135 char is_in_range(int t, char*irange)
136 {
137     char*pos = irange;
138     char*digits;
139     int num;
140     char range = 0;
141     int last=0;
142     char tmp;
143
144     if(!irange)  // no range resembles (-OO,OO)
145         return 1;
146
147     while(*pos)
148     {
149         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
150             pos++;
151
152         digits = pos;
153         while(*digits>='0' && *digits<='9')
154             digits++;
155         if(digits == pos) {
156             fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange);
157             exit(1);
158         }
159         
160         tmp=*digits;*digits=0;
161         num = atoi(pos);
162         *digits=tmp;
163         pos = digits;
164
165         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
166             pos++;
167
168         if(range && last<=t && num>=t)
169             return 1;
170         if(range) {
171             range = 0;
172             if(*pos)
173              pos ++;
174             continue;
175         }
176
177         if(*pos=='-')
178         {
179             if(range) {
180                 fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange);
181                 exit(1);
182             }
183             last = num;
184             range = 1;
185             if(*pos)
186              pos ++;
187             continue;
188         } 
189         else 
190         {
191             /* if it isn't a '-', we assume it is a seperator like
192                ',', ';', ':', whatever. */
193             if(t == num)
194                 return 1;
195             if(*pos)
196              pos ++;
197             continue;
198         }
199     }
200     if(range && last<=t)
201         return 1;
202     return 0;
203 }
204
205 #endif //__args_h__