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

Go to the source code of this file.

Functions

static wordlistasubst ()
 
void cp_doalias (wordlist **list)
 
static wordlistasubst (wordlist *wlist)
 
void cp_setalias (char *word, wordlist *wlist)
 
void cp_unalias (char *word)
 
void cp_paliases (char *word)
 
void com_alias (wordlist *wl)
 
void com_unalias (wordlist *wl)
 

Variables

struct aliascp_aliases = NULL
 

Function Documentation

static wordlist* asubst ( )
static
static wordlist* asubst ( wordlist wlist)
static

Definition at line 128 of file alias.c.

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 }
#define eq(a, b)
Definition: misc.h:29
wordlist * hi_wlist
Definition: cpdefs.h:42
struct alias * al_next
Definition: cpdefs.h:60
char * al_name
Definition: cpdefs.h:58
Definition: cddefs.h:169
Definition: library.c:18
void wl_free()
struct wordlist * wl_prev
Definition: cpstd.h:24
wordlist * al_text
Definition: cpdefs.h:59
#define NULL
Definition: spdefs.h:121
Definition: cpstd.h:21
struct histent * cp_lastone
Definition: history.c:34
struct alias * cp_aliases
Definition: alias.c:22
struct wordlist * wl_next
Definition: cpstd.h:23
void cp_histsubst()
char * wl_word
Definition: cpstd.h:22
bool cp_didhsubst
Definition: history.c:38
Definition: cpdefs.h:57
wordlist * wl_copy()
void com_alias ( wordlist wl)

Definition at line 275 of file alias.c.

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 }
#define NULL
Definition: spdefs.h:121
void cp_setalias(char *word, wordlist *wlist)
Definition: alias.c:173
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
void cp_paliases(char *word)
Definition: alias.c:255
void com_unalias ( wordlist wl)

Definition at line 290 of file alias.c.

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 }
#define eq(a, b)
Definition: misc.h:29
void cp_unalias(char *word)
Definition: alias.c:226
struct alias * al_next
Definition: cpdefs.h:60
char * al_name
Definition: cpdefs.h:58
void wl_free()
wordlist * al_text
Definition: cpdefs.h:59
#define tfree(x)
Definition: cdmacs.h:22
#define NULL
Definition: spdefs.h:121
struct alias * cp_aliases
Definition: alias.c:22
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
Definition: cpdefs.h:57
void cp_doalias ( wordlist **  list)

Definition at line 25 of file alias.c.

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 }
#define eq(a, b)
Definition: misc.h:29
wordlist * hi_wlist
Definition: cpdefs.h:42
Definition: cpdefs.h:20
#define alloc(type)
Definition: cdmacs.h:21
void wl_free()
FILE * cp_err
Definition: help.c:101
struct wordlist * wl_prev
Definition: cpstd.h:24
#define tfree(x)
Definition: cdmacs.h:22
Definition: cpdefs.h:40
#define NULL
Definition: spdefs.h:121
static wordlist * asubst()
Definition: cpstd.h:21
struct histent * cp_lastone
Definition: history.c:34
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
char * cp_csep
Definition: front.c:121
wordlist * wl_copy()
void cp_paliases ( char *  word)

Definition at line 255 of file alias.c.

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 }
#define eq(a, b)
Definition: misc.h:29
void out_wlprint()
void out_printf()
struct alias * al_next
Definition: cpdefs.h:60
char * al_name
Definition: cpdefs.h:58
wordlist * al_text
Definition: cpdefs.h:59
#define NULL
Definition: spdefs.h:121
struct alias * cp_aliases
Definition: alias.c:22
void out_send()
Definition: cpdefs.h:57
void cp_setalias ( char *  word,
wordlist wlist 
)

Definition at line 173 of file alias.c.

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 }
void cp_unalias(char *word)
Definition: alias.c:226
struct alias * al_next
Definition: cpdefs.h:60
char * al_name
Definition: cpdefs.h:58
#define alloc(type)
Definition: cdmacs.h:21
char * copy()
wordlist * al_text
Definition: cpdefs.h:59
#define NULL
Definition: spdefs.h:121
#define CT_ALIASES
Definition: cpdefs.h:77
struct alias * al_prev
Definition: cpdefs.h:61
struct alias * cp_aliases
Definition: alias.c:22
void cp_striplist()
void cp_addcomm()
void cp_addkword()
Definition: cpdefs.h:57
wordlist * wl_copy()
void cp_unalias ( char *  word)

Definition at line 226 of file alias.c.

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 }
#define eq(a, b)
Definition: misc.h:29
struct alias * al_next
Definition: cpdefs.h:60
char * al_name
Definition: cpdefs.h:58
void wl_free()
void cp_remkword()
wordlist * al_text
Definition: cpdefs.h:59
#define tfree(x)
Definition: cdmacs.h:22
#define NULL
Definition: spdefs.h:121
#define CT_ALIASES
Definition: cpdefs.h:77
struct alias * al_prev
Definition: cpdefs.h:61
struct alias * cp_aliases
Definition: alias.c:22
Definition: cpdefs.h:57
void cp_remcomm()

Variable Documentation

struct alias* cp_aliases = NULL

Definition at line 22 of file alias.c.