| | 1 | #ifdef RCSID |
| | 2 | static char RCSid[] = |
| | 3 | "$Header$"; |
| | 4 | #endif |
| | 5 | |
| | 6 | /* |
| | 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| | 8 | * |
| | 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| | 10 | * on using and copying this software. |
| | 11 | */ |
| | 12 | /* |
| | 13 | Name |
| | 14 | vmpreini.cpp - preinitializer |
| | 15 | Function |
| | 16 | Main entrypoint for compile-time preinitialization. Loads an image |
| | 17 | file, runs the main entrypoint in pre-init mode, then rewrites the |
| | 18 | image file in its new state after execution. |
| | 19 | Notes |
| | 20 | |
| | 21 | Modified |
| | 22 | 07/21/99 MJRoberts - Creation |
| | 23 | */ |
| | 24 | |
| | 25 | #include <stdlib.h> |
| | 26 | |
| | 27 | #include "t3std.h" |
| | 28 | #include "vminit.h" |
| | 29 | #include "vmerr.h" |
| | 30 | #include "vmfile.h" |
| | 31 | #include "vmimage.h" |
| | 32 | #include "vmrun.h" |
| | 33 | #include "vmimgrb.h" |
| | 34 | #include "vmpreini.h" |
| | 35 | #include "vmconsol.h" |
| | 36 | |
| | 37 | /* |
| | 38 | * Run pre-initialization |
| | 39 | */ |
| | 40 | void vm_run_preinit(CVmFile *origfp, const char *image_fname, |
| | 41 | CVmFile *newfp, class CVmHostIfc *hostifc, |
| | 42 | class CVmMainClientIfc *clientifc, |
| | 43 | const char *const *argv, int argc, |
| | 44 | class CVmRuntimeSymbols *runtime_symtab) |
| | 45 | { |
| | 46 | vm_globals *vmg__; |
| | 47 | CVmImageLoader *volatile loader = 0; |
| | 48 | CVmImageFile *volatile imagefp = 0; |
| | 49 | |
| | 50 | /* initialize the VM */ |
| | 51 | vm_init_options opts(hostifc, clientifc); |
| | 52 | vm_initialize(&vmg__, &opts); |
| | 53 | |
| | 54 | /* |
| | 55 | * turn off "more" on the console - when running preinitialization, |
| | 56 | * any output is purely diagnostic information for the programmer |
| | 57 | * and thus should be formatted as simple stdio-style console output |
| | 58 | */ |
| | 59 | G_console->set_more_state(FALSE); |
| | 60 | |
| | 61 | err_try |
| | 62 | { |
| | 63 | long start_pos; |
| | 64 | |
| | 65 | /* note where the image file starts */ |
| | 66 | start_pos = origfp->get_pos(); |
| | 67 | |
| | 68 | /* create the loader */ |
| | 69 | imagefp = new CVmImageFileExt(origfp); |
| | 70 | loader = new CVmImageLoader(imagefp, image_fname, 0); |
| | 71 | |
| | 72 | /* load the image */ |
| | 73 | loader->load(vmg0_); |
| | 74 | |
| | 75 | /* set pre-init mode */ |
| | 76 | G_preinit_mode = TRUE; |
| | 77 | |
| | 78 | /* run it, using the runtime symbols the caller sent us */ |
| | 79 | loader->run(vmg_ argv, argc, runtime_symtab, 0); |
| | 80 | |
| | 81 | /* |
| | 82 | * seek back to the start of the image file, since we need to |
| | 83 | * copy parts of the original file to the new file |
| | 84 | */ |
| | 85 | origfp->set_pos(start_pos); |
| | 86 | |
| | 87 | /* save the new image file */ |
| | 88 | vm_rewrite_image(vmg_ origfp, newfp, loader->get_static_cs_ofs()); |
| | 89 | } |
| | 90 | err_finally |
| | 91 | { |
| | 92 | /* detach the pools from the image file */ |
| | 93 | if (loader != 0) |
| | 94 | loader->unload(vmg0_); |
| | 95 | |
| | 96 | /* delete the loader and the image file object */ |
| | 97 | if (loader != 0) |
| | 98 | delete loader; |
| | 99 | if (imagefp != 0) |
| | 100 | delete imagefp; |
| | 101 | |
| | 102 | /* terminate the VM */ |
| | 103 | vm_terminate(vmg__, clientifc); |
| | 104 | } |
| | 105 | err_end; |
| | 106 | } |
| | 107 | |