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