prepare to introduce output formats

This commit is contained in:
Edward Emelianov 2023-11-03 12:37:33 +03:00
parent 84bfe8b499
commit ac4694375e
5 changed files with 31 additions and 72 deletions

14
main.c
View File

@ -53,8 +53,6 @@ int main(int argc, char **argv){
} }
strcpy(conndev.eol, EOL); strcpy(conndev.eol, EOL);
strcpy(conndev.seol, seol); strcpy(conndev.seol, seol);
int eollen = strlen(EOL);
conndev.eollen = eollen;
DBG("eol: %s, seol: %s", conndev.eol, conndev.seol); DBG("eol: %s, seol: %s", conndev.eol, conndev.seol);
if(!G->ttyname){ if(!G->ttyname){
WARNX("You should point name"); WARNX("You should point name");
@ -86,17 +84,7 @@ int main(int argc, char **argv){
int l; int l;
char *buf = ReadData(&l); char *buf = ReadData(&l);
if(buf && l > 0){ if(buf && l > 0){
char *eol = NULL, *estr = buf + l; ShowData(buf);
do{
eol = strstr(buf, EOL);
if(eol){
*eol = 0;
ShowData(buf);
buf = eol + eollen;
}else{
ShowData(buf);
}
}while(eol && buf < estr);
}else if(l < 0){ }else if(l < 0){
pthread_mutex_unlock(&conndev.mutex); pthread_mutex_unlock(&conndev.mutex);
ERRX("Device disconnected"); ERRX("Device disconnected");

View File

@ -17,14 +17,20 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "string_functions.h" #include "string_functions.h"
// end of line - for text mode
static char *eol = "\n";
static int eollen = 1;
// read text string and throw out all < 31 and > 126 // read text string and throw out all < 31 and > 126
static inline const char *omit_nonletters(const char *line){ static inline const char *omit_nonletters(disptype input_type, const char *line){
int start = (input_type == DISP_TEXT) ? 31 : 32; // remove spaces for RAW and HEX modes
while(*line){ while(*line){
char c = *line; char c = *line;
if(c > 31 && c < 127) break; if(c > start && c < 127) break;
++line; ++line;
} }
return line; return line;
@ -163,7 +169,7 @@ int convert_and_send(disptype input_type, const char *line){
static char *buf = NULL; static char *buf = NULL;
static size_t bufsiz = 0; static size_t bufsiz = 0;
size_t curpos = 0; // position in `buf` size_t curpos = 0; // position in `buf`
line = omit_nonletters(line); line = omit_nonletters(input_type, line);
while(*line){ while(*line){
if(curpos >= bufsiz){ // out ouptut buffer can't be larger than input if(curpos >= bufsiz){ // out ouptut buffer can't be larger than input
bufsiz += BUFSIZ; bufsiz += BUFSIZ;
@ -204,17 +210,28 @@ int convert_and_send(disptype input_type, const char *line){
return 0; // unknown display type return 0; // unknown display type
} }
if(ch > -1) buf[curpos++] = ch; if(ch > -1) buf[curpos++] = ch;
line = omit_nonletters(line); line = omit_nonletters(input_type, line);
} }
// now insert EOL in text mode // now insert EOL in text mode
if(input_type == DISP_TEXT){ if(input_type == DISP_TEXT){
if(curpos+2 >= bufsiz){ if(curpos+eollen >= bufsiz){
bufsiz += BUFSIZ; bufsiz += BUFSIZ;
buf = realloc(buf, bufsiz); buf = realloc(buf, bufsiz);
} }
int l; char *e = geteol(&l); snprintf(buf+curpos, eollen+1, "%s", eol);
snprintf(buf+curpos, l, "%s", e); curpos += eollen;
curpos += l; /*snprintf(buf+curpos, 7, "_TEST_");
curpos += 6;*/
} }
return SendData(buf, curpos); return SendData(buf, curpos);
} }
/**
* @brief changeeol - set EOL to given
* @param e - new end of line for text mode
* WARNING! This function doesn't call free(), so don't call it more than once
*/
void changeeol(const char *e){
eollen = strlen(e);
eol = strdup(e);
}

View File

@ -21,3 +21,4 @@
#include "ncurses_and_readline.h" #include "ncurses_and_readline.h"
int convert_and_send(disptype input_type, const char *line); int convert_and_send(disptype input_type, const char *line);
void changeeol(const char *e);

View File

@ -29,6 +29,7 @@
#include <sys/un.h> // unix socket #include <sys/un.h> // unix socket
#include "dbg.h" #include "dbg.h"
#include "string_functions.h"
#include "ttysocket.h" #include "ttysocket.h"
static int sec = 0, usec = 100; // timeout static int sec = 0, usec = 100; // timeout
@ -77,45 +78,6 @@ static int waittoread(int fd){
return 0; return 0;
} }
// substitute all EOL's by '\n'
static size_t rmeols(){
if(!device){
DBG("d is NULL");
return 0;
}
TTY_descr2 *D = device->dev;
if(!D || D->comfd < 0){
DBG("D bad");
return 0;
}
if(0 == strcmp(device->eol, "\n")){
DBG("No subs need");
return D->buflen; // don't need to do this
}
int L = strlen(D->buf);
char *newbuf = MALLOC(char, L), *ptr = D->buf, *eptr = D->buf + L;
while(ptr < eptr){
char *eol = strstr(ptr, device->eol);
if(eol){
eol[0] = '\n';
eol[1] = 0;
}
strcat(newbuf, ptr);
if(!eol) break;
ptr = eol + device->eollen;
}
strcpy(D->buf, newbuf);
FREE(newbuf);
D->buflen = strlen(D->buf);
return D->buflen;
}
char *geteol(int *s){
if(!device) return NULL;
*s = device->eollen;
return device->eol;
}
// get data drom TTY // get data drom TTY
static char *getttydata(int *len){ static char *getttydata(int *len){
if(!device || !device->dev) return NULL; if(!device || !device->dev) return NULL;
@ -138,15 +100,11 @@ static char *getttydata(int *len){
} }
ptr += l; L += l; ptr += l; L += l;
length -= l; length -= l;
if(L >= device->eollen && 0 == strcmp(&ptr[-(device->eollen)], device->eol)){ // found end of line
break;
}
}while(length); }while(length);
D->buflen = L; D->buflen = L;
D->buf[L] = 0; D->buf[L] = 0;
if(len) *len = L; if(len) *len = L;
if(!L) return NULL; if(!L) return NULL;
rmeols(device);
DBG("buffer len: %zd, content: =%s=", D->buflen, D->buf); DBG("buffer len: %zd, content: =%s=", D->buflen, D->buf);
return D->buf; return D->buf;
} }
@ -163,7 +121,6 @@ static char *getsockdata(int *len){
ptr = D->buf; ptr = D->buf;
ptr[n] = 0; ptr[n] = 0;
D->buflen = n; D->buflen = n;
n = rmeols(device);
DBG("got %d: ..%s..", n, ptr); DBG("got %d: ..%s..", n, ptr);
}else{ }else{
DBG("Got nothing"); DBG("Got nothing");
@ -389,7 +346,8 @@ static TTY_descr2* opentty(){
} }
descr->tty = descr->oldtty; descr->tty = descr->oldtty;
descr->tty.c_lflag = 0; // ~(ICANON | ECHO | ECHOE | ISIG) descr->tty.c_lflag = 0; // ~(ICANON | ECHO | ECHOE | ISIG)
descr->tty.c_iflag = 0; descr->tty.c_iflag = 0; // don't do any changes in input stream
descr->tty.c_oflag = 0; // don't do any changes in output stream
descr->tty.c_cflag = BOTHER | flags |CREAD|CLOCAL; descr->tty.c_cflag = BOTHER | flags |CREAD|CLOCAL;
descr->tty.c_ispeed = device->speed; descr->tty.c_ispeed = device->speed;
descr->tty.c_ospeed = device->speed; descr->tty.c_ospeed = device->speed;
@ -399,10 +357,6 @@ static TTY_descr2* opentty(){
} }
ioctl(descr->comfd, TCGETS2, &descr->tty); ioctl(descr->comfd, TCGETS2, &descr->tty);
device->speed = descr->tty.c_ispeed; device->speed = descr->tty.c_ispeed;
#ifdef EBUG
printf("ispeed: %d, ospeed: %d, cflag=%d (BOTHER=%d)\n", descr->tty.c_ispeed, descr->tty.c_ospeed, descr->tty.c_cflag&CBAUD, BOTHER);
if(system("stty -F /dev/ttyUSB0")) WARN("system()");
#endif
return descr; return descr;
someerr: someerr:
FREE(descr->format); FREE(descr->format);
@ -453,6 +407,7 @@ int opendev(chardevice *d, char *path){
return FALSE; return FALSE;
} }
} }
changeeol(device->eol); // allow string functions to know EOL
return TRUE; return TRUE;
} }

View File

@ -53,10 +53,8 @@ typedef struct{
pthread_mutex_t mutex; // reading/writing mutex pthread_mutex_t mutex; // reading/writing mutex
char eol[3]; // end of line char eol[3]; // end of line
char seol[5]; // `eol` with doubled backslash (for print @ screen) char seol[5]; // `eol` with doubled backslash (for print @ screen)
int eollen; // length of `eol`
} chardevice; } chardevice;
char *geteol(int *s);
char *ReadData(int *l); char *ReadData(int *l);
int SendData(const char *data, size_t len); int SendData(const char *data, size_t len);
void settimeout(int tms); void settimeout(int tms);