Jspice3
output.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: 1986 Wayne A. Christopher
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * Routines to handle "more"d output. There are some serious system
10  * dependencies in here, and it isn't clear that versions of this stuff
11  * can be written for every possible machine...
12  */
13 
14 #include "spice.h"
15 #include "cpdefs.h"
16 #define SHORT_SCEDIO
17 #include "scedio.h"
18 #ifdef __STDC__
19 #include <stdarg.h>
20 #else
21 #include <varargs.h>
22 #endif
23 
24 
25 #ifdef HAVE_SGTTY_H
26 #include <sgtty.h>
27 #endif
28 
29 #include "suffix.h"
30 
31 #define DEF_SCRHEIGHT 24
32 #define DEF_SCRWIDTH 80
33 
34 #ifdef __STDC__
35 static void outbufputc(void);
36 static int fbufputc(int);
37 static void promptreturn(void);
38 #else
39 static void outbufputc();
40 static int fbufputc();
41 static void promptreturn();
42 #endif
43 
44 bool out_moremode = true;
45 bool out_isatty = true;
48 
49 static int xsize, ysize;
50 static int xpos, ypos;
51 static bool noprint, nopause;
52 
53 
54 void
55 out_winsize(cols,rows)
56 
57 int *cols, *rows;
58 {
59  int x, y;
60 #ifdef TIOCGWINSZ
61  struct winsize ws;
62 #endif
63  char tbuf[4096], *s;
64 
65  x = y = 0;
66 
67  /* Figure out the screen size. We try, in order, TIOCGSIZE,
68  * tgetent(), and cp_getvar(height). Default is 24 x 80.
69  */
70 
71 #ifdef TIOCGWINSZ
72  if (!x || !y) {
73  (void) ioctl(fileno(stdout), TIOCGWINSZ, (char *) &ws);
74  x = ws.ws_col;
75  y = ws.ws_row;
76  }
77 #endif
78 
79 #ifdef HAVE_TGETENT
80  if (!x || !y) {
81  if ((s = getenv("TERM")) && (tgetent(tbuf, s) != -1)) {
82  x = tgetnum("co");
83  y = tgetnum("li");
84  if ((x <= 0) || (y <= 0))
85  x = y = 0;
86  }
87  }
88 #endif
89 
90  if (!x) {
91  if ((s = getenv("COLS"))) {
92  x = atoi(s);
93  if (x <= 0)
94  x = 0;
95  }
96  }
97 
98  if (!y) {
99  if ((s = getenv("LINES"))) {
100  y = atoi(s);
101  if (y <= 0)
102  y = 0;
103  }
104  }
105 
106  if (!x)
107  if (out_width > 0) x = out_width;
108  if (!y)
109  if (out_height > 0) y = out_height;
110 
111  if (!x)
112  x = DEF_SCRWIDTH;
113  if (!y)
114  y = DEF_SCRHEIGHT;
115 
116  if (cols != NULL)
117  *cols = x;
118  if (rows != NULL)
119  *rows = y;
120 
121  return;
122 }
123 
124 
125 /* Start output... */
126 
127 void
129 {
130  bool moremode;
131 
132  noprint = nopause = false;
133 
134  if (!out_moremode || !cp_interactive)
135  out_isatty = false;
136 
137  if (!out_isatty)
138  return;
139 
141  ysize -= 2; /* Fudge room... */
142  xpos = ypos = 0;
143 
144  return;
145 }
146 
147 
148 /* Putc may not be buffered (sp?), so we do it ourselves. */
149 
150 static char staticbuf[BUFSIZ];
151 struct {
152  int count;
153  char *ptr;
154 } ourbuf = { BUFSIZ, staticbuf };
155 
156 
157 /* send buffer out */
158 static void
160 {
161  if (ourbuf.count != BUFSIZ) {
162  fputs(staticbuf, cp_out);
163  bzero(staticbuf, BUFSIZ-ourbuf.count);
164  ourbuf.count = BUFSIZ;
165  ourbuf.ptr = staticbuf;
166  }
167 }
168 
169 #define bufputc(c) ( --ourbuf.count >= 0 ? ((int) \
170  (*ourbuf.ptr++ = (unsigned)(c))) : fbufputc((unsigned) (c)))
171 
172 
173 static int
175 
176 unsigned char c;
177 {
178 
179  ourbuf.count = 0;
180  outbufputc();
181  ourbuf.count = BUFSIZ;
182  ourbuf.ptr = staticbuf;
183  bufputc(c);
184 
185 }
186 
187 
188 static void
190 {
191  char buf[16];
192  int attr;
193  char *xx = " -- hit return for more, ? for help -- ";
194  char *menu =
195 "\nPossible responses:\n\
196  q : Discard the rest of the output.\n\
197  c : Continuously print the rest of the output.\n\
198  h,? : Print this help message.\n\
199  other : Print the next page of output.\n";
200 
201 moe:
202  fprintf(cp_out,"\n");
203  attr = getattr();
204  if (cp_out == stdout)
205  cprint(15,xx);
206  else {
207  (void) fprintf(cp_out,xx);
208  (void) fflush(cp_out);
209  }
210  if (cp_in == stdin)
211  *buf = cp_inchar(cp_in);
212  else if (!fgets(buf, 16, cp_in)) {
213  clearerr(cp_in);
214  *buf = 'q';
215  }
216  if (cp_out == stdout)
217  cprint(attr,"\r \r");
218 
219  switch (*buf) {
220  case 'q':
221  noprint = true;
222  break;
223  case 'c':
224  nopause = true;
225  break;
226  case '?':
227  case 'h':
228  if (cp_out = stdout) {
229  cprint(4,menu);
230  cprint(attr," \n"); /* keeps the cursor the attr color */
231  }
232  else {
233  fprintf(cp_out,menu);
234  }
235  goto moe;
236  }
237 }
238 
239 
240 /* Print a string to the output. If this would cause the screen to scroll,
241  * print "more".
242  */
243 
244 void
245 out_send(string)
246 
247 char *string;
248 {
249 
250  if (noprint)
251  return;
252 
253  if (SCEDline(string))
254  return;
255 
256  if (!out_isatty || nopause) {
257  fputs(string, cp_out);
258  return;
259  }
260  while (*string) {
261  switch (*string) {
262  case '\n':
263  xpos = 0;
264  ypos++;
265  break;
266  case '\f':
267  ypos = ysize;
268  xpos = 0;
269  break;
270  case '\t':
271  xpos = xpos / 8 + 1;
272  xpos *= 8;
273  break;
274  default:
275  xpos++;
276  break;
277  }
278  while (xpos >= xsize) {
279  xpos -= xsize;
280  ypos++;
281  }
282  if (ypos >= ysize) {
283  outbufputc(); /* out goes buffer */
284  promptreturn();
285  (void) fflush(cp_out);
286  ypos = xpos = 0;
287  }
288  bufputc(*string); /* we need to buffer these */
289  string++;
290  }
291  (void) outbufputc();
292  return;
293 }
294 
295 /* Printf some stuff using more mode. */
296 
297 #define MAXLEN 4096
298 
299 
300 #ifdef __STDC__
301 
302 void
303 out_printf(char *fmt, ...)
304 
305 {
306  va_list args;
307  char buf[MAXLEN];
308 
309  va_start(args,fmt);
310  (void) vsprintf(buf, fmt, args);
311  va_end(args);
312 
313  out_send(buf);
314  return;
315 }
316 
317 #else
318 
319 
320 void
321 out_printf(va_alist)
322 va_dcl
323 {
324  va_list args;
325  char *fmt;
326  char buf[MAXLEN];
327 
328  va_start(args);
329  fmt = va_arg(args,char *);
330  (void) vsprintf(buf, fmt, args);
331  va_end(args);
332 
333  out_send(buf);
334  return;
335 }
336 
337 #endif /* __STDC__ */
338 
339 
340 void
342 
343 /* print a wordlist */
344 wordlist* wl;
345 {
346  wordlist *wlc = wl_copy(wl);
347 
348  cp_striplist(wlc);
349 
350  for (wl = wlc; wl; wl = wl->wl_next) {
351  out_send(wl->wl_word);
352  if (wl->wl_next)
353  out_send(" ");
354  }
355  wl_free(wlc);
356 }
static bool noprint
Definition: output.c:51
static char buf[MAXPROMPT]
Definition: arg.c:18
int cp_inchar()
void out_wlprint(wordlist *wl)
Definition: output.c:341
static int xsize
Definition: output.c:49
Definition: cddefs.h:119
#define DEF_SCRWIDTH
Definition: output.c:32
void out_winsize(int *cols, int *rows)
Definition: output.c:55
static void outbufputc()
Definition: output.c:159
bool out_moremode
Definition: output.c:44
Definition: library.c:18
struct @17 ourbuf
int bzero(char *ptr, int num)
Definition: string.c:357
char * getenv(char *c)
Definition: libfuncs.c:106
int out_width
Definition: output.c:46
void wl_free()
bool out_isatty
Definition: output.c:45
static bool nopause
Definition: output.c:51
void out_printf(va_alist)
Definition: output.c:321
bool cp_interactive
Definition: help.c:100
#define NULL
Definition: spdefs.h:121
int out_height
Definition: output.c:47
static int fbufputc()
FILE * cp_out
Definition: help.c:101
static int ysize
Definition: output.c:49
void out_init()
Definition: output.c:128
#define MAXLEN
Definition: output.c:297
char * ptr
Definition: output.c:153
Definition: cpstd.h:21
static int xpos
Definition: output.c:50
Definition: cddefs.h:177
struct wordlist * wl_next
Definition: cpstd.h:23
void cp_striplist()
static int ypos
Definition: output.c:50
int count
Definition: output.c:152
int SCEDline(char *string)
Definition: scedstub.c:21
void out_send(char *string)
Definition: output.c:245
#define bufputc(c)
Definition: output.c:169
FILE * cp_in
Definition: help.c:101
char getattr()
Definition: cprint.c:23
Definition: sced.h:363
void clearerr(FILE *fp)
Definition: libfuncs.c:116
#define DEF_SCRHEIGHT
Definition: output.c:31
static void promptreturn()
Definition: output.c:189
static char staticbuf[BUFSIZ]
Definition: output.c:150
void cprint(int clr, char *buf)
Definition: cprint.c:14
wordlist * wl_copy()