From 84149c371d220bcba3671a57e50b923b1f51c035 Mon Sep 17 00:00:00 2001 From: "Wolfgang (Blub) Bumiller" Date: Thu, 23 Aug 2012 10:35:03 +0200 Subject: [PATCH] Possibility to call a main function with parameters by specifying -float, -vector or -string parameters to standalone qcvm --- exec.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) 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 -- 2.39.2