version 0.2.1

This commit is contained in:
2024-11-15 11:42:29 +03:00
parent 511a83e506
commit e77fddc3b8
22 changed files with 1337 additions and 495 deletions

View File

@@ -9,3 +9,4 @@ link_libraries(usefull_macros)
add_executable(helloworld helloworld.c)
add_executable(options options.c cmdlnopts.c)
add_executable(fifo fifo.c)
add_executable(conffile conffile.c)

View File

@@ -38,6 +38,9 @@ static glob_pars G;
// DEFAULTS
// default global parameters
static glob_pars const Gdefault = {
.lo0 = INT_MIN,
.lo1 = INT_MIN,
.lo2 = INT_MIN,
.device = NULL,
.pidfile = DEFAULT_PIDFILE,
.speed = 9600,
@@ -48,16 +51,22 @@ static glob_pars const Gdefault = {
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
// common options
static sl_option_t cmdlnopts[] = {
{"lo0", NEED_ARG, NULL, 0, arg_int, APTR(&G.lo0), _("only long arg 0")},
{"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")},
{"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")},
end_option
// 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")},
end_option
};
/**
@@ -75,10 +84,10 @@ glob_pars *parse_args(int argc, char **argv){
char helpstring[1024], *hptr = helpstring;
snprintf(hptr, hlen, "Usage: %%s [args]\n\n\tWhere args are:\n");
// format of help: "Usage: progname [args]\n"
change_helpstring(helpstring);
sl_helpstring(helpstring);
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
sl_parseargs(&argc, &argv, cmdlnopts);
if(help) sl_showhelp(-1, cmdlnopts);
if(argc > 0){
G.rest_pars_num = argc;
G.rest_pars = MALLOC(char *, argc);

View File

@@ -31,8 +31,14 @@ typedef struct{
char *pidfile; // name of PID file
char *logfile; // logging to this file
int speed; // connection speed
int rest_pars_num; // number of rest parameters
int exclusive; // exclusive open port
int **intarr; // integer multopt
double **dblarr; // double -//-
char **strarr; // char -//-
int lo0; // only long options
int lo1;
int lo2;
int rest_pars_num; // number of rest parameters
char** rest_pars; // the rest parameters: array of char*
} glob_pars;

101
examples/conffile.c Normal file
View File

@@ -0,0 +1,101 @@
/*
* This file is part of the Snippets project.
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h> // NaN
#include <stdint.h>
#include <stdio.h>
#include "usefull_macros.h"
typedef struct{
char *sp1;
char *sp2;
int ip1;
int ip2;
double dp1;
double dp2;
float fp1;
float fp2;
int help;
int verbose;
char *confname;
} parameters;
static parameters G = {
.ip1 = INT_MIN,
.ip2 = INT_MIN,
.dp1 = NAN,
.dp2 = NAN,
.fp1 = NAN,
.fp2 = NAN
};
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"},
{"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
};
int main(int argc, char **argv){
sl_init();
sl_parseargs(&argc, &argv, cmdlnopts);
if(G.help) sl_showhelp(-1, cmdlnopts);
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);
}
}
green("Starting parameters values:\n");
char *buf = sl_print_opts(cmdlnopts, TRUE);
printf("%s\n", buf);
FREE(buf);
if(G.confname){
int o = sl_conf_readopts(G.confname, cmdlnopts);
if(o > 0){
printf("got %d options in '%s'\n", o, G.confname);
green("And after reading of conffile:\n");
buf = sl_print_opts(cmdlnopts, TRUE);
printf("%s\n", buf);
FREE(buf);
}
}
return 0;
}

View File

@@ -23,8 +23,8 @@
*/
int main(int argc, char *argv[argc]) {
List *f = NULL;
printf("Available memory: %luMB\n", get_available_mem()/1024/1024);
sl_list_t *f = NULL;
printf("Available memory: %luMB\n", sl_mem_avail()/1024/1024);
//initial_setup(); - there's no need for this function if you don't use locale & don't want to have
// specific output in non-tty
if(argc == 1){
@@ -33,26 +33,26 @@ int main(int argc, char *argv[argc]) {
}
red("\n\nLIFO example\n");
for(int i = 1; i < argc; ++i){
if(!list_push(&f, argv[i])) ERR("Allocation error!");
if(!sl_list_push(&f, argv[i])) ERR("Allocation error!");
green("push to list ");
printf("%s\n", argv[i]);
}
char *d;
printf("\n");
while(f){
d = list_pop(&f);
d = sl_list_pop(&f);
green("pull: ");
printf("%s\n", d);
}
red("\n\nFIFO example\n");
for(int i = 1; i < argc; ++i){
if(!list_push_tail(&f, argv[i])) ERR("Allocation error!");
if(!sl_list_push_tail(&f, argv[i])) ERR("Allocation error!");
green("push to list ");
printf("%s\n", argv[i]);
}
printf("\n");
while(f){
d = list_pop(&f);
d = sl_list_pop(&f);
green("pull: ");
printf("%s\n", d);
// after last usage we should FREE data, but here it is parameter of main()

View File

@@ -23,13 +23,13 @@
*/
int main(){
initial_setup();
sl_init();
// setup non-echo non-canonical mode
setup_con();
sl_setup_con();
green("Press any key...\n");
char k = mygetchar();
char k = sl_getchar();
red("You press %c\n", k);
// don't forget to restore @exit!
restore_console();
sl_restore_con();
return 0;
}

View File

@@ -24,20 +24,6 @@
#include <unistd.h> // sleep
#include <usefull_macros.h>
#include <termios.h> // tcsetattr
#include <unistd.h> // tcsetattr, close, read, write
#include <sys/ioctl.h> // ioctl
#include <stdio.h> // printf, getchar, fopen, perror
#include <stdlib.h> // exit
#include <sys/stat.h> // read
#include <fcntl.h> // read
#include <signal.h> // signal
#include <time.h> // time
#include <string.h> // memcpy
#include <stdint.h> // int types
#include <sys/time.h> // gettimeofday
/**
* This is an example of usage:
* - command line arguments,
@@ -48,7 +34,7 @@
* The `cmdlnopts.[hc]` are intrinsic files of this demo.
*/
static TTY_descr *dev = NULL; // shoul be global to restore if die
static sl_tty_t *dev = NULL; // shoul be global to restore if die
static glob_pars *GP = NULL; // for GP->pidfile need in `signals`
/**
@@ -62,28 +48,26 @@ void signals(int sig){
LOGERR("Exit with status %d", sig);
if(GP && GP->pidfile) // remove unnesessary PID file
unlink(GP->pidfile);
restore_console();
if(dev) close_tty(&dev);
sl_restore_con();
if(dev) sl_tty_close(&dev);
exit(sig);
}
void iffound_default(pid_t pid){
void sl_iffound_deflt(pid_t pid){
ERRX("Another copy of this process found, pid=%d. Exit.", pid);
}
int main(int argc, char *argv[]){
initial_setup();
char *self = strdup(argv[0]);
sl_init();
GP = parse_args(argc, argv);
if(GP->rest_pars_num){
printf("%d extra options:\n", GP->rest_pars_num);
for(int i = 0; i < GP->rest_pars_num; ++i)
printf("%s\n", GP->rest_pars[i]);
}
check4running(self, GP->pidfile);
red("%s started, snippets library version is %s\n", self, sl_libversion());
free(self);
setup_con();
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
@@ -95,10 +79,25 @@ int main(int argc, char *argv[]){
for(int i = 0; i < GP->rest_pars_num; ++i)
printf("Extra argument: %s\n", GP->rest_pars[i]);
}
if(GP->intarr){
int **p = GP->intarr;
for(int i = 0; *p; ++i) printf("Integer[%d]: %d\n", i, **p++);
}
if(GP->dblarr){
double **p = GP->dblarr;
for(int i = 0; *p; ++i) printf("Double[%d]: %g\n", i, **p++);
}
if(GP->strarr){
char **p = GP->strarr;
for(int i = 0; *p; ++i) 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->device){
LOGDBG("Try to open serial %s", GP->device);
dev = new_tty(GP->device, GP->speed, 4096);
if(dev) dev = tty_open(dev, GP->exclusive);
dev = sl_tty_new(GP->device, GP->speed, 4096);
if(dev) dev = sl_tty_open(dev, GP->exclusive);
if(!dev){
LOGERR("Can't open %s with speed %d. Exit.", GP->device, GP->speed);
signals(0);
@@ -106,26 +105,26 @@ int main(int argc, char *argv[]){
}
// main stuff goes here
long seed = throw_random_seed();
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);
LOGWARN("warning message example");
LOGWARNADD("with next string without timestamp");
double t0 = dtime();
double t0 = sl_dtime();
char b[2] = {0};
while(dtime() - t0 < 10.){ // read data from port and print in into terminal
while(sl_dtime() - t0 < 10.){ // read data from port and print in into terminal
if(dev){
if(read_tty(dev)){
if(sl_tty_read(dev)){
printf("Got %zd bytes from port: %s\n", dev->buflen, dev->buf);
LOGMSG("Got from serial: %s", dev->buf);
t0 = dtime();
t0 = sl_dtime();
}
int r = read_console();
int r = sl_read_con();
if(r < 1) continue;
t0 = dtime();
t0 = sl_dtime();
b[0] = (char) r;
printf("send to tty: %d (%c)\n", r, b[0]);
LOGMSG("send to tty: %d (%c)\n", r, b[0]);
write_tty(dev->comfd, b, 1);
sl_tty_write(dev->comfd, b, 1);
}
}
// clean everything