aboutsummaryrefslogtreecommitdiff
path: root/src/driver/utils.c
blob: 92f89eb5a7c457dc1ce7eb15c96b912d85ecc9ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
   Copyright (C) 2008-2009, Gabriel Dos Reis.
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are
   met:

       - Redistributions of source code must retain the above copyright
         notice, this list of conditions and the following disclaimer.

       - Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in
         the documentation and/or other materials provided with the
         distribution.

       - Neither the name of The Numerical Algorithms Group Ltd. nor the
         names of its contributors may be used to endorse or promote products
         derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "openaxiom-c-macros.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "utils.h"
#include <stdio.h>

/* The basename of the file holding the OpenAxiom core executable.  */
#define OPENAXIOM_CORE_EXECUTABLE \
   "AXIOMsys" OPENAXIOM_EXEEXT

/* The basename of the file holding the session manager executable.  */
#define OPENAXIOM_SMAN_EXECUTABLE \
   "sman" OPENAXIOM_EXEEXT

/* Path to the OpenAxiom executable core, relative to
   OPENAXIOM_ROOT_DIRECTORY, or to the system root directory as specified
   on command line.  */
#define OPENAXIOM_CORE_PATH \
   "/bin/" OPENAXIOM_CORE_EXECUTABLE

/* Path to the session manager, relative to OPENAXIOM_ROOT_DIRECTORY,
   or to the system root directory as specified on command line.  */
#define OPENAXIOM_SMAN_PATH \
   "/bin/" OPENAXIOM_SMAN_EXECUTABLE

/* Name of the entry point for Lisp-based OpenAxiom.  */
#define OPENAXIOM_LISP_CORE_ENTRY_POINT \
   "|AxiomCore|::|topLevel|"


/* Return a path to the running system, either as specified on command
   line through --system=, or as specified at configuration time.  */
const char*
openaxiom_get_systemdir(int argc, char* argv[])
{
   int i;

   /* Root directory specified on command line takes precedence
      over location specified at configuration time.  */
   for (i = 1; i < argc; ++i)
      if (strcmp("--", argv[i]) == 0)
         break;
      else if (strncmp("--system=", argv[i], sizeof("--system=") - 1) == 0) {
         return argv[i] + sizeof ("--system=") - 1;
      }

   /* Command line did not change the system location to use.
      Return what was computed at configuration time.  */
   return OPENAXIOM_ROOT_DIRECTORY;
}

/* Return the path to `driver'. */
static const char*
get_driver_name(openaxiom_driver driver)
{
   switch (driver) {
   case openaxiom_sman_driver:
      return OPENAXIOM_SMAN_PATH;

   case openaxiom_script_driver:
   case openaxiom_compiler_driver:
   case openaxiom_core_driver:
      return OPENAXIOM_CORE_PATH;

   default:
      abort();
   }
}


/* Return a path for PROG specified as a relative path to PREFIX.  */
const char*
openaxiom_make_path_for(const char* prefix, openaxiom_driver driver)
{
   const int prefix_length = strlen(prefix);
   const char* prog = get_driver_name(driver);
   char* execpath = (char*) malloc(prefix_length + strlen(prog) + 1);
   strcpy(execpath, prefix);
   strcpy(execpath + prefix_length, prog);
   return execpath;
}

/* Build arguments, if any, to be supplied to the runtime system
   of `driver'.  */
void
openaxiom_build_rts_options(openaxiom_command* command,
                            openaxiom_driver driver)
{
   switch (driver) {
   case openaxiom_sman_driver:
   case openaxiom_unknown_driver:
      break;

   case openaxiom_core_driver:
      break;

   case openaxiom_compiler_driver:
   case openaxiom_script_driver:
      switch (OPENAXIOM_BASE_RTS) {
      case openaxiom_gcl_runtime:
         command->rt_argc = 3;
         command->rt_argv = (char **)
            malloc(command->rt_argc * sizeof (char*));
         command->rt_argv[0] = "-batch";
         command->rt_argv[1] = "-eval";
         command->rt_argv[2] = "(" OPENAXIOM_LISP_CORE_ENTRY_POINT ")";
         break;

      case openaxiom_sbcl_runtime:
         command->rt_argc = 3;
         command->rt_argv = (char **)
            malloc(command->rt_argc * sizeof (char*));
         command->rt_argv[0] = "--noinform";
         command->rt_argv[1] = "--end-runtime-options";
         command->rt_argv[2] = "--noprint";
         command->rt_argv[3] = "--end-toplevel-options";
         break;
         
      default:
         abort();
      }
      break;

   default:
      abort();
   }
}

#if OPENAXIOM_USE_SMAN
#  define OPENAXIOM_DEFAULT_DRIVER openaxiom_sman_driver
#else
#  define OPENAXIOM_DEFAULT_DRIVER openaxiom_core_driver
#endif


static void print_line(const char* line) {
   fputs(line, stdout);
   fputc('\n', stdout);
}

/* Print OpenAxiom version information.  */
static void print_version(void) {
   print_line(PACKAGE_STRING);
}

/* Print OpenAxiom invokation syntax (e.g. options) on standard
   output stream.  */

static void print_usage(void) {
   print_line("Usage: open-axiom [options] [file]");
   print_line("General options:");
   print_line("  --help           Print this information and exit.");
   print_line("  --version        Print OpenAxiom version and exit.");
   print_line("  --script         Execute the file argument as a Spad script.");
   print_line("                   If specified, this option should be last before file argument.");
   print_line("  --compile        Invoke the compiler on the file argument.");
   print_line("                   If specified, this option should be last before file argument.");
   print_line("  --server         Start the Superman as master process.");
   print_line("  --no-server      Do not start Superman as master process.");
   print_line("");
   print_line("Superman options:");
   print_line("  --no-gui         Do not start the Graphics or HyperDoc components.");
   print_line("  --graph          Start the Graphics component.  This option is meaningful");
   print_line("                   only if OpenAxiom was built with graphics support.");
   print_line("  --no-graph       Do not start the Graphics component.");
   print_line("  --hyperdoc       Start the HyperDoc component.  This option is meaningful");
   print_line("                   only if OpenAxiom was built with graphics support.");
   print_line("  --no-hyperdoc    Do not start the HyperDoc component.");
              
   print_line("");
   print_line("Compiler options:");
   print_line("  --optimize=<n>   Set compiler optimization level to <n>, a natural number.");
   print_line("");
   print_line("If invoked without options and without an input file "
              "OpenAxiom will start as an interative program with Superman"
              " as the master process, the majority of uses.  If invoked "
              "with a file as single argument, OpenAxiom assumes the file is a Spad "
              "script and will attempt to execute it as such.");
   print_line("");
   print_line("Submit bug report to " PACKAGE_BUGREPORT);
}

/* Determine driver to be used for executing `command'.  */
openaxiom_driver
openaxiom_preprocess_arguments(openaxiom_command* command,
                               int argc, char** argv)
{
   int i;
   int other = 1;
   int files = 0;
   openaxiom_driver driver = openaxiom_unknown_driver;

   command->root_dir = openaxiom_get_systemdir(argc, argv);
   for (i = 1; i < argc; ++i)
      if(strcmp(argv[i], "--no-server") == 0)
         driver = openaxiom_core_driver;
      else if (strcmp(argv[i], "--server") == 0)
         driver = openaxiom_sman_driver;
      else {
         if (strcmp(argv[i], "--script") == 0)
            driver = openaxiom_script_driver;
         else if(strcmp(argv[i], "--compile") == 0)
            driver = openaxiom_compiler_driver;
         else if (strcmp(argv[i], "--help") == 0) {
            print_usage();
            driver = openaxiom_null_driver;
            break;
         }
         else if (strcmp(argv[i], "--version") == 0) {
            print_version();
            driver = openaxiom_null_driver;
            break;
         }
         else {
            if (argv[i][0] == '-')
               /* Maybe option for the driver.  */
               ;
            else if (strlen(argv[i]) > 0)
               /* Assume a file.  */
               ++files;
            else 
               /* Silly.  */
               continue;
         }
         /* Save it for the core executable.  */
         argv[other++] = argv[i];
      }
   command->core_argc = other;
   command->core_argv = argv;

   if (driver != openaxiom_null_driver) {
      /* If we have a file but not instructed to compiler, assume
         we are asked to interpret a script.  */
      if (files > 0)
         switch (driver) {
         case openaxiom_unknown_driver:
         case openaxiom_sman_driver:
            command->core_argc += 1;
            command->core_argv =
               (char**) malloc((other + 2) * sizeof(char*));
            command->core_argv[0] = argv[0];
            command->core_argv[1] = "--script";
            for (i = 0; i < other; ++i)
               command->core_argv[2 + i] = argv[1 + i];
            driver = openaxiom_script_driver;
            break;
         default:
            /* Driver specified by user.  */
            break;
         }
      else if (driver == openaxiom_unknown_driver)
         driver = OPENAXIOM_DEFAULT_DRIVER;
      command->core_argv[command->core_argc] = NULL;
      
      openaxiom_build_rts_options(command, driver);
   }
   return driver;
}

   


/* Execute the Core Executable as described by `command'.  On
   POSIX systems, this is a non-return function on success.
   See execv().  */
int
openaxiom_execute_core(const openaxiom_command* command,
                       openaxiom_driver driver)
{
   const char* execpath =
      openaxiom_make_path_for(command->root_dir, driver);
#ifdef __WIN32__
   char* command_line;
   int cur = strlen(command->core_argv[0]);
   int command_line_length = 0;
   int i;
   PROCESS_INFORMATION procInfo;
   STARTUPINFO startupInfo = { 0 };
   DWORD status;                /* Exit code for this program masqueraded as
                                   the child created below.  */

   /* How long is the final command line for the MS system? */
   command_line_length += cur;
   for (i = 0; i < command->rt_argc; ++i)
      command_line_length += 1  /* blank char as separator */
	 + 2			/* quotes around every argument.  */
         + strlen(command->rt_argv[i]); /* room for each argument */
   /* Don't forget room for the doubledash string.  */
   command_line_length += sizeof("--") - 1;
   /* And arguments to the actual command.  */
   for (i = 1; i < command->core_argc; ++i)
      command_line_length += 1 + 2 + strlen(command->core_argv[i]);

   /* Now, build the actual command line.  This is done by
      concatenating the arguments into a single string. */
   command_line = (char*) malloc(command_line_length + 1);
   strcpy(command_line, command->core_argv[0]);
   for (i = 0; i < command->rt_argc; ++i) {
      const int arg_length = strlen(command->rt_argv[i]);
      command_line[cur++] = ' ';
      command_line[cur++] = '"';
      strcpy(command_line + cur, command->rt_argv[i]);
      cur += arg_length;
      command_line[cur++] = '"';
   }
   command_line[cur++] = ' ';
   command_line[cur++] = '-'; /*  start arguments to the core executable.  */
   command_line[cur++] = '-';
   for (i = 1; i < command->core_argc; ++i) {
      const int arg_length = strlen(command->core_argv[i]);
      command_line[cur++] = ' ';
      command_line[cur++] = '"';
      strcpy(command_line + cur, command->core_argv[i]);
      cur += arg_length;
      command_line[cur++] = '"';
   }
   command_line[cur] = '\0';	/* The command line is done.  */

   if(CreateProcess(/* lpApplicationName */ execpath,
                    /* lpCommandLine */ command_line,
                    /* lpProcessAttributes */ NULL,
                    /* lpThreadAttributes */ NULL,
                    /* bInheritHandles */ TRUE,
                    /* dwCreationFlags */ 0,
                    /* lpEnvironment */ NULL,
                    /* lpCurrentDirectory */ NULL,
                    /* lpstartupInfo */ &startupInfo,
                    /* lpProcessInformation */ &procInfo) == 0) {
      fprintf(stderr, "error %d\n", GetLastError());
      abort();
   }
   WaitForSingleObject(procInfo.hProcess, INFINITE);
   GetExitCodeProcess(procInfo.hProcess, &status);
   CloseHandle(procInfo.hThread);
   CloseHandle(procInfo.hProcess);
   return status;
                        
#else  /* __WIN32__ */
   int i;
   char** args = (char**)
      malloc(sizeof (char*) * (command->rt_argc + command->core_argc + 2));
   args[0] = command->core_argv[0];
   for (i = 0; i < command->rt_argc; ++i)
      args[i + 1] = command->rt_argv[i];
   if (command->core_argc > 1) {
      /* Insert a doubledash to indicate beginning of arguments.  */
      args[command->rt_argc + 1] = "--";
      for (i = 1; i < command->core_argc; ++i)
         args[command->rt_argc + i + 1] = command->core_argv[i];
      args[command->rt_argc + command->core_argc + 1] = NULL;
   }
   else
      args[command->rt_argc + command->core_argc] = NULL;

   execv(execpath, args);
   perror(strerror(errno));
   return -1;
#endif /* __WIN32__ */
}