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 <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.h" |
---|
24 | #include "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 | omMarkAsStaticAddr(feBuffer); |
---|
66 | feBufferLength=more; |
---|
67 | s=feBuffer+l; |
---|
68 | #ifndef BSD_SPRINTF |
---|
69 | feBufferStart=s; |
---|
70 | #endif |
---|
71 | } |
---|
72 | #ifdef BSD_SPRINTF |
---|
73 | vsprintf(s, fmt, ap); |
---|
74 | while (*s!='\0') s++; |
---|
75 | feBufferStart =s; |
---|
76 | #else |
---|
77 | #ifdef HAVE_VSNPRINTF |
---|
78 | vs = vsnprintf(s, feBufferLength - (feBufferStart - feBuffer), fmt, ap); |
---|
79 | if (vs == -1) |
---|
80 | { |
---|
81 | assume(0); |
---|
82 | feBufferStart = feBuffer + feBufferLength -1; |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | feBufferStart += vs; |
---|
87 | } |
---|
88 | #else |
---|
89 | feBufferStart += vsprintf(s, fmt, ap); |
---|
90 | #endif |
---|
91 | #endif |
---|
92 | omCheckAddrSize(feBuffer, feBufferLength); |
---|
93 | va_end(ap); |
---|
94 | return feBuffer; |
---|
95 | } |
---|
96 | |
---|
97 | char * StringAppendS(const char *st) |
---|
98 | { |
---|
99 | if (*st!='\0') |
---|
100 | { |
---|
101 | /* feBufferStart is feBuffer + strlen(feBuffer);*/ |
---|
102 | int l; |
---|
103 | long more; |
---|
104 | int ll=feBufferStart-feBuffer; |
---|
105 | if ((more=ll+2+(l=strlen(st)))>feBufferLength) |
---|
106 | { |
---|
107 | more = ((more + (8*1024-1))/(8*1024))*(8*1024); |
---|
108 | feBuffer=(char *)omReallocSize((void *)feBuffer,feBufferLength, |
---|
109 | more); |
---|
110 | feBufferLength=more; |
---|
111 | feBufferStart=feBuffer+ll; |
---|
112 | } |
---|
113 | strcat(feBufferStart, st); |
---|
114 | feBufferStart +=l; |
---|
115 | } |
---|
116 | return feBuffer; |
---|
117 | } |
---|
118 | |
---|
119 | char * StringSetS(const char *st) |
---|
120 | { |
---|
121 | int l; |
---|
122 | long more; |
---|
123 | if ((l=strlen(st))>feBufferLength) |
---|
124 | { |
---|
125 | more = ((l + (4*1024-1))/(4*1024))*(4*1024); |
---|
126 | feBuffer=(char *)omReallocSize((void *)feBuffer,feBufferLength, |
---|
127 | more); |
---|
128 | feBufferLength=more; |
---|
129 | } |
---|
130 | strcpy(feBuffer,st); |
---|
131 | feBufferStart=feBuffer+l; |
---|
132 | return feBuffer; |
---|
133 | } |
---|
134 | |
---|
135 | #ifdef HAVE_TCL |
---|
136 | extern "C" { |
---|
137 | void PrintTCLS(const char c, const char *s) |
---|
138 | { |
---|
139 | int l=strlen(s); |
---|
140 | if (l>0) PrintTCL(c,l,s); |
---|
141 | } |
---|
142 | } |
---|
143 | #endif |
---|
144 | |
---|
145 | extern "C" { |
---|
146 | void WerrorS(const char *s) |
---|
147 | { |
---|
148 | #ifdef HAVE_MPSR |
---|
149 | if (fe_fgets_stdin==fe_fgets_dummy) |
---|
150 | { |
---|
151 | if (feErrors==NULL) |
---|
152 | { |
---|
153 | feErrors=(char *)omAlloc(256); |
---|
154 | feErrorsLen=256; |
---|
155 | *feErrors = '\0'; |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | if (((int)(strlen((char *)s)+ 20 +strlen(feErrors)))>=feErrorsLen) |
---|
160 | { |
---|
161 | feErrors=(char *)omReallocSize(feErrors,feErrorsLen,feErrorsLen+256); |
---|
162 | feErrorsLen+=256; |
---|
163 | } |
---|
164 | } |
---|
165 | strcat(feErrors, "Singular error: "); |
---|
166 | strcat(feErrors, (char *)s); |
---|
167 | } |
---|
168 | else |
---|
169 | #endif |
---|
170 | { |
---|
171 | #ifdef HAVE_TCL |
---|
172 | if (tclmode) |
---|
173 | { |
---|
174 | PrintTCLS('E',(char *)s); |
---|
175 | PrintTCLS('E',"\n"); |
---|
176 | } |
---|
177 | else |
---|
178 | #endif |
---|
179 | { |
---|
180 | fwrite(" ? ",1,5,stderr); |
---|
181 | fwrite((char *)s,1,strlen((char *)s),stderr); |
---|
182 | fwrite("\n",1,1,stderr); |
---|
183 | fflush(stderr); |
---|
184 | if (feProt&PROT_O) |
---|
185 | { |
---|
186 | fwrite(" ? ",1,5,feProtFile); |
---|
187 | fwrite((char *)s,1,strlen((char *)s),feProtFile); |
---|
188 | fwrite("\n",1,1,feProtFile); |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | errorreported = TRUE; |
---|
193 | #ifdef HAVE_FACTORY |
---|
194 | // libfac: |
---|
195 | //extern int libfac_interruptflag; |
---|
196 | //libfac_interruptflag=1; |
---|
197 | #endif |
---|
198 | } |
---|
199 | |
---|
200 | void Werror(const char *fmt, ...) |
---|
201 | { |
---|
202 | va_list ap; |
---|
203 | va_start(ap, fmt); |
---|
204 | char *s=(char *)omAlloc(256); |
---|
205 | vsprintf(s, fmt, ap); |
---|
206 | WerrorS(s); |
---|
207 | omFreeSize(s,256); |
---|
208 | va_end(ap); |
---|
209 | } |
---|
210 | |
---|
211 | void WarnS(const char *s) |
---|
212 | { |
---|
213 | #define warn_str "// ** " |
---|
214 | #ifdef HAVE_TCL |
---|
215 | if (tclmode) |
---|
216 | { |
---|
217 | PrintTCLS('W',warn_str); |
---|
218 | PrintTCLS('W',s); |
---|
219 | PrintTCLS('W',"\n"); |
---|
220 | } |
---|
221 | else |
---|
222 | #endif |
---|
223 | if (feWarn) /* ignore warnings if option --no-warn was given */ |
---|
224 | { |
---|
225 | fwrite(warn_str,1,6,stdout); |
---|
226 | fwrite(s,1,strlen(s),stdout); |
---|
227 | fwrite("\n",1,1,stdout); |
---|
228 | fflush(stdout); |
---|
229 | if (feProt&PROT_O) |
---|
230 | { |
---|
231 | fwrite(warn_str,1,6,feProtFile); |
---|
232 | fwrite(s,1,strlen(s),feProtFile); |
---|
233 | fwrite("\n",1,1,feProtFile); |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|
237 | } /* end extern "C" */ |
---|
238 | |
---|
239 | void Warn(const char *fmt, ...) |
---|
240 | { |
---|
241 | va_list ap; |
---|
242 | va_start(ap, fmt); |
---|
243 | char *s=(char *)omAlloc(256); |
---|
244 | vsprintf(s, fmt, ap); |
---|
245 | WarnS(s); |
---|
246 | omFreeSize(s,256); |
---|
247 | va_end(ap); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | // some routines which redirect the output of print to a string |
---|
252 | static char* sprint = NULL; |
---|
253 | void SPrintStart() |
---|
254 | { |
---|
255 | sprint = omStrDup(""); |
---|
256 | } |
---|
257 | |
---|
258 | static void SPrintS(const char* s) |
---|
259 | { |
---|
260 | omCheckAddr(sprint); |
---|
261 | if ((s == NULL)||(*s == '\0')) return; |
---|
262 | int ls = strlen(s); |
---|
263 | |
---|
264 | char* ns; |
---|
265 | int l = strlen(sprint); |
---|
266 | ns = (char*) omAlloc((l + ls + 1)*sizeof(char)); |
---|
267 | if (l > 0) strcpy(ns, sprint); |
---|
268 | |
---|
269 | strcpy(&(ns[l]), s); |
---|
270 | omFree(sprint); |
---|
271 | sprint = ns; |
---|
272 | omCheckAddr(sprint); |
---|
273 | } |
---|
274 | |
---|
275 | char* SPrintEnd() |
---|
276 | { |
---|
277 | char* ns = sprint; |
---|
278 | sprint = NULL; |
---|
279 | omCheckAddr(ns); |
---|
280 | return ns; |
---|
281 | } |
---|
282 | |
---|
283 | // Print routines |
---|
284 | extern "C" { |
---|
285 | void PrintS(const char *s) |
---|
286 | { |
---|
287 | if (sprint != NULL) |
---|
288 | { |
---|
289 | SPrintS(s); |
---|
290 | return; |
---|
291 | } |
---|
292 | else if (feOut) /* do not print when option --no-out was given */ |
---|
293 | { |
---|
294 | |
---|
295 | #ifdef HAVE_TCL |
---|
296 | if (tclmode) |
---|
297 | { |
---|
298 | PrintTCLS('N',s); |
---|
299 | } |
---|
300 | else |
---|
301 | #endif |
---|
302 | { |
---|
303 | fwrite(s,1,strlen(s),stdout); |
---|
304 | fflush(stdout); |
---|
305 | if (feProt&PROT_O) |
---|
306 | { |
---|
307 | fwrite(s,1,strlen(s),feProtFile); |
---|
308 | } |
---|
309 | } |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | void PrintLn() |
---|
314 | { |
---|
315 | PrintS("\n"); |
---|
316 | } |
---|
317 | |
---|
318 | void Print(const char *fmt, ...) |
---|
319 | { |
---|
320 | if (sprint != NULL) |
---|
321 | { |
---|
322 | va_list ap; |
---|
323 | va_start(ap, fmt); |
---|
324 | omCheckAddr(sprint); |
---|
325 | int ls = strlen(fmt); |
---|
326 | if (fmt != NULL && ls > 0) |
---|
327 | { |
---|
328 | char* ns; |
---|
329 | int l = strlen(sprint); |
---|
330 | ns = (char*) omAlloc(sizeof(char)*(ls + l + 512)); |
---|
331 | if (l > 0) strcpy(ns, sprint); |
---|
332 | |
---|
333 | #ifdef HAVE_VSNPRINTF |
---|
334 | l = vsnprintf(&(ns[l]), ls+511, fmt, ap); |
---|
335 | assume(l != -1); |
---|
336 | #else |
---|
337 | vsprintf(&(ns[l]), fmt, ap); |
---|
338 | #endif |
---|
339 | omCheckAddr(ns); |
---|
340 | omFree(sprint); |
---|
341 | sprint = ns; |
---|
342 | } |
---|
343 | va_end(ap); |
---|
344 | return; |
---|
345 | } |
---|
346 | else if (feOut) |
---|
347 | { |
---|
348 | va_list ap; |
---|
349 | va_start(ap, fmt); |
---|
350 | int l; |
---|
351 | long ls=strlen(fmt); |
---|
352 | char *s=(char *)omAlloc(ls+512); |
---|
353 | #ifdef HAVE_VSNPRINTF |
---|
354 | l = vsnprintf(s, ls+511, fmt, ap); |
---|
355 | if ((l==-1)||(s[l]!='\0')||(l!=strlen(s))) |
---|
356 | { |
---|
357 | printf("Print problem: l=%d, fmt=>>%s<<\n",l,fmt); |
---|
358 | } |
---|
359 | #else |
---|
360 | vsprintf(s, fmt, ap); |
---|
361 | #endif |
---|
362 | PrintS(s); |
---|
363 | omFree(s); |
---|
364 | va_end(ap); |
---|
365 | } |
---|
366 | } |
---|
367 | void PrintNSpaces(const int n) |
---|
368 | { |
---|
369 | int l=n-1; |
---|
370 | while(l>=0) { PrintS(" "); l--; } |
---|
371 | } |
---|
372 | |
---|
373 | /* end extern "C" */ |
---|
374 | } |
---|