Skip to content

Commit 44c8c6d

Browse files
committed
eliminated nested while loops
1 parent 7cadfac commit 44c8c6d

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

kirc.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,25 @@ printw(const char *format, ...) {
109109

110110
va_list argptr;
111111
char *tok, line[IRC_MSG_MAX + 1];
112-
int len, i, s;
112+
int i, wordwidth, spaceleft, spacewidth = 1;
113113

114114
va_start(argptr, format);
115115
vsnprintf(line, IRC_MSG_MAX + 1, format, argptr);
116116
va_end(argptr);
117+
117118
for (i = 0; isspace(line[i]); i++) printf("%c", line[i]);
118119

119-
s = cmax + gutl - (i - 1);
120+
spaceleft = cmax + gutl - (i - 1);
120121

121122
for(tok = strtok(&line[i], " "); tok != NULL; tok = strtok(NULL, " ")) {
122-
len = strlen(tok);
123-
if ((len + 1) > s) {
123+
wordwidth = strlen(tok);
124+
if ((wordwidth + spacewidth) > spaceleft) {
124125
printf("\n%*.s%s", gutl + 2, "", tok);
125-
s = cmax - (gutl + 2 + len);
126+
spaceleft = cmax - (gutl + 2 + wordwidth);
126127
}
127128
else {
128129
printf(" %s", tok);
129-
s = s - (len + 1);
130+
spaceleft = spaceleft - (wordwidth + spacewidth);
130131
}
131132
}
132133
}
@@ -223,34 +224,34 @@ main(int argc, char **argv) {
223224
if (u[0] != ':') raw(s, "%-*.*s\r\n", i, i, u);
224225
}
225226
}
226-
fprintf(stderr, "%*s <<irc server connection closed>>", gutl, "");
227227
}
228228
else {
229229
char usrin[IRC_MSG_MAX], v1[IRC_MSG_MAX-20], v2[20], c1;
230230

231231
while (waitpid(pid, NULL, WNOHANG) == 0) {
232-
while (!kbhit() && waitpid(pid, NULL, WNOHANG) == 0) {
233-
dprintf(fd[1], ":\n");
234-
}
235-
236-
strcpy(usrin, input_handler(IRC_MSG_MAX));
237-
238-
if (sscanf(usrin, ":%[M] %s %[^\n]\n", &c1, v2, v1) == 3 ||
239-
sscanf(usrin, ":%[Qnjpm] %[^\n]\n", &c1, v1) == 2 ||
240-
sscanf(usrin, ":%[q]\n", &c1) == 1) {
241-
switch (c1) {
242-
case 'q': dprintf(fd[1], "quit\n"); break;
243-
case 'Q': dprintf(fd[1], "quit %s\n", v1); break;
244-
case 'j': dprintf(fd[1], "join %s\n", v1); break;
245-
case 'p': dprintf(fd[1], "part %s\n", v1); break;
246-
case 'm': dprintf(fd[1], "privmsg #%s :%s\n", chan, v1); break;
247-
case 'n': dprintf(fd[1], "privmsg nickserv :%s\n", v1); break;
248-
case 'M': dprintf(fd[1], "privmsg %s :%s\n", v2, v1); break;
249-
case '?': break;
232+
if (!kbhit()) dprintf(fd[1], ":\n");
233+
else {
234+
strcpy(usrin, input_handler(IRC_MSG_MAX));
235+
236+
if (sscanf(usrin, ":%[M] %s %[^\n]\n", &c1, v2, v1) == 3 ||
237+
sscanf(usrin, ":%[Qnjpm] %[^\n]\n", &c1, v1) == 2 ||
238+
sscanf(usrin, ":%[q]\n", &c1) == 1) {
239+
switch (c1) {
240+
case 'q': dprintf(fd[1], "quit\n"); break;
241+
case 'Q': dprintf(fd[1], "quit %s\n", v1); break;
242+
case 'j': dprintf(fd[1], "join %s\n", v1); break;
243+
case 'p': dprintf(fd[1], "part %s\n", v1); break;
244+
case 'm': dprintf(fd[1], "privmsg #%s :%s\n", chan, v1); break;
245+
case 'n': dprintf(fd[1], "privmsg nickserv :%s\n", v1); break;
246+
case 'M': dprintf(fd[1], "privmsg %s :%s\n", v2, v1); break;
247+
case '?': break;
248+
}
250249
}
250+
else dprintf(fd[1], "%s", usrin);
251251
}
252-
else dprintf(fd[1], "%s", usrin);
253252
}
253+
254+
fprintf(stderr, "%*s <<irc server connection closed>>\n", gutl, "");
254255
}
255256
return 0;
256257
}

0 commit comments

Comments
 (0)