@@ -24,21 +24,20 @@ static char *port = "6667"; /* port */
24
24
static char * nick = NULL ; /* nickname */
25
25
26
26
static int
27
- kbhit (void )
28
- {
27
+ kbhit (void ) {
29
28
int byteswaiting ;
30
29
struct termios term ;
31
30
fd_set fds ;
32
- struct timespec ts = {0 };
31
+ struct timespec ts = {0 };
33
32
tcgetattr (0 , & term );
34
33
35
34
struct termios term2 = term ;
36
35
37
36
term2 .c_lflag &= ~ICANON ;
38
37
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 );
42
41
tcsetattr (0 , TCSANOW , & term );
43
42
44
43
return byteswaiting > 0 ;
@@ -58,8 +57,7 @@ raw(char *fmt, ...) {
58
57
}
59
58
60
59
static void
61
- con (void )
62
- {
60
+ con (void ) {
63
61
struct addrinfo * res , hints = {
64
62
.ai_family = AF_INET ,
65
63
.ai_socktype = SOCK_STREAM
@@ -76,85 +74,73 @@ con(void)
76
74
}
77
75
78
76
static void
79
- printw (const char * format , ...)
80
- {
77
+ printw (const char * format , ...) {
78
+
81
79
int s1 = 0 , s2 , i , o ;
82
80
va_list argptr ;
83
81
char line [BUFF + 1 ];
82
+
84
83
va_start (argptr , format );
85
84
vsnprintf (line , BUFF + 1 , format , argptr );
86
85
va_end (argptr );
87
86
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 ++ ) {
92
89
if (line [i ] == ' ' ) s1 = i ;
93
90
if (i == CMAX - 1 ) printf ("%-*.*s\n" , s1 , s1 , line );
94
91
}
95
92
s2 = o = s1 ;
96
- for (i = s1 ; line [i ] != '\0' ; i ++ )
97
- {
93
+ for (i = s1 ; line [i ] != '\0' ; i ++ ) {
98
94
if (line [i ] == ' ' ) s2 = i ;
99
- if ((i - o ) == (CMAX - GUTL ))
100
- {
95
+ if ((i - o ) == (CMAX - GUTL )) {
101
96
printf ("%*s %-*.*s\n" , GUTL , " " , s2 - o , s2 - o , & line [o + 1 ]);
102
97
o = i = s2 ;
103
98
}
104
- else if (line [i + 1 ] == '\0' )
105
- {
99
+ else if (line [i + 1 ] == '\0' ) {
106
100
printf ("%*s %-*.*s" , GUTL , " " , i - o , i - o , & line [o + 1 ]);
107
101
}
108
102
}
109
103
}
110
-
111
104
}
112
105
113
106
static void
114
- pars (int sl , char * buf )
115
- {
107
+ pars (int sl , char * buf ) {
108
+
116
109
char buf_c [BUFF + 1 ], ltr [200 ], cha [200 ], nic [200 ], hos [200 ], \
117
110
usr [200 ], cmd [200 ], msg [200 ], pre [200 ];
118
111
int o = -1 ;
119
112
120
- for (int i = 0 ; i < sl ; i ++ )
121
- {
113
+ for (int i = 0 ; i < sl ; i ++ ) {
122
114
o ++ ;
123
115
buf_c [o ] = buf [i ];
124
116
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 ) {
127
118
buf_c [o + 1 ] = '\0' ;
128
119
o = -1 ;
129
120
130
121
if (verb ) printf (">> %s" , buf_c );
131
122
132
- if (!strncmp (buf_c , "PING" , 4 ))
133
- {
123
+ if (!strncmp (buf_c , "PING" , 4 )) {
134
124
buf_c [1 ] = 'O' ;
135
125
raw (buf_c );
136
126
}
137
127
138
- else if (buf_c [0 ] == ':' )
139
- {
128
+ else if (buf_c [0 ] == ':' ) {
140
129
sscanf (buf_c , ":%[^ ] %[^:]:%[^\r]" , pre , cmd , msg );
141
130
sscanf (pre , "%[^!]!%[^@]@%s" , nic , usr , hos );
142
131
sscanf (cmd , "%[^#& ]%s" , ltr , cha );
143
132
144
133
if (!strncmp (ltr , "001" , 3 )) raw ("JOIN #%s\r\n" , chan );
145
134
146
- if (!strncmp (ltr , "QUIT" , 4 ))
147
- {
135
+ if (!strncmp (ltr , "QUIT" , 4 )) {
148
136
printw ("%*.*s \x1b[34;1m%s\x1b[0m left %s\n" , \
149
137
GUTL , GUTL , "<--" , nic , cha );
150
138
}
151
- else if (!strncmp (ltr , "JOIN" , 4 ))
152
- {
139
+ else if (!strncmp (ltr , "JOIN" , 4 )) {
153
140
printw ("%*.*s \x1b[32;1m%s\x1b[0m joined %s\n" , \
154
141
GUTL , GUTL , "-->" , nic , cha );
155
142
}
156
- else
157
- {
143
+ else {
158
144
printw ("\x1b[1m%*.*s\x1b[0m %s\n" , \
159
145
GUTL , GUTL , nic , msg );
160
146
}
@@ -164,14 +150,12 @@ pars(int sl, char *buf)
164
150
}
165
151
166
152
int
167
- main (int argc , char * * argv )
168
- {
153
+ main (int argc , char * * argv ) {
154
+
169
155
int fd [2 ], cval ;
170
156
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 ) {
175
159
case 'v' : printf ("kirc 0.0.1\n" ); break ;
176
160
case 'V' : verb = 1 ; break ;
177
161
case 's' : host = optarg ; break ;
@@ -182,64 +166,59 @@ main(int argc, char **argv)
182
166
}
183
167
}
184
168
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 ) {
187
175
fprintf (stderr , "pipe() failed" );
188
176
return 2 ;
189
177
}
190
178
191
179
pid_t pid = fork ();
192
180
193
- if (pid == 0 )
194
- {
181
+ if (pid == 0 ) {
195
182
int sl ;
196
183
char u [BUFF ];
197
184
198
185
con ();
199
186
200
- while ((sl = read (conn , sbuf , BUFF )))
201
- {
187
+ while ((sl = read (conn , sbuf , BUFF ))) {
202
188
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" )) {
205
190
raw ("%s\r\n" , u );
206
191
}
207
192
}
208
193
printf ("(press <ENTER> to quit)\n" );
209
194
}
210
- else
211
- {
195
+ else {
212
196
char usrin [CMAX ];
213
- int l = CMAX - strlen (chan );
214
197
char cmd = '\n' ;
215
198
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 ) {
220
201
write (fd [1 ], "WAIT_SIG" , CMAX );
221
202
}
222
203
223
204
fgets (usrin , CMAX , stdin );
224
205
225
- if (usrin [0 ] == ':' && usrin [1 ])
226
- {
206
+ if (usrin [0 ] == ':' && usrin [1 ]) {
227
207
char * cmd_val = & usrin [2 ];
228
208
cmd = usrin [1 ];
229
209
230
- switch (cmd )
231
- {
210
+ switch (cmd ) {
232
211
case 'q' :
233
212
write (fd [1 ], "quit" , sizeof ("quit" ));
234
213
break ;
235
214
case 'm' :
236
215
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 );
238
218
break ;
239
219
}
240
220
}
241
- else
242
- {
221
+ else {
243
222
write (fd [1 ], usrin , CMAX );
244
223
}
245
224
fflush (stdout );
0 commit comments