1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: Esingular main file |
---|
6 | */ |
---|
7 | |
---|
8 | #ifdef HAVE_CONFIG_H |
---|
9 | #include "singularconfig.h" |
---|
10 | #endif /* HAVE_CONFIG_H */ |
---|
11 | #include <kernel/mod2.h> |
---|
12 | |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <unistd.h> |
---|
16 | |
---|
17 | #ifdef DecAlpha_OSF1 |
---|
18 | #define _BSD |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <stdlib.h> |
---|
22 | #include <stdarg.h> |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | #ifdef ix86_Win |
---|
26 | #include <windows.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <omalloc/omalloc.h> |
---|
30 | #include <resources/feResource.h> |
---|
31 | #include <Singular/feOpt.h> |
---|
32 | |
---|
33 | #if !defined(TSINGULAR) && !defined(ESINGULAR) |
---|
34 | #define ESINGULAR |
---|
35 | #endif |
---|
36 | |
---|
37 | #ifdef system |
---|
38 | #undef system |
---|
39 | #endif |
---|
40 | |
---|
41 | #ifndef BOOLEAN |
---|
42 | #define BOOLEAN int |
---|
43 | #endif |
---|
44 | #ifndef FALSE |
---|
45 | #define FALSE 0 |
---|
46 | #endif |
---|
47 | #ifndef TRUE |
---|
48 | #define TRUE 1 |
---|
49 | #endif |
---|
50 | #ifndef MAXPATHLEN |
---|
51 | #define MAXPATHLEN 1024 |
---|
52 | #endif |
---|
53 | # define DIR_SEP '/' |
---|
54 | # define DIR_SEPP "/" |
---|
55 | # define UP_DIR ".." |
---|
56 | |
---|
57 | #ifndef ix86_Win |
---|
58 | void error(const char *fmt, ...) |
---|
59 | { |
---|
60 | va_list ap; |
---|
61 | va_start(ap, fmt); |
---|
62 | vfprintf(stderr, fmt, ap); |
---|
63 | } |
---|
64 | #else |
---|
65 | void error(char* fmt, ...) |
---|
66 | { |
---|
67 | char buf[4096]; |
---|
68 | int j =0; |
---|
69 | va_list args; |
---|
70 | va_start(args, fmt); |
---|
71 | j = sprintf(buf, ""); |
---|
72 | j += vsprintf(buf + j,fmt,args); |
---|
73 | j += sprintf(buf + j,"\n"); |
---|
74 | va_end(args); |
---|
75 | MessageBox(NULL, buf, "ESingular.exe", MB_ICONSTOP); |
---|
76 | exit(1); |
---|
77 | } |
---|
78 | #endif |
---|
79 | |
---|
80 | #define Warn error |
---|
81 | #define WarnS error |
---|
82 | #define StringAppend printf |
---|
83 | #define Print error |
---|
84 | |
---|
85 | #define feReportBug(s) fePrintReportBug(s, __FILE__, __LINE__) |
---|
86 | void fePrintReportBug(char* msg, char* file, int line) |
---|
87 | { |
---|
88 | error("YOU HAVE FOUND A BUG IN SINGULAR.\n" |
---|
89 | "Please, email the following output to singular@mathematik.uni-kl.de\n" |
---|
90 | "Bug occured at %s:%d\n" |
---|
91 | "Message: %s\n" |
---|
92 | "Version: " S_UNAME S_VERSION1 __DATE__ __TIME__, |
---|
93 | file, line, msg); |
---|
94 | |
---|
95 | } |
---|
96 | |
---|
97 | void mainUsage() |
---|
98 | { |
---|
99 | error( "Use `%s --help' for a complete list of options\n", feArgv0); |
---|
100 | } |
---|
101 | |
---|
102 | extern char* feResourceDefault(const char id); |
---|
103 | extern char* feResourceDefault(const char* key); |
---|
104 | |
---|
105 | |
---|
106 | int main(int argc, char** argv) |
---|
107 | { |
---|
108 | char* singular = NULL; |
---|
109 | char* emacs = NULL; |
---|
110 | #ifndef TSINGULAR |
---|
111 | char* emacs_dir = NULL; |
---|
112 | char* emacs_load = NULL; |
---|
113 | char cwd[MAXPATHLEN]; |
---|
114 | #endif |
---|
115 | int no_emacs_call = 0; |
---|
116 | |
---|
117 | // parse-cmdline options |
---|
118 | |
---|
119 | feInitResources(argv[0]); |
---|
120 | feResource('S'); |
---|
121 | feResource('b'); |
---|
122 | feResource('r'); |
---|
123 | |
---|
124 | int optc, option_index; |
---|
125 | |
---|
126 | while ((optc = fe_getopt_long(argc, argv, SHORT_OPTS_STRING, |
---|
127 | feOptSpec, &option_index)) |
---|
128 | != EOF) |
---|
129 | { |
---|
130 | switch(optc) |
---|
131 | { |
---|
132 | case 'h': |
---|
133 | extern void feOptHelp(const char* name); |
---|
134 | |
---|
135 | feOptHelp(feArgv0); |
---|
136 | exit(0); |
---|
137 | |
---|
138 | case '?': |
---|
139 | case ':': |
---|
140 | case '\0': |
---|
141 | mainUsage(); |
---|
142 | exit(1); |
---|
143 | |
---|
144 | case LONG_OPTION_RETURN: |
---|
145 | { |
---|
146 | switch(option_index) |
---|
147 | { |
---|
148 | #ifdef TSINGULAR |
---|
149 | case FE_OPT_XTERM: |
---|
150 | emacs = fe_optarg; |
---|
151 | break; |
---|
152 | #else |
---|
153 | case FE_OPT_EMACS: |
---|
154 | emacs = fe_optarg; |
---|
155 | break; |
---|
156 | |
---|
157 | case FE_OPT_EMACS_DIR: |
---|
158 | emacs_dir = fe_optarg; |
---|
159 | break; |
---|
160 | |
---|
161 | case FE_OPT_EMACS_LOAD: |
---|
162 | emacs_load = fe_optarg; |
---|
163 | break; |
---|
164 | #endif |
---|
165 | case FE_OPT_SINGULAR: |
---|
166 | singular = fe_optarg; |
---|
167 | break; |
---|
168 | |
---|
169 | case FE_OPT_NO_CALL: |
---|
170 | no_emacs_call = 1; |
---|
171 | break; |
---|
172 | |
---|
173 | default: |
---|
174 | goto NEXT; |
---|
175 | } |
---|
176 | // delete options from option-list |
---|
177 | if (fe_optind > 2 && *argv[fe_optind-1] != '-' && |
---|
178 | fe_optarg != NULL && feOptSpec[option_index].has_arg) |
---|
179 | { |
---|
180 | argv[fe_optind-2] = NULL; |
---|
181 | } |
---|
182 | argv[fe_optind-1] = NULL; |
---|
183 | } |
---|
184 | } |
---|
185 | NEXT:{} |
---|
186 | } |
---|
187 | |
---|
188 | int i, length = 0; |
---|
189 | char* syscall; |
---|
190 | for (i=1; i<argc; i++) |
---|
191 | { |
---|
192 | if (argv[i] != NULL) length += strlen(argv[i]) + 3; |
---|
193 | } |
---|
194 | |
---|
195 | #ifdef TSINGULAR |
---|
196 | if (emacs == NULL) emacs = feResource('X', 0); |
---|
197 | if (emacs == NULL) |
---|
198 | { |
---|
199 | error( "Error: Can't find emacs xterm program. \n Expected it at %s or %s\n Specify alternative with --xterm=PROGRAM option,\n or set ESINGULAR_EMACS environment variable to the name of the program to use as xterm.\n", |
---|
200 | feResourceDefault('X')); |
---|
201 | mainUsage(); |
---|
202 | exit(1); |
---|
203 | } |
---|
204 | |
---|
205 | if (singular == NULL) singular = feResource("SingularXterm", 0); |
---|
206 | if (singular == NULL) |
---|
207 | { |
---|
208 | error( "Error: Can't find singular executable.\n Expected it at %s\n Specify with --singular option,\n or set TSINGULAR_SINGULAR environment variable.\n", |
---|
209 | feResourceDefault("SingularXterm")); |
---|
210 | mainUsage(); |
---|
211 | exit(1); |
---|
212 | } |
---|
213 | |
---|
214 | #ifdef ix86_Win |
---|
215 | #define EXTRA_XTERM_ARGS "+vb -sl 2000 -fb Courier-bold-12 -tn xterm -cr Red3" |
---|
216 | #else |
---|
217 | #define EXTRA_XTERM_ARGS "" |
---|
218 | #endif |
---|
219 | |
---|
220 | syscall = (char*) omAlloc(strlen(emacs) + |
---|
221 | strlen(singular) + |
---|
222 | length + 300); |
---|
223 | sprintf(syscall, "%s %s -e %s ", emacs, EXTRA_XTERM_ARGS, singular); |
---|
224 | |
---|
225 | for (i=1; i<argc; i++) |
---|
226 | { |
---|
227 | if (argv[i] != NULL) |
---|
228 | { |
---|
229 | strcat(syscall, " "); |
---|
230 | strcat(syscall, argv[i]); |
---|
231 | } |
---|
232 | } |
---|
233 | #else |
---|
234 | // make sure emacs, singular, emacs_dir, emacs_load are set |
---|
235 | if (emacs == NULL) emacs = feResource("xemacs", 0); |
---|
236 | if (emacs == NULL) emacs = feResource("emacs", 0); |
---|
237 | if (emacs == NULL) |
---|
238 | { |
---|
239 | error( "Error: Can't find emacs or xemacs executable. \n Expected it at %s or %s\n Specify alternative with --emacs option,\n or set ESINGULAR_EMACS environment variable.\n", |
---|
240 | feResourceDefault("emacs"), feResourceDefault("xemacs")); |
---|
241 | mainUsage(); |
---|
242 | exit(1); |
---|
243 | } |
---|
244 | |
---|
245 | if (singular == NULL) singular = feResource("SingularEmacs", 0); |
---|
246 | if (singular == NULL) |
---|
247 | { |
---|
248 | error( "Error: Can't find singular executable.\n Expected it at %s\n Specify with --singular option,\n or set ESINGULAR_SINGULAR environment variable.\n", |
---|
249 | feResourceDefault("SingularEmacs")); |
---|
250 | mainUsage(); |
---|
251 | exit(1); |
---|
252 | } |
---|
253 | |
---|
254 | if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0); |
---|
255 | if (emacs_dir == NULL) |
---|
256 | { |
---|
257 | error( "Error: Can't find emacs directory for Singular lisp files. \n Expected it at %s\n Specify with --emacs_dir option,\n or set ESINGULAR_EMACS_DIR environment variable.\n", |
---|
258 | feResourceDefault("EmacsDir")); |
---|
259 | mainUsage(); |
---|
260 | exit(1); |
---|
261 | } |
---|
262 | |
---|
263 | if (emacs_load == NULL) |
---|
264 | { |
---|
265 | // look into env variable |
---|
266 | emacs_load = getenv("ESINGULAR_EMACS_LOAD"); |
---|
267 | if (access(emacs_load, R_OK)) |
---|
268 | { |
---|
269 | // look in home-dir |
---|
270 | emacs_load = getenv("HOME"); |
---|
271 | #ifdef ix86_Win |
---|
272 | if ((emacs_load==NULL)||(!access(emacs_load,X_OK))) |
---|
273 | emacs_load = getenv("SINGHOME"); |
---|
274 | #endif |
---|
275 | sprintf(cwd, "%s/.emacs-singular", emacs_load); |
---|
276 | if (! access(cwd, R_OK)) |
---|
277 | { |
---|
278 | emacs_load = omStrDup(cwd); |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | // try with resources |
---|
283 | emacs_load = feResource("EmacsLoad", 0); |
---|
284 | if (emacs_load == NULL) |
---|
285 | { |
---|
286 | error( "Error: Can't find emacs load file for Singular mode. \n Expected it at %s\n Specify with --emacs_load option,\n or set ESINGULAR_EMACS_LOAD environment variable,\n or put file '.emacs-singular' in your home directory.\n", |
---|
287 | feResourceDefault("EmacsLoad")); |
---|
288 | mainUsage(); |
---|
289 | exit(1); |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | syscall = (char*) omAlloc(strlen(emacs) + |
---|
296 | strlen(singular) + |
---|
297 | strlen(emacs_dir) + |
---|
298 | strlen(emacs_load) + |
---|
299 | length + 300); |
---|
300 | const char* prefix = "--"; |
---|
301 | if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS")) |
---|
302 | prefix = "-"; |
---|
303 | getcwd(cwd, MAXPATHLEN); |
---|
304 | // append / at the end of cwd |
---|
305 | if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/"); |
---|
306 | |
---|
307 | // Note: option -no-init-file should be equivalent to -q. Anyhow, |
---|
308 | // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why. |
---|
309 | sprintf(syscall, "%s %sno-init-file %seval '(progn (setq singular-emacs-home-directory \"%s\") (load-file \"%s\") (singular-other \"%s\" \"%s\" (list ", |
---|
310 | emacs, prefix, prefix, emacs_dir, emacs_load, |
---|
311 | singular, cwd); |
---|
312 | |
---|
313 | |
---|
314 | for (i=1; i<argc; i++) |
---|
315 | { |
---|
316 | if (argv[i] != NULL) |
---|
317 | { |
---|
318 | strcat(syscall, "\""); |
---|
319 | strcat(syscall, argv[i]); |
---|
320 | strcat(syscall, "\" "); |
---|
321 | } |
---|
322 | } |
---|
323 | strcat(syscall, ") \"singular\"))'"); |
---|
324 | #endif |
---|
325 | |
---|
326 | if (no_emacs_call) |
---|
327 | { |
---|
328 | printf("%s\n", syscall); |
---|
329 | } |
---|
330 | else |
---|
331 | { |
---|
332 | if (system(syscall) != 0) |
---|
333 | { |
---|
334 | error( "Error: Execution of\n%s\n", syscall); |
---|
335 | mainUsage(); |
---|
336 | exit(1); |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | |
---|