* 0 on ctrl-C,
* >0 length of input string, including terminating '\n'
*/
- len = read_line_input(": ", buf, sizeof(buf), NULL);
+ len = read_line_input(NULL, ": ", buf, sizeof(buf), /*timeout*/ -1);
if (len <= 0)
return;
endbuf = &buf[len - 1];
}
if (!dirty)
return;
- len = read_line_input("Really quit? ", buf, 16, NULL);
+ len = read_line_input(NULL, "Really quit? ", buf, 16, /*timeout*/ -1);
/* read error/EOF - no way to continue */
if (len < 0)
return;
* 0 on ctrl-C,
* >0 length of input string, including terminating '\n'
*/
- len = read_line_input("", buf, sizeof(buf), NULL);
+ len = read_line_input(NULL, "", buf, sizeof(buf), /*timeout*/ -1);
if (len <= 0) {
/* Previously, ctrl-C was exiting to shell.
* Now we exit to ed prompt. Is in important? */
* 0 on ctrl-C (the line entered is still returned in 'command'),
* >0 length of input string, including terminating '\n'
*/
-/* NB: ash has timeout code which can be moved into read_line_input, if needed */
-int read_line_input(const char* prompt, char* command, int maxsize, line_input_t *state) FAST_FUNC;
+int read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout) FAST_FUNC;
#else
#define MAX_HISTORY 0
int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
-#define read_line_input(prompt, command, maxsize, state) \
+#define read_line_input(state, prompt, command, maxsize, timeout) \
read_line_input(prompt, command, maxsize)
#endif
errno = sv_errno;
}
-static int lineedit_read_key(char *read_key_buffer)
+static int lineedit_read_key(char *read_key_buffer, int timeout)
{
int64_t ic;
- int timeout = -1;
#if ENABLE_UNICODE_SUPPORT
char unicode_buf[MB_CUR_MAX + 1];
int unicode_idx = 0;
* 0 on ctrl-C (the line entered is still returned in 'command'),
* >0 length of input string, including terminating '\n'
*/
-int FAST_FUNC read_line_input(const char *prompt, char *command, int maxsize, line_input_t *st)
+int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout)
{
int len;
#if ENABLE_FEATURE_TAB_COMPLETION
new_settings.c_cc[VINTR] = _POSIX_VDISABLE;
tcsetattr_stdin_TCSANOW(&new_settings);
- /* Now initialize things */
previous_SIGWINCH_handler = signal(SIGWINCH, win_changed);
win_changed(0); /* do initial resizing */
#if ENABLE_USERNAME_OR_HOMEDIR
int32_t ic, ic_raw;
fflush_all();
- ic = ic_raw = lineedit_read_key(read_key_buffer);
+ ic = ic_raw = lineedit_read_key(read_key_buffer, timeout);
#if ENABLE_FEATURE_EDITING_VI
newdelflag = 1;
case 'd'|VI_CMDMODE_BIT: {
int nc, sc;
- ic = lineedit_read_key(read_key_buffer);
+ ic = lineedit_read_key(read_key_buffer, timeout);
if (errno) /* error */
goto return_error_indicator;
if (ic == ic_raw) { /* "cc", "dd" */
break;
case 'r'|VI_CMDMODE_BIT:
//FIXME: unicode case?
- ic = lineedit_read_key(read_key_buffer);
+ ic = lineedit_read_key(read_key_buffer, timeout);
if (errno) /* error */
goto return_error_indicator;
if (ic < ' ' || ic > 255) {
//config: default n
//config: depends on ASH
//config: help
-//config: Enables bash-like auto-logout after "$TMOUT" seconds
-//config: of idle time.
+//config: Enables bash-like auto-logout after $TMOUT seconds of idle time.
//config:
//config:config ASH_JOB_CONTROL
//config: bool "Job control"
/* ============ Interrupts / exceptions */
+
+static void exitshell(void) NORETURN;
+
/*
* These macros allow the user to suspend the handling of interrupt signals
* over a period of time. This is similar to SIGHOLD or to sigblock, but
if (!iflag || g_parsefile->pf_fd != STDIN_FILENO)
nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
else {
+ int timeout = -1;
+# if ENABLE_ASH_IDLE_TIMEOUT
+ if (iflag) {
+ const char *tmout_var = lookupvar("TMOUT");
+ if (tmout_var) {
+ timeout = atoi(tmout_var) * 1000;
+ if (timeout <= 0)
+ timeout = -1;
+ }
+ }
+# endif
# if ENABLE_FEATURE_TAB_COMPLETION
line_input_state->path_lookup = pathval();
# endif
- nr = read_line_input(cmdedit_prompt, buf, IBUFSIZ, line_input_state);
+ nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ, timeout);
if (nr == 0) {
/* Ctrl+C pressed */
if (trap[SIGINT]) {
}
goto retry;
}
- if (nr < 0 && errno == 0) {
- /* Ctrl+D pressed */
- nr = 0;
+ if (nr < 0) {
+ if (errno == 0) {
+ /* Ctrl+D pressed */
+ nr = 0;
+ }
+# if ENABLE_ASH_IDLE_TIMEOUT
+ else if (errno == EAGAIN && timeout > 0) {
+ printf("\007timed out waiting for input: auto-logout\n");
+ exitshell();
+ }
+# endif
}
}
#else
return exitstatus;
}
-#if ENABLE_ASH_IDLE_TIMEOUT
-static smallint timed_out;
-
-static void alrm_sighandler(int sig UNUSED_PARAM)
-{
- /* Close stdin, making interactive command reading stop.
- * Otherwise, timeout doesn't trigger until <Enter> is pressed.
- */
- int sv = errno;
- close(0);
- open("/dev/null", O_RDONLY);
- errno = sv;
-
- timed_out = 1;
-}
-#endif
-
/*
* Read and execute commands.
* "Top" is nonzero for the top level command loop;
TRACE(("cmdloop(%d) called\n", top));
for (;;) {
int skip;
-#if ENABLE_ASH_IDLE_TIMEOUT
- int tmout_seconds = 0;
-
- if (top && iflag) {
- const char *tmout_var = lookupvar("TMOUT");
- if (tmout_var) {
- tmout_seconds = atoi(tmout_var);
- if (tmout_seconds > 0) {
- signal(SIGALRM, alrm_sighandler);
- alarm(tmout_seconds);
- }
- }
- }
-#endif
setstackmark(&smark);
#if JOBS
chkmail();
}
n = parsecmd(inter);
-#if ENABLE_ASH_IDLE_TIMEOUT
- if (timed_out) {
- printf("\007timed out waiting for input: auto-logout\n");
- break;
- }
- if (tmout_seconds > 0)
- alarm(0);
-#endif
#if DEBUG
if (DEBUG > 2 && debug && (n != NODE_EOF))
showtree(n);
/*
* Called to exit the shell.
*/
-static void exitshell(void) NORETURN;
static void
exitshell(void)
{
G.flag_SIGINT = 0;
/* buglet: SIGINT will not make new prompt to appear _at once_,
* only after <Enter>. (^C will work) */
- r = read_line_input(prompt_str, G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, G.line_input_state);
+ r = read_line_input(G.line_input_state, prompt_str, G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, /*timeout*/ -1);
/* catch *SIGINT* etc (^C is handled by read_line_input) */
check_and_run_traps(0);
} while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */
{
int sz;
- sz = read_line_input(prompt, line_buffer, sizeof(line_buffer), NULL);
+ sz = read_line_input(NULL, prompt, line_buffer, sizeof(line_buffer), /*timeout*/ -1);
if (sz <= 0)
exit(EXIT_SUCCESS); /* Ctrl-D or Ctrl-C */