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