Jspice3
inchar.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3f2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1985 Thomas L. Quarles
5  1994 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 #include "spice.h"
9 #include <errno.h>
10 #include "ftedefs.h"
11 #include "ftegraph.h"
12 
13 #ifndef MSDOS
14 
15 /* A special 'getc' so that we can deal with ^D properly. There is no way for
16  * stdio to know if we have typed a ^D after some other characters, so
17  * don't use buffering at all.
18  *
19  * Also, we can't use getc() from the keyboard with X, as it will block
20  * twice, since X11_Input() blocks on the CR.
21  */
22 int
24 
25 FILE *fp;
26 {
27  char c;
28  int i;
29 
30  if (cp_interactive) {
31  do {
32  i = read((int) fileno(fp), &c, 1);
33  } while (i == -1 && errno == EINTR);
34 
35  if (i == 0 || c == '\004')
36  return (EOF);
37  else if (i == -1) {
38  perror("read");
39  return (EOF);
40  }
41  }
42  else {
43  c = getc(fp);
44  }
45  return ((int) c);
46 }
47 
48 #else /* MSDOS */
49 
50 #include <dos.h>
51 
52 /* Input routines that support DOS command completion. Used in lexical.c
53  * and complete.c. TIOCSTI must be defined.
54  * These routines are compiler specific.
55  */
56 
57 #if __NDPC__
58 
59 static bool init_ioctl;
60 static bool init_alloc;
61 
62 void
63 ioctl(fd,mode,c)
64 
65 int fd;
66 int mode;
67 char *c;
68 {
69  /* add character to input queue and maybe print it */
70  if (mode != TIOCSTI) return;
71  if (init_ioctl) {
72  init_alloc = true;
73  init_ioctl = false;
74  if (!_iob[fd].curbuf->base) {
75  _iob[fd].curbuf->base = (unsigned char *) tmalloc(BUFSIZ);
76  _iob[fd].curbuf->lastwritable = _iob[fd].curbuf->base + BUFSIZ;
77  }
78  _iob[fd].curbuf->lastvalid = _iob[fd].curbuf->base;
79  _iob[fd].curbuf->firstvalid = _iob[fd].curbuf->base;
80  _iob[fd].oldnext = _iob[fd].next = _iob[fd].curbuf->base;
81  _iob[fd].lastpos = _iob[fd].curpos = 0L;
82  }
83  if (fd == 0) {
84  putc(*c,stdout);
85  fflush(stdout);
86  }
87  *((_iob[fd].curbuf)->lastvalid)++ = *c;
88 }
89 
90 
91 int
92 cp_inchar(fp)
93 
94 /* getc() that works with ioctl above, NDPC only */
95 
96 FILE *fp;
97 {
98  short c;
99 
100  if (!cp_interactive)
101  return (getc(fp));
102 
103  if (!init_alloc && fp->next >= fp->curbuf->firstvalid &&
104  fp->next < fp->curbuf->lastvalid)
105  return *fp->next++;
106  if (!init_alloc) {
107  init_alloc = true;
108  if (!fp->curbuf->base) {
109  fp->curbuf->base = (unsigned char *) tmalloc(BUFSIZ);
110  fp->curbuf->lastwritable = fp->curbuf->base + BUFSIZ;
111  }
112  fp->curbuf->firstvalid = fp->curbuf->base;
113  fp->curbuf->lastvalid = fp->curbuf->base;
114  fp->oldnext = fp->next = fp->curbuf->base;
115  fp->lastpos = fp->curpos = 0L;
116  }
117  for (;;) {
118  while (!(c = inkey$())) ;
119  switch (c) {
120  case 4: /* ^D */
121  case 27: /* ESC */
122  init_ioctl = true;
123  case 1: /* ^A */
124  case 26: /* ^Z */
125  if (c == 26) c = EOF;
126  *fp->curbuf->lastvalid++ = c;
127  init_alloc = false;
128  return *fp->next++;
129  case 8: /* ^H (backspace) */
130  if (fp->curbuf->lastvalid >
131  fp->curbuf->firstvalid) {
132  fp->curbuf->lastvalid--;
133  if (!fileno(fp)) {
134  putc(c,stdout);
135  putc(' ',stdout);
136  putc(c,stdout);
137  fflush(stdout);
138  }
139  }
140  break;
141  case 13: /* ENTER */
142  *fp->curbuf->lastvalid++ = '\n';
143  if (!fileno(fp)) {
144  putc('\n',stdout);
145  fflush(stdout);
146  }
147  init_alloc = false;
148  return *fp->next++;
149  case 21: /* ^U */
150  while (fp->curbuf->lastvalid >
151  fp->curbuf->firstvalid) {
152  fp->curbuf->lastvalid--;
153  if (!fileno(fp)) {
154  putc(8,stdout);
155  putc(' ',stdout);
156  putc(8,stdout);
157  }
158  }
159  if (!fileno(fp)) {
160  fflush(stdout);
161  }
162  break;
163  default:
164  if (isprint(c)) {
165  *fp->curbuf->lastvalid++ = c;
166  if (!fileno(fp)) {
167  putc(c,stdout);
168  fflush(stdout);
169  }
170  break;
171  }
172  }
173  }
174 }
175 
176 #else
177 #if __GNUC__
178 
179 static bool init_ioctl;
180 static bool init_alloc;
181 
182 
183 void
184 ioctl(fd,mode,c)
185 
186 int fd;
187 int mode;
188 char *c;
189 {
190  /* add character to input queue and maybe print it */
191  if (mode != TIOCSTI) return;
192  if (init_ioctl) {
193  init_alloc = true;
194  init_ioctl = false;
195  if (!_iob[fd]._base) {
196  _iob[fd]._base = tmalloc(BUFSIZ);
197  _iob[fd]._bufsiz = BUFSIZ;
198  }
199  _iob[fd]._cnt = 0;
200  _iob[fd]._ptr = _iob[fd]._base;
201  }
202  if (fd == 0) {
203  putc(*c,stdout);
204  fflush(stdout);
205  }
206  *(_iob[fd]._base + _iob[fd]._cnt) = *c;
207  (_iob[fd]._cnt)++;
208 }
209 
210 
211 int
212 cp_inchar(fp)
213 
214 /* getc() that works with ioctl above, GCC only */
215 
216 FILE *fp;
217 {
218  short c;
219 
220  if (!cp_interactive)
221  return (getc(fp));
222 
223  if (!init_alloc && fp->_cnt > 0) {
224  fp->_cnt--;
225  return ((int)(*(unsigned char*)(fp)->_ptr++));
226  }
227  if (!init_alloc) {
228  init_alloc = true;
229  if (!fp->_base) {
230  fp->_base = tmalloc(BUFSIZ);
231  fp->_bufsiz = BUFSIZ;
232  }
233  fp->_cnt = 0;
234  fp->_ptr = fp->_base;
235  }
236  for (;;) {
237  c = getch() & 0xff;
238  if (c == 0)
239  c = 256 + getch();
240  switch (c) {
241  case 4: /* ^D */
242  case 27: /* ESC */
243  init_ioctl = true;
244  case 1: /* ^A */
245  case 26: /* ^Z */
246  if (c == 26) c = EOF;
247  *(fp->_base + fp->_cnt) = c;
248  init_alloc = false;
249  return ((int)(*(unsigned char*)(fp)->_ptr++));
250  case 8: /* ^H (backspace) */
251  if (fp->_cnt) {
252  fp->_cnt--;
253  if (!fileno(fp)) {
254  putc(c,stdout);
255  putc(' ',stdout);
256  putc(c,stdout);
257  fflush(stdout);
258  }
259  }
260  break;
261  case 13: /* ENTER */
262  *(fp->_base + fp->_cnt) = '\n';
263  if (!fileno(fp)) {
264  putc('\n',stdout);
265  fflush(stdout);
266  }
267  init_alloc = false;
268  return ((int)(*(unsigned char*)(fp)->_ptr++));
269  case 21: /* ^U */
270  while (fp->_cnt) {
271  fp->_cnt--;
272  if (!fileno(fp)) {
273  putc(8,stdout);
274  putc(' ',stdout);
275  putc(8,stdout);
276  }
277  }
278  if (!fileno(fp)) {
279  fflush(stdout);
280  }
281  fp->_ptr = fp->_base;
282  break;
283  default:
284  if (isprint(c)) {
285  *(fp->_base + fp->_cnt) = c;
286  fp->_cnt++;
287  if (!fileno(fp)) {
288  putc(c,stdout);
289  fflush(stdout);
290  }
291  break;
292  }
293  }
294  }
295 }
296 
297 #endif
298 #endif
299 #endif
int cp_inchar(FILE *fp)
Definition: inchar.c:23
#define L
Definition: parse.c:442
char * tmalloc()
bool cp_interactive
Definition: help.c:100
static double c
Definition: vectors.c:16
void perror()
Definition: cddefs.h:177