}
char *
-ioq_gets(struct ioqueue *ioq, char *buf, int cc)
+ioq_gets(struct ioqueue *ioq, char *buf, int cc, int *eol)
{
char *p;
char *end;
int nbytes;
+ *eol = 0;
+
+ cc--;
+
nbytes = ioqtobuf(ioq, buf, cc);
- if (nbytes < cc)
- cc = nbytes;
- end = &buf[cc];
+ end = &buf[nbytes];
for (p = buf; p < end && *p; p++) {
if (*p == '\n') {
*p = '\0';
+ *eol = 1;
dequeuecc(ioq, (p - buf) + 1);
return buf;
}
}
+ if (cc && (p - buf) == cc) {
+ dequeuecc(ioq, cc);
+ buf[cc] = '\0';
+ return buf;
+ }
return NULL;
}
void ioq_write(struct ioqueue *ioq, char *buf, int cc);
int ioq_qsize(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);
static void doredir(char *p);
static void dopipe(char *p);
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);
void
{
char buf[1024];
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;
- while (*p && !isspace(*p))
- p++;
- *p++ = 0;
- if (isalpha(*buf))
- code = 10 + (*buf - 'a');
- else
- code = *buf - '0';
+ if (code == -1) {
+ if (isalpha(*buf))
+ code = 10 + (*buf - 'a');
+ else
+ code = *buf - '0';
+ while (*p && !isspace(*p))
+ p++;
+ *p++ = 0;
+ }
switch (code) {
case C_PROMPT:
if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
*num_teles = '\0';
break;
default:
- output(code, p, auxfi);
+ output(code, p, auxfi, eol);
break;
}
+ if (eol)
+ code = -1;
}
}
}
static void
-output(int code, char *buf, FILE *auxfi)
+output(int code, char *buf, FILE *auxfi, int eol)
{
switch (code) {
case C_NOECHO:
fprintf(auxfi, "%s", buf);
if (code == C_FLUSH)
(void)fflush(auxfi);
- else
+ else if (eol)
(void)putc('\n', auxfi);
}
if (redir_fp)
- fprintf(redir_fp, "%s\n", buf);
+ fprintf(redir_fp, "%s%s", buf, eol ? "\n" : "");
else if (pipe_fp)
- fprintf(pipe_fp, "%s\n", buf);
+ fprintf(pipe_fp, eol ? "%s\n": "%s", buf);
else {
screen(buf);
if (code == C_FLUSH)
(void)fflush(stdout);
- else
+ else if (eol)
(void)putc('\n', stdout);
}
}