The last parameter of exec_on_terminal() gets values 0, 1, and 2.
These values don't seem to be documented anywhere.
To make the code easier to understand, I suggest giving them names.
While doing that, I noticed this in in_sock():

                if (is_blocked() && fg) {
                        if (*delete.source) unlink(delete.source);
                        goto nasty_thing;
                }

And this in exec_on_terminal():

                if (fg && is_blocked()) {
                        unlink(delete);
                        return;
                }

But in_sock() and exec_on_master_terminal() call block_itrm() and
unblock_itrm() only if fg == 1.  So, if fg == 2 and ditrm has
already been blocked, then the exec fails, even though the
program to be run was not going to use the terminal.  Should
the two fg checks shown above be changed to fg == 1?

Also I am not sure about the purpose of setpgid() in exec_thread().
That code was inherited from Links.  Is it perhaps intended to
protect the child process when the user hits ^C and sends SIGINT
to the process group of ELinks?  If so, that should be documented.

I have not pushed this patch yet, because of the fg == 1 doubt,
and because Witek seems to be working on bug 638.


Name the exec_on_terminal() fg values.

---
commit aa00b6f8e969c0c2c929e6591790283c0796aaf2
tree 6c5d84304acb74b15f6a1733aa6b9c65ba0302d9
parent eccc8c23f0901b8560f27741a631d858c04203a4
author Kalle Olavi Niemitalo <[EMAIL PROTECTED]> Sat, 14 Jul 2007 12:26:45 +0300
committer Kalle Olavi Niemitalo <[EMAIL PROTECTED]> Sat, 14 Jul 2007 12:26:45 
+0300

 src/dialogs/menu.c                 |    2 +-
 src/osdep/newwin.c                 |    2 +-
 src/protocol/user.c                |    2 +-
 src/scripting/lua/core.c           |    5 +++--
 src/scripting/smjs/elinks_object.c |    2 +-
 src/session/download.c             |    5 +++--
 src/terminal/kbd.c                 |   10 +++++-----
 src/terminal/terminal.c            |   22 +++++++++++-----------
 src/terminal/terminal.h            |   20 +++++++++++++++++++-
 src/viewer/text/textarea.c         |    2 +-
 10 files changed, 46 insertions(+), 26 deletions(-)

diff --git a/src/dialogs/menu.c b/src/dialogs/menu.c
index 0707b98..172965c 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 25e4fe1..8c92300 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 76b466a..f0bcc5d 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 bd1b8df..8aec3a9 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 e017723..55d7a29 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 3f34121..376a294 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 16be876..9a7f544 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 befaa0a..00a2854 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 d95da99..f661e02 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 a6b739f..dfb4460 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;

Attachment: pgpnuJzrVWLT4.pgp
Description: PGP signature

_______________________________________________
elinks-dev mailing list
elinks-dev@linuxfromscratch.org
http://linuxfromscratch.org/mailman/listinfo/elinks-dev

Reply via email to