comparison src/channel.c @ 8210:b717dae2f26d v7.4.1398

commit https://github.com/vim/vim/commit/4e221c99e85ed40c98892068a01270b9e7492d98 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Feb 23 13:20:22 2016 +0100 patch 7.4.1398 Problem: The close-cb option is not implemented yet. Solution: Implemente close-cb. (Yasuhiro Matsumoto)
author Christian Brabandt <cb@256bit.org>
date Tue, 23 Feb 2016 13:30:07 +0100
parents 08d251a1c178
children 3456e2ebebd4
comparison
equal deleted inserted replaced
8209:30b7880d7522 8210:b717dae2f26d
483 * When negative wait forever. 483 * When negative wait forever.
484 * Returns the channel for success. 484 * Returns the channel for success.
485 * Returns NULL for failure. 485 * Returns NULL for failure.
486 */ 486 */
487 channel_T * 487 channel_T *
488 channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void)) 488 channel_open(
489 char *hostname,
490 int port_in,
491 int waittime,
492 void (*nb_close_cb)(void))
489 { 493 {
490 int sd = -1; 494 int sd = -1;
491 struct sockaddr_in server; 495 struct sockaddr_in server;
492 struct hostent *host; 496 struct hostent *host;
493 #ifdef WIN32 497 #ifdef WIN32
709 (void)fcntl(sd, F_SETFL, 0); 713 (void)fcntl(sd, F_SETFL, 0);
710 #endif 714 #endif
711 } 715 }
712 716
713 channel->CH_SOCK_FD = (sock_T)sd; 717 channel->CH_SOCK_FD = (sock_T)sd;
714 channel->ch_close_cb = close_cb; 718 channel->ch_nb_close_cb = nb_close_cb;
715 719
716 #ifdef FEAT_GUI 720 #ifdef FEAT_GUI
717 channel_gui_register(channel); 721 channel_gui_register(channel);
718 #endif 722 #endif
719 723
785 { 789 {
786 cbp = &channel->ch_part[PART_ERR].ch_callback; 790 cbp = &channel->ch_part[PART_ERR].ch_callback;
787 vim_free(*cbp); 791 vim_free(*cbp);
788 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL) 792 if (opt->jo_err_cb != NULL && *opt->jo_err_cb != NUL)
789 *cbp = vim_strsave(opt->jo_err_cb); 793 *cbp = vim_strsave(opt->jo_err_cb);
794 else
795 *cbp = NULL;
796 }
797 if (opt->jo_set & JO_CLOSE_CALLBACK)
798 {
799 cbp = &channel->ch_close_cb;
800 vim_free(*cbp);
801 if (opt->jo_close_cb != NULL && *opt->jo_close_cb != NUL)
802 *cbp = vim_strsave(opt->jo_close_cb);
790 else 803 else
791 *cbp = NULL; 804 *cbp = NULL;
792 } 805 }
793 } 806 }
794 807
1253 typval_T argv[CH_JSON_MAX_ARGS]; 1266 typval_T argv[CH_JSON_MAX_ARGS];
1254 int seq_nr = -1; 1267 int seq_nr = -1;
1255 ch_mode_T ch_mode = channel->ch_part[part].ch_mode; 1268 ch_mode_T ch_mode = channel->ch_part[part].ch_mode;
1256 char_u *callback = NULL; 1269 char_u *callback = NULL;
1257 1270
1258 if (channel->ch_close_cb != NULL) 1271 if (channel->ch_nb_close_cb != NULL)
1259 /* this channel is handled elsewhere (netbeans) */ 1272 /* this channel is handled elsewhere (netbeans) */
1260 return FALSE; 1273 return FALSE;
1261 1274
1262 if (channel->ch_part[part].ch_callback != NULL) 1275 if (channel->ch_part[part].ch_callback != NULL)
1263 callback = channel->ch_part[part].ch_callback; 1276 callback = channel->ch_part[part].ch_callback;
1475 fd_close(channel->CH_ERR_FD); 1488 fd_close(channel->CH_ERR_FD);
1476 channel->CH_ERR_FD = INVALID_FD; 1489 channel->CH_ERR_FD = INVALID_FD;
1477 } 1490 }
1478 #endif 1491 #endif
1479 1492
1480 channel->ch_close_cb = NULL; 1493 if (channel->ch_close_cb != NULL)
1494 {
1495 typval_T argv[1];
1496 typval_T rettv;
1497 int dummy;
1498
1499 /* invoke the close callback; increment the refcount to avoid it
1500 * being freed halfway */
1501 argv[0].v_type = VAR_CHANNEL;
1502 argv[0].vval.v_channel = channel;
1503 ++channel->ch_refcount;
1504 call_func(channel->ch_close_cb, (int)STRLEN(channel->ch_close_cb),
1505 &rettv, 1, argv, 0L, 0L, &dummy, TRUE, NULL);
1506 clear_tv(&rettv);
1507 --channel->ch_refcount;
1508
1509 /* the callback is only called once */
1510 vim_free(channel->ch_close_cb);
1511 channel->ch_close_cb = NULL;
1512 }
1513
1514 channel->ch_nb_close_cb = NULL;
1481 channel_clear(channel); 1515 channel_clear(channel);
1482 } 1516 }
1483 1517
1484 /* 1518 /*
1485 * Return the first buffer from "channel"/"part" without removing it. 1519 * Return the first buffer from "channel"/"part" without removing it.
1537 channel_clear_one(channel, PART_OUT); 1571 channel_clear_one(channel, PART_OUT);
1538 channel_clear_one(channel, PART_ERR); 1572 channel_clear_one(channel, PART_ERR);
1539 #endif 1573 #endif
1540 vim_free(channel->ch_callback); 1574 vim_free(channel->ch_callback);
1541 channel->ch_callback = NULL; 1575 channel->ch_callback = NULL;
1576 vim_free(channel->ch_close_cb);
1577 channel->ch_close_cb = NULL;
1542 } 1578 }
1543 1579
1544 #if defined(EXITFREE) || defined(PROTO) 1580 #if defined(EXITFREE) || defined(PROTO)
1545 void 1581 void
1546 channel_free_all(void) 1582 channel_free_all(void)
1730 1766
1731 /* TODO: When reading from stdout is not possible, should we try to 1767 /* TODO: When reading from stdout is not possible, should we try to
1732 * keep stdin and stderr open? Probably not, assume the other side 1768 * keep stdin and stderr open? Probably not, assume the other side
1733 * has died. */ 1769 * has died. */
1734 channel_close(channel); 1770 channel_close(channel);
1735 if (channel->ch_close_cb != NULL) 1771 if (channel->ch_nb_close_cb != NULL)
1736 (*channel->ch_close_cb)(); 1772 (*channel->ch_nb_close_cb)();
1737 1773
1738 if (len < 0) 1774 if (len < 0)
1739 { 1775 {
1740 ch_error(channel, "channel_read(): cannot read from channel"); 1776 ch_error(channel, "channel_read(): cannot read from channel");
1741 PERROR(_("E896: read from channel")); 1777 PERROR(_("E896: read from channel"));