2 Copyright (c) 2007-2008 Luca Bruno
4 This file is part of Smalltalk YX.
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
36 printf ("This is Smalltalk YX. Usage:\n\n"
37 "\tsyx [options] [ filename [ arguments ] ] \n\n"
39 " -r --root=DIR\t\tSpecify the root path of Syx\n\t\t\t(default: %s).\n\n"
40 " -i --image=IMAGEFILE\tLoad the image IMAGEFILE, also SYX_IMAGE_PATH=x\n"
41 "\t\t\t(default: %s)\n\n"
42 " -s --scratch\t\tBuild the environment from scratch and save the image.\n"
43 " -S\t\t\tLike --scratch. Exits once the environment is built.\n",
44 SYX_ROOT_PATH
, SYX_IMAGE_PATH
);
46 printf (" -c\t\t\tContinue startup process after loading files\n"
47 "\t\t\tor evaluating code.\n\n"
48 " -e CODE\t\tEvaluate one line of code.\n"
49 " --recovery=IMAGEFILE\tLoad the default image and save the recovered copy\n"
50 "\t\t\tof it to IMAGEFILE.\n\n"
51 " -v --version\t\tPrint version information and then exit.\n"
52 " -h --help\t\tPrint this message.\n\n"
53 "For more informations, please visit the homepage: http://code.google.com/p/syx.\n"
54 "Report bugs to \"lethalman88@gmail.com\".\n");
61 printf ("Syx %s\nVisit the homepage: http://code.google.com/p/syx\n"
62 "Copyright (c) 2007-2008 Luca Bruno\n",
67 _do_recovery (const char *rim_path
)
69 SyxOop process
= syx_processor_active_process
;
70 syx_scheduler_remove_process (process
);
72 if (!syx_memory_save_image (rim_path
))
74 printf("Can't save a recovered copy of the image at %s.\n", rim_path
);
79 printf("Recovered copy of the image has been created at %s.\n", rim_path
);
84 /* Thanks to Krzysztof Kowalczyk for this getopt.
85 We need it on Windows and Windows Mobile devices like PocketPC. */
103 const char* arg_name
;
104 enum OptArgument arg_enum
;
107 {"--root", ARG_ROOT
, TRUE
},
108 {"--image", ARG_IMAGE
, TRUE
},
109 {"--scratch", ARG_SCRATCH
, 0},
110 {"--version", ARG_VERSION
, 0},
111 {"--recovery", ARG_RECOVERY
, TRUE
},
112 {"--help", ARG_HELP
, 0},
113 {"-r", ARG_ROOT
, TRUE
},
114 {"-i", ARG_IMAGE
, TRUE
},
115 {"-s", ARG_SCRATCH
, 0},
116 {"-S", ARG_SCRATCH_AND_QUIT
, 0},
117 {"-v", ARG_VERSION
, 0},
122 static int curr_arg
=1;
124 static enum OptArgument
125 arg_enum_from_name (int argc
, const char **argv
, const char **arg_val
)
130 if (curr_arg
>= argc
)
133 arg
= argv
[curr_arg
];
141 for (i
=0; arg_defs
[i
].arg_name
; i
++)
143 size_t len
= strlen(arg_defs
[i
].arg_name
);
144 if (!strncmp(arg
, arg_defs
[i
].arg_name
, len
))
147 if (arg_defs
[i
].need_param
)
149 if ((strlen(arg
) > len
) && ('=' == arg
[len
]))
150 *arg_val
= arg
+ len
+ 1;
151 else if (curr_arg
< argc
&& *argv
[curr_arg
] != '-')
153 *arg_val
= argv
[curr_arg
];
158 printf("Error: %s option expects an argument\n", arg_defs
[i
].arg_name
);
162 return arg_defs
[i
].arg_enum
;
169 _parse_args (int argc
, char **argv
)
171 SyxOop process
, context
;
173 syx_string root_path
= NULL
;
174 syx_string image_path
= NULL
;
175 syx_symbol recovery
= NULL
;
176 syx_bool scratch
= FALSE
;
177 syx_bool quit
= FALSE
;
179 while (curr_arg
< argc
)
182 enum OptArgument arg_enum
= arg_enum_from_name(argc
, (const char **)argv
, &arg_val
);
186 root_path
= syx_strdup (arg_val
);
189 image_path
= syx_strdup (arg_val
);
191 case ARG_SCRATCH_AND_QUIT
:
211 Unknown arguments will be handled by Smalltalk */
217 init
= syx_init (argc
-curr_arg
, argv
+curr_arg
, root_path
);
220 syx_free (root_path
);
223 syx_error ("Couldn't initialize Syx for root: %s\n", syx_get_root_path ());
227 syx_set_image_path (image_path
);
228 syx_free (image_path
);
235 if (!syx_memory_save_image (NULL
))
236 syx_warning ("Can't save the image\n");
244 syx_initialize_system ();
248 if (!syx_memory_load_image (NULL
))
249 syx_error ("Image not found\n");
252 _do_recovery (recovery
);
254 /* Force WinWorkspace startup on WinCE */
256 SYX_OBJECT_VARS(syx_globals
)[4] = syx_globals_at ("WinWorkspace");
259 /* Now schedule to startup */
260 process
= syx_process_new ();
261 context
= syx_send_unary_message (syx_globals
, "startupSystem");
262 syx_interp_enter_context (process
, context
);
263 SYX_PROCESS_SUSPENDED (process
) = syx_false
;
264 SYX_OBJECT_VARS(syx_globals
)[3] = process
;
268 int SYX_CDECL
main(int argc
, char **argv
)
270 _parse_args(argc
, argv
);
272 syx_scheduler_run ();