Setup and clear of string pool.
[SquirrelJME.git] / nanocoat / frontend / stdc / stdcMain.c
blob35c8094cabfc5ca8b9229eec4b76995e03b93b70
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <stdlib.h>
11 #include <string.h>
13 #include "sjme/debug.h"
14 #include "sjme/alloc.h"
15 #include "sjme/nvm/boot.h"
16 #include "sjme/nvm/nvm.h"
17 #include "sjme/nvm/nvmFunc.h"
19 /**
20 * Main program entry point.
22 * @param argc Argument count.
23 * @param argv Arguments passed.
24 * @return Returns @c 0 on success, otherwise another exit code.
26 int main(int argc, sjme_lpcstr* argv)
28 sjme_errorCode error;
29 sjme_alloc_pool* pool;
30 sjme_nvm inState;
31 sjme_nvm_bootParam bootParam;
32 sjme_jint exitCode;
33 sjme_jboolean terminated;
34 const sjme_nal* nal;
36 /* Use default NAL. */
37 nal = &sjme_nal_default;
39 /* Allocate main pool. */
40 pool = NULL;
41 if (sjme_error_is(error = sjme_alloc_poolInitMalloc(&pool,
42 16777216)) || pool == NULL)
43 goto fail_poolInit;
45 /* Setup boot parameters. */
46 memset(&bootParam, 0, sizeof(bootParam));
47 bootParam.nal = nal;
49 /* Parse main arguments. */
50 if (sjme_error_is(error = sjme_nvm_parseCommandLine(pool,
51 nal, &bootParam, argc, argv)))
53 /* Exit instead of continuing? */
54 if (error == SJME_ERROR_EXIT)
56 exitCode = 0;
57 goto okay_shortExit;
60 goto fail_argParse;
63 /* If there was no boot suite specified, load a default one. */
64 if (bootParam.bootSuite == NULL)
65 if (sjme_error_is(error = sjme_nvm_defaultBootSuite(pool,
66 nal, &bootParam.bootSuite)) ||
67 bootParam.bootSuite == NULL)
68 goto fail_bootSuiteBackup;
70 /* If no classpath was specified, load the launcher instead. */
71 if (bootParam.mainClassPathById == NULL &&
72 bootParam.mainClassPathByName == NULL)
74 /* Try to find default launcher. */
75 if (sjme_error_is(error = sjme_rom_suiteDefaultLaunch(pool,
76 bootParam.bootSuite,
77 (sjme_lpstr*)&bootParam.mainClass,
78 (sjme_list_sjme_lpstr**)&bootParam.mainArgs,
79 (sjme_list_sjme_jint**)&bootParam.mainClassPathById,
80 (sjme_list_sjme_lpstr**)&bootParam.mainClassPathByName)))
81 goto fail_defaultLaunch;
83 /* Still failed? */
84 if (bootParam.mainClassPathById == NULL &&
85 bootParam.mainClassPathByName == NULL)
87 error = SJME_ERROR_NO_CLASS;
88 goto fail_defaultLaunch;
92 /* Boot the virtual machine. */
93 inState = NULL;
94 if (sjme_error_is(error = sjme_nvm_boot(pool,
95 NULL, &bootParam, &inState)))
96 goto fail_boot;
98 /* Iterate the virtual machine loop. */
99 for (terminated = SJME_JNI_FALSE; !terminated;)
101 /* Let other threads run. */
102 sjme_thread_yield();
104 /* Tick the virtual machine. */
105 if (sjme_error_is(error = sjme_nvm_tick(inState, -1,
106 &terminated)))
108 /* Fail unless this was interrupted. */
109 if (error == SJME_ERROR_INTERRUPTED)
110 continue;
112 goto fail_loop;
116 /* Destroy the VM before exit. */
117 exitCode = -1;
118 if (sjme_error_is(error = sjme_nvm_destroy(inState, &exitCode)))
119 goto fail_destroy;
121 okay_shortExit:
122 /* Destroy the main memory pool. */
123 if (sjme_error_is(error = sjme_alloc_poolDestroy(pool)))
124 goto fail_destroyPool;
126 /* Exit with the given code. */
127 return exitCode;
129 fail_destroyPool:
130 fail_destroy:
131 fail_loop:
132 fail_boot:
133 if (inState != NULL)
134 sjme_nvm_destroy(inState, NULL);
136 fail_defaultLaunch:
137 fail_bootSuiteBackup:
138 fail_argParse:
139 fail_poolInit:
140 if (pool != NULL)
141 sjme_alloc_poolDestroy(pool);
143 if (error == SJME_ERROR_NONE || error == SJME_ERROR_UNKNOWN)
144 return EXIT_FAILURE;
145 return (-error);