source: git/Singular/emacs.cc @ 49c75e

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