source: trunk/gsdl/src/mgpp/text/commands.cpp@ 711

Last change on this file since 711 was 711, checked in by cs025, 25 years ago

Changes to eradicate Xmalloc

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