1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: febase.cc,v 1.20 1997-07-16 11:25:55 Singular 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((env != NULL ? strlen(env) : 0) |
---|
164 | +strlen(SINGULAR_DATADIR)+2); |
---|
165 | if (env != NULL) |
---|
166 | { |
---|
167 | strcpy(s, env); |
---|
168 | s[strlen(env)] = FS_SEP; |
---|
169 | s[strlen(env)+1] = '\0'; |
---|
170 | strcat(s, SINGULAR_DATADIR); |
---|
171 | } |
---|
172 | else strcpy(s, SINGULAR_DATADIR); |
---|
173 | env = s; |
---|
174 | #endif |
---|
175 | if (where==NULL) s=(char *)AllocL(250); |
---|
176 | else s=where; |
---|
177 | if (env!=NULL) |
---|
178 | { |
---|
179 | char *p,*q; |
---|
180 | p = env; |
---|
181 | while( (q=strchr(p, FS_SEP)) != NULL) |
---|
182 | { |
---|
183 | *q = '\0'; |
---|
184 | strcpy(s,p); |
---|
185 | *q = FS_SEP; |
---|
186 | strcat(s, DIR_SEPP); |
---|
187 | strcat(s, path); |
---|
188 | #ifndef macintosh |
---|
189 | if(!access(s, R_OK)) { found++; break; } |
---|
190 | #else |
---|
191 | f=fopen(s,mode); |
---|
192 | if (f!=NULL) { found++; fclose(f); break; } |
---|
193 | #endif |
---|
194 | p = q+1; |
---|
195 | } |
---|
196 | if(!found) |
---|
197 | { |
---|
198 | strcpy(s,p); |
---|
199 | strcat(s, DIR_SEPP); |
---|
200 | strcat(s, path); |
---|
201 | } |
---|
202 | f=fopen(s,mode); |
---|
203 | if (f!=NULL) |
---|
204 | { |
---|
205 | if (where==NULL) FreeL((ADDRESS)s); |
---|
206 | return f; |
---|
207 | } |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | if (where!=NULL) strcpy(s/*where*/,path); |
---|
212 | f=fopen(path,mode); |
---|
213 | } |
---|
214 | #ifndef macintosh |
---|
215 | FreeL(env); |
---|
216 | #endif |
---|
217 | if (where==NULL) FreeL((ADDRESS)s); |
---|
218 | } |
---|
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 | memset(&FileAttribs[levmax],0,16*sizeof(Voices)); |
---|
299 | ifswitch=(short *)ReAlloc(ifswitch, |
---|
300 | levmax*sizeof(short), |
---|
301 | (levmax+16)*sizeof(short)); |
---|
302 | memset(&ifswitch[levmax],0,16*sizeof(short)); |
---|
303 | levmax+=16; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | /*2 |
---|
308 | * init a new voice similiar to the current |
---|
309 | */ |
---|
310 | void Voices::Next() |
---|
311 | { |
---|
312 | v_oldlineno = yylineno; |
---|
313 | v_echo = si_echo; |
---|
314 | v_fileVoice = fileVoice; |
---|
315 | inc_voice(); |
---|
316 | |
---|
317 | currentVoice = &FileAttribs[voice]; |
---|
318 | currentVoice->Init(); |
---|
319 | } |
---|
320 | |
---|
321 | /*2 |
---|
322 | * start the file 'fname' (STDIN is stdin) as a new voice (cf.VFile) |
---|
323 | */ |
---|
324 | int newVoice(char* s) |
---|
325 | { |
---|
326 | currentVoice->Next(); |
---|
327 | return (int)currentVoice->VFile((char *)s); |
---|
328 | } |
---|
329 | |
---|
330 | void newBuffer(char* s, int t, char* pname) |
---|
331 | { |
---|
332 | currentVoice->Next(); |
---|
333 | currentVoice->Buffer(s,t); |
---|
334 | if (pname) currentVoice->filename = mstrdup(pname); |
---|
335 | //printf("start buffer %d typ %d\n",voice,t); |
---|
336 | } |
---|
337 | |
---|
338 | Voices * Voices::Buffer(char* buf, int t) |
---|
339 | { |
---|
340 | inputswitch = v_inputswitch = t; |
---|
341 | buffer = buf; |
---|
342 | typ = t; |
---|
343 | //si_echo = 0; |
---|
344 | switch (t) |
---|
345 | { |
---|
346 | case BT_example: |
---|
347 | case BT_proc: v_lineno = yylineno; ::yylineno = 3; break; |
---|
348 | case BT_file: v_lineno = yylineno; ::yylineno = 1; break; |
---|
349 | case BT_if: |
---|
350 | case BT_else: ::yylineno = v_lineno = blocklineno - 1; break; |
---|
351 | case BT_break: ::yylineno = v_lineno = blocklineno - 2; break; |
---|
352 | } |
---|
353 | return this; |
---|
354 | } |
---|
355 | |
---|
356 | /*2 |
---|
357 | * after leaving a voice: |
---|
358 | * setup everything from the this level |
---|
359 | */ |
---|
360 | int Voices::Exit() |
---|
361 | { |
---|
362 | if (voice >= 0) |
---|
363 | { |
---|
364 | si_echo = v_echo; |
---|
365 | fileVoice = v_fileVoice; |
---|
366 | yyin = files; |
---|
367 | yylineno = v_oldlineno; |
---|
368 | inputswitch = v_inputswitch; |
---|
369 | return 0; |
---|
370 | } |
---|
371 | //Print("Exit:%d\n",voice); |
---|
372 | return 1; |
---|
373 | } |
---|
374 | |
---|
375 | /*2 |
---|
376 | * exit Buffer of type 'typ': |
---|
377 | * returns 1 if buffer type could not be found |
---|
378 | */ |
---|
379 | int exitBuffer(int typ) |
---|
380 | { |
---|
381 | //printf("exitBuffer: %d\n",typ); |
---|
382 | if (typ == BT_break) // valid inside for, while. may skip if, else |
---|
383 | { |
---|
384 | /*4 first check for valid buffer type, skip if/else*/ |
---|
385 | for (int i=voice; i>0; i--) |
---|
386 | { |
---|
387 | if ((FileAttribs[i].typ == BT_if) |
---|
388 | ||(FileAttribs[i].typ == BT_else)) continue; |
---|
389 | if (FileAttribs[i].typ == BT_break /*typ*/) |
---|
390 | { |
---|
391 | while ((/*typ*/ BT_break != currentVoice->typ) |
---|
392 | && (voice > 0)) |
---|
393 | { |
---|
394 | exitVoice(); |
---|
395 | } |
---|
396 | return exitVoice(); |
---|
397 | } |
---|
398 | else return 1; |
---|
399 | } |
---|
400 | /*4 break not inside a for/while: return an error*/ |
---|
401 | if (/*typ*/ BT_break != currentVoice->typ) return 1; |
---|
402 | return exitVoice(); |
---|
403 | } |
---|
404 | |
---|
405 | if ((typ == BT_proc) |
---|
406 | || (typ == BT_example)) |
---|
407 | { |
---|
408 | for (int i=voice; i>0; i--) |
---|
409 | { |
---|
410 | if (FileAttribs[i].typ == 0) break; |
---|
411 | if ((FileAttribs[i].typ == BT_proc) |
---|
412 | || (FileAttribs[i].typ == BT_example)) |
---|
413 | { |
---|
414 | while ((BT_proc != currentVoice->typ) |
---|
415 | && (BT_example != currentVoice->typ) |
---|
416 | && (voice > 0)) |
---|
417 | { |
---|
418 | exitVoice(); |
---|
419 | } |
---|
420 | return exitVoice(); |
---|
421 | } |
---|
422 | } |
---|
423 | } |
---|
424 | /*4 return not inside a proc: return an error*/ |
---|
425 | return 1; |
---|
426 | } |
---|
427 | |
---|
428 | /*2 |
---|
429 | * jump to the beginning of a buffer |
---|
430 | */ |
---|
431 | int contBuffer(int typ) |
---|
432 | { |
---|
433 | if (typ == BT_break) // valid inside for, while. may skip if, else |
---|
434 | { |
---|
435 | // first check for valid buffer type |
---|
436 | for (int i=voice; i>0; i--) |
---|
437 | { |
---|
438 | if ((FileAttribs[i].typ == BT_if) |
---|
439 | ||(FileAttribs[i].typ == BT_else)) continue; |
---|
440 | if (FileAttribs[i].typ == BT_break /*typ*/) |
---|
441 | { |
---|
442 | while (/*typ*/ BT_break != currentVoice->typ && (voice > i)) |
---|
443 | { |
---|
444 | exitVoice(); |
---|
445 | } |
---|
446 | currentVoice->fptr = 0L; |
---|
447 | yylineno = currentVoice->v_lineno; |
---|
448 | return 0; |
---|
449 | } |
---|
450 | else return 1; |
---|
451 | } |
---|
452 | } |
---|
453 | return 1; |
---|
454 | } |
---|
455 | |
---|
456 | /*2 |
---|
457 | * leave a voice: kill local variables |
---|
458 | * setup everything from the previous level (via Exit) |
---|
459 | */ |
---|
460 | int exitVoice() |
---|
461 | { |
---|
462 | if (voice <= 0) |
---|
463 | { |
---|
464 | if (feBatch) return 1; |
---|
465 | else m2_end(0); |
---|
466 | } |
---|
467 | //printf("exitVoice %d, typ %d\n",voice,FileAttribs[voice].typ); |
---|
468 | if (FileAttribs[voice].typ==BT_if) |
---|
469 | { |
---|
470 | ifswitch[voice-1]=2; |
---|
471 | } |
---|
472 | else |
---|
473 | { |
---|
474 | ifswitch[voice-1]=0; |
---|
475 | //if ((FileAttribs[voice].typ==BT_proc) |
---|
476 | //||(FileAttribs[voice].typ==BT_example) |
---|
477 | //||(FileAttribs[voice].typ==0)) |
---|
478 | //{ |
---|
479 | // killlocals(myynest); |
---|
480 | // printf("killlocals %d\n",myynest); |
---|
481 | //} |
---|
482 | } |
---|
483 | if (inputswitch == -1) |
---|
484 | { |
---|
485 | fclose(yyin); |
---|
486 | } |
---|
487 | else if (inputswitch > 0) |
---|
488 | { |
---|
489 | if (FileAttribs[voice].filename!=NULL) |
---|
490 | { |
---|
491 | FreeL((ADDRESS)FileAttribs[voice].filename); |
---|
492 | FileAttribs[voice].filename=NULL; |
---|
493 | } |
---|
494 | if (FileAttribs[voice].buffer!=NULL) |
---|
495 | { |
---|
496 | FreeL((ADDRESS)FileAttribs[voice].buffer); |
---|
497 | FileAttribs[voice].buffer=NULL; |
---|
498 | } |
---|
499 | } |
---|
500 | currentVoice = &FileAttribs[--voice]; |
---|
501 | return currentVoice->Exit(); |
---|
502 | } |
---|
503 | |
---|
504 | int readbuf(char* buf, int l) |
---|
505 | { |
---|
506 | char *s; |
---|
507 | char * t = buf; |
---|
508 | int i = 0; |
---|
509 | long fl = currentVoice->fptr; |
---|
510 | if (fl == -1L) |
---|
511 | { |
---|
512 | t[0] = '\0'; |
---|
513 | exitVoice(); |
---|
514 | return 0; |
---|
515 | } |
---|
516 | |
---|
517 | s = currentVoice->buffer + fl; |
---|
518 | while (l > 0) |
---|
519 | { |
---|
520 | fl++; |
---|
521 | i++; |
---|
522 | l--; |
---|
523 | *t++ = *s; |
---|
524 | if (*s == '\n') |
---|
525 | { |
---|
526 | *t = '\0'; |
---|
527 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
528 | || (traceit&TRACE_SHOW_LINE1)) |
---|
529 | { |
---|
530 | if (currentVoice->filename==NULL) |
---|
531 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
532 | else if (VoiceType()!=BT_example) |
---|
533 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
534 | prompt_char = '.'; |
---|
535 | } |
---|
536 | if (*(s+1) == '\0') |
---|
537 | { |
---|
538 | currentVoice->fptr = -1; |
---|
539 | FreeL((ADDRESS)(currentVoice->buffer)); |
---|
540 | currentVoice->buffer=NULL; |
---|
541 | FileAttribs[voice].buffer = NULL; |
---|
542 | exitVoice(); |
---|
543 | } |
---|
544 | else |
---|
545 | currentVoice->fptr = fl; |
---|
546 | return i; |
---|
547 | } |
---|
548 | else if (*s == '\0') |
---|
549 | { |
---|
550 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
551 | || (traceit&TRACE_SHOW_LINE1)) |
---|
552 | { |
---|
553 | if (currentVoice->filename==NULL) |
---|
554 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
555 | else |
---|
556 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
557 | prompt_char = '.'; |
---|
558 | } |
---|
559 | currentVoice->fptr = -1; |
---|
560 | FreeL((ADDRESS)(currentVoice->buffer)); |
---|
561 | currentVoice->buffer = NULL; |
---|
562 | exitVoice(); |
---|
563 | return i-1; |
---|
564 | } |
---|
565 | s++; |
---|
566 | } |
---|
567 | currentVoice->fptr = fl; |
---|
568 | return i; |
---|
569 | } |
---|
570 | |
---|
571 | /*2 |
---|
572 | * init all data structures |
---|
573 | */ |
---|
574 | void I_FEbase(void) |
---|
575 | { |
---|
576 | currentVoice = FileAttribs[0].VFile("STDIN"); |
---|
577 | } |
---|
578 | |
---|
579 | static char * feBufferStart; |
---|
580 | /* only used in StringSet(S)/StringAppend(S)*/ |
---|
581 | char * StringAppend(char *fmt, ...) |
---|
582 | { |
---|
583 | va_list ap; |
---|
584 | char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/ |
---|
585 | int more; |
---|
586 | va_start(ap, fmt); |
---|
587 | if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength) |
---|
588 | { |
---|
589 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
590 | int l=s-feBuffer; |
---|
591 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
592 | more); |
---|
593 | feBufferLength=more; |
---|
594 | s=feBuffer+l; |
---|
595 | #ifndef BSD_SPRINTF |
---|
596 | feBufferStart=s; |
---|
597 | #endif |
---|
598 | } |
---|
599 | #ifdef BSD_SPRINTF |
---|
600 | vsprintf(s, fmt, ap); |
---|
601 | while (*s!='\0') s++; |
---|
602 | feBufferStart =s; |
---|
603 | #else |
---|
604 | feBufferStart += vsprintf(s, fmt, ap); |
---|
605 | #endif |
---|
606 | va_end(ap); |
---|
607 | return feBuffer; |
---|
608 | } |
---|
609 | |
---|
610 | char * StringAppendS(char *st) |
---|
611 | { |
---|
612 | /* feBufferStart is feBuffer + strlen(feBuffer);*/ |
---|
613 | int more,l; |
---|
614 | int ll=feBufferStart-feBuffer; |
---|
615 | if ((more=ll+2+(l=strlen(st)))>feBufferLength) |
---|
616 | { |
---|
617 | more = ((more + (4*1024-1))/(4*1024))*(4*1024); |
---|
618 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
619 | more); |
---|
620 | feBufferLength=more; |
---|
621 | feBufferStart=feBuffer+ll; |
---|
622 | } |
---|
623 | strcat(feBufferStart, st); |
---|
624 | feBufferStart +=l; |
---|
625 | return feBuffer; |
---|
626 | } |
---|
627 | |
---|
628 | char * StringSet(char *fmt, ...) |
---|
629 | { |
---|
630 | va_list ap; |
---|
631 | char *s = feBuffer; |
---|
632 | va_start(ap, fmt); |
---|
633 | #ifdef BSD_SPRINTF |
---|
634 | vsprintf(s, fmt, ap); |
---|
635 | while (*s!='\0') s++; |
---|
636 | feBufferStart = s; |
---|
637 | #else |
---|
638 | feBufferStart = feBuffer + vsprintf(s, fmt, ap); |
---|
639 | #endif |
---|
640 | va_end(ap); |
---|
641 | return feBuffer; |
---|
642 | } |
---|
643 | |
---|
644 | char * StringSetS(char *st) |
---|
645 | { |
---|
646 | int more,l; |
---|
647 | if ((l=strlen(st))>feBufferLength) |
---|
648 | { |
---|
649 | more = ((l + (4*1024-1))/(4*1024))*(4*1024); |
---|
650 | feBuffer=(char *)ReAlloc((ADDRESS)feBuffer,feBufferLength, |
---|
651 | more); |
---|
652 | feBufferLength=more; |
---|
653 | } |
---|
654 | strcpy(feBuffer,st); |
---|
655 | feBufferStart=feBuffer+l; |
---|
656 | return feBuffer; |
---|
657 | } |
---|
658 | |
---|
659 | void PrintTCLS(char c, char *s) |
---|
660 | { |
---|
661 | #ifndef macintosh |
---|
662 | int l=strlen(s); |
---|
663 | if (l>0) PrintTCL(c,l,s); |
---|
664 | #endif |
---|
665 | } |
---|
666 | |
---|
667 | void WerrorS(char *s) |
---|
668 | { |
---|
669 | #ifdef HAVE_MPSR |
---|
670 | if (feBatch) |
---|
671 | { |
---|
672 | if (feErrors==NULL) |
---|
673 | { |
---|
674 | feErrors=(char *)Alloc(256); |
---|
675 | feErrorsLen=256; |
---|
676 | strcpy(feErrors,s); |
---|
677 | } |
---|
678 | else |
---|
679 | { |
---|
680 | if (((int)(strlen(s)+strlen(feErrors)))>=feErrorsLen) |
---|
681 | { |
---|
682 | feErrors=(char *)ReAlloc(feErrors,feErrorsLen,feErrorsLen+256); |
---|
683 | feErrorsLen+=256; |
---|
684 | } |
---|
685 | strcat(feErrors,s); |
---|
686 | } |
---|
687 | strcat(feErrors,"\n"); |
---|
688 | } |
---|
689 | else |
---|
690 | #endif |
---|
691 | { |
---|
692 | #ifdef HAVE_TCL |
---|
693 | if (tclmode) |
---|
694 | { |
---|
695 | //PrintTCLS('E',s); |
---|
696 | //PrintTCLS('E',"\n"); |
---|
697 | PrintTCLS('N',s); |
---|
698 | PrintTCLS('N',"\n"); |
---|
699 | } |
---|
700 | else |
---|
701 | #endif |
---|
702 | { |
---|
703 | fwrite(" ? ",1,5,stderr); |
---|
704 | fwrite(s,1,strlen(s),stderr); |
---|
705 | fwrite("\n",1,1,stderr); |
---|
706 | fflush(stderr); |
---|
707 | if (feProt&PROT_O) |
---|
708 | { |
---|
709 | fwrite(" ? ",1,5,feProtFile); |
---|
710 | fwrite(s,1,strlen(s),feProtFile); |
---|
711 | fwrite("\n",1,1,feProtFile); |
---|
712 | } |
---|
713 | } |
---|
714 | } |
---|
715 | errorreported = TRUE; |
---|
716 | } |
---|
717 | |
---|
718 | extern "C" { |
---|
719 | void Werror(char *fmt, ...) |
---|
720 | { |
---|
721 | va_list ap; |
---|
722 | va_start(ap, fmt); |
---|
723 | char *s=(char *)Alloc(256); |
---|
724 | vsprintf(s, fmt, ap); |
---|
725 | WerrorS(s); |
---|
726 | Free(s,256); |
---|
727 | va_end(ap); |
---|
728 | } |
---|
729 | } |
---|
730 | |
---|
731 | void WarnS(char *s) |
---|
732 | { |
---|
733 | #ifdef HAVE_TCL |
---|
734 | if (tclmode) |
---|
735 | { |
---|
736 | PrintTCLS('W',s); |
---|
737 | } |
---|
738 | else |
---|
739 | #endif |
---|
740 | { |
---|
741 | fwrite("// ** ",1,6,stdout); |
---|
742 | fwrite(s,1,strlen(s),stdout); |
---|
743 | fwrite("\n",1,1,stdout); |
---|
744 | fflush(stdout); |
---|
745 | if (feProt&PROT_O) |
---|
746 | { |
---|
747 | fwrite("// ** ",1,6,feProtFile); |
---|
748 | fwrite(s,1,strlen(s),feProtFile); |
---|
749 | fwrite("\n",1,1,feProtFile); |
---|
750 | } |
---|
751 | } |
---|
752 | } |
---|
753 | |
---|
754 | void Warn(char *fmt, ...) |
---|
755 | { |
---|
756 | va_list ap; |
---|
757 | va_start(ap, fmt); |
---|
758 | char *s=(char *)Alloc(256); |
---|
759 | vsprintf(s, fmt, ap); |
---|
760 | WarnS(s); |
---|
761 | Free(s,256); |
---|
762 | va_end(ap); |
---|
763 | } |
---|
764 | |
---|
765 | #ifdef macintosh |
---|
766 | static int lines = 0; |
---|
767 | static int cols = 0; |
---|
768 | void mwrite(uchar c) |
---|
769 | { |
---|
770 | if (c == '\n') |
---|
771 | { |
---|
772 | cols = 0; |
---|
773 | if (lines == pagelength) |
---|
774 | { |
---|
775 | lines = 0; |
---|
776 | fePause(); |
---|
777 | } |
---|
778 | else |
---|
779 | { |
---|
780 | lines++; |
---|
781 | fePutChar(c); |
---|
782 | } |
---|
783 | } |
---|
784 | else |
---|
785 | { |
---|
786 | fePutChar(c); |
---|
787 | cols++; |
---|
788 | if (cols == colmax) |
---|
789 | { |
---|
790 | // cols = 0; //will be done by mwrite('\n'); |
---|
791 | mwrite('\n'); |
---|
792 | } |
---|
793 | } |
---|
794 | } |
---|
795 | #endif |
---|
796 | |
---|
797 | void PrintS(char *s) |
---|
798 | { |
---|
799 | #ifdef macintosh |
---|
800 | char c; |
---|
801 | while ('\0' != (c = *s++)) |
---|
802 | { |
---|
803 | mwrite(c); |
---|
804 | } |
---|
805 | #else |
---|
806 | #ifdef HAVE_TCL |
---|
807 | if (tclmode) |
---|
808 | { |
---|
809 | PrintTCLS('N',s); |
---|
810 | } |
---|
811 | else |
---|
812 | #endif |
---|
813 | { |
---|
814 | fwrite(s,1,strlen(s),stdout); |
---|
815 | fflush(stdout); |
---|
816 | if (feProt&PROT_O) |
---|
817 | { |
---|
818 | fwrite(s,1,strlen(s),feProtFile); |
---|
819 | } |
---|
820 | } |
---|
821 | #endif |
---|
822 | } |
---|
823 | |
---|
824 | void Print(char *fmt, ...) |
---|
825 | { |
---|
826 | va_list ap; |
---|
827 | va_start(ap, fmt); |
---|
828 | #ifdef HAVE_TCL |
---|
829 | if(tclmode) |
---|
830 | #endif |
---|
831 | #if (defined(HAVE_TCL) || defined(macintosh)) |
---|
832 | { |
---|
833 | char *s=(char *)Alloc(strlen(fmt)+256); |
---|
834 | vsprintf(s,fmt, ap); |
---|
835 | #ifdef HAVE_TCL |
---|
836 | PrintTCLS('N',s); |
---|
837 | #endif |
---|
838 | #ifdef macintosh |
---|
839 | char c; |
---|
840 | while ('\0' != (c = *s++)) |
---|
841 | { |
---|
842 | mwrite(c); |
---|
843 | } |
---|
844 | if (feProt&PROT_O) |
---|
845 | { |
---|
846 | vfprintf(feProtFile,fmt,ap); |
---|
847 | } |
---|
848 | #endif |
---|
849 | } |
---|
850 | #endif |
---|
851 | #if !defined(macintosh) || defined(HAVE_TCL) |
---|
852 | #ifdef HAVE_TCL |
---|
853 | else |
---|
854 | #endif |
---|
855 | { |
---|
856 | vfprintf(stdout, fmt, ap); |
---|
857 | fflush(stdout); |
---|
858 | if (feProt&PROT_O) |
---|
859 | { |
---|
860 | vfprintf(feProtFile,fmt,ap); |
---|
861 | } |
---|
862 | } |
---|
863 | #endif |
---|
864 | va_end(ap); |
---|
865 | } |
---|
866 | |
---|
867 | void PrintLn() |
---|
868 | { |
---|
869 | PrintS("\n"); |
---|
870 | } |
---|
871 | |
---|
872 | void fePause() |
---|
873 | { |
---|
874 | uchar c; |
---|
875 | mflush(); |
---|
876 | #ifndef macintosh |
---|
877 | fputs("pause>",stderr); |
---|
878 | #else |
---|
879 | fputs("pause>\n",stderr); |
---|
880 | #endif |
---|
881 | c = fgetc(stdin); |
---|
882 | if (((c == '\003') || (c == 'C')) || (c == 'c')) |
---|
883 | { |
---|
884 | m2_end(1); |
---|
885 | } |
---|
886 | } |
---|
887 | |
---|
888 | /*2 |
---|
889 | * print input lines (si_echo or TRACE), set prompt_char |
---|
890 | */ |
---|
891 | void showInput(void) |
---|
892 | { |
---|
893 | if ((inputswitch <= 0) || (traceit&TRACE_SHOW_LINE) |
---|
894 | || (traceit&TRACE_SHOW_LINE1)) |
---|
895 | { |
---|
896 | if ((si_echo > voice) || (inputswitch == 0) || (traceit&TRACE_SHOW_LINE) |
---|
897 | || (traceit&TRACE_SHOW_LINE1)) |
---|
898 | { |
---|
899 | #ifdef HAVE_TCL |
---|
900 | if (tclmode) |
---|
901 | { |
---|
902 | PrintTCL('P',(prompt_char=='>')? 0 : 1,NULL); |
---|
903 | } |
---|
904 | else |
---|
905 | #endif |
---|
906 | if (BVERBOSE(V_PROMPT)) |
---|
907 | { |
---|
908 | if (inputswitch == 0) |
---|
909 | { |
---|
910 | fe_promptstr[0]=prompt_char; |
---|
911 | #ifndef HAVE_READLINE |
---|
912 | PrintS(fe_promptstr); |
---|
913 | #endif |
---|
914 | } |
---|
915 | else |
---|
916 | { |
---|
917 | if (currentVoice->filename==NULL) |
---|
918 | Print("(none) %3d%c ",yylineno,prompt_char); |
---|
919 | else |
---|
920 | Print("%s %3d%c ",currentVoice->filename,yylineno,prompt_char); |
---|
921 | } |
---|
922 | prompt_char = '.'; |
---|
923 | mflush(); |
---|
924 | } |
---|
925 | #ifdef macintosh |
---|
926 | cols = 0; |
---|
927 | #endif |
---|
928 | } |
---|
929 | } |
---|
930 | } |
---|
931 | |
---|
932 | void monitor(char* s, int mode) |
---|
933 | { |
---|
934 | if (feProt) |
---|
935 | { |
---|
936 | fclose(feProtFile); |
---|
937 | } |
---|
938 | if ((s!=NULL) && (*s!='\0')) |
---|
939 | { |
---|
940 | feProtFile = fopen(s,"w"); |
---|
941 | if (feProtFile==NULL) |
---|
942 | { |
---|
943 | Werror("cannot open %s",s); |
---|
944 | } |
---|
945 | else |
---|
946 | feProt = (BOOLEAN)mode; |
---|
947 | } |
---|
948 | } |
---|
949 | |
---|
950 | |
---|
951 | char* eati(char *s, int *i) |
---|
952 | { |
---|
953 | int l=0; |
---|
954 | |
---|
955 | if (*s >= '0' && *s <= '9') |
---|
956 | { |
---|
957 | *i = 0; |
---|
958 | while (*s >= '0' && *s <= '9') |
---|
959 | { |
---|
960 | *i *= 10; |
---|
961 | *i += *s++ - '0'; |
---|
962 | l++; |
---|
963 | if ((l>MAX_INT_LEN)||((*i) <0)) |
---|
964 | { |
---|
965 | s-=l; |
---|
966 | Werror("`%s` greater than %d(max. integer representation)", |
---|
967 | s,INT_MAX); |
---|
968 | return s; |
---|
969 | } |
---|
970 | } |
---|
971 | } |
---|
972 | else *i = 1; |
---|
973 | return s; |
---|
974 | } |
---|
975 | |
---|