1 | #ifndef FEBASE_H |
---|
2 | #define FEBASE_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id$ */ |
---|
7 | /* |
---|
8 | * ABSTRACT: basic i/o |
---|
9 | */ |
---|
10 | #include <stdio.h> |
---|
11 | #include <string.h> |
---|
12 | #include "structs.h" |
---|
13 | |
---|
14 | /* define DIR_SEPP, etc */ |
---|
15 | /* unix , WINNT */ |
---|
16 | # define DIR_SEP '/' |
---|
17 | # define DIR_SEPP "/" |
---|
18 | # define UP_DIR ".." |
---|
19 | // this might still get reset by feInitResources |
---|
20 | extern char fePathSep; |
---|
21 | |
---|
22 | /* define MAXPATHLEN */ |
---|
23 | #ifdef HAVE_SYS_PARAM_H |
---|
24 | #include <sys/param.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | /* the are versions of limits.h with incorrect values (IRIX_6) |
---|
28 | * let's include our own */ |
---|
29 | |
---|
30 | #include <mylimits.h> |
---|
31 | |
---|
32 | /* OSF/1 and AIX_4 are missing the header for setenv, but the proc exists */ |
---|
33 | #if defined(DecAlpha_OSF1) || defined(AIX_4) |
---|
34 | #ifdef __cplusplus |
---|
35 | extern "C" |
---|
36 | { |
---|
37 | #endif |
---|
38 | int setenv(const char *name, const char *value, int overwrite); |
---|
39 | #ifdef __cplusplus |
---|
40 | } |
---|
41 | #endif |
---|
42 | #endif |
---|
43 | |
---|
44 | #ifndef MAXPATHLEN |
---|
45 | #define MAXPATHLEN 1024 |
---|
46 | #endif |
---|
47 | |
---|
48 | #ifdef HAVE_LIBREADLINE |
---|
49 | #define HAVE_READLINE 1 |
---|
50 | #endif |
---|
51 | |
---|
52 | #ifdef HAVE_DYN_RL |
---|
53 | #undef HAVE_READLINE |
---|
54 | #define HAVE_FEREAD 1 |
---|
55 | #elif ! defined(HAVE_READLINE) |
---|
56 | #ifndef ix86_Win |
---|
57 | #define HAVE_FEREAD 1 |
---|
58 | #endif |
---|
59 | #endif |
---|
60 | #ifdef ix86_Linux |
---|
61 | // FEREAD stuff does not work with glibc2 |
---|
62 | #undef HAVE_FEREAD |
---|
63 | #endif |
---|
64 | |
---|
65 | /* |
---|
66 | // These are our versions of fopen and fread They are very similar to |
---|
67 | // the usual fopen and fread, except that on reading, they always |
---|
68 | // convert "\r\n" into " \n" and "\r" into "\n". |
---|
69 | // |
---|
70 | // IMPORTANT: do only use myfopen and myfread when reading text, |
---|
71 | // do never use fopen and fread |
---|
72 | */ |
---|
73 | #ifdef ix86_Win |
---|
74 | FILE *myfopen(const char *path, const char *mode); |
---|
75 | #else |
---|
76 | #define myfopen fopen |
---|
77 | #endif |
---|
78 | size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream); |
---|
79 | |
---|
80 | enum noeof_t |
---|
81 | { |
---|
82 | noeof_brace = 1, |
---|
83 | noeof_asstring, |
---|
84 | noeof_block, |
---|
85 | noeof_bracket, |
---|
86 | noeof_comment, |
---|
87 | noeof_procname, |
---|
88 | noeof_string |
---|
89 | }; /* for scanner.l */ |
---|
90 | |
---|
91 | extern char* feErrors; |
---|
92 | extern char* feArgv0; |
---|
93 | extern int feErrorsLen; |
---|
94 | extern FILE* feProtFile; |
---|
95 | extern FILE* feFilePending; /*temp. storage for grammar.y */ |
---|
96 | extern char fe_promptstr[]; |
---|
97 | extern int si_echo, printlevel; |
---|
98 | extern int pagelength, colmax; |
---|
99 | extern int yy_blocklineno; |
---|
100 | extern int yy_noeof; |
---|
101 | extern char prompt_char; |
---|
102 | extern const char feNotImplemented[]; |
---|
103 | #ifdef HAVE_TCL |
---|
104 | extern BOOLEAN tclmode; |
---|
105 | #endif |
---|
106 | extern BOOLEAN errorreported; |
---|
107 | extern int feProt; |
---|
108 | extern BOOLEAN feWarn; |
---|
109 | extern BOOLEAN feOut; |
---|
110 | extern int traceit ; |
---|
111 | #define TRACE_SHOW_PROC 1 |
---|
112 | #define TRACE_SHOW_LINENO 2 |
---|
113 | #define TRACE_SHOW_LINE 4 |
---|
114 | #define TRACE_SHOW_RINGS 8 |
---|
115 | #define TRACE_SHOW_LINE1 16 |
---|
116 | #define TRACE_BREAKPOINT 32 |
---|
117 | #define TRACE_TMP_BREAKPOINT 64 |
---|
118 | extern int myynest; |
---|
119 | extern int yylineno; |
---|
120 | extern char my_yylinebuf[80]; |
---|
121 | |
---|
122 | #define PROT_NONE 0 |
---|
123 | #define PROT_I 1 |
---|
124 | #define PROT_O 2 |
---|
125 | #define PROT_IO 3 |
---|
126 | |
---|
127 | /* the C-part: */ |
---|
128 | #define mflush() fflush(stdout) |
---|
129 | |
---|
130 | #ifdef __cplusplus |
---|
131 | extern "C" { |
---|
132 | #endif |
---|
133 | |
---|
134 | void Werror(const char *fmt, ...) __attribute__((format(printf,1,2))); |
---|
135 | void WerrorS(const char *s); |
---|
136 | void WarnS(const char *s); |
---|
137 | void Print(const char* fmt, ...) __attribute__((format(printf,1,2))); |
---|
138 | /* Print should not produce more than strlen(fmt)+510 characters! */ |
---|
139 | |
---|
140 | void PrintNSpaces(const int n); |
---|
141 | void PrintLn(); |
---|
142 | #ifdef HAVE_TCL |
---|
143 | void PrintTCLS(const char c, const char * s); |
---|
144 | #else |
---|
145 | #define PrintTCLS(A,B) Print("TCL-ErrS:%s",B) |
---|
146 | #endif |
---|
147 | void PrintS(const char* s); |
---|
148 | char* feGetResource(const char id); |
---|
149 | |
---|
150 | #ifdef HAVE_FEREAD |
---|
151 | extern BOOLEAN fe_is_raw_tty; |
---|
152 | void fe_temp_reset (void); |
---|
153 | #endif |
---|
154 | |
---|
155 | #ifdef __cplusplus |
---|
156 | } |
---|
157 | /* the C++-part: */ |
---|
158 | |
---|
159 | typedef enum { LANG_NONE, LANG_TOP, LANG_SINGULAR, LANG_C, LANG_MAX} language_defs; |
---|
160 | // LANG_TOP : Toplevel package only |
---|
161 | // LANG_SINGULAR: |
---|
162 | // LANG_C : |
---|
163 | // |
---|
164 | |
---|
165 | class proc_singular |
---|
166 | { |
---|
167 | public: |
---|
168 | long proc_start; // position where proc is starting |
---|
169 | long def_end; // position where proc header is ending |
---|
170 | long help_start; // position where help is starting |
---|
171 | long help_end; // position where help is starting |
---|
172 | long body_start; // position where proc-body is starting |
---|
173 | long body_end; // position where proc-body is ending |
---|
174 | long example_start; // position where example is starting |
---|
175 | long proc_end; // position where proc is ending |
---|
176 | int proc_lineno; |
---|
177 | int body_lineno; |
---|
178 | int example_lineno; |
---|
179 | char *body; |
---|
180 | long help_chksum; |
---|
181 | }; |
---|
182 | |
---|
183 | struct proc_object |
---|
184 | { |
---|
185 | //public: |
---|
186 | BOOLEAN (*function)(leftv res, leftv v); |
---|
187 | }; |
---|
188 | union uprocinfodata |
---|
189 | { |
---|
190 | public: |
---|
191 | proc_singular s; // data of Singular-procedure |
---|
192 | struct proc_object o; // pointer to binary-function |
---|
193 | }; |
---|
194 | |
---|
195 | typedef union uprocinfodata procinfodata; |
---|
196 | |
---|
197 | class procinfo; |
---|
198 | typedef procinfo * procinfov; |
---|
199 | |
---|
200 | class procinfo |
---|
201 | { |
---|
202 | public: |
---|
203 | char *libname; |
---|
204 | char *procname; |
---|
205 | package pack; |
---|
206 | language_defs language; |
---|
207 | short ref; |
---|
208 | char is_static; // if set, proc not accessible for user |
---|
209 | char trace_flag; |
---|
210 | procinfodata data; |
---|
211 | }; |
---|
212 | enum feBufferTypes |
---|
213 | { |
---|
214 | BT_none = 0, // entry level |
---|
215 | BT_break = 1, // while, for |
---|
216 | BT_proc, // proc |
---|
217 | BT_example, // example |
---|
218 | BT_file, // <"file" |
---|
219 | BT_execute, // execute |
---|
220 | BT_if, // if |
---|
221 | BT_else // else |
---|
222 | }; |
---|
223 | |
---|
224 | enum feBufferInputs |
---|
225 | { |
---|
226 | BI_stdin = 1, |
---|
227 | BI_buffer, |
---|
228 | BI_file |
---|
229 | }; |
---|
230 | |
---|
231 | /***************************************************************** |
---|
232 | * |
---|
233 | * Resource management (feResources.cc) |
---|
234 | * |
---|
235 | *****************************************************************/ |
---|
236 | // returns value of Resource as read-only string, or NULL |
---|
237 | // if Resource not found |
---|
238 | // issues warning, if explicitely requested (warn > 0), or |
---|
239 | // if warn < 0 and Resource is gotten for the first time |
---|
240 | // Always quiet if warn == 0 |
---|
241 | char* feResource(const char id, int warn = -1); |
---|
242 | char* feResource(const char* key, int warn = -1); |
---|
243 | // This needs to be called before the first call to feResource |
---|
244 | // Initializes Resources, SearchPath, and extends PATH |
---|
245 | void feInitResources(char* argv0); |
---|
246 | // Re-inits resources, should be called after changing env. variables |
---|
247 | void feReInitResources(); |
---|
248 | // Prints resources into string with StringAppend, etc |
---|
249 | void feStringAppendResources(int warn = -1); |
---|
250 | |
---|
251 | /***************************************************************** |
---|
252 | * |
---|
253 | * help system (fehelp.cc) |
---|
254 | * |
---|
255 | *****************************************************************/ |
---|
256 | // if str != NULL display help for str |
---|
257 | // display general help, otherwise |
---|
258 | void feHelp(char* str = NULL); |
---|
259 | // if browser != NULL or feOpt("browser") != NULL |
---|
260 | // set HelpBrowser to browser |
---|
261 | // otherwise, if browser was already set, leave as is, |
---|
262 | // if not, choose first available browser |
---|
263 | // return string identifying current browser |
---|
264 | // keeps feOpt("browser") up-to-date |
---|
265 | // Optional warn argument is as in feResource |
---|
266 | const char* feHelpBrowser(char* browser = NULL, int warn = -1); |
---|
267 | void feStringAppendBrowsers(int warn = -1); |
---|
268 | |
---|
269 | /***************************************************************** |
---|
270 | * |
---|
271 | * version Id |
---|
272 | * |
---|
273 | *****************************************************************/ |
---|
274 | /* extern unsigned long feVersionId; */ |
---|
275 | |
---|
276 | /***************************************************************** |
---|
277 | * |
---|
278 | * File Stuff |
---|
279 | * |
---|
280 | *****************************************************************/ |
---|
281 | FILE * feFopen(const char *path, const char *mode, char *where=NULL, |
---|
282 | int useWerror=FALSE, int path_only=FALSE); |
---|
283 | |
---|
284 | #ifdef HAVE_TCL |
---|
285 | |
---|
286 | inline void PrintTCL(const char c, int l,const char *s) |
---|
287 | { |
---|
288 | if (s!=NULL) printf("%c:%d:%s",c,l,s); |
---|
289 | else if(l==0) printf("%c:0:",c); |
---|
290 | else printf("%c:1:%c",c,'0'+l); |
---|
291 | fflush(stdout); |
---|
292 | } |
---|
293 | #else |
---|
294 | #define PrintTCL(A,B,C) Print("TCL-Err:%s",C) |
---|
295 | #endif /* HAVE_TCL */ |
---|
296 | |
---|
297 | char * StringAppend(const char *fmt, ...); |
---|
298 | char * StringAppendS(const char *s); |
---|
299 | char * StringSetS(const char* s); |
---|
300 | const char * VoiceName(); |
---|
301 | void VoiceBackTrack(); |
---|
302 | void Warn(const char *fmt, ...); |
---|
303 | BOOLEAN contBuffer(feBufferTypes typ); |
---|
304 | const char * eati(const char *s, int *i); |
---|
305 | BOOLEAN exitBuffer(feBufferTypes typ); |
---|
306 | BOOLEAN exitVoice(); |
---|
307 | void monitor(void *F, int mode); /* FILE*, int */ |
---|
308 | BOOLEAN newFile(char* fname, FILE *f=NULL); |
---|
309 | void newBuffer(char* s, feBufferTypes t, procinfo *pname = NULL, int start_lineno = 0); |
---|
310 | void * myynewbuffer(); |
---|
311 | void myyoldbuffer(void * oldb); |
---|
312 | |
---|
313 | class Voice |
---|
314 | { |
---|
315 | public: |
---|
316 | Voice * next; |
---|
317 | Voice * prev; |
---|
318 | char * filename; // file name or proc name |
---|
319 | procinfo * pi; // proc info |
---|
320 | void * oldb; // internal scanner buffer |
---|
321 | // for files only: |
---|
322 | FILE * files; // file handle |
---|
323 | // for buffers only: |
---|
324 | char * buffer; // buffer pointer |
---|
325 | long fptr; // current position in buffer |
---|
326 | |
---|
327 | int start_lineno; // lineno, to restore in recursion |
---|
328 | int curr_lineno; // current lineno |
---|
329 | feBufferInputs sw; // BI_stdin: read from STDIN |
---|
330 | // BI_buffer: buffer |
---|
331 | // BI_file: files |
---|
332 | char ifsw; // if-switch: |
---|
333 | /*1 ifsw==0: no if statement, else is invalid |
---|
334 | * ==1: if (0) processed, execute else |
---|
335 | * ==2: if (1) processed, else allowed but not executed |
---|
336 | */ |
---|
337 | feBufferTypes typ; // buffer type: see BT_.. |
---|
338 | |
---|
339 | Voice() { memset(this,0,sizeof(*this));} |
---|
340 | feBufferTypes Typ(); |
---|
341 | void Next(); |
---|
342 | } ; |
---|
343 | |
---|
344 | extern Voice *currentVoice; |
---|
345 | |
---|
346 | Voice * feInitStdin(Voice *pp); |
---|
347 | |
---|
348 | /* feread.cc: */ |
---|
349 | |
---|
350 | /* the interface for reading: */ |
---|
351 | extern char * (*fe_fgets_stdin)(const char *pr,char *s, int size); |
---|
352 | |
---|
353 | #ifdef HAVE_DYN_RL |
---|
354 | char * fe_fgets_stdin_drl(const char *pr,char *s, int size); |
---|
355 | #endif |
---|
356 | |
---|
357 | extern "C" void fe_reset_input_mode(); |
---|
358 | |
---|
359 | extern "C" { |
---|
360 | #ifndef HAVE_ATEXIT |
---|
361 | void fe_reset_fe (int i, void *v); |
---|
362 | #else |
---|
363 | void fe_reset_fe (void); |
---|
364 | #endif |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | /* possible implementations: */ |
---|
369 | #ifdef __cplusplus |
---|
370 | extern "C" |
---|
371 | { |
---|
372 | #endif |
---|
373 | /* readline, linked in: */ |
---|
374 | char * fe_fgets_stdin_rl(const char *pr,char *s, int size); |
---|
375 | |
---|
376 | /* emulated readline: */ |
---|
377 | char * fe_fgets_stdin_emu(const char *pr,char *s, int size); |
---|
378 | |
---|
379 | /* fgets: */ |
---|
380 | char * fe_fgets(const char *pr,char *s, int size); |
---|
381 | |
---|
382 | #ifdef HAVE_TCL |
---|
383 | /* tcl: */ |
---|
384 | char * fe_fgets_tcl(const char *pr,char *s, int size); |
---|
385 | #endif |
---|
386 | |
---|
387 | /* dummy (for batch mode): */ |
---|
388 | char * fe_fgets_dummy(const char *pr,char *s, int size); |
---|
389 | |
---|
390 | #ifdef __cplusplus |
---|
391 | } |
---|
392 | #endif |
---|
393 | #endif /* c++ only */ |
---|
394 | |
---|
395 | /* everything in between calls to these procedures is printed into a string |
---|
396 | * which is returned by SprintEnd() |
---|
397 | */ |
---|
398 | void SPrintStart(); |
---|
399 | char* SPrintEnd(); |
---|
400 | |
---|
401 | |
---|
402 | #endif /* ifndef FEBASE_H */ |
---|