1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: output system |
---|
7 | */ |
---|
8 | |
---|
9 | #include "config.h" |
---|
10 | |
---|
11 | #include <stdlib.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <misc/mylimits.h> |
---|
14 | #include <stdarg.h> |
---|
15 | #include <sys/stat.h> |
---|
16 | #include <ctype.h> |
---|
17 | #include <unistd.h> |
---|
18 | |
---|
19 | #ifdef HAVE_PWD_H |
---|
20 | #include <pwd.h> |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <reporter/reporter.h> |
---|
24 | #include <omalloc/omalloc.h> |
---|
25 | //#include "options.h" |
---|
26 | |
---|
27 | #define fePutChar(c) fputc((unsigned char)(c),stdout) |
---|
28 | /*0 implementation */ |
---|
29 | |
---|
30 | // output/print buffer: |
---|
31 | #define INITIAL_PRINT_BUFFER 24*1024L |
---|
32 | // line buffer for reading: |
---|
33 | // minimal value for MAX_FILE_BUFFER: 4*4096 - see Tst/Long/gcd0_l.tst |
---|
34 | // this is an upper limit for the size of monomials/numbers read via the interpreter |
---|
35 | #define MAX_FILE_BUFFER 4*4096 |
---|
36 | static long feBufferLength=INITIAL_PRINT_BUFFER; |
---|
37 | static char * feBuffer=(char *)omAlloc(INITIAL_PRINT_BUFFER); |
---|
38 | |
---|
39 | BOOLEAN errorreported = FALSE; |
---|
40 | char * feErrors=NULL; |
---|
41 | int feErrorsLen=0; |
---|
42 | BOOLEAN feWarn = TRUE; |
---|
43 | BOOLEAN feOut = TRUE; |
---|
44 | |
---|
45 | const char feNotImplemented[]="not implemented"; |
---|
46 | |
---|
47 | int feProt = FALSE; |
---|
48 | FILE* feProtFile; |
---|
49 | |
---|
50 | static char * feBufferStart; |
---|
51 | /* only used in StringSet(S)/StringAppend(S)*/ |
---|
52 | char * StringAppend(const char *fmt, ...) |
---|
53 | { |
---|
54 | va_list ap; |
---|
55 | char *s = feBufferStart; /*feBuffer + strlen(feBuffer);*/ |
---|
56 | int vs; |
---|
57 | long more; |
---|
58 | va_start(ap, fmt); |
---|
59 | if ((more=feBufferStart-feBuffer+strlen(fmt)+100)>feBufferLength) |
---|
60 | { |
---|
61 | more = ((more + (8*1024-1))/(8*1024))*(8*1024); |
---|
62 | int l=s-feBuffer; |
---|
63 | feBuffer=(char *)omReallocSize((void *)feBuffer,feBufferLength, |
---|
64 | more); |
---|
65 | #if (!defined(NDEBUG)) && (!defined(OM_NDEBUG)) && defined(HAVE_CONFIG_H) |
---|
66 | omMarkAsStaticAddr(feBuffer); |
---|
67 | #endif |
---|
68 | feBufferLength=more; |
---|
69 | s=feBuffer+l; |
---|
70 | #ifndef BSD_SPRINTF |
---|
71 | feBufferStart=s; |
---|
72 | #endif |
---|
73 | } |
---|
74 | #ifdef BSD_SPRINTF |
---|
75 | vsprintf(s, fmt, ap); |
---|
76 | while (*s!='\0') s++; |
---|
77 | feBufferStart =s; |
---|
78 | #else |
---|
79 | #ifdef HAVE_VSNPRINTF |
---|
80 | vs = vsnprintf(s, feBufferLength - (feBufferStart - feBuffer), fmt, ap); |
---|
81 | if (vs == -1) |
---|
82 | { |
---|
83 | assume(0); |
---|
84 | feBufferStart = feBuffer + feBufferLength -1; |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | feBufferStart += vs; |
---|
89 | } |
---|
90 | #else |
---|
91 | feBufferStart += vsprintf(s, fmt, ap); |
---|
92 | #endif |
---|
93 | #endif |
---|
94 | omCheckAddrSize(feBuffer, feBufferLength); |
---|
95 | va_end(ap); |
---|
96 | return feBuffer; |
---|
97 | } |
---|
98 | |
---|
99 | char * StringAppendS(const char *st) |
---|
100 | { |
---|
101 | if (*st!='\0') |
---|
102 | { |
---|
103 | /* feBufferStart is feBuffer + strlen(feBuffer);*/ |
---|
104 | int l; |
---|
105 | long more; |
---|
106 | int ll=feBufferStart-feBuffer; |
---|
107 | if ((more=ll+2+(l=strlen(st)))>feBufferLength) |
---|
108 | { |
---|
109 | more = ((more + (8*1024-1))/(8*1024))*(8*1024); |
---|
110 | feBuffer=(char *)omReallocSize((void *)feBuffer,feBufferLength, |
---|
111 | more); |
---|
112 | feBufferLength=more; |
---|
113 | feBufferStart=feBuffer+ll; |
---|
114 | } |
---|
115 | strcat(feBufferStart, st); |
---|
116 | feBufferStart +=l; |
---|
117 | } |
---|
118 | return feBuffer; |
---|
119 | } |
---|
120 | |
---|
121 | char * StringSetS(const char *st) |
---|
122 | { |
---|
123 | int l; |
---|
124 | long more; |
---|
125 | if ((l=strlen(st))>feBufferLength) |
---|
126 | { |
---|
127 | more = ((l + (4*1024-1))/(4*1024))*(4*1024); |
---|
128 | feBuffer=(char *)omReallocSize((void *)feBuffer,feBufferLength, |
---|
129 | more); |
---|
130 | feBufferLength=more; |
---|
131 | } |
---|
132 | strcpy(feBuffer,st); |
---|
133 | feBufferStart=feBuffer+l; |
---|
134 | return feBuffer; |
---|
135 | } |
---|
136 | |
---|
137 | #ifdef HAVE_TCL |
---|
138 | extern "C" { |
---|
139 | void PrintTCLS(const char c, const char *s) |
---|
140 | { |
---|
141 | int l=strlen(s); |
---|
142 | if (l>0) PrintTCL(c,l,s); |
---|
143 | } |
---|
144 | } |
---|
145 | #endif |
---|
146 | |
---|
147 | extern "C" { |
---|
148 | void WerrorS(const char *s) |
---|
149 | { |
---|
150 | #ifdef HAVE_MPSR |
---|
151 | if (fe_fgets_stdin==fe_fgets_dummy) |
---|
152 | { |
---|
153 | if (feErrors==NULL) |
---|
154 | { |
---|
155 | feErrors=(char *)omAlloc(256); |
---|
156 | feErrorsLen=256; |
---|
157 | *feErrors = '\0'; |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | if (((int)(strlen((char *)s)+ 20 +strlen(feErrors)))>=feErrorsLen) |
---|
162 | { |
---|
163 | feErrors=(char *)omReallocSize(feErrors,feErrorsLen,feErrorsLen+256); |
---|
164 | feErrorsLen+=256; |
---|
165 | } |
---|
166 | } |
---|
167 | strcat(feErrors, "Singular error: "); |
---|
168 | strcat(feErrors, (char *)s); |
---|
169 | } |
---|
170 | else |
---|
171 | #endif |
---|
172 | { |
---|
173 | #ifdef HAVE_TCL |
---|
174 | if (tclmode) |
---|
175 | { |
---|
176 | PrintTCLS('E',(char *)s); |
---|
177 | PrintTCLS('E',"\n"); |
---|
178 | } |
---|
179 | else |
---|
180 | #endif |
---|
181 | { |
---|
182 | fwrite(" ? ",1,5,stderr); |
---|
183 | fwrite((char *)s,1,strlen((char *)s),stderr); |
---|
184 | fwrite("\n",1,1,stderr); |
---|
185 | fflush(stderr); |
---|
186 | if (feProt&PROT_O) |
---|
187 | { |
---|
188 | fwrite(" ? ",1,5,feProtFile); |
---|
189 | fwrite((char *)s,1,strlen((char *)s),feProtFile); |
---|
190 | fwrite("\n",1,1,feProtFile); |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | errorreported = TRUE; |
---|
195 | #ifdef HAVE_FACTORY |
---|
196 | // libfac: |
---|
197 | //extern int libfac_interruptflag; |
---|
198 | //libfac_interruptflag=1; |
---|
199 | #endif |
---|
200 | } |
---|
201 | |
---|
202 | void Werror(const char *fmt, ...) |
---|
203 | { |
---|
204 | va_list ap; |
---|
205 | va_start(ap, fmt); |
---|
206 | char *s=(char *)omAlloc(256); |
---|
207 | vsprintf(s, fmt, ap); |
---|
208 | WerrorS(s); |
---|
209 | omFreeSize(s,256); |
---|
210 | va_end(ap); |
---|
211 | } |
---|
212 | |
---|
213 | void WarnS(const char *s) |
---|
214 | { |
---|
215 | #define warn_str "// ** " |
---|
216 | #ifdef HAVE_TCL |
---|
217 | if (tclmode) |
---|
218 | { |
---|
219 | PrintTCLS('W',warn_str); |
---|
220 | PrintTCLS('W',s); |
---|
221 | PrintTCLS('W',"\n"); |
---|
222 | } |
---|
223 | else |
---|
224 | #endif |
---|
225 | if (feWarn) /* ignore warnings if option --no-warn was given */ |
---|
226 | { |
---|
227 | fwrite(warn_str,1,6,stdout); |
---|
228 | fwrite(s,1,strlen(s),stdout); |
---|
229 | fwrite("\n",1,1,stdout); |
---|
230 | fflush(stdout); |
---|
231 | if (feProt&PROT_O) |
---|
232 | { |
---|
233 | fwrite(warn_str,1,6,feProtFile); |
---|
234 | fwrite(s,1,strlen(s),feProtFile); |
---|
235 | fwrite("\n",1,1,feProtFile); |
---|
236 | } |
---|
237 | } |
---|
238 | } |
---|
239 | } /* end extern "C" */ |
---|
240 | |
---|
241 | void Warn(const char *fmt, ...) |
---|
242 | { |
---|
243 | va_list ap; |
---|
244 | va_start(ap, fmt); |
---|
245 | char *s=(char *)omAlloc(256); |
---|
246 | vsprintf(s, fmt, ap); |
---|
247 | WarnS(s); |
---|
248 | omFreeSize(s,256); |
---|
249 | va_end(ap); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | // some routines which redirect the output of print to a string |
---|
254 | static char* sprint = NULL; |
---|
255 | void SPrintStart() |
---|
256 | { |
---|
257 | sprint = omStrDup(""); |
---|
258 | } |
---|
259 | |
---|
260 | static void SPrintS(const char* s) |
---|
261 | { |
---|
262 | omCheckAddr(sprint); |
---|
263 | if ((s == NULL)||(*s == '\0')) return; |
---|
264 | int ls = strlen(s); |
---|
265 | |
---|
266 | char* ns; |
---|
267 | int l = strlen(sprint); |
---|
268 | ns = (char*) omAlloc((l + ls + 1)*sizeof(char)); |
---|
269 | if (l > 0) strcpy(ns, sprint); |
---|
270 | |
---|
271 | strcpy(&(ns[l]), s); |
---|
272 | omFree(sprint); |
---|
273 | sprint = ns; |
---|
274 | omCheckAddr(sprint); |
---|
275 | } |
---|
276 | |
---|
277 | char* SPrintEnd() |
---|
278 | { |
---|
279 | char* ns = sprint; |
---|
280 | sprint = NULL; |
---|
281 | omCheckAddr(ns); |
---|
282 | return ns; |
---|
283 | } |
---|
284 | |
---|
285 | // Print routines |
---|
286 | extern "C" { |
---|
287 | void PrintS(const char *s) |
---|
288 | { |
---|
289 | if (sprint != NULL) |
---|
290 | { |
---|
291 | SPrintS(s); |
---|
292 | return; |
---|
293 | } |
---|
294 | else if (feOut) /* do not print when option --no-out was given */ |
---|
295 | { |
---|
296 | |
---|
297 | #ifdef HAVE_TCL |
---|
298 | if (tclmode) |
---|
299 | { |
---|
300 | PrintTCLS('N',s); |
---|
301 | } |
---|
302 | else |
---|
303 | #endif |
---|
304 | { |
---|
305 | fwrite(s,1,strlen(s),stdout); |
---|
306 | fflush(stdout); |
---|
307 | if (feProt&PROT_O) |
---|
308 | { |
---|
309 | fwrite(s,1,strlen(s),feProtFile); |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | void PrintLn() |
---|
316 | { |
---|
317 | PrintS("\n"); |
---|
318 | } |
---|
319 | |
---|
320 | void Print(const char *fmt, ...) |
---|
321 | { |
---|
322 | if (sprint != NULL) |
---|
323 | { |
---|
324 | va_list ap; |
---|
325 | va_start(ap, fmt); |
---|
326 | omCheckAddr(sprint); |
---|
327 | int ls = strlen(fmt); |
---|
328 | if (fmt != NULL && ls > 0) |
---|
329 | { |
---|
330 | char* ns; |
---|
331 | int l = strlen(sprint); |
---|
332 | ns = (char*) omAlloc(sizeof(char)*(ls + l + 512)); |
---|
333 | if (l > 0) strcpy(ns, sprint); |
---|
334 | |
---|
335 | #ifdef HAVE_VSNPRINTF |
---|
336 | l = vsnprintf(&(ns[l]), ls+511, fmt, ap); |
---|
337 | assume(l != -1); |
---|
338 | #else |
---|
339 | vsprintf(&(ns[l]), fmt, ap); |
---|
340 | #endif |
---|
341 | omCheckAddr(ns); |
---|
342 | omFree(sprint); |
---|
343 | sprint = ns; |
---|
344 | } |
---|
345 | va_end(ap); |
---|
346 | return; |
---|
347 | } |
---|
348 | else if (feOut) |
---|
349 | { |
---|
350 | va_list ap; |
---|
351 | va_start(ap, fmt); |
---|
352 | int l; |
---|
353 | long ls=strlen(fmt); |
---|
354 | char *s=(char *)omAlloc(ls+512); |
---|
355 | #ifdef HAVE_VSNPRINTF |
---|
356 | l = vsnprintf(s, ls+511, fmt, ap); |
---|
357 | if ((l==-1)||(s[l]!='\0')||(l!=strlen(s))) |
---|
358 | { |
---|
359 | printf("Print problem: l=%d, fmt=>>%s<<\n",l,fmt); |
---|
360 | } |
---|
361 | #else |
---|
362 | vsprintf(s, fmt, ap); |
---|
363 | #endif |
---|
364 | PrintS(s); |
---|
365 | omFree(s); |
---|
366 | va_end(ap); |
---|
367 | } |
---|
368 | } |
---|
369 | void PrintNSpaces(const int n) |
---|
370 | { |
---|
371 | int l=n-1; |
---|
372 | while(l>=0) { PrintS(" "); l--; } |
---|
373 | } |
---|
374 | |
---|
375 | /* end extern "C" */ |
---|
376 | } |
---|
377 | |
---|
378 | const char* eati(const char *s, int *i) |
---|
379 | { |
---|
380 | int l=0; |
---|
381 | |
---|
382 | if (*s >= '0' && *s <= '9') |
---|
383 | { |
---|
384 | *i = 0; |
---|
385 | while (*s >= '0' && *s <= '9') |
---|
386 | { |
---|
387 | *i *= 10; |
---|
388 | *i += *s++ - '0'; |
---|
389 | l++; |
---|
390 | if ((l>=MAX_INT_LEN)||((*i) <0)) |
---|
391 | { |
---|
392 | s-=l; |
---|
393 | Werror("`%s` greater than %d(max. integer representation)", |
---|
394 | s,MAX_INT_VAL); |
---|
395 | return s; |
---|
396 | } |
---|
397 | } |
---|
398 | } |
---|
399 | else *i = 1; |
---|
400 | return s; |
---|
401 | } |
---|