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

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