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