diff --git a/src/dialogs/menu.c b/src/dialogs/menu.c index 0707b9807..172965ca2 100644 --- a/src/dialogs/menu.c +++ b/src/dialogs/menu.c @@ -759,7 +759,7 @@ do_pass_uri_to_command(struct terminal *term, void *command_, void *xxx) { unsigned char *command = command_; - exec_on_terminal(term, command, "", 0); + exec_on_terminal(term, command, "", TERM_EXEC_BG); mem_free(command); } diff --git a/src/osdep/newwin.c b/src/osdep/newwin.c index 25e4fe1ad..8c92300d3 100644 --- a/src/osdep/newwin.c +++ b/src/osdep/newwin.c @@ -90,6 +90,6 @@ open_new_window(struct terminal *term, unsigned char *exe_name, (unsigned char *) NULL); if (!command) return; - exec_on_terminal(term, command, "", 2); + exec_on_terminal(term, command, "", TERM_EXEC_NEWWIN); mem_free(command); } diff --git a/src/protocol/user.c b/src/protocol/user.c index 3a92ead71..e0b3f6288 100644 --- a/src/protocol/user.c +++ b/src/protocol/user.c @@ -300,7 +300,7 @@ user_protocol_handler(struct session *ses, struct uri *uri) if (prog) { unsigned char *delete = empty_string_or_(filename); - exec_on_terminal(ses->tab->term, prog, delete, 1); + exec_on_terminal(ses->tab->term, prog, delete, TERM_EXEC_FG); mem_free(prog); } else if (filename) { diff --git a/src/scripting/lua/core.c b/src/scripting/lua/core.c index bd1b8df0c..8aec3a91a 100644 --- a/src/scripting/lua/core.c +++ b/src/scripting/lua/core.c @@ -231,7 +231,8 @@ static int l_execute(LS) { if (lua_isstring(S, 1)) { - exec_on_terminal(lua_ses->tab->term, (unsigned char *) lua_tostring(S, 1), "", 0); + exec_on_terminal(lua_ses->tab->term, (unsigned char *) lua_tostring(S, 1), "", + TERM_EXEC_BG); lua_pushnumber(S, 0); return 1; } @@ -795,7 +796,7 @@ handle_ret_run(struct session *ses) unsigned char *cmd = (unsigned char *) lua_tostring(L, -1); if (cmd) { - exec_on_terminal(ses->tab->term, cmd, "", 1); + exec_on_terminal(ses->tab->term, cmd, "", TERM_EXEC_FG); return; } diff --git a/src/scripting/smjs/elinks_object.c b/src/scripting/smjs/elinks_object.c index e017723b9..55d7a2970 100644 --- a/src/scripting/smjs/elinks_object.c +++ b/src/scripting/smjs/elinks_object.c @@ -103,7 +103,7 @@ elinks_execute(JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rv if (!*string) return JS_TRUE; - exec_on_terminal(smjs_ses->tab->term, string, "", 0); + exec_on_terminal(smjs_ses->tab->term, string, "", TERM_EXEC_BG); undef_to_jsval(ctx, rval); return JS_TRUE; diff --git a/src/session/download.c b/src/session/download.c index 3f3412129..376a29446 100644 --- a/src/session/download.c +++ b/src/session/download.c @@ -376,7 +376,7 @@ download_data_store(struct download *download, struct file_download *file_downlo } else { exec_on_terminal(term, file_download->external_handler, file_download->file, - !!file_download->block); + file_download->block ? TERM_EXEC_FG : TERM_EXEC_BG); file_download->delete = 0; abort_download_and_beep(file_download, term); } @@ -1100,7 +1100,8 @@ tp_open(struct type_query *type_query) read_from_popen(type_query->ses, handler, NULL); else exec_on_terminal(type_query->ses->tab->term, - handler, "", !!type_query->block); + handler, "", + type_query->block ? TERM_EXEC_FG : TERM_EXEC_BG); mem_free(handler); } diff --git a/src/terminal/kbd.c b/src/terminal/kbd.c index 16be876b0..9a7f54406 100644 --- a/src/terminal/kbd.c +++ b/src/terminal/kbd.c @@ -510,7 +510,7 @@ in_sock(struct itrm *itrm) struct string path; struct string delete; char ch; - int fg; + int fg; /* enum term_exec */ ssize_t bytes_read, i, p; unsigned char buf[ITRM_OUT_QUEUE_SIZE]; @@ -575,7 +575,7 @@ has_nul_byte: unsigned char *param; int path_len, del_len, param_len; - if (is_blocked() && fg) { + if (is_blocked() && fg != TERM_EXEC_BG) { if (*delete.source) unlink(delete.source); goto nasty_thing; } @@ -591,20 +591,20 @@ has_nul_byte: memcpy(param + 1, path.source, path_len + 1); memcpy(param + 1 + path_len + 1, delete.source, del_len + 1); - if (fg == 1) block_itrm(); + if (fg == TERM_EXEC_FG) block_itrm(); blockh = start_thread((void (*)(void *, int)) exec_thread, param, param_len); mem_free(param); if (blockh == -1) { - if (fg == 1) + if (fg == TERM_EXEC_FG) unblock_itrm(); goto nasty_thing; } - if (fg == 1) { + if (fg == TERM_EXEC_FG) { set_handlers(blockh, (select_handler_T) unblock_itrm_x, NULL, (select_handler_T) unblock_itrm_x, (void *) (long) blockh); diff --git a/src/terminal/terminal.c b/src/terminal/terminal.c index befaa0a1d..00a2854c4 100644 --- a/src/terminal/terminal.c +++ b/src/terminal/terminal.c @@ -179,7 +179,7 @@ exec_thread(unsigned char *path, int p) int plen = strlen(path + 1) + 2; #if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD) - if (path[0] == 2) setpgid(0, 0); + if (path[0] == TERM_EXEC_NEWWIN) setpgid(0, 0); #endif exe(path + 1); if (path[plen]) unlink(path + plen); @@ -210,7 +210,7 @@ static void exec_on_master_terminal(struct terminal *term, unsigned char *path, int plen, unsigned char *delete, int dlen, - int fg) + enum term_exec fg) { int blockh; int param_size = plen + dlen + 2 /* 2 null char */ + 1 /* fg */; @@ -222,17 +222,17 @@ exec_on_master_terminal(struct terminal *term, memcpy(param + 1, path, plen + 1); memcpy(param + 1 + plen + 1, delete, dlen + 1); - if (fg == 1) block_itrm(); + if (fg == TERM_EXEC_FG) block_itrm(); blockh = start_thread((void (*)(void *, int)) exec_thread, param, param_size); fmem_free(param); if (blockh == -1) { - if (fg == 1) unblock_itrm(); + if (fg == TERM_EXEC_FG) unblock_itrm(); return; } - if (fg == 1) { + if (fg == TERM_EXEC_FG) { term->blocked = blockh; set_handlers(blockh, (select_handler_T) unblock_terminal, @@ -253,7 +253,7 @@ static void exec_on_slave_terminal( struct terminal *term, unsigned char *path, int plen, unsigned char *delete, int dlen, - int fg) + enum term_exec fg) { int data_size = plen + dlen + 1 /* 0 */ + 1 /* fg */ + 2 /* 2 null char */; unsigned char *data = fmem_alloc(data_size); @@ -270,7 +270,7 @@ exec_on_slave_terminal( struct terminal *term, void exec_on_terminal(struct terminal *term, unsigned char *path, - unsigned char *delete, int fg) + unsigned char *delete, enum term_exec fg) { if (path) { if (!*path) return; @@ -279,7 +279,7 @@ exec_on_terminal(struct terminal *term, unsigned char *path, } #ifdef NO_FG_EXEC - fg = 0; + fg = TERM_EXEC_BG; #endif if (term->master) { @@ -288,7 +288,7 @@ exec_on_terminal(struct terminal *term, unsigned char *path, return; } - if (fg && is_blocked()) { + if (fg != TERM_EXEC_BG && is_blocked()) { unlink(delete); return; } @@ -314,7 +314,7 @@ exec_shell(struct terminal *term) sh = get_shell(); if (sh && *sh) - exec_on_terminal(term, sh, "", 1); + exec_on_terminal(term, sh, "", TERM_EXEC_FG); } @@ -328,7 +328,7 @@ do_terminal_function(struct terminal *term, unsigned char code, if (!x_data) return; x_data[0] = code; memcpy(x_data + 1, data, data_len + 1); - exec_on_terminal(term, NULL, x_data, 0); + exec_on_terminal(term, NULL, x_data, TERM_EXEC_BG); fmem_free(x_data); } diff --git a/src/terminal/terminal.h b/src/terminal/terminal.h index d95da99a2..f661e02ae 100644 --- a/src/terminal/terminal.h +++ b/src/terminal/terminal.h @@ -175,10 +175,28 @@ void destroy_all_terminals(void); void exec_thread(unsigned char *, int); void close_handle(void *); +/* Operations that can be requested with do_terminal_function(). + * The interlink protocol passes these values as one byte in a + * null-terminated string, so zero cannot be used. */ #define TERM_FN_TITLE 1 #define TERM_FN_RESIZE 2 -void exec_on_terminal(struct terminal *, unsigned char *, unsigned char *, int); +/* How to execute a program in a terminal. These values are used in + * the interlink protocol and must fit in one byte. */ +enum term_exec { + /* Execute in the background. ELinks keeps using the terminal + * and the program should not use it. */ + TERM_EXEC_BG = 0, + + /* Execute in the foreground. The program may use the terminal. + * ELinks will redraw when the program exits. */ + TERM_EXEC_FG = 1, + + /* Execute in the background and in a new process group. */ + TERM_EXEC_NEWWIN = 2 +}; + +void exec_on_terminal(struct terminal *, unsigned char *, unsigned char *, enum term_exec); void exec_shell(struct terminal *term); void set_terminal_title(struct terminal *, unsigned char *); diff --git a/src/viewer/text/textarea.c b/src/viewer/text/textarea.c index a6b739ff7..dfb446051 100644 --- a/src/viewer/text/textarea.c +++ b/src/viewer/text/textarea.c @@ -625,7 +625,7 @@ textarea_edit(int op, struct terminal *term_, struct form_state *fs_, } if (term_) term = term_; - exec_on_terminal(term, ex, "", 1); + exec_on_terminal(term, ex, "", TERM_EXEC_FG); mem_free(ex); textarea_editor = 1;