source: git/Singular/emacs.cc @ 34e5b70

spielwiese
Last change on this file since 34e5b70 was 34e5b70, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* include getopt git-svn-id: file:///usr/local/Singular/svn/trunk@3560 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: emacs.cc,v 1.5 1999-08-30 14:18:48 obachman Exp $ */
5/*
6* ABSTRACT: Esingular main file
7*/
8
9#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12
13#include "mod2.h"
14#include "version.h"
15
16#define ESINGULAR
17
18#define Alloc   malloc
19#define AllocL  malloc
20#define mstrdup strdup
21#define Free    free
22#define FreeL   free
23#define Alloc   malloc
24#define AllocL  malloc
25#ifndef BOOLEAN
26#define BOOLEAN int
27#endif
28#ifndef FALSE
29#define FALSE 0
30#endif
31#ifndef TRUE
32#define TRUE 1
33#endif
34#ifndef MAXPATHLEN
35#define MAXPATHLEN 1024
36#endif
37#  define  DIR_SEP '/'
38#  define  DIR_SEPP "/"
39#  define  UP_DIR ".."
40#define Warn  printf
41#define WarnS printf
42#define StringAppend printf
43#define Print printf
44
45#define feReportBug(s) fePrintReportBug(s, __FILE__, __LINE__)
46void fePrintReportBug(char* msg, char* file, int line)
47{
48  WarnS("YOU HAVE FOUND A BUG IN SINGULAR. ");
49  WarnS("Please, email the following output to singular@mathematik.uni-kl.de ");
50  Warn("Bug occured at %s:%d ", file, line);
51  Warn("Message: %s ", msg);
52  Warn("Version: " S_UNAME S_VERSION1 " (%d) " __DATE__ __TIME__,
53       SINGULAR_VERSION_ID);
54}
55
56void assume_violation(char* file, int line)
57{
58  fprintf(stderr, "Internal assume violation: file %s line %d\n", file, line);
59}
60   
61#include "find_exec.c"
62#include "feResource.cc"
63#include "feCmdLineOptions.cc"
64#include "getopt.c"
65
66int main(int argc, char** argv)
67{
68  char* singular = NULL;
69  char* emacs = NULL;
70  char* emacs_dir = NULL;
71  char* emacs_load = NULL;
72  int no_emacs_call = 0;
73  char cwd[MAXPATHLEN];
74 
75  // parse-cmdline options
76 
77  feInitResources(argv[0]);
78  feResource('S');
79  feResource('b');
80  feResource('r');
81 
82  int optc, option_index;
83 
84  while ((optc = getopt_long(argc, argv, SHORT_OPTS_STRING, 
85                             longopts, &option_index))
86        != EOF)
87  {
88    switch(optc)
89    {
90        case 'h':
91          mainHelp(argv[0]);
92          exit(0);
93         
94        case '?':
95        case ':':
96          mainUsage(argv[0]);
97          exit(1);
98
99        case  LONG_OPTION_RETURN:
100          if (strcmp(longopts[option_index].name, LON_EMACS) == 0)
101          {
102            emacs = optarg;
103          }
104          else if (strcmp(longopts[option_index].name, LON_EMACS_DIR) == 0)
105          {
106            emacs_dir = optarg;
107          }
108          else if (strcmp(longopts[option_index].name, LON_EMACS_LOAD) == 0)
109          {
110            emacs_load = optarg;
111          }
112          else if (strcmp(longopts[option_index].name, LON_SINGULAR) == 0)
113          {
114            singular = optarg;
115          }
116          else if (strcmp(longopts[option_index].name, LON_NO_EMACS_CALL) == 0)
117          {
118            no_emacs_call = 1;
119          }
120          else
121          {
122            break;
123          }
124          // delete options from option-list
125          if (optind > 2 && *argv[optind-1] != '-' && 
126              optarg != NULL && longopts[option_index].has_arg)
127          {
128            argv[optind-2] = NULL;
129          }
130          argv[optind-1] = NULL;
131    }
132  } 
133 
134  // make sure  emacs, singular, emacs_dir, emacs_load are set
135  if (emacs == NULL) emacs = feResource("emacs", 0);
136  if (emacs == NULL)
137  {
138    fprintf(stderr, "Error: Can't find emacs executable. \nExpected it at %s\n. Specify alternative with --emacs option,\n or set EMACS environment variable.\n", 
139            feResourceDefault("emacs"));
140    mainUsage(argv[0]);
141    exit(1);
142  }
143           
144  if (singular == NULL) singular = feResource("SingularEmacs", 0);
145  if (singular == NULL)
146  {
147    fprintf(stderr, "Error: Can't find singular executable.\nExpected it at %s\nSpecify with --singular option,\n  or set SINGULAR_EMACS environment variable.\n", 
148            feResourceDefault("SingularEmacs"));
149    mainUsage(argv[0]);
150    exit(1);
151  }
152   
153  if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0);
154  if (emacs_dir == NULL)
155  {
156    fprintf(stderr, "Error: Can't find emacs directory for Singular lisp files. \nExpected it at %s\nSpecify with --emacs_dir option,\n  or set SINGULAR_EMACS_DIR environment variable.\n", 
157            feResourceDefault("EmacsDir"));
158    mainUsage(argv[0]);
159    exit(1);
160  }
161
162  if (emacs_load == NULL) 
163  {
164    // look into env variable
165    emacs_load = getenv("SINGULAR_EMACS_LOAD");
166    if (access(emacs_load, R_OK))
167    {
168      // look in home-dir
169      emacs_load = getenv("HOME");
170      sprintf(cwd, "%s/.emacs-singular", emacs_load);
171      if (! access(cwd, R_OK))
172      {
173        emacs_load = mstrdup(cwd);
174      }
175      else
176      {
177        // try witrh resources
178        emacs_load = feResource("EmacsLoad", 0);
179        if (emacs_load == NULL)
180        {
181          fprintf(stderr, "Error: Can't find emacs load file for Singular mode. \nExpected it at %s\nSpecify with --emacs_load option,\n or set SINGULAR_EMACS_LOAD environment variable,\n or put file '.emacs-singular' in your home directory.\n", 
182                  feResourceDefault("EmacsLoad")); 
183          mainUsage(argv[0]);
184          exit(1);
185        }
186      }
187    }
188  }
189 
190  // construct options
191  int i, length = 0;
192  for (i=1; i<argc; i++)
193  {
194    if (argv[i] != NULL) length += strlen(argv[i]) + 3;
195  }
196 
197  char* syscall = (char*) AllocL(strlen(emacs) + 
198                                 strlen(singular) + 
199                                 strlen(emacs_dir) + 
200                                 strlen(emacs_load) +
201                                 length + 300);
202  char* prefix = "--";
203  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
204    prefix = "-";
205  getcwd(cwd, MAXPATHLEN);
206
207  // Note: option -no-init-file should be equivalent to -q. Anyhow,
208  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
209  sprintf(syscall, "%s %seval '(setq singular-emacs-home-directory \"%s\")' %sno-init-file %sl %s %seval '(singular-other \"%s\" \"%s\" (list ",
210          emacs, prefix, emacs_dir, prefix, prefix, emacs_load, prefix, 
211          singular, cwd);
212
213  for (i=1; i<argc; i++)
214  {
215    if (argv[i] != NULL)
216    {
217      strcat(syscall, "\"");
218      strcat(syscall, argv[i]);
219      strcat(syscall, "\" ");
220    }
221  }
222  strcat(syscall, ") \"singular\")'");
223 
224  if (no_emacs_call)
225  {
226    printf("%s\n", syscall);
227  }
228  else
229  {
230    if (system(syscall) != 0)
231    {
232      fprintf(stderr, "Error: Executation of\n%s\n");
233      mainUsage(argv[0]);
234      exit(1);
235    }
236  }
237}
238
239
Note: See TracBrowser for help on using the repository browser.