fixed bug with EOLs

This commit is contained in:
Edward Emelianov 2022-02-02 17:06:11 +03:00
parent 4d3300f623
commit 0175cb4a8d
4 changed files with 59 additions and 18 deletions

6
.gitignore vendored
View File

@ -14,10 +14,10 @@
*.so *.so
*.so.* *.so.*
# qt-creator # Qt creator
*.config *.config
*.cflags
*.cxxflags
*.creator* *.creator*
*.files *.files
*.includes *.includes
*.cflags
*.cxxflags

View File

@ -19,7 +19,7 @@ set(CMAKE_COLOR_MAKEFILE ON)
# here is one of two variants: all .c in directory or .c files in list # here is one of two variants: all .c in directory or .c files in list
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SOURCES) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SOURCES)
# cmake -DDEBUG=1 -> debugging # cmake -DEBUG=1 -> debugging
if(DEFINED EBUG) if(DEFINED EBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra -Wall -Werror -W") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra -Wall -Werror -W")

View File

@ -147,8 +147,12 @@ static void show_mode(bool for_resize){
dtty->name, dtty->seol, dtty->port); dtty->name, dtty->seol, dtty->port);
break; break;
case DEV_UNIXSOCKET: case DEV_UNIXSOCKET:
snprintf(buf, 127, "INSERT (TAB to switch, ctrl+D to quit) HOST: %s, ENDLINE: %s, PATH: %s", if(*dtty->name)
dtty->name, dtty->seol, dtty->port); snprintf(buf, 127, "INSERT (TAB to switch, ctrl+D to quit) PATH: %s, ENDLINE: %s",
dtty->name, dtty->seol);
else // name starting from \0
snprintf(buf, 127, "INSERT (TAB to switch, ctrl+D to quit) PATH: \\0%s, ENDLINE: %s",
dtty->name+1, dtty->seol);
break; break;
case DEV_TTY: case DEV_TTY:
snprintf(buf, 127, "INSERT (TAB to switch, ctrl+D to quit) DEV: %s, ENDLINE: %s, SPEED: %d", snprintf(buf, 127, "INSERT (TAB to switch, ctrl+D to quit) DEV: %s, ENDLINE: %s, SPEED: %d",

View File

@ -72,11 +72,40 @@ static int waittoread(int fd){
return 0; return 0;
} }
// substitute all EOL's by '\n'
static size_t rmeols(chardevice *d){
if(!d) return 0;
TTY_descr *D = d->dev;
if(!D || D->comfd < 0) return 0;
if(0 == strcmp(d->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, d->eol);
if(eol){
eol[0] = '\n';
eol[1] = 0;
}
strcat(newbuf, ptr);
if(!eol) break;
ptr = eol + d->eollen;
}
strcpy(D->buf, newbuf);
FREE(newbuf);
D->buflen = strlen(D->buf);
return D->buflen;
}
// get data drom TTY // get data drom TTY
static char *getttydata(TTY_descr *D, int *len){ static char *getttydata(chardevice *d, int *len){
if(!D || D->comfd < 0) return NULL; if(!d || !d->dev) return NULL;
size_t L = 0; TTY_descr *D = d->dev;
size_t length = D->bufsz; if(D->comfd < 0) return NULL;
int L = 0;
int length = D->bufsz;
char *ptr = D->buf; char *ptr = D->buf;
int s = 0; int s = 0;
do{ do{
@ -85,22 +114,29 @@ static char *getttydata(TTY_descr *D, int *len){
if(len) *len = 0; if(len) *len = 0;
return NULL; return NULL;
} }
ssize_t l = read(D->comfd, ptr, length); int l = read(D->comfd, ptr, length);
if(l < 1){ // disconnected if(l < 1){ // disconnected
if(len) *len = -1; if(len) *len = -1;
return NULL; return NULL;
} }
ptr += l; L += l; ptr += l; L += l;
length -= l; length -= l;
if(L >= d->eollen && 0 == strcmp(&ptr[-(d->eollen)], d->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;
rmeols(d);
return D->buf; return D->buf;
} }
static char *getsockdata(TTY_descr *D, int *len){ static char *getsockdata(chardevice *d, int *len){
if(!D || D->comfd < 0) return NULL; if(!d || !d->dev) return NULL;
TTY_descr *D = d->dev;
if(D->comfd < 0) return NULL;
char *ptr = NULL; char *ptr = NULL;
int n = waittoread(D->comfd); int n = waittoread(D->comfd);
if(n == 1){ if(n == 1){
@ -108,7 +144,8 @@ static char *getsockdata(TTY_descr *D, int *len){
if(n > 0){ if(n > 0){
ptr = D->buf; ptr = D->buf;
ptr[n] = 0; ptr[n] = 0;
DBG("got %d: %s", n, ptr); n = rmeols(d);
DBG("got %d: ..%s..", n, ptr);
}else{ }else{
DBG("Got nothing"); DBG("Got nothing");
n = -1; n = -1;
@ -130,12 +167,11 @@ char *ReadData(chardevice *d, int *len){
char *r = NULL; char *r = NULL;
switch(d->type){ switch(d->type){
case DEV_TTY: case DEV_TTY:
if(!d || !d->dev) return NULL; r = getttydata(d, len);
r = getttydata(d->dev, len);
break; break;
case DEV_NETSOCKET: case DEV_NETSOCKET:
case DEV_UNIXSOCKET: case DEV_UNIXSOCKET:
r = getsockdata(d->dev, len); r = getsockdata(d, len);
break; break;
default: default:
break; break;
@ -159,10 +195,11 @@ int SendData(chardevice *d, char *str){
if(!str) return 0; if(!str) return 0;
int ret = 0; int ret = 0;
if(0 == pthread_mutex_lock(&d->mutex)){ if(0 == pthread_mutex_lock(&d->mutex)){
int l = strlen(str), lplus = l + d->eollen + 1; int l = strlen(str), lplus = l + d->eollen;
if(l < 1) return 0; if(l < 1) return 0;
if(lplus > BUFSIZ-1) lplus = BUFSIZ-1; if(lplus > BUFSIZ-1) lplus = BUFSIZ-1;
snprintf(buf, lplus+1, "%s%s", str, d->eol); snprintf(buf, lplus+1, "%s%s", str, d->eol);
DBG("SENDBUF (%d): _%s_", lplus, buf);
switch(d->type){ switch(d->type){
case DEV_TTY: case DEV_TTY:
if(write_tty(d->dev->comfd, buf, lplus)) ret = 0; if(write_tty(d->dev->comfd, buf, lplus)) ret = 0;