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