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

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