source: git/Singular/emacs.cc @ e6cdad

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