source: git/Singular/emacs.cc @ c232af

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