2006-11-23 19:38:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include "ikarus.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2006-11-23 19:48:14 -05:00
|
|
|
#include <gmp.h>
|
2006-12-24 01:43:20 -05:00
|
|
|
#include <signal.h>
|
2006-11-23 19:38:26 -05:00
|
|
|
|
2006-12-24 01:43:20 -05:00
|
|
|
void register_handlers();
|
|
|
|
void register_alt_stack();
|
|
|
|
|
|
|
|
ikpcb* the_pcb;
|
2006-12-07 01:38:04 -05:00
|
|
|
|
2006-12-01 09:23:37 -05:00
|
|
|
/* get_option
|
|
|
|
|
|
|
|
takes pointers to argc and argv and looks for the first
|
|
|
|
option matching opt. If one exists, it removes it from the argv
|
|
|
|
list, updates argc, and returns a pointer to the option value.
|
|
|
|
returns null if option is not found.
|
|
|
|
*/
|
|
|
|
char*
|
|
|
|
get_option(char* opt, int argc, char** argv){
|
|
|
|
int i;
|
|
|
|
for(i=1; i<argc; i++){
|
|
|
|
if(strcmp(opt, argv[i]) == 0){
|
|
|
|
if((i+1) < argc){
|
|
|
|
char* rv = argv[i+1];
|
|
|
|
int j;
|
|
|
|
for(j=i+2; j<argc; j++, i++){
|
|
|
|
argv[i] = argv[j];
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Error: option %s not provided\n", opt);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(strcmp("--", argv[i]) == 0){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-01 11:18:01 -05:00
|
|
|
int
|
|
|
|
file_exists(char* filename){
|
|
|
|
struct stat sb;
|
|
|
|
int s = stat(filename, &sb);
|
|
|
|
return (s == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
copy_fst_path(char* buff, char* x){
|
|
|
|
int i = 0;
|
|
|
|
while(1){
|
|
|
|
char c = x[i];
|
|
|
|
if ((c == 0) || (c == ':')){
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
buff[i] = c;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int global_exe(char* x){
|
|
|
|
while(1){
|
|
|
|
char c = *x;
|
|
|
|
if(c == 0){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if(c == '/'){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
2006-12-01 09:23:37 -05:00
|
|
|
|
2006-11-23 19:38:26 -05:00
|
|
|
int main(int argc, char** argv){
|
2006-12-01 11:18:01 -05:00
|
|
|
char buff[FILENAME_MAX];
|
2006-12-01 09:23:37 -05:00
|
|
|
char* boot_file = get_option("-b", argc, argv);
|
|
|
|
if(boot_file){
|
|
|
|
argc -= 2;
|
2006-12-01 11:18:01 -05:00
|
|
|
}
|
|
|
|
else if(global_exe(argv[0])){
|
|
|
|
/* search path name */
|
|
|
|
char* path = getenv("PATH");
|
|
|
|
if(path == NULL){
|
|
|
|
fprintf(stderr, "unable to locate boot file\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
while(*path){
|
|
|
|
int len = copy_fst_path(buff, path);
|
|
|
|
char* x = buff + len;
|
|
|
|
x = stpcpy(x, "/");
|
|
|
|
x = stpcpy(x, argv[0]);
|
|
|
|
if(file_exists(buff)){
|
|
|
|
x = stpcpy(x, ".boot");
|
|
|
|
boot_file = buff;
|
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(path[len]){
|
|
|
|
path += (len+1);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "unable to locate %s\n", argv[0]);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char* x = buff;
|
2006-12-01 09:23:37 -05:00
|
|
|
x = stpcpy(x, argv[0]);
|
|
|
|
x = stpcpy(x, ".boot");
|
2006-12-01 11:18:01 -05:00
|
|
|
boot_file = buff;
|
2006-12-01 09:23:37 -05:00
|
|
|
}
|
|
|
|
|
2006-12-01 11:18:01 -05:00
|
|
|
|
|
|
|
|
2006-11-23 19:48:14 -05:00
|
|
|
if(sizeof(mp_limb_t) != sizeof(int)){
|
|
|
|
fprintf(stderr, "ERROR: limb size\n");
|
|
|
|
}
|
|
|
|
if(mp_bits_per_limb != (8*sizeof(int))){
|
|
|
|
fprintf(stderr, "ERROR: bits_per_limb=%d\n", mp_bits_per_limb);
|
|
|
|
}
|
2006-11-23 19:38:26 -05:00
|
|
|
ikpcb* pcb = ik_make_pcb();
|
2006-12-24 01:43:20 -05:00
|
|
|
the_pcb = pcb;
|
2006-12-01 09:52:12 -05:00
|
|
|
{ /* set up arg_list */
|
|
|
|
ikp arg_list = null_object;
|
|
|
|
int i = argc-1;
|
|
|
|
while(i > 0){
|
|
|
|
char* s = argv[i];
|
|
|
|
int n = strlen(s);
|
2007-05-19 14:13:51 -04:00
|
|
|
ikp str = ik_alloc(pcb, align(n*string_char_size+disp_string_data+1))
|
|
|
|
+ string_tag;
|
|
|
|
ref(str, off_string_length) = fix(n);
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0; i<n; i++){
|
|
|
|
string_set(str, i, integer_to_char(s[i]));
|
|
|
|
}
|
|
|
|
}
|
2006-12-01 09:52:12 -05:00
|
|
|
ikp p = ik_alloc(pcb, pair_size);
|
2007-05-19 14:13:51 -04:00
|
|
|
ref(p, disp_car) = str;
|
2006-12-01 09:52:12 -05:00
|
|
|
ref(p, disp_cdr) = arg_list;
|
|
|
|
arg_list = p+pair_tag;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
pcb->arg_list = arg_list;
|
|
|
|
}
|
2006-12-24 01:43:20 -05:00
|
|
|
register_handlers();
|
|
|
|
register_alt_stack();
|
2006-12-01 09:23:37 -05:00
|
|
|
ik_fasl_load(pcb, boot_file);
|
2006-11-29 18:45:13 -05:00
|
|
|
/*
|
2006-11-23 19:44:29 -05:00
|
|
|
fprintf(stderr, "collect time: %d.%03d utime, %d.%03d stime (%d collections)\n",
|
2006-11-29 18:45:13 -05:00
|
|
|
pcb->collect_utime.tv_sec,
|
|
|
|
pcb->collect_utime.tv_usec/1000,
|
|
|
|
pcb->collect_stime.tv_sec,
|
|
|
|
pcb->collect_stime.tv_usec/1000,
|
|
|
|
pcb->collection_id );
|
|
|
|
*/
|
2006-11-23 19:38:26 -05:00
|
|
|
ik_delete_pcb(pcb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-24 01:43:20 -05:00
|
|
|
#if 0
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
struct sigaction {
|
|
|
|
union {
|
|
|
|
void (*__sa_handler)(int);
|
|
|
|
void (*__sa_sigaction)(int, struct __siginfo *, void *);
|
|
|
|
} __sigaction_u; /* signal handler */
|
|
|
|
int sa_flags; /* see signal options below */
|
|
|
|
sigset_t sa_mask; /* signal mask to apply */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define sa_handler __sigaction_u.__sa_handler
|
|
|
|
#define sa_sigaction __sigaction_u.__sa_sigaction
|
|
|
|
|
|
|
|
int
|
|
|
|
sigaction(int sig, const struct sigaction * restrict act,
|
|
|
|
struct sigaction * restrict oact);
|
|
|
|
#endif
|
2006-11-23 19:38:26 -05:00
|
|
|
|
2007-08-29 01:45:10 -04:00
|
|
|
void handler(int signo, struct __siginfo* info, void* uap){
|
2006-12-24 03:24:53 -05:00
|
|
|
the_pcb->engine_counter = -1;
|
2006-12-24 01:43:20 -05:00
|
|
|
the_pcb->interrupted = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
register_handlers(){
|
|
|
|
struct sigaction sa;
|
2007-08-28 21:27:37 -04:00
|
|
|
sa.sa_sigaction = handler;
|
2006-12-24 01:43:20 -05:00
|
|
|
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
|
2007-08-29 01:45:10 -04:00
|
|
|
sigemptyset(&sa.sa_mask);
|
2006-12-24 01:43:20 -05:00
|
|
|
int err = sigaction(SIGINT, &sa, 0);
|
|
|
|
if(err){
|
|
|
|
fprintf(stderr, "Sigaction Failed: %s\n", strerror(errno));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2006-11-23 19:38:26 -05:00
|
|
|
|
2006-12-24 01:43:20 -05:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
SYNOPSIS
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
struct sigaltstack {
|
|
|
|
char *ss_sp;
|
|
|
|
int ss_size;
|
|
|
|
int ss_flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
register_alt_stack(){
|
|
|
|
char* stk = ik_mmap(SIGSTKSZ);
|
|
|
|
if(stk == 0){
|
|
|
|
fprintf(stderr, "Cannot maloc an alt stack\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sigaltstack sa;
|
|
|
|
sa.ss_sp = stk;
|
|
|
|
sa.ss_size = SIGSTKSZ;
|
|
|
|
sa.ss_flags = 0;
|
|
|
|
int err = sigaltstack(&sa, 0);
|
|
|
|
if(err){
|
|
|
|
fprintf(stderr, "Cannot set alt stack: %s\n", strerror(errno));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|