From: Wolfgang (Blub) Bumiller Date: Thu, 23 Aug 2012 08:35:03 +0000 (+0200) Subject: Possibility to call a main function with parameters by specifying -float, -vector... X-Git-Tag: 0.1-rc1~81 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=84149c371d220bcba3671a57e50b923b1f51c035;p=xonotic%2Fgmqcc.git Possibility to call a main function with parameters by specifying -float, -vector or -string parameters to standalone qcvm --- diff --git a/exec.c b/exec.c index 53bcb07..e30e2fa 100644 --- a/exec.c +++ b/exec.c @@ -645,6 +645,13 @@ const char *type_name[TYPE_COUNT] = { bool opts_debug = false; bool opts_memchk = false; +typedef struct { + int vtype; + const char *value; +} qcvm_parameter; + +VECTOR_MAKE(qcvm_parameter, main_params); + #define CheckArgs(num) do { \ if (prog->argc != (num)) { \ prog->vmerror++; \ @@ -758,6 +765,43 @@ void usage() exit(1); } +static void prog_main_setparams(qc_program *prog) +{ + size_t i; + qcany *arg; + + for (i = 0; i < main_params_elements; ++i) { + arg = GetGlobal(OFS_PARM0 + 3*i); + arg->vector[0] = 0; + arg->vector[1] = 0; + arg->vector[2] = 0; + switch (main_params_data[i].vtype) { + case TYPE_VECTOR: +#ifdef WIN32 + (void)sscanf_s(main_params_data[i].value, " %f %f %f ", + &arg->vector[0], + &arg->vector[1], + &arg->vector[2]); +#else + (void)sscanf(main_params_data[i].value, " %f %f %f ", + &arg->vector[0], + &arg->vector[1], + &arg->vector[2]); +#endif + break; + case TYPE_FLOAT: + arg->_float = atof(main_params_data[i].value); + break; + case TYPE_STRING: + arg->string = prog_tempstring(prog, main_params_data[i].value); + break; + default: + printf("error: unhandled parameter type: %i\n", main_params_data[i].vtype); + break; + } + } +} + int main(int argc, char **argv) { size_t i; @@ -799,6 +843,33 @@ int main(int argc, char **argv) ++argv; opts_printfields = true; } + else if (!strcmp(argv[1], "-vector") || + !strcmp(argv[1], "-string") || + !strcmp(argv[1], "-float") ) + { + qcvm_parameter p; + if (argv[1][1] == 'f') + p.vtype = TYPE_FLOAT; + else if (argv[1][1] == 's') + p.vtype = TYPE_STRING; + else if (argv[1][1] == 'v') + p.vtype = TYPE_VECTOR; + + --argc; + ++argv; + if (argc < 3) + usage(); + p.value = argv[1]; + + if (main_params_add(p) < 0) { + if (main_params_data) + mem_d(main_params_data); + printf("cannot add parameter\n"); + exit(1); + } + --argc; + ++argv; + } else usage(); } @@ -847,6 +918,7 @@ int main(int argc, char **argv) { if (fnmain > 0) { + prog_main_setparams(prog); prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT); } else