diff --git a/Makefile b/Makefile index 3456087bea..75e736c14e 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,10 @@ CC = gcc CP = cp BINARY = f +IMAGE = factor.image BUNDLE = Factor.app BUNDLE_BINARY = $(BUNDLE)/Contents/MacOS/Factor +BUNDLE_IMAGE = $(BUNDLE)/Contents/factor.image ifdef DEBUG DEFAULT_CFLAGS = -g @@ -92,7 +94,10 @@ macosx: CFLAGS="$(DEFAULT_CFLAGS)" \ LIBS="$(DEFAULT_LIBS) -framework Cocoa -framework OpenGL" \ MACOSX=y + +macosx.app: $(CP) $(BINARY) $(BUNDLE_BINARY) + $(CP) $(IMAGE) $(BUNDLE_IMAGE) linux linux-x86 linux-amd64: $(MAKE) $(BINARY) \ diff --git a/native/factor.c b/native/factor.c index a2f2a27de9..164e0a9466 100644 --- a/native/factor.c +++ b/native/factor.c @@ -37,9 +37,24 @@ INLINE bool factor_arg(const char* str, const char* arg, CELL* value) return false; } +void usage(void) +{ + printf("Usage: factor [ parameters ... ]\n"); + printf("Runtime options -- n is a number:\n"); + printf(" +Dn Data stack size, kilobytes\n"); + printf(" +Cn Call stack size, kilobytes\n"); + printf(" +Gn Number of generations, must be >= 2\n"); + printf(" +Yn Size of n-1 youngest generations, megabytes\n"); + printf(" +An Size of tenured and semi-spaces, megabytes\n"); + printf(" +Xn Code heap size, megabytes\n"); + printf("Other options are handled by the Factor library.\n"); + printf("See the documentation for details.\n"); + printf("Send bug reports to Slava Pestov .\n"); +} + int main(int argc, char** argv) { - char *image; + char *image = NULL; CELL ds_size = 128; CELL cs_size = 128; CELL generations = 2; @@ -50,21 +65,7 @@ int main(int argc, char** argv) CELL args; CELL i; - if(argc == 1) - { - printf("Usage: factor [ parameters ... ]\n"); - printf("Runtime options -- n is a number:\n"); - printf(" +Dn Data stack size, kilobytes\n"); - printf(" +Cn Call stack size, kilobytes\n"); - printf(" +Gn Number of generations, must be >= 2\n"); - printf(" +Yn Size of n-1 youngest generations, megabytes\n"); - printf(" +An Size of tenured and semi-spaces, megabytes\n"); - printf(" +Xn Code heap size, megabytes\n"); - printf("Other options are handled by the Factor library.\n"); - printf("See the documentation for details.\n"); - printf("Send bug reports to Slava Pestov .\n"); - return 1; - } + early_init(); for(i = 1; i < argc; i++) { @@ -78,11 +79,16 @@ int main(int argc, char** argv) if(strncmp(argv[i],"+",1) == 0) { printf("Unknown option: %s\n",argv[i]); + usage(); return 1; } + + if(strncmp(argv[i],"-",1) != 0 && image == NULL) + image = argv[1]; } - image = argv[1]; + if(image == NULL) + image = default_image_path(); init_factor(image, ds_size * 1024, @@ -94,7 +100,7 @@ int main(int argc, char** argv) literal_size * 1024); args = F; - while(--argc != 1) + while(--argc > 1) { args = cons(tag_object(from_c_string(argv[argc])),args); } diff --git a/native/factor.h b/native/factor.h index 32851b83d3..5e46d5319a 100644 --- a/native/factor.h +++ b/native/factor.h @@ -141,4 +141,10 @@ CELL executing; #include "dll.h" #include "wrapper.h" +void usage(void); + +void early_init(void); + +const char *default_image_path(void); + #endif /* __FACTOR_H__ */ diff --git a/native/image.c b/native/image.c index 1e033ef26f..903f9edf39 100644 --- a/native/image.c +++ b/native/image.c @@ -20,11 +20,16 @@ void load_image(char* filename, int literal_table) HEADER h; HEADER_2 ext_h; - printf("Loading %s...",filename); - file = fopen(filename,"rb"); if(file == NULL) - fatal_error("Cannot open image for reading",errno); + { + fprintf(stderr,"Cannot open image file: %s\n",filename); + fprintf(stderr,"%s\n",strerror(errno)); + usage(); + exit(1); + } + + printf("Loading %s...",filename); /* read header */ { diff --git a/native/macosx/run.m b/native/macosx/run.m index 3fadf44a25..7851e21dfd 100644 --- a/native/macosx/run.m +++ b/native/macosx/run.m @@ -1,7 +1,10 @@ -/* Cocoa exception handling for Mac OS X */ +/* Cocoa exception handling and default image path for Mac OS X */ #include "../factor.h" +#import "Foundation/NSAutoreleasePool.h" +#import "Foundation/NSBundle.h" #import "Foundation/NSException.h" +#import "Foundation/NSString.h" /* This code is convoluted because Cocoa places restrictions on longjmp and exception handling. In particular, a longjmp can never cross an NS_DURING, @@ -20,7 +23,7 @@ NS_DURING { CELL e = error; error = F; - general_error(ERROR_OBJECTIVE_C,error,true); + general_error(ERROR_OBJECTIVE_C,e,true); } run(); @@ -31,3 +34,15 @@ NS_ENDHANDLER } } +void early_init(void) +{ + [[NSAutoreleasePool alloc] init]; +} + +const char *default_image_path(void) +{ + NSBundle *bundle = [NSBundle mainBundle]; + NSString *path = [bundle bundlePath]; + NSString *image = [path stringByAppendingString:@"/Contents/factor.image"]; + return [image cString]; +} diff --git a/native/unix/run.c b/native/unix/run.c index e7cc648124..2d65e03ac0 100644 --- a/native/unix/run.c +++ b/native/unix/run.c @@ -1,7 +1,13 @@ #include "../factor.h" -void platform_run() +void platform_run(void) { run_toplevel(); } +void early_init(void) {} + +char *default_image_path() +{ + return "factor.image"; +} diff --git a/native/win32/run.c b/native/win32/run.c index caf93c49aa..7369607a37 100644 --- a/native/win32/run.c +++ b/native/win32/run.c @@ -24,7 +24,14 @@ static long exception_handler(void *rec, void *frame, void *ctx, void *dispatch) signal_error(SIGSEGV); } -void platform_run () +void platform_run(void) { seh_call(run_toplevel, exception_handler); } + +void early_init(void) {} + +const char *default_image_path() +{ + return "factor.image"; +}