Skip to content

Commit 060acb9

Browse files
committed
cleanup formatting
1 parent 95741cf commit 060acb9

File tree

1 file changed

+44
-65
lines changed

1 file changed

+44
-65
lines changed

kirc.c

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ static char *port = "6667"; /* port */
2424
static char *nick = NULL; /* nickname */
2525

2626
static int
27-
kbhit(void)
28-
{
27+
kbhit(void) {
2928
int byteswaiting;
3029
struct termios term;
3130
fd_set fds;
32-
struct timespec ts = {0};
31+
struct timespec ts = {0};
3332
tcgetattr(0, &term);
3433

3534
struct termios term2 = term;
3635

3736
term2.c_lflag &= ~ICANON;
3837
tcsetattr(0, TCSANOW, &term2);
39-
FD_ZERO(&fds);
40-
FD_SET(0, &fds);
41-
byteswaiting = pselect(1, &fds, NULL, NULL, &ts, NULL);
38+
FD_ZERO(&fds);
39+
FD_SET(0, &fds);
40+
byteswaiting = pselect(1, &fds, NULL, NULL, &ts, NULL);
4241
tcsetattr(0, TCSANOW, &term);
4342

4443
return byteswaiting > 0;
@@ -58,8 +57,7 @@ raw(char *fmt, ...) {
5857
}
5958

6059
static void
61-
con(void)
62-
{
60+
con(void) {
6361
struct addrinfo *res, hints = {
6462
.ai_family = AF_INET,
6563
.ai_socktype = SOCK_STREAM
@@ -76,85 +74,73 @@ con(void)
7674
}
7775

7876
static void
79-
printw(const char *format, ...)
80-
{
77+
printw(const char *format, ...) {
78+
8179
int s1 = 0, s2, i, o;
8280
va_list argptr;
8381
char line[BUFF + 1];
82+
8483
va_start(argptr, format);
8584
vsnprintf(line, BUFF + 1, format, argptr);
8685
va_end(argptr);
8786
if (strlen(line) <= CMAX) printf("%s", line);
88-
else if (strlen(line) > CMAX)
89-
{
90-
for (i = 0; i < CMAX; i++)
91-
{
87+
else if (strlen(line) > CMAX) {
88+
for (i = 0; i < CMAX; i++) {
9289
if (line[i] == ' ') s1 = i;
9390
if (i == CMAX - 1) printf("%-*.*s\n", s1, s1, line);
9491
}
9592
s2 = o = s1;
96-
for (i = s1; line[i] != '\0'; i++)
97-
{
93+
for (i = s1; line[i] != '\0'; i++) {
9894
if (line[i] == ' ') s2 = i;
99-
if ((i - o) == (CMAX - GUTL))
100-
{
95+
if ((i - o) == (CMAX - GUTL)) {
10196
printf("%*s %-*.*s\n", GUTL, " ", s2 - o, s2 - o, &line[o + 1]);
10297
o = i = s2;
10398
}
104-
else if (line[i + 1] == '\0')
105-
{
99+
else if (line[i + 1] == '\0') {
106100
printf("%*s %-*.*s", GUTL, " ", i - o, i - o, &line[o + 1]);
107101
}
108102
}
109103
}
110-
111104
}
112105

113106
static void
114-
pars(int sl, char *buf)
115-
{
107+
pars(int sl, char *buf) {
108+
116109
char buf_c[BUFF + 1], ltr[200], cha[200], nic[200], hos[200], \
117110
usr[200], cmd[200], msg[200], pre[200];
118111
int o = -1;
119112

120-
for (int i = 0; i < sl; i++)
121-
{
113+
for (int i = 0; i < sl; i++) {
122114
o++;
123115
buf_c[o] = buf[i];
124116

125-
if ((i > 0 && buf[i] == '\n' && buf[i - 1] == '\r') || o == BUFF)
126-
{
117+
if ((i > 0 && buf[i] == '\n' && buf[i - 1] == '\r') || o == BUFF) {
127118
buf_c[o + 1] = '\0';
128119
o = -1;
129120

130121
if (verb) printf(">> %s", buf_c);
131122

132-
if (!strncmp(buf_c, "PING", 4))
133-
{
123+
if (!strncmp(buf_c, "PING", 4)) {
134124
buf_c[1] = 'O';
135125
raw(buf_c);
136126
}
137127

138-
else if (buf_c[0] == ':')
139-
{
128+
else if (buf_c[0] == ':') {
140129
sscanf(buf_c, ":%[^ ] %[^:]:%[^\r]", pre, cmd, msg);
141130
sscanf(pre, "%[^!]!%[^@]@%s", nic, usr, hos);
142131
sscanf(cmd, "%[^#& ]%s", ltr, cha);
143132

144133
if (!strncmp(ltr, "001", 3)) raw("JOIN #%s\r\n", chan);
145134

146-
if (!strncmp(ltr, "QUIT", 4))
147-
{
135+
if (!strncmp(ltr, "QUIT", 4)) {
148136
printw("%*.*s \x1b[34;1m%s\x1b[0m left %s\n", \
149137
GUTL, GUTL, "<--", nic, cha);
150138
}
151-
else if (!strncmp(ltr, "JOIN", 4))
152-
{
139+
else if (!strncmp(ltr, "JOIN", 4)) {
153140
printw("%*.*s \x1b[32;1m%s\x1b[0m joined %s\n", \
154141
GUTL, GUTL, "-->", nic, cha);
155142
}
156-
else
157-
{
143+
else {
158144
printw("\x1b[1m%*.*s\x1b[0m %s\n", \
159145
GUTL, GUTL, nic, msg);
160146
}
@@ -164,14 +150,12 @@ pars(int sl, char *buf)
164150
}
165151

166152
int
167-
main(int argc, char **argv)
168-
{
153+
main(int argc, char **argv) {
154+
169155
int fd[2], cval;
170156

171-
while ((cval = getopt(argc, argv, "s:p:n:k:c:vV")) != -1)
172-
{
173-
switch (cval)
174-
{
157+
while ((cval = getopt(argc, argv, "s:p:n:k:c:vV")) != -1) {
158+
switch (cval) {
175159
case 'v' : printf("kirc 0.0.1\n"); break;
176160
case 'V' : verb = 1; break;
177161
case 's' : host = optarg; break;
@@ -182,64 +166,59 @@ main(int argc, char **argv)
182166
}
183167
}
184168

185-
if (pipe(fd) < 0)
186-
{
169+
if (nick == NULL) {
170+
fprintf(stderr, "nick not specified");
171+
return 1;
172+
}
173+
174+
if (pipe(fd) < 0) {
187175
fprintf(stderr, "pipe() failed");
188176
return 2;
189177
}
190178

191179
pid_t pid = fork();
192180

193-
if (pid == 0)
194-
{
181+
if (pid == 0) {
195182
int sl;
196183
char u[BUFF];
197184

198185
con();
199186

200-
while ((sl = read(conn, sbuf, BUFF)))
201-
{
187+
while ((sl = read(conn, sbuf, BUFF))) {
202188
pars(sl, sbuf);
203-
if ((read(fd[0], u, CMAX) > 0) && !strstr(u, "WAIT_SIG"))
204-
{
189+
if ((read(fd[0], u, CMAX) > 0) && !strstr(u, "WAIT_SIG")) {
205190
raw("%s\r\n", u);
206191
}
207192
}
208193
printf("(press <ENTER> to quit)\n");
209194
}
210-
else
211-
{
195+
else {
212196
char usrin[CMAX];
213-
int l = CMAX - strlen(chan);
214197
char cmd = '\n';
215198

216-
while (waitpid(pid, NULL, WNOHANG) == 0)
217-
{
218-
while (!kbhit() && waitpid(pid, NULL, WNOHANG) == 0)
219-
{
199+
while (waitpid(pid, NULL, WNOHANG) == 0) {
200+
while (!kbhit() && waitpid(pid, NULL, WNOHANG) == 0) {
220201
write(fd[1], "WAIT_SIG", CMAX);
221202
}
222203

223204
fgets(usrin, CMAX, stdin);
224205

225-
if (usrin[0] == ':' && usrin[1])
226-
{
206+
if (usrin[0] == ':' && usrin[1]) {
227207
char *cmd_val = &usrin[2];
228208
cmd = usrin[1];
229209

230-
switch (cmd)
231-
{
210+
switch (cmd) {
232211
case 'q':
233212
write(fd[1], "quit", sizeof("quit"));
234213
break;
235214
case 'm':
236215
while (isspace(*cmd_val)) cmd_val++;
237-
dprintf(fd[1], "privmsg #%s :%-*s", chan, l - 11, cmd_val);
216+
dprintf(fd[1], "privmsg #%s :%-*s", \
217+
chan, CMAX - strlen(chan) - 11, cmd_val);
238218
break;
239219
}
240220
}
241-
else
242-
{
221+
else {
243222
write(fd[1], usrin, CMAX);
244223
}
245224
fflush(stdout);

0 commit comments

Comments
 (0)