source: git/Singular/emacs.cc @ 237b3e4

spielwiese
Last change on this file since 237b3e4 was 599326, checked in by Kai Krüger <krueger@…>, 14 years ago
Anne, Kai, Frank: - changes to #include "..." statements to allow cleaner build structure - affected directories: omalloc, kernel, Singular - not yet done: IntergerProgramming git-svn-id: file:///usr/local/Singular/svn/trunk@13032 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$ */
5/*
6* ABSTRACT: Esingular main file
7*/
8
9#include <stdio.h>
10#include <unistd.h>
11#ifdef DecAlpha_OSF1
12#define _BSD
13#endif
14#include <stdlib.h>
15#include <stdarg.h>
16#include <string.h>
17
18#ifdef ix86_Win
19#include <windows.h>
20#endif
21#include <Singular/mod2.h>
22#include <Singular/version.h>
23#include <omalloc.h>
24
25#if !defined(TSINGULAR) && !defined(ESINGULAR)
26#define ESINGULAR
27#endif
28
29#ifdef system
30#undef system
31#endif
32
33#ifndef BOOLEAN
34#define BOOLEAN int
35#endif
36#ifndef FALSE
37#define FALSE 0
38#endif
39#ifndef TRUE
40#define TRUE 1
41#endif
42#ifndef MAXPATHLEN
43#define MAXPATHLEN 1024
44#endif
45#  define  DIR_SEP '/'
46#  define  DIR_SEPP "/"
47#  define  UP_DIR ".."
48
49#ifndef ix86_Win
50void error(const char *fmt, ...)
51{
52  va_list ap;
53  va_start(ap, fmt);
54  vfprintf(stderr, fmt, ap);
55}
56#else
57void error(char* fmt, ...)
58{
59   char buf[4096];
60   int j =0;
61   va_list args;
62   va_start(args, fmt);
63   j =   sprintf(buf,    "");
64   j += vsprintf(buf + j,fmt,args);
65   j +=  sprintf(buf + j,"\n");
66   va_end(args);
67   MessageBox(NULL, buf, "ESingular.exe", MB_ICONSTOP);
68   exit(1);
69}
70#endif
71
72#define Warn  error
73#define WarnS error
74#define StringAppend printf
75#define Print error
76
77#define feReportBug(s) fePrintReportBug(s, __FILE__, __LINE__)
78void fePrintReportBug(char* msg, char* file, int line)
79{
80  error("YOU HAVE FOUND A BUG IN SINGULAR.\n"
81"Please, email the following output to singular@mathematik.uni-kl.de\n"
82"Bug occured at %s:%d\n"
83"Message: %s\n"
84"Version: " S_UNAME S_VERSION1 " (%lu) " __DATE__ __TIME__,
85        file, line, msg, feVersionId);
86
87}
88
89#include <Singular/../kernel/feResource.cc>
90#include <Singular/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 ix86_Win
202#define EXTRA_XTERM_ARGS "+vb -sl 2000 -fb Courier-bold-12 -tn xterm -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("xemacs", 0);
223  if (emacs == NULL) emacs = feResource("emacs", 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#ifdef ix86_Win
259      if ((emacs_load==NULL)||(!access(emacs_load,X_OK)))
260        emacs_load = getenv("SINGHOME");
261#endif
262      sprintf(cwd, "%s/.emacs-singular", emacs_load);
263      if (! access(cwd, R_OK))
264      {
265        emacs_load = omStrDup(cwd);
266      }
267      else
268      {
269        // try with resources
270        emacs_load = feResource("EmacsLoad", 0);
271        if (emacs_load == NULL)
272        {
273          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",
274                  feResourceDefault("EmacsLoad"));
275          mainUsage();
276          exit(1);
277        }
278      }
279    }
280  }
281
282  syscall = (char*) omAlloc(strlen(emacs) +
283                           strlen(singular) +
284                           strlen(emacs_dir) +
285                           strlen(emacs_load) +
286                           length + 300);
287  const char* prefix = "--";
288  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
289    prefix = "-";
290  getcwd(cwd, MAXPATHLEN);
291  // append / at the end of cwd
292  if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/");
293
294  // Note: option -no-init-file should be equivalent to -q. Anyhow,
295  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
296  sprintf(syscall, "%s %sno-init-file %seval '(progn (setq singular-emacs-home-directory \"%s\") (load-file \"%s\") (singular-other \"%s\" \"%s\" (list ",
297          emacs, prefix, prefix, emacs_dir, emacs_load, 
298          singular, cwd);
299
300
301  for (i=1; i<argc; i++)
302  {
303    if (argv[i] != NULL)
304    {
305      strcat(syscall, "\"");
306      strcat(syscall, argv[i]);
307      strcat(syscall, "\" ");
308    }
309  }
310  strcat(syscall, ") \"singular\"))'");
311#endif
312
313  if (no_emacs_call)
314  {
315    printf("%s\n", syscall);
316  }
317  else
318  {
319    if (system(syscall) != 0)
320    {
321      error( "Error: Execution of\n%s\n", syscall);
322      mainUsage();
323      exit(1);
324    }
325  }
326}
327
328
Note: See TracBrowser for help on using the repository browser.