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

spielwiese
Last change on this file since 933516 was 933516, checked in by Hans Schoenemann <hannes@…>, 10 years ago
code cleanup: types in lib, comments
  • Property mode set to 100644
File size: 8.4 KB
Line 
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
39static long feBufferLength=0;
40static char * feBuffer=NULL;
41static long feBufferLength_save[8];
42static char * feBuffer_save[8];
43static int feBuffer_cnt=0;
44static char * feBufferStart_save[8];
45
46
47char *  feErrors=NULL;
48int     feErrorsLen=0;
49BOOLEAN feWarn = TRUE;
50BOOLEAN feOut = TRUE;
51
52//void (*WerrorS_callback)(const char *s) = NULL;
53
54const char feNotImplemented[]="not implemented";
55
56int feProt = FALSE;
57FILE*   feProtFile;
58
59static char * feBufferStart;
60  /* only used in StringSet(S)/StringAppend(S)*/
61void 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
80    feBufferStart=s;
81#endif
82  }
83#ifdef BSD_SPRINTF
84  vsprintf(s, fmt, ap);
85  while (*s!='\0') s++;
86  feBufferStart =s;
87#else
88#ifdef HAVE_VSNPRINTF
89  vs = vsnprintf(s, feBufferLength - (feBufferStart - feBuffer), fmt, ap);
90  if (vs == -1)
91  {
92    assume(0);
93    feBufferStart = feBuffer + feBufferLength -1;
94  }
95  else
96  {
97    feBufferStart += vs;
98  }
99#else
100  feBufferStart += vsprintf(s, fmt, ap);
101#endif
102#endif
103  omCheckAddrSize(feBuffer, feBufferLength);
104  va_end(ap);
105}
106
107void 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
128void StringSetS(const char *st)
129{
130  feBuffer_save[feBuffer_cnt]=feBuffer;
131  feBuffer=(char*)omAlloc0(INITIAL_PRINT_BUFFER);
132  feBufferLength_save[feBuffer_cnt]=feBufferLength;
133  feBufferLength=INITIAL_PRINT_BUFFER;
134  feBufferStart_save[feBuffer_cnt]=feBufferStart;
135  feBufferStart=feBuffer;
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);
143    feBuffer=(char *)omReallocSize((ADDRESS)feBuffer,feBufferLength,
144                                                     more);
145    feBufferLength=more;
146  }
147  strcpy(feBuffer,st);
148  feBufferStart=feBuffer+l;
149}
150
151char * StringEndS()
152{
153  char *r=feBuffer;
154  feBuffer_cnt--;
155  assume(feBuffer_cnt >=0);
156  feBuffer=feBuffer_save[feBuffer_cnt];
157  feBufferLength=feBufferLength_save[feBuffer_cnt];
158  feBufferStart=feBufferStart_save[feBuffer_cnt];
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
169extern "C" {
170void 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
178void 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    {
190      feErrors=(char *)omReallocSize(feErrors,feErrorsLen,feErrorsLen+256);
191      feErrorsLen+=256;
192    }
193  }
194  strcat(feErrors, "Singular error: ");
195  strcat(feErrors, (char *)s);
196  errorreported = TRUE;
197}
198
199void 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
210void 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
237void Warn(const char *fmt, ...)
238{
239  va_list ap;
240  va_start(ap, fmt);
241  char *s=(char *)omAlloc(256);
242  vsprintf(s, fmt, ap);
243  WarnS(s);
244  omFreeSize(s,256);
245  va_end(ap);
246}
247
248
249// some routines which redirect the output of print to a string
250static char* sprint = NULL;
251static char* sprint_backup = NULL;
252void SPrintStart()
253{
254  if (sprint!=NULL)
255  {
256    if (sprint_backup!=NULL) WerrorS("internal error: SPrintStart");
257    else sprint_backup=sprint;
258  }
259  sprint = omStrDup("");
260}
261
262static void SPrintS(const char* s)
263{
264  omCheckAddr(sprint);
265  if ((s == NULL)||(*s == '\0')) return;
266  int ls = strlen(s);
267
268  char* ns;
269  int l = strlen(sprint);
270  ns = (char*) omAlloc((l + ls + 1)*sizeof(char));
271  if (l > 0) strcpy(ns, sprint);
272
273  strcpy(&(ns[l]), s);
274  omFree(sprint);
275  sprint = ns;
276  omCheckAddr(sprint);
277}
278
279char* SPrintEnd()
280{
281  char* ns = sprint;
282  sprint = sprint_backup;
283  sprint_backup=NULL;
284  omCheckAddr(ns);
285  return ns;
286}
287
288// Print routines
289extern "C" {
290void PrintS(const char *s)
291{
292  if (sprint != NULL)
293  {
294    SPrintS(s);
295    return;
296  }
297  else if (feOut) /* do not print when option --no-out was given */
298  {
299
300#ifdef HAVE_TCL
301    if (tclmode)
302    {
303      PrintTCLS('N',s);
304    }
305    else
306#endif
307    {
308      fwrite(s,1,strlen(s),stdout);
309      fflush(stdout);
310      if (feProt&SI_PROT_O)
311      {
312        fwrite(s,1,strlen(s),feProtFile);
313      }
314    }
315  }
316}
317
318void PrintLn()
319{
320  PrintS("\n");
321}
322
323void Print(const char *fmt, ...)
324{
325  if (sprint != NULL)
326  {
327    va_list ap;
328    va_start(ap, fmt);
329    omCheckAddr(sprint);
330    int ls = strlen(fmt);
331    if (fmt != NULL && ls > 0)
332    {
333      char* ns;
334      int l = strlen(sprint);
335      ns = (char*) omAlloc(sizeof(char)*(ls + l + 512));
336      if (l > 0)  strcpy(ns, sprint);
337
338#ifdef HAVE_VSNPRINTF
339      l = vsnprintf(&(ns[l]), ls+511, fmt, ap);
340      assume(l != -1);
341#else
342      vsprintf(&(ns[l]), fmt, ap);
343#endif
344      omCheckAddr(ns);
345      omFree(sprint);
346      sprint = ns;
347    }
348    va_end(ap);
349    return;
350  }
351  else if (feOut)
352  {
353    va_list ap;
354    va_start(ap, fmt);
355    int l;
356    long ls=strlen(fmt);
357    char *s=(char *)omAlloc(ls+512);
358#ifdef HAVE_VSNPRINTF
359    l = vsnprintf(s, ls+511, fmt, ap);
360    if ((l==-1)||(s[l]!='\0')||(l!=(int)strlen(s)))
361    {
362      printf("Print problem: l=%d, fmt=>>%s<<\n",l,fmt);
363    }
364#else
365    vsprintf(s, fmt, ap);
366#endif
367    PrintS(s);
368    omFree(s);
369    va_end(ap);
370  }
371}
372void PrintNSpaces(const int n)
373{
374  int l=n-1;
375  while(l>=0) { PrintS(" "); l--; }
376}
377
378/* end extern "C" */
379}
380
381const char* eati(const char *s, int *i)
382{
383  int l=0;
384
385  if    (*s >= '0' && *s <= '9')
386  {
387    *i = 0;
388    while (*s >= '0' && *s <= '9')
389    {
390      *i *= 10;
391      *i += *s++ - '0';
392      l++;
393      if ((l>=MAX_INT_LEN)||((*i) <0))
394      {
395        s-=l;
396        Werror("`%s` greater than %d(max. integer representation)",
397                s,MAX_INT_VAL);
398        return s;
399      }
400    }
401  }
402  else *i = 1;
403  return s;
404}
405
406void feStringAppendResources(int warn)
407{
408  int i = 0;
409  char* r;
410  StringAppend("%-10s:\t%s\n", "argv[0]", feArgv0);
411  while (feResourceConfigs[i].key != NULL)
412  {
413    r = feResource(feResourceConfigs[i].key, warn);
414    StringAppend("%-10s:\t%s\n", feResourceConfigs[i].key,
415                 (r != NULL ? r : ""));
416    i++;
417  }
418}
Note: See TracBrowser for help on using the repository browser.