File: token.c

package info (click to toggle)
jgraph 83-20
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 628 kB
  • ctags: 445
  • sloc: ansic: 4,728; makefile: 186; sh: 106; awk: 104
file content (421 lines) | stat: -rw-r--r-- 7,889 bytes parent folder | download | duplicates (4)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* 
 * $Source: /tmp_mnt/n/fs/grad1/jsp/src/jgraph/RCS/token.c,v $
 * $Revision: 8.3 $
 * $Date: 92/11/30 11:42:42 $
 * $Author: jsp $
 */

#include <math.h>
#include <stdio.h>

#ifdef LCC 
#include <stdlib.h>
#endif

#ifdef VMS 
#include <stdlib.h>
#include <redexp.VMS>
#endif

#include "list.h"

#define CNULL ((char *)0)

typedef struct iostack {
  struct iostack *flink;
  struct iostack *blink;
  char *filename;
  FILE *stream;
  int oldcharvalid;
  char oldchar;
  char pipe;
  int line;
} *Iostack;

static char INPUT[1000];
static int getnew = 1;
static char oldchar = '\0';
static oldcharvalid = 0;
static char pipe = 0;
static int eof = 0;
static int init = 0;
static Iostack stack;
static char real_eof = EOF;

static FILE *IOSTREAM;
static char FILENAME[300];
static int line = 1;

#ifdef VMS
/* On VMS, there are no popen() and pclose(), so we provide dummies here. */
FILE *popen(command, type)
char *command, *type;
{
    return(NULL);
}
int pclose(stream)
FILE *stream;
{
    return(-1);
}
#endif /*VMS*/

set_input_file(s)
char *s;
{
  FILE *f;
  Iostack n;

  if (init == 0) {
    stack = (Iostack) make_list(sizeof(struct iostack));
    if (s == CNULL) {
      IOSTREAM = stdin;
      strcpy(FILENAME, "<stdin>");
    } else {
      IOSTREAM = fopen(s, "r");
      if (IOSTREAM == NULL) {
        fprintf(stderr, "Error: cannot open file \"%s\"\n", s);
        exit(1);
      }
      strcpy(FILENAME, s);
    }
    init = 1;
  } else {
    n = (Iostack) get_node(stack);
    n->stream = NULL;
    n->filename = (char *) malloc (sizeof(char)*(strlen(s)+2));
    strcpy(n->filename, s);
    n->oldchar = oldchar;
    n->oldcharvalid = oldcharvalid;
    n->pipe = pipe;
    n->line = line;
    insert(n, stack->flink);
  }
}

error_header()
{
  fprintf(stderr, "%s,%d: ", FILENAME, line);
}
  
int gettokenchar()
{
  if (oldcharvalid == 0) oldchar = getc(IOSTREAM);
  oldcharvalid = 0;
  if (oldchar == '\n') line++;
  return oldchar;
}

ungettokenchar()
{
  oldcharvalid = 1;
  if (oldchar == '\n') line--;
}

int gettoken(s)
char *s;
{
  int i;
  char c;

  for (c = gettokenchar(); 
       c == ' ' || c == '\t' || c == '\n';
       c = gettokenchar()) ;
  for (i = 0;
       c != real_eof && c != ' ' && c != '\t' && c != '\n';
       c = gettokenchar()) {
    s[i++] = c;
  }
  s[i] = '\0';
  ungettokenchar();
  return i;
}

get_comment()
{
  if (eof) return;
  while (1) {
    if (gettoken(INPUT) == 0) return;
    else if (strcmp(INPUT, "(*") == 0)
      get_comment();
    else if (strcmp(INPUT, "*)") == 0) 
      return;
  }
}

static int iostackempty()
{
  return (first(stack) == nil(stack));
}

static push_iostack(p)
int p;
{
  Iostack n;

  n = (Iostack) get_node(stack);
  n->stream = IOSTREAM;
  n->filename = (char *) malloc (sizeof(char)*(strlen(FILENAME)+2));
  n->oldchar = oldchar;
  n->oldcharvalid = oldcharvalid;
  n->pipe = pipe;
  n->line = line;
  strcpy(n->filename, FILENAME);
  insert(n, stack);
  if (p) {
    IOSTREAM = (FILE *) popen(INPUT, "r");
  } else {
    IOSTREAM = fopen(INPUT, "r");
  }
  pipe = p;
  line = 1;
  if (IOSTREAM == NULL) {
    error_header();
    fprintf(stderr, "Include file \"%s\" does not exist\n", INPUT);
    exit(1);
  }
  strcpy(FILENAME, INPUT);
}

static pop_iostack()
{
  Iostack n;

  fflush(IOSTREAM);
  if (pipe) {
    if (pclose(IOSTREAM)) {
      /*error_header();
      fprintf(stderr, "\n\nPipe returned a non-zero error code.\n");
      exit(1); */
    }
  } else {
    fclose(IOSTREAM);
  }
  n = last(stack);
  if (n->stream == NULL) {
    n->stream = fopen(n->filename, "r");
    if (n->stream == NULL) {
      fprintf(stderr, "Error: cannot open file \"%s\"\n", n->stream);
      exit(1);
    }
  }
  IOSTREAM = n->stream;
  strcpy(FILENAME, n->filename);
  free(n->filename);
  pipe = n->pipe;
  line = n->line;
  oldchar = n->oldchar;
  oldcharvalid = n->oldcharvalid;
  delete_item(n);
  free_node(n, stack);
}

static nexttoken()
{
  if (eof) return;
  if (getnew) {
    while (1) {
      if (gettoken(INPUT) == 0) {
        if (iostackempty()) {
          eof = 1;
          getnew = 0;
          return;
        } else {
          pop_iostack();
        }
      } else if (strcmp(INPUT, "(*") == 0) {
        get_comment();
      } else if (strcmp(INPUT, "include") == 0) {
        if (gettoken(INPUT) == 0) {
          error_header();
          fprintf(stderr, "Empty include statement\n");
          exit(1);
        } else {
          push_iostack(0);
        }
      } else if (strcmp(INPUT, "shell") == 0) {
#ifdef VMS 
        fprintf(stderr, "No shell option on VMS, sorry.\n");
        exit(1);
#endif /*VMS*/	
        if (gettoken(INPUT) == 0 || strcmp(INPUT, ":") != 0) {
          error_header();
          fprintf(stderr, "'shell' must be followed by ':'\n");
          exit(1);
        } 
        if (getsystemstring() == 0) {
          fprintf(stderr, "Empty shell statement\n");
          exit(1);
        }
        push_iostack(1);
      } else {
        getnew = 1;
        return;
      }
    }
  }
  getnew = 1;
  return;
}

int getstring(s)
char *s;
{
  nexttoken();
  if (eof) return 0;
  strcpy(s, INPUT);
  return 1;
}

int getint(i)
int *i;
{
  int j;

  nexttoken();
  if (eof) return 0;
  *i = atoi(INPUT);
  if (*i == 0) {
    for (j = 0; INPUT[j] != '\0'; j++)
      if (INPUT[j] != '0') return 0;
  }
  return 1;
}

int getfloat(f)
float *f;
{
  int j;

  nexttoken();
  if (eof) return 0;
  *f = (float) atof(INPUT);
  if (*f == 0.0) {
    for (j = 0; INPUT[j] == '-'; j++);
    while (INPUT[j] == '0') j++;
    if (INPUT[j] == '.') j++;
    while (INPUT[j] == '0') j++;
    if (INPUT[j] == 'e' || INPUT[j] == 'E') {
      j++;
      if (INPUT[j] == '+' || INPUT[j] == '-') j++;
      while (INPUT[j] == '0') j++;
    }
    return (INPUT[j] == '\0');
  } else return 1;
}

static char *new_printable_text(s)
char *s;
{
  char *new_s;
  int to_pad, i, j;

  to_pad = 0;
  for (i = 0; s[i] != '\0'; i++) {
    if (s[i] == '\\' || s[i] == ')' || s[i] == '(') {
       to_pad++;
    }
  }

  j = sizeof(char) * (i + to_pad + 2);
  if ((j % 8) != 0) j += 8 - j % 8;
  new_s = (char *) malloc (j);
  j = 0;
  for (i = 0; s[i] != '\0'; i++) {
    if (s[i] == '\\' || s[i] == ')' || s[i] == '(') {
      new_s[j++] = '\\';
    }
    new_s[j++] = s[i];
  }
  new_s[j] = '\0';		/* added: tie off -hdd */
  return new_s;
}

char *getmultiline()
{
  char c;
  int i, j, done, len, started;
  char *out_str;

  if (getnew == 0) return CNULL;
  
  c = gettokenchar();
  if (c == real_eof) {
    ungettokenchar();
    return CNULL;
  }
  done = 0;
  started = 0;
  while (!done) {
    i = 0;
    for (c = gettokenchar(); c != real_eof && c != '\n';  c = gettokenchar()) {
      INPUT[i++] = c;
    }
    INPUT[i] = '\0';
    if (!started) {
      out_str = (char *) malloc (sizeof(char)*(i+1));
      strcpy(out_str, INPUT);
      len = i;
      started = 1;
    } else {
      out_str = (char *) realloc(out_str, (len + i + 3) * sizeof(char));
      sprintf(&(out_str[len]), "\n%s", INPUT);
      len += i+1;
    }
    if (c == '\n' && len != 0 && out_str[len-1] == '\\') {
      len--;
    } else {
      done = 1;
    }
  }
  ungettokenchar();
  return out_str;
}

char *getlabel()
{
  char c;
  char *txt, *new;
  int i;

  txt = getmultiline();
  if (txt == CNULL) return CNULL;
  new = new_printable_text(txt);
  free(txt);
  return new;
}

int getsystemstring()
{
  char c;
  int i;
  int done;

  if (getnew == 0) return 0;
  
  c = gettokenchar();
  if (c == real_eof) {
    ungettokenchar();
    return 0;
  }
  i = 0;
  done = 0;
  while (!done) {
    for (c = gettokenchar(); c != real_eof && c != '\n';  c = gettokenchar()) {
      INPUT[i++] = c;
    }
    if (c == '\n' && i > 0 && INPUT[i-1] == '\\') {
      INPUT[i++] = '\n';
    } else {
      done = 1;
    }
  }
  ungettokenchar();
  INPUT[i] = '\0';
  return 1;
}

rejecttoken()
{
  getnew = 0;
}