use b_plain use cz/vars use cz/compile cstr version = "1.0" cstr description = "CZ translator - Sam Watkins, 2009" cstr usage[] = "[opts] prog [arg ...]", "[opts] -h [prog1 ...]", "--help", NULL cstr options[][3] = { "V", "version", "" }, { "v", "verbose", "" }, { "d", "dump", "dump configuration" }, { "c", "compile", "translate and compile only" }, { "t", "translate", "translate only" }, { "h", "headers", "generate headers" }, { "r", "run", "run only, do not compile" }, { "C", "clean", "recompile, remove unneeded files" }, { "1", "b1file", "generate b1 file" }, { "B", "nobackup", "don't backup files" }, { "u", "useb", "use b" }, { "m", "addmain", "add Main" }, { NULL, NULL, NULL } # TODO warn unknown options, check option arity # change get_options() to also read from config file/s and env vars Main() bm_enabled = 0 bm_start() O = get_options(options) boolean ok = 1 if opt("help") help() eif opt("version") version() else ok = cz_main_processor() exit(!ok) boolean cz_main_processor() boolean ok = 1 tofree_block() init() bm("done init") config() bm("done config") setup() bm("done setup") if opt("headers") boolean first = 1 eacharg(x) if first first = 0 else cz_parse_clear() src_file = x prepare() ok = cz_create_header() && ok else Getargs(cstr, src_file) prepare() bm("done prepare") if opt("dump") dump_settings() if opt("clean") clean() bm("done clean") ok = cz_compile_run() cz_free() return ok boolean cz_compile_run() boolean ok = 1 check_status() bm("done check") if need_compile && !opt("run") lock(lock_file) check_status() bm("done lock + check 2") if need_compile && !already_compiled if !opt("nobackup") do_backup() bm("done backup") ok = cz_translate_compile() bm("done translate / compile") if !file_size(log_file) remove(log_file) if ok && opt("clean") remove(c_file) if already_compiled error("%s has not been updated, check %s or try: fix %s", src_file, log_file, src_file) if ok && !opt("compile") && !opt("translate") cz_run() return ok check_status() NEW(src_stat, Stats, src_file) tofree(src_stat) if !stat_exists(src_stat) error("source file %s does not exist", src_file) NEW(exe_stat, Stats, exe_file) tofree(exe_stat) NEW(log_stat, Stats, log_file) tofree(log_stat) need_compile = !stat_exists(exe_stat) || exe_stat->st_size == 0 || exe_stat->st_mtime < src_stat->st_mtime || exe_stat->st_mtime < libb_stat->st_mtime already_compiled = stat_exists(log_stat) && src_stat->st_mtime < log_stat->st_mtime && log_stat->st_mtime > libb_stat->st_mtime do_backup() if exists(exe_file) remove(xbak_file) cp(exe_file, xbak_file) if file_cmp(src_file, bbak_file) remove(bbak2_file) rename(bbak_file, bbak2_file) cp(src_file, bbak_file) cz_run() if run_in_gdb cstr gdb = Which("gdb") cstr *gdbargs = Nalloc(cstr, args+4) gdbargs[0] = gdb gdbargs[1] = "--args" gdbargs[2] = exe_file memmove(gdbargs+3, arg, (args+1)*sizeof(cstr)) exe_file = gdb arg = gdbargs else --arg ; ++args ; arg[0] = exe_file if mingw && !*Getenv("MSYSTEM") && !run_in_gdb Atexit(hold_term_open) if cz_verbose decl(v, vec) array_to_vec(v, arg+1) warn_vec_cstr(exe_file, v) if mingw exit(Systemv(exe_file, arg)) # Execv doesn't wait on mingw! else Execv(exe_file, arg)