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