option.c
4.24 KB
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
/*==============================================================================
option.c
==============================================================================*/
#include <string.h>
#include <malloc.h>
#include "global.h"
#include "const.h"
Option option;
Status status = {
FALSE,FALSE};
/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
static void init_options(void)
{
option.in_file = (char *) NULL;
option.out_file = (char *) NULL;
option.merge_file = (char *) NULL;
option.interp = (char *) NULL;
option.debug = 0; /* no debug */
option.verbose = FALSE;
option.r4000_fix = FALSE;
option.big_procedure_list = FALSE;
}
/*------------------------------------------------------------------------------
check error in options
------------------------------------------------------------------------------*/
static void check_options(void)
{
if (option.in_file == NULL){
error("program name not specified");
}
}
/*------------------------------------------------------------------------------
allocate space to store the option parameters.
------------------------------------------------------------------------------*/
static char **alloc_opt_parm( char **pp, char *s)
{
int i;
int first_null;
if (pp == NULL){
pp = (char **) realloc(pp, 10*sizeof(char*));
pp[0] = s;
for(i=1;i<10;i++)
pp[i] = (char *) NULL;
} else{
/* look for null pointer */
for (first_null=0; pp[first_null]!= NULL;first_null++);
/* allocate 10 at a time */
if (first_null%10 == 9){
pp = (char **) realloc(pp, (first_null+1+10)
*sizeof(char*));
for(i=first_null+1;i<=first_null+10;i++)
pp[i] = (char *) NULL;
}
pp[first_null] = s;
}
return pp;
}
/*------------------------------------------------------------------------------
set defaults or implied options
------------------------------------------------------------------------------*/
static void set_implied_options(void)
{
char *basename;
/*------------------
find the base name of input file
------------------*/
if((basename = strrchr(option.in_file, '/')) != NULL)
basename++;
else
basename = option.in_file;
if (option.reorder_file == NULL){
/*------------------
set default reorder filename
------------------*/
char *name;
name = malloc(strlen(basename) + strlen(REORDER_SUFFIX));
strcpy(name, basename);
strcat(name, REORDER_SUFFIX);
option.reorder_file =
alloc_opt_parm(option.reorder_file,name);
}
if (option.out_file == NULL){
/*------------------
set default output filename
------------------*/
char *outname;
outname = malloc(strlen(basename) + strlen(CORD_SUFFIX));
strcpy(outname, basename);
strcat(outname, CORD_SUFFIX);
option.out_file = outname;
}
}
/*------------------------------------------------------------------------------
parse command line options
------------------------------------------------------------------------------*/
void parse_command_line(int argc, char **argv)
{
int i;
init_options();
for (i = 1; i < argc; i++) {
char *arg = argv[i];
if (strcmp(arg, "-verbose") == 0 ||
strcmp(arg, "-v") == 0) {
option.verbose = TRUE;
} else if (strcmp(arg, "-flip") == 0) {
option.flip = TRUE;
} else if (strcmp(arg, "-B") == 0) {
option.big_procedure_list = TRUE;
} else if (strcmp(arg, "-o") == 0) {
if (i == (argc-1))
error("file not specified with -o");
option.out_file = argv[++i];
} else if (strcmp(arg, "-merge") == 0) {
if (i == (argc-1))
error("file not specified with -merge");
option.merge_file = argv[++i];
} else if (strcmp(arg, "-interp") == 0) {
option.interp = argv[++i];
} else if (strcmp(arg, "-debug") == 0) {
option.debug = 1;
} else if (strcmp(arg, "-r4000_fix") == 0) {
option.r4000_fix = TRUE;
} else if (strcmp(arg, "-no_r4000_fix") == 0) {
option.r4000_fix = FALSE;
} else if (!*arg || *arg == '-'){
error("Incorrect option %s", arg);
} else if (!option.in_file){
option.in_file = arg;
} else{
option.reorder_file =
alloc_opt_parm(option.reorder_file, arg);
}
}
check_options();
set_implied_options();
}