comparison src/channel.c @ 18757:c469e1930456 v8.1.2368

patch 8.1.2368: using old C style comments Commit: https://github.com/vim/vim/commit/c667da5185ce5dce914d2006d62da2be0cedb384 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Nov 30 20:52:27 2019 +0100 patch 8.1.2368: using old C style comments Problem: Using old C style comments. Solution: Use // comments where appropriate.
author Bram Moolenaar <Bram@vim.org>
date Sat, 30 Nov 2019 21:00:04 +0100
parents 0f8a34c996eb
children ab97d1aea4aa
comparison
equal deleted inserted replaced
18756:c58a9ff2b806 18757:c469e1930456
12 12
13 #include "vim.h" 13 #include "vim.h"
14 14
15 #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) 15 #if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
16 16
17 /* TRUE when netbeans is running with a GUI. */ 17 // TRUE when netbeans is running with a GUI.
18 #ifdef FEAT_GUI 18 #ifdef FEAT_GUI
19 # define CH_HAS_GUI (gui.in_use || gui.starting) 19 # define CH_HAS_GUI (gui.in_use || gui.starting)
20 #endif 20 #endif
21 21
22 /* Note: when making changes here also adjust configure.ac. */ 22 // Note: when making changes here also adjust configure.ac.
23 #ifdef MSWIN 23 #ifdef MSWIN
24 /* WinSock API is separated from C API, thus we can't use read(), write(), 24 // WinSock API is separated from C API, thus we can't use read(), write(),
25 * errno... */ 25 // errno...
26 # define SOCK_ERRNO errno = WSAGetLastError() 26 # define SOCK_ERRNO errno = WSAGetLastError()
27 # undef ECONNREFUSED 27 # undef ECONNREFUSED
28 # define ECONNREFUSED WSAECONNREFUSED 28 # define ECONNREFUSED WSAECONNREFUSED
29 # undef EWOULDBLOCK 29 # undef EWOULDBLOCK
30 # define EWOULDBLOCK WSAEWOULDBLOCK 30 # define EWOULDBLOCK WSAEWOULDBLOCK
59 static int channel_get_timeout(channel_T *channel, ch_part_T part); 59 static int channel_get_timeout(channel_T *channel, ch_part_T part);
60 static ch_part_T channel_part_send(channel_T *channel); 60 static ch_part_T channel_part_send(channel_T *channel);
61 static ch_part_T channel_part_read(channel_T *channel); 61 static ch_part_T channel_part_read(channel_T *channel);
62 static void free_job_options(jobopt_T *opt); 62 static void free_job_options(jobopt_T *opt);
63 63
64 /* Whether a redraw is needed for appending a line to a buffer. */ 64 // Whether a redraw is needed for appending a line to a buffer.
65 static int channel_need_redraw = FALSE; 65 static int channel_need_redraw = FALSE;
66 66
67 /* Whether we are inside channel_parse_messages() or another situation where it 67 // Whether we are inside channel_parse_messages() or another situation where it
68 * is safe to invoke callbacks. */ 68 // is safe to invoke callbacks.
69 static int safe_to_invoke_callback = 0; 69 static int safe_to_invoke_callback = 0;
70 70
71 static char *part_names[] = {"sock", "out", "err", "in"}; 71 static char *part_names[] = {"sock", "out", "err", "in"};
72 72
73 #ifdef MSWIN 73 #ifdef MSWIN
127 127
128 CloseHandle(h); 128 CloseHandle(h);
129 } 129 }
130 #endif 130 #endif
131 131
132 /* Log file opened with ch_logfile(). */ 132 // Log file opened with ch_logfile().
133 static FILE *log_fd = NULL; 133 static FILE *log_fd = NULL;
134 #ifdef FEAT_RELTIME 134 #ifdef FEAT_RELTIME
135 static proftime_T log_start; 135 static proftime_T log_start;
136 #endif 136 #endif
137 137
267 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), 267 MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
268 (LPTSTR) &msgbuf, 268 (LPTSTR) &msgbuf,
269 0, 269 0,
270 NULL); 270 NULL);
271 if (msgbuf != NULL) 271 if (msgbuf != NULL)
272 /* chomp \r or \n */ 272 // chomp \r or \n
273 for (ptr = (char_u *)msgbuf; *ptr; ptr++) 273 for (ptr = (char_u *)msgbuf; *ptr; ptr++)
274 switch (*ptr) 274 switch (*ptr)
275 { 275 {
276 case '\r': 276 case '\r':
277 STRMOVE(ptr, ptr + 1); 277 STRMOVE(ptr, ptr + 1);
350 { 350 {
351 int has_sock_msg; 351 int has_sock_msg;
352 int has_out_msg; 352 int has_out_msg;
353 int has_err_msg; 353 int has_err_msg;
354 354
355 /* If the job was killed the channel is not expected to work anymore. */ 355 // If the job was killed the channel is not expected to work anymore.
356 if (channel->ch_job_killed && channel->ch_job == NULL) 356 if (channel->ch_job_killed && channel->ch_job == NULL)
357 return FALSE; 357 return FALSE;
358 358
359 /* If there is a close callback it may still need to be invoked. */ 359 // If there is a close callback it may still need to be invoked.
360 if (channel->ch_close_cb.cb_name != NULL) 360 if (channel->ch_close_cb.cb_name != NULL)
361 return TRUE; 361 return TRUE;
362 362
363 /* If reading from or a buffer it's still useful. */ 363 // If reading from or a buffer it's still useful.
364 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL) 364 if (channel->ch_part[PART_IN].ch_bufref.br_buf != NULL)
365 return TRUE; 365 return TRUE;
366 366
367 /* If there is no callback then nobody can get readahead. If the fd is 367 // If there is no callback then nobody can get readahead. If the fd is
368 * closed and there is no readahead then the callback won't be called. */ 368 // closed and there is no readahead then the callback won't be called.
369 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD 369 has_sock_msg = channel->ch_part[PART_SOCK].ch_fd != INVALID_FD
370 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL 370 || channel->ch_part[PART_SOCK].ch_head.rq_next != NULL
371 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL; 371 || channel->ch_part[PART_SOCK].ch_json_head.jq_next != NULL;
372 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD 372 has_out_msg = channel->ch_part[PART_OUT].ch_fd != INVALID_FD
373 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL 373 || channel->ch_part[PART_OUT].ch_head.rq_next != NULL
466 free_unused_channels_contents(int copyID, int mask) 466 free_unused_channels_contents(int copyID, int mask)
467 { 467 {
468 int did_free = FALSE; 468 int did_free = FALSE;
469 channel_T *ch; 469 channel_T *ch;
470 470
471 /* This is invoked from the garbage collector, which only runs at a safe 471 // This is invoked from the garbage collector, which only runs at a safe
472 * point. */ 472 // point.
473 ++safe_to_invoke_callback; 473 ++safe_to_invoke_callback;
474 474
475 for (ch = first_channel; ch != NULL; ch = ch->ch_next) 475 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
476 if (!channel_still_useful(ch) 476 if (!channel_still_useful(ch)
477 && (ch->ch_copyID & mask) != (copyID & mask)) 477 && (ch->ch_copyID & mask) != (copyID & mask))
478 { 478 {
479 /* Free the channel and ordinary items it contains, but don't 479 // Free the channel and ordinary items it contains, but don't
480 * recurse into Lists, Dictionaries etc. */ 480 // recurse into Lists, Dictionaries etc.
481 channel_free_contents(ch); 481 channel_free_contents(ch);
482 did_free = TRUE; 482 did_free = TRUE;
483 } 483 }
484 484
485 --safe_to_invoke_callback; 485 --safe_to_invoke_callback;
496 { 496 {
497 ch_next = ch->ch_next; 497 ch_next = ch->ch_next;
498 if (!channel_still_useful(ch) 498 if (!channel_still_useful(ch)
499 && (ch->ch_copyID & mask) != (copyID & mask)) 499 && (ch->ch_copyID & mask) != (copyID & mask))
500 { 500 {
501 /* Free the channel struct itself. */ 501 // Free the channel struct itself.
502 channel_free_channel(ch); 502 channel_free_channel(ch);
503 } 503 }
504 } 504 }
505 } 505 }
506 506
564 messageFromServerGtk3(GIOChannel *unused1 UNUSED, 564 messageFromServerGtk3(GIOChannel *unused1 UNUSED,
565 GIOCondition unused2 UNUSED, 565 GIOCondition unused2 UNUSED,
566 gpointer clientData) 566 gpointer clientData)
567 { 567 {
568 channel_read_fd(GPOINTER_TO_INT(clientData)); 568 channel_read_fd(GPOINTER_TO_INT(clientData));
569 return TRUE; /* Return FALSE instead in case the event source is to 569 return TRUE; // Return FALSE instead in case the event source is to
570 * be removed after this function returns. */ 570 // be removed after this function returns.
571 } 571 }
572 # else 572 # else
573 static void 573 static void
574 messageFromServerGtk2(gpointer clientData, 574 messageFromServerGtk2(gpointer clientData,
575 gint unused1 UNUSED, 575 gint unused1 UNUSED,
584 channel_gui_register_one(channel_T *channel, ch_part_T part UNUSED) 584 channel_gui_register_one(channel_T *channel, ch_part_T part UNUSED)
585 { 585 {
586 if (!CH_HAS_GUI) 586 if (!CH_HAS_GUI)
587 return; 587 return;
588 588
589 /* gets stuck in handling events for a not connected channel */ 589 // gets stuck in handling events for a not connected channel
590 if (channel->ch_keep_open) 590 if (channel->ch_keep_open)
591 return; 591 return;
592 592
593 # ifdef FEAT_GUI_X11 593 # ifdef FEAT_GUI_X11
594 /* Tell notifier we are interested in being called when there is input on 594 // Tell notifier we are interested in being called when there is input on
595 * the editor connection socket. */ 595 // the editor connection socket.
596 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL) 596 if (channel->ch_part[part].ch_inputHandler == (XtInputId)NULL)
597 { 597 {
598 ch_log(channel, "Registering part %s with fd %d", 598 ch_log(channel, "Registering part %s with fd %d",
599 part_names[part], channel->ch_part[part].ch_fd); 599 part_names[part], channel->ch_part[part].ch_fd);
600 600
605 messageFromServerX11, 605 messageFromServerX11,
606 (XtPointer)(long)channel->ch_part[part].ch_fd); 606 (XtPointer)(long)channel->ch_part[part].ch_fd);
607 } 607 }
608 # else 608 # else
609 # ifdef FEAT_GUI_GTK 609 # ifdef FEAT_GUI_GTK
610 /* Tell gdk we are interested in being called when there is input on the 610 // Tell gdk we are interested in being called when there is input on the
611 * editor connection socket. */ 611 // editor connection socket.
612 if (channel->ch_part[part].ch_inputHandler == 0) 612 if (channel->ch_part[part].ch_inputHandler == 0)
613 { 613 {
614 ch_log(channel, "Registering part %s with fd %d", 614 ch_log(channel, "Registering part %s with fd %d",
615 part_names[part], channel->ch_part[part].ch_fd); 615 part_names[part], channel->ch_part[part].ch_fd);
616 # if GTK_CHECK_VERSION(3,0,0) 616 # if GTK_CHECK_VERSION(3,0,0)
738 { 738 {
739 ch_error(NULL, "Cannot allocate channel."); 739 ch_error(NULL, "Cannot allocate channel.");
740 return NULL; 740 return NULL;
741 } 741 }
742 742
743 /* Get the server internet address and put into addr structure */ 743 // Get the server internet address and put into addr structure
744 /* fill in the socket address structure and connect to server */ 744 // fill in the socket address structure and connect to server
745 vim_memset((char *)&server, 0, sizeof(server)); 745 vim_memset((char *)&server, 0, sizeof(server));
746 server.sin_family = AF_INET; 746 server.sin_family = AF_INET;
747 server.sin_port = htons(port); 747 server.sin_port = htons(port);
748 if ((host = gethostbyname(hostname)) == NULL) 748 if ((host = gethostbyname(hostname)) == NULL)
749 { 749 {
753 return NULL; 753 return NULL;
754 } 754 }
755 { 755 {
756 char *p; 756 char *p;
757 757
758 /* When using host->h_addr_list[0] directly ubsan warns for it to not 758 // When using host->h_addr_list[0] directly ubsan warns for it to not
759 * be aligned. First copy the pointer to avoid that. */ 759 // be aligned. First copy the pointer to avoid that.
760 memcpy(&p, &host->h_addr_list[0], sizeof(p)); 760 memcpy(&p, &host->h_addr_list[0], sizeof(p));
761 memcpy((char *)&server.sin_addr, p, host->h_length); 761 memcpy((char *)&server.sin_addr, p, host->h_length);
762 } 762 }
763 763
764 /* On Mac and Solaris a zero timeout almost never works. At least wait 764 // On Mac and Solaris a zero timeout almost never works. At least wait
765 * one millisecond. Let's do it for all systems, because we don't know why 765 // one millisecond. Let's do it for all systems, because we don't know why
766 * this is needed. */ 766 // this is needed.
767 if (waittime == 0) 767 if (waittime == 0)
768 waittime = 1; 768 waittime = 1;
769 769
770 /* 770 /*
771 * For Unix we need to call connect() again after connect() failed. 771 * For Unix we need to call connect() again after connect() failed.
787 return NULL; 787 return NULL;
788 } 788 }
789 789
790 if (waittime >= 0) 790 if (waittime >= 0)
791 { 791 {
792 /* Make connect() non-blocking. */ 792 // Make connect() non-blocking.
793 if ( 793 if (
794 #ifdef MSWIN 794 #ifdef MSWIN
795 ioctlsocket(sd, FIONBIO, &val) < 0 795 ioctlsocket(sd, FIONBIO, &val) < 0
796 #else 796 #else
797 fcntl(sd, F_SETFL, O_NONBLOCK) < 0 797 fcntl(sd, F_SETFL, O_NONBLOCK) < 0
805 channel_free(channel); 805 channel_free(channel);
806 return NULL; 806 return NULL;
807 } 807 }
808 } 808 }
809 809
810 /* Try connecting to the server. */ 810 // Try connecting to the server.
811 ch_log(channel, "Connecting to %s port %d", hostname, port); 811 ch_log(channel, "Connecting to %s port %d", hostname, port);
812 ret = connect(sd, (struct sockaddr *)&server, sizeof(server)); 812 ret = connect(sd, (struct sockaddr *)&server, sizeof(server));
813 813
814 if (ret == 0) 814 if (ret == 0)
815 /* The connection could be established. */ 815 // The connection could be established.
816 break; 816 break;
817 817
818 SOCK_ERRNO; 818 SOCK_ERRNO;
819 if (waittime < 0 || (errno != EWOULDBLOCK 819 if (waittime < 0 || (errno != EWOULDBLOCK
820 && errno != ECONNREFUSED 820 && errno != ECONNREFUSED
829 sock_close(sd); 829 sock_close(sd);
830 channel_free(channel); 830 channel_free(channel);
831 return NULL; 831 return NULL;
832 } 832 }
833 833
834 /* Limit the waittime to 50 msec. If it doesn't work within this 834 // Limit the waittime to 50 msec. If it doesn't work within this
835 * time we close the socket and try creating it again. */ 835 // time we close the socket and try creating it again.
836 waitnow = waittime > 50 ? 50 : waittime; 836 waitnow = waittime > 50 ? 50 : waittime;
837 837
838 /* If connect() didn't finish then try using select() to wait for the 838 // If connect() didn't finish then try using select() to wait for the
839 * connection to be made. For Win32 always use select() to wait. */ 839 // connection to be made. For Win32 always use select() to wait.
840 #ifndef MSWIN 840 #ifndef MSWIN
841 if (errno != ECONNREFUSED) 841 if (errno != ECONNREFUSED)
842 #endif 842 #endif
843 { 843 {
844 struct timeval tv; 844 struct timeval tv;
874 channel_free(channel); 874 channel_free(channel);
875 return NULL; 875 return NULL;
876 } 876 }
877 877
878 #ifdef MSWIN 878 #ifdef MSWIN
879 /* On Win32: select() is expected to work and wait for up to 879 // On Win32: select() is expected to work and wait for up to
880 * "waitnow" msec for the socket to be open. */ 880 // "waitnow" msec for the socket to be open.
881 if (FD_ISSET(sd, &wfds)) 881 if (FD_ISSET(sd, &wfds))
882 break; 882 break;
883 elapsed_msec = waitnow; 883 elapsed_msec = waitnow;
884 if (waittime > 1 && elapsed_msec < waittime) 884 if (waittime > 1 && elapsed_msec < waittime)
885 { 885 {
886 waittime -= elapsed_msec; 886 waittime -= elapsed_msec;
887 continue; 887 continue;
888 } 888 }
889 #else 889 #else
890 /* On Linux-like systems: See socket(7) for the behavior 890 // On Linux-like systems: See socket(7) for the behavior
891 * After putting the socket in non-blocking mode, connect() will 891 // After putting the socket in non-blocking mode, connect() will
892 * return EINPROGRESS, select() will not wait (as if writing is 892 // return EINPROGRESS, select() will not wait (as if writing is
893 * possible), need to use getsockopt() to check if the socket is 893 // possible), need to use getsockopt() to check if the socket is
894 * actually able to connect. 894 // actually able to connect.
895 * We detect a failure to connect when either read and write fds 895 // We detect a failure to connect when either read and write fds
896 * are set. Use getsockopt() to find out what kind of failure. */ 896 // are set. Use getsockopt() to find out what kind of failure.
897 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds)) 897 if (FD_ISSET(sd, &rfds) || FD_ISSET(sd, &wfds))
898 { 898 {
899 ret = getsockopt(sd, 899 ret = getsockopt(sd,
900 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len); 900 SOL_SOCKET, SO_ERROR, &so_error, &so_error_len);
901 if (ret < 0 || (so_error != 0 901 if (ret < 0 || (so_error != 0
915 return NULL; 915 return NULL;
916 } 916 }
917 } 917 }
918 918
919 if (FD_ISSET(sd, &wfds) && so_error == 0) 919 if (FD_ISSET(sd, &wfds) && so_error == 0)
920 /* Did not detect an error, connection is established. */ 920 // Did not detect an error, connection is established.
921 break; 921 break;
922 922
923 gettimeofday(&end_tv, NULL); 923 gettimeofday(&end_tv, NULL);
924 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000 924 elapsed_msec = (end_tv.tv_sec - start_tv.tv_sec) * 1000
925 + (end_tv.tv_usec - start_tv.tv_usec) / 1000; 925 + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
927 } 927 }
928 928
929 #ifndef MSWIN 929 #ifndef MSWIN
930 if (waittime > 1 && elapsed_msec < waittime) 930 if (waittime > 1 && elapsed_msec < waittime)
931 { 931 {
932 /* The port isn't ready but we also didn't get an error. 932 // The port isn't ready but we also didn't get an error.
933 * This happens when the server didn't open the socket 933 // This happens when the server didn't open the socket
934 * yet. Select() may return early, wait until the remaining 934 // yet. Select() may return early, wait until the remaining
935 * "waitnow" and try again. */ 935 // "waitnow" and try again.
936 waitnow -= elapsed_msec; 936 waitnow -= elapsed_msec;
937 waittime -= elapsed_msec; 937 waittime -= elapsed_msec;
938 if (waitnow > 0) 938 if (waitnow > 0)
939 { 939 {
940 mch_delay((long)waitnow, TRUE); 940 mch_delay((long)waitnow, TRUE);
942 waittime -= waitnow; 942 waittime -= waitnow;
943 } 943 }
944 if (!got_int) 944 if (!got_int)
945 { 945 {
946 if (waittime <= 0) 946 if (waittime <= 0)
947 /* give it one more try */ 947 // give it one more try
948 waittime = 1; 948 waittime = 1;
949 continue; 949 continue;
950 } 950 }
951 /* we were interrupted, behave as if timed out */ 951 // we were interrupted, behave as if timed out
952 } 952 }
953 #endif 953 #endif
954 954
955 /* We timed out. */ 955 // We timed out.
956 ch_error(channel, "Connection timed out"); 956 ch_error(channel, "Connection timed out");
957 sock_close(sd); 957 sock_close(sd);
958 channel_free(channel); 958 channel_free(channel);
959 return NULL; 959 return NULL;
960 } 960 }
1113 1113
1114 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER) 1114 if ((opt->jo_set & JO_OUT_IO) && opt->jo_io[PART_OUT] == JIO_BUFFER)
1115 { 1115 {
1116 buf_T *buf; 1116 buf_T *buf;
1117 1117
1118 /* writing output to a buffer. Default mode is NL. */ 1118 // writing output to a buffer. Default mode is NL.
1119 if (!(opt->jo_set & JO_OUT_MODE)) 1119 if (!(opt->jo_set & JO_OUT_MODE))
1120 channel->ch_part[PART_OUT].ch_mode = MODE_NL; 1120 channel->ch_part[PART_OUT].ch_mode = MODE_NL;
1121 if (opt->jo_set & JO_OUT_BUF) 1121 if (opt->jo_set & JO_OUT_BUF)
1122 { 1122 {
1123 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]); 1123 buf = buflist_findnr(opt->jo_io_buf[PART_OUT]);
1158 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO) 1158 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
1159 && opt->jo_io[PART_OUT] == JIO_BUFFER))) 1159 && opt->jo_io[PART_OUT] == JIO_BUFFER)))
1160 { 1160 {
1161 buf_T *buf; 1161 buf_T *buf;
1162 1162
1163 /* writing err to a buffer. Default mode is NL. */ 1163 // writing err to a buffer. Default mode is NL.
1164 if (!(opt->jo_set & JO_ERR_MODE)) 1164 if (!(opt->jo_set & JO_ERR_MODE))
1165 channel->ch_part[PART_ERR].ch_mode = MODE_NL; 1165 channel->ch_part[PART_ERR].ch_mode = MODE_NL;
1166 if (opt->jo_io[PART_ERR] == JIO_OUT) 1166 if (opt->jo_io[PART_ERR] == JIO_OUT)
1167 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf; 1167 buf = channel->ch_part[PART_OUT].ch_bufref.br_buf;
1168 else if (opt->jo_set & JO_ERR_BUF) 1168 else if (opt->jo_set & JO_ERR_BUF)
1224 { 1224 {
1225 emsg(_(e_invarg)); 1225 emsg(_(e_invarg));
1226 return NULL; 1226 return NULL;
1227 } 1227 }
1228 1228
1229 /* parse address */ 1229 // parse address
1230 p = vim_strchr(address, ':'); 1230 p = vim_strchr(address, ':');
1231 if (p == NULL) 1231 if (p == NULL)
1232 { 1232 {
1233 semsg(_(e_invarg2), address); 1233 semsg(_(e_invarg2), address);
1234 return NULL; 1234 return NULL;
1240 p[-1] = ':'; 1240 p[-1] = ':';
1241 semsg(_(e_invarg2), address); 1241 semsg(_(e_invarg2), address);
1242 return NULL; 1242 return NULL;
1243 } 1243 }
1244 1244
1245 /* parse options */ 1245 // parse options
1246 clear_job_options(&opt); 1246 clear_job_options(&opt);
1247 opt.jo_mode = MODE_JSON; 1247 opt.jo_mode = MODE_JSON;
1248 opt.jo_timeout = 2000; 1248 opt.jo_timeout = 2000;
1249 if (get_job_options(&argvars[1], &opt, 1249 if (get_job_options(&argvars[1], &opt,
1250 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL) 1250 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL, 0) == FAIL)
1275 { 1275 {
1276 if (part == PART_SOCK) 1276 if (part == PART_SOCK)
1277 sock_close(*fd); 1277 sock_close(*fd);
1278 else 1278 else
1279 { 1279 {
1280 /* When using a pty the same FD is set on multiple parts, only 1280 // When using a pty the same FD is set on multiple parts, only
1281 * close it when the last reference is closed. */ 1281 // close it when the last reference is closed.
1282 if ((part == PART_IN || channel->CH_IN_FD != *fd) 1282 if ((part == PART_IN || channel->CH_IN_FD != *fd)
1283 && (part == PART_OUT || channel->CH_OUT_FD != *fd) 1283 && (part == PART_OUT || channel->CH_OUT_FD != *fd)
1284 && (part == PART_ERR || channel->CH_ERR_FD != *fd)) 1284 && (part == PART_ERR || channel->CH_ERR_FD != *fd))
1285 { 1285 {
1286 #ifdef MSWIN 1286 #ifdef MSWIN
1290 fd_close(*fd); 1290 fd_close(*fd);
1291 } 1291 }
1292 } 1292 }
1293 *fd = INVALID_FD; 1293 *fd = INVALID_FD;
1294 1294
1295 /* channel is closed, may want to end the job if it was the last */ 1295 // channel is closed, may want to end the job if it was the last
1296 channel->ch_to_be_closed &= ~(1U << part); 1296 channel->ch_to_be_closed &= ~(1U << part);
1297 } 1297 }
1298 } 1298 }
1299 1299
1300 void 1300 void
1303 if (in != INVALID_FD) 1303 if (in != INVALID_FD)
1304 { 1304 {
1305 ch_close_part(channel, PART_IN); 1305 ch_close_part(channel, PART_IN);
1306 channel->CH_IN_FD = in; 1306 channel->CH_IN_FD = in;
1307 # if defined(UNIX) 1307 # if defined(UNIX)
1308 /* Do not end the job when all output channels are closed, wait until 1308 // Do not end the job when all output channels are closed, wait until
1309 * the job ended. */ 1309 // the job ended.
1310 if (mch_isatty(in)) 1310 if (mch_isatty(in))
1311 channel->ch_to_be_closed |= (1U << PART_IN); 1311 channel->ch_to_be_closed |= (1U << PART_IN);
1312 # endif 1312 # endif
1313 } 1313 }
1314 if (out != INVALID_FD) 1314 if (out != INVALID_FD)
1357 (char *)in_part->ch_bufref.br_buf->b_ffname); 1357 (char *)in_part->ch_bufref.br_buf->b_ffname);
1358 if (options->jo_set & JO_IN_TOP) 1358 if (options->jo_set & JO_IN_TOP)
1359 { 1359 {
1360 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT)) 1360 if (options->jo_in_top == 0 && !(options->jo_set & JO_IN_BOT))
1361 { 1361 {
1362 /* Special mode: send last-but-one line when appending a line 1362 // Special mode: send last-but-one line when appending a line
1363 * to the buffer. */ 1363 // to the buffer.
1364 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE; 1364 in_part->ch_bufref.br_buf->b_write_to_channel = TRUE;
1365 in_part->ch_buf_append = TRUE; 1365 in_part->ch_buf_append = TRUE;
1366 in_part->ch_buf_top = 1366 in_part->ch_buf_top =
1367 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1; 1367 in_part->ch_bufref.br_buf->b_ml.ml_line_count + 1;
1368 } 1368 }
1411 char_u *line = ml_get_buf(buf, lnum, FALSE); 1411 char_u *line = ml_get_buf(buf, lnum, FALSE);
1412 int len = (int)STRLEN(line); 1412 int len = (int)STRLEN(line);
1413 char_u *p; 1413 char_u *p;
1414 int i; 1414 int i;
1415 1415
1416 /* Need to make a copy to be able to append a NL. */ 1416 // Need to make a copy to be able to append a NL.
1417 if ((p = alloc(len + 2)) == NULL) 1417 if ((p = alloc(len + 2)) == NULL)
1418 return; 1418 return;
1419 memcpy((char *)p, (char *)line, len); 1419 memcpy((char *)p, (char *)line, len);
1420 1420
1421 if (channel->ch_write_text_mode) 1421 if (channel->ch_write_text_mode)
1441 can_write_buf_line(channel_T *channel) 1441 can_write_buf_line(channel_T *channel)
1442 { 1442 {
1443 chanpart_T *in_part = &channel->ch_part[PART_IN]; 1443 chanpart_T *in_part = &channel->ch_part[PART_IN];
1444 1444
1445 if (in_part->ch_fd == INVALID_FD) 1445 if (in_part->ch_fd == INVALID_FD)
1446 return FALSE; /* pipe was closed */ 1446 return FALSE; // pipe was closed
1447 1447
1448 /* for testing: block every other attempt to write */ 1448 // for testing: block every other attempt to write
1449 if (in_part->ch_block_write == 1) 1449 if (in_part->ch_block_write == 1)
1450 in_part->ch_block_write = -1; 1450 in_part->ch_block_write = -1;
1451 else if (in_part->ch_block_write == -1) 1451 else if (in_part->ch_block_write == -1)
1452 in_part->ch_block_write = 1; 1452 in_part->ch_block_write = 1;
1453 1453
1454 /* TODO: Win32 implementation, probably using WaitForMultipleObjects() */ 1454 // TODO: Win32 implementation, probably using WaitForMultipleObjects()
1455 #ifndef MSWIN 1455 #ifndef MSWIN
1456 { 1456 {
1457 # if defined(HAVE_SELECT) 1457 # if defined(HAVE_SELECT)
1458 struct timeval tval; 1458 struct timeval tval;
1459 fd_set wfds; 1459 fd_set wfds;
1512 linenr_T lnum; 1512 linenr_T lnum;
1513 buf_T *buf = in_part->ch_bufref.br_buf; 1513 buf_T *buf = in_part->ch_bufref.br_buf;
1514 int written = 0; 1514 int written = 0;
1515 1515
1516 if (buf == NULL || in_part->ch_buf_append) 1516 if (buf == NULL || in_part->ch_buf_append)
1517 return; /* no buffer or using appending */ 1517 return; // no buffer or using appending
1518 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL) 1518 if (!bufref_valid(&in_part->ch_bufref) || buf->b_ml.ml_mfp == NULL)
1519 { 1519 {
1520 /* buffer was wiped out or unloaded */ 1520 // buffer was wiped out or unloaded
1521 ch_log(channel, "input buffer has been wiped out"); 1521 ch_log(channel, "input buffer has been wiped out");
1522 in_part->ch_bufref.br_buf = NULL; 1522 in_part->ch_bufref.br_buf = NULL;
1523 return; 1523 return;
1524 } 1524 }
1525 1525
1539 1539
1540 in_part->ch_buf_top = lnum; 1540 in_part->ch_buf_top = lnum;
1541 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot) 1541 if (lnum > buf->b_ml.ml_line_count || lnum > in_part->ch_buf_bot)
1542 { 1542 {
1543 #if defined(FEAT_TERMINAL) 1543 #if defined(FEAT_TERMINAL)
1544 /* Send CTRL-D or "eof_chars" to close stdin on MS-Windows. */ 1544 // Send CTRL-D or "eof_chars" to close stdin on MS-Windows.
1545 if (channel->ch_job != NULL) 1545 if (channel->ch_job != NULL)
1546 term_send_eof(channel); 1546 term_send_eof(channel);
1547 #endif 1547 #endif
1548 1548
1549 /* Writing is done, no longer need the buffer. */ 1549 // Writing is done, no longer need the buffer.
1550 in_part->ch_bufref.br_buf = NULL; 1550 in_part->ch_bufref.br_buf = NULL;
1551 ch_log(channel, "Finished writing all lines to channel"); 1551 ch_log(channel, "Finished writing all lines to channel");
1552 1552
1553 /* Close the pipe/socket, so that the other side gets EOF. */ 1553 // Close the pipe/socket, so that the other side gets EOF.
1554 ch_close_part(channel, PART_IN); 1554 ch_close_part(channel, PART_IN);
1555 } 1555 }
1556 else 1556 else
1557 ch_log(channel, "Still %ld more lines to write", 1557 ch_log(channel, "Still %ld more lines to write",
1558 (long)(buf->b_ml.ml_line_count - lnum + 1)); 1558 (long)(buf->b_ml.ml_line_count - lnum + 1));
1619 channel_write_new_lines(buf_T *buf) 1619 channel_write_new_lines(buf_T *buf)
1620 { 1620 {
1621 channel_T *channel; 1621 channel_T *channel;
1622 int found_one = FALSE; 1622 int found_one = FALSE;
1623 1623
1624 /* There could be more than one channel for the buffer, loop over all of 1624 // There could be more than one channel for the buffer, loop over all of
1625 * them. */ 1625 // them.
1626 for (channel = first_channel; channel != NULL; channel = channel->ch_next) 1626 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
1627 { 1627 {
1628 chanpart_T *in_part = &channel->ch_part[PART_IN]; 1628 chanpart_T *in_part = &channel->ch_part[PART_IN];
1629 linenr_T lnum; 1629 linenr_T lnum;
1630 int written = 0; 1630 int written = 0;
1631 1631
1632 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append) 1632 if (in_part->ch_bufref.br_buf == buf && in_part->ch_buf_append)
1633 { 1633 {
1634 if (in_part->ch_fd == INVALID_FD) 1634 if (in_part->ch_fd == INVALID_FD)
1635 continue; /* pipe was closed */ 1635 continue; // pipe was closed
1636 found_one = TRUE; 1636 found_one = TRUE;
1637 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count; 1637 for (lnum = in_part->ch_buf_bot; lnum < buf->b_ml.ml_line_count;
1638 ++lnum) 1638 ++lnum)
1639 { 1639 {
1640 if (!can_write_buf_line(channel)) 1640 if (!can_write_buf_line(channel))
1721 1721
1722 if (node == NULL) 1722 if (node == NULL)
1723 return NULL; 1723 return NULL;
1724 if (outlen != NULL) 1724 if (outlen != NULL)
1725 *outlen += node->rq_buflen; 1725 *outlen += node->rq_buflen;
1726 /* dispose of the node but keep the buffer */ 1726 // dispose of the node but keep the buffer
1727 p = node->rq_buffer; 1727 p = node->rq_buffer;
1728 head->rq_next = node->rq_next; 1728 head->rq_next = node->rq_next;
1729 if (node->rq_next == NULL) 1729 if (node->rq_next == NULL)
1730 head->rq_prev = NULL; 1730 head->rq_prev = NULL;
1731 else 1731 else
1851 len += last_node->rq_buflen; 1851 len += last_node->rq_buflen;
1852 } 1852 }
1853 1853
1854 p = newbuf = alloc(len + 1); 1854 p = newbuf = alloc(len + 1);
1855 if (newbuf == NULL) 1855 if (newbuf == NULL)
1856 return FAIL; /* out of memory */ 1856 return FAIL; // out of memory
1857 mch_memmove(p, node->rq_buffer, node->rq_buflen); 1857 mch_memmove(p, node->rq_buffer, node->rq_buflen);
1858 p += node->rq_buflen; 1858 p += node->rq_buflen;
1859 vim_free(node->rq_buffer); 1859 vim_free(node->rq_buffer);
1860 node->rq_buffer = newbuf; 1860 node->rq_buffer = newbuf;
1861 for (n = node; n != last_node; ) 1861 for (n = node; n != last_node; )
1866 vim_free(n->rq_buffer); 1866 vim_free(n->rq_buffer);
1867 } 1867 }
1868 *p = NUL; 1868 *p = NUL;
1869 node->rq_buflen = (long_u)(p - newbuf); 1869 node->rq_buflen = (long_u)(p - newbuf);
1870 1870
1871 /* dispose of the collapsed nodes and their buffers */ 1871 // dispose of the collapsed nodes and their buffers
1872 for (n = node->rq_next; n != last_node; ) 1872 for (n = node->rq_next; n != last_node; )
1873 { 1873 {
1874 n = n->rq_next; 1874 n = n->rq_next;
1875 vim_free(n->rq_prev); 1875 vim_free(n->rq_prev);
1876 } 1876 }
1897 char_u *p; 1897 char_u *p;
1898 int i; 1898 int i;
1899 1899
1900 node = ALLOC_ONE(readq_T); 1900 node = ALLOC_ONE(readq_T);
1901 if (node == NULL) 1901 if (node == NULL)
1902 return FAIL; /* out of memory */ 1902 return FAIL; // out of memory
1903 /* A NUL is added at the end, because netbeans code expects that. 1903 // A NUL is added at the end, because netbeans code expects that.
1904 * Otherwise a NUL may appear inside the text. */ 1904 // Otherwise a NUL may appear inside the text.
1905 node->rq_buffer = alloc(len + 1); 1905 node->rq_buffer = alloc(len + 1);
1906 if (node->rq_buffer == NULL) 1906 if (node->rq_buffer == NULL)
1907 { 1907 {
1908 vim_free(node); 1908 vim_free(node);
1909 return FAIL; /* out of memory */ 1909 return FAIL; // out of memory
1910 } 1910 }
1911 1911
1912 if (channel->ch_part[part].ch_mode == MODE_NL) 1912 if (channel->ch_part[part].ch_mode == MODE_NL)
1913 { 1913 {
1914 /* Drop any CR before a NL. */ 1914 // Drop any CR before a NL.
1915 p = node->rq_buffer; 1915 p = node->rq_buffer;
1916 for (i = 0; i < len; ++i) 1916 for (i = 0; i < len; ++i)
1917 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL) 1917 if (buf[i] != CAR || i + 1 >= len || buf[i + 1] != NL)
1918 *p++ = buf[i]; 1918 *p++ = buf[i];
1919 *p = NUL; 1919 *p = NUL;
1977 return FALSE; 1977 return FALSE;
1978 1978
1979 keeplen = reader->js_end - reader->js_buf; 1979 keeplen = reader->js_end - reader->js_buf;
1980 if (keeplen > 0) 1980 if (keeplen > 0)
1981 { 1981 {
1982 /* Prepend unused text. */ 1982 // Prepend unused text.
1983 addlen = (int)STRLEN(next); 1983 addlen = (int)STRLEN(next);
1984 p = alloc(keeplen + addlen + 1); 1984 p = alloc(keeplen + addlen + 1);
1985 if (p == NULL) 1985 if (p == NULL)
1986 { 1986 {
1987 vim_free(next); 1987 vim_free(next);
2021 reader.js_used = 0; 2021 reader.js_used = 0;
2022 reader.js_fill = channel_fill; 2022 reader.js_fill = channel_fill;
2023 reader.js_cookie = channel; 2023 reader.js_cookie = channel;
2024 reader.js_cookie_arg = part; 2024 reader.js_cookie_arg = part;
2025 2025
2026 /* When a message is incomplete we wait for a short while for more to 2026 // When a message is incomplete we wait for a short while for more to
2027 * arrive. After the delay drop the input, otherwise a truncated string 2027 // arrive. After the delay drop the input, otherwise a truncated string
2028 * or list will make us hang. 2028 // or list will make us hang.
2029 * Do not generate error messages, they will be written in a channel log. */ 2029 // Do not generate error messages, they will be written in a channel log.
2030 ++emsg_silent; 2030 ++emsg_silent;
2031 status = json_decode(&reader, &listtv, 2031 status = json_decode(&reader, &listtv,
2032 chanpart->ch_mode == MODE_JS ? JSON_JS : 0); 2032 chanpart->ch_mode == MODE_JS ? JSON_JS : 0);
2033 --emsg_silent; 2033 --emsg_silent;
2034 if (status == OK) 2034 if (status == OK)
2035 { 2035 {
2036 /* Only accept the response when it is a list with at least two 2036 // Only accept the response when it is a list with at least two
2037 * items. */ 2037 // items.
2038 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2) 2038 if (listtv.v_type != VAR_LIST || listtv.vval.v_list->lv_len < 2)
2039 { 2039 {
2040 if (listtv.v_type != VAR_LIST) 2040 if (listtv.v_type != VAR_LIST)
2041 ch_error(channel, "Did not receive a list, discarding"); 2041 ch_error(channel, "Did not receive a list, discarding");
2042 else 2042 else
2079 { 2079 {
2080 size_t buflen = STRLEN(reader.js_buf); 2080 size_t buflen = STRLEN(reader.js_buf);
2081 2081
2082 if (chanpart->ch_wait_len < buflen) 2082 if (chanpart->ch_wait_len < buflen)
2083 { 2083 {
2084 /* First time encountering incomplete message or after receiving 2084 // First time encountering incomplete message or after receiving
2085 * more (but still incomplete): set a deadline of 100 msec. */ 2085 // more (but still incomplete): set a deadline of 100 msec.
2086 ch_log(channel, 2086 ch_log(channel,
2087 "Incomplete message (%d bytes) - wait 100 msec for more", 2087 "Incomplete message (%d bytes) - wait 100 msec for more",
2088 (int)buflen); 2088 (int)buflen);
2089 reader.js_used = 0; 2089 reader.js_used = 0;
2090 chanpart->ch_wait_len = buflen; 2090 chanpart->ch_wait_len = buflen;
2135 ret = FALSE; 2135 ret = FALSE;
2136 chanpart->ch_wait_len = 0; 2136 chanpart->ch_wait_len = 0;
2137 } 2137 }
2138 else if (reader.js_buf[reader.js_used] != NUL) 2138 else if (reader.js_buf[reader.js_used] != NUL)
2139 { 2139 {
2140 /* Put the unread part back into the channel. */ 2140 // Put the unread part back into the channel.
2141 channel_save(channel, part, reader.js_buf + reader.js_used, 2141 channel_save(channel, part, reader.js_buf + reader.js_used,
2142 (int)(reader.js_end - reader.js_buf) - reader.js_used, 2142 (int)(reader.js_end - reader.js_buf) - reader.js_used,
2143 TRUE, NULL); 2143 TRUE, NULL);
2144 ret = status == MAYBE ? FALSE: TRUE; 2144 ret = status == MAYBE ? FALSE: TRUE;
2145 } 2145 }
2294 jsonq_T *head = &channel->ch_part[part].ch_json_head; 2294 jsonq_T *head = &channel->ch_part[part].ch_json_head;
2295 jsonq_T *item = head->jq_next; 2295 jsonq_T *item = head->jq_next;
2296 jsonq_T *newitem; 2296 jsonq_T *newitem;
2297 2297
2298 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback) 2298 if (head->jq_prev != NULL && head->jq_prev->jq_no_callback)
2299 /* last item was pushed back, append to the end */ 2299 // last item was pushed back, append to the end
2300 item = NULL; 2300 item = NULL;
2301 else while (item != NULL && item->jq_no_callback) 2301 else while (item != NULL && item->jq_no_callback)
2302 /* append after the last item that was pushed back */ 2302 // append after the last item that was pushed back
2303 item = item->jq_next; 2303 item = item->jq_next;
2304 2304
2305 newitem = ALLOC_ONE(jsonq_T); 2305 newitem = ALLOC_ONE(jsonq_T);
2306 if (newitem == NULL) 2306 if (newitem == NULL)
2307 clear_tv(rettv); 2307 clear_tv(rettv);
2317 { 2317 {
2318 newitem->jq_no_callback = FALSE; 2318 newitem->jq_no_callback = FALSE;
2319 *newitem->jq_value = *rettv; 2319 *newitem->jq_value = *rettv;
2320 if (item == NULL) 2320 if (item == NULL)
2321 { 2321 {
2322 /* append to the end */ 2322 // append to the end
2323 newitem->jq_prev = head->jq_prev; 2323 newitem->jq_prev = head->jq_prev;
2324 head->jq_prev = newitem; 2324 head->jq_prev = newitem;
2325 newitem->jq_next = NULL; 2325 newitem->jq_next = NULL;
2326 if (newitem->jq_prev == NULL) 2326 if (newitem->jq_prev == NULL)
2327 head->jq_next = newitem; 2327 head->jq_next = newitem;
2328 else 2328 else
2329 newitem->jq_prev->jq_next = newitem; 2329 newitem->jq_prev->jq_next = newitem;
2330 } 2330 }
2331 else 2331 else
2332 { 2332 {
2333 /* append after "item" */ 2333 // append after "item"
2334 newitem->jq_prev = item; 2334 newitem->jq_prev = item;
2335 newitem->jq_next = item->jq_next; 2335 newitem->jq_next = item->jq_next;
2336 item->jq_next = newitem; 2336 item->jq_next = newitem;
2337 if (newitem->jq_next == NULL) 2337 if (newitem->jq_next == NULL)
2338 head->jq_prev = newitem; 2338 head->jq_prev = newitem;
2388 2388
2389 ch_log(channel, "Executing normal command '%s'", (char *)arg); 2389 ch_log(channel, "Executing normal command '%s'", (char *)arg);
2390 vim_memset(&ea, 0, sizeof(ea)); 2390 vim_memset(&ea, 0, sizeof(ea));
2391 ea.arg = arg; 2391 ea.arg = arg;
2392 ea.addr_count = 0; 2392 ea.addr_count = 0;
2393 ea.forceit = TRUE; /* no mapping */ 2393 ea.forceit = TRUE; // no mapping
2394 ex_normal(&ea); 2394 ex_normal(&ea);
2395 } 2395 }
2396 else if (STRCMP(cmd, "redraw") == 0) 2396 else if (STRCMP(cmd, "redraw") == 0)
2397 { 2397 {
2398 exarg_T ea; 2398 exarg_T ea;
2428 typval_T *tv = NULL; 2428 typval_T *tv = NULL;
2429 typval_T res_tv; 2429 typval_T res_tv;
2430 typval_T err_tv; 2430 typval_T err_tv;
2431 char_u *json = NULL; 2431 char_u *json = NULL;
2432 2432
2433 /* Don't pollute the display with errors. */ 2433 // Don't pollute the display with errors.
2434 ++emsg_skip; 2434 ++emsg_skip;
2435 if (!is_call) 2435 if (!is_call)
2436 { 2436 {
2437 ch_log(channel, "Evaluating expression '%s'", (char *)arg); 2437 ch_log(channel, "Evaluating expression '%s'", (char *)arg);
2438 tv = eval_expr(arg, NULL); 2438 tv = eval_expr(arg, NULL);
2450 2450
2451 if (tv != NULL) 2451 if (tv != NULL)
2452 json = json_encode_nr_expr(id, tv, options | JSON_NL); 2452 json = json_encode_nr_expr(id, tv, options | JSON_NL);
2453 if (tv == NULL || (json != NULL && *json == NUL)) 2453 if (tv == NULL || (json != NULL && *json == NUL))
2454 { 2454 {
2455 /* If evaluation failed or the result can't be encoded 2455 // If evaluation failed or the result can't be encoded
2456 * then return the string "ERROR". */ 2456 // then return the string "ERROR".
2457 vim_free(json); 2457 vim_free(json);
2458 err_tv.v_type = VAR_STRING; 2458 err_tv.v_type = VAR_STRING;
2459 err_tv.vval.v_string = (char_u *)"ERROR"; 2459 err_tv.vval.v_string = (char_u *)"ERROR";
2460 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL); 2460 json = json_encode_nr_expr(id, &err_tv, options | JSON_NL);
2461 } 2461 }
2492 cbq_T *item, 2492 cbq_T *item,
2493 typval_T *argv) 2493 typval_T *argv)
2494 { 2494 {
2495 ch_log(channel, "Invoking one-time callback %s", 2495 ch_log(channel, "Invoking one-time callback %s",
2496 (char *)item->cq_callback.cb_name); 2496 (char *)item->cq_callback.cb_name);
2497 /* Remove the item from the list first, if the callback 2497 // Remove the item from the list first, if the callback
2498 * invokes ch_close() the list will be cleared. */ 2498 // invokes ch_close() the list will be cleared.
2499 remove_cb_node(cbhead, item); 2499 remove_cb_node(cbhead, item);
2500 invoke_callback(channel, &item->cq_callback, argv); 2500 invoke_callback(channel, &item->cq_callback, argv);
2501 free_callback(&item->cq_callback); 2501 free_callback(&item->cq_callback);
2502 vim_free(item); 2502 vim_free(item);
2503 } 2503 }
2522 ch_part->ch_nomod_error = TRUE; 2522 ch_part->ch_nomod_error = TRUE;
2523 } 2523 }
2524 return; 2524 return;
2525 } 2525 }
2526 2526
2527 /* If the buffer is also used as input insert above the last 2527 // If the buffer is also used as input insert above the last
2528 * line. Don't write these lines. */ 2528 // line. Don't write these lines.
2529 if (save_write_to) 2529 if (save_write_to)
2530 { 2530 {
2531 --lnum; 2531 --lnum;
2532 buffer->b_write_to_channel = FALSE; 2532 buffer->b_write_to_channel = FALSE;
2533 } 2533 }
2534 2534
2535 /* Append to the buffer */ 2535 // Append to the buffer
2536 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty); 2536 ch_log(channel, "appending line %d to buffer", (int)lnum + 1 - empty);
2537 2537
2538 buffer->b_p_ma = TRUE; 2538 buffer->b_p_ma = TRUE;
2539 2539
2540 /* Save curbuf/curwin/curtab and make "buffer" the current buffer. */ 2540 // Save curbuf/curwin/curtab and make "buffer" the current buffer.
2541 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf); 2541 switch_to_win_for_buf(buffer, &save_curwin, &save_curtab, &save_curbuf);
2542 2542
2543 u_sync(TRUE); 2543 u_sync(TRUE);
2544 /* ignore undo failure, undo is not very useful here */ 2544 // ignore undo failure, undo is not very useful here
2545 vim_ignored = u_save(lnum - empty, lnum + 1); 2545 vim_ignored = u_save(lnum - empty, lnum + 1);
2546 2546
2547 if (empty) 2547 if (empty)
2548 { 2548 {
2549 /* The buffer is empty, replace the first (dummy) line. */ 2549 // The buffer is empty, replace the first (dummy) line.
2550 ml_replace(lnum, msg, TRUE); 2550 ml_replace(lnum, msg, TRUE);
2551 lnum = 0; 2551 lnum = 0;
2552 } 2552 }
2553 else 2553 else
2554 ml_append(lnum, msg, 0, FALSE); 2554 ml_append(lnum, msg, 0, FALSE);
2555 appended_lines_mark(lnum, 1L); 2555 appended_lines_mark(lnum, 1L);
2556 2556
2557 /* Restore curbuf/curwin/curtab */ 2557 // Restore curbuf/curwin/curtab
2558 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf); 2558 restore_win_for_buf(save_curwin, save_curtab, &save_curbuf);
2559 2559
2560 if (ch_part->ch_nomodifiable) 2560 if (ch_part->ch_nomodifiable)
2561 buffer->b_p_ma = FALSE; 2561 buffer->b_p_ma = FALSE;
2562 else 2562 else
2596 2596
2597 if (save_write_to) 2597 if (save_write_to)
2598 { 2598 {
2599 channel_T *ch; 2599 channel_T *ch;
2600 2600
2601 /* Find channels reading from this buffer and adjust their 2601 // Find channels reading from this buffer and adjust their
2602 * next-to-read line number. */ 2602 // next-to-read line number.
2603 buffer->b_write_to_channel = TRUE; 2603 buffer->b_write_to_channel = TRUE;
2604 for (ch = first_channel; ch != NULL; ch = ch->ch_next) 2604 for (ch = first_channel; ch != NULL; ch = ch->ch_next)
2605 { 2605 {
2606 chanpart_T *in_part = &ch->ch_part[PART_IN]; 2606 chanpart_T *in_part = &ch->ch_part[PART_IN];
2607 2607
2642 callback_T *callback = NULL; 2642 callback_T *callback = NULL;
2643 buf_T *buffer = NULL; 2643 buf_T *buffer = NULL;
2644 char_u *p; 2644 char_u *p;
2645 2645
2646 if (channel->ch_nb_close_cb != NULL) 2646 if (channel->ch_nb_close_cb != NULL)
2647 /* this channel is handled elsewhere (netbeans) */ 2647 // this channel is handled elsewhere (netbeans)
2648 return FALSE; 2648 return FALSE;
2649 2649
2650 /* Use a message-specific callback, part callback or channel callback */ 2650 // Use a message-specific callback, part callback or channel callback
2651 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next) 2651 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2652 if (cbitem->cq_seq_nr == 0) 2652 if (cbitem->cq_seq_nr == 0)
2653 break; 2653 break;
2654 if (cbitem != NULL) 2654 if (cbitem != NULL)
2655 callback = &cbitem->cq_callback; 2655 callback = &cbitem->cq_callback;
2660 2660
2661 buffer = ch_part->ch_bufref.br_buf; 2661 buffer = ch_part->ch_bufref.br_buf;
2662 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref) 2662 if (buffer != NULL && (!bufref_valid(&ch_part->ch_bufref)
2663 || buffer->b_ml.ml_mfp == NULL)) 2663 || buffer->b_ml.ml_mfp == NULL))
2664 { 2664 {
2665 /* buffer was wiped out or unloaded */ 2665 // buffer was wiped out or unloaded
2666 ch_log(channel, "%s buffer has been wiped out", part_names[part]); 2666 ch_log(channel, "%s buffer has been wiped out", part_names[part]);
2667 ch_part->ch_bufref.br_buf = NULL; 2667 ch_part->ch_bufref.br_buf = NULL;
2668 buffer = NULL; 2668 buffer = NULL;
2669 } 2669 }
2670 2670
2671 if (ch_mode == MODE_JSON || ch_mode == MODE_JS) 2671 if (ch_mode == MODE_JSON || ch_mode == MODE_JS)
2672 { 2672 {
2673 listitem_T *item; 2673 listitem_T *item;
2674 int argc = 0; 2674 int argc = 0;
2675 2675
2676 /* Get any json message in the queue. */ 2676 // Get any json message in the queue.
2677 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL) 2677 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
2678 { 2678 {
2679 /* Parse readahead, return when there is still no message. */ 2679 // Parse readahead, return when there is still no message.
2680 channel_parse_json(channel, part); 2680 channel_parse_json(channel, part);
2681 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL) 2681 if (channel_get_json(channel, part, -1, FALSE, &listtv) == FAIL)
2682 return FALSE; 2682 return FALSE;
2683 } 2683 }
2684 2684
2689 while (argc < CH_JSON_MAX_ARGS) 2689 while (argc < CH_JSON_MAX_ARGS)
2690 argv[argc++].v_type = VAR_UNKNOWN; 2690 argv[argc++].v_type = VAR_UNKNOWN;
2691 2691
2692 if (argv[0].v_type == VAR_STRING) 2692 if (argv[0].v_type == VAR_STRING)
2693 { 2693 {
2694 /* ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg] */ 2694 // ["cmd", arg] or ["cmd", arg, arg] or ["cmd", arg, arg, arg]
2695 channel_exe_cmd(channel, part, argv); 2695 channel_exe_cmd(channel, part, argv);
2696 free_tv(listtv); 2696 free_tv(listtv);
2697 return TRUE; 2697 return TRUE;
2698 } 2698 }
2699 2699
2706 } 2706 }
2707 seq_nr = argv[0].vval.v_number; 2707 seq_nr = argv[0].vval.v_number;
2708 } 2708 }
2709 else if (channel_peek(channel, part) == NULL) 2709 else if (channel_peek(channel, part) == NULL)
2710 { 2710 {
2711 /* nothing to read on RAW or NL channel */ 2711 // nothing to read on RAW or NL channel
2712 return FALSE; 2712 return FALSE;
2713 } 2713 }
2714 else 2714 else
2715 { 2715 {
2716 /* If there is no callback or buffer drop the message. */ 2716 // If there is no callback or buffer drop the message.
2717 if (callback == NULL && buffer == NULL) 2717 if (callback == NULL && buffer == NULL)
2718 { 2718 {
2719 /* If there is a close callback it may use ch_read() to get the 2719 // If there is a close callback it may use ch_read() to get the
2720 * messages. */ 2720 // messages.
2721 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never) 2721 if (channel->ch_close_cb.cb_name == NULL && !channel->ch_drop_never)
2722 drop_messages(channel, part); 2722 drop_messages(channel, part);
2723 return FALSE; 2723 return FALSE;
2724 } 2724 }
2725 2725
2727 { 2727 {
2728 char_u *nl = NULL; 2728 char_u *nl = NULL;
2729 char_u *buf; 2729 char_u *buf;
2730 readq_T *node; 2730 readq_T *node;
2731 2731
2732 /* See if we have a message ending in NL in the first buffer. If 2732 // See if we have a message ending in NL in the first buffer. If
2733 * not try to concatenate the first and the second buffer. */ 2733 // not try to concatenate the first and the second buffer.
2734 while (TRUE) 2734 while (TRUE)
2735 { 2735 {
2736 node = channel_peek(channel, part); 2736 node = channel_peek(channel, part);
2737 nl = channel_first_nl(node); 2737 nl = channel_first_nl(node);
2738 if (nl != NULL) 2738 if (nl != NULL)
2739 break; 2739 break;
2740 if (channel_collapse(channel, part, TRUE) == FAIL) 2740 if (channel_collapse(channel, part, TRUE) == FAIL)
2741 { 2741 {
2742 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0) 2742 if (ch_part->ch_fd == INVALID_FD && node->rq_buflen > 0)
2743 break; 2743 break;
2744 return FALSE; /* incomplete message */ 2744 return FALSE; // incomplete message
2745 } 2745 }
2746 } 2746 }
2747 buf = node->rq_buffer; 2747 buf = node->rq_buffer;
2748 2748
2749 // Convert NUL to NL, the internal representation. 2749 // Convert NUL to NL, the internal representation.
2763 msg = channel_get(channel, part, NULL); 2763 msg = channel_get(channel, part, NULL);
2764 *nl = NUL; 2764 *nl = NUL;
2765 } 2765 }
2766 else 2766 else
2767 { 2767 {
2768 /* Copy the message into allocated memory (excluding the NL) 2768 // Copy the message into allocated memory (excluding the NL)
2769 * and remove it from the buffer (including the NL). */ 2769 // and remove it from the buffer (including the NL).
2770 msg = vim_strnsave(buf, (int)(nl - buf)); 2770 msg = vim_strnsave(buf, (int)(nl - buf));
2771 channel_consume(channel, part, (int)(nl - buf) + 1); 2771 channel_consume(channel, part, (int)(nl - buf) + 1);
2772 } 2772 }
2773 } 2773 }
2774 else 2774 else
2775 { 2775 {
2776 /* For a raw channel we don't know where the message ends, just 2776 // For a raw channel we don't know where the message ends, just
2777 * get everything we have. 2777 // get everything we have.
2778 * Convert NUL to NL, the internal representation. */ 2778 // Convert NUL to NL, the internal representation.
2779 msg = channel_get_all(channel, part, NULL); 2779 msg = channel_get_all(channel, part, NULL);
2780 } 2780 }
2781 2781
2782 if (msg == NULL) 2782 if (msg == NULL)
2783 return FALSE; /* out of memory (and avoids Coverity warning) */ 2783 return FALSE; // out of memory (and avoids Coverity warning)
2784 2784
2785 argv[1].v_type = VAR_STRING; 2785 argv[1].v_type = VAR_STRING;
2786 argv[1].vval.v_string = msg; 2786 argv[1].vval.v_string = msg;
2787 } 2787 }
2788 2788
2789 if (seq_nr > 0) 2789 if (seq_nr > 0)
2790 { 2790 {
2791 int done = FALSE; 2791 int done = FALSE;
2792 2792
2793 /* JSON or JS mode: invoke the one-time callback with the matching nr */ 2793 // JSON or JS mode: invoke the one-time callback with the matching nr
2794 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next) 2794 for (cbitem = cbhead->cq_next; cbitem != NULL; cbitem = cbitem->cq_next)
2795 if (cbitem->cq_seq_nr == seq_nr) 2795 if (cbitem->cq_seq_nr == seq_nr)
2796 { 2796 {
2797 invoke_one_time_callback(channel, cbhead, cbitem, argv); 2797 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2798 done = TRUE; 2798 done = TRUE;
2800 } 2800 }
2801 if (!done) 2801 if (!done)
2802 { 2802 {
2803 if (channel->ch_drop_never) 2803 if (channel->ch_drop_never)
2804 { 2804 {
2805 /* message must be read with ch_read() */ 2805 // message must be read with ch_read()
2806 channel_push_json(channel, part, listtv); 2806 channel_push_json(channel, part, listtv);
2807 listtv = NULL; 2807 listtv = NULL;
2808 } 2808 }
2809 else 2809 else
2810 ch_log(channel, "Dropping message %d without callback", 2810 ch_log(channel, "Dropping message %d without callback",
2814 else if (callback != NULL || buffer != NULL) 2814 else if (callback != NULL || buffer != NULL)
2815 { 2815 {
2816 if (buffer != NULL) 2816 if (buffer != NULL)
2817 { 2817 {
2818 if (msg == NULL) 2818 if (msg == NULL)
2819 /* JSON or JS mode: re-encode the message. */ 2819 // JSON or JS mode: re-encode the message.
2820 msg = json_encode(listtv, ch_mode); 2820 msg = json_encode(listtv, ch_mode);
2821 if (msg != NULL) 2821 if (msg != NULL)
2822 { 2822 {
2823 #ifdef FEAT_TERMINAL 2823 #ifdef FEAT_TERMINAL
2824 if (buffer->b_term != NULL) 2824 if (buffer->b_term != NULL)
2833 { 2833 {
2834 if (cbitem != NULL) 2834 if (cbitem != NULL)
2835 invoke_one_time_callback(channel, cbhead, cbitem, argv); 2835 invoke_one_time_callback(channel, cbhead, cbitem, argv);
2836 else 2836 else
2837 { 2837 {
2838 /* invoke the channel callback */ 2838 // invoke the channel callback
2839 ch_log(channel, "Invoking channel callback %s", 2839 ch_log(channel, "Invoking channel callback %s",
2840 (char *)callback->cb_name); 2840 (char *)callback->cb_name);
2841 invoke_callback(channel, callback, argv); 2841 invoke_callback(channel, callback, argv);
2842 } 2842 }
2843 } 2843 }
2945 2945
2946 static void 2946 static void
2947 channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part) 2947 channel_part_info(channel_T *channel, dict_T *dict, char *name, ch_part_T part)
2948 { 2948 {
2949 chanpart_T *chanpart = &channel->ch_part[part]; 2949 chanpart_T *chanpart = &channel->ch_part[part];
2950 char namebuf[20]; /* longest is "sock_timeout" */ 2950 char namebuf[20]; // longest is "sock_timeout"
2951 size_t tail; 2951 size_t tail;
2952 char *status; 2952 char *status;
2953 char *s = ""; 2953 char *s = "";
2954 2954
2955 vim_strncpy((char_u *)namebuf, (char_u *)name, 4); 2955 vim_strncpy((char_u *)namebuf, (char_u *)name, 4);
3033 3033
3034 if (invoke_close_cb) 3034 if (invoke_close_cb)
3035 { 3035 {
3036 ch_part_T part; 3036 ch_part_T part;
3037 3037
3038 /* Invoke callbacks and flush buffers before the close callback. */ 3038 // Invoke callbacks and flush buffers before the close callback.
3039 if (channel->ch_close_cb.cb_name != NULL) 3039 if (channel->ch_close_cb.cb_name != NULL)
3040 ch_log(channel, 3040 ch_log(channel,
3041 "Invoking callbacks and flushing buffers before closing"); 3041 "Invoking callbacks and flushing buffers before closing");
3042 for (part = PART_SOCK; part < PART_IN; ++part) 3042 for (part = PART_SOCK; part < PART_IN; ++part)
3043 { 3043 {
3044 if (channel->ch_close_cb.cb_name != NULL 3044 if (channel->ch_close_cb.cb_name != NULL
3045 || channel->ch_part[part].ch_bufref.br_buf != NULL) 3045 || channel->ch_part[part].ch_bufref.br_buf != NULL)
3046 { 3046 {
3047 /* Increment the refcount to avoid the channel being freed 3047 // Increment the refcount to avoid the channel being freed
3048 * halfway. */ 3048 // halfway.
3049 ++channel->ch_refcount; 3049 ++channel->ch_refcount;
3050 if (channel->ch_close_cb.cb_name == NULL) 3050 if (channel->ch_close_cb.cb_name == NULL)
3051 ch_log(channel, "flushing %s buffers before closing", 3051 ch_log(channel, "flushing %s buffers before closing",
3052 part_names[part]); 3052 part_names[part]);
3053 while (may_invoke_callback(channel, part)) 3053 while (may_invoke_callback(channel, part))
3059 if (channel->ch_close_cb.cb_name != NULL) 3059 if (channel->ch_close_cb.cb_name != NULL)
3060 { 3060 {
3061 typval_T argv[1]; 3061 typval_T argv[1];
3062 typval_T rettv; 3062 typval_T rettv;
3063 3063
3064 /* Increment the refcount to avoid the channel being freed 3064 // Increment the refcount to avoid the channel being freed
3065 * halfway. */ 3065 // halfway.
3066 ++channel->ch_refcount; 3066 ++channel->ch_refcount;
3067 ch_log(channel, "Invoking close callback %s", 3067 ch_log(channel, "Invoking close callback %s",
3068 (char *)channel->ch_close_cb.cb_name); 3068 (char *)channel->ch_close_cb.cb_name);
3069 argv[0].v_type = VAR_CHANNEL; 3069 argv[0].v_type = VAR_CHANNEL;
3070 argv[0].vval.v_channel = channel; 3070 argv[0].vval.v_channel = channel;
3071 call_callback(&channel->ch_close_cb, -1, &rettv, 1, argv); 3071 call_callback(&channel->ch_close_cb, -1, &rettv, 1, argv);
3072 clear_tv(&rettv); 3072 clear_tv(&rettv);
3073 channel_need_redraw = TRUE; 3073 channel_need_redraw = TRUE;
3074 3074
3075 /* the callback is only called once */ 3075 // the callback is only called once
3076 free_callback(&channel->ch_close_cb); 3076 free_callback(&channel->ch_close_cb);
3077 3077
3078 if (channel_need_redraw) 3078 if (channel_need_redraw)
3079 { 3079 {
3080 channel_need_redraw = FALSE; 3080 channel_need_redraw = FALSE;
3081 redraw_after_callback(TRUE); 3081 redraw_after_callback(TRUE);
3082 } 3082 }
3083 3083
3084 if (!channel->ch_drop_never) 3084 if (!channel->ch_drop_never)
3085 /* any remaining messages are useless now */ 3085 // any remaining messages are useless now
3086 for (part = PART_SOCK; part < PART_IN; ++part) 3086 for (part = PART_SOCK; part < PART_IN; ++part)
3087 drop_messages(channel, part); 3087 drop_messages(channel, part);
3088 3088
3089 --channel->ch_refcount; 3089 --channel->ch_refcount;
3090 } 3090 }
3181 channel_clear(channel); 3181 channel_clear(channel);
3182 } 3182 }
3183 #endif 3183 #endif
3184 3184
3185 3185
3186 /* Sent when the netbeans channel is found closed when reading. */ 3186 // Sent when the netbeans channel is found closed when reading.
3187 #define DETACH_MSG_RAW "DETACH\n" 3187 #define DETACH_MSG_RAW "DETACH\n"
3188 3188
3189 /* Buffer size for reading incoming messages. */ 3189 // Buffer size for reading incoming messages.
3190 #define MAXMSGSIZE 4096 3190 #define MAXMSGSIZE 4096
3191 3191
3192 #if defined(HAVE_SELECT) 3192 #if defined(HAVE_SELECT)
3193 /* 3193 /*
3194 * Add write fds where we are waiting for writing to be possible. 3194 * Add write fds where we are waiting for writing to be possible.
3268 DWORD nread; 3268 DWORD nread;
3269 int sleep_time; 3269 int sleep_time;
3270 DWORD deadline = GetTickCount() + timeout; 3270 DWORD deadline = GetTickCount() + timeout;
3271 int delay = 1; 3271 int delay = 1;
3272 3272
3273 /* reading from a pipe, not a socket */ 3273 // reading from a pipe, not a socket
3274 while (TRUE) 3274 while (TRUE)
3275 { 3275 {
3276 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL); 3276 int r = PeekNamedPipe((HANDLE)fd, NULL, 0, NULL, &nread, NULL);
3277 3277
3278 if (r && nread > 0) 3278 if (r && nread > 0)
3284 ConnectNamedPipe((HANDLE)fd, NULL); 3284 ConnectNamedPipe((HANDLE)fd, NULL);
3285 } 3285 }
3286 else if (r == 0) 3286 else if (r == 0)
3287 return CW_ERROR; 3287 return CW_ERROR;
3288 3288
3289 /* perhaps write some buffer lines */ 3289 // perhaps write some buffer lines
3290 channel_write_any_lines(); 3290 channel_write_any_lines();
3291 3291
3292 sleep_time = deadline - GetTickCount(); 3292 sleep_time = deadline - GetTickCount();
3293 if (sleep_time <= 0) 3293 if (sleep_time <= 0)
3294 break; 3294 break;
3295 /* Wait for a little while. Very short at first, up to 10 msec 3295 // Wait for a little while. Very short at first, up to 10 msec
3296 * after looping a few times. */ 3296 // after looping a few times.
3297 if (sleep_time > delay) 3297 if (sleep_time > delay)
3298 sleep_time = delay; 3298 sleep_time = delay;
3299 Sleep(sleep_time); 3299 Sleep(sleep_time);
3300 delay = delay * 2; 3300 delay = delay * 2;
3301 if (delay > 10) 3301 if (delay > 10)
3317 for (;;) 3317 for (;;)
3318 { 3318 {
3319 FD_ZERO(&rfds); 3319 FD_ZERO(&rfds);
3320 FD_SET((int)fd, &rfds); 3320 FD_SET((int)fd, &rfds);
3321 3321
3322 /* Write lines to a pipe when a pipe can be written to. Need to 3322 // Write lines to a pipe when a pipe can be written to. Need to
3323 * set this every time, some buffers may be done. */ 3323 // set this every time, some buffers may be done.
3324 maxfd = (int)fd + 1; 3324 maxfd = (int)fd + 1;
3325 FD_ZERO(&wfds); 3325 FD_ZERO(&wfds);
3326 maxfd = channel_fill_wfds(maxfd, &wfds); 3326 maxfd = channel_fill_wfds(maxfd, &wfds);
3327 3327
3328 ret = select(maxfd, &rfds, &wfds, NULL, &tval); 3328 ret = select(maxfd, &rfds, &wfds, NULL, &tval);
3368 channel_T *channel, ch_part_T part, int is_err, char *func) 3368 channel_T *channel, ch_part_T part, int is_err, char *func)
3369 { 3369 {
3370 char msg[] = "%s(): Read %s from ch_part[%d], closing"; 3370 char msg[] = "%s(): Read %s from ch_part[%d], closing";
3371 3371
3372 if (is_err) 3372 if (is_err)
3373 /* Do not call emsg(), most likely the other end just exited. */ 3373 // Do not call emsg(), most likely the other end just exited.
3374 ch_error(channel, msg, func, "error", part); 3374 ch_error(channel, msg, func, "error", part);
3375 else 3375 else
3376 ch_log(channel, msg, func, "EOF", part); 3376 ch_log(channel, msg, func, "EOF", part);
3377 3377
3378 /* Queue a "DETACH" netbeans message in the command queue in order to 3378 // Queue a "DETACH" netbeans message in the command queue in order to
3379 * terminate the netbeans session later. Do not end the session here 3379 // terminate the netbeans session later. Do not end the session here
3380 * directly as we may be running in the context of a call to 3380 // directly as we may be running in the context of a call to
3381 * netbeans_parse_messages(): 3381 // netbeans_parse_messages():
3382 * netbeans_parse_messages 3382 // netbeans_parse_messages
3383 * -> autocmd triggered while processing the netbeans cmd 3383 // -> autocmd triggered while processing the netbeans cmd
3384 * -> ui_breakcheck 3384 // -> ui_breakcheck
3385 * -> gui event loop or select loop 3385 // -> gui event loop or select loop
3386 * -> channel_read() 3386 // -> channel_read()
3387 * Only send "DETACH" for a netbeans channel. 3387 // Only send "DETACH" for a netbeans channel.
3388 */
3389 if (channel->ch_nb_close_cb != NULL) 3388 if (channel->ch_nb_close_cb != NULL)
3390 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW, 3389 channel_save(channel, PART_SOCK, (char_u *)DETACH_MSG_RAW,
3391 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT "); 3390 (int)STRLEN(DETACH_MSG_RAW), FALSE, "PUT ");
3392 3391
3393 /* When reading is not possible close this part of the channel. Don't 3392 // When reading is not possible close this part of the channel. Don't
3394 * close the channel yet, there may be something to read on another part. 3393 // close the channel yet, there may be something to read on another part.
3395 * When stdout and stderr use the same FD we get the error only on one of 3394 // When stdout and stderr use the same FD we get the error only on one of
3396 * them, also close the other. */ 3395 // them, also close the other.
3397 if (part == PART_OUT || part == PART_ERR) 3396 if (part == PART_OUT || part == PART_ERR)
3398 { 3397 {
3399 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT; 3398 ch_part_T other = part == PART_OUT ? PART_ERR : PART_OUT;
3400 3399
3401 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd) 3400 if (channel->ch_part[part].ch_fd == channel->ch_part[other].ch_fd)
3402 ch_close_part(channel, other); 3401 ch_close_part(channel, other);
3403 } 3402 }
3404 ch_close_part(channel, part); 3403 ch_close_part(channel, part);
3405 3404
3406 #ifdef FEAT_GUI 3405 #ifdef FEAT_GUI
3407 /* Stop listening to GUI events right away. */ 3406 // Stop listening to GUI events right away.
3408 channel_gui_unregister_one(channel, part); 3407 channel_gui_unregister_one(channel, part);
3409 #endif 3408 #endif
3410 } 3409 }
3411 3410
3412 static void 3411 static void
3439 part_names[part]); 3438 part_names[part]);
3440 return; 3439 return;
3441 } 3440 }
3442 use_socket = fd == channel->CH_SOCK_FD; 3441 use_socket = fd == channel->CH_SOCK_FD;
3443 3442
3444 /* Allocate a buffer to read into. */ 3443 // Allocate a buffer to read into.
3445 if (buf == NULL) 3444 if (buf == NULL)
3446 { 3445 {
3447 buf = alloc(MAXMSGSIZE); 3446 buf = alloc(MAXMSGSIZE);
3448 if (buf == NULL) 3447 if (buf == NULL)
3449 return; /* out of memory! */ 3448 return; // out of memory!
3450 } 3449 }
3451 3450
3452 /* Keep on reading for as long as there is something to read. 3451 // Keep on reading for as long as there is something to read.
3453 * Use select() or poll() to avoid blocking on a message that is exactly 3452 // Use select() or poll() to avoid blocking on a message that is exactly
3454 * MAXMSGSIZE long. */ 3453 // MAXMSGSIZE long.
3455 for (;;) 3454 for (;;)
3456 { 3455 {
3457 if (channel_wait(channel, fd, 0) != CW_READY) 3456 if (channel_wait(channel, fd, 0) != CW_READY)
3458 break; 3457 break;
3459 if (use_socket) 3458 if (use_socket)
3460 len = sock_read(fd, (char *)buf, MAXMSGSIZE); 3459 len = sock_read(fd, (char *)buf, MAXMSGSIZE);
3461 else 3460 else
3462 len = fd_read(fd, (char *)buf, MAXMSGSIZE); 3461 len = fd_read(fd, (char *)buf, MAXMSGSIZE);
3463 if (len <= 0) 3462 if (len <= 0)
3464 break; /* error or nothing more to read */ 3463 break; // error or nothing more to read
3465 3464
3466 /* Store the read message in the queue. */ 3465 // Store the read message in the queue.
3467 channel_save(channel, part, buf, len, FALSE, "RECV "); 3466 channel_save(channel, part, buf, len, FALSE, "RECV ");
3468 readlen += len; 3467 readlen += len;
3469 if (len < MAXMSGSIZE) 3468 if (len < MAXMSGSIZE)
3470 break; /* did read everything that's available */ 3469 break; // did read everything that's available
3471 } 3470 }
3472 3471
3473 /* Reading a disconnection (readlen == 0), or an error. */ 3472 // Reading a disconnection (readlen == 0), or an error.
3474 if (readlen <= 0) 3473 if (readlen <= 0)
3475 { 3474 {
3476 if (!channel->ch_keep_open) 3475 if (!channel->ch_keep_open)
3477 ch_close_part_on_error(channel, part, (len < 0), func); 3476 ch_close_part_on_error(channel, part, (len < 0), func);
3478 } 3477 }
3479 #if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK) 3478 #if defined(CH_HAS_GUI) && defined(FEAT_GUI_GTK)
3480 else if (CH_HAS_GUI && gtk_main_level() > 0) 3479 else if (CH_HAS_GUI && gtk_main_level() > 0)
3481 /* signal the main loop that there is something to read */ 3480 // signal the main loop that there is something to read
3482 gtk_main_quit(); 3481 gtk_main_quit();
3483 #endif 3482 #endif
3484 } 3483 }
3485 3484
3486 /* 3485 /*
3510 node = channel_peek(channel, part); 3509 node = channel_peek(channel, part);
3511 if (node != NULL) 3510 if (node != NULL)
3512 { 3511 {
3513 if (mode == MODE_RAW || (mode == MODE_NL 3512 if (mode == MODE_RAW || (mode == MODE_NL
3514 && channel_first_nl(node) != NULL)) 3513 && channel_first_nl(node) != NULL))
3515 /* got a complete message */ 3514 // got a complete message
3516 break; 3515 break;
3517 if (channel_collapse(channel, part, mode == MODE_NL) == OK) 3516 if (channel_collapse(channel, part, mode == MODE_NL) == OK)
3518 continue; 3517 continue;
3519 /* If not blocking or nothing more is coming then return what we 3518 // If not blocking or nothing more is coming then return what we
3520 * have. */ 3519 // have.
3521 if (raw || fd == INVALID_FD) 3520 if (raw || fd == INVALID_FD)
3522 break; 3521 break;
3523 } 3522 }
3524 3523
3525 /* Wait for up to the channel timeout. */ 3524 // Wait for up to the channel timeout.
3526 if (fd == INVALID_FD) 3525 if (fd == INVALID_FD)
3527 return NULL; 3526 return NULL;
3528 if (channel_wait(channel, fd, timeout) != CW_READY) 3527 if (channel_wait(channel, fd, timeout) != CW_READY)
3529 { 3528 {
3530 ch_log(channel, "Timed out"); 3529 ch_log(channel, "Timed out");
3531 return NULL; 3530 return NULL;
3532 } 3531 }
3533 channel_read(channel, part, "channel_read_block"); 3532 channel_read(channel, part, "channel_read_block");
3534 } 3533 }
3535 3534
3536 /* We have a complete message now. */ 3535 // We have a complete message now.
3537 if (mode == MODE_RAW || outlen != NULL) 3536 if (mode == MODE_RAW || outlen != NULL)
3538 { 3537 {
3539 msg = channel_get_all(channel, part, outlen); 3538 msg = channel_get_all(channel, part, outlen);
3540 } 3539 }
3541 else 3540 else
3543 char_u *p; 3542 char_u *p;
3544 3543
3545 buf = node->rq_buffer; 3544 buf = node->rq_buffer;
3546 nl = channel_first_nl(node); 3545 nl = channel_first_nl(node);
3547 3546
3548 /* Convert NUL to NL, the internal representation. */ 3547 // Convert NUL to NL, the internal representation.
3549 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p) 3548 for (p = buf; (nl == NULL || p < nl) && p < buf + node->rq_buflen; ++p)
3550 if (*p == NUL) 3549 if (*p == NUL)
3551 *p = NL; 3550 *p = NL;
3552 3551
3553 if (nl == NULL) 3552 if (nl == NULL)
3554 { 3553 {
3555 /* must be a closed channel with missing NL */ 3554 // must be a closed channel with missing NL
3556 msg = channel_get(channel, part, NULL); 3555 msg = channel_get(channel, part, NULL);
3557 } 3556 }
3558 else if (nl + 1 == buf + node->rq_buflen) 3557 else if (nl + 1 == buf + node->rq_buflen)
3559 { 3558 {
3560 /* get the whole buffer */ 3559 // get the whole buffer
3561 msg = channel_get(channel, part, NULL); 3560 msg = channel_get(channel, part, NULL);
3562 *nl = NUL; 3561 *nl = NUL;
3563 } 3562 }
3564 else 3563 else
3565 { 3564 {
3566 /* Copy the message into allocated memory and remove it from the 3565 // Copy the message into allocated memory and remove it from the
3567 * buffer. */ 3566 // buffer.
3568 msg = vim_strnsave(buf, (int)(nl - buf)); 3567 msg = vim_strnsave(buf, (int)(nl - buf));
3569 channel_consume(channel, part, (int)(nl - buf) + 1); 3568 channel_consume(channel, part, (int)(nl - buf) + 1);
3570 } 3569 }
3571 } 3570 }
3572 if (ch_log_active()) 3571 if (ch_log_active())
3625 break; 3624 break;
3626 } 3625 }
3627 3626
3628 if (!more) 3627 if (!more)
3629 { 3628 {
3630 /* Handle any other messages in the queue. If done some more 3629 // Handle any other messages in the queue. If done some more
3631 * messages may have arrived. */ 3630 // messages may have arrived.
3632 if (channel_parse_messages()) 3631 if (channel_parse_messages())
3633 continue; 3632 continue;
3634 3633
3635 /* Wait for up to the timeout. If there was an incomplete message 3634 // Wait for up to the timeout. If there was an incomplete message
3636 * use the deadline for that. */ 3635 // use the deadline for that.
3637 timeout = timeout_arg; 3636 timeout = timeout_arg;
3638 if (chanpart->ch_wait_len > 0) 3637 if (chanpart->ch_wait_len > 0)
3639 { 3638 {
3640 #ifdef MSWIN 3639 #ifdef MSWIN
3641 timeout = chanpart->ch_deadline - GetTickCount() + 1; 3640 timeout = chanpart->ch_deadline - GetTickCount() + 1;
3651 + 1; 3650 + 1;
3652 } 3651 }
3653 #endif 3652 #endif
3654 if (timeout < 0) 3653 if (timeout < 0)
3655 { 3654 {
3656 /* Something went wrong, channel_parse_json() didn't 3655 // Something went wrong, channel_parse_json() didn't
3657 * discard message. Cancel waiting. */ 3656 // discard message. Cancel waiting.
3658 chanpart->ch_wait_len = 0; 3657 chanpart->ch_wait_len = 0;
3659 timeout = timeout_arg; 3658 timeout = timeout_arg;
3660 } 3659 }
3661 else if (timeout > timeout_arg) 3660 else if (timeout > timeout_arg)
3662 timeout = timeout_arg; 3661 timeout = timeout_arg;
3735 int mode; 3734 int mode;
3736 int timeout; 3735 int timeout;
3737 int id = -1; 3736 int id = -1;
3738 typval_T *listtv = NULL; 3737 typval_T *listtv = NULL;
3739 3738
3740 /* return an empty string by default */ 3739 // return an empty string by default
3741 rettv->v_type = VAR_STRING; 3740 rettv->v_type = VAR_STRING;
3742 rettv->vval.v_string = NULL; 3741 rettv->vval.v_string = NULL;
3743 3742
3744 clear_job_options(&opt); 3743 clear_job_options(&opt);
3745 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0) 3744 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID, 0)
3822 for (channel = first_channel; channel != NULL; channel = channel->ch_next) 3821 for (channel = first_channel; channel != NULL; channel = channel->ch_next)
3823 { 3822 {
3824 if (only_keep_open && !channel->ch_keep_open) 3823 if (only_keep_open && !channel->ch_keep_open)
3825 continue; 3824 continue;
3826 3825
3827 /* check the socket and pipes */ 3826 // check the socket and pipes
3828 for (part = PART_SOCK; part < PART_IN; ++part) 3827 for (part = PART_SOCK; part < PART_IN; ++part)
3829 { 3828 {
3830 fd = channel->ch_part[part].ch_fd; 3829 fd = channel->ch_part[part].ch_fd;
3831 if (fd != INVALID_FD) 3830 if (fd != INVALID_FD)
3832 { 3831 {
3931 char_u *buf; 3930 char_u *buf;
3932 int len; 3931 int len;
3933 3932
3934 if (wq->wq_next != NULL) 3933 if (wq->wq_next != NULL)
3935 { 3934 {
3936 /* first write what was queued */ 3935 // first write what was queued
3937 buf = wq->wq_next->wq_ga.ga_data; 3936 buf = wq->wq_next->wq_ga.ga_data;
3938 len = wq->wq_next->wq_ga.ga_len; 3937 len = wq->wq_next->wq_ga.ga_len;
3939 did_use_queue = TRUE; 3938 did_use_queue = TRUE;
3940 } 3939 }
3941 else 3940 else
3942 { 3941 {
3943 if (len_arg == 0) 3942 if (len_arg == 0)
3944 /* nothing to write, called from channel_select_check() */ 3943 // nothing to write, called from channel_select_check()
3945 return OK; 3944 return OK;
3946 buf = buf_arg; 3945 buf = buf_arg;
3947 len = len_arg; 3946 len = len_arg;
3948 } 3947 }
3949 3948
3963 if (res < 0 && (errno == EWOULDBLOCK 3962 if (res < 0 && (errno == EWOULDBLOCK
3964 #ifdef EAGAIN 3963 #ifdef EAGAIN
3965 || errno == EAGAIN 3964 || errno == EAGAIN
3966 #endif 3965 #endif
3967 )) 3966 ))
3968 res = 0; /* nothing got written */ 3967 res = 0; // nothing got written
3969 3968
3970 if (res >= 0 && ch_part->ch_nonblocking) 3969 if (res >= 0 && ch_part->ch_nonblocking)
3971 { 3970 {
3972 writeq_T *entry = wq->wq_next; 3971 writeq_T *entry = wq->wq_next;
3973 3972
3974 if (did_use_queue) 3973 if (did_use_queue)
3975 ch_log(channel, "Sent %d bytes now", res); 3974 ch_log(channel, "Sent %d bytes now", res);
3976 if (res == len) 3975 if (res == len)
3977 { 3976 {
3978 /* Wrote all the buf[len] bytes. */ 3977 // Wrote all the buf[len] bytes.
3979 if (entry != NULL) 3978 if (entry != NULL)
3980 { 3979 {
3981 /* Remove the entry from the write queue. */ 3980 // Remove the entry from the write queue.
3982 remove_from_writeque(wq, entry); 3981 remove_from_writeque(wq, entry);
3983 continue; 3982 continue;
3984 } 3983 }
3985 if (did_use_queue) 3984 if (did_use_queue)
3986 ch_log(channel, "Write queue empty"); 3985 ch_log(channel, "Write queue empty");
3987 } 3986 }
3988 else 3987 else
3989 { 3988 {
3990 /* Wrote only buf[res] bytes, can't write more now. */ 3989 // Wrote only buf[res] bytes, can't write more now.
3991 if (entry != NULL) 3990 if (entry != NULL)
3992 { 3991 {
3993 if (res > 0) 3992 if (res > 0)
3994 { 3993 {
3995 /* Remove the bytes that were written. */ 3994 // Remove the bytes that were written.
3996 mch_memmove(entry->wq_ga.ga_data, 3995 mch_memmove(entry->wq_ga.ga_data,
3997 (char *)entry->wq_ga.ga_data + res, 3996 (char *)entry->wq_ga.ga_data + res,
3998 len - res); 3997 len - res);
3999 entry->wq_ga.ga_len -= res; 3998 entry->wq_ga.ga_len -= res;
4000 } 3999 }
4006 buf += res; 4005 buf += res;
4007 len -= res; 4006 len -= res;
4008 } 4007 }
4009 ch_log(channel, "Adding %d bytes to the write queue", len); 4008 ch_log(channel, "Adding %d bytes to the write queue", len);
4010 4009
4011 /* Append the not written bytes of the argument to the write 4010 // Append the not written bytes of the argument to the write
4012 * buffer. Limit entries to 4000 bytes. */ 4011 // buffer. Limit entries to 4000 bytes.
4013 if (wq->wq_prev != NULL 4012 if (wq->wq_prev != NULL
4014 && wq->wq_prev->wq_ga.ga_len + len < 4000) 4013 && wq->wq_prev->wq_ga.ga_len + len < 4000)
4015 { 4014 {
4016 writeq_T *last = wq->wq_prev; 4015 writeq_T *last = wq->wq_prev;
4017 4016
4018 /* append to the last entry */ 4017 // append to the last entry
4019 if (len > 0 && ga_grow(&last->wq_ga, len) == OK) 4018 if (len > 0 && ga_grow(&last->wq_ga, len) == OK)
4020 { 4019 {
4021 mch_memmove((char *)last->wq_ga.ga_data 4020 mch_memmove((char *)last->wq_ga.ga_data
4022 + last->wq_ga.ga_len, 4021 + last->wq_ga.ga_len,
4023 buf, len); 4022 buf, len);
4091 *part_read = channel_part_read(channel); 4090 *part_read = channel_part_read(channel);
4092 4091
4093 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL) 4092 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT, 0) == FAIL)
4094 return NULL; 4093 return NULL;
4095 4094
4096 /* Set the callback. An empty callback means no callback and not reading 4095 // Set the callback. An empty callback means no callback and not reading
4097 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not 4096 // the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
4098 * allowed. */ 4097 // allowed.
4099 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL) 4098 if (opt->jo_callback.cb_name != NULL && *opt->jo_callback.cb_name != NUL)
4100 { 4099 {
4101 if (eval) 4100 if (eval)
4102 { 4101 {
4103 semsg(_("E917: Cannot use a callback with %s()"), fun); 4102 semsg(_("E917: Cannot use a callback with %s()"), fun);
4126 ch_part_T part_send; 4125 ch_part_T part_send;
4127 ch_part_T part_read; 4126 ch_part_T part_read;
4128 jobopt_T opt; 4127 jobopt_T opt;
4129 int timeout; 4128 int timeout;
4130 4129
4131 /* return an empty string by default */ 4130 // return an empty string by default
4132 rettv->v_type = VAR_STRING; 4131 rettv->v_type = VAR_STRING;
4133 rettv->vval.v_string = NULL; 4132 rettv->vval.v_string = NULL;
4134 4133
4135 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0); 4134 channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
4136 if (channel == NULL) 4135 if (channel == NULL)
4162 if (channel_read_json_block(channel, part_read, timeout, id, &listtv) 4161 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
4163 == OK) 4162 == OK)
4164 { 4163 {
4165 list_T *list = listtv->vval.v_list; 4164 list_T *list = listtv->vval.v_list;
4166 4165
4167 /* Move the item from the list and then change the type to 4166 // Move the item from the list and then change the type to
4168 * avoid the value being freed. */ 4167 // avoid the value being freed.
4169 *rettv = list->lv_last->li_tv; 4168 *rettv = list->lv_last->li_tv;
4170 list->lv_last->li_tv.v_type = VAR_NUMBER; 4169 list->lv_last->li_tv.v_type = VAR_NUMBER;
4171 free_tv(listtv); 4170 free_tv(listtv);
4172 } 4171 }
4173 } 4172 }
4186 channel_T *channel; 4185 channel_T *channel;
4187 ch_part_T part_read; 4186 ch_part_T part_read;
4188 jobopt_T opt; 4187 jobopt_T opt;
4189 int timeout; 4188 int timeout;
4190 4189
4191 /* return an empty string by default */ 4190 // return an empty string by default
4192 rettv->v_type = VAR_STRING; 4191 rettv->v_type = VAR_STRING;
4193 rettv->vval.v_string = NULL; 4192 rettv->vval.v_string = NULL;
4194 4193
4195 if (argvars[1].v_type == VAR_BLOB) 4194 if (argvars[1].v_type == VAR_BLOB)
4196 { 4195 {
4214 timeout, TRUE, NULL); 4213 timeout, TRUE, NULL);
4215 } 4214 }
4216 free_job_options(&opt); 4215 free_job_options(&opt);
4217 } 4216 }
4218 4217
4219 #define KEEP_OPEN_TIME 20 /* msec */ 4218 #define KEEP_OPEN_TIME 20 // msec
4220 4219
4221 #if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO) 4220 #if (defined(UNIX) && !defined(HAVE_SELECT)) || defined(PROTO)
4222 /* 4221 /*
4223 * Add open channels to the poll struct. 4222 * Add open channels to the poll struct.
4224 * Return the adjusted struct index. 4223 * Return the adjusted struct index.
4240 4239
4241 if (ch_part->ch_fd != INVALID_FD) 4240 if (ch_part->ch_fd != INVALID_FD)
4242 { 4241 {
4243 if (channel->ch_keep_open) 4242 if (channel->ch_keep_open)
4244 { 4243 {
4245 /* For unknown reason poll() returns immediately for a 4244 // For unknown reason poll() returns immediately for a
4246 * keep-open channel. Instead of adding it to the fds add 4245 // keep-open channel. Instead of adding it to the fds add
4247 * a short timeout and check, like polling. */ 4246 // a short timeout and check, like polling.
4248 if (*towait < 0 || *towait > KEEP_OPEN_TIME) 4247 if (*towait < 0 || *towait > KEEP_OPEN_TIME)
4249 *towait = KEEP_OPEN_TIME; 4248 *towait = KEEP_OPEN_TIME;
4250 } 4249 }
4251 else 4250 else
4252 { 4251 {
4291 --ret; 4290 --ret;
4292 } 4291 }
4293 else if (channel->ch_part[part].ch_fd != INVALID_FD 4292 else if (channel->ch_part[part].ch_fd != INVALID_FD
4294 && channel->ch_keep_open) 4293 && channel->ch_keep_open)
4295 { 4294 {
4296 /* polling a keep-open channel */ 4295 // polling a keep-open channel
4297 channel_read(channel, part, "channel_poll_check_keep_open"); 4296 channel_read(channel, part, "channel_poll_check_keep_open");
4298 } 4297 }
4299 } 4298 }
4300 4299
4301 in_part = &channel->ch_part[PART_IN]; 4300 in_part = &channel->ch_part[PART_IN];
4307 } 4306 }
4308 } 4307 }
4309 4308
4310 return ret; 4309 return ret;
4311 } 4310 }
4312 #endif /* UNIX && !HAVE_SELECT */ 4311 #endif // UNIX && !HAVE_SELECT
4313 4312
4314 #if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO) 4313 #if (!defined(MSWIN) && defined(HAVE_SELECT)) || defined(PROTO)
4315 4314
4316 /* 4315 /*
4317 * The "fd_set" type is hidden to avoid problems with the function proto. 4316 * The "fd_set" type is hidden to avoid problems with the function proto.
4338 4337
4339 if (fd != INVALID_FD) 4338 if (fd != INVALID_FD)
4340 { 4339 {
4341 if (channel->ch_keep_open) 4340 if (channel->ch_keep_open)
4342 { 4341 {
4343 /* For unknown reason select() returns immediately for a 4342 // For unknown reason select() returns immediately for a
4344 * keep-open channel. Instead of adding it to the rfds add 4343 // keep-open channel. Instead of adding it to the rfds add
4345 * a short timeout and check, like polling. */ 4344 // a short timeout and check, like polling.
4346 if (*tvp == NULL || tv->tv_sec > 0 4345 if (*tvp == NULL || tv->tv_sec > 0
4347 || tv->tv_usec > KEEP_OPEN_TIME * 1000) 4346 || tv->tv_usec > KEEP_OPEN_TIME * 1000)
4348 { 4347 {
4349 *tvp = tv; 4348 *tvp = tv;
4350 tv->tv_sec = 0; 4349 tv->tv_sec = 0;
4391 FD_CLR(fd, rfds); 4390 FD_CLR(fd, rfds);
4392 --ret; 4391 --ret;
4393 } 4392 }
4394 else if (fd != INVALID_FD && channel->ch_keep_open) 4393 else if (fd != INVALID_FD && channel->ch_keep_open)
4395 { 4394 {
4396 /* polling a keep-open channel */ 4395 // polling a keep-open channel
4397 channel_read(channel, part, "channel_select_check_keep_open"); 4396 channel_read(channel, part, "channel_select_check_keep_open");
4398 } 4397 }
4399 } 4398 }
4400 4399
4401 in_part = &channel->ch_part[PART_IN]; 4400 in_part = &channel->ch_part[PART_IN];
4432 ELAPSED_INIT(start_tv); 4431 ELAPSED_INIT(start_tv);
4433 #endif 4432 #endif
4434 4433
4435 ++safe_to_invoke_callback; 4434 ++safe_to_invoke_callback;
4436 4435
4437 /* Only do this message when another message was given, otherwise we get 4436 // Only do this message when another message was given, otherwise we get
4438 * lots of them. */ 4437 // lots of them.
4439 if ((did_repeated_msg & REPEATED_MSG_LOOKING) == 0) 4438 if ((did_repeated_msg & REPEATED_MSG_LOOKING) == 0)
4440 { 4439 {
4441 ch_log(NULL, "looking for messages on channels"); 4440 ch_log(NULL, "looking for messages on channels");
4442 // now we should also give the message for SafeState 4441 // now we should also give the message for SafeState
4443 did_repeated_msg = REPEATED_MSG_LOOKING; 4442 did_repeated_msg = REPEATED_MSG_LOOKING;
4473 continue; 4472 continue;
4474 } 4473 }
4475 if (channel->ch_part[part].ch_fd != INVALID_FD 4474 if (channel->ch_part[part].ch_fd != INVALID_FD
4476 || channel_has_readahead(channel, part)) 4475 || channel_has_readahead(channel, part))
4477 { 4476 {
4478 /* Increase the refcount, in case the handler causes the channel 4477 // Increase the refcount, in case the handler causes the channel
4479 * to be unreferenced or closed. */ 4478 // to be unreferenced or closed.
4480 ++channel->ch_refcount; 4479 ++channel->ch_refcount;
4481 r = may_invoke_callback(channel, part); 4480 r = may_invoke_callback(channel, part);
4482 if (r == OK) 4481 if (r == OK)
4483 ret = TRUE; 4482 ret = TRUE;
4484 if (channel_unref(channel) || (r == OK 4483 if (channel_unref(channel) || (r == OK
4485 #ifdef ELAPSED_FUNC 4484 #ifdef ELAPSED_FUNC
4486 /* Limit the time we loop here to 100 msec, otherwise 4485 // Limit the time we loop here to 100 msec, otherwise
4487 * Vim becomes unresponsive when the callback takes 4486 // Vim becomes unresponsive when the callback takes
4488 * more than a bit of time. */ 4487 // more than a bit of time.
4489 && ELAPSED_FUNC(start_tv) < 100L 4488 && ELAPSED_FUNC(start_tv) < 100L
4490 #endif 4489 #endif
4491 )) 4490 ))
4492 { 4491 {
4493 /* channel was freed or something was done, start over */ 4492 // channel was freed or something was done, start over
4494 channel = first_channel; 4493 channel = first_channel;
4495 part = PART_SOCK; 4494 part = PART_SOCK;
4496 continue; 4495 continue;
4497 } 4496 }
4498 } 4497 }
4981 break; 4980 break;
4982 opt->jo_set2 |= JO2_TERM_OPENCMD; 4981 opt->jo_set2 |= JO2_TERM_OPENCMD;
4983 p = opt->jo_term_opencmd = tv_get_string_chk(item); 4982 p = opt->jo_term_opencmd = tv_get_string_chk(item);
4984 if (p != NULL) 4983 if (p != NULL)
4985 { 4984 {
4986 /* Must have %d and no other %. */ 4985 // Must have %d and no other %.
4987 p = vim_strchr(p, '%'); 4986 p = vim_strchr(p, '%');
4988 if (p != NULL && (p[1] != 'd' 4987 if (p != NULL && (p[1] != 'd'
4989 || vim_strchr(p + 2, '%') != NULL)) 4988 || vim_strchr(p + 2, '%') != NULL))
4990 p = NULL; 4989 p = NULL;
4991 } 4990 }
5281 int i; 5280 int i;
5282 5281
5283 ch_log(job->jv_channel, "Freeing job"); 5282 ch_log(job->jv_channel, "Freeing job");
5284 if (job->jv_channel != NULL) 5283 if (job->jv_channel != NULL)
5285 { 5284 {
5286 /* The link from the channel to the job doesn't count as a reference, 5285 // The link from the channel to the job doesn't count as a reference,
5287 * thus don't decrement the refcount of the job. The reference from 5286 // thus don't decrement the refcount of the job. The reference from
5288 * the job to the channel does count the reference, decrement it and 5287 // the job to the channel does count the reference, decrement it and
5289 * NULL the reference. We don't set ch_job_killed, unreferencing the 5288 // NULL the reference. We don't set ch_job_killed, unreferencing the
5290 * job doesn't mean it stops running. */ 5289 // job doesn't mean it stops running.
5291 job->jv_channel->ch_job = NULL; 5290 job->jv_channel->ch_job = NULL;
5292 channel_unref(job->jv_channel); 5291 channel_unref(job->jv_channel);
5293 } 5292 }
5294 mch_clear_job(job); 5293 mch_clear_job(job);
5295 5294
5454 int i; 5453 int i;
5455 char_u *s, *d; 5454 char_u *s, *d;
5456 char_u *escaped_arg; 5455 char_u *escaped_arg;
5457 int has_spaces = FALSE; 5456 int has_spaces = FALSE;
5458 5457
5459 /* First count the number of extra bytes required. */ 5458 // First count the number of extra bytes required.
5460 slen = (int)STRLEN(arg); 5459 slen = (int)STRLEN(arg);
5461 dlen = slen; 5460 dlen = slen;
5462 for (s = arg; *s != NUL; MB_PTR_ADV(s)) 5461 for (s = arg; *s != NUL; MB_PTR_ADV(s))
5463 { 5462 {
5464 if (*s == '"' || *s == '\\') 5463 if (*s == '"' || *s == '\\')
5471 dlen += 2; 5470 dlen += 2;
5472 5471
5473 if (dlen == slen) 5472 if (dlen == slen)
5474 return vim_strsave(arg); 5473 return vim_strsave(arg);
5475 5474
5476 /* Allocate memory for the result and fill it. */ 5475 // Allocate memory for the result and fill it.
5477 escaped_arg = alloc(dlen + 1); 5476 escaped_arg = alloc(dlen + 1);
5478 if (escaped_arg == NULL) 5477 if (escaped_arg == NULL)
5479 return NULL; 5478 return NULL;
5480 memset(escaped_arg, 0, dlen+1); 5479 memset(escaped_arg, 0, dlen+1);
5481 5480
5504 MB_COPY_CHAR(s, d); 5503 MB_COPY_CHAR(s, d);
5505 break; 5504 break;
5506 } 5505 }
5507 } 5506 }
5508 5507
5509 /* add terminating quote and finish with a NUL */ 5508 // add terminating quote and finish with a NUL
5510 if (has_spaces) 5509 if (has_spaces)
5511 { 5510 {
5512 for (i = 0; i < escaping; i++) 5511 for (i = 0; i < escaping; i++)
5513 *d++ = '\\'; 5512 *d++ = '\\';
5514 *d++ = '"'; 5513 *d++ = '"';
5557 job_cleanup(job_T *job) 5556 job_cleanup(job_T *job)
5558 { 5557 {
5559 if (job->jv_status != JOB_ENDED) 5558 if (job->jv_status != JOB_ENDED)
5560 return; 5559 return;
5561 5560
5562 /* Ready to cleanup the job. */ 5561 // Ready to cleanup the job.
5563 job->jv_status = JOB_FINISHED; 5562 job->jv_status = JOB_FINISHED;
5564 5563
5565 /* When only channel-in is kept open, close explicitly. */ 5564 // When only channel-in is kept open, close explicitly.
5566 if (job->jv_channel != NULL) 5565 if (job->jv_channel != NULL)
5567 ch_close_part(job->jv_channel, PART_IN); 5566 ch_close_part(job->jv_channel, PART_IN);
5568 5567
5569 if (job->jv_exit_cb.cb_name != NULL) 5568 if (job->jv_exit_cb.cb_name != NULL)
5570 { 5569 {
5571 typval_T argv[3]; 5570 typval_T argv[3];
5572 typval_T rettv; 5571 typval_T rettv;
5573 5572
5574 /* Invoke the exit callback. Make sure the refcount is > 0. */ 5573 // Invoke the exit callback. Make sure the refcount is > 0.
5575 ch_log(job->jv_channel, "Invoking exit callback %s", 5574 ch_log(job->jv_channel, "Invoking exit callback %s",
5576 job->jv_exit_cb.cb_name); 5575 job->jv_exit_cb.cb_name);
5577 ++job->jv_refcount; 5576 ++job->jv_refcount;
5578 argv[0].v_type = VAR_JOB; 5577 argv[0].v_type = VAR_JOB;
5579 argv[0].vval.v_job = job; 5578 argv[0].vval.v_job = job;
5623 void 5622 void
5624 job_unref(job_T *job) 5623 job_unref(job_T *job)
5625 { 5624 {
5626 if (job != NULL && --job->jv_refcount <= 0) 5625 if (job != NULL && --job->jv_refcount <= 0)
5627 { 5626 {
5628 /* Do not free the job if there is a channel where the close callback 5627 // Do not free the job if there is a channel where the close callback
5629 * may get the job info. */ 5628 // may get the job info.
5630 if (!job_channel_still_useful(job)) 5629 if (!job_channel_still_useful(job))
5631 { 5630 {
5632 /* Do not free the job when it has not ended yet and there is a 5631 // Do not free the job when it has not ended yet and there is a
5633 * "stoponexit" flag or an exit callback. */ 5632 // "stoponexit" flag or an exit callback.
5634 if (!job_need_end_check(job)) 5633 if (!job_need_end_check(job))
5635 { 5634 {
5636 job_free(job); 5635 job_free(job);
5637 } 5636 }
5638 else if (job->jv_channel != NULL) 5637 else if (job->jv_channel != NULL)
5639 { 5638 {
5640 /* Do remove the link to the channel, otherwise it hangs 5639 // Do remove the link to the channel, otherwise it hangs
5641 * around until Vim exits. See job_free() for refcount. */ 5640 // around until Vim exits. See job_free() for refcount.
5642 ch_log(job->jv_channel, "detaching channel from job"); 5641 ch_log(job->jv_channel, "detaching channel from job");
5643 job->jv_channel->ch_job = NULL; 5642 job->jv_channel->ch_job = NULL;
5644 channel_unref(job->jv_channel); 5643 channel_unref(job->jv_channel);
5645 job->jv_channel = NULL; 5644 job->jv_channel = NULL;
5646 } 5645 }
5656 5655
5657 for (job = first_job; job != NULL; job = job->jv_next) 5656 for (job = first_job; job != NULL; job = job->jv_next)
5658 if ((job->jv_copyID & mask) != (copyID & mask) 5657 if ((job->jv_copyID & mask) != (copyID & mask)
5659 && !job_still_useful(job)) 5658 && !job_still_useful(job))
5660 { 5659 {
5661 /* Free the channel and ordinary items it contains, but don't 5660 // Free the channel and ordinary items it contains, but don't
5662 * recurse into Lists, Dictionaries etc. */ 5661 // recurse into Lists, Dictionaries etc.
5663 job_free_contents(job); 5662 job_free_contents(job);
5664 did_free = TRUE; 5663 did_free = TRUE;
5665 } 5664 }
5666 return did_free; 5665 return did_free;
5667 } 5666 }
5676 { 5675 {
5677 job_next = job->jv_next; 5676 job_next = job->jv_next;
5678 if ((job->jv_copyID & mask) != (copyID & mask) 5677 if ((job->jv_copyID & mask) != (copyID & mask)
5679 && !job_still_useful(job)) 5678 && !job_still_useful(job))
5680 { 5679 {
5681 /* Free the job struct itself. */ 5680 // Free the job struct itself.
5682 job_free_job(job); 5681 job_free_job(job);
5683 } 5682 }
5684 } 5683 }
5685 } 5684 }
5686 5685
5753 has_pending_job(void) 5752 has_pending_job(void)
5754 { 5753 {
5755 job_T *job; 5754 job_T *job;
5756 5755
5757 for (job = first_job; job != NULL; job = job->jv_next) 5756 for (job = first_job; job != NULL; job = job->jv_next)
5758 /* Only should check if the channel has been closed, if the channel is 5757 // Only should check if the channel has been closed, if the channel is
5759 * open the job won't exit. */ 5758 // open the job won't exit.
5760 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job)) 5759 if ((job->jv_status == JOB_STARTED && !job_channel_still_useful(job))
5761 || (job->jv_status == JOB_FINISHED 5760 || (job->jv_status == JOB_FINISHED
5762 && job_channel_can_close(job))) 5761 && job_channel_can_close(job)))
5763 return TRUE; 5762 return TRUE;
5764 return FALSE; 5763 return FALSE;
5841 5840
5842 if (opt_arg != NULL) 5841 if (opt_arg != NULL)
5843 opt = *opt_arg; 5842 opt = *opt_arg;
5844 else 5843 else
5845 { 5844 {
5846 /* Default mode is NL. */ 5845 // Default mode is NL.
5847 clear_job_options(&opt); 5846 clear_job_options(&opt);
5848 opt.jo_mode = MODE_NL; 5847 opt.jo_mode = MODE_NL;
5849 if (get_job_options(&argvars[1], &opt, 5848 if (get_job_options(&argvars[1], &opt,
5850 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT 5849 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL + JO_STOPONEXIT
5851 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE, 5850 + JO_EXIT_CB + JO_OUT_IO + JO_BLOCK_WRITE,
5852 JO2_ENV + JO2_CWD) == FAIL) 5851 JO2_ENV + JO2_CWD) == FAIL)
5853 goto theend; 5852 goto theend;
5854 } 5853 }
5855 5854
5856 /* Check that when io is "file" that there is a file name. */ 5855 // Check that when io is "file" that there is a file name.
5857 for (part = PART_OUT; part < PART_COUNT; ++part) 5856 for (part = PART_OUT; part < PART_COUNT; ++part)
5858 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT))) 5857 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
5859 && opt.jo_io[part] == JIO_FILE 5858 && opt.jo_io[part] == JIO_FILE
5860 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT))) 5859 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
5861 || *opt.jo_io_name[part] == NUL)) 5860 || *opt.jo_io_name[part] == NUL))
5866 5865
5867 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER) 5866 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
5868 { 5867 {
5869 buf_T *buf = NULL; 5868 buf_T *buf = NULL;
5870 5869
5871 /* check that we can find the buffer before starting the job */ 5870 // check that we can find the buffer before starting the job
5872 if (opt.jo_set & JO_IN_BUF) 5871 if (opt.jo_set & JO_IN_BUF)
5873 { 5872 {
5874 buf = buflist_findnr(opt.jo_io_buf[PART_IN]); 5873 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
5875 if (buf == NULL) 5874 if (buf == NULL)
5876 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]); 5875 semsg(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
5904 job_set_options(job, &opt); 5903 job_set_options(job, &opt);
5905 5904
5906 #ifdef USE_ARGV 5905 #ifdef USE_ARGV
5907 if (argv_arg != NULL) 5906 if (argv_arg != NULL)
5908 { 5907 {
5909 /* Make a copy of argv_arg for job->jv_argv. */ 5908 // Make a copy of argv_arg for job->jv_argv.
5910 for (i = 0; argv_arg[i] != NULL; i++) 5909 for (i = 0; argv_arg[i] != NULL; i++)
5911 argc++; 5910 argc++;
5912 argv = ALLOC_MULT(char *, argc + 1); 5911 argv = ALLOC_MULT(char *, argc + 1);
5913 if (argv == NULL) 5912 if (argv == NULL)
5914 goto theend; 5913 goto theend;
5918 } 5917 }
5919 else 5918 else
5920 #endif 5919 #endif
5921 if (argvars[0].v_type == VAR_STRING) 5920 if (argvars[0].v_type == VAR_STRING)
5922 { 5921 {
5923 /* Command is a string. */ 5922 // Command is a string.
5924 cmd = argvars[0].vval.v_string; 5923 cmd = argvars[0].vval.v_string;
5925 if (cmd == NULL || *cmd == NUL) 5924 if (cmd == NULL || *cmd == NUL)
5926 { 5925 {
5927 emsg(_(e_invarg)); 5926 emsg(_(e_invarg));
5928 goto theend; 5927 goto theend;
5949 goto theend; 5948 goto theend;
5950 cmd = ga.ga_data; 5949 cmd = ga.ga_data;
5951 #endif 5950 #endif
5952 } 5951 }
5953 5952
5954 /* Save the command used to start the job. */ 5953 // Save the command used to start the job.
5955 job->jv_argv = argv; 5954 job->jv_argv = argv;
5956 5955
5957 #ifdef USE_ARGV 5956 #ifdef USE_ARGV
5958 if (ch_log_active()) 5957 if (ch_log_active())
5959 { 5958 {
5974 #else 5973 #else
5975 ch_log(NULL, "Starting job: %s", (char *)cmd); 5974 ch_log(NULL, "Starting job: %s", (char *)cmd);
5976 mch_job_start((char *)cmd, job, &opt); 5975 mch_job_start((char *)cmd, job, &opt);
5977 #endif 5976 #endif
5978 5977
5979 /* If the channel is reading from a buffer, write lines now. */ 5978 // If the channel is reading from a buffer, write lines now.
5980 if (job->jv_channel != NULL) 5979 if (job->jv_channel != NULL)
5981 channel_write_in(job->jv_channel); 5980 channel_write_in(job->jv_channel);
5982 5981
5983 theend: 5982 theend:
5984 #ifndef USE_ARGV 5983 #ifndef USE_ARGV
5998 job_status(job_T *job) 5997 job_status(job_T *job)
5999 { 5998 {
6000 char *result; 5999 char *result;
6001 6000
6002 if (job->jv_status >= JOB_ENDED) 6001 if (job->jv_status >= JOB_ENDED)
6003 /* No need to check, dead is dead. */ 6002 // No need to check, dead is dead.
6004 result = "dead"; 6003 result = "dead";
6005 else if (job->jv_status == JOB_FAILED) 6004 else if (job->jv_status == JOB_FAILED)
6006 result = "fail"; 6005 result = "fail";
6007 else 6006 else
6008 { 6007 {
6048 } 6047 }
6049 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg); 6048 ch_log(job->jv_channel, "Stopping job with '%s'", (char *)arg);
6050 if (mch_signal_job(job, arg) == FAIL) 6049 if (mch_signal_job(job, arg) == FAIL)
6051 return 0; 6050 return 0;
6052 6051
6053 /* Assume that only "kill" will kill the job. */ 6052 // Assume that only "kill" will kill the job.
6054 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0) 6053 if (job->jv_channel != NULL && STRCMP(arg, "kill") == 0)
6055 job->jv_channel->ch_job_killed = TRUE; 6054 job->jv_channel->ch_job_killed = TRUE;
6056 6055
6057 /* We don't try freeing the job, obviously the caller still has a 6056 // We don't try freeing the job, obviously the caller still has a
6058 * reference to it. */ 6057 // reference to it.
6059 return 1; 6058 return 1;
6060 } 6059 }
6061 6060
6062 void 6061 void
6063 invoke_prompt_callback(void) 6062 invoke_prompt_callback(void)
6298 { 6297 {
6299 char_u *fname; 6298 char_u *fname;
6300 char_u *opt = (char_u *)""; 6299 char_u *opt = (char_u *)"";
6301 char_u buf[NUMBUFLEN]; 6300 char_u buf[NUMBUFLEN];
6302 6301
6303 /* Don't open a file in restricted mode. */ 6302 // Don't open a file in restricted mode.
6304 if (check_restricted() || check_secure()) 6303 if (check_restricted() || check_secure())
6305 return; 6304 return;
6306 fname = tv_get_string(&argvars[0]); 6305 fname = tv_get_string(&argvars[0]);
6307 if (argvars[1].v_type == VAR_STRING) 6306 if (argvars[1].v_type == VAR_STRING)
6308 opt = tv_get_string_buf(&argvars[1], buf); 6307 opt = tv_get_string_buf(&argvars[1], buf);
6411 { 6410 {
6412 channel_T *channel; 6411 channel_T *channel;
6413 jobopt_T opt; 6412 jobopt_T opt;
6414 int part = -1; 6413 int part = -1;
6415 6414
6416 /* return an empty string by default */ 6415 // return an empty string by default
6417 rettv->v_type = VAR_STRING; 6416 rettv->v_type = VAR_STRING;
6418 rettv->vval.v_string = NULL; 6417 rettv->vval.v_string = NULL;
6419 6418
6420 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0); 6419 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
6421 6420
6610 6609
6611 if (job != NULL) 6610 if (job != NULL)
6612 rettv->vval.v_number = job_stop(job, argvars, NULL); 6611 rettv->vval.v_number = job_stop(job, argvars, NULL);
6613 } 6612 }
6614 6613
6615 #endif /* FEAT_JOB_CHANNEL */ 6614 #endif // FEAT_JOB_CHANNEL