forked from yszheda/sim-outorder_RRIP-HP-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.c
1799 lines (1628 loc) · 46.5 KB
/
options.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* options.c - options package routines */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC ([email protected]). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC ([email protected]).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC ([email protected]). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
#else /* _MSC_VER */
#define chdir _chdir
#define getcwd _getcwd
#endif
#include <string.h>
#include <ctype.h>
#include <float.h>
#include "host.h"
#include "misc.h"
#include "options.h"
/* create a new option database */
struct opt_odb_t *
opt_new(orphan_fn_t orphan_fn) /* user-specified orphan parser */
{
struct opt_odb_t *odb;
odb = (struct opt_odb_t *)calloc(1, sizeof(struct opt_odb_t));
if (!odb)
fatal("out of virtual memory");
odb->options = NULL;
odb->orphan_fn = orphan_fn;
odb->header = NULL;
odb->notes = NULL;
return odb;
}
/* free an option database */
void
opt_delete(struct opt_odb_t *odb) /* option database */
{
struct opt_opt_t *opt, *opt_next;
struct opt_note_t *note, *note_next;
/* free all options */
for (opt=odb->options; opt; opt=opt_next)
{
opt_next = opt->next;
opt->next = NULL;
free(opt);
}
/* free all notes */
for (note = odb->notes; note != NULL; note = note_next)
{
note_next = note->next;
note->next = NULL;
free(note);
}
odb->notes = NULL;
free(odb);
}
/* add option OPT to option database ODB */
static void
add_option(struct opt_odb_t *odb, /* option database */
struct opt_opt_t *opt) /* option variable */
{
struct opt_opt_t *elt, *prev;
/* sanity checks on option name */
if (opt->name[0] != '-')
panic("option `%s' must start with a `-'", opt->name);
/* add to end of option list */
for (prev=NULL, elt=odb->options; elt != NULL; prev=elt, elt=elt->next)
{
/* sanity checks on option name */
if (elt->name[0] == opt->name[0] && !strcmp(elt->name, opt->name))
panic("option `%s' is multiply defined", opt->name);
}
if (prev != NULL)
prev->next = opt;
else /* prev == NULL */
odb->options = opt;
opt->next = NULL;
}
/* register an integer option variable */
void
opt_reg_int(struct opt_odb_t *odb, /* option data base */
char *name, /* option name */
char *desc, /* option description */
int *var, /* target variable */
int def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format) /* optional value print format */
{
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = 1;
opt->nelt = NULL;
opt->format = format ? format : "%12d";
opt->oc = oc_int;
opt->variant.for_int.var = var;
opt->print = print;
opt->accrue = FALSE;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
*var = def_val;
}
/* register an integer option list */
void
opt_reg_int_list(struct opt_odb_t *odb,/* option database */
char *name, /* option name */
char *desc, /* option description */
int *vars, /* pointer to option array */
int nvars, /* total entries in option array */
int *nelt, /* number of entries parsed */
int *def_val, /* default value of option array */
int print, /* print during `-dumpconfig'? */
char *format, /* optional user print format */
int accrue) /* accrue list across uses */
{
int i;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = nvars;
opt->nelt = nelt;
opt->format = format ? format : "%d";
opt->oc = oc_int;
opt->variant.for_int.var = vars;
opt->print = print;
opt->accrue = accrue;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
for (i=0; i < *nelt; i++)
vars[i] = def_val[i];
}
/* register an unsigned integer option variable */
void
opt_reg_uint(struct opt_odb_t *odb, /* option database */
char *name, /* option name */
char *desc, /* option description */
unsigned int *var, /* pointer to option variable */
unsigned int def_val, /* default value of option variable */
int print, /* print during `-dumpconfig'? */
char *format) /* optional user print format */
{
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = 1;
opt->nelt = NULL;
opt->format = format ? format : "%12u";
opt->oc = oc_uint;
opt->variant.for_uint.var = var;
opt->print = print;
opt->accrue = FALSE;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
*var = def_val;
}
/* register an unsigned integer option list */
void
opt_reg_uint_list(struct opt_odb_t *odb,/* option database */
char *name, /* option name */
char *desc, /* option description */
unsigned int *vars, /* pointer to option array */
int nvars, /* total entries in option array */
int *nelt, /* number of elements parsed */
unsigned int *def_val,/* default value of option array */
int print, /* print opt during `-dumpconfig'? */
char *format, /* optional user print format */
int accrue) /* accrue list across uses */
{
int i;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = nvars;
opt->nelt = nelt;
opt->format = format ? format : "%u";
opt->oc = oc_uint;
opt->variant.for_uint.var = vars;
opt->print = print;
opt->accrue = accrue;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
for (i=0; i < *nelt; i++)
vars[i] = def_val[i];
}
/* register a single-precision floating point option variable */
void
opt_reg_float(struct opt_odb_t *odb, /* option data base */
char *name, /* option name */
char *desc, /* option description */
float *var, /* target option variable */
float def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format) /* optional value print format */
{
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = 1;
opt->nelt = NULL;
opt->format = format ? format : "%12.4f";
opt->oc = oc_float;
opt->variant.for_float.var = var;
opt->print = print;
opt->accrue = FALSE;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
*var = def_val;
}
/* register a single-precision floating point option array */
void
opt_reg_float_list(struct opt_odb_t *odb,/* option data base */
char *name, /* option name */
char *desc, /* option description */
float *vars, /* target array */
int nvars, /* target array size */
int *nelt, /* number of args parsed goes here */
float *def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format, /* optional value print format */
int accrue) /* accrue list across uses */
{
int i;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = nvars;
opt->nelt = nelt;
opt->format = format ? format : "%.4f";
opt->oc = oc_float;
opt->variant.for_float.var = vars;
opt->print = print;
opt->accrue = accrue;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
for (i=0; i < *nelt; i++)
vars[i] = def_val[i];
}
/* register a double-precision floating point option variable */
void
opt_reg_double(struct opt_odb_t *odb, /* option data base */
char *name, /* option name */
char *desc, /* option description */
double *var, /* target variable */
double def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format) /* option value print format */
{
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = 1;
opt->nelt = NULL;
opt->format = format ? format : "%12.4f";
opt->oc = oc_double;
opt->variant.for_double.var = var;
opt->print = print;
opt->accrue = FALSE;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
*var = def_val;
}
/* register a double-precision floating point option array */
void
opt_reg_double_list(struct opt_odb_t *odb, /* option data base */
char *name, /* option name */
char *desc, /* option description */
double *vars, /* target array */
int nvars, /* target array size */
int *nelt, /* number of args parsed goes here */
double *def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format, /* option value print format */
int accrue) /* accrue list across uses */
{
int i;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = nvars;
opt->nelt = nelt;
opt->format = format ? format : "%.4f";
opt->oc = oc_double;
opt->variant.for_double.var = vars;
opt->print = print;
opt->accrue = accrue;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
for (i=0; i < *nelt; i++)
vars[i] = def_val[i];
}
/* bind an enumeration string to an enumeration value */
static int
bind_to_enum(char *str, /* string to bind to an enum */
char **emap, /* enumeration string map */
int *eval, /* enumeration value map, optional */
int emap_sz, /* size of maps */
int *res) /* enumeration string value result */
{
int i;
/* string enumeration string map */
for (i=0; i<emap_sz; i++)
{
if (!strcmp(str, emap[i]))
{
if (eval)
{
/* bind to eval value */
*res = eval[i];
}
else
{
/* bind to string index */
*res = i;
}
return TRUE;
}
}
/* not found */
*res = -1;
return FALSE;
}
/* bind a enumeration value to an enumeration string */
static char *
bind_to_str(int val, /* enumeration value */
char **emap, /* enumeration string map */
int *eval, /* enumeration value map, optional */
int emap_sz) /* size of maps */
{
int i;
if (eval)
{
/* bind to first matching eval value */
for (i=0; i<emap_sz; i++)
{
if (eval[i] == val)
{
/* found */
return emap[i];
}
}
/* not found */
return NULL;
}
else
{
/* bind to string at index */
if (val >= emap_sz)
{
/* invalid index */
return NULL;
}
/* else, index is in range */
return emap[val];
}
}
/* register an enumeration option variable, NOTE: all enumeration option
variables must be of type `int', since true enum variables may be allocated
with variable sizes by some compilers */
void
opt_reg_enum(struct opt_odb_t *odb, /* option data base */
char *name, /* option name */
char *desc, /* option description */
int *var, /* target variable */
char *def_val, /* default variable value */
char **emap, /* enumeration string map */
int *eval, /* enumeration value map, optional */
int emap_sz, /* size of maps */
int print, /* print during `-dumpconfig'? */
char *format) /* option value print format */
{
int enum_val;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = 1;
opt->nelt = NULL;
opt->format = format ? format : "%12s";
opt->oc = oc_enum;
opt->variant.for_enum.var = var;
opt->variant.for_enum.emap = emap;
opt->variant.for_enum.eval = eval;
opt->variant.for_enum.emap_sz = emap_sz;
if (def_val)
{
if (!bind_to_enum(def_val, emap, eval, emap_sz, &enum_val))
fatal("could not bind default value for option `%s'", name);
}
else
enum_val = 0;
opt->print = print;
opt->accrue = FALSE;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
*var = enum_val;
}
/* register an enumeration option array, NOTE: all enumeration option variables
must be of type `int', since true enum variables may be allocated with
variable sizes by some compilers */
void
opt_reg_enum_list(struct opt_odb_t *odb,/* option data base */
char *name, /* option name */
char *desc, /* option description */
int *vars, /* target array */
int nvars, /* target array size */
int *nelt, /* number of args parsed goes here */
char *def_val, /* default variable value */
char **emap, /* enumeration string map */
int *eval, /* enumeration value map, optional */
int emap_sz, /* size of maps */
int print, /* print during `-dumpconfig'? */
char *format, /* option value print format */
int accrue) /* accrue list across uses */
{
int i, enum_val;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = nvars;
opt->nelt = nelt;
opt->format = format ? format : "%s";
opt->oc = oc_enum;
opt->variant.for_enum.var = vars;
opt->variant.for_enum.emap = emap;
opt->variant.for_enum.eval = eval;
opt->variant.for_enum.emap_sz = emap_sz;
if (def_val)
{
if (!bind_to_enum(def_val, emap, eval, emap_sz, &enum_val))
fatal("could not bind default value for option `%s'", name);
}
else
enum_val = 0;
opt->print = print;
opt->accrue = accrue;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
for (i=0; i < *nelt; i++)
vars[i] = enum_val;
}
/* pre-defined boolean flag operands */
#define NUM_FLAGS 28
static char *flag_emap[NUM_FLAGS] = {
"true", "t", "T", "True", "TRUE", "1", "y", "Y", "yes", "Yes", "YES",
"on", "On", "ON",
"false", "f", "F", "False", "FALSE", "0", "n", "N", "no", "No", "NO",
"off", "Off", "OFF"
};
static int flag_eval[NUM_FLAGS] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/* register a boolean flag option variable */
void
opt_reg_flag(struct opt_odb_t *odb, /* option data base */
char *name, /* option name */
char *desc, /* option description */
int *var, /* target variable */
int def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format) /* optional value print format */
{
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = 1;
opt->nelt = NULL;
opt->format = format ? format : "%12s";
opt->oc = oc_flag;
opt->variant.for_enum.var = var;
opt->variant.for_enum.emap = flag_emap;
opt->variant.for_enum.eval = flag_eval;
opt->variant.for_enum.emap_sz = NUM_FLAGS;
opt->print = print;
opt->accrue = FALSE;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
*var = def_val;
}
/* register a boolean flag option array */
void
opt_reg_flag_list(struct opt_odb_t *odb,/* option database */
char *name, /* option name */
char *desc, /* option description */
int *vars, /* pointer to option array */
int nvars, /* total entries in option array */
int *nelt, /* number of elements parsed */
int *def_val, /* default array value */
int print, /* print during `-dumpconfig'? */
char *format, /* optional value print format */
int accrue) /* accrue list across uses */
{
int i;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = nvars;
opt->nelt = nelt;
opt->format = format ? format : "%s";
opt->oc = oc_flag;
opt->variant.for_enum.var = vars;
opt->variant.for_enum.emap = flag_emap;
opt->variant.for_enum.eval = flag_eval;
opt->variant.for_enum.emap_sz = NUM_FLAGS;
opt->print = print;
opt->accrue = accrue;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
for (i=0; i < *nelt; i++)
vars[i] = def_val[i];
}
/* register a string option variable */
void
opt_reg_string(struct opt_odb_t *odb, /* option data base */
char *name, /* option name */
char *desc, /* option description */
char **var, /* pointer to string option variable */
char *def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format) /* optional value print format */
{
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = 1;
opt->nelt = NULL;
opt->format = format ? format : "%12s";
opt->oc = oc_string;
opt->variant.for_string.var = var;
opt->print = print;
opt->accrue = FALSE;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
*var = def_val;
}
/* register a string option array */
void
opt_reg_string_list(struct opt_odb_t *odb,/* option data base */
char *name, /* option name */
char *desc, /* option description */
char **vars, /* pointer to option string array */
int nvars, /* target array size */
int *nelt, /* number of args parsed goes here */
char **def_val, /* default variable value */
int print, /* print during `-dumpconfig'? */
char *format, /* optional value print format */
int accrue) /* accrue list across uses */
{
int i;
struct opt_opt_t *opt;
opt = (struct opt_opt_t *)calloc(1, sizeof(struct opt_opt_t));
if (!opt)
fatal("out of virtual memory");
opt->name = name;
opt->desc = desc;
opt->nvars = nvars;
opt->nelt = nelt;
opt->format = format ? format : "%s";
opt->oc = oc_string;
opt->variant.for_string.var = vars;
opt->print = print;
opt->accrue = accrue;
/* place on ODB's option list */
opt->next = NULL;
add_option(odb, opt);
/* set default value */
for (i=0; i < *nelt; i++)
vars[i] = def_val[i];
}
/* process command line arguments, returns index of next argument to parse */
int
process_option(struct opt_odb_t *odb, /* option database */
int index, /* index of the first arg to parse */
int argc, /* total number of arguments */
char **argv) /* argument string array */
{
int cnt, ent, nvars;
char *endp;
double tmp;
struct opt_opt_t *opt;
/* locate the option in the option database */
for (opt=odb->options; opt != NULL; opt=opt->next)
{
if (!strcmp(opt->name, argv[index]))
break;
}
if (!opt)
{
/* no one registered this option */
fatal("option `%s' is undefined", argv[index]);
}
index++;
/* process option arguments */
switch (opt->oc)
{
case oc_int:
/* this option needs at least one argument */
if (index >= argc
|| (argv[index][0] == '-' && !isdigit((int)argv[index][1])))
{
/* no arguments available */
fatal("option `%s' requires an argument", opt->name);
}
cnt = 0;
if (opt->accrue)
{
ent = opt->nelt ? *opt->nelt : 0;
nvars = 1;
if (ent >= opt->nvars)
fatal("too many invocations of option `%s'", opt->name);
}
else
{
ent = 0;
if (opt->nelt)
*opt->nelt = 0;
nvars = opt->nvars;
}
/* parse all arguments */
while (index < argc && cnt < nvars &&
(argv[index][0] != '-' || isdigit((int)argv[index][1])))
{
opt->variant.for_int.var[ent] = strtol(argv[index], &endp, 0);
if (*endp)
{
/* could not parse entire argument */
fatal("could not parse argument `%s' of option `%s'",
argv[index], opt->name);
}
/* else, argument converted correctly */
if (opt->nelt)
(*opt->nelt)++;
cnt++; index++; ent++;
}
break;
case oc_uint:
/* this option needs at least one argument */
if (index >= argc || argv[index][0] == '-')
{
/* no arguments available */
fatal("option `%s' requires an argument", opt->name);
}
cnt = 0;
if (opt->accrue)
{
ent = opt->nelt ? *opt->nelt : 0;
nvars = 1;
if (ent >= opt->nvars)
fatal("too many invocations of option `%s'", opt->name);
}
else
{
ent = 0;
if (opt->nelt)
*opt->nelt = 0;
nvars = opt->nvars;
}
/* parse all arguments */
while (index < argc && cnt < nvars && argv[index][0] != '-')
{
opt->variant.for_uint.var[ent] = strtoul(argv[index], &endp, 0);
if (*endp)
{
/* could not parse entire argument */
fatal("could not parse argument `%s' of option `%s'",
argv[index], opt->name);
}
/* else, argument converted correctly */
if (opt->nelt)
(*opt->nelt)++;
cnt++; index++; ent++;
}
break;
case oc_float:
/* this option needs at least one argument */
if (index >= argc
|| (argv[index][0] == '-' && !isdigit((int)argv[index][1])))
{
/* no arguments available */
fatal("option `%s' requires an argument", opt->name);
}
cnt = 0;
if (opt->accrue)
{
ent = opt->nelt ? *opt->nelt : 0;
nvars = 1;
if (ent >= opt->nvars)
fatal("too many invocations of option `%s'", opt->name);
}
else
{
ent = 0;
if (opt->nelt)
*opt->nelt = 0;
nvars = opt->nvars;
}
/* parse all arguments */
while (index < argc && cnt < nvars &&
(argv[index][0] != '-' || isdigit((int)argv[index][1])))
{
tmp = strtod(argv[index], &endp);
if (*endp)
{
/* could not parse entire argument */
fatal("could not parse argument `%s' of option `%s'",
argv[index], opt->name);
}
if (tmp < FLT_MIN || tmp > FLT_MAX)
{
/* over/underflow */
fatal("FP over/underflow for argument `%s' of option `%s'",
argv[index], opt->name);
}
/* else, argument converted correctly */
opt->variant.for_float.var[ent] = (float)tmp;
if (opt->nelt)
(*opt->nelt)++;
cnt++; index++; ent++;
}
break;
case oc_double:
/* this option needs at least one argument */
if (index >= argc
|| (argv[index][0] == '-' && !isdigit((int)argv[index][1])))
{
/* no arguments available */
fatal("option `%s' requires an argument", opt->name);
}
cnt = 0;
if (opt->accrue)
{
ent = opt->nelt ? *opt->nelt : 0;
nvars = 1;
if (ent >= opt->nvars)
fatal("too many invocations of option `%s'", opt->name);
}
else
{
ent = 0;
if (opt->nelt)
*opt->nelt = 0;
nvars = opt->nvars;
}
/* parse all arguments */
while (index < argc && cnt < nvars &&
(argv[index][0] != '-' || isdigit((int)argv[index][1])))
{
opt->variant.for_double.var[ent] = strtod(argv[index], &endp);
if (*endp)
{
/* could not parse entire argument */
fatal("could not parse argument `%s' of option `%s'",
argv[index], opt->name);
}
/* else, argument converted correctly */
if (opt->nelt)
(*opt->nelt)++;
cnt++; index++; ent++;
}
break;
case oc_enum:
/* this option needs at least one argument */
if (index >= argc || argv[index][0] == '-')
{
/* no arguments available */
fatal("option `%s' requires an argument", opt->name);
}
cnt = 0;
if (opt->accrue)
{
ent = opt->nelt ? *opt->nelt : 0;
nvars = 1;
if (ent >= opt->nvars)
fatal("too many invocations of option `%s'", opt->name);
}
else
{
ent = 0;
if (opt->nelt)
*opt->nelt = 0;
nvars = opt->nvars;
}
/* parse all arguments */
while (index < argc && cnt < nvars && argv[index][0] != '-')
{
if (!bind_to_enum(argv[index],
opt->variant.for_enum.emap,
opt->variant.for_enum.eval,
opt->variant.for_enum.emap_sz,
&opt->variant.for_enum.var[ent]))
{
/* could not parse entire argument */
fatal("could not parse argument `%s' of option `%s'",
argv[index], opt->name);
}
/* else, argument converted correctly */
if (opt->nelt)
(*opt->nelt)++;