Fix plugins with MSVC, thanks to Gulam Faruque.
[syx.git] / src / main.c
blob0690edd365171382d10701ccff4d502ab2da0fc6
1 /*
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.
25 #include <syx/syx.h>
27 #include <stdio.h>
29 #ifdef WINDOWS
30 #include <windows.h>
31 #endif
33 static void
34 _help (void)
36 printf ("This is Smalltalk YX. Usage:\n\n"
37 "\tsyx [options] [ filename [ arguments ] ] \n\n"
38 "Options:\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");
55 exit (EXIT_SUCCESS);
58 static void
59 _version (void)
61 printf ("Syx %s\nVisit the homepage: http://code.google.com/p/syx\n"
62 "Copyright (c) 2007-2008 Luca Bruno\n",
63 SYX_VERSION);
66 static void
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);
75 exit (EXIT_FAILURE);
77 else
79 printf("Recovered copy of the image has been created at %s.\n", rim_path);
80 exit (EXIT_SUCCESS);
84 /* Thanks to Krzysztof Kowalczyk for this getopt.
85 We need it on Windows and Windows Mobile devices like PocketPC. */
87 enum OptArgument
89 ARG_UNKNOWN,
90 ARG_ERROR,
91 ARG_ROOT,
92 ARG_IMAGE,
93 ARG_SCRATCH,
94 ARG_SCRATCH_AND_QUIT,
95 ARG_VERSION,
96 ARG_RECOVERY,
97 ARG_HELP,
98 ARG_CONTINUE_STARTUP
101 struct
103 const char* arg_name;
104 enum OptArgument arg_enum;
105 int need_param;
106 } arg_defs[] = {
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},
118 {"-h", ARG_HELP, 0},
119 {NULL, 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)
127 const char *arg;
128 int i;
130 if (curr_arg >= argc)
131 return ARG_UNKNOWN;
133 arg = argv[curr_arg];
134 if (!arg)
135 return ARG_UNKNOWN;
137 if (*arg != '-')
138 return ARG_UNKNOWN;
140 *arg_val = NULL;
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))
146 curr_arg++;
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];
154 curr_arg++;
156 else
158 printf("Error: %s option expects an argument\n", arg_defs[i].arg_name);
159 return ARG_ERROR;
162 return arg_defs[i].arg_enum;
165 return ARG_UNKNOWN;
168 static void
169 _parse_args (int argc, char **argv)
171 SyxOop process, context;
172 syx_bool init;
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)
181 const char* arg_val;
182 enum OptArgument arg_enum = arg_enum_from_name(argc, (const char **)argv, &arg_val);
183 switch (arg_enum)
185 case ARG_ROOT:
186 root_path = syx_strdup (arg_val);
187 break;
188 case ARG_IMAGE:
189 image_path = syx_strdup (arg_val);
190 break;
191 case ARG_SCRATCH_AND_QUIT:
192 quit = TRUE;
193 /* fall through */
194 case ARG_SCRATCH:
195 scratch = TRUE;
196 break;
197 case ARG_VERSION:
198 _version();
199 exit (EXIT_FAILURE);
200 case ARG_RECOVERY:
201 recovery = arg_val;
202 break;
203 case ARG_ERROR:
204 case ARG_HELP:
205 _help ();
206 exit (EXIT_FAILURE);
207 break;
208 case ARG_UNKNOWN:
209 default:
210 /* Exit loop.
211 Unknown arguments will be handled by Smalltalk */
212 goto end_of_arg;
216 end_of_arg:
217 init = syx_init (argc-curr_arg, argv+curr_arg, root_path);
219 if (root_path)
220 syx_free (root_path);
222 if (!init)
223 syx_error ("Couldn't initialize Syx for root: %s\n", syx_get_root_path ());
225 if (image_path)
227 syx_set_image_path (image_path);
228 syx_free (image_path);
231 if (scratch)
233 syx_build_basic ();
235 if (!syx_memory_save_image (NULL))
236 syx_warning ("Can't save the image\n");
238 if (quit)
240 syx_quit ();
241 exit (EXIT_SUCCESS);
244 syx_initialize_system ();
246 else
248 if (!syx_memory_load_image (NULL))
249 syx_error ("Image not found\n");
251 if (recovery)
252 _do_recovery (recovery);
254 /* Force WinWorkspace startup on WinCE */
255 #ifdef WINCE
256 SYX_OBJECT_VARS(syx_globals)[4] = syx_globals_at ("WinWorkspace");
257 #endif
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 ();
274 syx_quit ();
276 return 0;