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