diff options
-rw-r--r-- | instr-client.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/instr-client.c b/instr-client.c index 4106852..8a62f13 100644 --- a/instr-client.c +++ b/instr-client.c @@ -3,6 +3,7 @@ #include <string.h> #include <stdlib.h> #include <strings.h> +#include <sys/time.h> #include "response.h" #include "version.h" @@ -45,6 +46,8 @@ static gboolean Matches(char *src, char* dst, gssize size) return TRUE; } +#define TIMEOUT 100000 // 100000 useconds, 100ms +#define ONESECONDINUSEC 1000000 //GIOChannel callback static gboolean @@ -57,6 +60,43 @@ stdinAvailable (GIOChannel *source, GIOCondition condition, gpointer data) return TRUE; } + // Ignore second \n if two are sent in rapid succession. + // This happens when the serial input is terminated with \r\n, + // which getty translates to \n\n. The IO handler splits this + // into 2 input lines, so we can't use a simple regex to + // replace it. + + static struct timeval last = { 0, 0 }; // the time this last ran + static struct timeval now; // the time now + gettimeofday(&now, NULL ); + + if (last.tv_sec != 0) { // shouldn't be zero if we have been in here before + if (strcmp(tmp, "\n") == 0) { // is this a newline all on it's own? + if (last.tv_sec == now.tv_sec) { + // if we're in the same second we can just check the usec field + if (now.tv_usec - last.tv_usec < TIMEOUT) { + // ignore this \n + return TRUE; + } + } + // - check that last and now sec fields are only one second apart + // - check that the time elapsed in this second is smaller than the + // timeout, if it isn't we don't need to check the other second + // - check that there is less of the last second remaining than the timeout + else if (now.tv_sec - last.tv_sec == 1 && now.tv_usec < TIMEOUT && (ONESECONDINUSEC - last.tv_usec) < TIMEOUT) { + // and then check the the time left in the last second + // plus the time in this second is less than the timeout + if ((ONESECONDINUSEC - last.tv_usec) + now.tv_usec < TIMEOUT) { + // ignore this \n + return TRUE; + } + } + } + } + + last = now; + + //test if we have an exit word int idx=0; for(idx=0; idx<sizeof(exitTokens)/sizeof(exitTokens[0]); idx++) { |