fix readline handline

This commit is contained in:
Olaf Rempel 2007-04-30 19:06:17 +02:00
parent a15af2d522
commit 65b85824ed
3 changed files with 72 additions and 56 deletions

View File

@ -36,7 +36,7 @@ static int open_serial(struct context *ctx, const char *devname)
}
bzero(&sdev->newtio, sizeof(struct termios));
sdev->newtio.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
sdev->newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
tcflush(ctx->fd, TCIOFLUSH);

View File

@ -41,16 +41,18 @@ enum {
static int my_readline(int fd, struct context *ctx, char **retval)
{
/* TODO: not tested */
char *newline = memchr(ctx->linebuf, '\r', ctx->linepos);
if (newline != NULL) {
int len = newline - ctx->linebuf;
*newline = '\0';
if (len > 0)
*retval = strdup(ctx->linebuf);
ctx->linepos -= (newline - ctx->linebuf);
ctx->linepos -= len +1;
memmove(ctx->linebuf, newline +1, ctx->linepos);
return 0;
newline = memchr(ctx->linebuf, '\r', ctx->linepos);
return ((*retval != NULL) ? 1 : 0) + ((newline != NULL) ? 1 : 0);
}
char buf[32];
@ -58,27 +60,34 @@ static int my_readline(int fd, struct context *ctx, char **retval)
if (len <= 0)
return -1;
int i;
int i, numlines = 0;
for (i = 0; i < len; i++) {
/* "understand" backspace */
if (buf[i] == 0x08 && ctx->linepos > 0) {
ctx->linepos--;
/* export buffer */
} else if (buf[i] == '\r' && *retval == NULL) {
} else if (buf[i] == '\r') {
if (*retval == NULL) {
ctx->linebuf[ctx->linepos] = '\0';
if (ctx->linepos > 0)
*retval = strdup(ctx->linebuf);
ctx->linepos = 0;
numlines = 1;
} else {
ctx->linebuf[ctx->linepos++] = buf[i];
numlines++;
}
/* copy */
} else if (buf[i] >= ' ') {
ctx->linebuf[ctx->linepos++] = buf[i];
}
}
return 0;
return numlines;
}
int statemachine_read(int fd, void *privdata)
@ -92,8 +101,11 @@ int statemachine_read(int fd, void *privdata)
return 0;
}
int numlines;
do {
char *line = NULL;
if (my_readline(fd, ctx, &line) < 0)
numlines = my_readline(fd, ctx, &line);
if (numlines < 0)
return -1;
if (line == NULL)
@ -129,6 +141,7 @@ int statemachine_read(int fd, void *privdata)
/* follow device to high baudrate */
} else if (msg == MSG_BAUD_HIGH) {
ctx->dev_setbaudrate(ctx, B115200);
numlines = 0;
ctx->state = STATE_XMODEM;
write(fd, "ATLC\r\n", 6);
@ -141,8 +154,11 @@ int statemachine_read(int fd, void *privdata)
/* follow device to low baudrate */
} else if (msg == MSG_BAUD_LOW) {
ctx->dev_setbaudrate(ctx, B9600);
numlines = 0;
}
free(line);
} while (numlines > 1);
return 0;
}

View File

@ -1,5 +1,5 @@
[global]
configdata 350LI2C1.rom.own
configdata 350LI2C1.rom
[ports]
serial /dev/ttyUSB0
serial /dev/ttyS0