File: cmdline.c

package info (click to toggle)
spass 3.7-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,500 kB
  • ctags: 5,981
  • sloc: ansic: 50,634; yacc: 3,038; sh: 1,072; lex: 430; perl: 407; makefile: 394
file content (456 lines) | stat: -rw-r--r-- 13,140 bytes parent folder | download | duplicates (3)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
 /**************************************************************/
/* ********************************************************** */
/* *                                                        * */
/* *                COMMAND LINE PARSER                     * */
/* *                                                        * */
/* *  $Module:   CMDLNE                                     * */ 
/* *                                                        * */
/* *  Copyright (C) 1996, 1997, 1998, 1999, 2000            * */
/* *  MPI fuer Informatik                                   * */
/* *                                                        * */
/* *  This program is free software; you can redistribute   * */
/* *  it and/or modify it under the terms of the FreeBSD    * */
/* *  Licence.                                              * */
/* *                                                        * */
/* *  This program is distributed in the hope that it will  * */
/* *  be useful, but WITHOUT ANY WARRANTY; without even     * */
/* *  the implied warranty of MERCHANTABILITY or FITNESS    * */
/* *  FOR A PARTICULAR PURPOSE.  See the LICENCE file       * */
/* *  for more details.                                     * */
/* *                                                        * */
/* *                                                        * */
/* $Revision: 1.5 $                                         * */
/* $State: Exp $                                            * */
/* $Date: 2010-02-22 14:09:57 $                             * */
/* $Author: weidenb $                                       * */
/* *                                                        * */
/* *             Contact:                                   * */
/* *             Christoph Weidenbach                       * */
/* *             MPI fuer Informatik                        * */
/* *             Stuhlsatzenhausweg 85                      * */
/* *             66123 Saarbruecken                         * */
/* *             Email: spass@mpi-inf.mpg.de                * */
/* *             Germany                                    * */
/* *                                                        * */
/* ********************************************************** */
/**************************************************************/
/* $RCSfile: cmdline.c,v $ */


/***************************************************************/



#include "cmdline.h"



/**************************************************************/
/* Local variables  and types                                 */
/**************************************************************/


const char* cmdlne_InputFile;
const char* cmdlne_OutputFile;

static LIST cmdlne_ArgumentsList;

/**************************************************************/
/* Functions                                                  */
/**************************************************************/


void cmdlne_Init()
/**************************************************************
  INPUT:   NONE
  RETURNS: Nothing.
  EFFECT:  Initializes command line module 
 ***************************************************************/
{
     cmdlne_InputFile = (char*)NULL;
     cmdlne_OutputFile = (char*)NULL;
     cmdlne_ArgumentsList = NULL;
}

void cmdlne_PrintSPASSNames()
/**************************************************************
  INPUT:   None 
  RETURNS: Nothing
  EFFECT:  Prints all options in three rows
***************************************************************/
{
  int i,j;
  
  for (i=0; i < flag_GetMaxFlag(); i=i+4) { 
    for (j =0; j <=3; j++) 
      if (i+j < flag_GetMaxFlag() && !flag_IsUndefined(i+j)) {
	printf("%-18s ", flag_Name(i+j)); 
      } 
    putchar('\n');
  }
}

void cmdlne_FreePair(LIST Pair)
/***************************************************************
  INPUT:   a Pair
  RETURNS: Nothing.
  EFFECT:  Free the Pair and its elements.
********************* ******************************************/

{
  string_StringFree(list_PairFirst(Pair));
  string_StringFree(list_PairSecond(Pair));

  list_PairFree(Pair);

}

void cmdlne_Free()
/****************************************************************
  INPUT:   NONE
  RETURNS: Nothing.
  EFFECT:  Free memory of module
  ***************************************************************/
{
  list_DeleteWithElement(cmdlne_ArgumentsList, (void (*) (POINTER)) cmdlne_FreePair);	 
}

const char* cmdlne_GetInputFile()
/****************************************************************
  INPUT:   None
  RETURNS: Pointer to the name of input file
 ****************************************************************/
{
      return cmdlne_InputFile;
}

const char* cmdlne_GetOutputFile()
/****************************************************************
  INPUT:   None
  RETURNS: Pointer to the name of output file
*****************************************************************/
{

      return cmdlne_OutputFile;
}


BOOL cmdlne_SetFlags(FLAGSTORE flagstore)
/****************************************************************
  INPUT:   a FLAGSTORE
  RETURNS: TRUE if all arguments of <cmdlne_ArgumentsList> are 
           valid arguments and the respective flags of <flagstore>
	   could be set and FALSE otherwise.
  EFFECT:  Set flags of <flagstore> according to arguments stored in
  	   <cmdlne_ArgumentsList>
*****************************************************************/
{
	int id, tk;
	LIST Scan;	
	BOOL found;


	for(Scan=cmdlne_ArgumentsList; !list_Empty(Scan); Scan = list_Cdr(Scan)) 
	{
		found = FALSE;
		
		for (id=0; id < flag_GetMaxFlag() && !found; id++)
		{
		  if (!flag_IsUndefined(id) && string_Equal(flag_Name(id), 
					       (char*) list_PairFirst(list_Car(Scan))) )
	   		{
			  if (flag_IsOfValueType(id, flag_INTEGER)) {
			    if(!string_StringIsInteger(list_PairSecond(list_Car(Scan)))){
			        misc_StartUserErrorReport();
			        misc_UserErrorReport("\nError: Argument of option %s must be an integer.\n\n", flag_Name(id));
				misc_FinishUserErrorReport();
				return FALSE;
			    }
				tk = atoi((char*) list_PairSecond(list_Car(Scan)));
				flag_SetFlagIntValue(flagstore, id, tk);
			  }
			  else {
			    flag_SetFlagStringValue(flagstore, id, string_StringCopy((char*)list_PairSecond(list_Car(Scan))));
			  }
			  found = TRUE;
           		}
	   
		}
		
		if(!found) {
		  misc_StartUserErrorReport();
		  misc_UserErrorReport("\n Unrecognized option %s\n\n", (char*) list_PairFirst(list_Car(Scan)));
		  misc_FinishUserErrorReport();
		  return FALSE;
		}
		
	}
	return TRUE;
}

BOOL cmdlne_SetArgument(const char* token, const char* value) 
/**************************************************************
  INPUT:   two pointers
  RETURNS: TRUE if the argument <token> has not been added to the
           list of arguments before and FALSE otherwise.
  EFFECT:  Adds argument <token> with value <value> to list of 
  	   arguments (<cmdlne_ArgumentsList>)
  CAUTION: <value> has to be a character pointer pointing to
  	   a string representing an integer
***************************************************************/
{

	LIST Pair;
	LIST Scan;

	
	/* Check if Argument has already been defined */
	for(Scan=cmdlne_ArgumentsList; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
		if(string_Equal(list_PairFirst(list_Car(Scan)),token)) {
			misc_StartUserErrorReport();
    			misc_UserErrorReport("\n Option %s is multiply defined.\n\n", token);
    			misc_FinishUserErrorReport();
    			return FALSE;
		}
	}
	
	   	
	
	/* Add <token> to argument list with value <value>*/
	Pair = list_PairCreate(string_StringCopy(token), string_StringCopy(value));
	cmdlne_ArgumentsList = list_Cons(Pair, cmdlne_ArgumentsList);
	
	return TRUE;
}


BOOL cmdlne_HandleShortArg(TOKEN token)
/******************************************************************
  INPUT:   FLAGSTORE, a TOKEN
  RETURNS: TRUE if a flag with name <token> exists in <flagstore>.
  	   FALSE otherwise
  EFFECT:  Adds argument <token> with default value to list of arguments
*******************************************************************/
{
   return cmdlne_SetArgument(token, cmdlne_DEFAULTARGVALUE);
}


BOOL cmdlne_HandleArg(TOKEN token1, TOKEN token2)
/*****************************************************************
  INPUT:   FLAGSTORE, two TOKEN's
  RETURNS: TRUE if a flag with name <token1> exists in <flagstore>
	   FALSE otherwise
  EFFECT:  Adds argument with name <token1> and value <token2>
  	   to list of agrguments.
  CAUTION: cmdlne_HandleArg assumes that <token2> is the
	   representation of an integer
******************************************************************/
{
   return cmdlne_SetArgument(token1, token2);
}


BOOL cmdlne_isArgSym(const char c)
/*****************************************************************
  INPUT:   a char constant 
  RETURNS: TRUE if <c> is a character symbol allowed to occur in 
           a string argument of a commandline option 
  ****************************************************************/
{

  if(isalnum(c)|| c == '.' || c == '-' || c == '+'  || c == '/' || c == '\\' || c == '_') {
    return TRUE;
  }
  else
    return FALSE;

}

BOOL cmdlne_LexScan(const char* string)
/*****************************************************************
  INPUT:   FLAGSTORE, a string containing program parameter data 
  RETURNS: TRUE iff <string> contains a valid option and returns 
           FALSE if <string> is not a valid option
  EFFECT:  Scan the cmdlne arguments 
  ****************************************************************/  
{
  int cp,tp1,tp2,tp3;
  char c;
  char token1[cmdlne_MAXTOKENLENGTH];
  char token2[cmdlne_MAXTOKENLENGTH];
  tp1 = tp2 = tp3 = 0;

  if (string[0] == '-')
    {
      cp = 1;
      c = string[cp];
      if (isalpha(c))
	{
	  while(isalpha(c))
	    {
	      token1[tp1] = c;
	      tp1++;
	      cp++;
	      c = string[cp];	
	    }
		
	   if (c == '\0')
	    {
		      token1[tp1] = '\0';
		      if(cmdlne_HandleShortArg(token1))
		      {
			return TRUE;
		      }
		      else
		      {
			misc_StartUserErrorReport();
			misc_UserErrorReport("\nUnrecognized option: -%s\n\n",token1);
			misc_FinishUserErrorReport();
			return FALSE;
		      }
					

	    }
	  else if (c == '=')
	    {
	      /*For long type of arguments*/
			
	      cp++;
	      c = string[cp];
	      token1[tp1] = '\0';
	      
   
	      if (cmdlne_isArgSym(c))
		{
		  token2[tp2]= c;
		  tp2++;
		  cp++;
		  c=string[cp];
		  
		  while (cmdlne_isArgSym(c))
		  {
		      token2[tp2]=c;
		      tp2++;
		      cp++;
		      c = string[cp];
		  }
		  if(c == '\0')
		  {
		        token2[tp2] = '\0';
		        if(cmdlne_HandleArg(token1, token2))
			{
			  return TRUE;
			}
		        else 
		        {
			  misc_StartUserErrorReport();
			  misc_UserErrorReport("\nUnrecognized option: -%s=%s\n\n",token1, token2);
			  misc_FinishUserErrorReport();
			  return FALSE;
		        }
		   }
		   else
		   {
		        misc_StartUserErrorReport();
		        misc_UserErrorReport("\nError: Invalid argument of option %s.\n\n", token1);
			misc_FinishUserErrorReport();
		        return FALSE;
		   }
		    
		  }
	      else 
		{
		  misc_StartUserErrorReport();
		  misc_UserErrorReport("\n Invalid argument of option %s.\n\n", token1);
		  misc_FinishUserErrorReport();
		  return FALSE;
		}
	    }
	  else
	    {
	        while (!c == '\0')
		    {
		      token1[tp1]=c;
		      tp1++;
		      cp++;
		      c = string[cp];
		    }

	       token1[tp1] = '\0';
	       printf("\nUnrecognized option: -%s\n\n",token1);
	       return FALSE;
	    }

	}
      else
	{
            while (!c == '\0')
		 {
		   token1[tp1]=c;
		   tp1++;
		   cp++;
		   c = string[cp];
		 }

	    token1[tp1] = '\0';

	    printf("\nUnrecognized option: -%s\n\n",token1);
	    return FALSE;
	}

 
    }
  else 
    {
	if(cmdlne_InputFile == (char*)NULL) 
	 {
	      cmdlne_InputFile = string;
	      /*printf("\nInput File: %s", cmdlne_InputFile);*/
	      return TRUE;
	 }
        else if(cmdlne_OutputFile == (char*)NULL)
	 {
	      cmdlne_OutputFile = string;
	      /*printf("\nOutput File: %s", cmdlne_OutputFile);*/
	      return TRUE;
	 }
   }

  /* This function should never execute this code */
  misc_StartUserErrorReport();
  misc_UserErrorReport("\n Parse error in cmdlne_LexScan.\n\n");
  misc_FinishUserErrorReport();


  return FALSE;
}


BOOL cmdlne_Read(int argc, const char *argv[])
/***************************************************************
  INPUT:   number of commandline argument, command
  	   line arguments  
  RETURNS: TRUE if options are in correct syntax and no option
           is defined multiple times. 
  EFFECT:  The options, specified by the commandline arguments,
           and their arguments are stored in <cmdlne_ArgumentsList>.
           The respective flags can be set with cmdline_SetFlags.
***************************************************************/
{

  int iterator;
  
  iterator = 1;
  
  while(iterator < argc)
    {
      if(!cmdlne_LexScan(argv[iterator])) 
      {
	return FALSE;
      }
      iterator++;
    }

  return TRUE;
}