~ubuntu-branches/ubuntu/utopic/clif/utopic

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
/*
    Clif - A C-like Interpreter Framework
    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997 T. Hruz, L. Koren
    1998 L. Koren

    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; either version 2 of the License, or
    (at your option) any later 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; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/* \verbatim{input_c.tex} */

/*
 * input.c
 *
 * Initialization of the main compiler
 * and redefinition of its input functions.
 * Different input function are used during
 * synchronous and asynchronous interrupt
 * handling.
 */

#include <stdio.h>
#include <fcntl.h>
#include "global.h"
#include "lex_t.h"
#include "input.h"

extern FILEATTR pf, spf[];
#ifndef FLEX_SCANNER
extern int yylineno;
extern int yytchar;
extern char *yysptr, yysbuf[];

extern int getcx PROTO((FILE *));
static int buf_pointer = 0;
#else
extern FILE *yyin;
#endif

#define U(x) x

int (*input) PROTO((void));

char string_resume[]="resume;"; /* Buffer for input by return 
				 * from asynchronous interrupt.
				 */
extern int no_compile_only;	/* Flag in the case of errors or */
				/* compile only. */
extern int handle_main;		/* If set, compiler like behavior. */
extern int source_line_number;
				/* Source line number. Detecting if */
				/* the current line was already */
				/* printed. Using in error messages. */
extern void exit_file_scope PROTO((void));

#ifndef FLEX_SCANNER
/*
 * Redefinition of the input for the main compiler.
 */
int 
input_komp ()
{
#ifdef DEBUG_INTER 
  printfx("in front of getc in input\n");
#endif
  yytchar=yysptr>yysbuf?U(*--yysptr):getcx(pf.fp);
#ifdef DEBUG_INTER 
  printfx("behind of getc in input, read character %c - %x\n",
	  yytchar, yytchar);
#endif
  if(yytchar == '\n')		/* LF */
    {
      yylineno++;
      spf[s].line_counter++;
      char_counter = 0;
      line_buf[0] = 0;
    }
  if(yytchar == EOF)		/* Is current char EOF? */
    { 
      if (s > 0)
	{
	  spf[s].line_counter = 1; /* Counting lines from beginning. */
	  source_line_number = 0; /* Resetting line number in the */
				  /* presence of errors. */
	  fclose (spf[s].fp); 
	  exit_file_scope ();
	  s = s - 1;
	  if (! s && ! no_compile_only)
	    return 0;

	  /* If we want compiler-like behavior, don't switch to
	     stdin. */
	  if (! s && handle_main)
	    return 0;
	  
	  pf = spf[s];		/* Move to the next opened file. */
	}
#ifdef DEBUG_INTER 
      printfx ("in front of the second getc in input\n");
#endif
      yytchar = yysptr>yysbuf?U(*--yysptr):getcx(pf.fp); /* The first char */
#ifdef DEBUG_INTER 
      printfx ("behind of the second getc in input, read character %c - %x\n",
	       yytchar, yytchar);
#endif
    }
  return (yytchar);
}

/*
 * Redefinition of the input for the main compiler.
 * It is used during an interrupt.
 */
int
input_std ()
{
#ifdef DEBUG_INTER 
  printfx ("in front of the third getc in input\n");
#endif
  yytchar = yysptr>yysbuf?U(*--yysptr):getcx(stdin);
#ifdef DEBUG_INTER 
  printfx ("behind of the third getc in input, read character %c - %x\n",
	   yytchar, yytchar);
#endif
  if (yytchar == '\n')		/* LF */
    {
      yylineno++;
      spf[s].line_counter++;
      char_counter = 0;
      line_buf[0] = 0;
    }
  
#ifndef NOT_MSWIN_AND_YES_DOS
  if (HANDLER_TEST)
    {
      HANDLER_SET;
      input = input_buf;
      buf_pointer = 0;
      return (input_buf());
    }
#endif
  return (yytchar);
}
	
/*
 * Redefinition of the input for the main compiler.
 * It is used by return from an interrupt.
 */
int
input_buf ()   
{
  yytchar = yysptr>yysbuf?U(*--yysptr):string_resume[buf_pointer++];
  if (yytchar == '\n')		/* LF */
    {
      yylineno++;
      spf[s].line_counter++;
      char_counter = 0;
      line_buf[0] = 0;
    }
  return (yytchar);
}

#else


int
terminate_buffer ()
{
  fclose (spf[s].fp);
  spf[s].fp = NULL;
  spf[s].name = NULL;
  spf[s--].line_counter = 1;
  source_line_number = 0;
  exit_file_scope ();

  /*  Do not switch to stdin, if the user wants compiler-like
      behavior.  */
  if (! s && handle_main)
    return 1;

  return (! s && ! no_compile_only);
}

#endif /* FLEX_SCANNER */

/*
 * Initialization of the main compiler input.
 */
int
init_input (argc1, argv1)
  int argc1;
  char *argv1[];
{
  int b;

#ifndef FLEX_SCANNER
  input = input_komp;
#endif
  s = argc1 - 1;
  spf[0].fp = stdin;
  spf[0].name = "stdin";
  spf[0].line_counter = 1;
  for (b = 1; b < argc1; b++)                       

				/* File opening and storing their pointer. */ 

    {
      spf[argc1 - b].fp = fopen(argv1[b],"r");
      spf[argc1 - b].name = argv1[b];
      spf[argc1 - b].line_counter = 1;
      if (spf[argc1-b].fp == NULL)
	{
	  s = argc1 - b;
	  error_message (7001);
	  return (-1);
	}	
    }
/* 
 * Takes the first file pointer from the stack. 
 */
#ifdef FLEX_SCANNER
  yyin = spf[s].fp;
#else
  pf.fp = spf[s].fp;
#endif
  return 0;
}

/* \verbatim */