From: Jean-Marc Pigeon (Delson) Date: Sat, 3 Aug 2024 13:15:09 +0000 (-0400) Subject: readline is working using a timeout X-Git-Tag: tag-0.4~24 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=eed035571a07ea034dd3f4013afcffdac52ce0c0;p=jmp%2Fmailleur readline is working using a timeout --- diff --git a/lib/Makefile b/lib/Makefile index eec4efb..34f5988 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -37,7 +37,6 @@ modrec.o: \ modrec.h modrec.c gestcp.o: \ - subrou.h \ unieml.h \ uniprc.h \ unisig.h \ @@ -67,6 +66,7 @@ subrou.o: \ subrou.h subrou.c gestcp.h: \ + subrou.h \ devsoc.h \ uniprc.h: \ diff --git a/lib/gestcp.c b/lib/gestcp.c index 20bb4ee..59e6203 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -9,12 +9,12 @@ #include #include #include +#include #include #include #include #include -#include "subrou.h" #include "unieml.h" #include "unisig.h" #include "uniprc.h" @@ -111,11 +111,72 @@ return contact; */ /********************************************************/ /* */ +/* Procedure to check if char are available on */ +/* soc interface. */ +/* comming from the remote end. */ +/* */ +/********************************************************/ +static int waitforchar(CONTYP *tact,TIMESPEC *attend) + +{ +struct pollfd polling[1]; + +polling[0].events=POLLIN|POLLPRI; +polling[0].revents=(short)0; +polling[0].fd=tact->channel; +return ppoll(polling,1,attend,(sigset_t *)0); +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to read the maximun of characters */ +/* comming from the remote end. */ +/* */ +/********************************************************/ +static void readmaxchar(CONTYP *tact) + +{ +int limit; + +limit=(tact->maxcarin-tact->carin)-1; +if (limit>0) { + int got; + + got=recv(tact->channel,tact->carpile+tact->carin,limit,MSG_DONTWAIT); + (void) printf("readmax got '%d' char\n",got); + switch (got) { + case -1 : //do not block + if (errno==EWOULDBLOCK) + errno=EAGAIN; + switch (errno) { + case EAGAIN : //no char available + break; + default : + (void) fprintf(stderr,"JMPDBG readmax error=<%s>\n",strerror(errno)); + break; + } + break; + case 0 : //EOL? + break; + default : //we got som char from remote + tact->carin+=got; //managing carpile + tact->carpile[tact->carin]='\000'; + break; + } + } +} +/* +^L +*/ +/********************************************************/ +/* */ /* Procedure to locate CRLF within the current line*/ /* assigne memory and store carpile contents */ /* */ /********************************************************/ -PUBLIC int getnextline(CONTYP *tact,char **lineptr) +static int getnextline(CONTYP *tact,char **lineptr) { int got; @@ -144,11 +205,12 @@ while (proceed==true) { *eol='\000'; (void) strcpy(*lineptr,tact->carpile); (void) strcat(*lineptr,tact->EOL); + got=strlen(*lineptr); break; case 3 : //managing carpile tact->carin-=strlen(*lineptr); if (tact->carin>0) - (void) memove(tact->carpile,tact->carpile+strlen(*lineptr),tact->carin); + (void) memmove(tact->carpile,tact->carpile+strlen(*lineptr),tact->carin); tact->carpile[tact->carin]='\000'; break; default : //SAFE guard @@ -168,7 +230,7 @@ return got; /* for a CRLF AS EOL. */ /* */ /********************************************************/ -PUBLIC int tcp_getline(CONTYP *contact,u_long usec,char **lineptr) +PUBLIC int tcp_getline(CONTYP *contact,TIMESPEC *attend,char **lineptr) { #define OPEP "gestcp.c:tcp_getline" @@ -193,7 +255,7 @@ while (proceed==true) { phase=999; //we got a line. break; case 2 : //waiting for new character presence - switch (waitforchar(contact,&usec)) { + switch (waitforchar(contact,attend)) { case -1 : //trouble? signal? case 0 : //normal time out break; //no need to read line diff --git a/lib/gestcp.h b/lib/gestcp.h index c06b300..2191062 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -9,6 +9,7 @@ #include +#include "subrou.h" #include "devsoc.h" typedef struct { @@ -23,7 +24,7 @@ typedef struct { }CONTYP; //read a line from contact up to CRLF -extern int tcp_getline(CONTYP *contact,u_long usec,char **lineptr); +extern int tcp_getline(CONTYP *contact,TIMESPEC *attend,char **lineptr); //Transmit formated data to the contact channel extern int tcp_write(CONTYP *contact,char *buffer,int parnum); diff --git a/lib/modrec.c b/lib/modrec.c index 7f67220..162c088 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -13,6 +13,7 @@ #include "subrou.h" #include "uniprc.h" +#include "unieml.h" #include "unisig.h" #include "devsoc.h" #include "gestcp.h" @@ -35,6 +36,7 @@ void docontact(SOCPTR *socptr,int pos) #define TESTL 8 CONTYP *contact; + int phase; _Bool proceed; @@ -53,18 +55,25 @@ while (proceed==true) { case 2 : //do contact printf("Client channel=%d (pid=%d)\n",contact->channel,getpid()); for (int i=0;i\n",got,line); + if (strcasecmp(line,"QUIT"CRLF)==0) { + (void) printf("remote quit\n"); + break; //remote disconnect } - (void) sleep(1); + (void) tcp_write(contact,line,strlen(line)); + (void) free(line); } break; case 3 : //connection terminated diff --git a/lib/subrou.c b/lib/subrou.c index 290f7e4..ceb2751 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -20,7 +20,7 @@ //version definition #define VERSION "0.3" -#define RELEASE "17" +#define RELEASE "18" //Public variables PUBLIC int debug=0; //debug level diff --git a/lib/subrou.h b/lib/subrou.h index 96d425a..7efcd65 100644 --- a/lib/subrou.h +++ b/lib/subrou.h @@ -16,6 +16,8 @@ typedef void (*freehandler_t)(void *); +typedef struct timespec TIMESPEC; + //--- Variables defined within subrou.c --------- extern int debug; //application debug mode extern _Bool foreground; //process is in foreground mode