Jspice3
unixcom.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: 1987 Wayne A. Christopher
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * Routines to do execution of operating system commands.
10  */
11 
12 #include "spice.h"
13 #include "cpdefs.h"
14 #define SHORT_SCEDIO
15 #include "scedio.h"
16 
17 #include <sys/types.h>
18 #ifdef HAVE_DIRENT_H
19 #include <dirent.h>
20 #ifndef direct
21 #define direct dirent
22 #endif
23 #else
24 #ifdef HAVE_SYS_DIR_H
25 #include <sys/dir.h>
26 #endif
27 #endif
28 
29 #ifdef __STDC__
30 static void upd_hash(bool);
31 static void add_dir(char*,bool,void**);
32 #else
33 static void upd_hash();
34 static void add_dir();
35 #endif
36 
37 static void *hashtabp; /* before . in path */
38 static void *hashtabc; /* . */
39 static void *hashtabs; /* after . in path */
40 static char *dirbuffer;
41 static int dirlength, dirpos;
42 
43 
44 /* Create the hash table for the given search path. pathlist is a : seperated
45  * list of directories. If docc is true, then all the commands found are
46  * added to the command completion lists.
47  */
48 
49 void
50 cp_rehash(pathlist, docc)
51 
52 char *pathlist;
53 bool docc;
54 {
55  int i, tabp;
56  char buf[BSIZE_SP];
57 
58  if (!pathlist) {
59  /* called from the cd command */
60  upd_hash(docc);
61  return;
62  }
63 
64  /* Clear out the old hash table. */
65  htab_free(hashtabp,true);
66  htab_free(hashtabc,true);
67  htab_free(hashtabs,true);
68 
69  if (docc) {
70  struct comm *c;
71  cp_ccrestart(false);
72  for (c = cp_coms; c->co_func; c++)
74  c->co_cctypes[1], c->co_cctypes[2],c->co_cctypes[3]);
75  }
76 
77  tabp = 0;
78  while (pathlist && *pathlist) {
79  i = 0;
80  while (*pathlist && *pathlist != ';' && *pathlist != ':')
81  buf[i++] = *pathlist++;
82  while (*pathlist == ';' || *pathlist == ':')
83  pathlist++;
84  buf[i] = '\0';
85  if (!strcmp(buf,".")) {
86  if (tabp)
87  continue;
88  tabp++;
89  }
90  if (tabp == 0)
91  add_dir(buf,docc,&hashtabp);
92  else if (tabp == 1) {
93  add_dir(buf,docc,&hashtabc);
94  tabp++;
95  }
96  else
97  add_dir(buf,docc,&hashtabs);
98  }
99  return;
100 }
101 
102 
103 static void
104 upd_hash(docc)
105 
106 /* update hash tables - quicker than rebuilding after cd */
107 bool docc;
108 {
109  wordlist *wl, *wl0;
110 
111  wl0 = wl = (wordlist*)htab_wl(hashtabc);
112  while (wl) {
113  if (!htab_get(wl->wl_word,hashtabp) &&
114  !htab_get(wl->wl_word,hashtabs)) {
115  cp_remcomm(wl->wl_word);
116  }
117  wl = wl->wl_next;
118  }
119  wl_free(wl0);
120 
121  htab_free(hashtabc,true);
122  hashtabc = (void*)NULL;
123  add_dir(".",docc,&hashtabc);
124 }
125 
126 
127 bool
129 
130 /* The return value is false if no command was found, and true if it was. */
131 wordlist *wl;
132 {
133  int i;
134  bool ret;
135  char *name;
136  char *path;
137  char **argv;
138  char buf[BSIZE_SP];
139 
140  if (SCEDactive()) {
141  ShowPrompt("Can't do this from sced.");
142  return (true);
143  }
144 
145  if (!wl)
146  return (false);
147  name = wl->wl_word;
148  argv = wl_mkvec(wl);
149  if (cp_debug) {
150  printf("name: %s, argv: ", name);
151  wl_print(wl, stdout);
152  printf("\n");
153  }
154  if (strchr(name, DIR_TERM)) {
155  ret = tryexec(name, argv);
156  goto end;
157  }
158  path = (char*) htab_get(name,hashtabp);
159  if (path) {
160  (void) sprintf(buf,"%s%c%s",path,DIR_TERM,name);
161  ret = tryexec(buf, argv);
162  goto end;
163  }
164  path = (char*) htab_get(name,hashtabc);
165  if (path) {
166  (void) sprintf(buf,"%s",name);
167  ret = tryexec(buf, argv);
168  goto end;
169  }
170  path = (char*) htab_get(name,hashtabs);
171  if (path) {
172  (void) sprintf(buf,"%s%c%s",path,DIR_TERM,name);
173  ret = tryexec(buf, argv);
174  goto end;
175  }
176  ret = false;
177 end:
178  for (i = 0; argv[i]; i++)
179  tfree(argv[i]);
180  tfree(argv);
181  return (ret);
182 }
183 
184 
185 static void
186 add_dir(dir,docc,htab)
187 
188 char *dir;
189 bool docc;
190 void **htab;
191 {
192  DIR *wdir;
193  struct direct *de;
194 
195  if (*htab == NULL)
196  *htab = htab_init();
197 
198  if (!(wdir = opendir(dir))) return;
199  while (de = readdir(wdir)) {
200  if (!is_exec(de->d_name,dir))
201  continue;
202  htab_add(de->d_name,(void*)copy(dir),*htab);
203  if (docc) {
204  /* Add to completion hash table. */
205  cp_addcomm(de->d_name,0L,0L,0L,0L);
206  }
207  }
208  closedir(wdir);
209 }
210 
211 
212 /* Debugging. */
213 
214 void
216 {
217  htab_print(hashtabp,"i = %d, name = %s, path = %s\n");
218  htab_print(hashtabc,"i = %d, name = %s, path = %s\n");
219  htab_print(hashtabs,"i = %d, name = %s, path = %s\n");
220 }
221 
222 
223 #ifdef notdef
224 /* if above doesn't work, use this */
225 
226 void
227 cp_rehash(pathlist, docc)
228 
229 char *pathlist;
230 bool docc;
231 { }
232 
233 
234 bool
235 cp_unixcom(wl)
236 
237 wordlist *wl;
238 {
239  char *s = wl_flatten(wl);
240 
241  if (system(s))
242  return (false);
243  else
244  return (true);
245 }
246 
247 #endif
static char buf[MAXPROMPT]
Definition: arg.c:18
static void add_dir()
#define BSIZE_SP
Definition: misc.h:19
void wl_print()
static char * dirbuffer
Definition: unixcom.c:40
Definition: cddefs.h:119
static void * hashtabs
Definition: unixcom.c:39
int system(char *str)
Definition: libfuncs.c:85
Definition: library.c:18
struct comm * cp_coms
Definition: main.c:163
int bool
Definition: cpstd.h:16
#define L
Definition: parse.c:442
Definition: cpdefs.h:20
char * copy()
void * htab_wl()
static void * hashtabp
Definition: unixcom.c:37
static void upd_hash()
void wl_free()
bool is_exec(char *file, char *dir)
Definition: exec.c:79
static void * hashtabc
Definition: unixcom.c:38
void * htab_init()
Definition: hash.c:27
char ** wl_mkvec()
void htab_free()
#define tfree(x)
Definition: cdmacs.h:22
static int dirpos
Definition: unixcom.c:41
#define NULL
Definition: spdefs.h:121
static int dirlength
Definition: unixcom.c:41
static double c
Definition: vectors.c:16
bool tryexec(char *name, argv)
Definition: exec.c:24
Definition: cpstd.h:21
bool cp_unixcom(wordlist *wl)
Definition: unixcom.c:128
void cp_ccrestart()
void htab_print()
void cp_hstat()
Definition: unixcom.c:215
void htab_add()
void * htab_get()
struct wordlist * wl_next
Definition: cpstd.h:23
char * wl_word
Definition: cpstd.h:22
void(* co_func)()
Definition: cpdefs.h:22
enum Active SCEDactive()
Definition: scedstub.c:63
void cp_addcomm()
void cp_rehash(char *pathlist, bool docc)
Definition: unixcom.c:50
static char * path
Definition: paths.c:13
char * co_comname
Definition: cpdefs.h:21
char * wl_flatten()
void cp_remcomm()
bool cp_debug
Definition: cshpar.c:61
void ShowPrompt(char *str)
Definition: scedstub.c:71
long co_cctypes[4]
Definition: cpdefs.h:26