Rev 2 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 11 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /********** |
1 | /********** |
2 | * $Id: cmdparse.c,v 1.1.1.1 2003/11/04 23:34:56 mjames Exp $ ********** |
2 | * $Id: cmdparse.c,v 1.1.1.1 2003/11/04 23:34:56 mjames Exp $ ********** |
3 | 3 | ||
4 | One time Author: Kevin Ross (x6143) |
4 | One time Author: Kevin Ross (x6143) |
5 | Philips Semiconductors Limited. |
5 | Philips Semiconductors Limited. |
6 | Millbrook Industrial Estate, Southampton, SO9 7BH. England. |
6 | Millbrook Industrial Estate, Southampton, SO9 7BH. England. |
Line 84... | Line 84... | ||
84 | There has been a lot more development, but the comments have been |
84 | There has been a lot more development, but the comments have been |
85 | removed in order to cut down the junk |
85 | removed in order to cut down the junk |
86 | 86 | ||
87 | *********************************************************************************************************/ |
87 | *********************************************************************************************************/ |
88 | 88 | ||
- | 89 | #include <stdio.h> |
|
- | 90 | #include <string.h> |
|
- | 91 | #include <stdlib.h> |
|
89 | #include "cmdparse.h" |
92 | #include <ctype.h> |
90 | 93 | ||
91 | #include "cmdexec.h" |
- | |
92 | #include "cmdlog.h" |
- | |
93 | #include "cmdutil.h" |
- | |
94 | #include "database.h" |
- | |
95 | #include "expression.h" |
94 | #include "expression.h" |
96 | #include "generic.h" |
95 | #include "generic.h" |
97 | #include "vertcl_main.h" |
96 | #include "database.h" |
98 | 97 | ||
- | 98 | #include "vertcl_main.h" |
|
99 | #include <ctype.h> |
99 | #include "cmdparse.h" |
100 | #include <stdio.h> |
100 | #include "cmdlog.h" |
101 | #include <stdlib.h> |
101 | #include "cmdutil.h" |
102 | #include <string.h> |
102 | #include "cmdexec.h" |
103 | /* used for dynamic string allocation/deallocation code |
103 | /* used for dynamic string allocation/deallocation code |
104 | * used here for command keywords May 2 2000 */ |
104 | * used here for command keywords May 2 2000 */ |
105 | #include "lx_support.h" |
105 | #include "lx_support.h" |
106 | 106 | ||
107 | /*********************************************************************************************************/ |
107 | /*********************************************************************************************************/ |
108 | #ident \ |
- | |
109 | "@(#)$Header: c:\\cygwin\\cvsroot/Vert03/cmdlib/cmdparse.c,v 1.1.1.1 2003/11/04 23:34:56 mjames Exp $" |
108 | #ident "@(#)$Header: c:\\cygwin\\cvsroot/Vert03/cmdlib/cmdparse.c,v 1.1.1.1 2003/11/04 23:34:56 mjames Exp $" |
110 | 109 | ||
111 | /******************************************* Execute a command |
110 | /******************************************* Execute a command ******************************************/ |
112 | * ******************************************/ |
- | |
113 | /* define flag for global control of execution mode |
111 | /* define flag for global control of execution mode |
114 | * can be Tcl_Cmd or Tcl_GUI : default to be Tcl_Cmd for |
112 | * can be Tcl_Cmd or Tcl_GUI : default to be Tcl_Cmd for |
115 | * backwards compatibility */ |
113 | * backwards compatibility */ |
116 | 114 | ||
117 | tcl_mode_t tcl_mode = Tcl_Cmd; |
115 | tcl_mode_t tcl_mode = Tcl_Cmd; |
118 | 116 | ||
- | 117 | ||
- | 118 | ||
119 | #define WORDWIDTH 100 /* max command word ever expected */ |
119 | #define WORDWIDTH 100 /* max command word ever expected */ |
120 | #define CMDWIDTH 20 /*Field width for printing CMD word */ |
120 | #define CMDWIDTH 20 /*Field width for printing CMD word */ |
121 | int Execute (int argc, char *argv[], struct command Dispatch[]) |
121 | int Execute (int argc,char * argv[], struct command Dispatch[]) |
122 | { |
122 | { |
123 | int i, j, FinalResult = UNKNOWNCMD; |
123 | int i,j, FinalResult = UNKNOWNCMD; |
124 | 124 | ||
125 | if (!argc) |
125 | if(!argc) |
126 | return (NARGS); |
126 | return(NARGS); |
127 | if (argv[0][0] == '?') |
127 | if(argv[0][0] == '?') |
128 | { |
128 | { |
129 | printf ("Commands available;\nHelp on:"); |
129 | printf("Commands available;\nHelp on:"); |
130 | for (i = 0; i < argc; i++) |
130 | for(i=0; i< argc;i++) |
131 | printf ("%s ", argv[i]); |
131 | printf("%s ",argv[i]); |
132 | putchar ('\n'); |
132 | putchar ('\n'); |
133 | for (i = 0; Dispatch[i].name; i++) |
133 | for (i=0; Dispatch[i].name; i++) |
134 | { |
134 | { |
135 | int p; |
135 | int p; |
136 | for (j = 0; j < Dispatch[i].NumChar; |
- | |
137 | j++) /* upper case significant chars */ |
136 | for(j=0;j<Dispatch[i].NumChar;j++) /* upper case significant chars */ |
138 | putchar (toupper (Dispatch[i].name[j])); |
137 | putchar(toupper(Dispatch[i].name[j])); |
139 | for (; Dispatch[i].name[j]; |
- | |
140 | j++) /* and all of the rest, we just print */ |
138 | for(;Dispatch[i].name[j];j++) /* and all of the rest, we just print */ |
141 | putchar (Dispatch[i].name[j]); |
139 | putchar(Dispatch[i].name[j]); |
142 | putchar (' '); |
- | |
143 | j++; |
140 | putchar(' '); j++; |
144 | if (Dispatch[i].extras) |
141 | if(Dispatch[i].extras) { |
145 | { |
142 | p=j; |
146 | p = j; |
- | |
147 | for (; Dispatch[i].extras[j - p]; |
- | |
148 | j++) /* and all of the extras field we just print */ |
143 | for(;Dispatch[i].extras[j-p];j++) /* and all of the extras field we just print */ |
149 | putchar (Dispatch[i].extras[j - p]); |
144 | putchar(Dispatch[i].extras[j-p]); |
150 | }; |
145 | }; |
151 | for (; j < CMDWIDTH; j++) /* create spacing , relies on auto - wrap */ |
146 | for(;j<CMDWIDTH;j++) /* create spacing , relies on auto - wrap */ |
152 | putchar (j & 1 ? '.' : ' '); |
147 | putchar(j&1 ? '.' : ' '); |
153 | putchar (' '); |
148 | putchar(' '); |
154 | puts (Dispatch[i].helpstr); |
149 | puts(Dispatch[i].helpstr); |
155 | } |
150 | } |
156 | putchar ('\n'); |
151 | putchar('\n'); |
157 | FinalResult = OKCMD; /* ? is a valid command at this level */ |
152 | FinalResult = OKCMD; /* ? is a valid command at this level */ |
158 | } |
153 | } |
159 | else |
154 | else |
160 | { |
155 | { |
161 | for (i = 0; Dispatch[i].name; i++) |
156 | for (i=0; Dispatch[i].name; i++) |
162 | if (strncmp (argv[0], Dispatch[i].name, Dispatch[i].NumChar) == 0) |
157 | if (strncmp (argv[0], Dispatch[i].name, Dispatch[i].NumChar)==0) |
163 | { |
158 | { |
164 | /* Dispatch the next level command */ |
159 | /* Dispatch the next level command */ |
165 | if (Dispatch[i].function) |
- | |
166 | { /* call function if there */ |
160 | if (Dispatch[i].function){ /* call function if there */ |
167 | FinalResult = (*Dispatch[i].function) ( |
- | |
168 | Tcl_interp, NULL, argc - 1, argv + 1); |
161 | FinalResult=(*Dispatch[i].function) (Tcl_interp,NULL,argc-1,argv+1); |
169 | } |
162 | } |
170 | else /* call down the menu */ |
163 | else /* call down the menu */ |
171 | FinalResult = |
- | |
172 | Execute (argc - 1, argv + 1, Dispatch[i].Menu); |
164 | FinalResult=Execute(argc-1,argv+1,Dispatch[i].Menu); |
173 | break; |
165 | break; |
174 | } |
166 | } |
175 | } |
167 | } |
176 | return (FinalResult); |
168 | return (FinalResult); |
177 | } |
169 | } |
178 | 170 | ||
179 | /******************************************** Strip off words |
171 | /******************************************** Strip off words ********************************************/ |
180 | * ********************************************/ |
- | |
181 | /* this needs repairing . This code is unsafe. |
172 | /* this needs repairing . This code is unsafe. |
182 | * Needs to keep a record of how many chars copied !! |
173 | * Needs to keep a record of how many chars copied !! |
183 | * and stop when in danger of a buffer overflow. |
174 | * and stop when in danger of a buffer overflow. |
184 | * |
175 | * |
185 | * Did not work properly if expanded strings are longer than the |
176 | * Did not work properly if expanded strings are longer than the |
186 | * original strings. This now fixed. |
177 | * original strings. This now fixed. |
187 | * |
178 | * |
188 | * Code modified to use some string allocation functions in lx_support.c |
179 | * Code modified to use some string allocation functions in lx_support.c |
189 | */ |
180 | */ |
190 | /*char wordbuff[1024]; |
181 | /*char wordbuff[1024]; |
191 | */ |
182 | */ |
192 | typedef enum |
183 | typedef enum { SPACES,CHARS } Phase_t; |
193 | { |
184 | |
194 | SPACES, |
185 | int FindWords (char * linebuff, int * pargc, char * argv[],int nArgs, char * Args[], |
195 | CHARS |
186 | struct str ** cmd_first,struct str ** cmd_last |
196 | } Phase_t; |
187 | ) |
197 | 188 | { |
|
198 | int FindWords ( |
189 | char *WordPtr; |
199 | char *linebuff, |
190 | char * BasePtr; |
200 | int *pargc, |
191 | int argc = 0; |
201 | char *argv[], |
192 | int term = ' ',escape,comment,done_word,chars_seen; |
202 | int nArgs, |
193 | char * copy_buff; |
203 | char *Args[], |
194 | Phase_t Phase ; |
204 | struct str **cmd_first, |
195 | |
205 | struct str **cmd_last) |
196 | |
206 | { |
197 | copy_buff = malloc(strlen(linebuff)+1); /* make a safe copy */ |
207 | char *WordPtr; |
198 | |
208 | char *BasePtr; |
199 | /* for (i=0; i<Len; i++) */ |
209 | int argc = 0; |
200 | /* Cmd->Text[i] = tolower(Cmd->Text[i]); */ /* convert text to lower case */ |
210 | int term = ' ', escape, comment, done_word, chars_seen; |
201 | WordPtr = linebuff; /* set LineStart to beginning of string */ |
211 | char *copy_buff; |
202 | escape = 0; |
212 | Phase_t Phase; |
203 | comment = 0; |
213 | 204 | /* Now split off the words */ |
|
214 | copy_buff = malloc (strlen (linebuff) + 1); /* make a safe copy */ |
205 | BasePtr = NULL; |
215 | 206 | while (*WordPtr && argc < WORDSINLINE && comment<2) |
|
216 | /* for (i=0; i<Len; i++) */ |
207 | { |
217 | /* Cmd->Text[i] = tolower(Cmd->Text[i]); */ /* convert text to lower |
208 | BasePtr = copy_buff; |
218 | case */ |
209 | Phase = SPACES; |
219 | WordPtr = linebuff; /* set LineStart to beginning of string */ |
210 | term = ' '; |
220 | escape = 0; |
211 | done_word = 0; |
221 | comment = 0; |
212 | chars_seen = 0; |
222 | /* Now split off the words */ |
213 | while(*WordPtr && !done_word && comment <2) |
223 | BasePtr = NULL; |
214 | { |
224 | while (*WordPtr && argc < WORDSINLINE && comment < 2) |
215 | switch (Phase) |
225 | { |
216 | { |
226 | BasePtr = copy_buff; |
217 | case SPACES: |
227 | Phase = SPACES; |
218 | if(isspace(*WordPtr)) /* skip leading spaces */ |
228 | term = ' '; |
219 | WordPtr++; |
229 | done_word = 0; |
220 | else |
230 | chars_seen = 0; |
221 | Phase = CHARS; |
231 | while (*WordPtr && !done_word && comment < 2) |
222 | break; |
232 | { |
223 | case CHARS: |
233 | switch (Phase) |
224 | if(!escape && *WordPtr == '\\') |
234 | { |
225 | { |
235 | case SPACES: |
226 | escape = 1; |
236 | if (isspace (*WordPtr)) /* skip leading spaces */ |
227 | WordPtr ++; |
237 | WordPtr++; |
228 | } |
238 | else |
229 | else |
239 | Phase = CHARS; |
230 | { |
240 | break; |
231 | if(term==' ' && !escape && (*WordPtr=='"' || *WordPtr == '\'') ) |
241 | case CHARS: |
232 | { /* sigle or double quotes ? */ |
242 | if (!escape && *WordPtr == '\\') |
233 | term = *WordPtr++; |
243 | { |
234 | chars_seen = 1; |
244 | escape = 1; |
235 | } |
245 | WordPtr++; |
236 | else |
246 | } |
237 | { |
247 | else |
238 | /* handle ordinary chars */ |
248 | { |
239 | if(!escape && term==' ' && WordPtr[0]=='#') |
249 | if (term == ' ' && !escape && |
240 | { |
250 | (*WordPtr == '"' || *WordPtr == '\'')) |
241 | comment=2; |
251 | { /* sigle or double quotes ? */ |
242 | done_word = chars_seen; /* comment is appended to valid chars */ |
252 | term = *WordPtr++; |
243 | WordPtr++; |
253 | chars_seen = 1; |
244 | } |
254 | } |
245 | else if (!escape && term == ' ' && WordPtr[0]=='-') |
255 | else |
246 | { |
256 | { |
247 | comment++; |
257 | /* handle ordinary chars */ |
248 | WordPtr++; |
258 | if (!escape && term == ' ' && |
249 | if(comment==2) |
259 | WordPtr[0] == '#') |
250 | done_word = chars_seen; /* comment is appended to valid chars */ |
260 | { |
251 | } |
261 | comment = 2; |
252 | else if ((*WordPtr=='\r') || |
262 | done_word = |
253 | (*WordPtr=='\n') || |
263 | chars_seen; /* comment is appended |
254 | (!escape && (*WordPtr == term))) |
264 | to valid chars */ |
255 | { |
265 | WordPtr++; |
256 | if (comment==1) |
266 | } |
257 | *BasePtr++ = '-'; |
267 | else if ( |
258 | comment = 0; |
268 | !escape && term == ' ' && |
259 | Phase = SPACES; |
269 | WordPtr[0] == '-') |
260 | *BasePtr = '\0'; |
270 | { |
261 | done_word = 1; |
271 | comment++; |
262 | WordPtr++; |
272 | WordPtr++; |
263 | } |
273 | if (comment == 2) |
264 | else |
274 | done_word = |
265 | { |
275 | chars_seen; /* comment is |
266 | if(escape) |
276 | appended to |
267 | *BasePtr++ = '\\'; |
277 | valid chars |
268 | if (comment==1) |
278 | */ |
269 | *BasePtr++ = '-'; |
279 | } |
270 | chars_seen = 1; |
280 | else if ( |
271 | comment = 0; |
281 | (*WordPtr == '\r') || (*WordPtr == '\n') || |
272 | escape = 0; |
282 | (!escape && (*WordPtr == term))) |
273 | *BasePtr++ = *WordPtr++; |
283 | { |
274 | } |
284 | if (comment == 1) |
275 | } |
285 | *BasePtr++ = '-'; |
276 | } |
286 | comment = 0; |
277 | break; |
287 | Phase = SPACES; |
278 | } /* switch */ |
288 | *BasePtr = '\0'; |
279 | } /* while */ |
289 | done_word = 1; |
280 | *BasePtr = '\0'; |
290 | WordPtr++; |
281 | /* make_string now performs an expand_string call */ |
291 | } |
282 | if(chars_seen) |
292 | else |
283 | { |
293 | { |
284 | argv[argc++] = make_string(copy_buff,cmd_first,cmd_last); |
294 | if (escape) |
285 | } |
295 | *BasePtr++ = '\\'; |
286 | |
296 | if (comment == 1) |
287 | } |
297 | *BasePtr++ = '-'; |
288 | * pargc = argc; |
298 | chars_seen = 1; |
289 | free(copy_buff); |
299 | comment = 0; |
290 | |
300 | escape = 0; |
291 | return (OKCMD); |
301 | *BasePtr++ = *WordPtr++; |
292 | } |
302 | } |
293 | |
303 | } |
294 | /********************************* executes the command from stdin or a do file **************************/ |
304 | } |
295 | |
305 | break; |
296 | int ExecuteCommand (FILE * CmdFile,int nArgs, char * Args[]) |
306 | } /* switch */ |
297 | { |
307 | } /* while */ |
298 | char Buff[PATHLENGTH]; |
308 | *BasePtr = '\0'; |
299 | int i, Status; |
309 | /* make_string now performs an expand_string call */ |
300 | char * p; |
310 | if (chars_seen) |
301 | int argc; |
311 | { |
302 | char * argv[WORDSINLINE]; |
312 | argv[argc++] = make_string (copy_buff, cmd_first, cmd_last); |
303 | struct str * cmd_first, * cmd_last; |
313 | } |
304 | cmd_first = NULL; |
314 | } |
305 | cmd_last = NULL; |
315 | *pargc = argc; |
306 | |
316 | free (copy_buff); |
307 | |
317 | 308 | ||
318 | return (OKCMD); |
309 | if(!CmdFile || feof(CmdFile)) |
319 | } |
310 | return(QUITCMD); |
320 | 311 | ||
321 | /********************************* executes the command from stdin or a do file |
312 | p=fgets(Buff,PATHLENGTH-1,CmdFile); /* read the command from the input stream */ |
322 | * **************************/ |
313 | argc=0; /* place the command in the command structure */ |
323 | 314 | if(p==NULL || *Buff=='\0') |
|
324 | int ExecuteCommand (FILE *CmdFile, int nArgs, char *Args[]) |
315 | return(OKCMD); |
325 | { |
316 | |
326 | char Buff[PATHLENGTH]; |
317 | /* convert control codes to spaces */ |
327 | int i, Status; |
318 | i=0; |
328 | char *p; |
319 | while(Buff[i]) { |
329 | int argc; |
320 | if (Buff[i]<' ') |
330 | char *argv[WORDSINLINE]; |
321 | Buff[i]=' '; |
331 | struct str *cmd_first, *cmd_last; |
322 | i++; |
332 | cmd_first = NULL; |
323 | } |
333 | cmd_last = NULL; |
324 | |
334 | 325 | FindWords (Buff,&argc,argv, nArgs,Args,&cmd_first,&cmd_last); /* split off the words and save in structure */ |
|
335 | if (!CmdFile || feof (CmdFile)) |
326 | if (1 || CmdFile != stdin) { |
336 | return (QUITCMD); |
327 | printf("-- "); |
337 | 328 | for(i=0;i<argc;i++) |
|
338 | p = fgets (Buff, PATHLENGTH - 1, CmdFile); /* read the command from the input stream */ |
329 | printf("%s ",ISNULLSTR(argv[i])?"\"\"":argv[i]); /* log the command word by word if its not given interactively */ |
339 | argc = 0; /* place the command in the command structure */ |
330 | printf("\n"); /* */ |
340 | if (p == NULL || *Buff == '\0') |
331 | } |
341 | return (OKCMD); |
332 | /* the code below is not as helpful as it seems . Moved recognizer for comments into |
342 | 333 | Execute as the comments can appear at any point on the command line */ |
|
343 | /* convert control codes to spaces */ |
334 | /* if (argc ==0 || |
344 | i = 0; |
335 | argv[0][0] == '-' || |
345 | while (Buff[i]) |
336 | argv[0][0] == '#') */ |
346 | { |
337 | if(argc==0) |
347 | if (Buff[i] < ' ') |
338 | { |
348 | Buff[i] = ' '; |
339 | |
349 | i++; |
340 | /* deallocate any previous structures */ |
350 | } |
341 | free_strings(&cmd_first,&cmd_last); |
351 | 342 | return (OKCMD) ; /* comment or a null string */ |
|
352 | FindWords (Buff, &argc, argv, nArgs, Args, &cmd_first, &cmd_last); /* split off the |
343 | } |
353 | words and save in |
344 | |
354 | structure */ |
345 | Status = Execute(argc,argv,TopMenu); /* pass the command through the top-level menu */ |
355 | if (1 || CmdFile != stdin) |
346 | switch (Status) { |
356 | { |
347 | case UNKNOWNCMD : |
357 | printf ("-- "); |
348 | Log (LOG_ERROR, "# Unknown command\n"); |
358 | for (i = 0; i < argc; i++) |
349 | break; |
359 | printf ( |
350 | case NARGS : |
360 | "%s ", |
351 | Log (LOG_ERROR, "# Too few parameters\n"); |
361 | ISNULLSTR (argv[i]) ? "\"\"" |
352 | break; |
362 | : argv[i]); /* log the command word by word if |
353 | case FAILED : |
363 | its not given interactively */ |
354 | Log (LOG_ERROR, "# Problem with command\n"); |
364 | printf ("\n"); /* */ |
355 | break; |
365 | } |
356 | }; |
366 | /* the code below is not as helpful as it seems . Moved recognizer for comments into |
357 | |
367 | Execute as the comments can appear at any point on the command line */ |
358 | if (Status !=OKCMD) { |
368 | /* if (argc ==0 || |
359 | Log(LOG_ERROR,"-- ? "); |
369 | argv[0][0] == '-' || |
360 | for (i=0; i < argc; i++) /* display offending command */ |
370 | argv[0][0] == '#') */ |
361 | Log (LOG_ERROR, " %s", ISNULLSTR(argv[i])?"\"\"":argv[i]); |
371 | if (argc == 0) |
362 | |
372 | { |
363 | Log(LOG_ERROR,"\n"); |
373 | /* deallocate any previous structures */ |
364 | |
374 | free_strings (&cmd_first, &cmd_last); |
365 | }; |
375 | return (OKCMD); /* comment or a null string */ |
366 | |
376 | } |
367 | /* deallocate any previous structures */ |
377 | 368 | free_strings(&cmd_first,&cmd_last); |
|
378 | Status = |
369 | |
379 | Execute (argc, argv, TopMenu); /* pass the command through the top-level menu */ |
370 | |
380 | switch (Status) |
371 | return(Status); |
381 | { |
372 | } |
382 | case UNKNOWNCMD: |
373 | |
383 | Log (LOG_ERROR, "# Unknown command\n"); |
374 | /********************************* Execute a command string ******************************************/ |
384 | break; |
375 | int ExecuteString (char * commandstring , int nArgs, char * Args[]) { |
385 | case NARGS: |
376 | int argc =0; |
386 | Log (LOG_ERROR, "# Too few parameters\n"); |
377 | char * argv[WORDSINLINE]; |
387 | break; |
378 | int Status = 0; |
388 | case FAILED: |
379 | |
389 | Log (LOG_ERROR, "# Problem with command\n"); |
380 | struct str * cmd_first, * cmd_last; |
390 | break; |
381 | cmd_first = NULL; |
391 | }; |
382 | cmd_last = NULL; |
392 | 383 | FindWords (commandstring,&argc,argv, nArgs,Args,&cmd_first,&cmd_last); /* split off the words and save in structure */ |
|
393 | if (Status != OKCMD) |
384 | Status = Execute(argc,argv,TopMenu); /* pass the command through the top-level menu */ |
394 | { |
385 | /* deallocate any previous structures */ |
395 | Log (LOG_ERROR, "-- ? "); |
386 | free_strings(&cmd_first,&cmd_last); |
396 | for (i = 0; i < argc; i++) /* display offending command */ |
387 | return(Status); |
397 | Log (LOG_ERROR, " %s", ISNULLSTR (argv[i]) ? "\"\"" : argv[i]); |
388 | } |
398 | 389 | ||
399 | Log (LOG_ERROR, "\n"); |
390 | |
400 | }; |
391 | |
401 | 392 | ||
402 | /* deallocate any previous structures */ |
393 | |
403 | free_strings (&cmd_first, &cmd_last); |
394 | |
404 | 395 | /********************************* A parser for the '*.do files ******************************************/ |
|
405 | return (Status); |
396 | static int Depth=0; |
406 | } |
397 | #define MAXDEPTH 10 /* maximum depth of 'do' files */ |
407 | 398 | #define MAXARGV 10 |
|
408 | /********************************* Execute a command string |
399 | int ParseFile (FILE * CmdFile,int nArgs,char * Args[]) |
409 | * ******************************************/ |
400 | { |
410 | int ExecuteString (char *commandstring, int nArgs, char *Args[]) |
401 | int Status = 0; |
411 | { |
402 | char * argp[MAXARGV]; |
412 | int argc = 0; |
403 | char ** argp_prev; |
413 | char *argv[WORDSINLINE]; |
404 | int nArgs_prev; |
414 | int Status = 0; |
405 | int i; |
415 | 406 | /* diagnosis */ |
|
416 | struct str *cmd_first, *cmd_last; |
407 | if ( nArgs > MAXARGV ) |
417 | cmd_first = NULL; |
408 | nArgs = MAXARGV; |
418 | cmd_last = NULL; |
409 | if ( nArgs < 0 ) |
419 | FindWords ( |
410 | nArgs = 0; |
420 | commandstring, &argc, argv, nArgs, Args, &cmd_first, &cmd_last); /* split off the |
411 | /* belt and braces null check */ |
421 | words and save |
412 | if(!CmdFile) |
422 | in structure */ |
413 | return(FAILED); |
423 | Status = |
414 | |
424 | Execute (argc, argv, TopMenu); /* pass the command through the top-level menu */ |
415 | for(i=0;i<MAXARGV;i++) { |
425 | /* deallocate any previous structures */ |
416 | |
426 | free_strings (&cmd_first, &cmd_last); |
417 | if (i < nArgs) |
427 | return (Status); |
418 | argp[i] = strdup(Args[i]); |
428 | } |
419 | else |
429 | 420 | argp[i] = NULL; |
|
430 | /********************************* A parser for the '*.do files |
421 | |
431 | * ******************************************/ |
422 | |
432 | static int Depth = 0; |
423 | /* printf("Arg [%2d]='%s'\n",i,argp[i]); */ |
433 | #define MAXDEPTH 10 /* maximum depth of 'do' files */ |
424 | |
434 | #define MAXARGV 10 |
425 | } |
435 | int ParseFile (FILE *CmdFile, int nArgs, char *Args[]) |
426 | nArgs_prev = yy_nArgs; |
436 | { |
427 | argp_prev = yy_Args; |
437 | int Status = 0; |
428 | yy_nArgs = nArgs; |
438 | char *argp[MAXARGV]; |
429 | yy_Args = argp; |
439 | char **argp_prev; |
430 | |
440 | int nArgs_prev; |
431 | Depth ++; /* keep a count of recursion depth */ |
441 | int i; |
432 | printf("-- Command file nesting level = %d\n",Depth); |
442 | /* diagnosis */ |
433 | if (Depth > MAXDEPTH) |
443 | if (nArgs > MAXARGV) |
434 | { |
444 | nArgs = MAXARGV; |
435 | Log (LOG_ERROR, "Recursive call of files is greater than %d\n", MAXDEPTH); |
445 | if (nArgs < 0) |
436 | Depth--; |
446 | nArgs = 0; |
437 | return (FAILED); |
447 | /* belt and braces null check */ |
438 | |
448 | if (!CmdFile) |
439 | } |
449 | return (FAILED); |
440 | while(Status == OKCMD ) /* execute the command from do file input stream */ |
450 | 441 | Status = ExecuteCommand(CmdFile, nArgs, argp); |
|
451 | for (i = 0; i < MAXARGV; i++) |
442 | Depth--; |
452 | { |
443 | yy_nArgs = nArgs_prev; |
453 | if (i < nArgs) |
444 | yy_Args = argp_prev; |
454 | argp[i] = strdup (Args[i]); |
445 | for(i=0;i<MAXARGV;i++) |
455 | else |
446 | if (argp[i]) { |
456 | argp[i] = NULL; |
447 | free(argp[i]); |
457 | 448 | argp[i] = NULL; |
|
458 | /* printf("Arg [%2d]='%s'\n",i,argp[i]); */ |
449 | } |
459 | } |
450 | return(Status); |
460 | nArgs_prev = yy_nArgs; |
451 | } |
461 | argp_prev = yy_Args; |
- | |
462 | yy_nArgs = nArgs; |
- | |
463 | yy_Args = argp; |
- | |
464 | - | ||
465 | Depth++; /* keep a count of recursion depth */ |
- | |
466 | printf ("-- Command file nesting level = %d\n", Depth); |
- | |
467 | if (Depth > MAXDEPTH) |
- | |
468 | { |
- | |
469 | Log (LOG_ERROR, "Recursive call of files is greater than %d\n", MAXDEPTH); |
- | |
470 | Depth--; |
- | |
471 | return (FAILED); |
- | |
472 | } |
- | |
473 | while (Status == OKCMD) /* execute the command from do file input stream */ |
- | |
474 | Status = ExecuteCommand (CmdFile, nArgs, argp); |
- | |
475 | Depth--; |
- | |
476 | yy_nArgs = nArgs_prev; |
- | |
477 | yy_Args = argp_prev; |
- | |
478 | for (i = 0; i < MAXARGV; i++) |
- | |
479 | if (argp[i]) |
- | |
480 | { |
- | |
481 | free (argp[i]); |
- | |
482 | argp[i] = NULL; |
- | |
483 | } |
- | |
484 | return (Status); |
- | |
485 | } |
- | |
486 | 452 | ||
487 | /*********************************************************************************************************/ |
453 | /*********************************************************************************************************/ |