source: git/Singular/emacs.cc @ 6ce030f

spielwiese
Last change on this file since 6ce030f was f323dd1, checked in by Hans Schoenemann <hannes@…>, 12 years ago
chg: move feResource to findexec lib
  • 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  char* emacs_dir = NULL;
110  char* emacs_load = NULL;
111  int no_emacs_call = 0;
112  char cwd[MAXPATHLEN];
113
114  // parse-cmdline options
115
116  feInitResources(argv[0]);
117  feResource('S');
118  feResource('b');
119  feResource('r');
120
121  int optc, option_index;
122
123  while ((optc = fe_getopt_long(argc, argv, SHORT_OPTS_STRING,
124                                feOptSpec, &option_index))
125        != EOF)
126  {
127    switch(optc)
128    {
129      case 'h':
130          extern void feOptHelp(const char* name);
131
132          feOptHelp(feArgv0);
133          exit(0);
134
135        case '?':
136        case ':':
137        case '\0':
138          mainUsage();
139          exit(1);
140
141        case  LONG_OPTION_RETURN:
142        {
143          switch(option_index)
144          {
145#ifdef TSINGULAR
146              case FE_OPT_XTERM:
147                emacs = fe_optarg;
148              break;
149#else
150              case FE_OPT_EMACS:
151                emacs = fe_optarg;
152                break;
153
154              case FE_OPT_EMACS_DIR:
155                emacs_dir = fe_optarg;
156                break;
157
158              case FE_OPT_EMACS_LOAD:
159                emacs_load = fe_optarg;
160                break;
161#endif
162              case FE_OPT_SINGULAR:
163                singular = fe_optarg;
164                break;
165
166              case FE_OPT_NO_CALL:
167                no_emacs_call = 1;
168                break;
169
170              default:
171                goto NEXT;
172          }
173          // delete options from option-list
174          if (fe_optind > 2 && *argv[fe_optind-1] != '-' &&
175              fe_optarg != NULL && feOptSpec[option_index].has_arg)
176          {
177            argv[fe_optind-2] = NULL;
178          }
179          argv[fe_optind-1] = NULL;
180        }
181    }
182    NEXT:{}
183  }
184
185  int i, length = 0;
186  char* syscall;
187  for (i=1; i<argc; i++)
188  {
189    if (argv[i] != NULL) length += strlen(argv[i]) + 3;
190  }
191
192#ifdef TSINGULAR
193  if (emacs == NULL) emacs = feResource('X', 0);
194  if (emacs == NULL)
195  {
196    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",
197           feResourceDefault('X'));
198    mainUsage();
199    exit(1);
200  }
201
202  if (singular == NULL) singular = feResource("SingularXterm", 0);
203  if (singular == NULL)
204  {
205    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",
206            feResourceDefault("SingularXterm"));
207    mainUsage();
208    exit(1);
209  }
210
211#ifdef ix86_Win
212#define EXTRA_XTERM_ARGS "+vb -sl 2000 -fb Courier-bold-12 -tn xterm -cr Red3"
213#else
214#define EXTRA_XTERM_ARGS ""
215#endif
216
217  syscall = (char*) omAlloc(strlen(emacs) +
218                                 strlen(singular) +
219                                 length + 300);
220  sprintf(syscall, "%s %s -e %s ", emacs, EXTRA_XTERM_ARGS, singular);
221
222  for (i=1; i<argc; i++)
223  {
224    if (argv[i] != NULL)
225    {
226      strcat(syscall, " ");
227      strcat(syscall, argv[i]);
228    }
229  }
230#else
231  // make sure  emacs, singular, emacs_dir, emacs_load are set
232  if (emacs == NULL) emacs = feResource("xemacs", 0);
233  if (emacs == NULL) emacs = feResource("emacs", 0);
234  if (emacs == NULL)
235  {
236    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",
237            feResourceDefault("emacs"), feResourceDefault("xemacs"));
238    mainUsage();
239    exit(1);
240  }
241
242  if (singular == NULL) singular = feResource("SingularEmacs", 0);
243  if (singular == NULL)
244  {
245    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",
246            feResourceDefault("SingularEmacs"));
247    mainUsage();
248    exit(1);
249  }
250
251  if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0);
252  if (emacs_dir == NULL)
253  {
254    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",
255            feResourceDefault("EmacsDir"));
256    mainUsage();
257    exit(1);
258  }
259
260  if (emacs_load == NULL)
261  {
262    // look into env variable
263    emacs_load = getenv("ESINGULAR_EMACS_LOAD");
264    if (access(emacs_load, R_OK))
265    {
266      // look in home-dir
267      emacs_load = getenv("HOME");
268#ifdef ix86_Win
269      if ((emacs_load==NULL)||(!access(emacs_load,X_OK)))
270        emacs_load = getenv("SINGHOME");
271#endif
272      sprintf(cwd, "%s/.emacs-singular", emacs_load);
273      if (! access(cwd, R_OK))
274      {
275        emacs_load = omStrDup(cwd);
276      }
277      else
278      {
279        // try with resources
280        emacs_load = feResource("EmacsLoad", 0);
281        if (emacs_load == NULL)
282        {
283          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",
284                  feResourceDefault("EmacsLoad"));
285          mainUsage();
286          exit(1);
287        }
288      }
289    }
290  }
291
292  syscall = (char*) omAlloc(strlen(emacs) +
293                           strlen(singular) +
294                           strlen(emacs_dir) +
295                           strlen(emacs_load) +
296                           length + 300);
297  const char* prefix = "--";
298  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
299    prefix = "-";
300  getcwd(cwd, MAXPATHLEN);
301  // append / at the end of cwd
302  if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/");
303
304  // Note: option -no-init-file should be equivalent to -q. Anyhow,
305  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
306  sprintf(syscall, "%s %sno-init-file %seval '(progn (setq singular-emacs-home-directory \"%s\") (load-file \"%s\") (singular-other \"%s\" \"%s\" (list ",
307          emacs, prefix, prefix, emacs_dir, emacs_load,
308          singular, cwd);
309
310
311  for (i=1; i<argc; i++)
312  {
313    if (argv[i] != NULL)
314    {
315      strcat(syscall, "\"");
316      strcat(syscall, argv[i]);
317      strcat(syscall, "\" ");
318    }
319  }
320  strcat(syscall, ") \"singular\"))'");
321#endif
322
323  if (no_emacs_call)
324  {
325    printf("%s\n", syscall);
326  }
327  else
328  {
329    if (system(syscall) != 0)
330    {
331      error( "Error: Execution of\n%s\n", syscall);
332      mainUsage();
333      exit(1);
334    }
335  }
336}
337
338
Note: See TracBrowser for help on using the repository browser.