static int parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args) { char *outfile = NULL; static const struct mrbc_args args_zero = { 0 }; int i; *args = args_zero; args->argc = argc; args->argv = argv; args->prog = argv[0]; for (i=1; i<argc; i++) { if (argv[i][0] == '-') { switch ((argv[i])[1]) { case 'o': if (args->outfile) { fprintf(stderr, "%s: an output file is already specified. (%s)\n", args->prog, outfile); return -1; } if (argv[i][2] == '\0' && argv[i+1]) { i++; args->outfile = get_outfilename(mrb, argv[i], ""); } else { args->outfile = get_outfilename(mrb, argv[i] + 2, ""); } break; case 'B': if (argv[i][2] == '\0' && argv[i+1]) { i++; args->initname = argv[i]; } else { args->initname = argv[i]+2; } if (*args->initname == '\0') { fprintf(stderr, "%s: function name is not specified.\n", args->prog); return -1; } break; case 'c': args->check_syntax = 1; break; case 'v': if (!args->verbose) mrb_show_version(mrb); args->verbose = 1; break; case 'g': args->debug_info = 1; break; case '-': if (argv[i][1] == '\n') { return i; } if (strcmp(argv[i] + 2, "version") == 0) { mrb_show_version(mrb); exit(EXIT_SUCCESS); } else if (strcmp(argv[i] + 2, "verbose") == 0) { args->verbose = 1; break; } else if (strcmp(argv[i] + 2, "copyright") == 0) { mrb_show_copyright(mrb); exit(EXIT_SUCCESS); } return -1; default: return i; } } else { break; } } return i; }
int main(int argc, char **argv) { mrb_state *mrb = mrb_open(); int n, result; struct mrbc_args args; FILE *wfp; if (mrb == NULL) { fputs("Invalid mrb_state, exiting mrbc\n", stderr); return EXIT_FAILURE; } n = parse_args(mrb, argc, argv, &args); if (n < 0) { cleanup(mrb, &args); usage(argv[0]); return EXIT_FAILURE; } if (n == argc) { fprintf(stderr, "%s: no program file given\n", args.prog); return EXIT_FAILURE; } if (args.outfile == NULL) { if (n + 1 == argc) { args.outfile = get_outfilename(mrb, argv[n], args.initname ? C_EXT : RITEBIN_EXT); } else { fprintf(stderr, "%s: output file should be specified to compile multiple files\n", args.prog); return EXIT_FAILURE; } } args.idx = n; if (load_file(mrb, &args) == EXIT_FAILURE) { cleanup(mrb, &args); return EXIT_FAILURE; } if (args.check_syntax) { printf("%s:%s:Syntax OK", args.prog, argv[n]); } if (args.check_syntax) { cleanup(mrb, &args); return EXIT_SUCCESS; } if (args.outfile) { if (strcmp("-", args.outfile) == 0) { wfp = stdout; } else if ((wfp = fopen(args.outfile, "wb")) == NULL) { fprintf(stderr, "%s: cannot open output file:(%s)\n", args.prog, args.outfile); return EXIT_FAILURE; } } else { fprintf(stderr, "Output file is required\n"); return EXIT_FAILURE; } result = dump_file(mrb, wfp, args.outfile, &args); fclose(wfp); cleanup(mrb, &args); if (result != MRB_DUMP_OK) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
static int parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) { char *infile = NULL; char *outfile = NULL; char **origargv = argv; int result = 0; static const struct _args args_zero = { 0 }; *args = args_zero; args->ext = RITEBIN_EXT; for (argc--,argv++; argc > 0; argc--,argv++) { if (**argv == '-') { if (strlen(*argv) == 1) { args->filename = infile = "-"; args->rfp = stdin; break; } switch ((*argv)[1]) { case 'o': if (outfile) { printf("%s: An output file is already specified. (%s)\n", *origargv, outfile); result = -5; goto exit; } outfile = get_outfilename((*argv) + 2, ""); break; case 'B': args->ext = C_EXT; args->initname = (*argv) + 2; if (*args->initname == '\0') { printf("%s: Function name is not specified.\n", *origargv); result = -2; goto exit; } break; case 'c': args->check_syntax = 1; break; case 'v': mrb_show_version(mrb); args->verbose = 1; break; case '-': if (strcmp((*argv) + 2, "version") == 0) { mrb_show_version(mrb); exit(0); } else if (strcmp((*argv) + 2, "verbose") == 0) { args->verbose = 1; break; } else if (strcmp((*argv) + 2, "copyright") == 0) { mrb_show_copyright(mrb); exit(0); } result = -3; goto exit; default: break; } } else if (args->rfp == NULL) { args->filename = infile = *argv; if ((args->rfp = fopen(infile, "r")) == NULL) { printf("%s: Cannot open program file. (%s)\n", *origargv, infile); goto exit; } } } if (infile == NULL) { result = -4; goto exit; } if (!args->check_syntax) { if (outfile == NULL) { if (strcmp("-", infile) == 0) { outfile = infile; } else { outfile = get_outfilename(infile, args->ext); } } if (strcmp("-", outfile) == 0) { args->wfp = stdout; } else if ((args->wfp = fopen(outfile, "wb")) == NULL) { printf("%s: Cannot open output file. (%s)\n", *origargv, outfile); result = -1; goto exit; } } exit: if (outfile && infile != outfile) free(outfile); return result; }
static int parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) { char *infile = NULL; char *outfile = NULL; char **origargv = argv; memset(args, 0, sizeof(*args)); args->ext = RITEBIN_EXT; for (argc--,argv++; argc > 0; argc--,argv++) { if (**argv == '-') { if (strlen(*argv) <= 1) return -1; switch ((*argv)[1]) { case 'o': outfile = get_outfilename((*argv) + 2, ""); break; case 'B': case 'C': args->ext = C_EXT; args->initname = (*argv) + 2; if (*args->initname == '\0') { printf("%s: Function name is not specified.\n", *origargv); return -2; } args->dump_type = ((*argv)[1] == 'B') ? DUMP_TYPE_BIN : DUMP_TYPE_CODE; break; case 'c': args->check_syntax = 1; break; case 'v': ruby_show_version(mrb); args->verbose = 1; break; case '-': if (strcmp((*argv) + 2, "version") == 0) { ruby_show_version(mrb); } else if (strcmp((*argv) + 2, "verbose") == 0) { args->verbose = 1; break; } else if (strcmp((*argv) + 2, "copyright") == 0) { ruby_show_copyright(mrb); } else return -3; return 0; } } else if (args->rfp == NULL) { infile = *argv; if ((args->rfp = fopen(infile, "r")) == NULL) { printf("%s: Cannot open program file. (%s)\n", *origargv, infile); return 0; } } } if (infile == NULL) return -4; if (args->check_syntax) return 0; if (outfile == NULL) outfile = get_outfilename(infile, args->ext); if ((args->wfp = fopen(outfile, "wb")) == NULL) { printf("%s: Cannot open output file. (%s)\n", *origargv, outfile); return 0; } return 0; }
/* * args_ok * * prepare args, check & parse user args, display error and * help message if neccessary * * result: TRUE, if all args ok */ BOOL args_ok(HSCPRC * hp, int argc, char *argv[]) { BOOL ok; /* return value */ DLLIST *ignore_list = NULL; /* dummy */ EXPSTR *destdir = init_estr(32); /* destination dir */ EXPSTR *rel_destdir = init_estr(32); /* relative destination dir */ EXPSTR *kack_name = init_estr(0); /* temp. str for outfilename */ struct arglist *hsc_args; /* argument structure */ arg_hp = hp; arg_mode_CB(DEFAULT_MODE_STR); /* create arg-table */ hsc_args = prepare_args("HSC_ARGS", /* file args */ "FROM/M", &incfile, "include- and input-file(s)", "TO/K", &arg_outfname, "output file (default: stdout)", "PRJFILE/T/K", &prjfilename, "project file (default: none)", "PREFSFILE/T/K", &prefsfilename, "syntax preferences (default: hsc.prefs)", "MSGFILE=MF/T/K", &msgfilename, "message file (default: stderr)", "MSGFORMAT/T/K", &msg_format, "how to display message", /* numeric */ "MAXERR/N/K", &max_error, "max. number of errors (default: 20)", "EXTENSION/T/K", &arg_extension, "output-file-extension (default: " DEFAULT_EXTENSION ")", "DEFINE=DEF/T/K/M", &define_list, "define global attribute", "IGNORE=IGN/N/K/M/$", arg_ignore_CB, &ignore_list, "ignore message number", "MODE/E/K/$", arg_mode_CB, MODE_ENUMSTR, &arg_mode, "mode for syntax check (" MODE_ENUMSTR ")", "QUOTESTYLE=QS/E/K", QMODE_ENUMSTR, &arg_quotemode, "defines how quotes appear (" QMODE_ENUMSTR ")", #if 0 "ENTITYSTYLE=ES/E/K", EMODE_ENUMSTR, &entmode, "defines how special chars. appear (" EMODE_ENUMSTR ")", /* switches */ #endif "COMPACT=CO/S", &arg_compact, "strip useless LFs and white-spaces", "GETSIZE/S", &arg_getsize, "get width and height of images", "MSGANSI/S", &msg_ansi, "use ansi-sequences in messages", "RPLCENT=RE/S", &arg_rplc_ent, "replace special characters", "RPLCQUOTE=RQ/S", &arg_rplc_quote, "replace quotes in text by `"'", "SMARTENT=SA/S", &arg_smart_ent, "replace special entities (`&<>\"')", "JENS/S", &arg_jens, "don't try this at home", "STRIPCOMMENT=SC/S", &arg_strip_cmt, "strip SGML-comments", "STRIPEXTERNAL=SX/S", &arg_strip_ext, "strip tags with external URIs", "STRIPTAGS=ST/K", &arg_striptags, "tags to be stripped", "ICONBASE/T/K", &arg_iconbase, "base-uri for icon-entities", "STATUS/E/K/$", arg_status_CB, STATUS_ENUM_STR, &disp_status, "status message (" STATUS_ENUM_STR ")", "-DEBUG/S", &arg_debug, "enable debugging output", /* help */ "HELP=?/S", &arg_help, "display this text", "LICENSE/S", &arg_license, "display license", NULL); /* remove dummy list TODO: this sucks */ del_dllist(ignore_list); ok = (hsc_args != NULL); /* set & test args */ if (ok) { BOOL use_stdout = FALSE; /* flag: use stdout as output-file */ ok = set_args(argc, argv, hsc_args); /* display help, if requested vie HELP switch, or no * input to pipe or read is passed */ ok &= (!arg_help && (arg_pipe_in || (incfile && dll_first(incfile)))); if (arg_license) { /* display license text */ fprintf_prginfo(stderr); show_license(); set_return_code(RC_WARN); } else if (!ok) { /* display help, if error in args or HELP-switch set */ fprintf_prginfo(stderr); fprintf_arghelp(stderr, hsc_args); set_return_code(RC_WARN); } else { BOOL fnsux = FALSE; /* flag: TRUE = can't evaluate out-filename */ /* set debugging switch */ hsc_set_debug(hp, arg_debug); /* autoset depending options */ if (hsc_get_debug(hp)) disp_status = STATUS_VERBOSE; /* set default options */ if (!arg_extension) arg_extension = DEFAULT_EXTENSION; /* disable ID-warning if no project-file */ if (!prjfilename) hsc_set_msg_ignore(hp, MSG_NO_DOCENTRY, TRUE); /* compute name of input file */ arg_inpfname = NULL; if (dll_first(incfile) && !arg_pipe_in) { /* use last FROM as input file */ arg_inpfname = dln_data(dll_last(incfile)); set_estr(inpfilename, arg_inpfname); /* get path part of inputfilename as relative * destination directory */ get_fpath(rel_destdir, arg_inpfname); /* TODO: set reldir when including first file */ /* TODO: find out why the above TODO is there */ /* remove input filename from incfile */ del_dlnode(incfile, dll_last(incfile)); D(fprintf(stderr, DHSC "input : use `%s'\n" DHSC "reldir: use `%s'\n", estr2str(inpfilename), estr2str(rel_destdir))); } /* display include files */ D( { DLNODE * nd = dll_first(incfile); while (nd) { fprintf(stderr, DHSC "includ: use `%s'\n", ( STRPTR) dln_data(nd)); nd = dln_next(nd); } } ); /* * if no output-filename given, * outfilename stays NULL. this let open_output * open stdout as output-file */ if (arg_outfname) { /* check, if last char of outputfilename is a * directory separator; if so, use the filename * as destination directory */ if (arg_outfname) { UBYTE lastch = 0; /* get last char of outfname to determine * if it's a directory */ if (strlen(arg_outfname)) lastch = arg_outfname[strlen(arg_outfname) - 1]; #ifdef AMIGA /* for Amiga, execpt empty string for current dir */ if (!lastch) { lastch = (PATH_SEPARATOR[0]); D(fprintf(stderr, DHSC "AMIGA: use current dir\n")); } #endif if (strchr(PATH_SEPARATOR, lastch)) { /* use outfilename as destdir */ set_estr(destdir, arg_outfname); arg_outfname = NULL; D(fprintf(stderr, DHSC "output: use `%s' as destdir\n", estr2str(destdir))); } else if (arg_inpfname) { /* output-filename already specified */ /* separate it to destdir + reldir + name */ EXPSTR *kack_destdir = init_estr(0); EXPSTR *kack_reldir = init_estr(0); STRPTR inp_reldir = estr2str(rel_destdir); STRPTR out_reldir = NULL; STRPTR ou2_reldir = NULL; get_fname(kack_name, arg_outfname); get_fpath(kack_destdir, arg_outfname); /* check corresponding dirs for * consistency: check if last strlen(rel_destdir) * chars are equal */ out_reldir = estr2str(kack_destdir); ou2_reldir = out_reldir; out_reldir = out_reldir + (strlen(out_reldir) - strlen(inp_reldir)); if (out_reldir[0]) { /* search for next dir-sparator backwards */ /* (this ones only needed for a smart error message) */ while ((out_reldir != ou2_reldir) && (!strchr(PATH_SEPARATOR, out_reldir[0])) ) { out_reldir--; } out_reldir++; } D(fprintf(stderr, DHSC "corr_inp: `%s'\n" DHSC "corr_out: `%s'\n", inp_reldir, out_reldir) ); /* check if correspondig relative in/out-dirs * are equal */ if (!fnamecmp(inp_reldir, out_reldir)) { /* they match.. */ STRPTR tmp_name = NULL; /* copy of kack_nam */ /* cut corresponding chars */ get_left_estr(kack_destdir, kack_destdir, estrlen(kack_destdir) - strlen(out_reldir)); set_estr(kack_reldir, inp_reldir); D(fprintf(stderr, DHSC "kack_dst: `%s'\n" DHSC "kack_rel: `%s'\n" DHSC "kack_nam: `%s'\n", estr2str(kack_destdir), estr2str(kack_reldir), estr2str(kack_name)) ); /* just copy these values where they are * expected to be */ estrcpy(destdir, kack_destdir); estrcpy(rel_destdir, kack_reldir); /* create output filename */ tmp_name = strclone(estr2str(kack_name)); estrcpy(kack_name, kack_destdir); estrcat(kack_name, kack_reldir); app_estr(kack_name, tmp_name); ufreestr(tmp_name); arg_outfname = estr2str(kack_name); } else { /* unmatched corresponding dirs */ fprintf(stderr, "unmatched corresponding relative directories:\n" " input `%s'\n output `%s'\n", inp_reldir, out_reldir); ok = FALSE; } /* free temp. vars */ del_estr(kack_reldir); del_estr(kack_destdir); } } if (arg_outfname) { /* set outputfilename with value passed iwithin args */ outfilename = init_estr(32); set_estr(outfilename, arg_outfname); D(fprintf(stderr, DHSC "output: set to `%s'\n", estr2str(outfilename))); } else { if (!arg_pipe_in) { /* no outfilename given */ /* ->outfilename = destdir + inpfilename + ".html" */ /* link destdir & input filename */ outfilename = init_estr(32); link_fname(outfilename, estr2str(destdir), arg_inpfname); if (strcmp(arg_extension, ".")) set_fext(outfilename, arg_extension); D(fprintf(stderr, DHSC "output: concat destdir+inpfile+`.%s'\n" DHSC "output: set to `%s'\n", arg_extension, estr2str(outfilename))); } else fnsux = TRUE; } if (fnsux) { /* no way to find out output filename */ status_error("unable to evaluate output filename\n"); arg_outfname = NULL; ok = FALSE; } } else { D(fprintf(stderr, DHSC "output: use stdout\n")); use_stdout = TRUE; } if (!ok) set_return_code(RC_ERROR); } if (ok) { if (arg_iconbase) hsc_set_iconbase(hp, arg_iconbase); if (!use_stdout) hsc_set_filename_document(hp, estr2str(outfilename)); } /* display argument error message */ if (!ok) { /* NOTE: no strclone() is used on outfilename, if an * error already occured within set_args(). therefore, * you must not call ufreestr( outfilename ) */ pargerr(); arg_outfname = NULL; set_return_code(RC_ERROR); } else { EXPSTR *tmp_fname = init_estr(32); /* filename only part */ fileattr_str = init_estr(64); if (outfilename) get_fname(tmp_fname, estr2str(outfilename)); set_dest_attribs(hp, estr2str(rel_destdir), estr2str(tmp_fname)); if (!arg_pipe_in) { if (outfilename) get_fname(tmp_fname, estr2str(outfilename)); else clr_estr(tmp_fname); set_source_attribs(hp, estr2str(rel_destdir), estr2str(tmp_fname)); } else set_source_attribs(hp, NULL, NULL); D( { HSCMSG_ID i; fprintf(stderr, "\n" DHSC "input : `%s'\n", estr2str(inpfilename)); fprintf(stderr, DHSC "output: `%s'\n", get_outfilename()); fprintf(stderr, DHSC "destdr: `%s'\n", estr2str(destdir)); fprintf(stderr, DHSC "reldst: `%s'\n", estr2str(rel_destdir)); if (prjfilename) fprintf(stderr, DHSC "projct: `%s'\n", prjfilename); if (!use_stdout) fprintf(stderr, DHSC "procss: `%s'\n", estr2str(outfilename)); fprintf(stderr, DHSC "ignore:"); for (i = 0; i < MAX_MSGID; i++) if (hsc_get_msg_ignore(hp, i)) fprintf(stderr, " %lu", i); fprintf(stderr, "\n"); } ); del_estr(tmp_fname); }