Jspice3
backq.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1985 Wayne A. Christopher
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * Do backquote substitution on a word list.
10  */
11 
12 #include "spice.h"
13 #include "misc.h"
14 #include "cpdefs.h"
15 #include "suffix.h"
16 
17 #ifdef __STDC__
18 static wordlist *backeval(char*);
19 #else
20 static wordlist *backeval();
21 #endif
22 
23 char cp_back = '`';
24 
25 
26 void
28 
29 wordlist **list;
30 {
31  wordlist *wl, *nwl, *owl, *wlist;
32  char *s, *t, buf[BSIZE_SP], wbuf[BSIZE_SP];
33  int i;
34 
35  if (list == NULL)
36  return;
37  wlist = *list;
38 
39  for (wl = wlist; wl; wl = wl->wl_next) {
40  t = wl->wl_word;
41  if (!t)
42  continue;
43  i = 0;
44 
45  while (s = strchr(t, cp_back)) {
46  while (t < s)
47  wbuf[i++] = *t++;
48  wbuf[i] = '\0';
49  (void) strcpy(buf, ++s);
50  s = buf;
51  t++;
52  while (*s && (*s != cp_back)) {
53  /* Get s and t past the next backquote. */
54  t++;
55  s++;
56  }
57  /* What the heck, let "echo `foo" work... */
58  *s = '\0';
59  t++; /* Get past the second ` */
60  if (!(nwl = backeval(buf)) || !nwl->wl_word) {
61  wl_free(wlist);
62  *list = NULL;
63  return;
64  }
65  if (i) {
66  (void) strcpy(buf, wbuf);
67  if (nwl->wl_word) {
68  (void) strcat(buf, nwl->wl_word);
69  tfree(nwl->wl_word);
70  }
71  nwl->wl_word = copy(buf);
72  }
73  owl = wl;
74  wl = wl_splice(wl, nwl);
75  if (owl == wlist)
76  wlist = nwl;
77  (void) strcpy(buf, wl->wl_word);
78  i = strlen(buf);
79  (void) strcat(buf, t);
80  tfree(wl->wl_word);
81  wl->wl_word = copy(buf);
82  t = &wl->wl_word[i];
83  s = wl->wl_word;
84  for (i = 0; s < t; s++)
85  wbuf[i++] = *s;
86  }
87  }
88  *list = wlist;
89  return;
90 }
91 
92 
93 /* Do a popen with the string, and then reset the file pointers so that
94  * we can use the first pass of the parser on the output.
95  */
96 
97 static wordlist *
98 backeval(string)
99 
100 char *string;
101 {
102  FILE *proc, *old;
103  wordlist *wl;
104  bool intv;
105  extern FILE *popen( );
106 
107  proc = popen(string, "r");
108  if (proc == NULL) {
109  fprintf(cp_err, "Error: can't evaluate %s.\n", string);
110  return (NULL);
111  }
112  old = cp_inp_cur;
113  cp_inp_cur = proc;
114  intv = cp_interactive;
115  cp_interactive = false;
116  cp_bqflag = true;
117  wl = cp_lexer((char *) NULL);
118  cp_bqflag = false;
119  cp_inp_cur = old;
120  cp_interactive = intv;
121  (void) pclose(proc);
122  return (wl);
123 }
static char buf[MAXPROMPT]
Definition: arg.c:18
#define BSIZE_SP
Definition: misc.h:19
bool cp_bqflag
Definition: lexical.c:51
char * strcpy()
Definition: cddefs.h:119
Definition: library.c:18
char * copy()
void wl_free()
FILE * cp_err
Definition: help.c:101
int pclose(FILE *fp)
Definition: libfuncs.c:41
bool cp_interactive
Definition: help.c:100
#define tfree(x)
Definition: cdmacs.h:22
char cp_back
Definition: backq.c:23
#define NULL
Definition: spdefs.h:121
wordlist * wl_splice()
wordlist * cp_lexer()
Definition: cpstd.h:21
FILE * cp_inp_cur
Definition: lexical.c:48
Definition: dir.c:53
struct wordlist * wl_next
Definition: cpstd.h:23
void cp_bquote(wordlist **list)
Definition: backq.c:27
char * wl_word
Definition: cpstd.h:22
static wordlist * backeval()
FILE * popen(char *cmd, char *mode) const
Definition: libfuncs.c:20
Definition: cddefs.h:192