source: git/Singular/emacs.cc @ a3bc95e

spielwiese
Last change on this file since a3bc95e was a3bc95e, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: namespaces ->ns git-svn-id: file:///usr/local/Singular/svn/trunk@5651 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: emacs.cc,v 1.21 2001-10-09 16:35:57 Singular 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#include <string.h>
14
15#ifdef WINNT
16#include <windows.h>
17#endif
18#include "mod2.h"
19#include "version.h"
20#include <omalloc.h>
21
22#if !defined(TSINGULAR) && !defined(ESINGULAR)
23#define ESINGULAR
24#endif
25
26#ifdef system
27#undef system
28#endif
29
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  vfprintf(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
86#include "feResource.cc"
87#include "feOpt.cc"
88
89void mainUsage()
90{
91  error( "Use `%s --help' for a complete list of options\n", feArgv0);
92}
93
94int main(int argc, char** argv)
95{
96  char* singular = NULL;
97  char* emacs = NULL;
98  char* emacs_dir = NULL;
99  char* emacs_load = NULL;
100  int no_emacs_call = 0;
101  char cwd[MAXPATHLEN];
102
103  // parse-cmdline options
104
105  feInitResources(argv[0]);
106  feResource('S');
107  feResource('b');
108  feResource('r');
109
110  int optc, option_index;
111
112  while ((optc = fe_getopt_long(argc, argv, SHORT_OPTS_STRING,
113                                feOptSpec, &option_index))
114        != EOF)
115  {
116    switch(optc)
117    {
118        case 'h':
119          feOptHelp(feArgv0);
120          exit(0);
121
122        case '?':
123        case ':':
124        case '\0':
125          mainUsage();
126          exit(1);
127
128        case  LONG_OPTION_RETURN:
129        {
130          switch(option_index)
131          {
132#ifdef TSINGULAR
133              case FE_OPT_XTERM:
134                emacs = fe_optarg;
135              break;
136#else
137              case FE_OPT_EMACS:
138                emacs = fe_optarg;
139                break;
140
141              case FE_OPT_EMACS_DIR:
142                emacs_dir = fe_optarg;
143                break;
144
145              case FE_OPT_EMACS_LOAD:
146                emacs_load = fe_optarg;
147                break;
148#endif
149              case FE_OPT_SINGULAR:
150                singular = fe_optarg;
151                break;
152
153              case FE_OPT_NO_CALL:
154                no_emacs_call = 1;
155                break;
156
157              default:
158                goto NEXT;
159          }
160          // delete options from option-list
161          if (fe_optind > 2 && *argv[fe_optind-1] != '-' &&
162              fe_optarg != NULL && feOptSpec[option_index].has_arg)
163          {
164            argv[fe_optind-2] = NULL;
165          }
166          argv[fe_optind-1] = NULL;
167        }
168    }
169    NEXT:{}
170  }
171
172  int i, length = 0;
173  char* syscall;
174  for (i=1; i<argc; i++)
175  {
176    if (argv[i] != NULL) length += strlen(argv[i]) + 3;
177  }
178
179#ifdef TSINGULAR
180  if (emacs == NULL) emacs = feResource('X', 0);
181  if (emacs == NULL)
182  {
183    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",
184           feResourceDefault('X'));
185    mainUsage();
186    exit(1);
187  }
188
189  if (singular == NULL) singular = feResource("SingularXterm", 0);
190  if (singular == NULL)
191  {
192    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",
193            feResourceDefault("SingularXterm"));
194    mainUsage();
195    exit(1);
196  }
197
198#ifdef WINNT
199#define EXTRA_XTERM_ARGS "+vb -sl 2000 -fb Courier-bold-13 -tn linux -cr Red3"
200#else
201#define EXTRA_XTERM_ARGS ""
202#endif
203
204  syscall = (char*) omAlloc(strlen(emacs) +
205                                 strlen(singular) +
206                                 length + 300);
207  sprintf(syscall, "%s %s -e %s ", emacs, EXTRA_XTERM_ARGS, singular);
208
209  for (i=1; i<argc; i++)
210  {
211    if (argv[i] != NULL)
212    {
213      strcat(syscall, " ");
214      strcat(syscall, argv[i]);
215    }
216  }
217#else
218  // make sure  emacs, singular, emacs_dir, emacs_load are set
219  if (emacs == NULL) emacs = feResource("xemacs", 0);
220  if (emacs == NULL) emacs = feResource("emacs", 0);
221  if (emacs == NULL)
222  {
223    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",
224            feResourceDefault("emacs"), feResourceDefault("xemacs"));
225    mainUsage();
226    exit(1);
227  }
228
229  if (singular == NULL) singular = feResource("SingularEmacs", 0);
230  if (singular == NULL)
231  {
232    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",
233            feResourceDefault("SingularEmacs"));
234    mainUsage();
235    exit(1);
236  }
237
238  if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0);
239  if (emacs_dir == NULL)
240  {
241    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",
242            feResourceDefault("EmacsDir"));
243    mainUsage();
244    exit(1);
245  }
246
247  if (emacs_load == NULL)
248  {
249    // look into env variable
250    emacs_load = getenv("ESINGULAR_EMACS_LOAD");
251    if (access(emacs_load, R_OK))
252    {
253      // look in home-dir
254      emacs_load = getenv("HOME");
255#ifdef ix86_Win
256      if ((emacs_load==NULL)||(!access(emacs_load,X_OK)))
257        emacs_load = getenv("SINGHOME");
258#endif 
259      sprintf(cwd, "%s/.emacs-singular", emacs_load);
260      if (! access(cwd, R_OK))
261      {
262        emacs_load = omStrDup(cwd);
263      }
264      else
265      {
266        // try with resources
267        emacs_load = feResource("EmacsLoad", 0);
268        if (emacs_load == NULL)
269        {
270          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",
271                  feResourceDefault("EmacsLoad"));
272          mainUsage();
273          exit(1);
274        }
275      }
276    }
277  }
278
279  syscall = (char*) omAlloc(strlen(emacs) +
280                           strlen(singular) +
281                           strlen(emacs_dir) +
282                           strlen(emacs_load) +
283                           length + 300);
284  char* prefix = "--";
285  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
286    prefix = "-";
287  getcwd(cwd, MAXPATHLEN);
288  // append / at the end of cwd
289  if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/");
290
291  // Note: option -no-init-file should be equivalent to -q. Anyhow,
292  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
293  sprintf(syscall, "%s %seval '(setq singular-emacs-home-directory \"%s\")' %sno-init-file %sl %s %seval '(singular-other \"%s\" \"%s\" (list ",
294          emacs, prefix, emacs_dir, prefix, prefix, emacs_load, prefix,
295          singular, cwd);
296
297
298  for (i=1; i<argc; i++)
299  {
300    if (argv[i] != NULL)
301    {
302      strcat(syscall, "\"");
303      strcat(syscall, argv[i]);
304      strcat(syscall, "\" ");
305    }
306  }
307  strcat(syscall, ") \"singular\")'");
308#endif
309
310  if (no_emacs_call)
311  {
312    printf("%s\n", syscall);
313  }
314  else
315  {
316    if (system(syscall) != 0)
317    {
318      error( "Error: Execution of\n%s\n", syscall);
319      mainUsage();
320      exit(1);
321    }
322  }
323}
324
325
Note: See TracBrowser for help on using the repository browser.