1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: febase.cc,v 1.16 1997-05-03 17:23:33 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 *)Alloc0(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 SINGULARDATADIR |
---|
123 | * as set by configure |
---|
124 | */ |
---|
125 | #ifdef macintosh |
---|
126 | # define FS_SEP ',' |
---|
127 | # define DIR_SEP ':' |
---|
128 | # define DIR_SEPP ":" |
---|
129 | #else |
---|
130 | #ifdef MSDOS |
---|
131 | # define FS_SEP ';' |
---|
132 | # define DIR_SEP '\\' |
---|
133 | # define DIR_SEPP "\\" |
---|
134 | #else |
---|
135 | #ifdef atarist |
---|
136 | # define FS_SEP ';' |
---|
137 | # define DIR_SEP '\\' |
---|
138 | # define DIR_SEPP "\\" |
---|
139 | #else /* unix */ |
---|
140 | # define FS_SEP ':' |
---|
141 | # define DIR_SEP '/' |
---|
142 | # define DIR_SEPP "/" |
---|
143 | #endif /* atarist */ |
---|
144 | #endif /* MSDOS */ |
---|
145 | #endif /* macintosh */ |
---|
146 | |
---|
147 | FILE * feFopen(char *path, char *mode, char *where,int useWerror) |
---|
148 | { |
---|
149 | FILE * f=fopen(path,mode); |
---|
150 | if (where!=NULL) strcpy(where,path); |
---|
151 | if ((*mode=='r') && (path[0]!=DIR_SEP)&&(path[0]!='.') |
---|
152 | &&(f==NULL)) |
---|
153 | { |
---|
154 | char found = 0; |
---|
155 | #ifdef MSDOS |
---|
156 | char *env=getenv("SPATH"); |
---|
157 | #else |
---|
158 | char *env=getenv("SINGULARPATH"); |
---|
159 | #endif |
---|
160 | char *s; |
---|
161 | #ifndef macintosh |
---|
162 | // extend path by SINGULAR_DATADIR |
---|
163 | s = (char*) AllocL(strlen(env)+strlen(SINGULAR_DATADIR)+2); |
---|
164 | if (env != NULL) |
---|
165 | { |
---|
166 | strcpy(s, env); |
---|
167 | s[strlen(env)] = FS_SEP; |
---|
168 | s[strlen(env)+1] = '\0'; |
---|
169 | strcat(s, SINGULAR_DATADIR); |
---|
170 | } |
---|
171 | else strcpy(s, SINGULAR_DATADIR); |
---|
172 | env = s; |
---|
173 | #endif |
---|
174 | if (where==NULL) s=(char *)AllocL(250); |
---|
175 | else s=where; |
---|
176 | if (env!=NULL) |
---|
177 | { |
---|
178 | char *p,*q; |
---|
179 | p = env; |
---|
180 | while( (q=strchr(p, FS_SEP)) != NULL) |
---|
181 | { |
---|
182 | *q = '\0'; |
---|
183 | strcpy(s,p); |
---|
184 | *q = FS_SEP; |
---|
185 | strcat(s, DIR_SEPP); |
---|
186 | strcat(s, path); |
---|
187 | #ifndef macintosh |
---|
188 | if(!access(s, R_OK)) { found++; break; } |
---|
189 | #else |
---|
190 | f=fopen(s,mode); |
---|
191 | if (f!=NULL) { found++; fclose(f); break; } |
---|
192 | #endif |
---|
193 | p = q+1; |
---|
194 | } |
---|
195 | if(!found) |
---|
196 | { |
---|
197 | strcpy(s,p); |
---|
198 | strcat(s, DIR_SEPP); |
---|
199 | strcat(s, path); |
---|
200 | } |
---|
201 | f=fopen(s,mode); |
---|
202 | if (f!=NULL) |
---|
203 | { |
---|
204 | if (where==NULL) FreeL((ADDRESS)s); |
---|
205 | return f; |
---|
206 | } |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | if (where!=NULL) strcpy(s/*where*/,path); |
---|
211 | f=fopen(path,mode); |
---|
212 | } |
---|
213 | #ifndef macintosh |
---|
214 | FreeL(env); |
---|
215 | #endif |
---|
216 | if (where==NULL) FreeL((ADDRESS)s); |
---|
217 | } |
---|
218 | if ((f==NULL)&&(useWerror)) |
---|
219 | Werror("cannot open `%s`",path); |
---|
220 | return f; |
---|
221 | } |
---|
222 | |
---|
223 | /*2 |
---|
224 | * the name of the current 'voice': the procname (or filename) |
---|
225 | */ |
---|
226 | const char * VoiceName() |
---|
227 | { |
---|
228 | if (FileAttribs[fileVoice].filename!=NULL) |
---|
229 | return FileAttribs[fileVoice].filename; |
---|
230 | return sNoName; |
---|
231 | } |
---|
232 | |
---|
233 | /*2 |
---|
234 | * the name of the 'voice' number 'i': the procname (or filename) |
---|
235 | */ |
---|
236 | const char * VoiceName(int i) |
---|
237 | { |
---|
238 | if (FileAttribs[i].filename!=NULL) |
---|
239 | return FileAttribs[i].filename; |
---|
240 | return sNoName; |
---|
241 | } |
---|
242 | |
---|
243 | /*2 |
---|
244 | * the type of the current voice:BT_proc, BT_example, BT_file |
---|
245 | */ |
---|
246 | int VoiceType() |
---|
247 | { |
---|
248 | int i=fileVoice; |
---|
249 | while ((FileAttribs[i].typ!=BT_proc) |
---|
250 | &&(FileAttribs[i].typ!=BT_example) |
---|
251 | &&(FileAttribs[i].typ!=BT_file) |
---|
252 | &&(i>0)) |
---|
253 | i--; |
---|
254 | return FileAttribs[i].typ; |
---|
255 | } |
---|
256 | /*2 |
---|
257 | * start the file 'fname' (STDIN is stdin) in the current voice (cf.newVoice) |
---|
258 | */ |
---|
259 | Voices * Voices::VFile(char* fname) |
---|
260 | { |
---|
261 | if (strcmp(fname,"STDIN") == 0) |
---|
262 | { |
---|
263 | yyin = stdin; |
---|
264 | v_inputswitch = 0; |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | yyin = feFopen(fname,"r",NULL,TRUE); |
---|
269 | v_inputswitch = -1; |
---|
270 | } |
---|
271 | files = yyin; |
---|
272 | filename = mstrdup(fname); |
---|
273 | v_echo = si_echo; |
---|
274 | fileVoice = voice; |
---|
275 | yylineno = 1; |
---|
276 | if (files==NULL) |
---|
277 | { |
---|
278 | inputswitch = 0; |
---|
279 | exitVoice(); |
---|
280 | return NULL; |
---|
281 | } |
---|
282 | inputswitch= v_inputswitch; |
---|
283 | return this; |
---|
284 | } |
---|
285 | |
---|
286 | /*3 |
---|
287 | * increment voice counter, allocate new memory |
---|
288 | */ |
---|
289 | static inline void inc_voice() |
---|
290 | { |
---|
291 | voice++; |
---|
292 | if (voice >= levmax) |
---|
293 | { |
---|
294 | FileAttribs=(Voices *)ReAlloc(FileAttribs, |
---|
295 | levmax*sizeof(Voices), |
---|
296 | (levmax+16)*sizeof(Voices)); |
---|
297 | memset(&FileAttribs[levmax],0,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 voice: kill local variables |
---|
457 | * setup everything from the previous level (via Exit) |
---|
458 | */ |
---|
459 | int exitVoice() |
---|
460 | { |
---|
461 | if (voice <= 0) m2_end(0); |
---|
462 | //printf("exitVoice %d, typ %d\n",voice,FileAttribs[voice].typ); |
---|
463 | if (FileAttribs[voice].typ==BT_if) |
---|
464 | { |
---|
465 | ifswitch[voice-1]=2; |
---|
466 | } |
---|
467 | else |
---|
468 | { |
---|
469 | ifswitch[voice-1]=0; |
---|
470 | //if ((FileAttribs[voice].typ==BT_proc) |
---|
471 | //||(FileAttribs[voice].typ==BT_example) |
---|
472 | //||(FileAttribs[voice].typ==0)) |
---|
473 | //{ |
---|
474 | // killlocals(myynest); |
---|
475 | // printf("killlocals %d\n",myynest); |
---|
476 | //} |
---|
477 | } |
---|
478 | if (inputswitch == -1) |
---|
479 | { |
---|
480 | fclose(yyin); |
---|
481 | } |
---|
482 | else if (inputswitch > 0) |
---|
483 | { |
---|
484 | if (FileAttribs[voice].filename!=NULL) |
---|
485 | { |
---|
486 | FreeL((ADDRESS)FileAttribs[voice].filename); |
---|
487 | FileAttribs[voice].filename=NULL; |
---|
488 | } |
---|
489 | if (FileAttribs[voice].buffer!=NULL) |
---|
490 | { |
---|
491 | FreeL((ADDRESS)FileAttribs[voice].buffer); |
---|
492 | FileAttribs[voice].buffer=NULL; |
---|
493 | } |
---|
494 | } |
---|
495 | currentVoice = &FileAttribs[--voice]; |
---|
496 | return currentVoice->Exit(); |
---|
497 | } |
---|
498 | |
---|
499 | int readbuf(char* buf, int l) |
---|
500 | { |
---|
501 | char *s; |
---|
502 | char * t = buf; |
---|
503 | int i = 0; |
---|
504 | long fl = currentVoice->fptr; |
---|
505 | if (fl == -1L) |
---|
506 | { |
---|
507 | t[0] = '\0'; |
---|
508 | exitVoice(); |
---|
509 | return 0; |
---|
510 | } |
---|
511 | |
---|
512 | s = currentVoice->buffer + fl; |
---|
513 | while (l > 0) |
---|
514 | { |
---|
515 | fl++; |
---|
516 | i++; |
---|
517 | l--; |
---|
518 | *t++ = *s; |
---|
519 | if (*s == '\n') |
---|
520 | { |
---|
521 | *t = '\0'; |
---|
522 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
523 | || (traceit&TRACE_SHOW_LINE1)) |
---|
524 | { |
---|
525 | if (currentVoice->filename==NULL) |
---|
526 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
527 | else if (VoiceType()!=BT_example) |
---|
528 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
529 | prompt_char = '.'; |
---|
530 | } |
---|
531 | if (*(s+1) == '\0') |
---|
532 | { |
---|
533 | currentVoice->fptr = -1; |
---|
534 | FreeL((ADDRESS)(currentVoice->buffer)); |
---|
535 | currentVoice->buffer=NULL; |
---|
536 | FileAttribs[voice].buffer = NULL; |
---|
537 | exitVoice(); |
---|
538 | } |
---|
539 | else |
---|
540 | currentVoice->fptr = fl; |
---|
541 | return i; |
---|
542 | } |
---|
543 | else if (*s == '\0') |
---|
544 | { |
---|
545 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
546 | || (traceit&TRACE_SHOW_LINE1)) |
---|
547 | { |
---|
548 | if (currentVoice->filename==NULL) |
---|
549 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
550 | else |
---|
551 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
552 | prompt_char = '.'; |
---|
553 | } |
---|
554 | currentVoice->fptr = -1; |
---|
555 | FreeL((ADDRESS)(currentVoice->buffer)); |
---|
556 | currentVoice->buffer = NULL; |
---|
557 | exitVoice(); |
---|
558 | return i-1; |
---|
559 | } |
---|
560 | s++; |
---|
561 | } |
---|
562 | currentVoice->fptr = fl; |
---|
563 | return i; |
---|
564 | } |
---|
565 | |
---|
566 | /*2 |
---|
567 | * init all data structures |
---|
568 | */ |
---|
569 | void I_FEbase(void) |
---|
570 | { |
---|
571 | currentVoice = FileAttribs[0].VFile("STDIN"); |
---|
572 | } |
---|
573 | |
---|
574 | static char * feBufferStart; |
---|
575 | /* only used in StringSet(S)/StringAppend(S)*/ |
---|
576 | char * StringAppend(char *fmt, ...) |
---|
577 | { |
---|
578 | va_list ap; |
---|
579 | char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/ |
---|
580 | int more; |
---|
581 | va_start(ap, fmt); |
---|
582 | if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength) |
---|
583 | { |
---|
584 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
585 | int l=s-feBuffer; |
---|
586 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
587 | more); |
---|
588 | feBufferLength=more; |
---|
589 | s=feBuffer+l; |
---|
590 | #ifndef BSD_SPRINTF |
---|
591 | feBufferStart=s; |
---|
592 | #endif |
---|
593 | } |
---|
594 | #ifdef BSD_SPRINTF |
---|
595 | vsprintf(s, fmt, ap); |
---|
596 | while (*s!='\0') s++; |
---|
597 | feBufferStart =s; |
---|
598 | #else |
---|
599 | feBufferStart += vsprintf(s, fmt, ap); |
---|
600 | #endif |
---|
601 | va_end(ap); |
---|
602 | return feBuffer; |
---|
603 | } |
---|
604 | |
---|
605 | char * StringAppendS(char *st) |
---|
606 | { |
---|
607 | /* feBufferStart is feBuffer + strlen(feBuffer);*/ |
---|
608 | int more,l; |
---|
609 | int ll=feBufferStart-feBuffer; |
---|
610 | if ((more=ll+2+(l=strlen(st)))>feBufferLength) |
---|
611 | { |
---|
612 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
613 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
614 | more); |
---|
615 | feBufferLength=more; |
---|
616 | feBufferStart=feBuffer+ll; |
---|
617 | } |
---|
618 | strcat(feBufferStart, st); |
---|
619 | feBufferStart +=l; |
---|
620 | return feBuffer; |
---|
621 | } |
---|
622 | |
---|
623 | char * StringSet(char *fmt, ...) |
---|
624 | { |
---|
625 | va_list ap; |
---|
626 | char *s = feBuffer; |
---|
627 | va_start(ap, fmt); |
---|
628 | #ifdef BSD_SPRINTF |
---|
629 | vsprintf(s, fmt, ap); |
---|
630 | while (*s!='\0') s++; |
---|
631 | feBufferStart = s; |
---|
632 | #else |
---|
633 | feBufferStart = feBuffer + vsprintf(s, fmt, ap); |
---|
634 | #endif |
---|
635 | va_end(ap); |
---|
636 | return feBuffer; |
---|
637 | } |
---|
638 | |
---|
639 | char * StringSetS(char *st) |
---|
640 | { |
---|
641 | int more,l; |
---|
642 | if ((l=strlen(st))>feBufferLength) |
---|
643 | { |
---|
644 | more = ((l + (4*1024-1))/(4*1024))*(4*1024); |
---|
645 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
646 | more); |
---|
647 | feBufferLength=more; |
---|
648 | } |
---|
649 | strcpy(feBuffer,st); |
---|
650 | feBufferStart=feBuffer+l; |
---|
651 | return feBuffer; |
---|
652 | } |
---|
653 | |
---|
654 | void PrintTCLS(char c, char *s) |
---|
655 | { |
---|
656 | #ifndef macintosh |
---|
657 | int l=strlen(s); |
---|
658 | if (l>0) PrintTCL(c,l,s); |
---|
659 | #endif |
---|
660 | } |
---|
661 | |
---|
662 | void WerrorS(char *s) |
---|
663 | { |
---|
664 | #ifdef HAVE_MPSR |
---|
665 | if (feBatch) |
---|
666 | { |
---|
667 | if (feErrors==NULL) |
---|
668 | { |
---|
669 | feErrors=(char *)Alloc(256); |
---|
670 | feErrorsLen=256; |
---|
671 | strcpy(feErrors,s); |
---|
672 | } |
---|
673 | else |
---|
674 | { |
---|
675 | if (((int)(strlen(s)+strlen(feErrors)))>=feErrorsLen) |
---|
676 | { |
---|
677 | feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256); |
---|
678 | feErrorsLen+=256; |
---|
679 | } |
---|
680 | strcat(feErrors,s); |
---|
681 | } |
---|
682 | strcat(feErrors,"\n"); |
---|
683 | } |
---|
684 | else |
---|
685 | #endif |
---|
686 | { |
---|
687 | #ifdef HAVE_TCL |
---|
688 | if (tclmode) |
---|
689 | { |
---|
690 | //PrintTCLS('E',s); |
---|
691 | //PrintTCLS('E',"\n"); |
---|
692 | PrintTCLS('N',s); |
---|
693 | PrintTCLS('N',"\n"); |
---|
694 | } |
---|
695 | else |
---|
696 | #endif |
---|
697 | { |
---|
698 | fwrite(" ? ",1,5,stderr); |
---|
699 | fwrite(s,1,strlen(s),stderr); |
---|
700 | fwrite("\n",1,1,stderr); |
---|
701 | fflush(stderr); |
---|
702 | if (feProt&PROT_O) |
---|
703 | { |
---|
704 | fwrite(" ? ",1,5,feProtFile); |
---|
705 | fwrite(s,1,strlen(s),feProtFile); |
---|
706 | fwrite("\n",1,1,feProtFile); |
---|
707 | } |
---|
708 | } |
---|
709 | } |
---|
710 | errorreported = TRUE; |
---|
711 | } |
---|
712 | |
---|
713 | extern "C" { |
---|
714 | void Werror(char *fmt, ...) |
---|
715 | { |
---|
716 | va_list ap; |
---|
717 | va_start(ap, fmt); |
---|
718 | char *s=(char *)Alloc(256); |
---|
719 | vsprintf(s, fmt, ap); |
---|
720 | WerrorS(s); |
---|
721 | Free(s,256); |
---|
722 | va_end(ap); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | void WarnS(char *s) |
---|
727 | { |
---|
728 | #ifdef HAVE_TCL |
---|
729 | if (tclmode) |
---|
730 | { |
---|
731 | PrintTCLS('W',s); |
---|
732 | } |
---|
733 | else |
---|
734 | #endif |
---|
735 | { |
---|
736 | fwrite("// ** ",1,6,stdout); |
---|
737 | fwrite(s,1,strlen(s),stdout); |
---|
738 | fwrite("\n",1,1,stdout); |
---|
739 | fflush(stdout); |
---|
740 | if (feProt&PROT_O) |
---|
741 | { |
---|
742 | fwrite("// ** ",1,6,feProtFile); |
---|
743 | fwrite(s,1,strlen(s),feProtFile); |
---|
744 | fwrite("\n",1,1,feProtFile); |
---|
745 | } |
---|
746 | } |
---|
747 | } |
---|
748 | |
---|
749 | void Warn(char *fmt, ...) |
---|
750 | { |
---|
751 | va_list ap; |
---|
752 | va_start(ap, fmt); |
---|
753 | char *s=(char *)Alloc(256); |
---|
754 | vsprintf(s, fmt, ap); |
---|
755 | WarnS(s); |
---|
756 | Free(s,256); |
---|
757 | va_end(ap); |
---|
758 | } |
---|
759 | |
---|
760 | #ifdef macintosh |
---|
761 | static int lines = 0; |
---|
762 | static int cols = 0; |
---|
763 | void mwrite(uchar c) |
---|
764 | { |
---|
765 | if (c == '\n') |
---|
766 | { |
---|
767 | cols = 0; |
---|
768 | if (lines == pagelength) |
---|
769 | { |
---|
770 | lines = 0; |
---|
771 | fePause(); |
---|
772 | } |
---|
773 | else |
---|
774 | { |
---|
775 | lines++; |
---|
776 | fePutChar(c); |
---|
777 | } |
---|
778 | } |
---|
779 | else |
---|
780 | { |
---|
781 | fePutChar(c); |
---|
782 | cols++; |
---|
783 | if (cols == colmax) |
---|
784 | { |
---|
785 | // cols = 0; //will be done by mwrite('\n'); |
---|
786 | mwrite('\n'); |
---|
787 | } |
---|
788 | } |
---|
789 | } |
---|
790 | #endif |
---|
791 | |
---|
792 | void PrintS(char *s) |
---|
793 | { |
---|
794 | #ifdef macintosh |
---|
795 | char c; |
---|
796 | while ('\0' != (c = *s++)) |
---|
797 | { |
---|
798 | mwrite(c); |
---|
799 | } |
---|
800 | #else |
---|
801 | #ifdef HAVE_TCL |
---|
802 | if (tclmode) |
---|
803 | { |
---|
804 | PrintTCLS('N',s); |
---|
805 | } |
---|
806 | else |
---|
807 | #endif |
---|
808 | { |
---|
809 | fwrite(s,1,strlen(s),stdout); |
---|
810 | fflush(stdout); |
---|
811 | if (feProt&PROT_O) |
---|
812 | { |
---|
813 | fwrite(s,1,strlen(s),feProtFile); |
---|
814 | } |
---|
815 | } |
---|
816 | #endif |
---|
817 | } |
---|
818 | |
---|
819 | void Print(char *fmt, ...) |
---|
820 | { |
---|
821 | va_list ap; |
---|
822 | va_start(ap, fmt); |
---|
823 | #ifdef HAVE_TCL |
---|
824 | if(tclmode) |
---|
825 | #endif |
---|
826 | #if (defined(HAVE_TCL) || defined(macintosh)) |
---|
827 | { |
---|
828 | char *s=(char *)Alloc(strlen(fmt)+256); |
---|
829 | vsprintf(s,fmt, ap); |
---|
830 | #ifdef HAVE_TCL |
---|
831 | PrintTCLS('N',s); |
---|
832 | #endif |
---|
833 | #ifdef macintosh |
---|
834 | char c; |
---|
835 | while ('\0' != (c = *s++)) |
---|
836 | { |
---|
837 | mwrite(c); |
---|
838 | } |
---|
839 | if (feProt&PROT_O) |
---|
840 | { |
---|
841 | vfprintf(feProrFile,fmt,ap); |
---|
842 | } |
---|
843 | #endif |
---|
844 | } |
---|
845 | #endif |
---|
846 | #if !defined(macintosh) || defined(HAVE_TCL) |
---|
847 | #ifdef HAVE_TCL |
---|
848 | else |
---|
849 | #endif |
---|
850 | { |
---|
851 | vfprintf(stdout, fmt, ap); |
---|
852 | fflush(stdout); |
---|
853 | if (feProt&PROT_O) |
---|
854 | { |
---|
855 | vfprintf(feProtFile,fmt,ap); |
---|
856 | } |
---|
857 | } |
---|
858 | #endif |
---|
859 | va_end(ap); |
---|
860 | } |
---|
861 | |
---|
862 | void PrintLn() |
---|
863 | { |
---|
864 | PrintS("\n"); |
---|
865 | } |
---|
866 | |
---|
867 | void fePause() |
---|
868 | { |
---|
869 | uchar c; |
---|
870 | mflush(); |
---|
871 | #ifndef macintosh |
---|
872 | fputs("pause>",stderr); |
---|
873 | #else |
---|
874 | fputs("pause>\n",stderr); |
---|
875 | #endif |
---|
876 | c = fgetc(stdin); |
---|
877 | if (((c == '\003') || (c == 'C')) || (c == 'c')) |
---|
878 | { |
---|
879 | m2_end(1); |
---|
880 | } |
---|
881 | } |
---|
882 | |
---|
883 | /*2 |
---|
884 | * print input lines (si_echo or TRACE), set prompt_char |
---|
885 | */ |
---|
886 | void showInput(void) |
---|
887 | { |
---|
888 | if ((inputswitch <= 0) || (traceit&TRACE_SHOW_LINE) |
---|
889 | || (traceit&TRACE_SHOW_LINE1)) |
---|
890 | { |
---|
891 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
892 | || (traceit&TRACE_SHOW_LINE1)) |
---|
893 | { |
---|
894 | #ifdef HAVE_TCL |
---|
895 | if (tclmode) |
---|
896 | { |
---|
897 | PrintTCL('P',(prompt_char=='>')? 0 : 1,NULL); |
---|
898 | } |
---|
899 | else |
---|
900 | #endif |
---|
901 | if (BVERBOSE(V_PROMPT)) |
---|
902 | { |
---|
903 | if (inputswitch == 0) |
---|
904 | { |
---|
905 | fe_promptstr[0]=prompt_char; |
---|
906 | #ifndef HAVE_READLINE |
---|
907 | PrintS(fe_promptstr); |
---|
908 | #endif |
---|
909 | } |
---|
910 | else |
---|
911 | { |
---|
912 | if (currentVoice->filename==NULL) |
---|
913 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
914 | else |
---|
915 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
916 | } |
---|
917 | prompt_char = '.'; |
---|
918 | mflush(); |
---|
919 | } |
---|
920 | #ifdef macintosh |
---|
921 | cols = 0; |
---|
922 | #endif |
---|
923 | } |
---|
924 | } |
---|
925 | } |
---|
926 | |
---|
927 | void monitor(char* s, int mode) |
---|
928 | { |
---|
929 | if (feProt) |
---|
930 | { |
---|
931 | fclose(feProtFile); |
---|
932 | } |
---|
933 | if ((s!=NULL) && (*s!='\0')) |
---|
934 | { |
---|
935 | feProtFile = fopen(s,"w"); |
---|
936 | if (feProtFile==NULL) |
---|
937 | { |
---|
938 | Werror("cannot open %s",s); |
---|
939 | } |
---|
940 | else |
---|
941 | feProt = (BOOLEAN)mode; |
---|
942 | } |
---|
943 | } |
---|
944 | |
---|
945 | |
---|
946 | char* eati(char *s, int *i) |
---|
947 | { |
---|
948 | int l=0; |
---|
949 | |
---|
950 | if (*s >= '0' && *s <= '9') |
---|
951 | { |
---|
952 | *i = 0; |
---|
953 | while (*s >= '0' && *s <= '9') |
---|
954 | { |
---|
955 | *i *= 10; |
---|
956 | *i += *s++ - '0'; |
---|
957 | l++; |
---|
958 | if ((l>MAX_INT_LEN)||((*i) <0)) |
---|
959 | { |
---|
960 | s-=l; |
---|
961 | Werror("`%s` greater than %d(max. integer representation)", |
---|
962 | s,INT_MAX); |
---|
963 | return s; |
---|
964 | } |
---|
965 | } |
---|
966 | } |
---|
967 | else *i = 1; |
---|
968 | return s; |
---|
969 | } |
---|
970 | |
---|