diff options
author | Bram Moolenaar <Bram@vim.org> | 2016-02-13 17:04:46 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2016-02-13 17:04:46 +0100 |
commit | 6463ca229cb9412581419497924c85fcbfc854ab (patch) | |
tree | 6c55cc23cf038e6f80ea954e670c691e72dc515a | |
parent | 00af60bbb6cc7e8ccafddb30a1964f891b800bce (diff) |
patch 7.4.1310v7.4.1310
Problem: Jobs don't open a channel.
Solution: Create pipes and add them to the channel. Add ch_logfile().
Only Unix for now.
-rw-r--r-- | runtime/doc/eval.txt | 104 | ||||
-rw-r--r-- | src/channel.c | 787 | ||||
-rw-r--r-- | src/eval.c | 78 | ||||
-rw-r--r-- | src/gui_w48.c | 4 | ||||
-rw-r--r-- | src/os_unix.c | 151 | ||||
-rw-r--r-- | src/proto/channel.pro | 9 | ||||
-rw-r--r-- | src/structs.h | 97 | ||||
-rw-r--r-- | src/testdir/test_channel.vim | 17 | ||||
-rw-r--r-- | src/testdir/test_channel_pipe.py | 24 | ||||
-rw-r--r-- | src/version.c | 2 |
10 files changed, 931 insertions, 342 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 18069f02f4..63438c9212 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1,4 +1,4 @@ -*eval.txt* For Vim version 7.4. Last change: 2016 Feb 07 +*eval.txt* For Vim version 7.4. Last change: 2016 Feb 13 VIM REFERENCE MANUAL by Bram Moolenaar @@ -1416,7 +1416,7 @@ v:exception The value of the exception most recently caught and not *v:false* *false-variable* v:false A Number with value zero. Used to put "false" in JSON. See - |jsonencode()|. + |json_encode()|. When used as a string this evaluates to "false". > echo v:false < false ~ @@ -1556,7 +1556,7 @@ v:mouse_col Column number for a mouse click obtained with |getchar()|. *v:none* *none-variable* v:none An empty String. Used to put an empty item in JSON. See - |jsonencode()|. + |json_encode()|. When used as a number this evaluates to zero. When used as a string this evaluates to "none". > echo v:none @@ -1564,7 +1564,7 @@ v:none An empty String. Used to put an empty item in JSON. See *v:null* *null-variable* v:null An empty String. Used to put "null" in JSON. See - |jsonencode()|. + |json_encode()|. When used as a number this evaluates to zero. When used as a string this evaluates to "null". > echo v:null @@ -1737,7 +1737,7 @@ v:throwpoint The point where the exception most recently caught and not *v:true* *true-variable* v:true A Number with value one. Used to put "true" in JSON. See - |jsonencode()|. + |json_encode()|. When used as a string this evaluates to "true". > echo v:true < true ~ @@ -1816,7 +1816,9 @@ call( {func}, {arglist} [, {dict}]) any call {func} with arguments {arglist} ceil( {expr}) Float round {expr} up ch_close( {handle}) none close a channel +ch_logfile( {fname} [, {mode}]) none start logging channel activity ch_open( {address} [, {argdict})] Number open a channel to {address} +ch_readraw( {handle}) String read from channel {handle} ch_sendexpr( {handle}, {expr} [, {callback}]) any send {expr} over JSON channel {handle} ch_sendraw( {handle}, {string} [, {callback}]) @@ -1980,9 +1982,9 @@ mapcheck( {name}[, {mode} [, {abbr}]]) String check for mappings matching {name} match( {expr}, {pat}[, {start}[, {count}]]) Number position where {pat} matches in {expr} -matchadd( {group}, {pattern}[, {priority}[, {id}]]) +matchadd( {group}, {pattern}[, {priority}[, {id} [, {dict}]]]) Number highlight {pattern} with {group} -matchaddpos( {group}, {list}[, {priority}[, {id}]]) +matchaddpos( {group}, {pos}[, {priority}[, {id}[, {dict}]]]) Number highlight positions with {group} matcharg( {nr}) List arguments of |:match| matchdelete( {id}) Number delete match identified by {id} @@ -2274,7 +2276,7 @@ assert_fails({cmd} [, {error}]) *assert_fails()* assert_false({actual} [, {msg}]) *assert_false()* When {actual} is not false an error message is added to |v:errors|, like with |assert_equal()|. - A value is false when it is zero. When "{actual}" is not a + A value is false when it is zero. When {actual} is not a number the assert fails. When {msg} is omitted an error in the form "Expected False but got {actual}" is produced. @@ -2676,10 +2678,16 @@ confirm({msg} [, {choices} [, {default} [, {type}]]]) don't fit, a vertical layout is used anyway. For some systems the horizontal layout is always used. -ch_close({handle}) *ch_close()* +ch_close({handle}) *ch_close()* Close channel {handle}. See |channel|. {only available when compiled with the |+channel| feature} +ch_logfile( {fname} [, {mode}]) *ch_logfile()* + Start logging channel activity to {fname}. + When {mode} is omitted or "a" append to the file. + When {mode} is "w" start with an empty file. + When {fname} is an empty string: stop logging. + ch_open({address} [, {argdict}]) *ch_open()* Open a channel to {address}. See |channel|. Returns the channel handle on success. Returns a negative @@ -2703,7 +2711,13 @@ ch_open({address} [, {argdict}]) *ch_open()* Default: 2000. {only available when compiled with the |+channel| feature} -ch_sendexpr({handle}, {expr} [, {callback}]) *ch_sendexpr()* +ch_readraw({handle}) *ch_readraw()* + Read from channel {handle} and return the received message. + This uses the channel timeout. When there is nothing to read + within that time an empty string is returned. + TODO: depends on channel mode. + +ch_sendexpr({handle}, {expr} [, {callback}]) *ch_sendexpr()* Send {expr} over channel {handle}. The {expr} is encoded according to the type of channel. The function cannot be used with a raw channel. See |channel-use|. *E912* @@ -2844,9 +2858,11 @@ deepcopy({expr}[, {noref}]) *deepcopy()* *E698* different from using {expr} directly. When {expr} is a |List| a full copy is created. This means that the original |List| can be changed without changing the - copy, and vice versa. When an item is a |List|, a copy for it - is made, recursively. Thus changing an item in the copy does - not change the contents of the original |List|. + copy, and vice versa. When an item is a |List| or + |Dictionary|, a copy for it is made, recursively. Thus + changing an item in the copy does not change the contents of + the original |List|. + A |Dictionary| is copied in a similar way as a |List|. When {noref} is omitted or zero a contained |List| or |Dictionary| is only copied once. All references point to this single copy. With {noref} set to 1 every occurrence of a @@ -2907,6 +2923,14 @@ diff_hlID({lnum}, {col}) *diff_hlID()* The highlight ID can be used with |synIDattr()| to obtain syntax information about the highlighting. + *disable_char_avail_for_testing()* +disable_char_avail_for_testing({expr}) + When {expr} is 1 the internal char_avail() function will + return FALSE. When {expr} is 0 the char_avail() function will + function normally. + Only use this for a test where typeahead causes the test not + to work. E.g., to trigger the CursorMovedI autocommand event. + empty({expr}) *empty()* Return the Number 1 if {expr} is empty, zero otherwise. - A |List| or |Dictionary| is empty when it does not have any @@ -3937,7 +3961,7 @@ glob2regpat({expr}) *glob2regpat()* empty string. *globpath()* -globpath({path}, {expr} [, {nosuf} [, {list} [, {allinks}]]]) +globpath({path}, {expr} [, {nosuf} [, {list} [, {alllinks}]]]) Perform glob() on all directories in {path} and concatenate the results. Example: > :echo globpath(&rtp, "syntax/c.vim") @@ -3963,7 +3987,7 @@ globpath({path}, {expr} [, {nosuf} [, {list} [, {allinks}]]]) they are separated by <NL> characters. Example: > :echo globpath(&rtp, "syntax/c.vim", 0, 1) < - {allinks} is used as with |glob()|. + {alllinks} is used as with |glob()|. The "**" item can be used to search in a directory tree. For example, to find all "README.txt" files in the directories @@ -4314,22 +4338,25 @@ job_start({command} [, {options}]) *job_start()* Start a job and return a Job object. Unlike |system()| and |:!cmd| this does not wait for the job to finish. - {command} can be a string. This works best on MS-Windows. On + {command} can be a String. This works best on MS-Windows. On Unix it is split up in white-separated parts to be passed to execvp(). Arguments in double quotes can contain white space. - {command} can be a list, where the first item is the executable + {command} can be a List, where the first item is the executable and further items are the arguments. All items are converted to String. This works best on Unix. + On MS-Windows, job_start() makes a GUI application hidden. If + want to show it, Use |:!start| instead. + The command is executed directly, not through a shell, the 'shell' option is not used. To use the shell: > let job = job_start(["/bin/sh", "-c", "echo hello"]) < Or: > let job = job_start('/bin/sh -c "echo hello"') -< However, the status of the job will now be the status of the - shell, and stopping the job means stopping the shell and the - command may continue to run. +< Note that this will start two processes, the shell and the + command it executes. If you don't want this use the "exec" + shell command. On Unix $PATH is used to search for the executable only when the command does not contain a slash. @@ -4342,12 +4369,10 @@ job_start({command} [, {options}]) *job_start()* The returned Job object can be used to get the status with |job_status()| and stop the job with |job_stop()|. - {options} must be a Dictionary. It can contain these optional - items: - killonexit When non-zero kill the job when Vim - exits. (default: 0, don't kill) + {options} must be a Dictionary. It can contain many optional + items, see |job-options|. - {only available when compiled with the |+channel| feature} + {only available when compiled with the |+job| feature} job_status({job}) *job_status()* Returns a String with the status of {job}: @@ -4355,27 +4380,40 @@ job_status({job}) *job_status()* "fail" job failed to start "dead" job died or was stopped after running - {only available when compiled with the |+channel| feature} + {only available when compiled with the |+job| feature} job_stop({job} [, {how}]) *job_stop()* Stop the {job}. This can also be used to signal the job. When {how} is omitted or is "term" the job will be terminated - normally. For Unix SIGTERM is sent. - Other values: + normally. For Unix SIGTERM is sent. For MS-Windows + CTRL_BREAK will be sent. This goes to the process group, thus + children may also be affected. + + Other values for Unix: "hup" Unix: SIGHUP "quit" Unix: SIGQUIT "kill" Unix: SIGKILL (strongest way to stop) number Unix: signal with that number + Other values for MS-Windows: + "int" Windows: CTRL_C + "kill" Windows: terminate process forcedly + Others Windows: CTRL_BREAK + + On Unix the signal is sent to the process group. This means + that when the job is "sh -c command" it affects both the shell + and the command. + The result is a Number: 1 if the operation could be executed, 0 if "how" is not supported on the system. Note that even when the operation was executed, whether the job was actually stopped needs to be checked with job_status(). - The operation will even be done when the job wasn't running. + The status of the job isn't checked, the operation will even + be done when Vim thinks the job isn't running. - {only available when compiled with the |+channel| feature} + {only available when compiled with the |+job| feature} join({list} [, {sep}]) *join()* Join the items in {list} together into one String. @@ -4773,7 +4811,7 @@ match({expr}, {pat}[, {start}[, {count}]]) *match()* done like 'magic' is set and 'cpoptions' is empty. *matchadd()* *E798* *E799* *E801* -matchadd({group}, {pattern}[, {priority}[, {id} [, {dict}]]]) +matchadd({group}, {pattern}[, {priority}[, {id}[, {dict}]]]) Defines a pattern to be highlighted in the current window (a "match"). It will be highlighted with {group}. Returns an identification number (ID), which can be used to delete the @@ -4809,7 +4847,7 @@ matchadd({group}, {pattern}[, {priority}[, {id} [, {dict}]]]) highlighted matches. The dict can have the following members: conceal Special character to show instead of the - match (only for |hl-Conceal| highlighed + match (only for |hl-Conceal| highlighted matches, see |:syn-cchar|) The number of matches is not limited, as it is the case with @@ -6808,7 +6846,7 @@ type({expr}) The result is a Number, depending on the type of {expr}: :if type(myvar) == type({}) :if type(myvar) == type(0.0) :if type(myvar) == type(v:false) - :if type(myvar) == type(v:none + :if type(myvar) == type(v:none) undofile({name}) *undofile()* Return the name of the undo file that would be used for a file diff --git a/src/channel.c b/src/channel.c index af282cdae4..d810925f9e 100644 --- a/src/channel.c +++ b/src/channel.c @@ -14,22 +14,6 @@ #if defined(FEAT_CHANNEL) || defined(PROTO) -/* - * Change the zero to 1 to enable debugging. - * This will write a file "channel_debug.log". - */ -#if 0 -# define CHERROR(fmt, arg) cherror(fmt, arg) -# define CHLOG(idx, send, buf) chlog(idx, send, buf) -# define CHFILE "channel_debug.log" - -static void cherror(char *fmt, char *arg); -static void chlog(int send, char_u *buf); -#else -# define CHERROR(fmt, arg) -# define CHLOG(idx, send, buf) -#endif - /* TRUE when netbeans is running with a GUI. */ #ifdef FEAT_GUI # define CH_HAS_GUI (gui.in_use || gui.starting) @@ -70,74 +54,114 @@ static void chlog(int send, char_u *buf); extern HWND s_hwnd; /* Gvim's Window handle */ #endif -struct readqueue +/* + * Information about all channels. + * There can be gaps for closed channels, they will be reused later. + */ +static channel_T *channels = NULL; +static int channel_count = 0; + +/* Log file opened with ch_logfile(). */ +static FILE *log_fd = NULL; + + void +ch_logfile(FILE *file) { - char_u *buffer; - struct readqueue *next; - struct readqueue *prev; -}; -typedef struct readqueue readq_T; + if (log_fd != NULL) + fclose(log_fd); + log_fd = file; + if (log_fd != NULL) + fprintf(log_fd, "==== start log session ====\n"); +} -struct jsonqueue + static void +ch_log_lead(char *what, int ch_idx) { - typval_T *value; - struct jsonqueue *next; - struct jsonqueue *prev; -}; -typedef struct jsonqueue jsonq_T; + if (log_fd != NULL) + { + if (ch_idx >= 0) + fprintf(log_fd, "%son %d: ", what, ch_idx); + else + fprintf(log_fd, "%s: ", what); + } +} -struct cbqueue + static void +ch_log(int ch_idx, char *msg) { - char_u *callback; - int seq_nr; - struct cbqueue *next; - struct cbqueue *prev; -}; -typedef struct cbqueue cbq_T; - -typedef struct { - sock_T ch_fd; /* the socket, -1 for a closed channel */ - int ch_idx; /* used by channel_poll_setup() */ - readq_T ch_head; /* dummy node, header for circular queue */ - - int ch_error; /* When TRUE an error was reported. Avoids giving - * pages full of error messages when the other side - * has exited, only mention the first error until the - * connection works again. */ -#ifdef FEAT_GUI_X11 - XtInputId ch_inputHandler; /* Cookie for input */ -#endif -#ifdef FEAT_GUI_GTK - gint ch_inputHandler; /* Cookie for input */ -#endif -#ifdef WIN32 - int ch_inputHandler; /* simply ret.value of WSAAsyncSelect() */ -#endif + if (log_fd != NULL) + { + ch_log_lead("", ch_idx); + fputs(msg, log_fd); + fflush(log_fd); + } +} - void (*ch_close_cb)(void); /* callback for when channel is closed */ + static void +ch_logn(int ch_idx, char *msg, int nr) +{ + if (log_fd != NULL) + { + ch_log_lead("", ch_idx); + fprintf(log_fd, msg, nr); + fflush(log_fd); + } +} - int ch_block_id; /* ID that channel_read_json_block() is - waiting for */ - char_u *ch_callback; /* function to call when a msg is not handled */ - cbq_T ch_cb_head; /* dummy node for pre-request callbacks */ + static void +ch_logs(int ch_idx, char *msg, char *name) +{ + if (log_fd != NULL) + { + ch_log_lead("", ch_idx); + fprintf(log_fd, msg, name); + fflush(log_fd); + } +} - ch_mode_T ch_mode; - jsonq_T ch_json_head; /* dummy node, header for circular queue */ + static void +ch_logsn(int ch_idx, char *msg, char *name, int nr) +{ + if (log_fd != NULL) + { + ch_log_lead("", ch_idx); + fprintf(log_fd, msg, name, nr); + fflush(log_fd); + } +} - int ch_timeout; /* request timeout in msec */ -} channel_T; + static void +ch_error(int ch_idx, char *msg) +{ + if (log_fd != NULL) + { + ch_log_lead("ERR ", ch_idx); + fputs(msg, log_fd); + fflush(log_fd); + } +} -/* - * Information about all channels. - * There can be gaps for closed channels, they will be reused later. - */ -static channel_T *channels = NULL; -static int channel_count = 0; + static void +ch_errorn(int ch_idx, char *msg, int nr) +{ + if (log_fd != NULL) + { + ch_log_lead("ERR ", ch_idx); + fprintf(log_fd, msg, nr); + fflush(log_fd); + } +} -/* - * TODO: open debug file when desired. - */ -FILE *debugfd = NULL; + static void +ch_errors(int ch_idx, char *msg, char *arg) +{ + if (log_fd != NULL) + { + ch_log_lead("ERR ", ch_idx); + fprintf(log_fd, msg, arg); + fflush(log_fd); + } +} #ifdef _WIN32 # undef PERROR @@ -181,38 +205,20 @@ strerror_win32(int eno) } #endif -/* - * Add a new channel slot, return the index. - * The channel isn't actually used into ch_fd is set >= 0; - * Returns -1 if all channels are in use. - */ - static int -add_channel(void) + static void +init_channel(int ch_idx) { - int idx; channel_T *ch; - if (channels != NULL) - { - for (idx = 0; idx < channel_count; ++idx) - if (channels[idx].ch_fd < 0) - /* re-use a closed channel slot */ - return idx; - if (channel_count == MAX_OPEN_CHANNELS) - return -1; - } - else - { - channels = (channel_T *)alloc((int)sizeof(channel_T) - * MAX_OPEN_CHANNELS); - if (channels == NULL) - return -1; - } - - ch = &channels[channel_count]; + ch = &channels[ch_idx]; (void)vim_memset(ch, 0, sizeof(channel_T)); - ch->ch_fd = (sock_T)-1; + ch->ch_sock = (sock_T)-1; +#ifdef CHANNEL_PIPES + ch->ch_in = -1; + ch->ch_out = -1; + ch->ch_err = -1; +#endif #ifdef FEAT_GUI_X11 ch->ch_inputHandler = (XtInputId)NULL; #endif @@ -231,7 +237,40 @@ add_channel(void) ch->ch_json_head.prev = &ch->ch_json_head; ch->ch_timeout = 2000; +} +/* + * Add a new channel slot, return the index. + * The channel isn't actually used into ch_sock is set >= 0; + * Returns -1 if all channels are in use. + */ + int +add_channel(void) +{ + int ch_idx; + + if (channels != NULL) + { + for (ch_idx = 0; ch_idx < channel_count; ++ch_idx) + if (!channel_is_open(ch_idx)) + { + /* re-use a closed channel slot */ + init_channel(ch_idx); + ch_log(ch_idx, "Opening channel (used before)\n"); + return ch_idx; + } + if (channel_count == MAX_OPEN_CHANNELS) + return -1; + } + else + { + channels = (channel_T *)alloc((int)sizeof(channel_T) + * MAX_OPEN_CHANNELS); + if (channels == NULL) + return -1; + } + init_channel(channel_count); + ch_log(channel_count, "Opening new channel\n"); return channel_count++; } @@ -245,7 +284,7 @@ messageFromNetbeans(XtPointer clientData, int *unused1 UNUSED, XtInputId *unused2 UNUSED) { - channel_read((int)(long)clientData); + channel_read((int)(long)clientData, FALSE, "messageFromNetbeans"); } #endif @@ -255,27 +294,28 @@ messageFromNetbeans(gpointer clientData, gint unused1 UNUSED, GdkInputCondition unused2 UNUSED) { - channel_read((int)(long)clientData); + channel_read((int)(long)clientData, FALSE, "messageFromNetbeans"); } #endif static void -channel_gui_register(int idx) +channel_gui_register(int ch_idx) { - channel_T *channel = &channels[idx]; + channel_T *channel = &channels[ch_idx]; if (!CH_HAS_GUI) return; + /* TODO: pipes */ # ifdef FEAT_GUI_X11 /* tell notifier we are interested in being called * when there is input on the editor connection socket */ if (channel->ch_inputHandler == (XtInputId)NULL) channel->ch_inputHandler = - XtAppAddInput((XtAppContext)app_context, channel->ch_fd, + XtAppAddInput((XtAppContext)app_context, channel->ch_sock, (XtPointer)(XtInputReadMask + XtInputExceptMask), - messageFromNetbeans, (XtPointer)(long)idx); + messageFromNetbeans, (XtPointer)(long)ch_idx); # else # ifdef FEAT_GUI_GTK /* @@ -284,9 +324,9 @@ channel_gui_register(int idx) */ if (channel->ch_inputHandler == 0) channel->ch_inputHandler = - gdk_input_add((gint)channel->ch_fd, (GdkInputCondition) + gdk_input_add((gint)channel->ch_sock, (GdkInputCondition) ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION), - messageFromNetbeans, (gpointer)(long)idx); + messageFromNetbeans, (gpointer)(long)ch_idx); # else # ifdef FEAT_GUI_W32 /* @@ -295,7 +335,7 @@ channel_gui_register(int idx) */ if (channel->ch_inputHandler == -1) channel->ch_inputHandler = - WSAAsyncSelect(channel->ch_fd, s_hwnd, WM_NETBEANS, FD_READ); + WSAAsyncSelect(channel->ch_sock, s_hwnd, WM_NETBEANS, FD_READ); # endif # endif # endif @@ -311,15 +351,17 @@ channel_gui_register_all(void) int i; for (i = 0; i < channel_count; ++i) - if (channels[i].ch_fd >= 0) + /* TODO: pipes */ + if (channels[i].ch_sock >= 0) channel_gui_register(i); } static void -channel_gui_unregister(int idx) +channel_gui_unregister(int ch_idx) { - channel_T *channel = &channels[idx]; + channel_T *channel = &channels[ch_idx]; + /* TODO: pipes */ # ifdef FEAT_GUI_X11 if (channel->ch_inputHandler != (XtInputId)NULL) { @@ -337,7 +379,7 @@ channel_gui_unregister(int idx) # ifdef FEAT_GUI_W32 if (channel->ch_inputHandler == 0) { - WSAAsyncSelect(channel->ch_fd, s_hwnd, 0, 0); + WSAAsyncSelect(channel->ch_sock, s_hwnd, 0, 0); channel->ch_inputHandler = -1; } # endif @@ -348,7 +390,7 @@ channel_gui_unregister(int idx) #endif /* - * Open a channel to "hostname":"port". + * Open a socket channel to "hostname":"port". * Returns the channel number for success. * Returns a negative number for failure. */ @@ -364,24 +406,24 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) #else int port = port_in; #endif - int idx; + int ch_idx; int ret; #ifdef WIN32 channel_init_winsock(); #endif - idx = add_channel(); - if (idx < 0) + ch_idx = add_channel(); + if (ch_idx < 0) { - CHERROR("All channels are in use\n", ""); + ch_error(-1, "All channels are in use.\n"); EMSG(_("E897: All channels are in use")); return -1; } if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1) { - CHERROR("error in socket() in channel_open()\n", ""); + ch_error(-1, "in socket() in channel_open().\n"); PERROR("E898: socket() in channel_open()"); return -1; } @@ -393,7 +435,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) server.sin_port = htons(port); if ((host = gethostbyname(hostname)) == NULL) { - CHERROR("error in gethostbyname() in channel_open()\n", ""); + ch_error(-1, "in gethostbyname() in channel_open()\n"); PERROR("E901: gethostbyname() in channel_open()"); sock_close(sd); return -1; @@ -412,21 +454,27 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) ) { SOCK_ERRNO; - CHERROR("channel_open: Connect failed with errno %d\n", errno); + ch_errorn(-1, "channel_open: Connect failed with errno %d\n", + errno); sock_close(sd); return -1; } } /* Try connecting to the server. */ + ch_logsn(-1, "Connecting to %s port %d", hostname, port); ret = connect(sd, (struct sockaddr *)&server, sizeof(server)); SOCK_ERRNO; if (ret < 0) { - if (errno != EWOULDBLOCK && errno != EINPROGRESS) + if (errno != EWOULDBLOCK +#ifdef EINPROGRESS + && errno != EINPROGRESS +#endif + ) { - CHERROR("channel_open: Connect failed with errno %d\n", errno); - CHERROR("Cannot connect to port\n", ""); + ch_errorn(-1, "channel_open: Connect failed with errno %d\n", + errno); PERROR(_("E902: Cannot connect to port")); sock_close(sd); return -1; @@ -446,8 +494,8 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) if (ret < 0) { SOCK_ERRNO; - CHERROR("channel_open: Connect failed with errno %d\n", errno); - CHERROR("Cannot connect to port\n", ""); + ch_errorn(-1, "channel_open: Connect failed with errno %d\n", + errno); PERROR(_("E902: Cannot connect to port")); sock_close(sd); return -1; @@ -477,7 +525,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1) { SOCK_ERRNO; - CHERROR("socket() retry in channel_open()\n", ""); + ch_log(-1, "socket() retry in channel_open()\n"); PERROR("E900: socket() retry in channel_open()"); return -1; } @@ -490,7 +538,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) while (retries-- && ((errno == ECONNREFUSED) || (errno == EINTR))) { - CHERROR("retrying...\n", ""); + ch_log(-1, "retrying...\n"); mch_delay(3000L, TRUE); ui_breakcheck(); if (got_int) @@ -509,7 +557,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) if (!success) { /* Get here when the server can't be found. */ - CHERROR("Cannot connect to port after retry\n", ""); + ch_error(-1, "Cannot connect to port after retry\n"); PERROR(_("E899: Cannot connect to port after retry2")); sock_close(sd); return -1; @@ -517,51 +565,69 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) } } - channels[idx].ch_fd = sd; - channels[idx].ch_close_cb = close_cb; + channels[ch_idx].ch_sock = sd; + channels[ch_idx].ch_close_cb = close_cb; #ifdef FEAT_GUI - channel_gui_register(idx); + channel_gui_register(ch_idx); +#endif + + return ch_idx; +} + +#if defined(CHANNEL_PIPES) || defined(PROTO) + void +channel_set_pipes(int ch_idx, int in, int out, int err) +{ + channel_T *channel = &channels[ch_idx]; + + channel->ch_in = in; + channel->ch_out = out; + channel->ch_err = err; +} #endif - return idx; + void +channel_set_job(int ch_idx, job_T *job) +{ + channels[ch_idx].ch_job = job; } /* - * Set the json mode of channel "idx" to "ch_mode". + * Set the json mode of channel "ch_idx" to "ch_mode". */ void -channel_set_json_mode(int idx, ch_mode_T ch_mode) +channel_set_json_mode(int ch_idx, ch_mode_T ch_mode) { - channels[idx].ch_mode = ch_mode; + channels[ch_idx].ch_mode = ch_mode; } /* - * Set the read timeout of channel "idx". + * Set the read timeout of channel "ch_idx". */ void -channel_set_timeout(int idx, int timeout) +channel_set_timeout(int ch_idx, int timeout) { - channels[idx].ch_timeout = timeout; + channels[ch_idx].ch_timeout = timeout; } /* - * Set the callback for channel "idx". + * Set the callback for channel "ch_idx". */ void -channel_set_callback(int idx, char_u *callback) +channel_set_callback(int ch_idx, char_u *callback) { - vim_free(channels[idx].ch_callback); - channels[idx].ch_callback = vim_strsave(callback); + vim_free(channels[ch_idx].ch_callback); + channels[ch_idx].ch_callback = vim_strsave(callback); } /* - * Set the callback for channel "idx" for the response with "id". + * Set the callback for channel "ch_idx" for the response with "id". */ void -channel_set_req_callback(int idx, char_u *callback, int id) +channel_set_req_callback(int ch_idx, char_u *callback, int id) { - cbq_T *cbhead = &channels[idx].ch_cb_head; + cbq_T *cbhead = &channels[ch_idx].ch_cb_head; cbq_T *item = (cbq_T *)alloc((int)sizeof(cbq_T)); if (item != NULL) @@ -576,16 +642,16 @@ channel_set_req_callback(int idx, char_u *callback, int id) } /* - * Invoke the "callback" on channel "idx". + * Invoke the "callback" on channel "ch_idx". */ static void -invoke_callback(int idx, char_u *callback, typval_T *argv) +invoke_callback(int ch_idx, char_u *callback, typval_T *argv) { typval_T rettv; int dummy; argv[0].v_type = VAR_NUMBER; - argv[0].vval.v_number = idx; + argv[0].vval.v_number = ch_idx; call_func(callback, (int)STRLEN(callback), &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL); @@ -602,9 +668,9 @@ invoke_callback(int idx, char_u *callback, typval_T *argv) * Returns NULL if there is nothing. */ char_u * -channel_get(int idx) +channel_get(int ch_idx) { - readq_T *head = &channels[idx].ch_head; + readq_T *head = &channels[ch_idx].ch_head; readq_T *node; char_u *p; @@ -623,23 +689,23 @@ channel_get(int idx) * Returns the whole buffer contents concatenated. */ static char_u * -channel_get_all(int idx) +channel_get_all(int ch_idx) { /* Concatenate everything into one buffer. * TODO: avoid multiple allocations. */ - while (channel_collapse(idx) == OK) + while (channel_collapse(ch_idx) == OK) ; - return channel_get(idx); + return channel_get(ch_idx); } /* - * Collapses the first and second buffer in the channel "idx". + * Collapses the first and second buffer in the channel "ch_idx". * Returns FAIL if that is not possible. */ int -channel_collapse(int idx) +channel_collapse(int ch_idx) { - readq_T *head = &channels[idx].ch_head; + readq_T *head = &channels[ch_idx].ch_head; readq_T *node = head->next; char_u *p; @@ -799,12 +865,12 @@ channel_get_json(int ch_idx, int id, typval_T **rettv) } /* - * Execute a command received over channel "idx". + * Execute a command received over channel "ch_idx". * "cmd" is the command string, "arg2" the second argument. * "arg3" is the third argument, NULL if missing. */ static void -channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3) +channel_exe_cmd(int ch_idx, char_u *cmd, typval_T *arg2, typval_T *arg3) { char_u *arg; @@ -862,7 +928,7 @@ channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3) typval_T *tv; typval_T err_tv; char_u *json = NULL; - channel_T *channel = &channels[idx]; + channel_T *channel = &channels[ch_idx]; int options = channel->ch_mode == MODE_JS ? JSON_JS : 0; /* Don't pollute the display with errors. */ @@ -885,7 +951,7 @@ channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3) } if (json != NULL) { - channel_send(idx, json, "eval"); + channel_send(ch_idx, json, "eval"); vim_free(json); } } @@ -899,11 +965,11 @@ channel_exe_cmd(int idx, char_u *cmd, typval_T *arg2, typval_T *arg3) } /* - * Invoke a callback for channel "idx" if needed. + * Invoke a callback for channel "ch_idx" if needed. * Return OK when a message was handled, there might be another one. */ static int -may_invoke_callback(int idx) +may_invoke_callback(int ch_idx) { char_u *msg = NULL; typval_T *listtv = NULL; @@ -911,7 +977,7 @@ may_invoke_callback(int idx) typval_T *typetv; typval_T argv[3]; int seq_nr = -1; - channel_T *channel = &channels[idx]; + channel_T *channel = &channels[ch_idx]; ch_mode_T ch_mode = channel->ch_mode; if (channel->ch_close_cb != NULL) @@ -921,11 +987,11 @@ may_invoke_callback(int idx) if (ch_mode != MODE_RAW) { /* Get any json message in the queue. */ - if (channel_get_json(idx, -1, &listtv) == FAIL) + if (channel_get_json(ch_idx, -1, &listtv) == FAIL) { /* Parse readahead, return when there is still no message. */ - channel_parse_json(idx); - if (channel_get_json(idx, -1, &listtv) == FAIL) + channel_parse_json(ch_idx); + if (channel_get_json(ch_idx, -1, &listtv) == FAIL) return FALSE; } @@ -940,56 +1006,69 @@ may_invoke_callback(int idx) /* ["cmd", arg] or ["cmd", arg, arg] */ if (list->lv_len == 3) arg3 = &list->lv_last->li_tv; - channel_exe_cmd(idx, cmd, &argv[1], arg3); + ch_logs(ch_idx, "Executing %s command", (char *)cmd); + channel_exe_cmd(ch_idx, cmd, &argv[1], arg3); clear_tv(listtv); return TRUE; } if (typetv->v_type != VAR_NUMBER) { - /* TODO: give error */ + ch_error(ch_idx, + "Dropping message with invalid sequence number type\n"); clear_tv(listtv); return FALSE; } seq_nr = typetv->vval.v_number; } - else if (channel_peek(idx) == NULL) + else if (channel_peek(ch_idx) == NULL) { /* nothing to read on raw channel */ return FALSE; } else { + /* If there is no callback, don't do anything. */ + if (channel->ch_callback == NULL) + return FALSE; + /* For a raw chan |