mirror of
https://github.com/eddyem/snippets_library.git
synced 2026-03-20 16:50:57 +03:00
some useful fixes
This commit is contained in:
@@ -50,22 +50,28 @@ static glob_pars const Gdefault = {
|
||||
/*
|
||||
* Define command line options by filling structure:
|
||||
* name has_arg flag val type argptr help
|
||||
* BE carefull! The `help` field is mandatory! Omitting it equivalent of 'end_option'
|
||||
*/
|
||||
static sl_option_t cmdlnopts[] = {
|
||||
{"lo0", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo0), _("only long arg 0")},
|
||||
// short option in only-long options should be zeroed, or you can add flag to set it to given value
|
||||
{"lo0", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo0), _("only long arg 0 (int)")},
|
||||
// for short-only options long option can be NULL
|
||||
{NULL, NEED_ARG, NULL, '0', arg_string, APTR(&G.so1), _("only short arg 1 (string)")},
|
||||
// if you change `arg_int` to `arg_none`, value will be incremented each `-h`
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
|
||||
// {"dup", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
|
||||
{"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("serial device name")},
|
||||
{"lo2", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo2), _("only long arg 2")},
|
||||
// for short-only options long option can also be an empty string
|
||||
{"", NEED_ARG, NULL, '1', arg_string, APTR(&G.so2), _("only short arg 2 (string)")},
|
||||
{"lo2", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo2), _("only long arg 2 (int)")},
|
||||
{"speed", NEED_ARG, NULL, 's', arg_int, APTR(&G.speed), _("serial device speed (default: 9600)")},
|
||||
{"logfile", NEED_ARG, NULL, 'l', arg_string, APTR(&G.logfile), _("file to save logs")},
|
||||
{"pidfile", NEED_ARG, NULL, 'P', arg_string, APTR(&G.pidfile), _("pidfile (default: " DEFAULT_PIDFILE ")")},
|
||||
{"exclusive",NO_ARGS, NULL, 'e', arg_int, APTR(&G.exclusive), _("open serial device exclusively")},
|
||||
// example of multiple options
|
||||
{"Int", MULT_PAR, NULL, 'I', arg_int, APTR(&G.intarr), _("integer multiplying parameter")},
|
||||
{"Dbl", MULT_PAR, NULL, 'D', arg_double, APTR(&G.dblarr), _("double multiplying parameter")},
|
||||
{"Str", MULT_PAR, NULL, 'S', arg_string, APTR(&G.strarr), _("string multiplying parameter")},
|
||||
{"lo1", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo1), _("only long arg 1")},
|
||||
{"Int", MULT_PAR, NULL, 'I', arg_int, APTR(&G.intarr), _("integer parameter")},
|
||||
{"Dbl", MULT_PAR, NULL, 'D', arg_double, APTR(&G.dblarr), _("double parameter")},
|
||||
{"Str", MULT_PAR, NULL, 'S', arg_string, APTR(&G.strarr), _("string parameter")},
|
||||
{"lo1", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo1), _("only long arg 1 (int)")},
|
||||
end_option
|
||||
};
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ typedef struct{
|
||||
int lo0; // only long options
|
||||
int lo1;
|
||||
int lo2;
|
||||
char *so1; // only short options
|
||||
char *so2;
|
||||
int rest_pars_num; // number of rest parameters
|
||||
char** rest_pars; // the rest parameters: array of char*
|
||||
} glob_pars;
|
||||
|
||||
@@ -23,12 +23,10 @@
|
||||
#include "usefull_macros.h"
|
||||
|
||||
typedef struct{
|
||||
char *sp1;
|
||||
char *sp2;
|
||||
char **sp;
|
||||
int ip1;
|
||||
int ip2;
|
||||
double dp1;
|
||||
double dp2;
|
||||
double **dp;
|
||||
float fp1;
|
||||
float fp2;
|
||||
int help;
|
||||
@@ -36,66 +34,94 @@ typedef struct{
|
||||
char *confname;
|
||||
} parameters;
|
||||
|
||||
static parameters G = {
|
||||
static parameters G, parini = {
|
||||
.ip1 = INT_MIN,
|
||||
.ip2 = INT_MIN,
|
||||
.dp1 = NAN,
|
||||
.dp2 = NAN,
|
||||
.fp1 = NAN,
|
||||
.fp2 = NAN
|
||||
};
|
||||
|
||||
#define CONFOPTS \
|
||||
{"string", MULT_PAR, NULL, 's', arg_string, APTR(&G.sp), "string array"}, \
|
||||
{"int1", NEED_ARG, NULL, 'i', arg_int, APTR(&G.ip1), "integer one"}, \
|
||||
{"int2", NEED_ARG, NULL, 'u', arg_int, APTR(&G.ip2), "integer two"}, \
|
||||
{"double", MULT_PAR, NULL, 'd', arg_double, APTR(&G.dp), "double array"}, \
|
||||
{"float1", NEED_ARG, NULL, 'f', arg_float, APTR(&G.fp1), "float one"}, \
|
||||
{"float2", NEED_ARG, NULL, 'l', arg_float, APTR(&G.fp2), "float two"}, \
|
||||
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose),"verbose level (each -v adds 1)"},
|
||||
|
||||
static sl_option_t cmdlnopts[] = {
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), "show this help"},
|
||||
{"string1", NEED_ARG, NULL, 's', arg_string, APTR(&G.sp1), "string one"},
|
||||
{"string2", NEED_ARG, NULL, 'c', arg_string, APTR(&G.sp2), "string two"},
|
||||
{"int1", NEED_ARG, NULL, 'i', arg_int, APTR(&G.ip1), "integer one"},
|
||||
{"int2", NEED_ARG, NULL, 'u', arg_int, APTR(&G.ip2), "integer two"},
|
||||
{"double1", NEED_ARG, NULL, 'd', arg_double, APTR(&G.dp1), "double one"},
|
||||
{"double2", NEED_ARG, NULL, 'o', arg_double, APTR(&G.dp2), "double two"},
|
||||
{"float1", NEED_ARG, NULL, 'f', arg_float, APTR(&G.fp1), "float one"},
|
||||
{"float2", NEED_ARG, NULL, 'l', arg_float, APTR(&G.fp2), "float two"},
|
||||
CONFOPTS
|
||||
{"config", NEED_ARG, NULL, 'C', arg_string, APTR(&G.confname),"name of configuration file"},
|
||||
{"verbose", NO_ARGS, NULL, 'v', arg_none, APTR(&G.verbose),"verbose level (each -v adds 1)"},
|
||||
end_option
|
||||
};
|
||||
// config options - without some unneed
|
||||
static sl_option_t confopts[] = {
|
||||
CONFOPTS
|
||||
end_option
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char **argv){
|
||||
sl_init();
|
||||
G = parini;
|
||||
sl_parseargs(&argc, &argv, cmdlnopts);
|
||||
if(G.help) sl_showhelp(-1, cmdlnopts);
|
||||
// if you will end main options with '--', you can write some additional options after and again run sl_parseargs with other sl_option_t array
|
||||
if(argc) for(int i = 0; i < argc; ++i){
|
||||
red("Extra arg: `%s`\n", argv[i]);
|
||||
}
|
||||
sl_loglevel_e lvl = G.verbose + LOGLEVEL_ERR;
|
||||
if(lvl >= LOGLEVEL_AMOUNT) lvl = LOGLEVEL_AMOUNT - 1;
|
||||
printf("verbose level: %d\n", lvl);
|
||||
if(G.sp1){
|
||||
printf("Parsing of string1: ");
|
||||
char key[SL_KEY_LEN], val[SL_VAL_LEN];
|
||||
int k = sl_get_keyval(G.sp1, key, val);
|
||||
switch(k){
|
||||
case 0:
|
||||
red("key not found\n");
|
||||
break;
|
||||
case 1:
|
||||
green("got key='%s'\n", key);
|
||||
break;
|
||||
default:
|
||||
green("got key='%s', value='%s'\n", key, val);
|
||||
if(G.sp){
|
||||
char **s = G.sp;
|
||||
while(*s){
|
||||
printf("Parsing of string: ");
|
||||
char key[SL_KEY_LEN], val[SL_VAL_LEN];
|
||||
int k = sl_get_keyval(*s, key, val);
|
||||
switch(k){
|
||||
case 0:
|
||||
red("key not found\n");
|
||||
break;
|
||||
case 1:
|
||||
green("got key='%s'\n", key);
|
||||
break;
|
||||
default:
|
||||
green("got key='%s', value='%s'\n", key, val);
|
||||
}
|
||||
++s;
|
||||
}
|
||||
}
|
||||
green("Starting parameters values:\n");
|
||||
char *buf = sl_print_opts(cmdlnopts, TRUE);
|
||||
printf("%s\n", buf);
|
||||
FREE(buf);
|
||||
FREE(buf); // don't forget to `free` this buffer
|
||||
if(G.confname){
|
||||
int o = sl_conf_readopts(G.confname, cmdlnopts);
|
||||
const char *confname = G.confname;
|
||||
G = parini;
|
||||
printf("now v=%d\n", G.verbose);
|
||||
int o = sl_conf_readopts(confname, confopts);
|
||||
if(o > 0){
|
||||
printf("got %d options in '%s'\n", o, G.confname);
|
||||
printf("got %d options in '%s'\n", o, confname);
|
||||
green("And after reading of conffile:\n");
|
||||
buf = sl_print_opts(cmdlnopts, TRUE);
|
||||
buf = sl_print_opts(confopts, TRUE);
|
||||
printf("%s\n", buf);
|
||||
FREE(buf);
|
||||
}
|
||||
// if we want to re-read conffile many times over program runs, don't forget to `free` old arrays like this:
|
||||
if(G.dp){
|
||||
DBG("Clear double array");
|
||||
double **p = G.dp;
|
||||
while(*p){ FREE(*p); ++p; }
|
||||
FREE(G.dp);
|
||||
}
|
||||
if(G.sp){
|
||||
DBG("Clear string array %s", *G.sp);
|
||||
char **s = G.sp;
|
||||
while(*s){ FREE(*s); ++s; }
|
||||
FREE(G.sp);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -67,12 +67,6 @@ int main(int argc, char *argv[]){
|
||||
}
|
||||
sl_check4running((char*)__progname, GP->pidfile);
|
||||
red("%s started, snippets library version is %s\n", __progname, sl_libversion());
|
||||
sl_setup_con();
|
||||
signal(SIGTERM, signals); // kill (-15) - quit
|
||||
signal(SIGHUP, SIG_IGN); // hup - ignore
|
||||
signal(SIGINT, signals); // ctrl+C - quit
|
||||
signal(SIGQUIT, signals); // ctrl+\ - quit
|
||||
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
|
||||
if(GP->logfile) OPENLOG(GP->logfile, LOGLEVEL_ANY, 1);
|
||||
LOGMSG("Start application...");
|
||||
if(GP->rest_pars_num){
|
||||
@@ -89,11 +83,22 @@ int main(int argc, char *argv[]){
|
||||
}
|
||||
if(GP->strarr){
|
||||
char **p = GP->strarr;
|
||||
for(int i = 0; *p; ++i) printf("String[%d]: \"%s\"\n", i, *p++);
|
||||
for(int i = 0; *p; ++i){
|
||||
sl_remove_quotes(*p);
|
||||
printf("String[%d]: \"%s\"\n", i, *p++);
|
||||
}
|
||||
}
|
||||
if(GP->lo0 != INT_MIN) printf("You set lo0 to %d\n", GP->lo0);
|
||||
if(GP->lo1 != INT_MIN) printf("You set lo1 to %d\n", GP->lo1);
|
||||
if(GP->lo2 != INT_MIN) printf("You set lo2 to %d\n", GP->lo2);
|
||||
if(GP->so1){
|
||||
sl_remove_quotes(GP->so1);
|
||||
printf("String so1=%s\n", GP->so1);
|
||||
}
|
||||
if(GP->so2){
|
||||
sl_remove_quotes(GP->so2);
|
||||
printf("String so2=%s\n", GP->so2);
|
||||
}
|
||||
if(GP->device){
|
||||
LOGDBG("Try to open serial %s", GP->device);
|
||||
dev = sl_tty_new(GP->device, GP->speed, 4096);
|
||||
@@ -103,7 +108,13 @@ int main(int argc, char *argv[]){
|
||||
signals(0);
|
||||
}
|
||||
}
|
||||
|
||||
if(!dev) return 0;
|
||||
sl_setup_con();
|
||||
signal(SIGTERM, signals); // kill (-15) - quit
|
||||
signal(SIGHUP, SIG_IGN); // hup - ignore
|
||||
signal(SIGINT, signals); // ctrl+C - quit
|
||||
signal(SIGQUIT, signals); // ctrl+\ - quit
|
||||
signal(SIGTSTP, SIG_IGN); // ignore ctrl+Z
|
||||
// main stuff goes here
|
||||
long seed = sl_random_seed();
|
||||
green("Now I will sleep for 10 seconds after your last input.\n Do whatever you want. Random seed: %ld\n", seed);
|
||||
|
||||
Reference in New Issue
Block a user