(servercmd, io_gets, output): Fix client to read lines longer than

1024 bytes from the server.  Closes #1223255.
This commit is contained in:
Ron Koenderink 2005-11-12 14:20:46 +00:00
parent 0b15696223
commit 89e2b3b481
3 changed files with 34 additions and 21 deletions

View file

@ -111,23 +111,31 @@ ioq_drain(struct ioqueue *ioq)
} }
char * char *
ioq_gets(struct ioqueue *ioq, char *buf, int cc) ioq_gets(struct ioqueue *ioq, char *buf, int cc, int *eol)
{ {
char *p; char *p;
char *end; char *end;
int nbytes; int nbytes;
*eol = 0;
cc--;
nbytes = ioqtobuf(ioq, buf, cc); nbytes = ioqtobuf(ioq, buf, cc);
if (nbytes < cc) end = &buf[nbytes];
cc = nbytes;
end = &buf[cc];
for (p = buf; p < end && *p; p++) { for (p = buf; p < end && *p; p++) {
if (*p == '\n') { if (*p == '\n') {
*p = '\0'; *p = '\0';
*eol = 1;
dequeuecc(ioq, (p - buf) + 1); dequeuecc(ioq, (p - buf) + 1);
return buf; return buf;
} }
} }
if (cc && (p - buf) == cc) {
dequeuecc(ioq, cc);
buf[cc] = '\0';
return buf;
}
return NULL; return NULL;
} }

View file

@ -52,4 +52,4 @@ int ioq_read(struct ioqueue *ioq, char *buf, int cc);
void ioq_write(struct ioqueue *ioq, char *buf, int cc); void ioq_write(struct ioqueue *ioq, char *buf, int cc);
int ioq_qsize(struct ioqueue *ioq); int ioq_qsize(struct ioqueue *ioq);
void ioq_drain(struct ioqueue *ioq); void ioq_drain(struct ioqueue *ioq);
char *ioq_gets(struct ioqueue *ioq, char *buf, int cc); char *ioq_gets(struct ioqueue *ioq, char *buf, int cc, int *eol);

View file

@ -62,7 +62,7 @@ static void prompt(FILE *auxfi);
static void doredir(char *p); static void doredir(char *p);
static void dopipe(char *p); static void dopipe(char *p);
static void doexecute(char *p, FILE *auxfi); static void doexecute(char *p, FILE *auxfi);
static void output(int code, char *buf, FILE *auxfi); static void output(int code, char *buf, FILE *auxfi, int eol);
static void screen(char *buf); static void screen(char *buf);
void void
@ -70,17 +70,20 @@ servercmd(struct ioqueue *ioq, FILE *auxfi)
{ {
char buf[1024]; char buf[1024];
char *p; char *p;
int code; static int code = -1;
int eol;
while (ioq_gets(ioq, buf, sizeof(buf))) { while (ioq_gets(ioq, buf, sizeof(buf), &eol)) {
p = buf; p = buf;
while (*p && !isspace(*p)) if (code == -1) {
p++;
*p++ = 0;
if (isalpha(*buf)) if (isalpha(*buf))
code = 10 + (*buf - 'a'); code = 10 + (*buf - 'a');
else else
code = *buf - '0'; code = *buf - '0';
while (*p && !isspace(*p))
p++;
*p++ = 0;
}
switch (code) { switch (code) {
case C_PROMPT: case C_PROMPT:
if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) { if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
@ -116,9 +119,11 @@ servercmd(struct ioqueue *ioq, FILE *auxfi)
*num_teles = '\0'; *num_teles = '\0';
break; break;
default: default:
output(code, p, auxfi); output(code, p, auxfi, eol);
break; break;
} }
if (eol)
code = -1;
} }
} }
@ -270,7 +275,7 @@ doexecute(char *p, FILE *auxfi)
} }
static void static void
output(int code, char *buf, FILE *auxfi) output(int code, char *buf, FILE *auxfi, int eol)
{ {
switch (code) { switch (code) {
case C_NOECHO: case C_NOECHO:
@ -307,19 +312,19 @@ output(int code, char *buf, FILE *auxfi)
fprintf(auxfi, "%s", buf); fprintf(auxfi, "%s", buf);
if (code == C_FLUSH) if (code == C_FLUSH)
(void)fflush(auxfi); (void)fflush(auxfi);
else else if (eol)
(void)putc('\n', auxfi); (void)putc('\n', auxfi);
} }
if (redir_fp) if (redir_fp)
fprintf(redir_fp, "%s\n", buf); fprintf(redir_fp, "%s%s", buf, eol ? "\n" : "");
else if (pipe_fp) else if (pipe_fp)
fprintf(pipe_fp, "%s\n", buf); fprintf(pipe_fp, eol ? "%s\n": "%s", buf);
else { else {
screen(buf); screen(buf);
if (code == C_FLUSH) if (code == C_FLUSH)
(void)fflush(stdout); (void)fflush(stdout);
else else if (eol)
(void)putc('\n', stdout); (void)putc('\n', stdout);
} }
} }