1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: febase.cc,v 1.8 1997-04-25 15:03:56 obachman Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: i/o system, handling of 'voices' |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdlib.h> |
---|
10 | #include <string.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <limits.h> |
---|
13 | #include <stdarg.h> |
---|
14 | #ifndef macintosh |
---|
15 | #include <unistd.h> |
---|
16 | #endif |
---|
17 | #ifdef NeXT |
---|
18 | #include <sys/file.h> |
---|
19 | #endif |
---|
20 | #include "mod2.h" |
---|
21 | #include "tok.h" |
---|
22 | #include "febase.h" |
---|
23 | #include "mmemory.h" |
---|
24 | #include "subexpr.h" |
---|
25 | #include "ipshell.h" |
---|
26 | |
---|
27 | #define fePutChar(c) fputc((uchar)(c),stdout) |
---|
28 | /*0 implementation */ |
---|
29 | |
---|
30 | char fe_promptstr[] |
---|
31 | #ifdef macintosh |
---|
32 | =" \n"; |
---|
33 | #else |
---|
34 | =" "; |
---|
35 | #endif |
---|
36 | |
---|
37 | class Voices |
---|
38 | { |
---|
39 | private: |
---|
40 | void Init() |
---|
41 | { |
---|
42 | memset(this,0,sizeof(*this)); |
---|
43 | v_lineno = 1; |
---|
44 | } |
---|
45 | public: |
---|
46 | int v_lineno; // lineno, to restore in recursion |
---|
47 | int v_oldlineno; // lineno, to restore at exit |
---|
48 | int typ; // buffer type: see BT_.. |
---|
49 | int v_echo; // echo, to restore in recursion |
---|
50 | int v_fileVoice; // to be restored in recursion |
---|
51 | int v_inputswitch; // ?? |
---|
52 | FILE * files; // file handle |
---|
53 | long fptr; // file | buffer seek |
---|
54 | char* filename; // file name |
---|
55 | char * buffer; // buffer pointer |
---|
56 | |
---|
57 | void Next(); |
---|
58 | Voices() { Init(); } |
---|
59 | Voices * VFile(char* fname); |
---|
60 | Voices * Buffer(char* buf, int t); |
---|
61 | int Exit(); |
---|
62 | } ; |
---|
63 | |
---|
64 | |
---|
65 | extern FILE* yyin; |
---|
66 | |
---|
67 | #define INITIAL_PRINT_BUFFER 24*1024 |
---|
68 | static int feBufferLength=INITIAL_PRINT_BUFFER; |
---|
69 | static char * feBuffer=(char *)Alloc(INITIAL_PRINT_BUFFER); |
---|
70 | |
---|
71 | #define START_LEVMAX 32 |
---|
72 | int levmax = START_LEVMAX; |
---|
73 | Voices *currentVoice = NULL; |
---|
74 | Voices *FileAttribs =(Voices *)Alloc(START_LEVMAX*sizeof(Voices)); |
---|
75 | short *ifswitch =(short *)Alloc0(START_LEVMAX*sizeof(short)); |
---|
76 | /*1 ifswitch==0: no if statement, else is invalid |
---|
77 | * ==1: if (0) processed, execute else |
---|
78 | * ==2: if (1) processed, else allowed but not executed |
---|
79 | */ |
---|
80 | int si_echo = 0; |
---|
81 | int printlevel = 0; |
---|
82 | int fileVoice = 0; |
---|
83 | #ifndef macintosh |
---|
84 | int pagelength = 24; |
---|
85 | #else |
---|
86 | int pagelength = -1; |
---|
87 | #endif |
---|
88 | int colmax = 80; |
---|
89 | int voice = 0; |
---|
90 | int inputswitch = 0; |
---|
91 | int blocklineno = 0; |
---|
92 | char prompt_char = '>'; /*1 either '>' or '.'*/ |
---|
93 | BITSET verbose = 1 |
---|
94 | | Sy_bit(V_REDEFINE) |
---|
95 | | Sy_bit(V_LOAD_LIB) |
---|
96 | | Sy_bit(V_SHOW_USE) |
---|
97 | | Sy_bit(V_PROMPT) |
---|
98 | // | Sy_bit(V_DEBUG_MEM) |
---|
99 | ; |
---|
100 | BOOLEAN errorreported = FALSE; |
---|
101 | BOOLEAN feBatch; |
---|
102 | char * feErrors=NULL; |
---|
103 | int feErrorsLen=0; |
---|
104 | |
---|
105 | BOOLEAN feProt = FALSE; |
---|
106 | FILE* feProtFile; |
---|
107 | BOOLEAN tclmode=FALSE; |
---|
108 | /* TCL-Protocoll (Singular -x): <char type>:<int length>:<string> \n |
---|
109 | * E:l:s error - not implemented yet (use N) |
---|
110 | * W:l:s warning |
---|
111 | * N:l:s stdout |
---|
112 | * Q:0: quit |
---|
113 | * P:0: prompt > |
---|
114 | * P:1: prompt . |
---|
115 | * R:l:<ring-name> ring change |
---|
116 | * plan: |
---|
117 | * O:l:<option/no-option> option change (option) |
---|
118 | * V:l:<option/no-option> option change (verbose) |
---|
119 | */ |
---|
120 | |
---|
121 | /*2 |
---|
122 | * fopen, but use 'SingularPath' from environment and '/usr/local/Singular' |
---|
123 | */ |
---|
124 | #ifdef macintosh |
---|
125 | # define FS_SEP ',' |
---|
126 | # define DIR_SEP ':' |
---|
127 | # define DIR_SEPP ":" |
---|
128 | #else |
---|
129 | #ifdef MSDOS |
---|
130 | # define FS_SEP ';' |
---|
131 | # define DIR_SEP '\\' |
---|
132 | # define DIR_SEPP "\\" |
---|
133 | #else |
---|
134 | #ifdef atarist |
---|
135 | # define FS_SEP ';' |
---|
136 | # define DIR_SEP '\\' |
---|
137 | # define DIR_SEPP "\\" |
---|
138 | #else /* unix */ |
---|
139 | # define FS_SEP ':' |
---|
140 | # define DIR_SEP '/' |
---|
141 | # define DIR_SEPP "/" |
---|
142 | #endif /* atarist */ |
---|
143 | #endif /* MSDOS */ |
---|
144 | #endif /* macintosh */ |
---|
145 | |
---|
146 | FILE * feFopen(char *path, char *mode, char *where,int useWerror) |
---|
147 | { |
---|
148 | if (where!=NULL) strcpy(where,path); |
---|
149 | if ((*mode=='a') ||(*mode=='w') || (path[0]==DIR_SEP)||(path[0]=='.')) |
---|
150 | return fopen(path,mode); |
---|
151 | char found = 0; |
---|
152 | FILE * f=NULL; |
---|
153 | #ifdef MSDOS |
---|
154 | char *env=getenv("SPATH"); |
---|
155 | #else |
---|
156 | char *env=getenv("SingularPath"); |
---|
157 | #endif |
---|
158 | char *s; |
---|
159 | if (where==NULL) s=(char *)AllocL(250); |
---|
160 | else s=where; |
---|
161 | if (env!=NULL) |
---|
162 | { |
---|
163 | char *p,*q; |
---|
164 | p = env; |
---|
165 | while( (q=strchr(p, FS_SEP)) != NULL) |
---|
166 | { |
---|
167 | *q = '\0'; |
---|
168 | strcpy(s,p); |
---|
169 | *q = FS_SEP; |
---|
170 | strcat(s, DIR_SEPP); |
---|
171 | strcat(s, path); |
---|
172 | #ifndef macintosh |
---|
173 | if(!access(s, R_OK)) { found++; break; } |
---|
174 | #else |
---|
175 | f=fopen(s,mode); |
---|
176 | if (f!=NULL) { found++; fclose(f); break; } |
---|
177 | #endif |
---|
178 | p = q+1; |
---|
179 | } |
---|
180 | if(!found) |
---|
181 | { |
---|
182 | strcpy(s,p); |
---|
183 | strcat(s, DIR_SEPP); |
---|
184 | strcat(s, path); |
---|
185 | } |
---|
186 | f=fopen(s,mode); |
---|
187 | if (f!=NULL) |
---|
188 | { |
---|
189 | if (where==NULL) FreeL((ADDRESS)s); |
---|
190 | return f; |
---|
191 | } |
---|
192 | } |
---|
193 | if (where!=NULL) strcpy(s/*where*/,path); |
---|
194 | f=fopen(path,mode); |
---|
195 | #ifndef macintosh |
---|
196 | if (f==NULL) |
---|
197 | { |
---|
198 | char* ss = s; |
---|
199 | int need_len = strlen(path) + strlen(SINGULAR_DATADIR) + 2; |
---|
200 | |
---|
201 | if (where == NULL) |
---|
202 | { |
---|
203 | if (need_len > 250) ss = (char *) AllocL(need_len); |
---|
204 | strcpy(ss,s); |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | if (need_len > strlen(where)) ss = AllocL(need_len); |
---|
209 | strcpy(ss, s); |
---|
210 | } |
---|
211 | strcpy(ss,SINGULAR_DATADIR); |
---|
212 | strcat(s, DIR_SEPP); |
---|
213 | strcat(ss,path); |
---|
214 | f=fopen(ss,mode); |
---|
215 | if (ss != s) FreeL((ADDRESS)ss); |
---|
216 | } |
---|
217 | #endif |
---|
218 | if (where==NULL) FreeL((ADDRESS)s); |
---|
219 | if ((f==NULL)&&(useWerror)) |
---|
220 | Werror("cannot open `%s`",path); |
---|
221 | return f; |
---|
222 | } |
---|
223 | |
---|
224 | /*2 |
---|
225 | * the name of the current 'voice': the procname (or filename) |
---|
226 | */ |
---|
227 | const char * VoiceName() |
---|
228 | { |
---|
229 | if (FileAttribs[fileVoice].filename!=NULL) |
---|
230 | return FileAttribs[fileVoice].filename; |
---|
231 | return sNoName; |
---|
232 | } |
---|
233 | |
---|
234 | /*2 |
---|
235 | * the name of the 'voice' number 'i': the procname (or filename) |
---|
236 | */ |
---|
237 | const char * VoiceName(int i) |
---|
238 | { |
---|
239 | if (FileAttribs[i].filename!=NULL) |
---|
240 | return FileAttribs[i].filename; |
---|
241 | return sNoName; |
---|
242 | } |
---|
243 | |
---|
244 | /*2 |
---|
245 | * the type of the current voice:BT_proc, BT_example, BT_file |
---|
246 | */ |
---|
247 | int VoiceType() |
---|
248 | { |
---|
249 | int i=fileVoice; |
---|
250 | while ((FileAttribs[i].typ!=BT_proc) |
---|
251 | &&(FileAttribs[i].typ!=BT_example) |
---|
252 | &&(FileAttribs[i].typ!=BT_file) |
---|
253 | &&(i>0)) |
---|
254 | i--; |
---|
255 | return FileAttribs[i].typ; |
---|
256 | } |
---|
257 | /*2 |
---|
258 | * start the file 'fname' (STDIN is stdin) in the current voice (cf.newVoice) |
---|
259 | */ |
---|
260 | Voices * Voices::VFile(char* fname) |
---|
261 | { |
---|
262 | if (strcmp(fname,"STDIN") == 0) |
---|
263 | { |
---|
264 | yyin = stdin; |
---|
265 | v_inputswitch = 0; |
---|
266 | } |
---|
267 | else |
---|
268 | { |
---|
269 | yyin = feFopen(fname,"r",NULL,TRUE); |
---|
270 | v_inputswitch = -1; |
---|
271 | } |
---|
272 | files = yyin; |
---|
273 | filename = mstrdup(fname); |
---|
274 | v_echo = si_echo; |
---|
275 | fileVoice = voice; |
---|
276 | yylineno = 1; |
---|
277 | if (files==NULL) |
---|
278 | { |
---|
279 | inputswitch = 0; |
---|
280 | exitVoice(); |
---|
281 | return NULL; |
---|
282 | } |
---|
283 | inputswitch= v_inputswitch; |
---|
284 | return this; |
---|
285 | } |
---|
286 | |
---|
287 | /*3 |
---|
288 | * increment voice counter, allocate new memory |
---|
289 | */ |
---|
290 | static inline void inc_voice() |
---|
291 | { |
---|
292 | voice++; |
---|
293 | if (voice >= levmax) |
---|
294 | { |
---|
295 | FileAttribs=(Voices *)ReAlloc(FileAttribs, |
---|
296 | levmax*sizeof(Voices), |
---|
297 | (levmax+16)*sizeof(Voices)); |
---|
298 | ifswitch=(short *)ReAlloc(ifswitch, |
---|
299 | levmax*sizeof(short), |
---|
300 | (levmax+16)*sizeof(short)); |
---|
301 | memset(&ifswitch[levmax],0,16*sizeof(short)); |
---|
302 | levmax+=16; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | /*2 |
---|
307 | * init a new voice similiar to the current |
---|
308 | */ |
---|
309 | void Voices::Next() |
---|
310 | { |
---|
311 | v_oldlineno = yylineno; |
---|
312 | v_echo = si_echo; |
---|
313 | v_fileVoice = fileVoice; |
---|
314 | inc_voice(); |
---|
315 | |
---|
316 | currentVoice = &FileAttribs[voice]; |
---|
317 | currentVoice->Init(); |
---|
318 | } |
---|
319 | |
---|
320 | /*2 |
---|
321 | * start the file 'fname' (STDIN is stdin) as a new voice (cf.VFile) |
---|
322 | */ |
---|
323 | int newVoice(char* s) |
---|
324 | { |
---|
325 | currentVoice->Next(); |
---|
326 | return (int)currentVoice->VFile((char *)s); |
---|
327 | } |
---|
328 | |
---|
329 | void newBuffer(char* s, int t, char* pname) |
---|
330 | { |
---|
331 | currentVoice->Next(); |
---|
332 | currentVoice->Buffer(s,t); |
---|
333 | if (pname) currentVoice->filename = mstrdup(pname); |
---|
334 | //printf("start buffer %d typ %d\n",voice,t); |
---|
335 | } |
---|
336 | |
---|
337 | Voices * Voices::Buffer(char* buf, int t) |
---|
338 | { |
---|
339 | inputswitch = v_inputswitch = t; |
---|
340 | buffer = buf; |
---|
341 | typ = t; |
---|
342 | //si_echo = 0; |
---|
343 | switch (t) |
---|
344 | { |
---|
345 | case BT_example: |
---|
346 | case BT_proc: v_lineno = yylineno; ::yylineno = 3; break; |
---|
347 | case BT_file: v_lineno = yylineno; ::yylineno = 1; break; |
---|
348 | case BT_if: |
---|
349 | case BT_else: ::yylineno = v_lineno = blocklineno - 1; break; |
---|
350 | case BT_break: ::yylineno = v_lineno = blocklineno - 2; break; |
---|
351 | } |
---|
352 | return this; |
---|
353 | } |
---|
354 | |
---|
355 | /*2 |
---|
356 | * after leaving a voice: |
---|
357 | * setup everything from the this level |
---|
358 | */ |
---|
359 | int Voices::Exit() |
---|
360 | { |
---|
361 | if (voice >= 0) |
---|
362 | { |
---|
363 | si_echo = v_echo; |
---|
364 | fileVoice = v_fileVoice; |
---|
365 | yyin = files; |
---|
366 | yylineno = v_oldlineno; |
---|
367 | inputswitch = v_inputswitch; |
---|
368 | return 0; |
---|
369 | } |
---|
370 | //Print("Exit:%d\n",voice); |
---|
371 | return 1; |
---|
372 | } |
---|
373 | |
---|
374 | /*2 |
---|
375 | * exit Buffer of type 'typ': |
---|
376 | * returns 1 if buffer type could not be found |
---|
377 | */ |
---|
378 | int exitBuffer(int typ) |
---|
379 | { |
---|
380 | //printf("exitBuffer: %d\n",typ); |
---|
381 | if (typ == BT_break) // valid inside for, while. may skip if, else |
---|
382 | { |
---|
383 | /*4 first check for valid buffer type, skip if/else*/ |
---|
384 | for (int i=voice; i>0; i--) |
---|
385 | { |
---|
386 | if ((FileAttribs[i].typ == BT_if) |
---|
387 | ||(FileAttribs[i].typ == BT_else)) continue; |
---|
388 | if (FileAttribs[i].typ == BT_break /*typ*/) |
---|
389 | { |
---|
390 | while ((/*typ*/ BT_break != currentVoice->typ) |
---|
391 | && (voice > 0)) |
---|
392 | { |
---|
393 | exitVoice(); |
---|
394 | } |
---|
395 | return exitVoice(); |
---|
396 | } |
---|
397 | else return 1; |
---|
398 | } |
---|
399 | /*4 break not inside a for/while: return an error*/ |
---|
400 | if (/*typ*/ BT_break != currentVoice->typ) return 1; |
---|
401 | return exitVoice(); |
---|
402 | } |
---|
403 | |
---|
404 | if ((typ == BT_proc) |
---|
405 | || (typ == BT_example)) |
---|
406 | { |
---|
407 | for (int i=voice; i>0; i--) |
---|
408 | { |
---|
409 | if (FileAttribs[i].typ == 0) break; |
---|
410 | if ((FileAttribs[i].typ == BT_proc) |
---|
411 | || (FileAttribs[i].typ == BT_example)) |
---|
412 | { |
---|
413 | while ((BT_proc != currentVoice->typ) |
---|
414 | && (BT_example != currentVoice->typ) |
---|
415 | && (voice > 0)) |
---|
416 | { |
---|
417 | exitVoice(); |
---|
418 | } |
---|
419 | return exitVoice(); |
---|
420 | } |
---|
421 | } |
---|
422 | } |
---|
423 | /*4 return not inside a proc: return an error*/ |
---|
424 | return 1; |
---|
425 | } |
---|
426 | |
---|
427 | /*2 |
---|
428 | * jump to the beginning of a buffer |
---|
429 | */ |
---|
430 | int contBuffer(int typ) |
---|
431 | { |
---|
432 | if (typ == BT_break) // valid inside for, while. may skip if, else |
---|
433 | { |
---|
434 | // first check for valid buffer type |
---|
435 | for (int i=voice; i>0; i--) |
---|
436 | { |
---|
437 | if ((FileAttribs[i].typ == BT_if) |
---|
438 | ||(FileAttribs[i].typ == BT_else)) continue; |
---|
439 | if (FileAttribs[i].typ == BT_break /*typ*/) |
---|
440 | { |
---|
441 | while (/*typ*/ BT_break != currentVoice->typ && (voice > i)) |
---|
442 | { |
---|
443 | exitVoice(); |
---|
444 | } |
---|
445 | currentVoice->fptr = 0L; |
---|
446 | yylineno = currentVoice->v_lineno; |
---|
447 | return 0; |
---|
448 | } |
---|
449 | else return 1; |
---|
450 | } |
---|
451 | } |
---|
452 | return 1; |
---|
453 | } |
---|
454 | |
---|
455 | /*2 |
---|
456 | * leave a file voice |
---|
457 | */ |
---|
458 | int exitFile() |
---|
459 | { |
---|
460 | int oldswitch; |
---|
461 | |
---|
462 | while ((voice > 0) && (inputswitch > 0)) |
---|
463 | { |
---|
464 | exitVoice(); |
---|
465 | } |
---|
466 | // now we have left all if-, else-, while-, for-, proc-levels |
---|
467 | // inside this file; |
---|
468 | // if the file is the terminal (inputswitch == 0) and |
---|
469 | // voice >0, so return 1 else return 0 |
---|
470 | // (used for EXIT_CMD in CNTRLC-C-handling) |
---|
471 | oldswitch = inputswitch; |
---|
472 | exitVoice(); |
---|
473 | #ifdef SIC |
---|
474 | return 1; |
---|
475 | #else |
---|
476 | if ((oldswitch)||(myynest<0)) return 0; |
---|
477 | else return 1; |
---|
478 | #endif |
---|
479 | } |
---|
480 | |
---|
481 | /*2 |
---|
482 | * leave a voice: kill local variables |
---|
483 | * setup everything from the previous level (via Exit) |
---|
484 | */ |
---|
485 | int exitVoice() |
---|
486 | { |
---|
487 | if (voice <= 0) m2_end(0); |
---|
488 | //printf("exitVoice %d, typ %d\n",voice,FileAttribs[voice].typ); |
---|
489 | if (FileAttribs[voice].typ==BT_if) |
---|
490 | { |
---|
491 | ifswitch[voice-1]=2; |
---|
492 | } |
---|
493 | else |
---|
494 | { |
---|
495 | ifswitch[voice-1]=0; |
---|
496 | //if ((FileAttribs[voice].typ==BT_proc) |
---|
497 | //||(FileAttribs[voice].typ==BT_example) |
---|
498 | //||(FileAttribs[voice].typ==0)) |
---|
499 | //{ |
---|
500 | // killlocals(myynest); |
---|
501 | // printf("killlocals %d\n",myynest); |
---|
502 | //} |
---|
503 | } |
---|
504 | if (inputswitch == -1) |
---|
505 | { |
---|
506 | fclose(yyin); |
---|
507 | } |
---|
508 | else if (inputswitch > 0) |
---|
509 | { |
---|
510 | if (FileAttribs[voice].filename!=NULL) |
---|
511 | { |
---|
512 | FreeL((ADDRESS)FileAttribs[voice].filename); |
---|
513 | FileAttribs[voice].filename=NULL; |
---|
514 | } |
---|
515 | if (FileAttribs[voice].buffer!=NULL) |
---|
516 | { |
---|
517 | FreeL((ADDRESS)FileAttribs[voice].buffer); |
---|
518 | FileAttribs[voice].buffer=NULL; |
---|
519 | } |
---|
520 | } |
---|
521 | currentVoice = &FileAttribs[--voice]; |
---|
522 | return currentVoice->Exit(); |
---|
523 | } |
---|
524 | |
---|
525 | int readbuf(char* buf, int l) |
---|
526 | { |
---|
527 | char *s; |
---|
528 | char * t = buf; |
---|
529 | int i = 0; |
---|
530 | long fl = currentVoice->fptr; |
---|
531 | if (fl == -1L) |
---|
532 | { |
---|
533 | t[0] = '\0'; |
---|
534 | exitVoice(); |
---|
535 | return 0; |
---|
536 | } |
---|
537 | |
---|
538 | s = currentVoice->buffer + fl; |
---|
539 | while (l > 0) |
---|
540 | { |
---|
541 | fl++; |
---|
542 | i++; |
---|
543 | l--; |
---|
544 | *t++ = *s; |
---|
545 | if (*s == '\n') |
---|
546 | { |
---|
547 | *t = '\0'; |
---|
548 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
549 | || (traceit&TRACE_SHOW_LINE1)) |
---|
550 | { |
---|
551 | if (currentVoice->filename==NULL) |
---|
552 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
553 | else if (VoiceType()!=BT_example) |
---|
554 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
555 | prompt_char = '.'; |
---|
556 | } |
---|
557 | if (*(s+1) == '\0') |
---|
558 | { |
---|
559 | currentVoice->fptr = -1; |
---|
560 | FreeL((ADDRESS)(currentVoice->buffer)); |
---|
561 | currentVoice->buffer=NULL; |
---|
562 | FileAttribs[voice].buffer = NULL; |
---|
563 | exitVoice(); |
---|
564 | } |
---|
565 | else |
---|
566 | currentVoice->fptr = fl; |
---|
567 | return i; |
---|
568 | } |
---|
569 | else if (*s == '\0') |
---|
570 | { |
---|
571 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
572 | || (traceit&TRACE_SHOW_LINE1)) |
---|
573 | { |
---|
574 | if (currentVoice->filename==NULL) |
---|
575 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
576 | else |
---|
577 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
578 | prompt_char = '.'; |
---|
579 | } |
---|
580 | currentVoice->fptr = -1; |
---|
581 | FreeL((ADDRESS)(currentVoice->buffer)); |
---|
582 | currentVoice->buffer = NULL; |
---|
583 | exitVoice(); |
---|
584 | return i-1; |
---|
585 | } |
---|
586 | s++; |
---|
587 | } |
---|
588 | currentVoice->fptr = fl; |
---|
589 | return i; |
---|
590 | } |
---|
591 | |
---|
592 | /*2 |
---|
593 | * init all data structures |
---|
594 | */ |
---|
595 | void I_FEbase(void) |
---|
596 | { |
---|
597 | FileAttribs[0].files = yyin = stdin; |
---|
598 | FileAttribs[0].filename = mstrdup("STDIN"); |
---|
599 | yylineno = 1; |
---|
600 | currentVoice = &FileAttribs[0]; |
---|
601 | } |
---|
602 | |
---|
603 | static char * feBufferStart; |
---|
604 | /* only used in StringSet(S)/StringAppend(S)*/ |
---|
605 | char * StringAppend(char *fmt, ...) |
---|
606 | { |
---|
607 | va_list ap; |
---|
608 | char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/ |
---|
609 | int more; |
---|
610 | va_start(ap, fmt); |
---|
611 | if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength) |
---|
612 | { |
---|
613 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
614 | int l=s-feBuffer; |
---|
615 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
616 | more); |
---|
617 | feBufferLength=more; |
---|
618 | s=feBuffer+l; |
---|
619 | #ifndef BSD_SPRINTF |
---|
620 | feBufferStart=s; |
---|
621 | #endif |
---|
622 | } |
---|
623 | #ifdef BSD_SPRINTF |
---|
624 | vsprintf(s, fmt, ap); |
---|
625 | while (*s!='\0') s++; |
---|
626 | feBufferStart =s; |
---|
627 | #else |
---|
628 | feBufferStart += vsprintf(s, fmt, ap); |
---|
629 | #endif |
---|
630 | va_end(ap); |
---|
631 | return feBuffer; |
---|
632 | } |
---|
633 | |
---|
634 | char * StringAppendS(char *st) |
---|
635 | { |
---|
636 | /* feBufferStart is feBuffer + strlen(feBuffer);*/ |
---|
637 | int more,l; |
---|
638 | int ll=feBufferStart-feBuffer; |
---|
639 | if ((more=ll+2+(l=strlen(st)))>feBufferLength) |
---|
640 | { |
---|
641 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
642 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
643 | more); |
---|
644 | feBufferLength=more; |
---|
645 | feBufferStart=feBuffer+ll; |
---|
646 | } |
---|
647 | strcat(feBufferStart, st); |
---|
648 | feBufferStart +=l; |
---|
649 | return feBuffer; |
---|
650 | } |
---|
651 | |
---|
652 | char * StringSet(char *fmt, ...) |
---|
653 | { |
---|
654 | va_list ap; |
---|
655 | char *s = feBuffer; |
---|
656 | va_start(ap, fmt); |
---|
657 | #ifdef BSD_SPRINTF |
---|
658 | vsprintf(s, fmt, ap); |
---|
659 | while (*s!='\0') s++; |
---|
660 | feBufferStart = s; |
---|
661 | #else |
---|
662 | feBufferStart = feBuffer + vsprintf(s, fmt, ap); |
---|
663 | #endif |
---|
664 | va_end(ap); |
---|
665 | return feBuffer; |
---|
666 | } |
---|
667 | |
---|
668 | char * StringSetS(char *st) |
---|
669 | { |
---|
670 | int more,l; |
---|
671 | if ((l=strlen(st))>feBufferLength) |
---|
672 | { |
---|
673 | more = ((l + (4*1024-1))/(4*1024))*(4*1024); |
---|
674 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
675 | more); |
---|
676 | feBufferLength=more; |
---|
677 | } |
---|
678 | strcpy(feBuffer,st); |
---|
679 | feBufferStart=feBuffer+l; |
---|
680 | return feBuffer; |
---|
681 | } |
---|
682 | |
---|
683 | void PrintTCLS(char c, char *s) |
---|
684 | { |
---|
685 | #ifndef macintosh |
---|
686 | int l=strlen(s); |
---|
687 | if (l>0) PrintTCL(c,l,s); |
---|
688 | #endif |
---|
689 | } |
---|
690 | |
---|
691 | void WerrorS(char *s) |
---|
692 | { |
---|
693 | #ifdef HAVE_MPSR |
---|
694 | if (feBatch) |
---|
695 | { |
---|
696 | if (feErrors==NULL) |
---|
697 | { |
---|
698 | feErrors=(char *)Alloc(256); |
---|
699 | feErrorsLen=256; |
---|
700 | strcpy(feErrors,s); |
---|
701 | } |
---|
702 | else |
---|
703 | { |
---|
704 | if (((int)(strlen(s)+strlen(feErrors)))>=feErrorsLen) |
---|
705 | { |
---|
706 | feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256); |
---|
707 | feErrorsLen+=256; |
---|
708 | } |
---|
709 | strcat(feErrors,s); |
---|
710 | } |
---|
711 | strcat(feErrors,"\n"); |
---|
712 | } |
---|
713 | else |
---|
714 | #endif |
---|
715 | { |
---|
716 | #ifdef HAVE_TCL |
---|
717 | if (tclmode) |
---|
718 | { |
---|
719 | //PrintTCLS('E',s); |
---|
720 | //PrintTCLS('E',"\n"); |
---|
721 | PrintTCLS('N',s); |
---|
722 | PrintTCLS('N',"\n"); |
---|
723 | } |
---|
724 | else |
---|
725 | #endif |
---|
726 | { |
---|
727 | fwrite(" ? ",1,5,stderr); |
---|
728 | fwrite(s,1,strlen(s),stderr); |
---|
729 | fwrite("\n",1,1,stderr); |
---|
730 | fflush(stderr); |
---|
731 | if (feProt&PROT_O) |
---|
732 | { |
---|
733 | fwrite(" ? ",1,5,feProtFile); |
---|
734 | fwrite(s,1,strlen(s),feProtFile); |
---|
735 | fwrite("\n",1,1,feProtFile); |
---|
736 | } |
---|
737 | } |
---|
738 | } |
---|
739 | errorreported = TRUE; |
---|
740 | } |
---|
741 | |
---|
742 | extern "C" { |
---|
743 | void Werror(char *fmt, ...) |
---|
744 | { |
---|
745 | va_list ap; |
---|
746 | va_start(ap, fmt); |
---|
747 | char *s=(char *)Alloc(256); |
---|
748 | vsprintf(s, fmt, ap); |
---|
749 | WerrorS(s); |
---|
750 | Free(s,256); |
---|
751 | va_end(ap); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | void WarnS(char *s) |
---|
756 | { |
---|
757 | #ifdef HAVE_TCL |
---|
758 | if (tclmode) |
---|
759 | { |
---|
760 | PrintTCLS('W',s); |
---|
761 | } |
---|
762 | else |
---|
763 | #endif |
---|
764 | { |
---|
765 | fwrite("// ** ",1,6,stdout); |
---|
766 | fwrite(s,1,strlen(s),stdout); |
---|
767 | fwrite("\n",1,1,stdout); |
---|
768 | fflush(stdout); |
---|
769 | if (feProt&PROT_O) |
---|
770 | { |
---|
771 | fwrite("// ** ",1,6,feProtFile); |
---|
772 | fwrite(s,1,strlen(s),feProtFile); |
---|
773 | fwrite("\n",1,1,feProtFile); |
---|
774 | } |
---|
775 | } |
---|
776 | } |
---|
777 | |
---|
778 | void Warn(char *fmt, ...) |
---|
779 | { |
---|
780 | va_list ap; |
---|
781 | va_start(ap, fmt); |
---|
782 | char *s=(char *)Alloc(256); |
---|
783 | vsprintf(s, fmt, ap); |
---|
784 | WarnS(s); |
---|
785 | Free(s,256); |
---|
786 | va_end(ap); |
---|
787 | } |
---|
788 | |
---|
789 | #ifdef macintosh |
---|
790 | static int lines = 0; |
---|
791 | static int cols = 0; |
---|
792 | void mwrite(uchar c) |
---|
793 | { |
---|
794 | if (c == '\n') |
---|
795 | { |
---|
796 | cols = 0; |
---|
797 | if (lines == pagelength) |
---|
798 | { |
---|
799 | lines = 0; |
---|
800 | fePause(); |
---|
801 | } |
---|
802 | else |
---|
803 | { |
---|
804 | lines++; |
---|
805 | fePutChar(c); |
---|
806 | } |
---|
807 | } |
---|
808 | else |
---|
809 | { |
---|
810 | fePutChar(c); |
---|
811 | cols++; |
---|
812 | if (cols == colmax) |
---|
813 | { |
---|
814 | // cols = 0; //will be done by mwrite('\n'); |
---|
815 | mwrite('\n'); |
---|
816 | } |
---|
817 | } |
---|
818 | } |
---|
819 | #endif |
---|
820 | |
---|
821 | void PrintS(char *s) |
---|
822 | { |
---|
823 | #ifdef macintosh |
---|
824 | char c; |
---|
825 | while ('\0' != (c = *s++)) |
---|
826 | { |
---|
827 | mwrite(c); |
---|
828 | } |
---|
829 | #else |
---|
830 | #ifdef HAVE_TCL |
---|
831 | if (tclmode) |
---|
832 | { |
---|
833 | PrintTCLS('N',s); |
---|
834 | } |
---|
835 | else |
---|
836 | #endif |
---|
837 | { |
---|
838 | fwrite(s,1,strlen(s),stdout); |
---|
839 | fflush(stdout); |
---|
840 | if (feProt&PROT_O) |
---|
841 | { |
---|
842 | fwrite(s,1,strlen(s),feProtFile); |
---|
843 | } |
---|
844 | } |
---|
845 | #endif |
---|
846 | } |
---|
847 | |
---|
848 | void Print(char *fmt, ...) |
---|
849 | { |
---|
850 | va_list ap; |
---|
851 | va_start(ap, fmt); |
---|
852 | #ifdef HAVE_TCL |
---|
853 | if(tclmode) |
---|
854 | #endif |
---|
855 | #if (defined(HAVE_TCL) || defined(macintosh)) |
---|
856 | { |
---|
857 | char *s=(char *)Alloc(strlen(fmt)+256); |
---|
858 | vsprintf(s,fmt, ap); |
---|
859 | #ifdef HAVE_TCL |
---|
860 | PrintTCLS('N',s); |
---|
861 | #endif |
---|
862 | #ifdef macintosh |
---|
863 | char c; |
---|
864 | while ('\0' != (c = *s++)) |
---|
865 | { |
---|
866 | mwrite(c); |
---|
867 | } |
---|
868 | if (feProt&PROT_O) |
---|
869 | { |
---|
870 | vfprintf(feProrFile,fmt,ap); |
---|
871 | } |
---|
872 | #endif |
---|
873 | } |
---|
874 | #endif |
---|
875 | #if !defined(macintosh) || defined(HAVE_TCL) |
---|
876 | #ifdef HAVE_TCL |
---|
877 | else |
---|
878 | #endif |
---|
879 | { |
---|
880 | vfprintf(stdout, fmt, ap); |
---|
881 | fflush(stdout); |
---|
882 | if (feProt&PROT_O) |
---|
883 | { |
---|
884 | vfprintf(feProtFile,fmt,ap); |
---|
885 | } |
---|
886 | } |
---|
887 | #endif |
---|
888 | va_end(ap); |
---|
889 | } |
---|
890 | |
---|
891 | void PrintLn() |
---|
892 | { |
---|
893 | PrintS("\n"); |
---|
894 | } |
---|
895 | |
---|
896 | void fePause() |
---|
897 | { |
---|
898 | uchar c; |
---|
899 | mflush(); |
---|
900 | #ifndef macintosh |
---|
901 | fputs("pause>",stderr); |
---|
902 | #else |
---|
903 | fputs("pause>\n",stderr); |
---|
904 | #endif |
---|
905 | c = fgetc(stdin); |
---|
906 | if (((c == '\003') || (c == 'C')) || (c == 'c')) |
---|
907 | { |
---|
908 | m2_end(1); |
---|
909 | } |
---|
910 | } |
---|
911 | |
---|
912 | /*2 |
---|
913 | * print input lines (si_echo or TRACE), set prompt_char |
---|
914 | */ |
---|
915 | void showInput(void) |
---|
916 | { |
---|
917 | if ((inputswitch <= 0) || (traceit&TRACE_SHOW_LINE) |
---|
918 | || (traceit&TRACE_SHOW_LINE1)) |
---|
919 | { |
---|
920 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
921 | || (traceit&TRACE_SHOW_LINE1)) |
---|
922 | { |
---|
923 | #ifdef HAVE_TCL |
---|
924 | if (tclmode) |
---|
925 | { |
---|
926 | PrintTCL('P',(prompt_char=='>')? 0 : 1,NULL); |
---|
927 | } |
---|
928 | else |
---|
929 | #endif |
---|
930 | if (BVERBOSE(V_PROMPT)) |
---|
931 | { |
---|
932 | if (inputswitch == 0) |
---|
933 | { |
---|
934 | fe_promptstr[0]=prompt_char; |
---|
935 | #ifndef HAVE_READLINE |
---|
936 | PrintS(fe_promptstr); |
---|
937 | #endif |
---|
938 | } |
---|
939 | else |
---|
940 | { |
---|
941 | if (currentVoice->filename==NULL) |
---|
942 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
943 | else |
---|
944 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
945 | } |
---|
946 | prompt_char = '.'; |
---|
947 | mflush(); |
---|
948 | } |
---|
949 | #ifdef macintosh |
---|
950 | cols = 0; |
---|
951 | #endif |
---|
952 | } |
---|
953 | } |
---|
954 | } |
---|
955 | |
---|
956 | void monitor(char* s, int mode) |
---|
957 | { |
---|
958 | if (feProt) |
---|
959 | { |
---|
960 | fclose(feProtFile); |
---|
961 | } |
---|
962 | if ((s!=NULL) && (*s!='\0')) |
---|
963 | { |
---|
964 | feProtFile = fopen(s,"w"); |
---|
965 | if (feProtFile==NULL) |
---|
966 | { |
---|
967 | Werror("cannot open %s",s); |
---|
968 | } |
---|
969 | else |
---|
970 | feProt = (BOOLEAN)mode; |
---|
971 | } |
---|
972 | } |
---|
973 | |
---|
974 | |
---|
975 | char* eati(char *s, int *i) |
---|
976 | { |
---|
977 | int l=0; |
---|
978 | |
---|
979 | if (*s >= '0' && *s <= '9') |
---|
980 | { |
---|
981 | *i = 0; |
---|
982 | while (*s >= '0' && *s <= '9') |
---|
983 | { |
---|
984 | *i *= 10; |
---|
985 | *i += *s++ - '0'; |
---|
986 | l++; |
---|
987 | if ((l>MAX_INT_LEN)||((*i) <0)) |
---|
988 | { |
---|
989 | s-=l; |
---|
990 | Werror("`%s` greater than %d(max. integer representation)", |
---|
991 | s,INT_MAX); |
---|
992 | return s; |
---|
993 | } |
---|
994 | } |
---|
995 | } |
---|
996 | else *i = 1; |
---|
997 | return s; |
---|
998 | } |
---|
999 | |
---|