-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoicb.c
813 lines (712 loc) · 18.5 KB
/
oicb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/*
* Copyright (c) 2014-2020 Vadim Zhukov <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <locale.h>
#include <netdb.h>
#include <poll.h>
#include <resolv.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <vis.h>
#include <signal.h>
#include <readline/readline.h>
#include "oicb.h"
#include "chat.h"
#include "history.h"
#include "private.h"
#include "utf8.h"
#ifndef HAVE_RL_BIND_KEYSEQ
static inline int rl_bind_keyseq(const char *keyseq, int(*function)(int, int));
#endif
enum ICBState state = Connecting;
enum SrvFeatures srv_features = Ping;
enum {
Network = 0,
Stdout,
Stdin,
MainFDCount
};
struct pollfd *pfd;
size_t npfd = 0;
static const char *stream_names[] = {
"network",
"stdout",
"stdin",
};
struct icb_task_queue tasks_stdout, tasks_net;
int debug = 0;
int sock = -1, histfile = -1;
volatile int want_exit = 0;
volatile int want_info = 0;
char *nick, *hostname, *room;
size_t nicklen;
char *o_rl_buf = NULL;
int o_rl_point, o_rl_mark;
int pings_sent = 0;
int utf8_ready = 0;
void usage(const char *msg);
void pledge_me(void);
int test_cmd(int count, int key);
size_t push_data(int fd, char *data, size_t len);
void proceed_output(struct icb_task_queue *q, int fd);
char *get_next_icb_msg(size_t *msglen);
void update_pollfds(void);
#ifdef SIGINFO
void siginfo_handler(int sig);
#endif
int siginfo_cmd(int count, int key);
void prepare_stdout(void);
void restore_rl(void);
void icb_connect(const char *addr, const char *port);
char *null_completer(const char *text, int cmpl_state);
// readline wrappers
int cycle_priv_chats_forward(int count, int key);
int cycle_priv_chats_backward(int count, int key);
int list_priv_chats_nicks_wrapper(int count, int key);
#ifdef SIGINFO
void
siginfo_handler(int sig) {
(void)sig;
want_info = 1;
}
#endif
int
siginfo_cmd(int count, int key) {
(void)count;
(void)key;
want_info = 1;
return 0;
}
void
prepare_stdout(void) {
char *p;
if (o_rl_buf != NULL)
errx(1, "%s: already called (internal error)", __func__);
o_rl_buf = strdup(rl_line_buffer);
if (o_rl_buf == NULL)
err(1, __func__);
o_rl_point = rl_point;
o_rl_mark = rl_mark;
for (p = rl_line_buffer; *p; p++)
*p = ' ';
rl_mark = 0;
rl_point = 0;
rl_redisplay();
}
void
restore_rl(void) {
size_t len;
if (repeat_priv_nick) {
rl_clear_message();
rl_point = 0;
if (prefer_long_priv_cmd)
rl_insert_text("/msg ");
else
rl_insert_text("/m ");
rl_insert_text(priv_chats_nicks[0]);
rl_insert_text(" ");
kill(getpid(), SIGWINCH);
repeat_priv_nick = 0;
} else {
len = strlen(o_rl_buf);
rl_extend_line_buffer(len+2);
memcpy(rl_line_buffer, o_rl_buf, len+1);
rl_point = o_rl_point;
rl_mark = o_rl_mark;
rl_redisplay();
}
free(o_rl_buf);
o_rl_buf = NULL;
}
/*
* Queue text coming from trusted source to be displayed.
*
* The data at 'text' won't be accessed after return, its contents will be
* copied to internal buffer for the further processing.
*
* Returns number of bytes queued, not including terminating NUL.
*/
int
push_stdout(const char *text, ...) {
struct icb_task *it;
int len;
va_list ap;
va_start(ap, text);
len = vsnprintf(NULL, 0, text, ap);
va_end(ap);
if (len == 0)
return 0;
it = calloc(1, sizeof(struct icb_task) + len + 1);
if (it == NULL)
err(1, __func__);
it->it_len = len + 1;
va_start(ap, text);
vsnprintf(it->it_data, len + 1, text, ap);
va_end(ap);
SIMPLEQ_INSERT_TAIL(&tasks_stdout, it, it_entry);
return len;
}
/*
* Queue text coming from possibly untrusted source to be displayed.
* The data will be processed with strvis(3) internally, if needed.
*
* The data at 'text' won't be accessed after return, its contents will be
* copied to internal buffer for the further processing.
*
* Returns number of bytes queued, not including terminating NUL.
*/
int
push_stdout_untrusted(const char *text, ...) {
struct icb_task *it, *tmp = NULL;
size_t len, buflen;
va_list ap;
char *fmtbuf;
va_start(ap, text);
len = vsnprintf(NULL, 0, text, ap);
va_end(ap);
if (len == 0)
return 0;
it = calloc(1, sizeof(struct icb_task) + len + 1);
if (it == NULL)
err(1, __func__);
va_start(ap, text);
vsnprintf(it->it_data, len + 1, text, ap);
va_end(ap);
if (utf8_ready && mbsvalidate(it->it_data) != -1) {
it->it_len = len + 1;
goto finish;
}
buflen = len * 4 + 1;
tmp = calloc(1, sizeof(struct icb_task) + buflen);
if (tmp == NULL)
err(1, __func__);
tmp->it_len = strvis(tmp->it_data, it->it_data, VIS_SAFE|VIS_NOSLASH|VIS_NL) + 1;
free(it);
it = tmp;
finish:
SIMPLEQ_INSERT_TAIL(&tasks_stdout, it, it_entry);
return (int)it->it_len;
}
// Input: line input from user
// Returns: 1 if valid command found, 0 otherwise
// Note: 'line' is not modified because struct fields being assigned are not.
int
parse_cmd_line(char *line, struct line_cmd *cmd) {
char *p = (char *)line;
memset(cmd, 0, sizeof(struct line_cmd));
if (*p != '/') {
if (debug >= 2)
warnx("%s: not a command, line='%s'",
__func__, line);
return 0;
}
cmd->start = p++;
if (!*p || isspace((unsigned char)*p)) {
if (debug >= 2)
warnx("%s: not a command name, line='%s'",
__func__, line);
return 0;
}
cmd->cmd_name = p;
while (*p && !isspace((unsigned char)*p))
p++;
cmd->cmd_name_end = p;
cmd->cmd_name_len = (int)(p - cmd->cmd_name);
if (*p)
cmd->has_args = 1;
else
goto end;
if ((cmd->cmd_name_len == 1 && cmd->cmd_name[0] == 'm') ||
(cmd->cmd_name_len == 3 && memcmp(cmd->cmd_name, "msg", 3) == 0)) {
cmd->is_private = 1;
while (isspace((unsigned char)*p))
p++;
if (!*p) {
cmd->private_prefix_len = (int)(p - line);
goto end;
}
cmd->peer_nick = p;
cmd->peer_nick_offset = (int)(p - line);
while (*p && !isspace((unsigned char)*p))
p++;
cmd->peer_nick_end = p;
cmd->peer_nick_len = (int)(p - cmd->peer_nick);
cmd->private_prefix_len = (int)(p - line);
if (isspace((unsigned char)*p)) {
cmd->private_msg = ++p;
cmd->private_prefix_len++;
}
}
end:
if (debug >= 2)
warnx("%s: cmd_name='%.*s' nick_name='%.*s' private_msg='%s'",
__func__, cmd->cmd_name_len, cmd->cmd_name,
cmd->peer_nick_len, cmd->peer_nick,
cmd->private_msg ? cmd->private_msg : "(null)");
return 1;
}
int
cycle_priv_chats_forward(int count, int key) {
(void)key;
while (count-- > 0)
cycle_priv_chats(1);
return 0;
}
int
cycle_priv_chats_backward(int count, int key) {
(void)key;
while (count-- > 0)
cycle_priv_chats(0);
return 0;
}
int
list_priv_chats_nicks_wrapper(int count, int key) {
(void)count;
(void)key;
list_priv_chats_nicks();
return 0;
}
size_t
push_data(int fd, char *data, size_t len) {
ssize_t nwritten = 0;
size_t total = 0;
while (len > 0 && (nwritten = write(fd, data, len)) >= 0) {
len -= (size_t) nwritten;
data += nwritten;
total += nwritten;
}
if (nwritten != -1 || errno == EAGAIN)
return total;
err(2, __func__);
}
/*
* Push queued text.
*/
void
proceed_output(struct icb_task_queue *q, int fd) {
struct icb_task *it;
size_t nwritten;
while (!SIMPLEQ_EMPTY(q)) {
it = SIMPLEQ_FIRST(q);
nwritten = push_data(fd, it->it_data + it->it_ndone,
it->it_len - it->it_ndone);
if (debug >= 2) {
warnx("output %zu from %zu bytes at fileno %d",
it->it_ndone, it->it_len, fd);
}
it->it_ndone += nwritten;
if (it->it_ndone < it->it_len)
break;
SIMPLEQ_REMOVE_HEAD(q, it_entry);
if (it->it_cb)
(*it->it_cb)(it);
free(it);
}
}
/*
* Extract next incoming ICB message on the network socket.
*
* Returned pointer contains message type in the first byte,
* with data bytes following it. Data always ends with NUL,
* which isn't taken into account of msglen returned.
*
* Returned pointer will be valid until next call of get_next_icb_msg().
*/
char*
get_next_icb_msg(size_t *msglen) {
static unsigned char *buf = NULL, *msgend = NULL;
static size_t bufread = 0, bufsize = 1024;
unsigned char *lastpkt, *pkt, *nbuf;
size_t roundread = 0;
ssize_t nread, shift;
if (buf == NULL) {
if ((buf = malloc(bufsize)) == NULL)
err(1, "%s: malloc", __func__);
} else if (msgend) {
// resetting state
memmove(buf, msgend, bufread - (msgend - buf));
bufread -= (msgend - buf);
msgend = NULL;
}
for (;;) {
// Reserve one byte for ending NUL in case it's missing in the
// input packet, see the "msglen++" block at the end of function.
if (bufread == bufsize - 1) {
if (bufsize >= 1024*1024)
err(2, "too long message");
if ((nbuf = reallocarray(buf, 2, bufsize)) == NULL)
err(1, "%s: reallocarray", __func__);
buf = nbuf;
bufsize *= 2;
}
nread = read(sock, buf + bufread, bufsize - bufread - 1);
if (nread < 0) {
if (errno != EAGAIN)
err(1, "%s: read", __func__);
break;
} else if (nread == 0) {
push_stdout("Server %s closed connection, exiting...\n",
hostname);
want_exit = 1;
break;
}
roundread += nread;
bufread += nread;
}
if (bufread == 0 && roundread == 0)
return NULL;
for (lastpkt = buf; lastpkt[0] == 0; lastpkt += 256) {
if (buf + bufread - 256 < lastpkt)
return NULL; // not received ending packet yet
}
// now lastpkt points to the ending packet
if (buf + bufread - lastpkt[0] < lastpkt)
return NULL; // not received ending packet fully yet
msgend = lastpkt + 1 + lastpkt[0];
// got full message, now remove extra data to get continious bytes
for (pkt = buf; pkt <= lastpkt; pkt += 256) {
if (pkt[1] != lastpkt[1])
// XXX Or just ignore? Which to use then?
err(2, "message types messed up in a single message");
if (pkt != buf) {
shift = (pkt[-1] == '\0') ? 3 : 2;
memmove(pkt + 2 - shift, pkt + 2, buf + bufread - (pkt + 2));
bufread -= shift;
lastpkt -= shift;
pkt -= shift;
}
}
// There always will be a place for NUL, see main read loop.
if (msgend[-1] != '\0') {
memmove(msgend + 1, msgend, bufread - (msgend - buf));
*msgend++ = '\0';
}
// -1 to skip initial byte count byte, another -1 for trailing NUL
*msglen = msgend - buf - 2;
return (char*)(buf + 1);
}
char *
null_completer(const char *text, int cmpl_state) {
(void)text;
(void)cmpl_state;
return NULL;
}
__dead void
usage(const char *msg) {
if (msg)
fprintf(stderr, "%s\n", msg);
fprintf(stderr, "usage: %s [-dH] [-t secs] [nick@]host[:port] room\n",
getprogname());
exit (1);
}
void
update_pollfds(void) {
const struct history_file *hfile;
size_t newnpfd;
int i;
newnpfd = MainFDCount;
LIST_FOREACH(hfile, &history_files, hf_entry)
newnpfd++;
if (npfd < newnpfd) {
pfd = reallocarray(pfd, newnpfd, sizeof(struct pollfd));
if (pfd == NULL)
err(1, __func__);
npfd = newnpfd;
}
memset(pfd, 0, sizeof(struct pollfd) * npfd);
pfd[Stdin].fd = STDIN_FILENO;
pfd[Stdin].events = (state == Connecting) ? 0 : POLLIN;
pfd[Stdout].fd = STDOUT_FILENO;
pfd[Stdout].events = 0;
if (!SIMPLEQ_EMPTY(&tasks_stdout))
pfd[Stdout].events |= POLLOUT;
pfd[Network].fd = sock;
pfd[Network].events = POLLIN;
if (!SIMPLEQ_EMPTY(&tasks_net))
pfd[Network].events |= POLLOUT;
i = 0;
LIST_FOREACH(hfile, &history_files, hf_entry) {
pfd[MainFDCount + i].fd = hfile->hf_fd;
if (hfile->hf_ntasks)
pfd[MainFDCount + i].events = POLLOUT;
i++;
}
}
void
icb_connect(const char *addr, const char *port) {
struct addrinfo *res, *p, hints;
int ec;
if (port == NULL)
port = "7326";
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((ec = getaddrinfo(addr, port, &hints, &res)) != 0)
errx(1, "could not resolve host/port name: %s",
gai_strerror(ec));
for (p = res; p != NULL; p = p->ai_next) {
if ((sock = socket(p->ai_family, p->ai_socktype|SOCK_NONBLOCK,
p->ai_protocol)) == -1) {
warn("%s: socket", __func__);
continue;
}
if (connect(sock, (struct sockaddr *)p->ai_addr,
p->ai_addrlen) == -1) {
if (errno != EINPROGRESS) {
/* make sure we'll report connect()'s error */
ec = errno;
close(sock);
errno = ec;
continue;
}
state = Connecting;
push_stdout("Connecting to %s ... ", hostname);
} else {
state = Connected;
push_stdout("Connected to %s\n", hostname);
}
freeaddrinfo(res);
return;
}
freeaddrinfo(res);
err(1, "could not connect");
}
void
pledge_me() {
#ifdef HAVE_PLEDGE
int result;
#endif
#ifdef HAVE_UNVEIL
if (enable_history) {
if (unveil(history_path, "wc") == -1)
err(1, "history unveil");
}
if (unveil(NULL, NULL) == -1)
err(1, "final unveil");
#endif
#ifdef HAVE_PLEDGE
if (enable_history)
result = pledge("stdio wpath cpath tty", NULL);
else
result = pledge("stdio tty", NULL);
if (result == -1)
err(1, "pledge");
#endif
}
#ifndef HAVE_RL_BIND_KEYSEQ
static inline int
rl_bind_keyseq(const char *keyseq, int(*function)(int, int))
{
return rl_generic_bind(ISFUNC, keyseq, (char *)function, rl_get_keymap());
}
#endif
int
main(int argc, char **argv) {
#ifdef SIGINFO
struct sigaction sa;
#endif
size_t msglen;
time_t ts_lastnetinput, t;
int ch, i, net_timeout, poll_timeout, max_pings;
char *msg, *port = NULL;
const char *errstr, *locale;
SIMPLEQ_INIT(&tasks_stdout);
SIMPLEQ_INIT(&tasks_net);
locale = setlocale(LC_CTYPE, "");
if (strstr(locale, ".UTF-8")) {
utf8_ready = 1;
if (debug >= 1)
warnx("UTF-8 support detected");
}
net_timeout = 30;
while ((ch = getopt(argc, argv, "dHt:")) != -1) {
switch (ch) {
case 'd':
debug++;
break;
case 'H':
enable_history = 0;
break;
case 't':
net_timeout = strtonum(optarg, 0, INT_MAX/1000,
&errstr);
if (errstr)
errx(1, "invalid network timeout: %s",
errstr);
break;
default:
/* error message is already printed by getopt() */
usage(NULL);
}
}
argc -= optind;
argv += optind;
if (argc != 2)
usage(NULL);
room = argv[1];
if ((hostname = strchr(argv[0], '@')) != NULL) {
nick = argv[0];
*hostname++ = '\0';
if (*hostname == '\0')
usage("invalid hostname specification");
} else {
hostname = argv[0];
nick = getlogin();
}
nicklen = strlen(nick);
if (nicklen >= NICKNAME_MAX)
usage("too long nickname");
if (hostname[0] == '[') {
char *hostend;
hostname++;
hostend = strrchr(hostname, ']');
if (hostend == NULL ||
(hostend[1] != '\0' && hostend[1] != ':'))
usage("invalid hostname specification");
if (hostend[1] == ':')
port = hostend + 2;
*hostend = '\0';
} else {
if ((port = strrchr(hostname, ':')) != NULL)
*port++ = '\0';
}
icb_connect(hostname, port);
if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
err(1, "stdin: fcntl");
if (fcntl(STDOUT_FILENO, F_SETFL, O_NONBLOCK) == -1)
err(1, "stdout: fcntl");
if (net_timeout)
poll_timeout = net_timeout * 100;
else
poll_timeout = INFTIM;
ts_lastnetinput = time(NULL);
max_pings = 3;
rl_callback_handler_install("", &proceed_user_input);
atexit(&rl_callback_handler_remove);
// disable completion, or readline will try to access file system
rl_completion_entry_function = null_completer;
rl_bind_key('\t', cycle_priv_chats_forward);
rl_bind_keyseq("\\e[Z", cycle_priv_chats_backward);
rl_bind_key(CTRL('p'), list_priv_chats_nicks_wrapper);
rl_bind_key(CTRL('t'), siginfo_cmd);
if (debug)
rl_bind_key(CTRL('x'), test_cmd);
#ifdef SIGINFO
if (sigaction(SIGINFO, NULL, &sa) == -1)
warn("sigaction(SIGINFO, NULL)");
else {
sa.sa_handler = siginfo_handler;
if (sigaction(SIGINFO, &sa, NULL) == -1)
warn("sigaction(SIGINFO, &sa)");
}
#endif
if (enable_history) {
snprintf(history_path, PATH_MAX, "%s/.oicb/logs/%s",
getenv("HOME"), hostname);
if (create_dir_for(history_path) == -1 ||
(mkdir(history_path, 0777) == -1 && errno != EEXIST)) {
warn("cannot make sure history directory \"%s\" exists",
history_path);
warnx("history saving is disabled");
enable_history = 0;
memset(history_path, 0, PATH_MAX);
}
}
pledge_me();
while (!want_exit) {
if (want_info) {
push_stdout("%s: sitting in room %s at %s",
getprogname(), room, hostname);
if (port)
push_stdout(":%s", port);
push_stdout(" as %s\n", nick);
if (debug)
push_stdout("%s: rl_line_buffer=0x%p '%s' [%zu] rl_point=%d rl_mark=%d\n",
getprogname(),
rl_line_buffer, rl_line_buffer,
strlen(rl_line_buffer),
rl_point, rl_mark);
want_info = 0;
}
proceed_output(&tasks_net, sock);
t = time(NULL);
if (net_timeout &&
ts_lastnetinput + net_timeout * (pings_sent + 1) < t) {
if ((srv_features & Ping) == Ping) {
push_icb_msg('l', "", 0);
pings_sent++;
} else {
push_icb_msg('n', "", 0);
ts_lastnetinput = t;
}
}
update_pollfds();
if (poll(pfd, npfd, poll_timeout) == -1) {
if (errno == EINTR)
continue;
err(1, "poll");
}
for (i = 0; i < MainFDCount; i++)
if ((pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL)))
errx(1, "error occured on %s", stream_names[i]);
if (state == Connecting) {
// the only cause we're here is that connect(2) succeeded
state = Connected;
push_stdout("connected\n");
continue;
}
if ((pfd[Stdin].revents & POLLIN))
rl_callback_read_char();
if ((pfd[Network].revents & POLLIN)) {
ts_lastnetinput = time(NULL);
pings_sent = 0;
while (!want_exit && (msg = get_next_icb_msg(&msglen)) != NULL)
proceed_icb_msg(msg, msglen);
} else if (net_timeout &&
ts_lastnetinput + net_timeout * max_pings < t) {
push_stdout("Server timed out, exiting\n");
want_exit = 1;
}
prepare_stdout();
proceed_output(&tasks_stdout, STDOUT_FILENO);
restore_rl();
proceed_history();
}
return 0;
}
int
test_cmd(int count, int key) {
(void)count;
(void)key;
return 0;
}