source: git/Singular/emacs.cc @ 84a6a7

fieker-DuValspielwiese
Last change on this file since 84a6a7 was 2c630c, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: feResource.cc ->kernel git-svn-id: file:///usr/local/Singular/svn/trunk@7259 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.1 KB
RevLine 
[e08f69]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[2c630c]4/* $Id: emacs.cc,v 1.26 2004-07-12 15:01:19 Singular Exp $ */
[e08f69]5/*
6* ABSTRACT: Esingular main file
7*/
8
9#include <stdio.h>
10#include <unistd.h>
[eba506]11#ifdef DecAlpha_OSF1
12#define _BSD
13#endif
[e08f69]14#include <stdlib.h>
[8562bf]15#include <stdarg.h>
[05db082]16#include <string.h>
17
[db8559]18#ifdef ix86_Win
[8562bf]19#include <windows.h>
20#endif
[e08f69]21#include "mod2.h"
22#include "version.h"
[c232af]23#include <omalloc.h>
[e08f69]24
[ef0124]25#if !defined(TSINGULAR) && !defined(ESINGULAR)
[e08f69]26#define ESINGULAR
[ef0124]27#endif
[e08f69]28
[dc0898]29#ifdef system
30#undef system
31#endif
32
[e08f69]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 ".."
[ef0124]48
[db8559]49#ifndef ix86_Win
[ef0124]50void error(const char *fmt, ...)
51{
52  va_list ap;
53  va_start(ap, fmt);
[dd01bf0]54  vfprintf(stderr, fmt, ap);
[ef0124]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
[e08f69]74#define StringAppend printf
[ef0124]75#define Print error
[e08f69]76
77#define feReportBug(s) fePrintReportBug(s, __FILE__, __LINE__)
78void fePrintReportBug(char* msg, char* file, int line)
79{
[4182c6]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__,
[ef0124]85        file, line, msg, feVersionId);
86
[e08f69]87}
88
[2c630c]89#include "../kernel/feResource.cc"
[c06a32]90#include "feOpt.cc"
91
92void mainUsage()
93{
[ef0124]94  error( "Use `%s --help' for a complete list of options\n", feArgv0);
[c06a32]95}
[e08f69]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;
[abcc453]104  char cwd[MAXPATHLEN];
[a3bc95e]105
[e08f69]106  // parse-cmdline options
[a3bc95e]107
[e08f69]108  feInitResources(argv[0]);
109  feResource('S');
110  feResource('b');
111  feResource('r');
[a3bc95e]112
[e08f69]113  int optc, option_index;
[a3bc95e]114
115  while ((optc = fe_getopt_long(argc, argv, SHORT_OPTS_STRING,
[9b4b45]116                                feOptSpec, &option_index))
[e08f69]117        != EOF)
118  {
119    switch(optc)
120    {
121        case 'h':
[c06a32]122          feOptHelp(feArgv0);
[e08f69]123          exit(0);
[a3bc95e]124
[e08f69]125        case '?':
[7272c7]126        case ':':
[c06a32]127        case '\0':
128          mainUsage();
[e08f69]129          exit(1);
130
131        case  LONG_OPTION_RETURN:
[c06a32]132        {
133          switch(option_index)
[7272c7]134          {
[ef0124]135#ifdef TSINGULAR
136              case FE_OPT_XTERM:
137                emacs = fe_optarg;
138              break;
[a3bc95e]139#else
[c06a32]140              case FE_OPT_EMACS:
141                emacs = fe_optarg;
142                break;
[a3bc95e]143
[c06a32]144              case FE_OPT_EMACS_DIR:
145                emacs_dir = fe_optarg;
146                break;
[a3bc95e]147
[c06a32]148              case FE_OPT_EMACS_LOAD:
149                emacs_load = fe_optarg;
150                break;
[a3bc95e]151#endif
[c06a32]152              case FE_OPT_SINGULAR:
153                singular = fe_optarg;
154                break;
155
[ef0124]156              case FE_OPT_NO_CALL:
[c06a32]157                no_emacs_call = 1;
158                break;
[a3bc95e]159
[c06a32]160              default:
161                goto NEXT;
[7272c7]162          }
163          // delete options from option-list
[a3bc95e]164          if (fe_optind > 2 && *argv[fe_optind-1] != '-' &&
[c06a32]165              fe_optarg != NULL && feOptSpec[option_index].has_arg)
[7272c7]166          {
[c06a32]167            argv[fe_optind-2] = NULL;
[7272c7]168          }
[c06a32]169          argv[fe_optind-1] = NULL;
170        }
[e08f69]171    }
[c06a32]172    NEXT:{}
173  }
[ef0124]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  {
[a3bc95e]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",
[ef0124]187           feResourceDefault('X'));
188    mainUsage();
189    exit(1);
190  }
[a3bc95e]191
[ef0124]192  if (singular == NULL) singular = feResource("SingularXterm", 0);
193  if (singular == NULL)
194  {
[a3bc95e]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",
[ef0124]196            feResourceDefault("SingularXterm"));
197    mainUsage();
198    exit(1);
199  }
200
[db8559]201#ifdef ix86_Win
[eba506]202#define EXTRA_XTERM_ARGS "+vb -sl 2000 -fb Courier-bold-12 -tn xterm -cr Red3"
[ef0124]203#else
204#define EXTRA_XTERM_ARGS ""
205#endif
206
[a3bc95e]207  syscall = (char*) omAlloc(strlen(emacs) +
208                                 strlen(singular) +
[ef0124]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  }
[a3bc95e]220#else
[e08f69]221  // make sure  emacs, singular, emacs_dir, emacs_load are set
[a70441f]222  if (emacs == NULL) emacs = feResource("xemacs", 0);
[dd01bf0]223  if (emacs == NULL) emacs = feResource("emacs", 0);
[e08f69]224  if (emacs == NULL)
225  {
[a3bc95e]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",
[ef0124]227            feResourceDefault("emacs"), feResourceDefault("xemacs"));
[c06a32]228    mainUsage();
[e08f69]229    exit(1);
230  }
[a3bc95e]231
[e08f69]232  if (singular == NULL) singular = feResource("SingularEmacs", 0);
233  if (singular == NULL)
234  {
[a3bc95e]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",
[e08f69]236            feResourceDefault("SingularEmacs"));
[c06a32]237    mainUsage();
[e08f69]238    exit(1);
239  }
[a3bc95e]240
[e08f69]241  if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0);
242  if (emacs_dir == NULL)
243  {
[a3bc95e]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",
[e08f69]245            feResourceDefault("EmacsDir"));
[c06a32]246    mainUsage();
[e08f69]247    exit(1);
248  }
249
[a3bc95e]250  if (emacs_load == NULL)
[e08f69]251  {
[abcc453]252    // look into env variable
[9a9127]253    emacs_load = getenv("ESINGULAR_EMACS_LOAD");
[abcc453]254    if (access(emacs_load, R_OK))
255    {
256      // look in home-dir
257      emacs_load = getenv("HOME");
[50cbdc]258#ifdef ix86_Win
259      if ((emacs_load==NULL)||(!access(emacs_load,X_OK)))
260        emacs_load = getenv("SINGHOME");
[db8559]261#endif
[abcc453]262      sprintf(cwd, "%s/.emacs-singular", emacs_load);
263      if (! access(cwd, R_OK))
264      {
[c232af]265        emacs_load = omStrDup(cwd);
[abcc453]266      }
267      else
268      {
[9a9127]269        // try with resources
[abcc453]270        emacs_load = feResource("EmacsLoad", 0);
271        if (emacs_load == NULL)
272        {
[a3bc95e]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"));
[c06a32]275          mainUsage();
[abcc453]276          exit(1);
277        }
278      }
279    }
[e08f69]280  }
[a3bc95e]281
282  syscall = (char*) omAlloc(strlen(emacs) +
283                           strlen(singular) +
284                           strlen(emacs_dir) +
[ef0124]285                           strlen(emacs_load) +
286                           length + 300);
[e08f69]287  char* prefix = "--";
288  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
289    prefix = "-";
290  getcwd(cwd, MAXPATHLEN);
[a70441f]291  // append / at the end of cwd
292  if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/");
[a3bc95e]293
294  // Note: option -no-init-file should be equivalent to -q. Anyhow,
[6e0c129]295  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
[6a8094]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, 
[e08f69]298          singular, cwd);
299
[ef0124]300
[e08f69]301  for (i=1; i<argc; i++)
302  {
[7272c7]303    if (argv[i] != NULL)
[e08f69]304    {
305      strcat(syscall, "\"");
306      strcat(syscall, argv[i]);
307      strcat(syscall, "\" ");
308    }
309  }
[6a8094]310  strcat(syscall, ") \"singular\"))'");
[ef0124]311#endif
312
[eba506]313  if (no_emacs_call)
314  {
[e08f69]315    printf("%s\n", syscall);
[eba506]316  }
317  else
[e08f69]318  {
319    if (system(syscall) != 0)
320    {
[ef0124]321      error( "Error: Execution of\n%s\n", syscall);
[c06a32]322      mainUsage();
[e08f69]323      exit(1);
324    }
325  }
326}
327
328
Note: See TracBrowser for help on using the repository browser.