Jspice3
cshpar.c File Reference
#include "spice.h"
#include "cpdefs.h"
#include "scedio.h"
Include dependency graph for cshpar.c:

Go to the source code of this file.

Macros

#define SHORT_SCEDIO
 

Functions

static void pwlist ()
 
static bool fileexists ()
 
void cp_init ()
 
wordlistcp_parse (char *string)
 
static void pwlist (wordlist *wlist, char *name)
 
void cp_redirect (wordlist **list)
 
void cp_ioreset ()
 
static bool fileexists (char *name)
 
void com_shell (wordlist *wl)
 
void cp_fixdescriptors ()
 
void com_rehash (wordlist *wl)
 
void com_chdir (wordlist *wl)
 
void com_strcmp (wordlist *wl)
 

Variables

bool cp_debug = false
 
char cp_gt = '>'
 
char cp_lt = '<'
 
char cp_amp = '&'
 
FILE * cp_in
 
FILE * cp_out
 
FILE * cp_err
 
FILE * cp_curin = NULL
 
FILE * cp_curout = NULL
 
FILE * cp_curerr = NULL
 
char cp_chars [128]
 

Macro Definition Documentation

#define SHORT_SCEDIO

Definition at line 14 of file cshpar.c.

Function Documentation

void com_chdir ( wordlist wl)

Definition at line 558 of file cshpar.c.

561 {
562  fprintf(cp_err, "Error: 'chdir' not available.\n");
563  return;
564 }
FILE * cp_err
Definition: cshpar.c:69
void com_rehash ( wordlist wl)

Definition at line 468 of file cshpar.c.

471 {
472  char *s;
473 
474  if (!cp_dounixcom) {
475  fprintf(cp_err, "Error: unixcom not set.\n");
476  return;
477  }
478  s = getenv("PATH");
479  if (s)
480  cp_rehash(s, !cp_nocc);
481  else
482  fprintf(cp_err, "Error: no PATH in environment.\n");
483  return;
484 }
bool cp_dounixcom
Definition: front.c:123
FILE * cp_err
Definition: cshpar.c:69
Definition: cddefs.h:119
char * getenv(char *c)
Definition: libfuncs.c:106
bool cp_nocc
Definition: complete.c:75
void cp_rehash()
void com_shell ( wordlist wl)

Definition at line 391 of file cshpar.c.

394 {
395  char *com, *shell = NULL;
396  long pid, r;
397 #ifdef HAVE_SIGNAL
398  static RETSIGTYPE (*svint)( ), (*svquit)( ), (*svtstp)( );
399 #endif
400 
401  if (SCEDactive()) {
402  ShowPrompt("Can't do this from sced.");
403  return;
404  }
405 
406  shell = getenv("SHELL");
407  if (shell == NULL)
408  shell = "/bin/csh";
409  cp_ccon(false);
410 
411 #ifdef HAVE_FORK
412  pid = fork();
413  if (pid == 0) {
415  if (wl == NULL) {
416  (void) execl(shell, shell, 0);
417  _exit(99);
418  }
419  else {
420  com = wl_flatten(wl);
421  (void) execl("/bin/sh", "sh", "-c", com, 0);
422  }
423  }
424  else {
425  /* XXX Better have all these signals */
426  svint = signal(SIGINT, SIG_DFL);
427  svquit = signal(SIGQUIT, SIG_DFL);
428  svtstp = signal(SIGTSTP, SIG_DFL);
429  /* XXX Sig on proc group */
430  do {
431  r = wait(NULL);
432  } while ((r != pid) && pid != -1);
433  (void) signal(SIGINT, svint);
434  (void) signal(SIGQUIT, svquit);
435  (void) signal(SIGTSTP, svtstp);
436  }
437 #else
438  /* Easier to forget about changing the io descriptors. */
439  if (wl) {
440  com = wl_flatten(wl);
441  (void) system(com);
442  tfree(com);
443  }
444  else
445  (void) system(shell);
446 #endif
447  return;
448 }
void cp_fixdescriptors()
Definition: cshpar.c:454
int system(char *str)
Definition: libfuncs.c:85
char * getenv(char *c)
Definition: libfuncs.c:106
#define tfree(x)
Definition: cdmacs.h:22
#define NULL
Definition: spdefs.h:121
Definition: cddefs.h:162
enum Active SCEDactive()
Definition: scedstub.c:63
char * wl_flatten()
void ShowPrompt(char *str)
Definition: scedstub.c:71
void cp_ccon()
void com_strcmp ( wordlist wl)

Definition at line 572 of file cshpar.c.

575 {
576  char *var, *s1, *s2;
577  int i;
578 
579  var = wl->wl_word;
580  s1 = copy(wl->wl_next->wl_word);
581  cp_unquote(s1);
582  s2 = copy(wl->wl_next->wl_next->wl_word);
583  cp_unquote(s2);
584 
585  i = strcmp(s1, s2);
586  tfree(s1);
587  tfree(s2);
588 
589  cp_vset(var, VT_NUM, (char *) &i);
590  return;
591 }
void cp_vset()
void cp_unquote()
char * copy()
#define tfree(x)
Definition: cdmacs.h:22
#define VT_NUM
Definition: cpstd.h:61
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
void cp_fixdescriptors ( )

Definition at line 454 of file cshpar.c.

455 {
456  if (cp_in != stdin)
457  (void) dup2(fileno(cp_in), fileno(stdin));
458  if (cp_out != stdout)
459  (void) dup2(fileno(cp_out), fileno(stdout));
460  if (cp_err != stderr)
461  (void) dup2(fileno(cp_err), fileno(stderr));
462  return;
463 }
FILE * cp_err
Definition: cshpar.c:69
FILE * cp_in
Definition: cshpar.c:67
FILE * cp_out
Definition: cshpar.c:68
void cp_init ( )

Definition at line 83 of file cshpar.c.

86 {
87  char *s, *singlec = "<>;&";
88 
90  bzero(cp_chars, 128);
91  for (s = singlec; *s; s++)
92  cp_chars[*s] = (CPC_BRR | CPC_BRL);
93  cp_vset("history", VT_NUM, (char *) &cp_maxhistlength);
94 
95  cp_curin = stdin;
96  cp_curout = stdout;
97  cp_curerr = stderr;
98 
99  cp_ioreset();
100 }
void cp_vset()
Definition: cddefs.h:119
#define CPC_BRL
Definition: cpdefs.h:67
void cp_internal_init()
Definition: options.c:555
FILE * cp_curout
Definition: cshpar.c:76
int bzero(char *ptr, int num)
Definition: string.c:357
void cp_ioreset()
Definition: cshpar.c:349
#define VT_NUM
Definition: cpstd.h:61
FILE * cp_curerr
Definition: cshpar.c:77
#define CPC_BRR
Definition: cpdefs.h:66
char cp_chars[128]
Definition: cshpar.c:79
FILE * cp_curin
Definition: cshpar.c:75
int cp_maxhistlength
Definition: history.c:35
void cp_ioreset ( )

Definition at line 349 of file cshpar.c.

350 {
351  // Careful not to double-close ">&" output.
352  FILE *fo = 0;
353  if (cp_in != cp_curin) {
354  if (cp_in && cp_in != stdin)
355  (void) fclose(cp_in);
356  cp_in = cp_curin;
357  }
358  if (cp_out != cp_curout) {
359  if (cp_out && cp_out != stdout) {
360  fo = cp_out;
361  (void) fclose(cp_out);
362  }
363  cp_out = cp_curout;
364  }
365  if (cp_err != cp_curerr) {
366  if (cp_err && cp_err != stderr && cp_err != stdout && cp_err != fo)
367  (void) fclose(cp_err);
368  cp_err = cp_curerr;
369  }
370 
371  /*** Minor bug here... */
372  out_isatty = true;
373  return;
374 }
FILE * cp_err
Definition: cshpar.c:69
FILE * cp_curout
Definition: cshpar.c:76
FILE * cp_in
Definition: cshpar.c:67
bool out_isatty
Definition: output.c:45
FILE * cp_curerr
Definition: cshpar.c:77
FILE * cp_curin
Definition: cshpar.c:75
FILE * cp_out
Definition: cshpar.c:68
wordlist* cp_parse ( char *  string)

Definition at line 104 of file cshpar.c.

107 {
108  wordlist *wlist;
109 
110  wlist = cp_lexer(string);
111 
112  if (!string)
113  cp_event++;
114 
115  if (!wlist || !wlist->wl_word)
116  return (wlist);
117 
118  pwlist(wlist, "Initial parse");
119 
120  cp_histsubst(&wlist);
121  if (!wlist || !wlist->wl_word)
122  return (wlist);
123 
124  pwlist(wlist, "After history substitution");
125 
126  if (cp_interactive && cp_didhsubst) {
127  wl_print(wlist, stdout);
128  (void) putc('\n', stdout);
129  }
130 
131  /* Add the word list to the history. */
132  if (!string && *wlist->wl_word)
133  cp_addhistent(cp_event - 1, wlist);
134 
135  cp_doalias(&wlist);
136  pwlist(wlist, "After alias substitution");
137 
138  return (wlist);
139 }
void wl_print()
int cp_event
Definition: lexical.c:49
void cp_addhistent()
bool cp_interactive
Definition: help.c:100
void cp_doalias()
wordlist * cp_lexer()
Definition: cpstd.h:21
void cp_histsubst()
char * wl_word
Definition: cpstd.h:22
bool cp_didhsubst
Definition: history.c:38
static void pwlist()
void cp_redirect ( wordlist **  list)

Definition at line 197 of file cshpar.c.

200 {
201  bool gotinput = false, gotoutput = false, goterror = false;
202  bool app = false, erralso = false;
203  wordlist *w, *wl, *bt, *nw;
204  char *s;
205  FILE *tmpfp;
206 
207  if (list == NULL)
208  return;
209  wl = *list;
210  w = wl->wl_next; /* Don't consider empty commands. */
211  while (w) {
212  if (*w->wl_word == cp_lt) {
213  bt = w;
214  if (gotinput) {
215  fprintf(cp_err,
216  "Error: ambiguous input redirect.\n");
217  goto error;
218  }
219  gotinput = true;
220  w = w->wl_next;
221  if (w == NULL) {
222  fprintf(cp_err,
223  "Error: missing name for input.\n");
224  goto error;
225  }
226  if (*w->wl_word == cp_lt) {
227  /* Do reasonable stuff here... */
228  }
229  else {
230  char *t = copy(w->wl_word);
231  cp_unquote(t);
232  tmpfp = fopen(t, "r");
233  tfree(t);
234  if (!tmpfp) {
235  perror(w->wl_word);
236  goto error;
237  }
238  else
239  cp_in = tmpfp;
240  }
241 #ifdef CPDEBUG
242  if (cp_debug)
243  fprintf(cp_err, "Input file is %s...\n",
244  w->wl_word);
245 #endif
246  bt->wl_prev->wl_next = w->wl_next;
247  if (w->wl_next)
248  w->wl_next->wl_prev = bt->wl_prev;
249  nw = w->wl_next;
250  w->wl_next = NULL;
251  w = nw;
252  wl_free(bt);
253  }
254  else if (*w->wl_word == cp_gt) {
255  bt = w;
256  if (gotoutput) {
257  fprintf(cp_err,
258  "Error: ambiguous output redirect.\n");
259  goto error;
260  }
261  gotoutput = true;
262  w = w->wl_next;
263  if (w == NULL) {
264  fprintf(cp_err,
265  "Error: missing name for output.\n");
266  goto error;
267  }
268  if (*w->wl_word == cp_gt) {
269  app = true;
270  w = w->wl_next;
271  if (w == NULL) {
272  fprintf(cp_err,
273  "Error: missing name for output.\n");
274  goto error;
275  }
276  }
277  if (*w->wl_word == cp_amp) {
278  erralso = true;
279  if (goterror) {
280  fprintf(cp_err,
281  "Error: ambiguous error redirect.\n");
282  goto error;
283  }
284  goterror = true;
285  w = w->wl_next;
286  if (w == NULL) {
287  fprintf(cp_err,
288  "Error: missing name for output.\n");
289  goto error;
290  }
291  }
292  s = copy(w->wl_word);
293  cp_unquote(s);
294  if (cp_noclobber && fileexists(s)) {
295  fprintf(stderr, "Error: %s: file exists\n", s);
296  tfree(s);
297  goto error;
298  }
299  if (app)
300  tmpfp = fopen(s, "a");
301  else
302  tmpfp = fopen(s, "w+");
303  tfree(s);
304  if (!tmpfp) {
305  perror(w->wl_word);
306  goto error;
307  }
308  else {
309  cp_out = tmpfp;
310  out_isatty = false;
311  }
312 #ifdef CPDEBUG
313  if (cp_debug)
314  fprintf(cp_err, "Output file is %s... %s\n",
315  w->wl_word, app ? "(append)" : "");
316 #endif
317  bt->wl_prev->wl_next = w->wl_next;
318  if (w->wl_next)
319  w->wl_next->wl_prev = bt->wl_prev;
320  w = w->wl_next;
321  if (w)
322  w->wl_prev->wl_next = NULL;
323  wl_free(bt);
324  if (erralso)
325  cp_err = cp_out;
326  }
327  else
328  w = w->wl_next;
329  }
330  *list = wl;
331  return;
332 
333 error:
334  wl_free(wl);
335  *list = NULL;
336  return;
337 }
static FILE * tmpfp
Definition: moretext.c:27
FILE * cp_err
Definition: cshpar.c:69
bool cp_debug
Definition: cshpar.c:61
Definition: cddefs.h:119
bool cp_noclobber
Definition: variable.c:48
char cp_gt
Definition: cshpar.c:63
Definition: cddefs.h:169
Definition: library.c:18
void cp_unquote()
char * copy()
FILE * cp_in
Definition: cshpar.c:67
void wl_free()
char cp_amp
Definition: cshpar.c:65
struct wordlist * wl_prev
Definition: cpstd.h:24
#define tfree(x)
Definition: cdmacs.h:22
#define NULL
Definition: spdefs.h:121
bool out_isatty
Definition: output.c:45
void perror()
char cp_lt
Definition: cshpar.c:64
Definition: cpstd.h:21
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
static bool fileexists()
Definition: cddefs.h:192
FILE * cp_out
Definition: cshpar.c:68
static bool fileexists ( )
static
static bool fileexists ( char *  name)
static

Definition at line 378 of file cshpar.c.

381 {
382  if (access(name, 0) == 0)
383  return (true);
384  return (false);
385 }
int access(char *pth, int m)
Definition: libfuncs.c:75
static void pwlist ( )
static
static void pwlist ( wordlist wlist,
char *  name 
)
static

Definition at line 143 of file cshpar.c.

147 {
148  wordlist *wl;
149 
150  if (!cp_debug)
151  return;
152  fprintf(cp_err, "%s : [ ", name);
153  for (wl = wlist; wl; wl = wl->wl_next)
154  fprintf(cp_err, "%s ", wl->wl_word);
155  fprintf(cp_err, "]\n");
156  return;
157 }
FILE * cp_err
Definition: cshpar.c:69
bool cp_debug
Definition: cshpar.c:61
Definition: library.c:18
Definition: cpstd.h:21
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22

Variable Documentation

char cp_amp = '&'

Definition at line 65 of file cshpar.c.

char cp_chars[128]

Definition at line 79 of file cshpar.c.

FILE* cp_curerr = NULL

Definition at line 77 of file cshpar.c.

FILE* cp_curin = NULL

Definition at line 75 of file cshpar.c.

FILE* cp_curout = NULL

Definition at line 76 of file cshpar.c.

bool cp_debug = false

Definition at line 61 of file cshpar.c.

FILE* cp_err

Definition at line 69 of file cshpar.c.

char cp_gt = '>'

Definition at line 63 of file cshpar.c.

FILE* cp_in

Definition at line 67 of file cshpar.c.

char cp_lt = '<'

Definition at line 64 of file cshpar.c.

FILE* cp_out

Definition at line 68 of file cshpar.c.