File: cmdline.c

package info (click to toggle)
s3mod 1.09-10
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 248 kB
  • ctags: 354
  • sloc: ansic: 2,856; makefile: 153
file content (274 lines) | stat: -rw-r--r-- 6,315 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/***************************************************************************
 *   S3m/Mod player by Daniel Marks (dmarks@ais.net)
 *   GUS support by David Jeske (jeske@uiuc.edu)
 *
 * (C) 1994,1995 By Daniel Marks and David Jeske
 *
 * While we retain the copyright to this code, this source code is FREE.
 * You may use it in any way you wish, in any product you wish. You may 
 * NOT steal the copyright for this code from us.
 *
 * We respectfully ask that you email one of us, if possible, if you
 * produce something significant with this code, or if you have any bug 
 * fixes to contribute.  We also request that you give credit where
 * credit is due if you include part of this code in a program of your own.
 *                                                 
 * Email: s3mod@uiuc.edu
 *        jeske@uiuc.edu
 *
 * See the associated README file for Thanks
 ***************************************************************************
 *
 * cmdline.c  -  commands for parsing command line input
 * 
 */


#include "config.h"
#include "cmdline.h"


char *str_parms=0;
char *flag_parms=0;
char *num_parms=0;

int               t_argc;
char            **t_argv;

char *null_string = "";

int parm_setup(int argc,char **argv,char *str_prms,char *flag_prms,
		char *num_prms)
{
  t_argc = argc;
  t_argv = argv;
  
  if (str_prms)
    str_parms = str_prms;
  else
    str_parms = null_string;
  if (flag_prms)
    flag_parms = flag_prms;
  else
    flag_parms = null_string;
  if (num_prms)
    num_parms = num_prms;
  else
    num_parms = null_string;

  return(0);
}




char *rnp_strrchr(char *in_string, char letter)
{
  if (!in_string)
    return (NULL);
  while (*in_string)
    if (*in_string == letter)
      return (in_string);
      else in_string++;
  return (NULL);
}

int rnp_get_num(char **string, long int *number)
{
  char ch;
  char neg = 0;
  long int num = 0;
  int error_code = -1;

  if (**string == '-')
  {
    (*string)++;
    neg = 1;
  }
  if (**string == '0')
  {
    (*string)++;
    ch = *(*string)++;
    if (ch > 'Z') ch -= ' ';
    if (ch == 'B')
    {
      while ((**string == '0') || (**string == '1'))
      {
        num = (num << 1) | (*(*string)++ & 0x01);
        error_code = 0;
      }
      if (neg) num = -num;
      *number = num;
      return (error_code);
    }
    if (ch == 'O')
    {
      while ((**string >= '0') && (**string <= '7'))
      {
        error_code = 0;
        num = (num << 3) | (*(*string)++ & 0x07);
      }
      if (neg) num = -num;
      *number = num;
      return (error_code);
    }
    if (ch == 'X')
    {
      while (ch = **string, ch = (ch>'Z') ? ch-(' '+48) : ch-48,
             ch = (ch>9) ? ch-7 : ch, ((ch >= 0) && (ch <= 15)))
      {
        error_code = 0;
        num = (num << 4) | ch;
        (*string)++;
      }
      if (neg) num = -num;
      *number = num;
      return (error_code);
    }
    (*string) -= 2;
  }
  while ((**string >= '0') && (**string <= '9'))
  {
    error_code = 0;
    num = (num * 10) + (*(*string)++ - 48);
  }
  if (neg) num = -num;
  *number = num;
  return (error_code);
}


/* My own favorite GETOPT substitute */
/* letter <= 32 means get non-flagged regular parameter # letter */
/* num_types  = letters that take numerical parameters */
/* str_types  = letters that take string parameters */
/* flag_types = letters that take no parameters */
/* str_value  = returned string value */
/* num_value  = returned numerical value */
/* argv/argc  = parameter list / parameter count */

int 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)
{
  int found = 0;
  char *cparm;
  char *str_parm;
  long int val;
  char ch;

  argc--;
  argv++;
  while (argc > 0)
  {
    argc--;
    cparm = *argv++;

    if (*cparm == '-')
    {
      cparm++;
      while (*cparm)
      {
        found = (((ch = *cparm) == letter) && (letter > 32)) ? 1 : 0;
        cparm++;
        if (rnp_strrchr(flag_types,ch))
        {
          if (found)
            return 1;
        }
        if (rnp_strrchr(str_types,ch))
        {
          if (*cparm)
             str_parm = cparm;
             else
             {
               if (argc)
               {
                 argc--;
                 str_parm = *argv;
                 if (str_parm)
                 {
                   if (*str_parm == '-')
                     str_parm = NULL;
                     else argv++;
                 }
               } else str_parm = NULL;
             }
          while (*cparm) cparm++;
          if (found)
          {
            if (!str_parm)
               return (-1);
            *str_value = str_parm;
            return (1);
          }
        }
        if (rnp_strrchr(num_types,ch))
        {
          if (*cparm)
             str_parm = cparm;
             else
             {
               if (argc)
               {
                 argc--;
                 str_parm = *argv;
                 if (str_parm)
                 {
                   if (*str_parm == '-')
                     str_parm = NULL;
                     else argv++;
                 }
               } else str_parm = NULL;
             }
          cparm++;
          rnp_get_num(&cparm,&val);
          if (found)
          {
            if (!str_parm)
              return (-1);
            if (rnp_get_num(&str_parm,num_value))
              return (-1);
            return (1);
          }
        }
      }
    } else
    {
      if (letter <= 32)
      {
        if (letter--,!(letter))
        {
          *str_value = cparm;
          return (1);
        }
      }
    }
  }
  if (letter <= 32)
  {
    *str_value = NULL;
    return (0);
  }
  if (rnp_strrchr(flag_types,letter))
    return (0);
  if (rnp_strrchr(str_types,letter))
    return (0);
  if (rnp_strrchr(num_types,letter))
    return (0);
  return (-1);
}

int read_parm(char letter, char **str_value, long int *num_value)
{
  if (read_next_parm(letter, num_parms, str_parms, flag_parms,
             str_value, num_value, t_argc, t_argv) == 1)
     return (1);
  return (read_next_parm(letter+' ', num_parms, str_parms, flag_parms,
             str_value, num_value, t_argc, t_argv) == 1);
}