source: git/Singular/emacs.cc @ fdc537

fieker-DuValspielwiese
Last change on this file since fdc537 was 8562bf, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* bug fixes git-svn-id: file:///usr/local/Singular/svn/trunk@4297 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: emacs.cc,v 1.15 2000-05-08 15:35:09 obachman Exp $ */
5/*
6* ABSTRACT: Esingular main file
7*/
8
9#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <stdarg.h>
13#ifdef WINNT
14#include <windows.h>
15#endif
16#include "mod2.h"
17#include "version.h"
18
19#if !defined(TSINGULAR) && !defined(ESINGULAR)
20#define ESINGULAR
21#endif
22
23#define Alloc   malloc
24#define AllocL  malloc
25#define mstrdup strdup
26#define Free    free
27#define FreeL   free
28#define Alloc   malloc
29#define AllocL  malloc
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 ".."
45
46#ifndef WINNT
47void error(const char *fmt, ...)
48{
49  va_list ap;
50  va_start(ap, fmt);
51  fprintf(stderr, fmt, ap);
52}
53#else
54void 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
71#define StringAppend printf
72#define Print error
73
74#define feReportBug(s) fePrintReportBug(s, __FILE__, __LINE__)
75void fePrintReportBug(char* msg, char* file, int line)
76{
77  error("YOU HAVE FOUND A BUG IN SINGULAR.
78Please, email the following output to singular@mathematik.uni-kl.de
79Bug occured at %s:%d
80Message: %s
81Version: " S_UNAME S_VERSION1 " (%lu) " __DATE__ __TIME__,
82        file, line, msg, feVersionId);
83
84}
85
86void assume_violation(char* file, int line)
87{
88  error( "Internal assume violation: file %s line %d\n", file, line);
89}
90   
91extern "C" {
92#include "find_exec.c"
93}
94#include "feResource.cc"
95#include "feOpt.cc"
96
97void mainUsage()
98{
99  error( "Use `%s --help' for a complete list of options\n", feArgv0);
100}
101
102int main(int argc, char** argv)
103{
104  char* singular = NULL;
105  char* emacs = NULL;
106  char* emacs_dir = NULL;
107  char* emacs_load = NULL;
108  int no_emacs_call = 0;
109  char cwd[MAXPATHLEN];
110 
111  // parse-cmdline options
112 
113  feInitResources(argv[0]);
114  feResource('S');
115  feResource('b');
116  feResource('r');
117 
118  int optc, option_index;
119 
120  while ((optc = fe_getopt_long(argc, argv, SHORT_OPTS_STRING, 
121                                feOptSpec, &option_index))
122        != EOF)
123  {
124    switch(optc)
125    {
126        case 'h':
127          feOptHelp(feArgv0);
128          exit(0);
129         
130        case '?':
131        case ':':
132        case '\0':
133          mainUsage();
134          exit(1);
135
136        case  LONG_OPTION_RETURN:
137        {
138          switch(option_index)
139          {
140#ifdef TSINGULAR
141              case FE_OPT_XTERM:
142                emacs = fe_optarg;
143              break;
144#else             
145              case FE_OPT_EMACS:
146                emacs = fe_optarg;
147                break;
148               
149              case FE_OPT_EMACS_DIR:
150                emacs_dir = fe_optarg;
151                break;
152               
153              case FE_OPT_EMACS_LOAD:
154                emacs_load = fe_optarg;
155                break;
156#endif               
157              case FE_OPT_SINGULAR:
158                singular = fe_optarg;
159                break;
160
161              case FE_OPT_NO_CALL:
162                no_emacs_call = 1;
163                break;
164               
165              default:
166                goto NEXT;
167          }
168          // delete options from option-list
169          if (fe_optind > 2 && *argv[fe_optind-1] != '-' && 
170              fe_optarg != NULL && feOptSpec[option_index].has_arg)
171          {
172            argv[fe_optind-2] = NULL;
173          }
174          argv[fe_optind-1] = NULL;
175        }
176    }
177    NEXT:{}
178  }
179
180  int i, length = 0;
181  char* syscall;
182  for (i=1; i<argc; i++)
183  {
184    if (argv[i] != NULL) length += strlen(argv[i]) + 3;
185  }
186
187#ifdef TSINGULAR
188  if (emacs == NULL) emacs = feResource('X', 0);
189  if (emacs == NULL)
190  {
191    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", 
192           feResourceDefault('X'));
193    mainUsage();
194    exit(1);
195  }
196 
197  if (singular == NULL) singular = feResource("SingularXterm", 0);
198  if (singular == NULL)
199  {
200    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", 
201            feResourceDefault("SingularXterm"));
202    mainUsage();
203    exit(1);
204  }
205
206#ifdef WINNT
207#define EXTRA_XTERM_ARGS "+vb -sl 2000 -fb Courier-bold-13 -tn linux -cr Red3"
208#else
209#define EXTRA_XTERM_ARGS ""
210#endif
211
212  syscall = (char*) AllocL(strlen(emacs) + 
213                                 strlen(singular) + 
214                                 length + 300);
215  sprintf(syscall, "%s %s -e %s ", emacs, EXTRA_XTERM_ARGS, singular);
216
217  for (i=1; i<argc; i++)
218  {
219    if (argv[i] != NULL)
220    {
221      strcat(syscall, " ");
222      strcat(syscall, argv[i]);
223    }
224  }
225#else 
226  // make sure  emacs, singular, emacs_dir, emacs_load are set
227  if (emacs == NULL) emacs = feResource("emacs", 0);
228  if (emacs == NULL) emacs = feResource("xemacs", 0);
229  if (emacs == NULL)
230  {
231    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", 
232            feResourceDefault("emacs"), feResourceDefault("xemacs"));
233    mainUsage();
234    exit(1);
235  }
236           
237  if (singular == NULL) singular = feResource("SingularEmacs", 0);
238  if (singular == NULL)
239  {
240    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", 
241            feResourceDefault("SingularEmacs"));
242    mainUsage();
243    exit(1);
244  }
245   
246  if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0);
247  if (emacs_dir == NULL)
248  {
249    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", 
250            feResourceDefault("EmacsDir"));
251    mainUsage();
252    exit(1);
253  }
254
255  if (emacs_load == NULL) 
256  {
257    // look into env variable
258    emacs_load = getenv("ESINGULAR_EMACS_LOAD");
259    if (access(emacs_load, R_OK))
260    {
261      // look in home-dir
262      emacs_load = getenv("HOME");
263      sprintf(cwd, "%s/.emacs-singular", emacs_load);
264      if (! access(cwd, R_OK))
265      {
266        emacs_load = mstrdup(cwd);
267      }
268      else
269      {
270        // try with resources
271        emacs_load = feResource("EmacsLoad", 0);
272        if (emacs_load == NULL)
273        {
274          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", 
275                  feResourceDefault("EmacsLoad")); 
276          mainUsage();
277          exit(1);
278        }
279      }
280    }
281  }
282 
283  syscall = (char*) AllocL(strlen(emacs) + 
284                           strlen(singular) + 
285                           strlen(emacs_dir) + 
286                           strlen(emacs_load) +
287                           length + 300);
288  char* prefix = "--";
289  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
290    prefix = "-";
291  getcwd(cwd, MAXPATHLEN);
292  // append / at the end of cwd
293  if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/");
294   
295  // Note: option -no-init-file should be equivalent to -q. Anyhow,
296  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
297  sprintf(syscall, "%s %seval '(setq singular-emacs-home-directory \"%s\")' %sno-init-file %sl %s %seval '(singular-other \"%s\" \"%s\" (list ",
298          emacs, prefix, emacs_dir, prefix, prefix, emacs_load, prefix, 
299          singular, cwd);
300
301
302  for (i=1; i<argc; i++)
303  {
304    if (argv[i] != NULL)
305    {
306      strcat(syscall, "\"");
307      strcat(syscall, argv[i]);
308      strcat(syscall, "\" ");
309    }
310  }
311  strcat(syscall, ") \"singular\")'");
312#endif
313
314  if (no_emacs_call)
315  {
316    printf("%s\n", syscall);
317  }
318  else
319  {
320    if (system(syscall) != 0)
321    {
322      error( "Error: Execution of\n%s\n", syscall);
323      mainUsage();
324      exit(1);
325    }
326  }
327}
328
329
Note: See TracBrowser for help on using the repository browser.