This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse.c
607 lines (569 loc) · 17.8 KB
/
parse.c
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* Copyright (c) 2007 Cisco, Inc. All rights reserved.
* Copyright (c) 2010 University of Houston, Inc. All rights reserved.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "otpo.h"
struct list_elem
{
int value;
struct list_elem *next;
};
typedef struct list_elem list;
static int get_possible_values ();
static int parse (char *);
static int get_num_values (char *);
static int set_rpn_stack_elements ();
static int set_string_values (char *, int);
int otpo_initialize_list (char *file_name)
{
int ret;
ret = parse (file_name);
if (SUCCESS != ret)
{
return ret;
}
ret = get_possible_values ();
if (SUCCESS != ret)
{
return ret;
}
ret = set_rpn_stack_elements ();
if (SUCCESS != ret)
{
return ret;
}
return ret;
}
int otpo_get_num_parameters (char *file_name)
{
FILE *fp;
char line [LINE_SIZE], *token = NULL;
int num_params;
num_params = 0;
fp = fopen (file_name, "r");
if (NULL == fp)
{
fprintf (stderr,"Unable to open file: %s\n",file_name);
exit (1);
}
while (NULL != fgets (line, LINE_SIZE, fp))
{
if (NULL == line || '\n' == line[0])
{
continue;
}
token = strtok (line," ");
if (token[0] == '#')
{
continue;
}
num_params ++;
}
fclose (fp);
return num_params;
}
static int parse (char *file_name)
{
FILE *fp;
char line [LINE_SIZE], strtmp[LINE_SIZE], cond_name[LINE_SIZE];
char *token = NULL, *tok = NULL, *values_string = NULL;
int i, j, val, values[50];
fp = fopen (file_name, "r");
if (NULL == fp)
{
fprintf (stderr,"Unable to open file: %s\n",file_name);
exit (1);
}
i=0;
while (NULL != fgets (line, LINE_SIZE, fp))
{
if (NULL == line || '\n' == line[0])
{
continue;
}
token = strtok (line," \t");
if ('#' == token[0])
{
continue;
}
list_params[i].otpo_flags = 0;
list_params[i].name = strdup (token);
list_params[i].num_values = 0;
list_params[i].possible_values = NULL;
list_params[i].condition[0] = '\0';
list_params[i].num_rpn_elements = 0;
list_params[i].default_value = NULL;
for (j=0 ; j<MAX_VALUES ; j++)
{
list_params[i].string_values[j] = NULL;
}
token = strtok (NULL," \t\n");
while(1)
{
if (NULL == token)
{
/*
If -p option is specified, then we set the sting values after we finish
parsing the current line. The reason is that when we have two nested
tokenizers, some badness is going on.
*/
if (NULL != values_string)
{
set_string_values (values_string, i);
free(values_string);
values_string = NULL;
}
break;
}
/* Virtual Param */
else if (0 == strcmp ("-v",token))
{
OTPO_PARAM_SET_VIRTUAL (list_params[i]);
token = strtok (NULL," \t\n");
}
/* Default Value */
else if (0 == strcmp ("-d",token))
{
token = strtok (NULL, " \t\n");
list_params[i].default_value = strdup (token);
token = strtok (NULL," \t\n");
}
/* Possible Values */
else if (0 == strcmp ("-p",token))
{
/* allocate all possible values as strings
if they are specified by the user */
token = strtok (NULL, "{}");
values_string = strdup (token);
token = strtok (NULL, " \t\n");
}
else if (0 == strcmp ("-a",token))
{
OTPO_PARAM_SET_AGGREGATE (list_params[i]);
token = strtok (NULL, "\"");
list_params[i].string_values[0] = strdup (token);
list_params[i].num_values = 1;
list_params[i].possible_values = (int *)malloc(sizeof(int) * 1);
if (NULL == list_params[i].possible_values)
{
fprintf (stderr,"Malloc Failed...\n");
return NO_MEMORY;
}
list_params[i].possible_values[0] = 0;
token = strtok (NULL," \t\n");
}
else if (0 == strcmp ("-r",token))
{
token = strtok (NULL, " \t");
list_params[i].start_value = atoi (token);
if (token[0] != '0' && list_params[i].start_value == 0)
{
fprintf (stderr,"Invalid Start Value\n");
return INVALID_VALUES;
}
token = strtok (NULL, " \t");
list_params[i].end_value = atoi (token);
if (token[0] != '0' && list_params[i].end_value == 0)
{
fprintf (stderr,"Invalid End Value\n");
return INVALID_VALUES;
}
token = strtok (NULL," \t\n");
}
else if (0 == strcmp ("-t",token))
{
token = strtok (NULL, " \t\n");
if (0 == strcmp (token,"increment"))
{
list_params[i].traverse = INCREMENT;
token = strtok (NULL, " \t");
switch (token [0])
{
case '+':
list_params[i].operation = ADD;
break;
case '*':
list_params[i].operation = MULTIPLY;
break;
case '-':
list_params[i].operation = SUBTRACT;
break;
case '/':
list_params[i].operation = DIVIDE;
break;
default:
fprintf (stderr,"Invalid operation %s\n",token);
return INVALID_OPERATION;
}
token = strtok (NULL, " \t");
list_params[i].traverse_attr.increment = atoi (token);
if (token[0] != '0' && list_params[i].traverse_attr.increment == 0)
{
fprintf (stderr,"Invalid End Value\n");
return INVALID_VALUES;
}
}
else
{
fprintf (stderr,"Invalid traversal method: %s\n",token);
return INVALID_TRAVERSE;
}
token = strtok (NULL," \t\n");
}
else if (0 == strcmp ("-i",token))
{
token = strtok (NULL, " \t\n");
if ('\"' != token[0])
{
list_params[i].condition[0] = '\0';
strcat (list_params[i].condition,token);
list_params[i].condition[strlen(token)] = '\0';
}
else
{
for (j=0 ; j<strlen(token)-1 ; j++)
{
cond_name[j] = token[j+1];
}
cond_name[j] = '\0';
token = strtok (NULL, "\"");
if (CONDITION_LENGTH < (strlen(token) + j + 1))
{
printf ("condition for %s is too long and will not be evaluated\n",
list_params[i].name);
}
else
{
list_params[i].condition[0] = '\0';
strncat (list_params[i].condition,cond_name,j);
strcat (list_params[i].condition, " ");
strcat (list_params[i].condition, token);
list_params[i].condition[strlen(token) + j + 1] = '\0';
}
}
token = strtok (NULL," \t\n");
}
else
{
fprintf(stderr, "Invalid Option %s in Param file...\n",token);
exit(1);
}
}
i++;
}
fclose (fp);
return SUCCESS;
}
static int get_num_values (char *str)
{
char *tok = NULL;
int num, tmp;
num = 0;
tok = strtok (str, " \t");
while(NULL != tok)
{
tmp = atoi (tok);
if (tok[0] != '0' && tmp == 0)
{
break;
}
num++;
tok = strtok (NULL," \t");
}
return num;
}
static int get_possible_values ()
{
int i, tmp, num, j;
list *curr, *head;
for (i=0 ; i<num_parameters ; i++)
{
if (0 != list_params[i].num_values)
{
continue;
}
num = 0;
head = NULL;
curr = (list*)malloc(sizeof (list));
curr->value = list_params[i].start_value;
curr->next = head;
head = curr;
num ++;
tmp = list_params[i].start_value;
while (tmp <= list_params[i].end_value)
{
switch (list_params[i].operation)
{
case '+':
tmp += list_params[i].traverse_attr.increment;
break;
case '-':
tmp -= list_params[i].traverse_attr.increment;
break;
case '*':
if (0 == tmp)
{
tmp = 1;
num --;
goto out;
}
else
{
tmp *= list_params[i].traverse_attr.increment;
}
break;
case '/':
if (0 == tmp)
{
tmp = 1;
num --;
goto out;
}
else
{
tmp /= list_params[i].traverse_attr.increment;
}
break;
default:
break;
}
if (tmp > list_params[i].end_value)
{
continue;
}
curr = (list*)malloc (sizeof (list));
curr->value = tmp;
curr->next = head;
head = curr;
out:
num ++;
}
curr = head;
/* allocating the array of possible arrays in the param struct */
list_params[i].num_values = num;
list_params[i].possible_values = (int *)malloc(sizeof(int)*list_params[i].num_values);
if (NULL == list_params[i].possible_values)
{
printf ("Malloc Failed...\n");
return NO_MEMORY;
}
/* Adding the values in the list to param_list */
j=num;
while (curr)
{
list_params[i].possible_values[--j] = curr->value;
curr = curr->next;
}
/* Freeing the Linked List */
curr = head;
while (curr)
{
head = curr;
curr = curr->next;
free(head);
}
}
return SUCCESS;
}
static int set_rpn_stack_elements ()
{
int i, j, count;
char *token=NULL, *condition_string=NULL;
for (i=0 ; i<num_parameters ; i++)
{
if ('\0' == list_params[i].condition[0])
{
list_params[i].num_rpn_elements = 0;
continue;
}
count = 0;
condition_string = strdup (list_params[i].condition);
token = strtok (condition_string," \t\n");
while (NULL != token)
{
list_params[i].rpn_elements[count].is_operator = 1;
if (strcmp (token,"+") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = ADD;
goto set;
}
if (strcmp (token,"-") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = SUBTRACT;
goto set;
}
if (strcmp (token,"*") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = MULTIPLY;
goto set;
}
if (strcmp (token,"/") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = DIVIDE;
goto set;
}
if (strcmp (token,"=") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = EQUAL;
goto set;
}
if (strcmp (token,"!=") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = NEQUAL;
goto set;
}
if (strcmp (token,">") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = GREATER;
goto set;
}
if (strcmp (token,"<") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = LESS;
goto set;
}
if (strcmp (token,">=") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = GEQUAL;
goto set;
}
if (strcmp (token,"<=") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = LEQUAL;
goto set;
}
if (strcmp (token,"&&") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = AND;
goto set;
}
if (strcmp (token,"||") == 0)
{
list_params[i].rpn_elements[count].type.operator_type = OR;
goto set;
}
list_params[i].rpn_elements[count].is_operator = 0;
if ((atoi(token)) || (strcmp (token, "0") == 0))
{
list_params[i].rpn_elements[count].type.operand_type = INTEGER;
list_params[i].rpn_elements[count].value.integer_value = atoi (token);
if (token[0] != '0' && list_params[i].rpn_elements[count].value.integer_value == 0)
{
fprintf (stderr,"Invalid value for comparison\n");
return INVALID_VALUES;
}
goto set;
}
for (j=0 ; j<num_parameters ; j++)
{
if (strcmp(token,list_params[j].name) == 0)
{
list_params[i].rpn_elements[count].type.operand_type = PARAM;
list_params[i].rpn_elements[count].value.param_index = j;
goto set;
}
}
list_params[i].rpn_elements[count].type.operand_type = STRING;
strcpy (list_params[i].rpn_elements[count].value.string_value, token);
set:
if (RPN_MAX_ELEMENTS == count)
{
fprintf (stderr,
"You have reached the limit of rpn elements (%d)\n",
RPN_MAX_ELEMENTS);
exit (1);
}
count ++;
token = strtok (NULL," \t\n");
}
list_params[i].num_rpn_elements = count;
if (NULL != condition_string)
{
free (condition_string);
}
}
return SUCCESS;
}
static int set_string_values (char *values, int index)
{
char *tok;
int j;
tok = strtok (values, " ,\t\n");
while (1)
{
if (NULL == tok)
{
break;
}
if (MAX_VALUES == list_params[index].num_values)
{
fprintf (stderr,
"You have reached the limit of possible string values (%d)\n",
MAX_VALUES);
exit (1);
}
list_params[index].string_values[list_params[index].num_values] =
strdup(tok);
list_params[index].num_values ++;
tok = strtok (NULL," ,\t\n");
}
/*
the int array in list_params will be the same as the strings, if they can
be converted into integers, otherwise it will be from 0->num_values
*/
list_params[index].possible_values = (int *)malloc(sizeof(int) *
list_params[index].num_values);
if (NULL == list_params[index].possible_values)
{
fprintf (stderr,"Malloc Failed...\n");
return NO_MEMORY;
}
for (j=0 ; j<list_params[index].num_values ; j++)
{
if (list_params[index].string_values[j][0] != '0' &&
atoi(list_params[index].string_values[j]) == 0)
{
break;
}
list_params[index].possible_values[j] =
atoi(list_params[index].string_values[j]);
}
if (j != list_params[index].num_values)
{
for (j=0 ; j<list_params[index].num_values ; j++)
{
list_params[index].possible_values[j] = j;
}
}
}
int otpo_free_list_params_objects ()
{
int i, j;
for (i=0 ; i<num_parameters ; i++)
{
if (NULL != list_params[i].name)
{
free (list_params[i].name);
}
if (NULL != list_params[i].default_value)
{
free (list_params[i].default_value);
}
if (NULL != list_params[i].possible_values)
{
free (list_params[i].possible_values);
}
for (j=0 ; j<list_params[i].num_values ; j++)
{
if (NULL != list_params[i].string_values[j])
{
free (list_params[i].string_values[j]);
}
}
}
return SUCCESS;
}