-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_parseopts.c
240 lines (219 loc) · 5.76 KB
/
c_parseopts.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
#include "c_includ.h"
/*
* cbzone_parseopts.c
* -- Todd W Mummert, December 1990, CMU
*
* RCS Info
* $Header: c_parseopts.c,v 1.1 91/01/12 02:03:36 mummert Locked $
*
* Parse the options, read the MOTD, etc...
*
* The prints in these routines will never go the game window, as it
* does not exist. cbzone cannot be backgrounded from the beginning
* since the execl will block on tty output. To allow this we create
* yet another flag that specifies that motd is not to be read.
*/
int pager(file)
char* file;
{
char buf[100], *pager, *getenv();
static char defaultpager[] = PAGER;
FILE *f;
if ((pager = getenv("PAGER")) == NULL)
pager = defaultpager;
sprintf(buf,"%s%s",TANKDIR,file);
if ((f=fopen(buf,"r")) != NULL) {
fclose(f);
switch (fork()) {
case 0:
#ifndef WIN31
execl(pager,pager,buf,0);
#endif
fprintf(stderr,"Exec of %s failed\n", pager);
return 1;
case -1:
fprintf(stderr,"Unable to fork process\n");
return 1;
default:
#ifndef WIN31
wait(0);
#endif
break;
}
}
else {
fprintf(stderr,"File %s not found or unreadable.\n", buf);
return 1;
}
return 0;
}
int getoptionint(s)
char *s;
{
char rest[100];
int num;
if (sscanf(s, "%d%s", &num, rest) != 1) {
printf("Error in optional argument %s; use -help for help.\n",
s);
exit(0);
}
return(num);
}
/*
* the following routine may be called in one of two ways...
* either w/ the display set or without...if without, then we
* will need to parse all the options...otherwise the resources should
* have taken care of most of them for us.
*
* now even if the display is set, we may get options that were
* ambiguous.
*
* since options are more than one letter, we can't use getopt.
*/
#define MAXOPTIONS 16
#define OPTIONINT 7
void parseopt(argc, argv, status)
int argc;
char* argv[];
Bool status;
{
int i;
Bool early_exit = False;
static char* optionnames[] = {
"-xrm", "-delay", "-blocks", "-landers", "-tanks", "-missiles",
"-salvos", "-coptersonly", "-quiet", "-scores", "-original",
"-version", "-help", "-nooutput", "-mono", "-cursor",
"-defaultcolormap", "-nofullscreen"};
#ifdef WIN32
// Eric Fogelin: Fixed options (original game, in mono color, quiet)
opt->mono = True;
opt->loud = False;
opt->mblocks = 8;
opt->mlanders = 1;
opt->mtanks = 1;
opt->practice = True;
#else //X11
for (argc--, argv++; argc>0; argc--, argv++) {
for (i=0; i<MAXOPTIONS; i++)
if (!strncmp(*argv,optionnames[i],strlen(*argv)))
break;
if (i < OPTIONINT) {
argc--; argv++;
}
switch(i) {
case 0: /* xrm */
break;
case 1: /* delay */
opt->delay = getoptionint(*argv);
break;
case 2: /* blocks*/
opt->mblocks = getoptionint(*argv);
break;
case 3: /* landers */
opt->mlanders = getoptionint(*argv);
break;
case 4: /* tanks */
opt->mtanks = getoptionint(*argv);
break;
case 5: /* missiles */
opt->mmissiles = getoptionint(*argv);
break;
case 6: /* salvos */
opt->msalvos = getoptionint(*argv);
break;
case 7: /* copter practice */
opt->copters = True;
break;
case 8: /* quiet mode */
opt->loud = False;
break;
case 9: /* scores only */
opt->scores = True;
break;
case 10: /* original */
opt->original = True;
break;
case 11: /* version */
opt->version = True;
break;
case 12: /* help */
opt->help = True;
break;
case 13: /* nooutput */
opt->output = False;
break;
case 14: /* monocolor */
opt->mono = True;
break;
case 15: /* cursor */
opt->cursor = True;
break;
case 16: /* default colormap */
opt->defaultcolormap = True;
break;
case 17: /* fullscreen */
opt->fullscreen = False;
break;
}
}
#endif //X11
if (opt->scores || opt->help || opt->version)
early_exit = True;
if (opt->output) {
pager("cbzone.motd");
if (opt->scores)
scores(-1);
if (opt->version)
printf("\nVersion \"%s\"\n", VERSION);
if (opt->help && pager("cbzone.help"))
printf("Sorry help information not available.\n");
}
if (early_exit)
#ifdef WIN32
return;
#else //X11
exit(0);
#endif
if (!status)
return;
#if 0 // Eric Fogelin: Allow copters and tanks
if (opt->copters)
opt->mtanks = 0;
if (opt->original) {
opt->mblocks = 8;
opt->copters = False;
opt->mlanders = 1;
opt->mmissiles = 1;
opt->mtanks = 1;
opt->practice = True;
opt->msalvos = 1;
}
#endif
opt->menemies = (opt->mtanks > opt->mmissiles ?
opt->mtanks : opt->mmissiles);
if (!opt->menemies) {
printf("Must have at least one missile or tank.\n");
#ifdef WIN32
return;
#else //X11
exit(1);
#endif
}
if (opt->msalvos == -1)
opt->msalvos = opt->menemies;
opt->mobjects = opt->mblocks + opt->mlanders + 2*opt->menemies +
opt->msalvos + 1;
opt->estart = 1;
opt->lstart = opt->estart + opt->menemies;
opt->sstart = opt->lstart + opt->mlanders;
opt->bstart = opt->sstart + opt->menemies + opt->msalvos;
if (opt->mmissiles == MMISSILES &&
opt->mtanks == MTANKS &&
opt->mlanders == MLANDERS &&
opt->mblocks == MBLOCKS &&
opt->delay <= DELAY &&
opt->msalvos == opt->menemies)
opt->practice = False;
else
opt->practice = True;
}