From ac4694375eff4d1ef6dcb9e57a55a476f7d52359 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Fri, 3 Nov 2023 12:37:33 +0300 Subject: [PATCH] prepare to introduce output formats --- main.c | 14 +----------- string_functions.c | 33 ++++++++++++++++++++++------- string_functions.h | 1 + ttysocket.c | 53 ++++------------------------------------------ ttysocket.h | 2 -- 5 files changed, 31 insertions(+), 72 deletions(-) diff --git a/main.c b/main.c index dac50ca..1ded239 100644 --- a/main.c +++ b/main.c @@ -53,8 +53,6 @@ int main(int argc, char **argv){ } strcpy(conndev.eol, EOL); strcpy(conndev.seol, seol); - int eollen = strlen(EOL); - conndev.eollen = eollen; DBG("eol: %s, seol: %s", conndev.eol, conndev.seol); if(!G->ttyname){ WARNX("You should point name"); @@ -86,17 +84,7 @@ int main(int argc, char **argv){ int l; char *buf = ReadData(&l); if(buf && l > 0){ - char *eol = NULL, *estr = buf + l; - do{ - eol = strstr(buf, EOL); - if(eol){ - *eol = 0; - ShowData(buf); - buf = eol + eollen; - }else{ - ShowData(buf); - } - }while(eol && buf < estr); + ShowData(buf); }else if(l < 0){ pthread_mutex_unlock(&conndev.mutex); ERRX("Device disconnected"); diff --git a/string_functions.c b/string_functions.c index 624c2ed..ba9c219 100644 --- a/string_functions.c +++ b/string_functions.c @@ -17,14 +17,20 @@ */ #include +#include #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 -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){ char c = *line; - if(c > 31 && c < 127) break; + if(c > start && c < 127) break; ++line; } return line; @@ -163,7 +169,7 @@ int convert_and_send(disptype input_type, const char *line){ static char *buf = NULL; static size_t bufsiz = 0; size_t curpos = 0; // position in `buf` - line = omit_nonletters(line); + line = omit_nonletters(input_type, line); while(*line){ if(curpos >= bufsiz){ // out ouptut buffer can't be larger than input bufsiz += BUFSIZ; @@ -204,17 +210,28 @@ int convert_and_send(disptype input_type, const char *line){ return 0; // unknown display type } if(ch > -1) buf[curpos++] = ch; - line = omit_nonletters(line); + line = omit_nonletters(input_type, line); } // now insert EOL in text mode if(input_type == DISP_TEXT){ - if(curpos+2 >= bufsiz){ + if(curpos+eollen >= bufsiz){ bufsiz += BUFSIZ; buf = realloc(buf, bufsiz); } - int l; char *e = geteol(&l); - snprintf(buf+curpos, l, "%s", e); - curpos += l; + snprintf(buf+curpos, eollen+1, "%s", eol); + curpos += eollen; + /*snprintf(buf+curpos, 7, "_TEST_"); + curpos += 6;*/ } 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); +} diff --git a/string_functions.h b/string_functions.h index 60cc1e2..ce37776 100644 --- a/string_functions.h +++ b/string_functions.h @@ -21,3 +21,4 @@ #include "ncurses_and_readline.h" int convert_and_send(disptype input_type, const char *line); +void changeeol(const char *e); diff --git a/ttysocket.c b/ttysocket.c index ee4e8af..c9a87a5 100644 --- a/ttysocket.c +++ b/ttysocket.c @@ -29,6 +29,7 @@ #include // unix socket #include "dbg.h" +#include "string_functions.h" #include "ttysocket.h" static int sec = 0, usec = 100; // timeout @@ -77,45 +78,6 @@ static int waittoread(int fd){ 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 static char *getttydata(int *len){ if(!device || !device->dev) return NULL; @@ -138,15 +100,11 @@ static char *getttydata(int *len){ } ptr += l; L += l; length -= l; - if(L >= device->eollen && 0 == strcmp(&ptr[-(device->eollen)], device->eol)){ // found end of line - break; - } }while(length); D->buflen = L; D->buf[L] = 0; if(len) *len = L; if(!L) return NULL; - rmeols(device); DBG("buffer len: %zd, content: =%s=", D->buflen, D->buf); return D->buf; } @@ -163,7 +121,6 @@ static char *getsockdata(int *len){ ptr = D->buf; ptr[n] = 0; D->buflen = n; - n = rmeols(device); DBG("got %d: ..%s..", n, ptr); }else{ DBG("Got nothing"); @@ -389,7 +346,8 @@ static TTY_descr2* opentty(){ } descr->tty = descr->oldtty; 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_ispeed = device->speed; descr->tty.c_ospeed = device->speed; @@ -399,10 +357,6 @@ static TTY_descr2* opentty(){ } ioctl(descr->comfd, TCGETS2, &descr->tty); 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; someerr: FREE(descr->format); @@ -453,6 +407,7 @@ int opendev(chardevice *d, char *path){ return FALSE; } } + changeeol(device->eol); // allow string functions to know EOL return TRUE; } diff --git a/ttysocket.h b/ttysocket.h index 78448b6..c6f8a6c 100644 --- a/ttysocket.h +++ b/ttysocket.h @@ -53,10 +53,8 @@ typedef struct{ pthread_mutex_t mutex; // reading/writing mutex char eol[3]; // end of line char seol[5]; // `eol` with doubled backslash (for print @ screen) - int eollen; // length of `eol` } chardevice; -char *geteol(int *s); char *ReadData(int *l); int SendData(const char *data, size_t len); void settimeout(int tms);