| 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 | class CVmRuntimeSymbols *runtime_macros) |
| 46 | { |
| 47 | vm_globals *vmg__; |
| 48 | CVmImageLoader *volatile loader = 0; |
| 49 | CVmImageFile *volatile imagefp = 0; |
| 50 | |
| 51 | /* initialize the VM */ |
| 52 | vm_init_options opts(hostifc, clientifc); |
| 53 | vm_initialize(&vmg__, &opts); |
| 54 | |
| 55 | /* |
| 56 | * turn off "more" on the console - when running preinitialization, |
| 57 | * any output is purely diagnostic information for the programmer |
| 58 | * and thus should be formatted as simple stdio-style console output |
| 59 | */ |
| 60 | G_console->set_more_state(FALSE); |
| 61 | |
| 62 | err_try |
| 63 | { |
| 64 | /* note where the image file starts */ |
| 65 | long start_pos = origfp->get_pos(); |
| 66 | |
| 67 | /* create the loader */ |
| 68 | imagefp = new CVmImageFileExt(origfp); |
| 69 | loader = new CVmImageLoader(imagefp, image_fname, 0); |
| 70 | |
| 71 | /* load the image */ |
| 72 | loader->load(vmg0_); |
| 73 | |
| 74 | /* set pre-init mode */ |
| 75 | G_preinit_mode = TRUE; |
| 76 | |
| 77 | /* run it, using the runtime symbols the caller sent us */ |
| 78 | loader->run(vmg_ argv, argc, runtime_symtab, runtime_macros, 0); |
| 79 | |
| 80 | /* |
| 81 | * seek back to the start of the image file, since we need to |
| 82 | * copy parts of the original file to the new file |
| 83 | */ |
| 84 | origfp->set_pos(start_pos); |
| 85 | |
| 86 | /* save the new image file */ |
| 87 | vm_rewrite_image(vmg_ origfp, newfp, loader->get_static_cs_ofs()); |
| 88 | } |
| 89 | err_finally |
| 90 | { |
| 91 | /* detach the pools from the image file */ |
| 92 | if (loader != 0) |
| 93 | loader->unload(vmg0_); |
| 94 | |
| 95 | /* delete the loader and the image file object */ |
| 96 | if (loader != 0) |
| 97 | delete loader; |
| 98 | if (imagefp != 0) |
| 99 | delete imagefp; |
| 100 | |
| 101 | /* terminate the VM */ |
| 102 | vm_terminate(vmg__, clientifc); |
| 103 | } |
| 104 | err_end; |
| 105 | } |
| 106 |