Skip to content

Commit 5f8bab4

Browse files
author
Cristian Canedo
authored
Allow the option of parsing multiple post-connection commands. (#125)
* Allow the option of parsing multiple post-connection commands. * Initial change for multiple commands via stdin * Use MSG_MAX as the max length of additional commands * Include + 1 to MSG_MAX to account for newline characters * Rework loop to process commands to avoid seg fault, use strnlen * Remove swap file * Restore previous formatting, restore man description for -x for now * Enable additional commands via both stdin and option argument * Move misplaced malloc check to where it belongs * Simplify logic to read commands from stdin * Retain functionality to accept server command as option argument * Maintain same functionality as before when sending inic
1 parent 7ebf6b9 commit 5f8bab4

File tree

2 files changed

+72
-43
lines changed

2 files changed

+72
-43
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ kirc
22
*.a
33
*.o
44
*.txt
5+
tags
6+
*.out
7+
*.log
8+
*.diff
9+
.*.swp

kirc.c

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@
2323
#define CHA_MAX 200
2424
#define NIC_MAX 26
2525
#define HIS_MAX 100
26+
#define CBUF_SIZ 1024
2627

27-
static char cdef[MSG_MAX] = "?"; /* default PRIVMSG channel */
28-
static int conn; /* connection socket */
29-
static int verb = 0; /* verbose output */
30-
static int sasl = 0; /* SASL method */
31-
static int isu8 = 0; /* UTF-8 flag */
32-
static char *host = "irc.libera.chat"; /* host address */
33-
static char *port = "6667"; /* port */
34-
static char *chan = NULL; /* channel(s) */
35-
static char *nick = NULL; /* nickname */
36-
static char *pass = NULL; /* password */
37-
static char *user = NULL; /* user name */
38-
static char *auth = NULL; /* PLAIN SASL token */
39-
static char *real = NULL; /* real name */
40-
static char *olog = NULL; /* chat log path*/
41-
static char *inic = NULL; /* additional server command */
28+
static char cdef[MSG_MAX] = "?"; /* default PRIVMSG channel */
29+
static int conn; /* connection socket */
30+
static int verb = 0; /* verbose output */
31+
static int sasl = 0; /* SASL method */
32+
static int isu8 = 0; /* UTF-8 flag */
33+
static char *host = "irc.libera.chat"; /* host address */
34+
static char *port = "6667"; /* port */
35+
static char *chan = NULL; /* channel(s) */
36+
static char *nick = NULL; /* nickname */
37+
static char *pass = NULL; /* password */
38+
static char *user = NULL; /* user name */
39+
static char *auth = NULL; /* PLAIN SASL token */
40+
static char *real = NULL; /* real name */
41+
static char *olog = NULL; /* chat log path*/
42+
static char *inic = NULL; /* additional server command */
43+
static int cmds = 0; /* indicates additional server commands */
44+
static char cbuf[CBUF_SIZ]; /* additional stdin server commands */
4245

4346
struct Param {
4447
char *prefix;
@@ -53,6 +56,7 @@ struct Param {
5356
int nicklen;
5457
};
5558

59+
static int ttyinfd = STDIN_FILENO;
5660
static struct termios orig;
5761
static int rawmode = 0;
5862
static int atexit_registered = 0;
@@ -92,14 +96,14 @@ static void freeHistory(void) {
9296
}
9397

9498
static void disableRawMode(void) {
95-
if (rawmode && tcsetattr(STDIN_FILENO,TCSAFLUSH,&orig) != -1) {
99+
if (rawmode && tcsetattr(ttyinfd,TCSAFLUSH,&orig) != -1) {
96100
rawmode = 0;
97101
}
98102
freeHistory();
99103
}
100104

101105
static int enableRawMode(int fd) {
102-
if (!isatty(STDIN_FILENO)) {
106+
if (!isatty(ttyinfd)) {
103107
goto fatal;
104108
}
105109
if (!atexit_registered) {
@@ -297,7 +301,7 @@ static void refreshLine(struct State *l) {
297301
size_t posu8 = l->posu8;
298302
size_t ch = plenu8, txtlenb = 0;
299303
struct abuf ab;
300-
l->cols = getColumns(STDIN_FILENO, STDOUT_FILENO);
304+
l->cols = getColumns(ttyinfd, STDOUT_FILENO);
301305
while ((plenu8 + posu8) >= l->cols) {
302306
buf += u8Next(buf, 0);
303307
posu8--;
@@ -510,12 +514,12 @@ static void editEnter(void) {
510514
}
511515

512516
static void editEscSequence(struct State *l, char seq[3]) {
513-
if (read(STDIN_FILENO, seq, 1) == -1) return;
514-
if (read(STDIN_FILENO, seq + 1, 1) == -1) return;
517+
if (read(ttyinfd, seq, 1) == -1) return;
518+
if (read(ttyinfd, seq + 1, 1) == -1) return;
515519
if (seq[0] == '[') { /* ESC [ sequences. */
516520
if ((seq[1] >= '0') && (seq[1] <= '9')) {
517521
/* Extended escape, read additional byte. */
518-
if (read(STDIN_FILENO, seq + 2, 1) == -1) {
522+
if (read(ttyinfd, seq + 2, 1) == -1) {
519523
return;
520524
}
521525
if ((seq[2] == '~') && (seq[1] == 3)) {
@@ -542,7 +546,7 @@ static void editEscSequence(struct State *l, char seq[3]) {
542546
static int edit(struct State *l) {
543547
char c, seq[3];
544548
int ret = 0;
545-
ssize_t nread = read(STDIN_FILENO, &c, 1);
549+
ssize_t nread = read(ttyinfd, &c, 1);
546550
if (nread <= 0) {
547551
ret = 1;
548552
} else {
@@ -577,7 +581,7 @@ static int edit(struct State *l) {
577581
aux[0] = c;
578582
int size = u8CharSize(c);
579583
for (int i = 1; i < size; i++) {
580-
nread = read(STDIN_FILENO, aux + i, 1);
584+
nread = read(ttyinfd, aux + i, 1);
581585
if ((aux[i] & 0xC0) != 0x80) {
582586
break;
583587
}
@@ -803,7 +807,7 @@ static void rawParser(char *string) {
803807
p.command = strtok(p.suffix, "#& ");
804808
p.channel = strtok(NULL, " \r");
805809
p.params = strtok(NULL, ":\r");
806-
p.maxcols = getColumns(STDIN_FILENO, STDOUT_FILENO);
810+
p.maxcols = getColumns(ttyinfd, STDOUT_FILENO);
807811
p.nicklen = (p.maxcols / 3 > NIC_MAX ? NIC_MAX : p.maxcols / 3);
808812
p.offset = 0;
809813
if (!strncmp(p.command, "001", 3) && chan != NULL) {
@@ -922,26 +926,41 @@ static void version(void) {
922926
exit(0);
923927
}
924928

929+
static void opentty()
930+
{
931+
if ((ttyinfd = open("/dev/tty", 0)) == -1) {
932+
perror("failed to open /dev/tty");
933+
exit(1);
934+
}
935+
}
936+
925937
int main(int argc, char **argv) {
938+
opentty();
926939
int cval;
927-
while ((cval = getopt(argc, argv, "s:p:o:n:k:c:u:r:x:a:evV")) != -1) {
940+
while ((cval = getopt(argc, argv, "s:p:o:n:k:c:u:r:a:exvV")) != -1) {
928941
switch (cval) {
929-
case 'v' : version(); break;
930-
case 'V' : ++verb; break;
931-
case 'e' : ++sasl; break;
932-
case 's' : host = optarg; break;
933-
case 'p' : port = optarg; break;
934-
case 'r' : real = optarg; break;
935-
case 'u' : user = optarg; break;
936-
case 'a' : auth = optarg; break;
937-
case 'o' : olog = optarg; break;
938-
case 'n' : nick = optarg; break;
939-
case 'k' : pass = optarg; break;
940-
case 'c' : chan = optarg; break;
941-
case 'x' : inic = optarg; break;
942+
case 'v' : version(); break;
943+
case 'V' : ++verb; break;
944+
case 'e' : ++sasl; break;
945+
case 's' : host = optarg; break;
946+
case 'p' : port = optarg; break;
947+
case 'r' : real = optarg; break;
948+
case 'u' : user = optarg; break;
949+
case 'a' : auth = optarg; break;
950+
case 'o' : olog = optarg; break;
951+
case 'n' : nick = optarg; break;
952+
case 'k' : pass = optarg; break;
953+
case 'c' : chan = optarg; break;
954+
case 'x' : cmds = 1; inic = argv[optind]; break;
942955
case '?' : usage(); break;
943956
}
944957
}
958+
if (cmds > 0) {
959+
int flag = 0;
960+
for (int i = 0; i < CBUF_SIZ && flag > -1; i++) {
961+
flag = read(STDIN_FILENO, &cbuf[i], 1);
962+
}
963+
}
945964
if (!nick) {
946965
fputs("Nick not specified\n", stderr);
947966
usage();
@@ -961,11 +980,16 @@ int main(int argc, char **argv) {
961980
if (pass) {
962981
raw("PASS %s\r\n", pass);
963982
}
964-
if (inic) {
965-
raw("%s\r\n", inic);
983+
if (cmds > 0) {
984+
for (char *tok = strtok(cbuf, "\n"); tok; tok = strtok(NULL, "\n")) {
985+
raw("%s\r\n", tok);
986+
}
987+
if (inic) {
988+
raw("%s\r\n", inic);
989+
}
966990
}
967991
struct pollfd fds[2];
968-
fds[0].fd = STDIN_FILENO;
992+
fds[0].fd = ttyinfd;
969993
fds[1].fd = conn;
970994
fds[0].events = POLLIN;
971995
fds[1].events = POLLIN;
@@ -976,10 +1000,10 @@ int main(int argc, char **argv) {
9761000
l.prompt = cdef;
9771001
stateReset(&l);
9781002
int rc, editReturnFlag = 0;
979-
if (enableRawMode(STDIN_FILENO) == -1) {
1003+
if (enableRawMode(ttyinfd) == -1) {
9801004
return 1;
9811005
}
982-
if (setIsu8_C(STDIN_FILENO, STDOUT_FILENO) == -1) {
1006+
if (setIsu8_C(ttyinfd, STDOUT_FILENO) == -1) {
9831007
return 1;
9841008
}
9851009
for (;;) {

0 commit comments

Comments
 (0)