Skip to content

Commit 68166e1

Browse files
authored
released at 0.2.4
1 parent efa1745 commit 68166e1

File tree

2 files changed

+4
-40
lines changed

2 files changed

+4
-40
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Michael Czigler
3+
Copyright (c) 2021 Michael Czigler
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

kirc.c

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#include <termios.h>
1414
#include <sys/ioctl.h>
1515

16-
#define VERSION "0.2.3" /* version */
17-
#define AUTHORS "Michael Czigler" /* authors */
16+
#define VERSION "0.2.4" /* version */
1817
#define MSG_MAX 512 /* max message length */
1918
#define CHA_MAX 200 /* max channel length */
2019
#define NIC_MAX 26 /* max nickname length */
@@ -82,19 +81,16 @@ static int enableRawMode(int fd) {
8281
}
8382
if (tcgetattr(fd,&orig) == -1)
8483
goto fatal;
85-
8684
struct termios raw = orig;
8785
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
8886
raw.c_oflag &= ~(OPOST);
8987
raw.c_cflag |= (CS8);
9088
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
9189
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0;
92-
9390
if (tcsetattr(fd,TCSAFLUSH,&raw) < 0)
9491
goto fatal;
9592
rawmode = 1;
9693
return 0;
97-
9894
fatal:
9995
errno = ENOTTY;
10096
return -1;
@@ -123,7 +119,6 @@ static int getCursorPosition(int ifd, int ofd) {
123119

124120
static int getColumns(int ifd, int ofd) {
125121
struct winsize ws;
126-
127122
if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
128123
int start = getCursorPosition(ifd, ofd);
129124
if (start == -1)
@@ -170,9 +165,7 @@ static void refreshLine(struct State * l) {
170165
size_t len = l->len;
171166
size_t pos = l->pos;
172167
struct abuf ab;
173-
174168
l->cols = getColumns(STDIN_FILENO, STDOUT_FILENO);
175-
176169
while (plen + pos >= l->cols) {
177170
buf++;
178171
len--;
@@ -271,12 +264,10 @@ static void editBackspace(struct State * l) {
271264

272265
static void editDeletePrevWord(struct State * l) {
273266
size_t old_pos = l->pos;
274-
275267
while (l->pos > 0 && l->buf[l->pos - 1] == ' ')
276268
l->pos--;
277269
while (l->pos > 0 && l->buf[l->pos - 1] != ' ')
278270
l->pos--;
279-
280271
size_t diff = old_pos - l->pos;
281272
memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1);
282273
l->len -= diff;
@@ -309,10 +300,8 @@ static void editSwapCharWithPrev(struct State * l) {
309300
static int edit(struct State * l) {
310301
char c, seq[3];
311302
ssize_t nread = read(STDIN_FILENO, &c, 1);
312-
313303
if (nread <= 0)
314304
return 1;
315-
316305
switch(c) {
317306
case 13: return 1; /* enter */
318307
case 3: errno = EAGAIN; return -1; /* ctrl-c */
@@ -385,7 +374,6 @@ static char * ctime_now(char buf[26]) {
385374
static void logAppend(char * str, char * path) {
386375
FILE * out;
387376
char buf[26];
388-
389377
if ((out = fopen(path, "a")) == NULL) {
390378
perror("fopen");
391379
exit(1);
@@ -398,16 +386,13 @@ static void logAppend(char * str, char * path) {
398386
static void raw(char * fmt, ...) {
399387
va_list ap;
400388
char *cmd_str = malloc(MSG_MAX);
401-
402389
if (!cmd_str) {
403390
perror("malloc");
404391
exit(1);
405392
}
406-
407393
va_start(ap, fmt);
408394
vsnprintf(cmd_str, MSG_MAX, fmt, ap);
409395
va_end(ap);
410-
411396
if (verb)
412397
printf("<< %s", cmd_str);
413398
if (olog)
@@ -416,7 +401,6 @@ static void raw(char * fmt, ...) {
416401
perror("write");
417402
exit(1);
418403
}
419-
420404
free(cmd_str);
421405
}
422406

@@ -426,12 +410,10 @@ static int initConnection(void) {
426410
.ai_family = AF_UNSPEC,
427411
.ai_socktype = SOCK_STREAM
428412
};
429-
430413
if ((gai_status = getaddrinfo(host, port, &hints, &res)) != 0) {
431414
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_status));
432415
return -1;
433416
}
434-
435417
struct addrinfo *p;
436418
for (p = res; p != NULL; p = p->ai_next) {
437419
if ((conn = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
@@ -445,14 +427,11 @@ static int initConnection(void) {
445427
}
446428
break;
447429
}
448-
449430
freeaddrinfo(res);
450-
451431
if (p == NULL) {
452432
fputs("Failed to connect\n", stderr);
453433
return -1;
454434
}
455-
456435
int flags = fcntl(conn, F_GETFL, 0);
457436
flags |= O_NONBLOCK;
458437
fcntl(conn, F_SETFL, flags);
@@ -554,14 +533,13 @@ static void rawParser(char * string) {
554533
raw("%s\r\n", string);
555534
return;
556535
}
557-
if (string[0] != ':')
536+
if (string[0] != ':' || (strnlen(string, MSG_MAX) < 4))
558537
return;
559538
printf("\r\x1b[0K");
560539
if (verb)
561540
printf(">> %s", string);
562541
if (olog)
563542
logAppend(string, olog);
564-
565543
char * tok;
566544
struct Param p;
567545
p.prefix = strtok(string, " ") + 1;
@@ -574,7 +552,6 @@ static void rawParser(char * string) {
574552
p.maxcols = getColumns(STDIN_FILENO, STDOUT_FILENO);
575553
p.nicklen = (p.maxcols / 3 > NIC_MAX ? NIC_MAX : p.maxcols / 3);
576554
p.offset = 0;
577-
578555
if (!strncmp(p.command, "001", 3) && chan != NULL) {
579556
for (tok = strtok(chan, ",|"); tok != NULL; tok = strtok(NULL, ",|")) {
580557
strcpy(cdef, tok);
@@ -617,10 +594,8 @@ static int handleServerMessage(void) {
617594
puts("\r\x1b[E");
618595
return -1;
619596
}
620-
621597
size_t i, old_message_end = message_end;
622598
message_end += nread;
623-
624599
for (i = old_message_end; i < message_end; ++i) {
625600
if (i != 0 && message_buffer[i - 1] == '\r' && message_buffer[i] == '\n') {
626601
char saved_char = message_buffer[i + 1];
@@ -640,10 +615,8 @@ static int handleServerMessage(void) {
640615
static void handleUserInput(struct State * l) {
641616
if (l->buf == NULL)
642617
return;
643-
644618
char * tok;
645619
size_t msg_len = strnlen(l->buf, MSG_MAX);
646-
647620
if (msg_len > 0 && l->buf[msg_len - 1] == '\n')
648621
l->buf[msg_len - 1] = '\0';
649622
printf("\r\x1b[0K");
@@ -679,13 +652,12 @@ static void usage(void) {
679652
}
680653

681654
static void version(void) {
682-
fputs("kirc-" VERSION " © 2020 " AUTHORS "\n", stdout);
655+
fputs("kirc-" VERSION "Copyright © 2021 Michael Czigler, MIT License\n", stdout);
683656
exit(0);
684657
}
685658

686659
int main(int argc, char **argv) {
687660
int cval;
688-
689661
while ((cval = getopt(argc, argv, "s:p:o:n:k:c:u:r:x:a:evV")) != -1) {
690662
switch (cval) {
691663
case 'v' : version(); break;
@@ -704,15 +676,12 @@ int main(int argc, char **argv) {
704676
case '?' : usage(); break;
705677
}
706678
}
707-
708679
if (!nick) {
709680
fputs("Nick not specified\n", stderr);
710681
usage();
711682
}
712-
713683
if (initConnection() != 0)
714684
return 1;
715-
716685
if (auth || sasl)
717686
raw("CAP REQ :sasl\r\n");
718687
raw("NICK %s\r\n", nick);
@@ -725,23 +694,18 @@ int main(int argc, char **argv) {
725694
raw("PASS %s\r\n", pass);
726695
if (inic)
727696
raw("%s\r\n", inic);
728-
729697
struct pollfd fds[2];
730698
fds[0].fd = STDIN_FILENO;
731699
fds[1].fd = conn;
732700
fds[0].events = POLLIN;
733701
fds[1].events = POLLIN;
734-
735702
char usrin[MSG_MAX];
736-
737703
struct State l;
738704
l.buf = usrin;
739705
l.buflen = MSG_MAX;
740706
l.prompt = cdef;
741707
stateReset(&l);
742-
743708
int rc, editReturnFlag = 0;
744-
745709
if (enableRawMode(STDIN_FILENO) == -1)
746710
return 1;
747711
for (;;) {

0 commit comments

Comments
 (0)