summaryrefslogtreecommitdiff
path: root/base/print.c
blob: cf6b80cbf672e2b97a3ed260206d400616153921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* "NETGEN", a netlist-specification tool for VLSI
   Copyright (C) 1989, 1990   Massimo A. Sivilotti
   Author's address: mass@csvax.cs.caltech.edu;
                     Caltech 256-80, Pasadena CA 91125.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (any version).

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file copying.  If not, write to
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

/* print.c -- routines to format and buffer output */

#include "config.h" 

#include <stdio.h>
#include <stdarg.h>  /* what about varargs support, as in pdutils.h ??? */
#include <ctype.h>

#ifdef TCL_NETGEN
#include <tcl.h>

void tcl_stdflush(FILE *);
void tcl_vprintf(FILE *, char *, va_list);

extern int ColumnBase;
#endif

#define MAXFILES 4

struct filestr {
  FILE *f;
  char buffer[200];
  int wrap;  /* column to wrap around in, or 0 if no wrap */
} file_buffers[MAXFILES];

static int findfile(FILE *f)
/* return the index into file_buffers of the file, or -1 if not found */
{
  int i;
  for (i = 0; i < MAXFILES; i++)
    if (file_buffers[i].f == f) return(i);
  return(-1);
}

static int freefile(void)
/* return the index of next free slot in file_buffers, or -1 */
{
  int i;
  for (i = 0; i < MAXFILES; i++)
    if (file_buffers[i].f == NULL) return(i);
  return(-1);
}


FILE *LoggingFile = NULL; /* if LoggingFile is non-null, write to it as well */
int NoOutput = 0;         /* by default, we allow stdout to be printed */

#ifdef HAVE_X11
#include "xnetgen.h"

void Output(FILE *f, char *s)
{
  if (f == stderr) X_display_line(s);
  else {
    if (f == stdout) {
      if (!NoOutput) X_display_line(s);
    }
    else fprintf(f,"%s",s);
  }
  if (LoggingFile != NULL) fprintf(LoggingFile, "%s", s);
}
#else
#ifndef TCL_NETGEN

void Output(FILE *f, char *s)
{
  if (!NoOutput || f != stdout) fprintf(f,"%s",s);
  if (LoggingFile != NULL) fprintf(LoggingFile, "%s", s);
}

#endif /* TCL_NETGEN */
#endif /* HAVE_X11 */

#ifdef TCL_NETGEN

void Fprintf(FILE *f, char *format, ...)
{
  va_list ap;

  va_start(ap, format);
  if (!NoOutput) tcl_vprintf(f, format, ap);
  if (LoggingFile != NULL) vfprintf(LoggingFile, format, ap);
  va_end(ap);
}

#else

void Fprintf(FILE *f, char *format, ...)
{
  va_list ap;
  int FileIndex;
  char tmpstr[200];
  int bufferlongenough;
  int linewrapexceeded;

  va_start(ap, format);
  vsprintf(tmpstr, format, ap);
  va_end(ap);

  FileIndex = findfile(f);
  if (FileIndex == -1) {
    fprintf(f, "%s", tmpstr);
    return;
  }

  /* file is being buffered, so see if there is space in the buffer */
  bufferlongenough = (strlen(file_buffers[FileIndex].buffer) + 
		      strlen(tmpstr) < sizeof(file_buffers[0].buffer) - 3);

  linewrapexceeded = (file_buffers[FileIndex].wrap &&
		      strlen(file_buffers[FileIndex].buffer) + 
		      strlen(tmpstr) > file_buffers[FileIndex].wrap);

  if (bufferlongenough && !linewrapexceeded) {
    strcat(file_buffers[FileIndex].buffer, tmpstr);
    if (strchr(tmpstr,'\n') != NULL) {
#if 0
      int i;
      i = strlen(file_buffers[FileIndex].buffer);
      /* trim any trailing spaces */
      for (i = i-1; i >= 0 && 
	   isspace((file_buffers[FileIndex].buffer)[i]); i--)
	(file_buffers[FileIndex].buffer)[i] = '\0';
#endif
      Output(f, file_buffers[FileIndex].buffer);
      strcpy(file_buffers[FileIndex].buffer,"");
    }
  }
  else {
    /* string too long, or line wrap exceeded, so flush it all */
    if (linewrapexceeded) {
#if 1
      strcat(file_buffers[FileIndex].buffer, "\n");
#endif
      Output(f, file_buffers[FileIndex].buffer);
      strcpy(file_buffers[FileIndex].buffer,"     ");
      strcat(file_buffers[FileIndex].buffer, tmpstr);
    }
    else {
      /* buffer is too small, so flush buffer and new string */
#if 1
      Output(f, file_buffers[FileIndex].buffer);
      Output(f, tmpstr);
      strcpy(file_buffers[FileIndex].buffer,"");
#else
      strcat(file_buffers[FileIndex].buffer, tmpstr);
      Output(f, file_buffers[FileIndex].buffer);
      strcpy(file_buffers[FileIndex].buffer,"");
#endif
    }
  }
}

#endif

void Finsert(FILE *f)
{
  /* try to insert f, if not alread present */
  if (findfile(f) == -1) {
    /* not found, so try to insert it */
    int freeslot;

    freeslot = freefile();
    if (freeslot != -1) {
	file_buffers[freeslot].f = f;
	strcpy(file_buffers[freeslot].buffer,"");
      }
    fflush(f);
  }
}

#ifdef TCL_NETGEN

void Printf(char *format, ...)
{
  va_list ap;

  va_start(ap, format);
  tcl_vprintf(stdout, format, ap);
  va_end(ap);
}

#else

void Printf(char *format, ...)
{
  va_list ap;
  char tmpstr[200];

  va_start(ap, format);
  vsprintf(tmpstr, format, ap);
  va_end(ap);

  /* insert stdout, if not already done */
  Finsert(stdout);
  Fprintf(stdout, "%s",tmpstr);
}

#endif

int Fcursor(FILE *f)
/* return the current column number of the cursor , or 0 if not known */
{
  int i;

  i = findfile(f);
  if (i == -1) return(0);
  return(strlen(file_buffers[i].buffer));
}

int Fwrap(FILE *f, int col)
/* set the wraparound column for this file */
/* returns the previous wrap around column */
{
  int i;
  int oldcol;

  i = findfile(f);
  if (i == -1) return(0);
  oldcol = file_buffers[i].wrap;
  file_buffers[i].wrap = col;
  return(oldcol);
}

/* If "f" is NULL, then print to stdout and NOT */
/* to the log file.				*/

void Ftab(FILE *f, int col)
{
  int i;
  int spaces;
  FILE *locf = (f == NULL) ? stdout : f;

  i = findfile(locf);
  if (i == -1) {
#ifdef TCL_NETGEN
    char *padding;
    if ((col - ColumnBase) <= 0) return;
    padding = (char *)MALLOC(col - ColumnBase + 1);
    for (i = 0; i < col - ColumnBase; i++)
      padding[i] = ' ';
    padding[i] = '\0';
    if (f)
       Fprintf(f, "%s", padding);
    else
       Printf("%s", padding);
#else
    for (i = 0; i < col; i++)
      if (f)
         Fprintf(f, " ");
      else
         Printf(" ");
#endif
    return;
  }

  /* try to pad the string with spaces */
  spaces = col - strlen(file_buffers[i].buffer) - 1;
  for (; spaces > 0; spaces--) strcat(file_buffers[i].buffer, " ");
  return;
}

FILE *Fopen(char *name, char *mode)
{
  FILE *ret;
  int i;

  ret = fopen(name,mode);
  i = freefile();
  if (i != -1) {
    /* empty slot */
    file_buffers[i].f = ret;
    strcpy(file_buffers[i].buffer, "");
  }
  return(ret);
}

void Fflush(FILE *f)
{
  int i;

#ifdef HAVE_X11
  if (f == stdout || f == stderr) {
    i = findfile(f);
    if (i != -1) {
      if (strlen(file_buffers[i].buffer)) 
	Output(f, file_buffers[i].buffer);
      strcpy(file_buffers[i].buffer,"");
    }
    X_display_refresh();
    return;
  }
#endif    

#ifdef TCL_NETGEN
  if (f == stdout || f == stderr) {
    i = findfile(f);
    if (i != -1) {
      if (strlen(file_buffers[i].buffer)) 
	Fprintf(f, file_buffers[i].buffer);
      strcpy(file_buffers[i].buffer, "");
    }
    tcl_stdflush(f);
    return;
  }
#endif
    
  i = findfile(f);
  if (i != -1) {
    if (strlen(file_buffers[i].buffer)) 
      fprintf(f,"%s",file_buffers[i].buffer);
    strcpy(file_buffers[i].buffer,"");
  }
  fflush(f);
}

int Fclose(FILE *f)
{
  int i;

  Fflush(f);
  i = findfile(f);
  if (i != -1) file_buffers[i].f = NULL;
  return (fclose(f));
}