1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * commands.c -- mgquery command processing functions
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: commands.c 439 1999-08-10 21:23:37Z sjboddie $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | /*
|
---|
25 | $Log$
|
---|
26 | Revision 1.1 1999/08/10 21:17:47 sjboddie
|
---|
27 | renamed mg-1.3d directory mg
|
---|
28 |
|
---|
29 | Revision 1.1 1998/11/17 09:34:32 rjmcnab
|
---|
30 | *** empty log message ***
|
---|
31 |
|
---|
32 | * Revision 1.2 1994/09/20 04:41:22 tes
|
---|
33 | * For version 1.1
|
---|
34 | *
|
---|
35 | */
|
---|
36 |
|
---|
37 | static char *RCSID = "$Id: commands.c 439 1999-08-10 21:23:37Z sjboddie $";
|
---|
38 |
|
---|
39 | #include "sysfuncs.h"
|
---|
40 | #include "memlib.h"
|
---|
41 | #include "messages.h"
|
---|
42 |
|
---|
43 | #include "environment.h"
|
---|
44 | #include "mg.h"
|
---|
45 | #include "locallib.h"
|
---|
46 | #include "backend.h" /* for qd */
|
---|
47 | #include "term_lists.h" /* for MAXTERMSTRLEN */
|
---|
48 |
|
---|
49 | /* [RPAP - Feb 97: WIN32 Port] */
|
---|
50 | #ifdef __WIN32__
|
---|
51 | # define OPENPIPE _popen
|
---|
52 | # define CLOSEPIPE _pclose
|
---|
53 | #else
|
---|
54 | # define OPENPIPE popen
|
---|
55 | # define CLOSEPIPE pclose
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | extern char *PathName;
|
---|
59 | extern FILE *OutFile, *InFile;
|
---|
60 | extern int OutPipe, InPipe;
|
---|
61 | extern int Quitting;
|
---|
62 |
|
---|
63 | char *CommandsErrorStr = NULL;
|
---|
64 |
|
---|
65 | /* [TS: June/95] */
|
---|
66 | /* current qd for use by any commands */
|
---|
67 | static query_data *the_qd = NULL;
|
---|
68 |
|
---|
69 |
|
---|
70 | #include "help.mg.h"
|
---|
71 | #include "warranty.h"
|
---|
72 | #include "conditions.h"
|
---|
73 |
|
---|
74 | static void
|
---|
75 | CmdQuit (void)
|
---|
76 | {
|
---|
77 | Quitting = 1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static void
|
---|
81 | CmdPush (void)
|
---|
82 | {
|
---|
83 | PushEnv ();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | static void
|
---|
88 | CmdPop (void)
|
---|
89 | {
|
---|
90 | PopEnv ();
|
---|
91 | if (EnvStackHeight () == 1)
|
---|
92 | PushEnv ();
|
---|
93 | }
|
---|
94 |
|
---|
95 | static void
|
---|
96 | CmdHelp (void)
|
---|
97 | {
|
---|
98 | FILE *more;
|
---|
99 | int i;
|
---|
100 |
|
---|
101 | if (!(more = OPENPIPE (GetDefEnv ("pager", "more"), "w"))) /* [RPAP - Feb 97: WIN32 Port] */
|
---|
102 | {
|
---|
103 | fprintf (stderr, "ERROR : Unable to run more\n");
|
---|
104 | return;
|
---|
105 | }
|
---|
106 | for (i = 0; i < sizeof (help_str) / sizeof (help_str[0]); i++)
|
---|
107 | fputs (help_str[i], more);
|
---|
108 |
|
---|
109 | CLOSEPIPE (more); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | static void
|
---|
114 | CmdWarranty (void)
|
---|
115 | {
|
---|
116 | FILE *more;
|
---|
117 | int i;
|
---|
118 |
|
---|
119 | if (!(more = OPENPIPE (GetDefEnv ("pager", "more"), "w"))) /* [RPAP - Feb 97: WIN32 Port] */
|
---|
120 | {
|
---|
121 | fprintf (stderr, "ERROR : Unable to run more\n");
|
---|
122 | return;
|
---|
123 | }
|
---|
124 | for (i = 0; i < sizeof (warranty_str) / sizeof (warranty_str[0]); i++)
|
---|
125 | fputs (warranty_str[i], more);
|
---|
126 |
|
---|
127 | CLOSEPIPE (more); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | static void
|
---|
132 | CmdConditions (void)
|
---|
133 | {
|
---|
134 | FILE *more;
|
---|
135 | int i;
|
---|
136 |
|
---|
137 | if (!(more = OPENPIPE (GetDefEnv ("pager", "more"), "w"))) /* [RPAP - Feb 97: WIN32 Port] */
|
---|
138 | {
|
---|
139 | fprintf (stderr, "ERROR : Unable to run more\n");
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | for (i = 0; i < sizeof (cond_str) / sizeof (cond_str[0]); i++)
|
---|
143 | fputs (cond_str[i], more);
|
---|
144 |
|
---|
145 | CLOSEPIPE (more);
|
---|
146 | }
|
---|
147 |
|
---|
148 | static void
|
---|
149 | CmdReset (void)
|
---|
150 | {
|
---|
151 | while (EnvStackHeight () > 1)
|
---|
152 | PopEnv ();
|
---|
153 | PushEnv ();
|
---|
154 | }
|
---|
155 |
|
---|
156 | static void
|
---|
157 | CmdDisplay (void)
|
---|
158 | {
|
---|
159 | int i;
|
---|
160 | size_t l = 0;
|
---|
161 | char *name;
|
---|
162 | for (i = 0; (name = GetEnvName (i)) != NULL; i++)
|
---|
163 | if (strlen (name) > l)
|
---|
164 | l = strlen (name);
|
---|
165 |
|
---|
166 | for (i = 0; (name = GetEnvName (i)) != NULL; i++)
|
---|
167 | printf ("%-*s = \"%s\"\n", (int) l, name, GetEnv (name));
|
---|
168 | }
|
---|
169 |
|
---|
170 | static void
|
---|
171 | CmdQueryTerms (void)
|
---|
172 | {
|
---|
173 | static char terms_str[MAXTERMSTRLEN + 1];
|
---|
174 |
|
---|
175 | if (the_qd && the_qd->TL)
|
---|
176 | {
|
---|
177 | ConvertTermsToString (the_qd->TL, terms_str);
|
---|
178 | printf ("%s\n", terms_str);
|
---|
179 | fflush (stdout);
|
---|
180 | }
|
---|
181 |
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | static void
|
---|
186 | CmdSet (char *name, char *value)
|
---|
187 | {
|
---|
188 | if (!*name)
|
---|
189 | {
|
---|
190 | CommandsErrorStr = "Blank names are not permitted";
|
---|
191 | return;
|
---|
192 | }
|
---|
193 | switch (SetEnv (name, value, NULL))
|
---|
194 | {
|
---|
195 | case -1:
|
---|
196 | CommandsErrorStr = "Out of memory\n";
|
---|
197 | break;
|
---|
198 | case -2:
|
---|
199 | CommandsErrorStr = ConstraintErrorStr;
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | static void
|
---|
205 | CmdUnset (char *name)
|
---|
206 | {
|
---|
207 | if (!*name)
|
---|
208 | {
|
---|
209 | CommandsErrorStr = "Blank names are not permitted";
|
---|
210 | return;
|
---|
211 | }
|
---|
212 | if (UnsetEnv (name, 0) == -1)
|
---|
213 | CommandsErrorStr = "Specified name does not exist";
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 |
|
---|
218 |
|
---|
219 |
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Do the .input command
|
---|
223 | */
|
---|
224 | static void
|
---|
225 | CmdInput (char *param1, char *param2)
|
---|
226 | {
|
---|
227 | enum
|
---|
228 | {
|
---|
229 | READ, APPEND, PIPEFROM
|
---|
230 | }
|
---|
231 | mode;
|
---|
232 | FILE *NewInput;
|
---|
233 | char *buf, *s;
|
---|
234 |
|
---|
235 | /* join the two parameters together */
|
---|
236 | buf = Xmalloc (strlen (param1) + strlen (param2) + 1);
|
---|
237 | strcpy (buf, param1);
|
---|
238 | strcat (buf, param2);
|
---|
239 | s = buf;
|
---|
240 |
|
---|
241 |
|
---|
242 | while (*s == ' ' || *s == '\t')
|
---|
243 | s++;
|
---|
244 |
|
---|
245 | if (*s == '<')
|
---|
246 | {
|
---|
247 | s++;
|
---|
248 | mode = READ;
|
---|
249 | }
|
---|
250 | else if (*s == '|')
|
---|
251 | {
|
---|
252 | mode = PIPEFROM;
|
---|
253 | s++;
|
---|
254 | }
|
---|
255 | else
|
---|
256 | {
|
---|
257 | if (InPipe)
|
---|
258 | CLOSEPIPE (InFile); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
259 | else if (InFile != stdin)
|
---|
260 | fclose (InFile);
|
---|
261 | InPipe = 0;
|
---|
262 | InFile = stdin;
|
---|
263 | Xfree (buf);
|
---|
264 | return;
|
---|
265 | }
|
---|
266 |
|
---|
267 | while (*s == ' ' || *s == '\t')
|
---|
268 | s++;
|
---|
269 |
|
---|
270 | if (*s)
|
---|
271 | {
|
---|
272 | if (mode == READ)
|
---|
273 | NewInput = fopen (s, "r");
|
---|
274 | else
|
---|
275 | NewInput = OPENPIPE (s, "r"); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
276 |
|
---|
277 | if (NewInput)
|
---|
278 | {
|
---|
279 | if (InPipe)
|
---|
280 | CLOSEPIPE (InFile); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
281 | else if (InFile != stdin)
|
---|
282 | fclose (InFile);
|
---|
283 | InPipe = (mode == PIPEFROM);
|
---|
284 | InFile = NewInput;
|
---|
285 | }
|
---|
286 | else
|
---|
287 | CommandsErrorStr = "Bad input file/pipe";
|
---|
288 | }
|
---|
289 | else
|
---|
290 | {
|
---|
291 | if (InPipe)
|
---|
292 | CLOSEPIPE (InFile); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
293 | else if (InFile != stdin)
|
---|
294 | fclose (InFile);
|
---|
295 | InPipe = 0;
|
---|
296 | InFile = stdin;
|
---|
297 | }
|
---|
298 | Xfree (buf);
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 |
|
---|
303 | static void
|
---|
304 | CmdOutput (char *param1, char *param2)
|
---|
305 | {
|
---|
306 | enum
|
---|
307 | {
|
---|
308 | WRITE, APPEND, PIPETO
|
---|
309 | }
|
---|
310 | mode;
|
---|
311 | char *buf, *s;
|
---|
312 | FILE *NewOutput;
|
---|
313 |
|
---|
314 | /* join the two parameters together */
|
---|
315 | buf = Xmalloc (strlen (param1) + strlen (param2) + 1);
|
---|
316 | strcpy (buf, param1);
|
---|
317 | strcat (buf, param2);
|
---|
318 | s = buf;
|
---|
319 |
|
---|
320 | while (*s == ' ' || *s == '\t')
|
---|
321 | s++;
|
---|
322 |
|
---|
323 | if (*s == '>')
|
---|
324 | {
|
---|
325 | s++;
|
---|
326 | mode = APPEND;
|
---|
327 | if (*s == '>')
|
---|
328 | s++;
|
---|
329 | else
|
---|
330 | mode = WRITE;
|
---|
331 | }
|
---|
332 | else if (*s == '|')
|
---|
333 | {
|
---|
334 | mode = PIPETO;
|
---|
335 | s++;
|
---|
336 | }
|
---|
337 | else
|
---|
338 | {
|
---|
339 | if (OutPipe)
|
---|
340 | CLOSEPIPE (OutFile); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
341 | else if (OutFile != stdout)
|
---|
342 | fclose (OutFile);
|
---|
343 | OutPipe = 0;
|
---|
344 | OutFile = stdout;
|
---|
345 | Xfree (buf);
|
---|
346 | return;
|
---|
347 | }
|
---|
348 |
|
---|
349 | while (*s == ' ' || *s == '\t')
|
---|
350 | s++;
|
---|
351 |
|
---|
352 | if (*s)
|
---|
353 | {
|
---|
354 | if (mode == PIPETO)
|
---|
355 | NewOutput = OPENPIPE (s, "w"); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
356 | else
|
---|
357 | NewOutput = fopen (s, mode == WRITE ? "w" : "a");
|
---|
358 | if (NewOutput)
|
---|
359 | {
|
---|
360 | if (OutPipe)
|
---|
361 | CLOSEPIPE (OutFile); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
362 | else if (OutFile != stdout)
|
---|
363 | fclose (OutFile);
|
---|
364 | OutPipe = (mode == PIPETO);
|
---|
365 | OutFile = NewOutput;
|
---|
366 | }
|
---|
367 | else
|
---|
368 | CommandsErrorStr = "Bad output file/pipe";
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | if (OutPipe)
|
---|
373 | CLOSEPIPE (OutFile); /* [RPAP - Feb 97: WIN32 Port] */
|
---|
374 | else if (OutFile != stdout)
|
---|
375 | fclose (OutFile);
|
---|
376 | OutPipe = 0;
|
---|
377 | OutFile = stdout;
|
---|
378 | }
|
---|
379 | Xfree (buf);
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 |
|
---|
384 | static char *
|
---|
385 | ParseArg (char *line, char *buffer)
|
---|
386 | {
|
---|
387 | if (*line == '\"')
|
---|
388 | {
|
---|
389 | line++;
|
---|
390 | while (*line != '\"' && *line != '\0')
|
---|
391 | {
|
---|
392 | if (*line == '\\')
|
---|
393 | *buffer++ = *line++;
|
---|
394 | if (*line != '\0')
|
---|
395 | *buffer++ = *line++;
|
---|
396 | }
|
---|
397 | if (*line == '\"')
|
---|
398 | line++;
|
---|
399 | }
|
---|
400 | else
|
---|
401 | while (*line != ' ' && *line != '\t' && *line != '\0')
|
---|
402 | {
|
---|
403 | if (*line == '\\')
|
---|
404 | line++;
|
---|
405 | if (*line != '\0')
|
---|
406 | *buffer++ = *line++;
|
---|
407 | }
|
---|
408 | *buffer = '\0';
|
---|
409 | while (*line == ' ' || *line == '\t')
|
---|
410 | line++;
|
---|
411 | return (line);
|
---|
412 | }
|
---|
413 |
|
---|
414 |
|
---|
415 |
|
---|
416 | char *
|
---|
417 | ProcessCommands (char *line, query_data * qd)
|
---|
418 | {
|
---|
419 | struct
|
---|
420 | {
|
---|
421 | char *Command;
|
---|
422 | void (*action) (void);
|
---|
423 | void (*action1) (char *);
|
---|
424 | void (*action2) (char *, char *);
|
---|
425 | }
|
---|
426 | *dc, dot_command[] =
|
---|
427 | {
|
---|
428 | {
|
---|
429 | "set", NULL, NULL, CmdSet
|
---|
430 | }
|
---|
431 | ,
|
---|
432 | {
|
---|
433 | "input", NULL, NULL, CmdInput
|
---|
434 | }
|
---|
435 | ,
|
---|
436 | {
|
---|
437 | "output", NULL, NULL, CmdOutput
|
---|
438 | }
|
---|
439 | ,
|
---|
440 | {
|
---|
441 | "unset", NULL, CmdUnset, NULL
|
---|
442 | }
|
---|
443 | ,
|
---|
444 | {
|
---|
445 | "help", CmdHelp, NULL, NULL
|
---|
446 | }
|
---|
447 | ,
|
---|
448 | {
|
---|
449 | "reset", CmdReset, NULL, NULL
|
---|
450 | }
|
---|
451 | ,
|
---|
452 | {
|
---|
453 | "display", CmdDisplay, NULL, NULL
|
---|
454 | }
|
---|
455 | ,
|
---|
456 | {
|
---|
457 | "push", CmdPush, NULL, NULL
|
---|
458 | }
|
---|
459 | ,
|
---|
460 | {
|
---|
461 | "quit", CmdQuit, NULL, NULL
|
---|
462 | }
|
---|
463 | ,
|
---|
464 | {
|
---|
465 | "pop", CmdPop, NULL, NULL
|
---|
466 | }
|
---|
467 | ,
|
---|
468 | {
|
---|
469 | "warranty", CmdWarranty, NULL, NULL
|
---|
470 | }
|
---|
471 | ,
|
---|
472 | {
|
---|
473 | "conditions", CmdConditions, NULL, NULL
|
---|
474 | }
|
---|
475 | ,
|
---|
476 | {
|
---|
477 | "queryterms", CmdQueryTerms, NULL, NULL
|
---|
478 | }
|
---|
479 | };
|
---|
480 |
|
---|
481 |
|
---|
482 | char command[512];
|
---|
483 | char param1[512], param2[512];
|
---|
484 | CommandsErrorStr = NULL;
|
---|
485 | the_qd = qd; /* set globally to be accessed by command functions if needed */
|
---|
486 | while (*line == ' ' || *line == '\t')
|
---|
487 | line++;
|
---|
488 | while (*line == '.')
|
---|
489 | {
|
---|
490 | int i;
|
---|
491 | line++;
|
---|
492 | for (i = 0; isalpha (*line) && i < 512; i++, line++)
|
---|
493 | command[i] = tolower (*line);
|
---|
494 | while (*line == ' ' || *line == '\t')
|
---|
495 | line++;
|
---|
496 | if (i != 512)
|
---|
497 | {
|
---|
498 | command[i] = '\0';
|
---|
499 | for (i = 0, dc = dot_command; i < NUMOF (dot_command); i++, dc++)
|
---|
500 | if (!strcmp (dc->Command, command))
|
---|
501 | {
|
---|
502 | if (dc->action)
|
---|
503 | dc->action ();
|
---|
504 | else if (dc->action1)
|
---|
505 | {
|
---|
506 | line = ParseArg (line, param1);
|
---|
507 | dc->action1 (param1);
|
---|
508 | break;
|
---|
509 | }
|
---|
510 | else if (dc->action2)
|
---|
511 | {
|
---|
512 | line = ParseArg (line, param1);
|
---|
513 | line = ParseArg (line, param2);
|
---|
514 | dc->action2 (param1, param2);
|
---|
515 | break;
|
---|
516 | }
|
---|
517 | if (CommandsErrorStr)
|
---|
518 | *line = '\0';
|
---|
519 | break;
|
---|
520 | }
|
---|
521 | if (i == NUMOF (dot_command))
|
---|
522 | i = 100;
|
---|
523 | }
|
---|
524 | if (i == 100)
|
---|
525 | {
|
---|
526 | CommandsErrorStr = "Illegal command";
|
---|
527 | *line = '\0';
|
---|
528 | }
|
---|
529 | while (*line == ' ' || *line == '\t')
|
---|
530 | line++;
|
---|
531 | }
|
---|
532 | return (line);
|
---|
533 | }
|
---|
534 |
|
---|
535 |
|
---|
536 |
|
---|
537 |
|
---|
538 |
|
---|
539 | void
|
---|
540 | read_mgrc_file (void)
|
---|
541 | {
|
---|
542 | char line[1000];
|
---|
543 | char FileName[256];
|
---|
544 | int LineNum = 0;
|
---|
545 | FILE *mgrc;
|
---|
546 |
|
---|
547 | strcpy (FileName, ".mgrc");
|
---|
548 | mgrc = fopen (FileName, "r");
|
---|
549 |
|
---|
550 | if (!mgrc)
|
---|
551 | {
|
---|
552 | char *home_path = getenv ("HOME");
|
---|
553 | if (home_path)
|
---|
554 | {
|
---|
555 | sprintf (FileName, "%s/.mgrc", home_path);
|
---|
556 | mgrc = fopen (FileName, "r");
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | if (!mgrc)
|
---|
561 | return;
|
---|
562 |
|
---|
563 | while (fgets (line, sizeof (line), mgrc))
|
---|
564 | {
|
---|
565 | char *s, linebuf[1000];
|
---|
566 | LineNum++;
|
---|
567 | if ((s = strchr (line, '\n')) != NULL)
|
---|
568 | *s = '\0';
|
---|
569 | strcpy (linebuf, line);
|
---|
570 | if ((s = strchr (line, '#')) != NULL)
|
---|
571 | *s = '\0';
|
---|
572 | if (!*line)
|
---|
573 | continue;
|
---|
574 | ProcessCommands (line, NULL);
|
---|
575 | if (CommandsErrorStr)
|
---|
576 | Message ("%s\n%s : ERROR in line %d of %s\n%s : %s\n",
|
---|
577 | linebuf,
|
---|
578 | msg_prefix, LineNum, FileName,
|
---|
579 | msg_prefix, CommandsErrorStr);
|
---|
580 | }
|
---|
581 | fclose (mgrc);
|
---|
582 | }
|
---|