source: git/Singular/emacs.cc @ f17318

spielwiese
Last change on this file since f17318 was 6909cfb, checked in by Yue Ren <ren@…>, 11 years ago
fix: -Wunused-variable warnings
  • Property mode set to 100644
File size: 8.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: Esingular main file
6*/
7
8#include "config.h"
9#include <kernel/mod2.h>
10
11
12#include <stdio.h>
13#include <unistd.h>
14
15#ifdef DecAlpha_OSF1
16#define _BSD
17#endif
18
19#include <stdlib.h>
20#include <stdarg.h>
21#include <string.h>
22
23#ifdef ix86_Win
24#include <windows.h>
25#endif
26
27#include <omalloc/omalloc.h>
28#include <Singular/version.h>
29#include <findexec/feResource.h>
30#include <Singular/feOpt.h>
31
32#if !defined(TSINGULAR) && !defined(ESINGULAR)
33#define ESINGULAR
34#endif
35
36#ifdef system
37#undef system
38#endif
39
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 ".."
55
56#ifndef ix86_Win
57void error(const char *fmt, ...)
58{
59  va_list ap;
60  va_start(ap, fmt);
61  vfprintf(stderr, fmt, ap);
62}
63#else
64void 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
81#define StringAppend printf
82#define Print error
83
84#define feReportBug(s) fePrintReportBug(s, __FILE__, __LINE__)
85void fePrintReportBug(char* msg, char* file, int line)
86{
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__,
92        file, line, msg, feVersionId);
93
94}
95
96void mainUsage()
97{
98  error( "Use `%s --help' for a complete list of options\n", feArgv0);
99}
100
101extern char* feResourceDefault(const char id);
102extern char* feResourceDefault(const char* key);
103
104
105int main(int argc, char** argv)
106{
107  char* singular = NULL;
108  char* emacs = NULL;
109#ifndef TSINGULAR
110  char* emacs_dir = NULL;
111  char* emacs_load = NULL;
112  char cwd[MAXPATHLEN];
113#endif
114  int no_emacs_call = 0;
115
116  // parse-cmdline options
117
118  feInitResources(argv[0]);
119  feResource('S');
120  feResource('b');
121  feResource('r');
122
123  int optc, option_index;
124
125  while ((optc = fe_getopt_long(argc, argv, SHORT_OPTS_STRING,
126                                feOptSpec, &option_index))
127        != EOF)
128  {
129    switch(optc)
130    {
131      case 'h':
132          extern void feOptHelp(const char* name);
133
134          feOptHelp(feArgv0);
135          exit(0);
136
137        case '?':
138        case ':':
139        case '\0':
140          mainUsage();
141          exit(1);
142
143        case  LONG_OPTION_RETURN:
144        {
145          switch(option_index)
146          {
147#ifdef TSINGULAR
148              case FE_OPT_XTERM:
149                emacs = fe_optarg;
150              break;
151#else
152              case FE_OPT_EMACS:
153                emacs = fe_optarg;
154                break;
155
156              case FE_OPT_EMACS_DIR:
157                emacs_dir = fe_optarg;
158                break;
159
160              case FE_OPT_EMACS_LOAD:
161                emacs_load = fe_optarg;
162                break;
163#endif
164              case FE_OPT_SINGULAR:
165                singular = fe_optarg;
166                break;
167
168              case FE_OPT_NO_CALL:
169                no_emacs_call = 1;
170                break;
171
172              default:
173                goto NEXT;
174          }
175          // delete options from option-list
176          if (fe_optind > 2 && *argv[fe_optind-1] != '-' &&
177              fe_optarg != NULL && feOptSpec[option_index].has_arg)
178          {
179            argv[fe_optind-2] = NULL;
180          }
181          argv[fe_optind-1] = NULL;
182        }
183    }
184    NEXT:{}
185  }
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  {
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",
199           feResourceDefault('X'));
200    mainUsage();
201    exit(1);
202  }
203
204  if (singular == NULL) singular = feResource("SingularXterm", 0);
205  if (singular == NULL)
206  {
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",
208            feResourceDefault("SingularXterm"));
209    mainUsage();
210    exit(1);
211  }
212
213#ifdef ix86_Win
214#define EXTRA_XTERM_ARGS "+vb -sl 2000 -fb Courier-bold-12 -tn xterm -cr Red3"
215#else
216#define EXTRA_XTERM_ARGS ""
217#endif
218
219  syscall = (char*) omAlloc(strlen(emacs) +
220                                 strlen(singular) +
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  }
232#else
233  // make sure  emacs, singular, emacs_dir, emacs_load are set
234  if (emacs == NULL) emacs = feResource("xemacs", 0);
235  if (emacs == NULL) emacs = feResource("emacs", 0);
236  if (emacs == NULL)
237  {
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",
239            feResourceDefault("emacs"), feResourceDefault("xemacs"));
240    mainUsage();
241    exit(1);
242  }
243
244  if (singular == NULL) singular = feResource("SingularEmacs", 0);
245  if (singular == NULL)
246  {
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",
248            feResourceDefault("SingularEmacs"));
249    mainUsage();
250    exit(1);
251  }
252
253  if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0);
254  if (emacs_dir == NULL)
255  {
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",
257            feResourceDefault("EmacsDir"));
258    mainUsage();
259    exit(1);
260  }
261
262  if (emacs_load == NULL)
263  {
264    // look into env variable
265    emacs_load = getenv("ESINGULAR_EMACS_LOAD");
266    if (access(emacs_load, R_OK))
267    {
268      // look in home-dir
269      emacs_load = getenv("HOME");
270#ifdef ix86_Win
271      if ((emacs_load==NULL)||(!access(emacs_load,X_OK)))
272        emacs_load = getenv("SINGHOME");
273#endif
274      sprintf(cwd, "%s/.emacs-singular", emacs_load);
275      if (! access(cwd, R_OK))
276      {
277        emacs_load = omStrDup(cwd);
278      }
279      else
280      {
281        // try with resources
282        emacs_load = feResource("EmacsLoad", 0);
283        if (emacs_load == NULL)
284        {
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"));
287          mainUsage();
288          exit(1);
289        }
290      }
291    }
292  }
293
294  syscall = (char*) omAlloc(strlen(emacs) +
295                           strlen(singular) +
296                           strlen(emacs_dir) +
297                           strlen(emacs_load) +
298                           length + 300);
299  const char* prefix = "--";
300  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
301    prefix = "-";
302  getcwd(cwd, MAXPATHLEN);
303  // append / at the end of cwd
304  if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/");
305
306  // Note: option -no-init-file should be equivalent to -q. Anyhow,
307  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
308  sprintf(syscall, "%s %sno-init-file %seval '(progn (setq singular-emacs-home-directory \"%s\") (load-file \"%s\") (singular-other \"%s\" \"%s\" (list ",
309          emacs, prefix, prefix, emacs_dir, emacs_load,
310          singular, cwd);
311
312
313  for (i=1; i<argc; i++)
314  {
315    if (argv[i] != NULL)
316    {
317      strcat(syscall, "\"");
318      strcat(syscall, argv[i]);
319      strcat(syscall, "\" ");
320    }
321  }
322  strcat(syscall, ") \"singular\"))'");
323#endif
324
325  if (no_emacs_call)
326  {
327    printf("%s\n", syscall);
328  }
329  else
330  {
331    if (system(syscall) != 0)
332    {
333      error( "Error: Execution of\n%s\n", syscall);
334      mainUsage();
335      exit(1);
336    }
337  }
338}
339
340
Note: See TracBrowser for help on using the repository browser.