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