Minimal changes to get it to compile (a few taken from David Given ack-6.0pre5)
[Ack-5.5.git] / util / ceg / EM_parser / common / scan.c
1 #include <stdio.h>
2
3 /* This file contains the scanner for mylex(), the following functions and 
4  * variables are exported :
5  *
6  *      int yylineno;                   - The current line number.
7  *
8  *      char scanc();                   - Return next character.
9  *
10  *      backc( c);                      - Push back one character.
11  *              char c;
12  *
13  *      FILE *switch_input( new);       - Scanner will from now on read from
14  *              FILE *new;              - 'new', old input-file is returned.
15  *
16  * The scanner must perform a lookahead of more than one character, so it uses
17  * it's own internal buffer.
18  */
19
20
21 int yylineno = 1;
22
23
24 /********* Internal variables + functions ***********************/
25
26
27 #define BUF_SIZE        16
28
29 static char buf[BUF_SIZE],      /* Bufer to save backc()-characters */
30             *bufptr = buf;      /* Pointer to space for backc()-character */
31
32 FILE *infile; /*static FILE *infile = stdin;*/
33
34
35
36
37 static char nextc()
38 {
39         if ( bufptr > buf)
40                 return( *--bufptr);
41         else
42                 return( getc( infile));
43 }
44
45
46 /***************************************************************/
47
48
49 char scanc()
50
51 /* Get next character, but delete al C-comments and count lines */
52
53 {
54         char c, nextc();
55
56         c = nextc();
57         while ( c == '/') {
58                 c = nextc();
59                 if ( c == '*') {        /* start of comment */
60                         while ( nextc() != '*' || nextc() != '/')
61                                 ;
62                         c = nextc();
63                 }
64                 else {
65                         backc( c);
66                         return( '/');
67                 }
68         }
69         if ( c == '\n')
70                 yylineno++;
71         return( c);
72 }
73
74
75 backc( c)
76 char c;
77 {
78         if ( bufptr >= buf + BUF_SIZE)
79                 error( "backc(), no space in buffer left!");
80         else {
81                 if ( c == '\n')
82                         yylineno--;
83                 *bufptr++ = c;
84         }
85 }
86
87
88 FILE *switch_input( new)
89 FILE *new;
90
91 /* Switch to a new input file, try to save the lookahead-characters in buf[]
92  * by calling ungetc(). If they can't be saved return NULL.
93  */
94
95 {
96         char *ptr; FILE *old;
97
98         /* Clean buf[] */
99         for ( ptr = buf; ptr < bufptr; ptr++)
100                 if ( ungetc( *ptr, infile) == EOF && *ptr != EOF)
101                         return( NULL);
102
103         bufptr = buf;
104         old = infile;
105         infile = new;
106         return( old);
107 }