int CVmNetFile::can_write(VMG_ const char *fname, int sfid) { /* note whether the file already exists */ int existed = !osfacc(fname); /* try opening it (keeping existing contents) or creating it */ osfildef *fp = osfoprwb(fname, OSFTBIN); if (fp != 0) { /* successfully opened it - close it */ osfcls(fp); /* if it didn't already exist, delete it */ if (!existed) osfdel(fname); /* success */ return TRUE; } else { /* couldn't open the file */ return FALSE; } }
/* * Empty a local directory */ static void empty_dir(VMG_ const char *dir) { /* open the directory search */ osdirhdl_t dirhdl; if (os_open_dir(dir, &dirhdl)) { err_try { /* keep going until we're out of files */ char fname[OSFNMAX]; while (os_read_dir(dirhdl, fname, sizeof(fname))) { /* get the full path */ char path[OSFNMAX]; os_build_full_path(path, sizeof(path), dir, fname); /* get the mode */ unsigned long fmode; unsigned long fattr; if (osfmode(path, FALSE, &fmode, &fattr)) { /* check whether it's a directory or an ordinary file */ if ((fmode & OSFMODE_DIR) != 0) { /* * directory - skip the special '.' and '..' links, * since they'd get us stuck in a loop */ os_specfile_t st = os_is_special_file(fname); if (st != OS_SPECFILE_SELF && st != OS_SPECFILE_PARENT) { /* recursively empty the directory */ empty_dir(vmg_ path); /* remove this directory */ if (!os_rmdir(path)) err_throw(VMERR_DELETE_FILE); } } else { /* ordinary file - delete it */ if (osfdel(path)) err_throw(VMERR_DELETE_FILE); } } } } err_finally { /* close the directory search handle */ os_close_dir(dirhdl); } err_end; }
int main(int argc, char **argv) { CResLoader *res_loader; CTcHostIfc *hostifc; int curarg; int fatal_error_count = 0; osfildef *fpout = 0; int next_local = 0; CVmFile *imgfile = 0; CVmFile *objfile = 0; const char *imgfname; int success; char pathbuf[OSFNMAX]; static const char tool_data[4] = { 't', 's', 't', 'L' }; /* initialize for testing */ test_init(); /* create the host interface object */ hostifc = new CTcHostIfcStdio(); /* create a resource loader */ os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_RES); res_loader = new CResLoader(pathbuf); /* initialize the compiler */ CTcMain::init(hostifc, res_loader, 0); err_try { /* scan arguments */ for (curarg = 1 ; curarg < argc ; ++curarg) { char *p; /* get the argument string for easy reference */ p = argv[curarg]; /* if it's not an option, we're done */ if (*p != '-') break; if (*(p + 1) == 'v') { /* set verbose mode */ G_tcmain->set_verbosity(TRUE); } else { /* * invalid usage - consume all the arguments and fall * through to the usage checker */ curarg = argc; break; } } /* check arguments */ if (curarg + 2 > argc) { /* terminate the compiler */ CTcMain::terminate(); /* delete our objects */ delete res_loader; /* exit with an error */ errexit("usage: test_link [options] obj-file [obj-file [...]] " "image-file\n" "options:\n" " -v - verbose error messages"); } /* set up an output file */ imgfname = argv[argc - 1]; fpout = osfopwb(imgfname, OSFTT3IMG); if (fpout == 0) errexit("unable to open image file"); imgfile = new CVmFile(); imgfile->set_file(fpout, 0); /* read the object files */ for ( ; curarg < argc - 1 ; ++curarg) { osfildef *fpobj; /* open this object file */ fpobj = osfoprb(argv[curarg], OSFTT3OBJ); if (fpobj == 0) { printf("unable to open object file \"%s\"\n", argv[curarg]); goto done; } /* note the loading */ printf("loading %s\n", argv[curarg]); /* set up the CVmFile object for it */ objfile = new CVmFile(); objfile->set_file(fpobj, 0); /* read the object file */ G_cg->load_object_file(objfile, argv[curarg]); /* done with the object file */ delete objfile; objfile = 0; } /* check for unresolved externals */ if (G_prs->check_unresolved_externs()) goto done; /* write the image file */ G_cg->write_to_image(imgfile, 0, tool_data); done: ; } err_catch(exc) { /* * if it's not a general internal or fatal error, log it; don't * log general errors, since these will have been logged as * specific internal errors before being thrown */ if (exc->get_error_code() != TCERR_INTERNAL_ERROR && exc->get_error_code() != TCERR_FATAL_ERROR) G_tok->log_error(TC_SEV_FATAL, exc->get_error_code()); /* count the fatal error */ ++fatal_error_count; } err_end; /* report errors */ fprintf(stderr, "Warnings: %d\n" "Errors: %d\n" "Longest string: %d, longest list: %d\n", G_tcmain->get_warning_count(), G_tcmain->get_error_count() + fatal_error_count, G_cg->get_max_str_len(), G_cg->get_max_list_cnt()); /* * note whether or not the compilation was successful - it succeeded * if we had no errors or fatal errors */ success = (G_tcmain->get_error_count() + fatal_error_count == 0); /* delete the object file object (this closes the file) */ delete imgfile; /* if we have an open object file, close it */ if (objfile != 0) delete objfile; /* * if any errors occurred, delete the object file in the external * file system - this prevents us from leaving around an incomplete * or corrupted image file when compilation fails, and helps * 'make'-type tools realize that they must generate the image file * target again on the next build, even if source files didn't * change */ if (!success) osfdel(imgfname); /* shut down the compiler */ CTcMain::terminate(); /* done with the res loader */ delete res_loader; /* delete the host interface */ delete hostifc; /* show any unfreed memory */ t3_list_memory_blocks(0); /* * terminate - exit with a success indication if we had no errors * (other than warnings); exit with an error indication otherwise */ return (success ? OSEXSUCC : OSEXFAIL); }
int main(int argc, char **argv) { CResLoader *res_loader; CTcHostIfc *hostifc; int curarg; int fatal_error_count = 0; osfildef *fpout = 0; CVmFile *imgfile = 0; CTcUnasSrcCodeStr *unas_in; CTcUnasOutStdio unas_out; CTPNStmProg *node; const char *image_fname; int success; int unasm = FALSE; uchar xor_mask = 0; char pathbuf[OSFNMAX]; /* initialize for testing */ test_init(); /* create the host interface object */ hostifc = new CTcHostIfcStdio(); /* create a resource loader */ os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_RES); res_loader = new CResLoader(pathbuf); /* initialize the compiler */ CTcMain::init(hostifc, res_loader, "us-ascii"); /* use test reporting mode */ G_tok->set_test_report_mode(TRUE); G_tcmain->set_test_report_mode(TRUE); /* create the disassembler input stream */ unas_in = new CTcUnasSrcCodeStr(G_cs); err_try { static const char tool_data[4] = { 't', 's', 't', 'P' }; /* scan options */ for (curarg = 1 ; curarg < argc ; ++curarg) { char *p; /* get the argument string for easy reference */ p = argv[curarg]; /* if it's not an option, we're done */ if (*p != '-') break; /* if it's a -I argument, use it */ if (*(p + 1) == 'I') { char *path; /* * if it's with this argument, read it, otherwise move * on to the next argument */ if (*(p + 2) == '\0') path = argv[++curarg]; else path = p + 2; /* add the directory to the include path list */ G_tok->add_inc_path(path); } else if (*(p + 1) == 'v') { /* set verbose mode */ G_tcmain->set_verbosity(TRUE); } else if (*(p + 1) == 'u') { /* note unassembly mode */ unasm = TRUE; } else { /* * invalid usage - consume all the arguments and fall * through to the usage checker */ curarg = argc; break; } } /* check arguments */ if (curarg + 2 != argc) { /* terminate the compiler */ CTcMain::terminate(); /* delete our objects */ delete res_loader; /* exit with an error */ errexit("usage: test_prs [options] <source-file> <image-file>\n" "options:\n" " -Idir - add dir to include path\n" " -v - verbose error messages"); } /* add the default system include directory to the include path */ os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_INC); G_tok->add_inc_path(pathbuf); /* set up the tokenizer with the main input file */ if (G_tok->set_source(argv[curarg], argv[curarg])) errexit("unable to open source file"); /* set up an output file */ image_fname = argv[curarg+1]; fpout = osfopwb(image_fname, OSFTT3IMG); if (fpout == 0) errexit("unable to open image file"); imgfile = new CVmFile(); imgfile->set_file(fpout, 0); /* read the first token */ G_tok->next(); /* parse at the top level */ node = G_prs->parse_top(); /* if errors occurred during parsing, stop here */ if (G_tcmain->get_error_count() != 0 || node == 0) goto done; /* fold symbolic constants for all nodes */ node->fold_constants(G_prs->get_global_symtab()); /* if errors occurred during constant folding, stop now */ if (G_tcmain->get_error_count() != 0) goto done; /* generate code and write the image file */ node->build_image(imgfile, xor_mask, tool_data); /* if errors occurred during code generation, stop now */ if (G_tcmain->get_error_count() != 0) goto done; /* disassemble the result if desired */ if (unasm) CTcT3Unasm::disasm(unas_in, &unas_out); done: ; } err_catch(exc) { /* * if it's not a general internal or fatal error, log it; don't * log general errors, since these will have been logged as * specific internal errors before being thrown */ if (exc->get_error_code() != TCERR_INTERNAL_ERROR && exc->get_error_code() != TCERR_FATAL_ERROR) G_tok->log_error(TC_SEV_FATAL, exc->get_error_code()); /* count the fatal error */ ++fatal_error_count; } err_end; /* report errors */ fprintf(stderr, "Warnings: %d\n" "Errors: %d\n" "Longest string: %d, longest list: %d\n", G_tcmain->get_warning_count(), G_tcmain->get_error_count() + fatal_error_count, G_cg->get_max_str_len(), G_cg->get_max_list_cnt()); /* * note whether or not the compilation was successful - it succeeded * if we had no errors or fatal errors */ success = (G_tcmain->get_error_count() + fatal_error_count == 0); /* delete the image file object (this closes the file) */ delete imgfile; /* * if any errors occurred, delete the image file in the external * file system - this prevents us from leaving around an incomplete * or corrupted image file when compilation fails, and helps * 'make'-type tools realize that they must generate the image file * target again on the next build, even if source files didn't * change */ if (!success) osfdel(image_fname); /* delete the disassembler input object */ delete unas_in; /* shut down the compiler */ CTcMain::terminate(); /* done with the res loader */ delete res_loader; /* delete the host interface */ delete hostifc; /* show any unfreed memory */ t3_list_memory_blocks(0); /* * terminate - exit with a success indication if we had no errors * (other than warnings); exit with an error indication otherwise */ return (success ? OSEXSUCC : OSEXFAIL); }