add fifo/lifo

This commit is contained in:
eddyem
2018-12-17 23:43:53 +03:00
parent bed730e245
commit e982f06ca1
13 changed files with 752 additions and 177 deletions

View File

@@ -8,3 +8,4 @@ link_libraries(usefull_macros)
# exe list
add_executable(helloworld helloworld.c)
add_executable(options options.c cmdlnopts.c)
add_executable(fifo fifo.c)

View File

@@ -32,15 +32,15 @@
int help;
glob_pars G;
#define DEFAULT_COMDEV "/dev/ttyUSB0"
// default PID filename:
#define DEFAULT_PIDFILE "/tmp/testcmdlnopts.pid"
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.device = DEFAULT_COMDEV,
.device = NULL,
.pidfile = DEFAULT_PIDFILE,
.speed = 9600,
.logfile = NULL // don't save logs
};
@@ -51,7 +51,8 @@ glob_pars const Gdefault = {
myoption cmdlnopts[] = {
// common options
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("serial device name (default: " DEFAULT_COMDEV ")")},
{"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("serial device name")},
{"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 ")")},
end_option

View File

@@ -30,6 +30,7 @@ typedef struct{
char *device; // serial device name
char *pidfile; // name of PID file
char *logfile; // logging to this file
int speed; // connection speed
int rest_pars_num; // number of rest parameters
char** rest_pars; // the rest parameters: array of char*
} glob_pars;

57
examples/fifo.c Normal file
View File

@@ -0,0 +1,57 @@
/*
* This file is part of the usefull_macros project.
* Copyright 2018 Edward V. Emelianov <edward.emelianoff@gmail.com>, <eddy@sao.ru>.
*
* 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 <usefull_macros.h>
#include <stdio.h>
int main(int argc, char *argv[argc]) {
List *f = NULL;
printf("Available memory: %luMB\n", get_available_mem()/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){
green("Usage:\n\t%s args - fill fifo with arguments\n", __progname);
return 1;
}
red("\n\nLIFO example\n");
for(int i = 1; i < argc; ++i){
if(!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);
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!");
green("push to list ");
printf("%s\n", argv[i]);
}
printf("\n");
while(f){
d = list_pop(&f);
green("pull: ");
printf("%s\n", d);
// after last usage we should FREE data, but here it is parameter of main()
}
return 0;
}

View File

@@ -16,12 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <usefull_macros.h>
#include <signal.h> // signal
#include <stdlib.h> // exit, free
#include <stdio.h> // printf
#include <string.h> // strdup
#include <unistd.h> // sleep
#include <usefull_macros.h>
#include "cmdlnopts.h"
/**
@@ -34,15 +34,22 @@
* The `cmdlnopts.[hc]` are intrinsic files of this demo.
*/
TTY_descr *dev = NULL; // shoul be global to restore if die
glob_pars *GP = NULL; // for GP->pidfile need in `signals`
/**
* We REDEFINE the default WEAK function of signal processing
*/
void signals(int sig){
signal(sig, SIG_IGN);
restore_console();
restore_tty();
DBG("Get signal %d, quit.\n", sig);
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
}
putlog("Exit with status %d", sig);
if(GP->pidfile) // remove unnesessary PID file
unlink(GP->pidfile);
restore_console();
close_tty(&dev);
exit(sig);
}
@@ -53,7 +60,7 @@ void iffound_default(pid_t pid){
int main(int argc, char *argv[]){
initial_setup();
char *self = strdup(argv[0]);
glob_pars *GP = parse_args(argc, argv);
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)
@@ -69,13 +76,32 @@ int main(int argc, char *argv[]){
if(GP->logfile) openlogfile(GP->logfile);
setup_con();
putlog(("Start application..."));
; // main stuff goes here
green("Now I will sleep for 10 seconds. Do whatever you want.\n");
sleep(10);
; // clean everything
if(GP->pidfile) // remove unnesessary PID file
unlink(GP->pidfile);
restore_console();
restore_tty();
if(GP->rest_pars_num){
for(int i = 0; i < GP->rest_pars_num; ++i)
printf("Extra argument: %s\n", GP->rest_pars[i]);
}
if(GP->device){
putlog("Try to open serial %s", GP->device);
dev = tty_open(GP->device, GP->speed, 256);
if(!dev){
putlog("Can't open %s with speed %d. Exit.", GP->device, GP->speed);
signals(0);
}
}
// main stuff goes here
long seed = throw_random_seed();
green("Now I will sleep for 10 seconds. Do whatever you want. Random seed: %ld\n", seed);
double t0 = dtime();
while(dtime() - t0 < 10.){ // read data from port and print in into terminal
if(dev){
if(read_tty(dev)){
printf("Data from port: %s\n", dev->buf);
}
char ch = read_console();
if(ch) write_tty(dev->comfd, &ch, 1);
}
}
// clean everything
signals(0);
return 0;
}