source: git/Singular/emacs.cc @ ef0124

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