1 /***************************************************************************
2  *   S3m/Mod player by Daniel Marks (dmarks@ais.net)
3  *   GUS support by David Jeske (jeske@uiuc.edu)
4  *
5  * (C) 1994,1995 By Daniel Marks and David Jeske
6  *
7  * While we retain the copyright to this code, this source code is FREE.
8  * You may use it in any way you wish, in any product you wish. You may
9  * NOT steal the copyright for this code from us.
10  *
11  * We respectfully ask that you email one of us, if possible, if you
12  * produce something significant with this code, or if you have any bug
13  * fixes to contribute.  We also request that you give credit where
14  * credit is due if you include part of this code in a program of your own.
15  *
16  * Email: s3mod@uiuc.edu
17  *        jeske@uiuc.edu
18  *
19  * See the associated README file for Thanks
20  ***************************************************************************
21  *
22  * cmdline.c  -  commands for parsing command line input
23  *
24  */
25 
26 
27 #include "config.h"
28 #include "cmdline.h"
29 
30 
31 char *str_parms=0;
32 char *flag_parms=0;
33 char *num_parms=0;
34 
35 int               t_argc;
36 char            **t_argv;
37 
38 char *null_string = "";
39 
parm_setup(int argc,char ** argv,char * str_prms,char * flag_prms,char * num_prms)40 int parm_setup(int argc,char **argv,char *str_prms,char *flag_prms,
41 		char *num_prms)
42 {
43   t_argc = argc;
44   t_argv = argv;
45 
46   if (str_prms)
47     str_parms = str_prms;
48   else
49     str_parms = null_string;
50   if (flag_prms)
51     flag_parms = flag_prms;
52   else
53     flag_parms = null_string;
54   if (num_prms)
55     num_parms = num_prms;
56   else
57     num_parms = null_string;
58 
59   return(0);
60 }
61 
62 
63 
64 
rnp_strrchr(char * in_string,char letter)65 char *rnp_strrchr(char *in_string, char letter)
66 {
67   if (!in_string)
68     return (NULL);
69   while (*in_string)
70     if (*in_string == letter)
71       return (in_string);
72       else in_string++;
73   return (NULL);
74 }
75 
rnp_get_num(char ** string,long int * number)76 int rnp_get_num(char **string, long int *number)
77 {
78   char ch;
79   char neg = 0;
80   long int num = 0;
81   int error_code = -1;
82 
83   if (**string == '-')
84   {
85     (*string)++;
86     neg = 1;
87   }
88   if (**string == '0')
89   {
90     (*string)++;
91     ch = *(*string)++;
92     if (ch > 'Z') ch -= ' ';
93     if (ch == 'B')
94     {
95       while ((**string == '0') || (**string == '1'))
96       {
97         num = (num << 1) | (*(*string)++ & 0x01);
98         error_code = 0;
99       }
100       if (neg) num = -num;
101       *number = num;
102       return (error_code);
103     }
104     if (ch == 'O')
105     {
106       while ((**string >= '0') && (**string <= '7'))
107       {
108         error_code = 0;
109         num = (num << 3) | (*(*string)++ & 0x07);
110       }
111       if (neg) num = -num;
112       *number = num;
113       return (error_code);
114     }
115     if (ch == 'X')
116     {
117       while (ch = **string, ch = (ch>'Z') ? ch-(' '+48) : ch-48,
118              ch = (ch>9) ? ch-7 : ch, ((ch >= 0) && (ch <= 15)))
119       {
120         error_code = 0;
121         num = (num << 4) | ch;
122         (*string)++;
123       }
124       if (neg) num = -num;
125       *number = num;
126       return (error_code);
127     }
128     (*string) -= 2;
129   }
130   while ((**string >= '0') && (**string <= '9'))
131   {
132     error_code = 0;
133     num = (num * 10) + (*(*string)++ - 48);
134   }
135   if (neg) num = -num;
136   *number = num;
137   return (error_code);
138 }
139 
140 
141 /* My own favorite GETOPT substitute */
142 /* letter <= 32 means get non-flagged regular parameter # letter */
143 /* num_types  = letters that take numerical parameters */
144 /* str_types  = letters that take string parameters */
145 /* flag_types = letters that take no parameters */
146 /* str_value  = returned string value */
147 /* num_value  = returned numerical value */
148 /* argv/argc  = parameter list / parameter count */
149 
read_next_parm(char letter,char * num_types,char * str_types,char * flag_types,char ** str_value,long int * num_value,int argc,char ** argv)150 int read_next_parm(char letter,
151                    char *num_types, char *str_types, char *flag_types,
152                    char **str_value, long int *num_value,
153                    int argc, char **argv)
154 {
155   int found = 0;
156   char *cparm;
157   char *str_parm;
158   long int val;
159   char ch;
160 
161   argc--;
162   argv++;
163   while (argc > 0)
164   {
165     argc--;
166     cparm = *argv++;
167 
168     if (*cparm == '-')
169     {
170       cparm++;
171       while (*cparm)
172       {
173         found = (((ch = *cparm) == letter) && (letter > 32)) ? 1 : 0;
174         cparm++;
175         if (rnp_strrchr(flag_types,ch))
176         {
177           if (found)
178             return 1;
179         }
180         if (rnp_strrchr(str_types,ch))
181         {
182           if (*cparm)
183              str_parm = cparm;
184              else
185              {
186                if (argc)
187                {
188                  argc--;
189                  str_parm = *argv;
190                  if (str_parm)
191                  {
192                    if (*str_parm == '-')
193                      str_parm = NULL;
194                      else argv++;
195                  }
196                } else str_parm = NULL;
197              }
198           while (*cparm) cparm++;
199           if (found)
200           {
201             if (!str_parm)
202                return (-1);
203             *str_value = str_parm;
204             return (1);
205           }
206         }
207         if (rnp_strrchr(num_types,ch))
208         {
209           if (*cparm)
210              str_parm = cparm;
211              else
212              {
213                if (argc)
214                {
215                  argc--;
216                  str_parm = *argv;
217                  if (str_parm)
218                  {
219                    if (*str_parm == '-')
220                      str_parm = NULL;
221                      else argv++;
222                  }
223                } else str_parm = NULL;
224              }
225           cparm++;
226           rnp_get_num(&cparm,&val);
227           if (found)
228           {
229             if (!str_parm)
230               return (-1);
231             if (rnp_get_num(&str_parm,num_value))
232               return (-1);
233             return (1);
234           }
235         }
236       }
237     } else
238     {
239       if (letter <= 32)
240       {
241         if (letter--,!(letter))
242         {
243           *str_value = cparm;
244           return (1);
245         }
246       }
247     }
248   }
249   if (letter <= 32)
250   {
251     *str_value = NULL;
252     return (0);
253   }
254   if (rnp_strrchr(flag_types,letter))
255     return (0);
256   if (rnp_strrchr(str_types,letter))
257     return (0);
258   if (rnp_strrchr(num_types,letter))
259     return (0);
260   return (-1);
261 }
262 
read_parm(char letter,char ** str_value,long int * num_value)263 int read_parm(char letter, char **str_value, long int *num_value)
264 {
265   if (read_next_parm(letter, num_parms, str_parms, flag_parms,
266              str_value, num_value, t_argc, t_argv) == 1)
267      return (1);
268   return (read_next_parm(letter+' ', num_parms, str_parms, flag_parms,
269              str_value, num_value, t_argc, t_argv) == 1);
270 }
271 
272 
273 
274 
275