source: git/Singular/emacs.cc @ 6ce030f

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