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