source: git/Singular/emacs.cc @ a70441f

spielwiese
Last change on this file since a70441f was a70441f, checked in by Olaf Bachmann <obachman@…>, 24 years ago
Windows and gcc 2.95 porting git-svn-id: file:///usr/local/Singular/svn/trunk@4273 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: emacs.cc,v 1.12 2000-04-27 10:07:05 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 " (%lu) " __DATE__ __TIME__,
53       feVersionId);
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   
61extern "C" {
62#include "find_exec.c"
63}
64#include "feResource.cc"
65#include "feOpt.cc"
66
67void mainUsage()
68{
69  fprintf(stderr, "Use `%s --help' for a complete list of options\n", feArgv0);
70}
71
72int main(int argc, char** argv)
73{
74  char* singular = NULL;
75  char* emacs = NULL;
76  char* emacs_dir = NULL;
77  char* emacs_load = NULL;
78  int no_emacs_call = 0;
79  char cwd[MAXPATHLEN];
80 
81  // parse-cmdline options
82 
83  feInitResources(argv[0]);
84  feResource('S');
85  feResource('b');
86  feResource('r');
87 
88  int optc, option_index;
89 
90  while ((optc = fe_getopt_long(argc, argv, SHORT_OPTS_STRING, 
91                                feOptSpec, &option_index))
92        != EOF)
93  {
94    switch(optc)
95    {
96        case 'h':
97          feOptHelp(feArgv0);
98          exit(0);
99         
100        case '?':
101        case ':':
102        case '\0':
103          mainUsage();
104          exit(1);
105
106        case  LONG_OPTION_RETURN:
107        {
108          switch(option_index)
109          {
110              case FE_OPT_EMACS:
111                emacs = fe_optarg;
112                break;
113               
114              case FE_OPT_EMACS_DIR:
115                emacs_dir = fe_optarg;
116                break;
117               
118              case FE_OPT_EMACS_LOAD:
119                emacs_load = fe_optarg;
120                break;
121               
122              case FE_OPT_SINGULAR:
123                singular = fe_optarg;
124                break;
125
126              case FE_OPT_NO_EMACS_CALL:
127                no_emacs_call = 1;
128                break;
129               
130              default:
131                goto NEXT;
132          }
133          // delete options from option-list
134          if (fe_optind > 2 && *argv[fe_optind-1] != '-' && 
135              fe_optarg != NULL && feOptSpec[option_index].has_arg)
136          {
137            argv[fe_optind-2] = NULL;
138          }
139          argv[fe_optind-1] = NULL;
140        }
141    }
142    NEXT:{}
143  }
144 
145  // make sure  emacs, singular, emacs_dir, emacs_load are set
146  if (emacs == NULL) emacs = feResource("emacs", 0);
147  if (emacs == NULL) emacs = feResource("xemacs", 0);
148  if (emacs == NULL)
149  {
150    fprintf(stderr, "Error: Can't find emacs executable. \n Expected it at %s\n Specify alternative with --emacs option,\n or set ESINGULAR_EMACS environment variable.\n", 
151            feResourceDefault("emacs"));
152    mainUsage();
153    exit(1);
154  }
155           
156  if (singular == NULL) singular = feResource("SingularEmacs", 0);
157  if (singular == NULL)
158  {
159    fprintf(stderr, "Error: Can't find singular executable.\n Expected it at %s\n Specify with --singular option,\n or set ESINGULAR_SINGULAR environment variable.\n", 
160            feResourceDefault("SingularEmacs"));
161    mainUsage();
162    exit(1);
163  }
164   
165  if (emacs_dir == NULL) emacs_dir = feResource("EmacsDir", 0);
166  if (emacs_dir == NULL)
167  {
168    fprintf(stderr, "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", 
169            feResourceDefault("EmacsDir"));
170    mainUsage();
171    exit(1);
172  }
173
174  if (emacs_load == NULL) 
175  {
176    // look into env variable
177    emacs_load = getenv("ESINGULAR_EMACS_LOAD");
178    if (access(emacs_load, R_OK))
179    {
180      // look in home-dir
181      emacs_load = getenv("HOME");
182      sprintf(cwd, "%s/.emacs-singular", emacs_load);
183      if (! access(cwd, R_OK))
184      {
185        emacs_load = mstrdup(cwd);
186      }
187      else
188      {
189        // try with resources
190        emacs_load = feResource("EmacsLoad", 0);
191        if (emacs_load == NULL)
192        {
193          fprintf(stderr, "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", 
194                  feResourceDefault("EmacsLoad")); 
195          mainUsage();
196          exit(1);
197        }
198      }
199    }
200  }
201 
202  // construct options
203  int i, length = 0;
204  for (i=1; i<argc; i++)
205  {
206    if (argv[i] != NULL) length += strlen(argv[i]) + 3;
207  }
208 
209  char* syscall = (char*) AllocL(strlen(emacs) + 
210                                 strlen(singular) + 
211                                 strlen(emacs_dir) + 
212                                 strlen(emacs_load) +
213                                 length + 300);
214  char* prefix = "--";
215  if (strstr(emacs, "xemacs") || strstr(emacs, "Xemacs") || strstr(emacs, "XEMACS"))
216    prefix = "-";
217  getcwd(cwd, MAXPATHLEN);
218  // append / at the end of cwd
219  if (cwd[strlen(cwd)-1] != '/') strcat(cwd, "/");
220   
221  // Note: option -no-init-file should be equivalent to -q. Anyhow,
222  // xemacs-20.4 sometimes crashed on startup when using -q. DonŽt know why.
223  sprintf(syscall, "%s %seval '(setq singular-emacs-home-directory \"%s\")' %sno-init-file %sl %s %seval '(singular-other \"%s\" \"%s\" (list ",
224          emacs, prefix, emacs_dir, prefix, prefix, emacs_load, prefix, 
225          singular, cwd);
226
227  for (i=1; i<argc; i++)
228  {
229    if (argv[i] != NULL)
230    {
231      strcat(syscall, "\"");
232      strcat(syscall, argv[i]);
233      strcat(syscall, "\" ");
234    }
235  }
236  strcat(syscall, ") \"singular\")'");
237 
238  if (no_emacs_call)
239  {
240    printf("%s\n", syscall);
241  }
242  else
243  {
244    if (system(syscall) != 0)
245    {
246      fprintf(stderr, "Error: Execution of\n%s\n", syscall);
247      mainUsage();
248      exit(1);
249    }
250  }
251}
252
253
Note: See TracBrowser for help on using the repository browser.