source: git/libpolys/reporter/reporter.cc @ 5348866

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