Пример #1
0
int
readcmd(int argc, char **argv)
{
	char **ap;
	int backslash;
	char c;
	int rflag;
	char *prompt;
	char *p;
	int status;
	int i;

	rflag = 0;
	prompt = NULL;
	while ((i = nextopt("p:r")) != '\0') {
		if (i == 'p')
			prompt = optionarg;
		else
			rflag = 1;
	}
	if (prompt && isatty(0)) {
		out2str(prompt);
#ifdef FLUSHERR
		flushall();
#endif
	}
	if (*(ap = argptr) == NULL)
		sh_error("arg count");
	status = 0;
	backslash = 0;
	STARTSTACKSTR(p);
	for (;;) {
		if (read(0, &c, 1) != 1) {
			status = 1;
			break;
		}
		if (c == '\0')
			continue;
		if (backslash) {
			if (c == '\n')
				goto resetbs;
			STPUTC(CTLESC, p);
			goto put;
		}
		if (!rflag && c == '\\') {
			backslash++;
			continue;
		}
		if (c == '\n')
			break;
put:
		STPUTC(c, p);
resetbs:
		backslash = 0;
	}
	STACKSTRNUL(p);
	readcmd_handle_line(stackblock(), ap, p + 1 - (char *)stackblock());
	return status;
}
Пример #2
0
int
readcmd(int argc, char **argv)
{
	char **ap;
	char c;
	int rflag;
	char *prompt;
	char *p;
	int startloc;
	int newloc;
	int status;
	int i;

	rflag = 0;
	prompt = NULL;
	while ((i = nextopt("p:r")) != '\0') {
		if (i == 'p')
			prompt = optionarg;
		else
			rflag = 1;
	}
	if (prompt && isatty(0)) {
		out2str(prompt);
#ifdef FLUSHERR
		flushall();
#endif
	}
	if (*(ap = argptr) == NULL)
		sh_error("arg count");

	status = 0;
	STARTSTACKSTR(p);

	goto start;

	for (;;) {
		switch (read(0, &c, 1)) {
		case 1:
			break;
		default:
			if (errno == EINTR && !pendingsigs)
				continue;
				/* fall through */
		case 0:
			status = 1;
			goto out;
		}
		if (c == '\0')
			continue;
		if (newloc >= startloc) {
			if (c == '\n')
				goto resetbs;
			goto put;
		}
		if (!rflag && c == '\\') {
			newloc = p - (char *)stackblock();
			continue;
		}
		if (c == '\n')
			break;
put:
		CHECKSTRSPACE(2, p);
		if (strchr(qchars, c))
			USTPUTC(CTLESC, p);
		USTPUTC(c, p);

		if (newloc >= startloc) {
resetbs:
			recordregion(startloc, newloc, 0);
start:
			startloc = p - (char *)stackblock();
			newloc = startloc - 1;
		}
	}
out:
	recordregion(startloc, p - (char *)stackblock(), 0);
	STACKSTRNUL(p);
	readcmd_handle_line(p + 1, ap);
	return status;
}
Пример #3
0
int
readcmd(int argc, char **argv)
{
	char **ap;
	char c;
	int rflag;
	char *prompt;
	char *p;
	int startloc;
	int newloc;
	int status;
	int timeout;
	int i;
	fd_set set;
	struct timeval ts, t0, t1, to;

	ts.tv_sec = ts.tv_usec = 0;

	rflag = 0;
	timeout = 0;
	prompt = NULL;
	while ((i = nextopt("p:rt:")) != '\0') {
		switch(i) {
		case 'p':
			prompt = optionarg;
			break;
		case 't':
			p = strtotimeval(optionarg, &ts);
			if (*p || (!ts.tv_sec && !ts.tv_usec))
				sh_error("invalid timeout");
			timeout = 1;
			break;
		case 'r':
			rflag = 1;
			break;
		default:
			break;
		}
	}
	if (prompt && isatty(0)) {
		out2str(prompt);
#ifdef FLUSHERR
		flushall();
#endif
	}
	if (*(ap = argptr) == NULL)
		sh_error("arg count");

	status = 0;
	if (timeout) {
		gettimeofday(&t0, NULL);

		/* ts += t0; */
		ts.tv_usec += t0.tv_usec;
		while (ts.tv_usec >= 1000000) {
			ts.tv_sec++;
			ts.tv_usec -= 1000000;
		}
		ts.tv_sec += t0.tv_sec;
	}
	STARTSTACKSTR(p);

	goto start;

	for (;;) {
		if (timeout) {
			gettimeofday(&t1, NULL);
			if (t1.tv_sec > ts.tv_sec ||
			    (t1.tv_sec == ts.tv_sec &&
			     t1.tv_usec >= ts.tv_usec)) {
				status = 1;
				break;	/* Timeout! */
			}

			/* to = ts - t1; */
			if (ts.tv_usec >= t1.tv_usec) {
				to.tv_usec = ts.tv_usec - t1.tv_usec;
				to.tv_sec  = ts.tv_sec - t1.tv_sec;
			} else {
				to.tv_usec = ts.tv_usec - t1.tv_usec + 1000000;
				to.tv_sec  = ts.tv_sec - t1.tv_sec - 1;
			}

			FD_ZERO(&set);
			FD_SET(0, &set);
			if (select(1, &set, NULL, NULL, &to) != 1) {
				status = 1;
				break; /* Timeout! */
			}
		}
		switch (read(0, &c, 1)) {
		case 1:
			break;
		default:
			if (errno == EINTR && !pendingsigs)
				continue;
				/* fall through */
		case 0:
			status = 1;
			goto out;
		}
		if (c == '\0')
			continue;
		if (newloc >= startloc) {
			if (c == '\n')
				goto resetbs;
			goto put;
		}
		if (!rflag && c == '\\') {
			newloc = p - (char *)stackblock();
			continue;
		}
		if (c == '\n')
			break;
put:
		CHECKSTRSPACE(2, p);
		if (strchr(qchars, c))
			USTPUTC(CTLESC, p);
		USTPUTC(c, p);

		if (newloc >= startloc) {
resetbs:
			recordregion(startloc, newloc, 0);
start:
			startloc = p - (char *)stackblock();
			newloc = startloc - 1;
		}
	}
out:
	recordregion(startloc, p - (char *)stackblock(), 0);
	STACKSTRNUL(p);
	readcmd_handle_line(p + 1, ap);
	return status;
}