Debian Codegraph @

terps/alan3/args.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 args - Argument handling for arun
4
5 Handles the various startup methods on all machines.
6
7 Main function args() will set up global variable adventureName and the flags,
8 the terminal will also be set up and connected.
9
10 \*----------------------------------------------------------------------*/
11
12 #include "sysdep.h"
13 #include "args.h"
14
15 #include "options.h"
16 #include "memory.h"
17 #include "utils.h"
18
19 #include "alan.version.h"
20
21 #ifdef HAVE_GLK
22 #include "glk.h"
23 #include "glkio.h"
24 #endif
25
26 #ifdef __windows__
27 #include <windows.h>
28 #endif
29
30 /* For strcasecmp() */
31 #include <strings.h>
32
33 /* PUBLIC DATA */
34 /* The files and filenames */
35 char *adventureName; /* The name of the game */
36 char *adventureFileName;
37
38 /*======================================================================*/
39 char *gameName(char *fullPathName) {
40 char *foundGameName = "";
41
42 if (fullPathName != NULL) {
43 foundGameName = strdup(baseNameStart(fullPathName));
44 foundGameName[strlen(foundGameName)-4] = '\0'; /* Strip off .A3C */
45 }
46
47 if (foundGameName[0] == '.' && foundGameName[1] == '/')
48 strcpy(foundGameName, &foundGameName[2]);
49
50 return foundGameName;
51 }
52
53
54 /*----------------------------------------------------------------------*/
55 static char *removeQuotes(char *argument) {
56 char *str = strdup(&argument[1]);
57 str[strlen(str)-1] = '\0';
58 return str;
59 }
60
61
62 /*----------------------------------------------------------------------*/
63 static bool isQuoted(char *argument) {
64 return argument[0] == '"' && strlen(argument) > 2;
65 }
66
67
68 /*----------------------------------------------------------------------*/
69 static char *addAcodeExtension(char *adventureFileName) {
70 if (strlen(adventureFileName) < strlen(ACODEEXTENSION)
71 || !equalStrings(&adventureFileName[strlen(adventureFileName)-4], ACODEEXTENSION)) {
72 adventureFileName = realloc(adventureFileName, strlen(adventureFileName)+strlen(ACODEEXTENSION)+1);
73 strcat(adventureFileName, ACODEEXTENSION);
74 }
75 return adventureFileName;
76 }
77
78
79 /*----------------------------------------------------------------------*/
80 static void version(void) {
81 #if (BUILD+0) != 0
82 printf("%s build %d", alan.version.string, BUILD);
83 #else
84 printf("%s", alan.version.string);
85 #endif
86 }
87
88
89 /*----------------------------------------------------------------------*/
90 static void switches(int argc, char *argv[])
91 {
92 int i;
93
94 for (i = 1; i < argc; i++) {
95 char *argument = argv[i];
96
97 if (argument[0] == '-') {
98 switch (toLower(argument[1]))
99 {
100 case 'h':
101 usage(argv[0]);
102 terminate(0);
103 break;
104 case 'i':
105 encodingOption = ENCODING_ISO;
106 break;
107 case 'u':
108 encodingOption = ENCODING_UTF;
109 break;
110 case 'e':
111 ignoreErrorOption = true;
112 break;
113 case 't':
114 traceSectionOption = true;
115 switch (argument[2]) {
116 case '9':
117 case '8':
118 case '7':
119 case '6':
120 case '5' : tracePushOption = true;
121 case '4' : traceStackOption = true;
122 case '3' : traceInstructionOption = true;
123 case '2' : traceSourceOption = true;
124 case '\0':
125 case '1': traceSectionOption = true;
126 }
127 break;
128 case 'd':
129 debugOption = true;
130 break;
131 case 'l':
132 transcriptOption = true;
133 break;
134 case 'v':
135 if (strcmp(argument, "-version") == 0) {
136 version();
137 terminate(0);
138 } else
139 verboseOption = true;
140 break;
141 case 'n':
142 statusLineOption = false;
143 break;
144 case 'c':
145 commandLogOption = true;
146 break;
147 case 'p':
148 nopagingOption = true;
149 break;
150 case 'r':
151 regressionTestOption = true;
152 statusLineOption = false;
153 break;
154 case '-':
155 if (strcasecmp(&argument[2], "version") == 0) {
156 version();
157 terminate(0);
158 break;
159 }
160 /* else fall-through */
161 default:
162 printf("Unrecognized switch, -%s\n", &argument[1]);
163 usage(argv[0]);
164 terminate(0);
165 }
166 } else {
167
168 if (isQuoted(argument))
169 adventureFileName = removeQuotes(argument);
170 else
171 adventureFileName = strdup(argument);
172
173 adventureFileName = addAcodeExtension(adventureFileName);
174
175 adventureName = gameName(adventureFileName);
176
177 }
178 }
179 }
180
181
182 /*----------------------------------------------------------------------*/
183 bool differentInterpreterName(char *string) {
184 return strcasecmp(string, PROGNAME) != 0;
185 }
186
187
188 /*======================================================================*/
189 #ifdef __windows__
190 #include <windows.h>
191 #endif
192
193
194 /*======================================================================*/
195 void args(int argc, char * argv[])
196 {
197 char *programName;
198 char *exePoint;
199
200 #ifdef ARGSDISPLAY
201 int i;
202
203 MessageBox(NULL, "Hello!", "Windows Arun interpreter", MB_OK);
204 MessageBox(NULL, GetCommandLine(), "", MB_OK);
205 for (i = 0; i < argc; i++) {
206 char buf[199];
207 sprintf(buf, "arg %d :\"%s\"", i, argv[i]);
208 MessageBox(NULL, buf, "Alan V3 compiler", MB_OK);
209 }
210 #endif
211
212 #ifdef HAVE_WINGLK
213 argv[0] = GetCommandLine();
214 #endif
215 if ((programName = strrchr(argv[0], '\\')) == NULL
216 && (programName = strrchr(argv[0], '/')) == NULL
217 && (programName = strrchr(argv[0], ':')) == NULL)
218 programName = strdup(argv[0]);
219 else
220 programName = strdup(&programName[1]);
221
222 if (strlen(programName) > 4 && (((exePoint = strstr(programName, ".EXE")) != NULL) || (exePoint = strstr(programName, ".exe")) != NULL))
223 *exePoint = '\0';
224
225 /* Now look at the switches and arguments */
226 switches(argc, argv);
227
228 #ifdef ARGSDISPLAY
229 {
230 char buf[100];
231 sprintf(buf, "programName = '%s'\nadventureFileName = '%s'", programName, adventureFileName);
232 MessageBox(NULL, buf, "Alan V3 compiler", MB_OK);
233 }
234 #endif
235
236 if (adventureFileName == NULL) {
237 /* No game given, try program name */
238 if (differentInterpreterName(programName)) {
239 // TODO break out as a function
240 FILE *adventureFile;
241 adventureFileName = allocate(strlen(programName)
242 +strlen(ACODEEXTENSION)+1);
243 strcpy(adventureFileName, programName);
244 strcat(adventureFileName, ACODEEXTENSION);
245 // TODO break out as utils::fileExists()
246 if ((adventureFile = fopen(adventureFileName, "r")) == NULL) {
247 free(adventureFileName);
248 adventureFileName = NULL;
249 } else
250 fclose(adventureFile);
251 }
252 }
253 adventureName = gameName(adventureFileName);
254 free(programName);
255 }