diff options
author | mcpcpc <michaelczigler@icloud.com> | 2020-08-07 23:33:03 -0400 |
---|---|---|
committer | mcpcpc <michaelczigler@icloud.com> | 2020-08-07 23:33:03 -0400 |
commit | 060acb934ba805652324c14320746e3296a41232 (patch) | |
tree | 57d7019e896f86e893a8956fdf25d49a30f83e18 /kirc.c | |
parent | remove ':' in getopts switch (diff) | |
download | kirc-060acb934ba805652324c14320746e3296a41232.tar.gz kirc-060acb934ba805652324c14320746e3296a41232.tar.bz2 kirc-060acb934ba805652324c14320746e3296a41232.tar.xz kirc-060acb934ba805652324c14320746e3296a41232.tar.zst kirc-060acb934ba805652324c14320746e3296a41232.zip |
cleanup formatting
Diffstat (limited to 'kirc.c')
-rw-r--r-- | kirc.c | 109 |
1 files changed, 44 insertions, 65 deletions
@@ -24,21 +24,20 @@ static char *port = "6667"; /* port */ static char *nick = NULL; /* nickname */ static int -kbhit(void) -{ +kbhit(void) { int byteswaiting; struct termios term; fd_set fds; - struct timespec ts = {0}; + struct timespec ts = {0}; tcgetattr(0, &term); struct termios term2 = term; term2.c_lflag &= ~ICANON; tcsetattr(0, TCSANOW, &term2); - FD_ZERO(&fds); - FD_SET(0, &fds); - byteswaiting = pselect(1, &fds, NULL, NULL, &ts, NULL); + FD_ZERO(&fds); + FD_SET(0, &fds); + byteswaiting = pselect(1, &fds, NULL, NULL, &ts, NULL); tcsetattr(0, TCSANOW, &term); return byteswaiting > 0; @@ -58,8 +57,7 @@ raw(char *fmt, ...) { } static void -con(void) -{ +con(void) { struct addrinfo *res, hints = { .ai_family = AF_INET, .ai_socktype = SOCK_STREAM @@ -76,85 +74,73 @@ con(void) } static void -printw(const char *format, ...) -{ +printw(const char *format, ...) { + int s1 = 0, s2, i, o; va_list argptr; char line[BUFF + 1]; + va_start(argptr, format); vsnprintf(line, BUFF + 1, format, argptr); va_end(argptr); if (strlen(line) <= CMAX) printf("%s", line); - else if (strlen(line) > CMAX) - { - for (i = 0; i < CMAX; i++) - { + else if (strlen(line) > CMAX) { + for (i = 0; i < CMAX; i++) { if (line[i] == ' ') s1 = i; if (i == CMAX - 1) printf("%-*.*s\n", s1, s1, line); } s2 = o = s1; - for (i = s1; line[i] != '\0'; i++) - { + for (i = s1; line[i] != '\0'; i++) { if (line[i] == ' ') s2 = i; - if ((i - o) == (CMAX - GUTL)) - { + if ((i - o) == (CMAX - GUTL)) { printf("%*s %-*.*s\n", GUTL, " ", s2 - o, s2 - o, &line[o + 1]); o = i = s2; } - else if (line[i + 1] == '\0') - { + else if (line[i + 1] == '\0') { printf("%*s %-*.*s", GUTL, " ", i - o, i - o, &line[o + 1]); } } } - } static void -pars(int sl, char *buf) -{ +pars(int sl, char *buf) { + char buf_c[BUFF + 1], ltr[200], cha[200], nic[200], hos[200], \ usr[200], cmd[200], msg[200], pre[200]; int o = -1; - for (int i = 0; i < sl; i++) - { + for (int i = 0; i < sl; i++) { o++; buf_c[o] = buf[i]; - if ((i > 0 && buf[i] == '\n' && buf[i - 1] == '\r') || o == BUFF) - { + if ((i > 0 && buf[i] == '\n' && buf[i - 1] == '\r') || o == BUFF) { buf_c[o + 1] = '\0'; o = -1; if (verb) printf(">> %s", buf_c); - if (!strncmp(buf_c, "PING", 4)) - { + if (!strncmp(buf_c, "PING", 4)) { buf_c[1] = 'O'; raw(buf_c); } - else if (buf_c[0] == ':') - { + else if (buf_c[0] == ':') { sscanf(buf_c, ":%[^ ] %[^:]:%[^\r]", pre, cmd, msg); sscanf(pre, "%[^!]!%[^@]@%s", nic, usr, hos); sscanf(cmd, "%[^#& ]%s", ltr, cha); if (!strncmp(ltr, "001", 3)) raw("JOIN #%s\r\n", chan); - if (!strncmp(ltr, "QUIT", 4)) - { + if (!strncmp(ltr, "QUIT", 4)) { printw("%*.*s \x1b[34;1m%s\x1b[0m left %s\n", \ GUTL, GUTL, "<--", nic, cha); } - else if (!strncmp(ltr, "JOIN", 4)) - { + else if (!strncmp(ltr, "JOIN", 4)) { printw("%*.*s \x1b[32;1m%s\x1b[0m joined %s\n", \ GUTL, GUTL, "-->", nic, cha); } - else - { + else { printw("\x1b[1m%*.*s\x1b[0m %s\n", \ GUTL, GUTL, nic, msg); } @@ -164,14 +150,12 @@ pars(int sl, char *buf) } int -main(int argc, char **argv) -{ +main(int argc, char **argv) { + int fd[2], cval; - while ((cval = getopt(argc, argv, "s:p:n:k:c:vV")) != -1) - { - switch (cval) - { + while ((cval = getopt(argc, argv, "s:p:n:k:c:vV")) != -1) { + switch (cval) { case 'v' : printf("kirc 0.0.1\n"); break; case 'V' : verb = 1; break; case 's' : host = optarg; break; @@ -182,64 +166,59 @@ main(int argc, char **argv) } } - if (pipe(fd) < 0) - { + if (nick == NULL) { + fprintf(stderr, "nick not specified"); + return 1; + } + + if (pipe(fd) < 0) { fprintf(stderr, "pipe() failed"); return 2; } pid_t pid = fork(); - if (pid == 0) - { + if (pid == 0) { int sl; char u[BUFF]; con(); - while ((sl = read(conn, sbuf, BUFF))) - { + while ((sl = read(conn, sbuf, BUFF))) { pars(sl, sbuf); - if ((read(fd[0], u, CMAX) > 0) && !strstr(u, "WAIT_SIG")) - { + if ((read(fd[0], u, CMAX) > 0) && !strstr(u, "WAIT_SIG")) { raw("%s\r\n", u); } } printf("(press <ENTER> to quit)\n"); } - else - { + else { char usrin[CMAX]; - int l = CMAX - strlen(chan); char cmd = '\n'; - while (waitpid(pid, NULL, WNOHANG) == 0) - { - while (!kbhit() && waitpid(pid, NULL, WNOHANG) == 0) - { + while (waitpid(pid, NULL, WNOHANG) == 0) { + while (!kbhit() && waitpid(pid, NULL, WNOHANG) == 0) { write(fd[1], "WAIT_SIG", CMAX); } fgets(usrin, CMAX, stdin); - if (usrin[0] == ':' && usrin[1]) - { + if (usrin[0] == ':' && usrin[1]) { char *cmd_val = &usrin[2]; cmd = usrin[1]; - switch (cmd) - { + switch (cmd) { case 'q': write(fd[1], "quit", sizeof("quit")); break; case 'm': while (isspace(*cmd_val)) cmd_val++; - dprintf(fd[1], "privmsg #%s :%-*s", chan, l - 11, cmd_val); + dprintf(fd[1], "privmsg #%s :%-*s", \ + chan, CMAX - strlen(chan) - 11, cmd_val); break; } } - else - { + else { write(fd[1], usrin, CMAX); } fflush(stdout); |