Jspice3
alias.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 alias substitution.
10  */
11 
12 #include "spice.h"
13 #include "cpdefs.h"
14 #include "suffix.h"
15 
16 #ifdef __STDC__
17 static wordlist *asubst(wordlist*);
18 #else
19 static wordlist *asubst();
20 #endif
21 
22 struct alias *cp_aliases = NULL;
23 
24 void
26 
27 wordlist **list;
28 {
29  int ntries;
30  wordlist *realw, *nwl, *nextc, *end;
31  wordlist *comm, *wlist;
32 
33 
34  if (list == NULL)
35  return;
36 
37  wlist = *list;
38 
39  /* strip off leading command separators */
40  while (wlist && eq(wlist->wl_word, cp_csep)) {
41  tfree(wlist->wl_word);
42  wlist = wlist->wl_next;
43  tfree(wlist);
44  }
45  wlist->wl_prev = NULL;
46  *list = wlist;
47 
48  /* The alias process is going to modify the "last" line typed,
49  * so save a copy of what it really is and restore it after
50  * aliasing is done. We have to do tricky things do get around
51  * the problems with ; ...
52  */
53  if (!cp_lastone) {
54  cp_lastone = alloc(struct histent);
55  realw = NULL;
56  }
57  else {
58  realw = wl_copy(cp_lastone->hi_wlist);
59  }
60  comm = wlist;
61  do {
62  end = comm->wl_prev;
63  comm->wl_prev = NULL;
64  for (nextc = comm; nextc; nextc = nextc->wl_next)
65  if (eq(nextc->wl_word, cp_csep)) {
66  if (nextc->wl_prev)
67  nextc->wl_prev->wl_next = NULL;
68  break;
69  }
70 
72  cp_lastone->hi_wlist = wl_copy(comm);
73  for (ntries = 21; ntries; ntries--) {
74  nwl = asubst(comm);
75  if (nwl == NULL)
76  break;
77  if (eq(nwl->wl_word, comm->wl_word)) {
78  /* Just once through... */
79  wl_free(comm);
80  comm = nwl;
81  break;
82  }
83  else {
84  wl_free(comm);
85  comm = nwl;
86  }
87  }
88 
89  if (!ntries) {
90  fprintf(cp_err, "Error: alias loop.\n");
91  wl_free(wlist);
92  *list = NULL;
93  return;
94  }
95  comm->wl_prev = end;
96  if (!end)
97  wlist = comm;
98  else
99  end->wl_next = comm;
100  while (comm->wl_next)
101  comm = comm->wl_next;
102  comm->wl_next = nextc;
103  if (nextc) {
104  nextc->wl_prev = comm;
105  nextc = nextc->wl_next;
106  comm = nextc;
107  }
108  } while (nextc);
109 
111  if (realw) {
112  cp_lastone->hi_wlist = realw;
113  }
114  else {
115  tfree(cp_lastone);
116  }
117 
118  *list = wlist;
119  return;
120 }
121 
122 /* Return NULL if no alias was found. We can get away with just calling
123  * cp_histsubst now because the line will have gone onto the history list
124  * by now and cp_histsubst will look in the right place.
125  */
126 
127 static wordlist *
128 asubst(wlist)
129 
130 wordlist *wlist;
131 {
132  struct alias *al;
133  wordlist *wl, *w = NULL;
134  char *word;
135 
136  word = wlist->wl_word;
137 
138  if (*word == '\\') {
139  while (*word) {
140  *word = *(word+1);
141  word++;
142  }
143  return (NULL);
144  }
145  for (al = cp_aliases; al; al = al->al_next)
146  if (eq(word, al->al_name))
147  break;
148  if (!al)
149  return (NULL);
150 
151  wl = wl_copy(al->al_text);
152  cp_histsubst(&wl);
153 
154  if (cp_didhsubst) {
155  /* Make sure that we have an up-to-date last history entry. */
157  cp_lastone->hi_wlist = wl_copy(wl);
158  }
159  else {
160  /* If it had no history args, then append the rest of the wl */
161  for (w = wl; w->wl_next; w = w->wl_next);
162  w->wl_next = wl_copy(wlist->wl_next);
163  if (w->wl_next)
164  w->wl_next->wl_prev = w;
165  }
166  return (wl);
167 }
168 
169 
170 /* If we use this, aliases will be in alphabetical order. */
171 
172 void
173 cp_setalias(word, wlist)
174 
175 char *word;
176 wordlist *wlist;
177 {
178  struct alias *al, *ta;
179 
180  cp_unalias(word);
181  cp_addkword(CT_ALIASES, word);
182  if (cp_aliases == NULL) {
183  /* printf("first one...\n"); */
184  al = cp_aliases = alloc(struct alias);
185  }
186  else {
187  /* printf("inserting %s: %s ...\n", word, wlist->wl_word); */
188  for (al = cp_aliases; al->al_next; al = al->al_next) {
189  /* printf("checking %s...\n", al->al_name); */
190  if (strcmp(al->al_name, word) > 0)
191  break;
192  }
193  /* The new one goes before al */
194  if (al->al_prev) {
195  al = al->al_prev;
196  ta = al->al_next;
197  al->al_next = alloc(struct alias);
198  al->al_next->al_prev = al;
199  al = al->al_next;
200  al->al_next = ta;
201  ta->al_prev = al;
202  }
203  else {
204  cp_aliases = alloc(struct alias);
205  cp_aliases->al_next = al;
206  al->al_prev = cp_aliases;
207  al = cp_aliases;
208  }
209  }
210  al->al_name = copy(word);
211  al->al_text = wl_copy(wlist);
212  cp_striplist(al->al_text);
213  /* We can afford to not worry about the bits, because before
214  * the keyword lookup is done the alias is evaluated.
215  * Make everything file completion, just in case...
216  */
217  cp_addcomm(word, (long) 1, (long) 1, (long) 1, (long) 1);
218  /* printf("word %s, next = %s, prev = %s...\n", al->al_name,
219  al->al_next ? al->al_next->al_name : "(none)",
220  al->al_prev ? al->al_prev->al_name : "(none)"); */
221  return;
222 }
223 
224 
225 void
227 
228 char *word;
229 {
230  struct alias *al;
231 
232  cp_remkword(CT_ALIASES, word);
233  for (al = cp_aliases; al; al = al->al_next)
234  if (eq(word, al->al_name))
235  break;
236  if (al == NULL)
237  return;
238  if (al->al_next)
239  al->al_next->al_prev = al->al_prev;
240  if (al->al_prev)
241  al->al_prev->al_next = al->al_next;
242  else {
243  al->al_next->al_prev = NULL;
244  cp_aliases = al->al_next;
245  }
246  wl_free(al->al_text);
247  tfree(al->al_name);
248  tfree(al);
249  cp_remcomm(word);
250  return;
251 }
252 
253 
254 void
256 
257 char *word;
258 {
259  struct alias *al;
260 
261  for (al = cp_aliases; al; al = al->al_next)
262  if ((word == NULL) || eq(al->al_name, word)) {
263  if (!word)
264  out_printf("%s\t", al->al_name);
265  out_wlprint(al->al_text);
266  out_send("\n");
267  }
268  return;
269 }
270 
271 
272 /* The routine for the "alias" command. */
273 
274 void
276 
277 wordlist *wl;
278 {
279  if (wl == NULL)
280  cp_paliases((char *) NULL);
281  else if (wl->wl_next == NULL)
282  cp_paliases(wl->wl_word);
283  else
284  cp_setalias(wl->wl_word, wl->wl_next);
285  return;
286 }
287 
288 
289 void
291 
292 wordlist *wl;
293 {
294  struct alias *al, *na;
295 
296  if (eq(wl->wl_word, "*")) {
297  for (al = cp_aliases; al; al = na) {
298  na = al->al_next;
299  wl_free(al->al_text);
300  tfree(al->al_name);
301  tfree(al);
302  }
303  cp_aliases = NULL;
304  wl = wl->wl_next;
305  }
306  while (wl != NULL) {
307  cp_unalias(wl->wl_word);
308  wl = wl->wl_next;
309  }
310  return;
311 }
312 
#define eq(a, b)
Definition: misc.h:29
void cp_unalias(char *word)
Definition: alias.c:226
void out_wlprint()
wordlist * hi_wlist
Definition: cpdefs.h:42
void out_printf()
struct alias * al_next
Definition: cpdefs.h:60
char * al_name
Definition: cpdefs.h:58
Definition: cddefs.h:169
Definition: library.c:18
Definition: cpdefs.h:20
#define alloc(type)
Definition: cdmacs.h:21
char * copy()
void wl_free()
void com_unalias(wordlist *wl)
Definition: alias.c:290
FILE * cp_err
Definition: help.c:101
struct wordlist * wl_prev
Definition: cpstd.h:24
void cp_remkword()
wordlist * al_text
Definition: cpdefs.h:59
#define tfree(x)
Definition: cdmacs.h:22
Definition: cpdefs.h:40
#define NULL
Definition: spdefs.h:121
static wordlist * asubst()
#define CT_ALIASES
Definition: cpdefs.h:77
void com_alias(wordlist *wl)
Definition: alias.c:275
struct alias * al_prev
Definition: cpdefs.h:61
Definition: cpstd.h:21
struct histent * cp_lastone
Definition: history.c:34
void cp_setalias(char *word, wordlist *wlist)
Definition: alias.c:173
void cp_doalias(wordlist **list)
Definition: alias.c:25
struct alias * cp_aliases
Definition: alias.c:22
Definition: dir.c:53
void out_send()
struct wordlist * wl_next
Definition: cpstd.h:23
void cp_striplist()
void cp_histsubst()
char * wl_word
Definition: cpstd.h:22
void cp_addcomm()
void cp_addkword()
bool cp_didhsubst
Definition: history.c:38
void cp_paliases(char *word)
Definition: alias.c:255
Definition: cpdefs.h:57
void cp_remcomm()
char * cp_csep
Definition: front.c:121
wordlist * wl_copy()