source: code/icb.c@ 8b1683e

Last change on this file since 8b1683e was 8b1683e, checked in by Mike Belopuhov <mike@…>, 10 years ago

Zero out buffers before passing them to the tokenizer

  • Property mode set to 100644
File size: 17.8 KB
Line 
1/*
2 * Copyright (c) 2009, 2010, 2013, 2014 Mike Belopuhov
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <sys/types.h>
18#include <sys/queue.h>
19#include <netdb.h>
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <syslog.h>
25#include <unistd.h>
26#include <ctype.h>
27#include <event.h>
28#include <vis.h>
29
30#include "icb.h"
31#include "icbd.h"
32
33extern int creategroups;
34extern char srvname[NI_MAXHOST];
35
36void icb_command(struct icb_session *, char *, char *);
37void icb_groupmsg(struct icb_session *, char *);
38int icb_login(struct icb_session *, char *, char *, char *);
39int icb_dowho(struct icb_session *, struct icb_group *);
40
41/*
42 * icb_init: initializes pointers to callbacks
43 */
44void
45icb_init(void)
46{
47 LIST_INIT(&groups);
48
49 if (strlen(srvname) == 0)
50 (void)gethostname(srvname, sizeof srvname);
51 /*
52 * NI_MAXHOST is usually greater than what we
53 * can actually send, hence truncation:
54 */
55 if (strlen(srvname) > 200)
56 srvname[200] = '\0';
57}
58
59/*
60 * icb_start: called upon accepting a new connection, greets new client
61 */
62void
63icb_start(struct icb_session *is)
64{
65 icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
66 ICB_M_SEP, "icbd");
67 SETF(is->flags, ICB_SF_PROTOSENT);
68}
69
70/*
71 * icb_input: main input processing routine
72 */
73int
74icb_input(struct icb_session *is)
75{
76 char *msg = is->buffer;
77 int msglen = is->length;
78 unsigned char type;
79 char *wptr = NULL;
80 int res = 0;
81
82 is->last = getmonotime();
83 type = msg[0];
84 msg++;
85 if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
86 icb_error(is, "Not logged in");
87 return (0);
88 }
89 switch (type) {
90 case ICB_M_LOGIN: {
91 char client[ICB_MAXNICKLEN];
92 char nick[ICB_MAXNICKLEN];
93 char group[ICB_MAXGRPLEN];
94 char cmd[ICB_MAXCMDLEN];
95
96 memset(client, 0, sizeof client);
97 if (icb_token(msg, msglen, &wptr, client, ICB_MAXNICKLEN,
98 ICB_M_SEP, 1) < 0) {
99 icb_error(is, "Invalid client");
100 icbd_drop(is, NULL);
101 return (1);
102 }
103 memset(nick, 0, sizeof nick);
104 if (icb_token(msg, msglen, &wptr, nick, ICB_MAXNICKLEN,
105 ICB_M_SEP, 1) <= 0) {
106 icb_error(is, "Invalid nick");
107 icbd_drop(is, NULL);
108 return (1);
109 }
110 memset(group, 0, sizeof group);
111 if (icb_token(msg, msglen, &wptr, group, ICB_MAXGRPLEN,
112 ICB_M_SEP, 1) < 0) {
113 icb_error(is, "Invalid login group");
114 icbd_drop(is, NULL);
115 return (1);
116 }
117 memset(cmd, 0, sizeof cmd);
118 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
119 ICB_M_SEP, 1) < 0) {
120 icb_error(is, "Invalid command");
121 icbd_drop(is, NULL);
122 return (1);
123 }
124 if (strlen(cmd) > 0 && cmd[0] == 'w') {
125 icb_error(is, "Command not implemented");
126 icbd_drop(is, NULL);
127 return (1);
128 }
129 if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
130 icb_error(is, "Malformed login packet");
131 icbd_drop(is, NULL);
132 return (1);
133 }
134 res = icb_login(is, group, nick, client);
135 break;
136 }
137 case ICB_M_OPEN: {
138 icb_groupmsg(is, msg);
139 break;
140 }
141 case ICB_M_COMMAND: {
142 char cmd[ICB_MAXCMDLEN];
143 char arg[ICB_MAXTOPICLEN];
144
145 memset(cmd, 0, sizeof cmd);
146 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
147 ICB_M_SEP, 1) <= 0) {
148 icb_error(is, "Invalid command");
149 icbd_drop(is, NULL);
150 return (1);
151 }
152 memset(arg, 0, sizeof arg);
153 if (icb_token(msg, msglen, &wptr, arg, ICB_MAXTOPICLEN,
154 ICB_M_SEP, 1) < 0) {
155 icb_error(is, "Invalid argument");
156 icbd_drop(is, NULL);
157 return (1);
158 }
159 icb_command(is, cmd, arg);
160 break;
161 }
162 case ICB_M_PONG: {
163 icb_sendfmt(is, "%c", ICB_M_PING);
164 break;
165 }
166 case ICB_M_PROTO:
167 case ICB_M_NOOP:
168 /* ignore */
169 break;
170 default:
171 /* everything else is not valid */
172 icb_error(is, "Undefined message type %u", type);
173 }
174 return (res);
175}
176
177/*
178 * icb_login: handles login ('a') packets
179 */
180int
181icb_login(struct icb_session *is, char *grp, char *nick, char *client)
182{
183 const char *defgrp = "1";
184 struct icb_group *ig;
185 struct icb_session *s;
186 char group[ICB_MAXGRPLEN];
187
188 if (!nick || strlen(nick) == 0 ||
189 icb_vis(is->nick, nick, ICB_MAXNICKLEN, VIS_SP)) {
190 icb_error(is, "Invalid nick");
191 icbd_drop(is, NULL);
192 return (1);
193 }
194 if (!grp || strlen(grp) == 0)
195 strlcpy(group, defgrp, ICB_MAXGRPLEN);
196 else
197 icb_vis(group, grp, ICB_MAXGRPLEN, VIS_SP);
198 LIST_FOREACH(ig, &groups, entry) {
199 if (strcmp(ig->name, group) == 0)
200 break;
201 }
202 if (ig == NULL) {
203 if (!creategroups) {
204 icb_error(is, "Can't create new groups", group);
205 icbd_drop(is, NULL);
206 return (1);
207 } else {
208 if ((ig = icb_addgroup(is, group)) == NULL) {
209 icb_error(is, "Can't create group %s", group);
210 return (0);
211 }
212 icbd_log(NULL, LOG_DEBUG, "%s created group %s",
213 is->nick, group);
214 }
215 }
216 LIST_FOREACH(s, &ig->sess, entry) {
217 if (strcmp(s->nick, is->nick) == 0) {
218 icb_error(is, "Nick is already in use");
219 icbd_drop(is, NULL);
220 return (1);
221 }
222 }
223
224 if (client && strlen(client) > 0)
225 icb_vis(is->client, client, sizeof is->client, VIS_SP);
226 else
227 strlcpy(is->client, is->nick, sizeof is->client);
228 is->group = ig;
229 is->login = time(NULL);
230 is->last = getmonotime();
231
232 /* notify group */
233 icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
234 is->nick, is->client, is->host);
235
236 CLRF(is->flags, ICB_SF_PROTOSENT);
237 SETF(is->flags, ICB_SF_LOGGEDIN);
238
239 LIST_INSERT_HEAD(&ig->sess, is, entry);
240
241 /* acknowledge successful login */
242 icb_sendfmt(is, "%c", ICB_M_LOGIN);
243
244 /* notify user */
245 icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
246 icb_ismod(ig, is) ? " as moderator" : "");
247
248 /* send user a topic name */
249 if (strlen(ig->topic) > 0)
250 icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
251 ig->name, ig->topic);
252 return (0);
253}
254
255/*
256 * icb_groupmsg: handles open message ('b') packets
257 */
258void
259icb_groupmsg(struct icb_session *is, char *msg)
260{
261 char buf[ICB_MSGSIZE];
262 struct icb_group *ig = is->group;
263 struct icb_session *s;
264 int res, buflen = 1;
265
266 if (strlen(msg) == 0) {
267 icb_error(is, "Empty message");
268 return;
269 }
270
271 res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
272 is->nick, ICB_M_SEP, msg);
273 if (res < 0) {
274 icb_error(is, "Format error");
275 return;
276 }
277 buflen += MIN((size_t)res, sizeof buf - 1);
278 buf[0] = buflen;
279
280 logger(ig->name, is->nick, msg);
281
282 LIST_FOREACH(s, &ig->sess, entry) {
283 if (s == is)
284 continue;
285 icbd_send(s, buf, buflen + 1);
286 }
287}
288
289/*
290 * icb_privmsg: handles personal message ('c') packets
291 */
292void
293icb_privmsg(struct icb_session *is, char *to, char *msg)
294{
295 struct icb_group *ig = is->group;
296 struct icb_session *s;
297 char whom[ICB_MAXNICKLEN];
298
299 icb_vis(whom, to, ICB_MAXNICKLEN, VIS_SP);
300
301 /* try home group first */
302 LIST_FOREACH(s, &ig->sess, entry) {
303 if (strcmp(s->nick, whom) == 0)
304 break;
305 }
306 if (!s) {
307 /* try all groups until the first match */
308 LIST_FOREACH(ig, &groups, entry) {
309 LIST_FOREACH(s, &ig->sess, entry) {
310 if (strcmp(s->nick, whom) == 0)
311 break;
312 }
313 if (s)
314 break;
315 }
316 if (!s) {
317 icb_error(is, "No such user %s", whom);
318 return;
319 }
320 }
321 icb_sendfmt(s, "%c%s%c%s", ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
322}
323
324/*
325 * icb_command: handles command ('h') packets
326 */
327void
328icb_command(struct icb_session *is, char *cmd, char *arg)
329{
330 void (*handler)(struct icb_session *, char *);
331 char command[ICB_MAXCMDLEN];
332
333 icb_vis(command, cmd, sizeof command, VIS_SP);
334
335 if ((handler = icb_cmd_lookup(command)) == NULL) {
336 icb_error(is, "Unsupported command: %s", command);
337 return;
338 }
339 handler(is, arg);
340}
341
342/*
343 * icb_cmdout: sends out command output ('i') packets, called from the
344 * command handlers
345 */
346void
347icb_cmdout(struct icb_session *is, int type, char *outmsg)
348{
349 char *otype = NULL;
350
351 switch (type) {
352 case CMDOUT_CO:
353 otype = "co";
354 break;
355 case CMDOUT_EC:
356 otype = "ec";
357 break;
358 case CMDOUT_WG:
359 otype = "wg";
360 break;
361 case CMDOUT_WH:
362 otype = "wh";
363 break;
364 case CMDOUT_WL:
365 otype = "wl";
366 break;
367 default:
368 icbd_log(is, LOG_ERR, "unknown cmdout type %d", type);
369 return;
370 }
371 if (outmsg)
372 icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
373 outmsg);
374 else
375 icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
376}
377
378/*
379 * icb_status: sends a status message ('d') to the client
380 */
381void
382icb_status(struct icb_session *is, int type, const char *fmt, ...)
383{
384 va_list ap;
385 char buf[ICB_MSGSIZE];
386 int res, buflen = 1;
387 static const struct {
388 int type;
389 const char *msg;
390 } msgtab[] = {
391 { STATUS_ARRIVE, "Arrive" },
392 { STATUS_BOOT, "Boot" },
393 { STATUS_DEPART, "Depart" },
394 { STATUS_HELP, "Help" },
395 { STATUS_NAME, "Name" },
396 { STATUS_NOBEEP, "No-Beep" },
397 { STATUS_NOTIFY, "Notify" },
398 { STATUS_SIGNON, "Sign-on" },
399 { STATUS_SIGNOFF, "Sign-off" },
400 { STATUS_STATUS, "Status" },
401 { STATUS_TOPIC, "Topic" },
402 { STATUS_WARNING, "Warning" },
403 { 0, NULL }
404 };
405
406 if (type < 0 || type > (int)nitems(msgtab) - 1)
407 return;
408 res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
409 msgtab[type].msg, ICB_M_SEP);
410 if (res < 0) {
411 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
412 return;
413 }
414 buflen += MIN((size_t)res, sizeof buf - 1);
415 if ((size_t)buflen >= sizeof buf) {
416 icbd_log(NULL, LOG_ERR, "Status buffer too small");
417 return;
418 }
419 va_start(ap, fmt);
420 res = vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
421 va_end(ap);
422 if (res < 0) {
423 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
424 return;
425 }
426 buflen += MIN((size_t)res, sizeof buf - buflen);
427 buf[0] = buflen;
428 icbd_send(is, buf, buflen + 1);
429}
430
431/*
432 * icb_status: sends a status message ('d') to the group except of the
433 * "ex" if it's not NULL
434 */
435void
436icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
437 const char *fmt, ...)
438{
439 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
440 va_list ap;
441 struct icb_session *s;
442
443 va_start(ap, fmt);
444 (void)vsnprintf(buf, sizeof buf, fmt, ap);
445 LIST_FOREACH(s, &ig->sess, entry) {
446 if (ex && s == ex)
447 continue;
448 icb_status(s, type, buf);
449 }
450 logger(ig->name, "", buf);
451 icbd_log(NULL, LOG_DEBUG, "%s", buf);
452 va_end(ap);
453}
454
455/*
456 * icb_error: sends an error message ('e') to the client
457 */
458void
459icb_error(struct icb_session *is, const char *fmt, ...)
460{
461 char buf[ICB_MSGSIZE];
462 va_list ap;
463 int res, buflen = 1;
464
465 va_start(ap, fmt);
466 res = vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
467 va_end(ap);
468 if (res < 0) {
469 icbd_log(NULL, LOG_ERR, "Format error");
470 return;
471 }
472 buflen += MIN((size_t)res, sizeof buf - 2);
473 buf[0] = ++buflen; /* account for ICB_M_ERROR */
474 buf[1] = ICB_M_ERROR;
475 icbd_send(is, buf, buflen + 1);
476 icbd_log(is, LOG_DEBUG, "%s", buf + 2);
477}
478
479/*
480 * icb_remove: removes a session from the associated group
481 */
482void
483icb_remove(struct icb_session *is, char *reason)
484{
485 if (is->group) {
486 if (icb_ismod(is->group, is))
487 (void)icb_pass(is->group, is, NULL);
488 LIST_REMOVE(is, entry);
489 if (reason)
490 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
491 "%s (%s@%s) just left: %s", is->nick, is->client,
492 is->host, reason);
493 else
494 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
495 "%s (%s@%s) just left", is->nick, is->client,
496 is->host);
497 is->group = NULL;
498 }
499}
500
501/*
502 * icb_addgroup: adds a new group to the list
503 */
504struct icb_group *
505icb_addgroup(struct icb_session *is, char *name)
506{
507 struct icb_group *ig;
508
509 if ((ig = calloc(1, sizeof *ig)) == NULL)
510 return (NULL);
511 strlcpy(ig->name, name, sizeof ig->name);
512 if (is)
513 ig->mod = is;
514 LIST_INIT(&ig->sess);
515 LIST_INSERT_HEAD(&groups, ig, entry);
516 return (ig);
517}
518
519#ifdef notused
520/*
521 * icb_delgroup: removes a group from the list
522 */
523void
524icb_delgroup(struct icb_group *ig)
525{
526 struct icb_session *s;
527
528 /* well, i guess we should kick out participants! ;-) */
529 LIST_FOREACH(s, &ig->sess, entry) {
530 icb_status(s, STATUS_WARNING, "Group dismissed");
531 s->group = NULL;
532 }
533 LIST_REMOVE(ig, entry);
534 bzero(ig, sizeof ig); /* paranoic thing, obviously */
535 free(ig);
536}
537#endif
538
539/*
540 * icb_dowho: a helper function that sends out a group header as a command
541 * output and user information in the "wl" format
542 */
543int
544icb_dowho(struct icb_session *is, struct icb_group *ig)
545{
546 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
547 struct icb_session *s;
548 time_t now;
549 int nusers = 0;
550
551 now = getmonotime();
552 icb_cmdout(is, CMDOUT_CO, " ");
553 snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
554 ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
555 strlen(ig->topic) > 0 ? ig->topic : "(None)");
556 icb_cmdout(is, CMDOUT_CO, buf);
557 LIST_FOREACH(s, &ig->sess, entry) {
558 (void)snprintf(buf, sizeof buf,
559 "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
560 icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
561 s->nick, ICB_M_SEP, now - s->last,
562 ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
563 s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
564 icb_cmdout(is, CMDOUT_WL, buf);
565 nusers++;
566 }
567 return (nusers);
568}
569
570/*
571 * icb_who: sends a list of users of either the specified group or all
572 * groups found on the server
573 */
574void
575icb_who(struct icb_session *is, struct icb_group *ig)
576{
577 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
578 struct icb_group *g;
579
580 if (!ig) {
581 int nusers = 0, ngroups = 0;
582
583 LIST_FOREACH(g, &groups, entry) {
584 nusers += icb_dowho(is, g);
585 ngroups++;
586 }
587 if (nusers > 0) {
588 (void)snprintf(buf, sizeof buf,
589 "Total: %d %s in %d %s",
590 nusers, nusers > 1 ? "users" : "user",
591 ngroups, ngroups > 1 ? "groups" : "group");
592 } else
593 (void)snprintf(buf, sizeof buf, "No users found.");
594 icb_cmdout(is, CMDOUT_CO, buf);
595 } else
596 (void)icb_dowho(is, ig);
597}
598
599/*
600 * icb_ismod: checks whether group is moderated by "is"
601 */
602inline int
603icb_ismod(struct icb_group *ig, struct icb_session *is)
604{
605 return (ig->mod == is);
606}
607
608/*
609 * icb_modpermit: checks user against the moderators table if it has
610 * been populated
611 */
612int
613icb_modpermit(struct icb_session *is, int enforce)
614{
615 extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
616 extern int modtabcnt;
617
618 icbd_modupdate();
619 if ((enforce ? 0 : modtabcnt == 0) ||
620 bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
621 (int (*)(const void *, const void *))strcmp))
622 return (1);
623 return (0);
624}
625
626/*
627 * icb_pass: passes moderation of group "ig" from "from" to "to",
628 * returns -1 if "from" is not a moderator, 1 if passed
629 * to "to" and 0 otherwise (no moderator or passed to the
630 * internal bot)
631 */
632int
633icb_pass(struct icb_group *ig, struct icb_session *from,
634 struct icb_session *to)
635{
636 if (ig->mod && ig->mod != from)
637 return (-1);
638 if (!from && !to)
639 return (-1);
640 ig->mod = to;
641 if (to)
642 icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
643 " of %s", from ? from->nick : "server", ig->name);
644 icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
645 "to %s", from ? from->nick : "server", to ? to->nick : "server");
646 return (1);
647}
648
649/*
650 * icb_sendfmt: formats a string and sends it over
651 */
652void
653icb_sendfmt(struct icb_session *is, const char *fmt, ...)
654{
655 char buf[ICB_MSGSIZE];
656 va_list ap;
657 int res, buflen = 1;
658
659 va_start(ap, fmt);
660 res = vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
661 va_end(ap);
662 if (res < 0) {
663 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
664 return;
665 }
666 buflen += MIN((size_t)res, sizeof buf - 1);
667 buf[0] = buflen;
668 icbd_send(is, buf, buflen + 1);
669}
670
671/*
672 * icb_token: copies a sequence of characters delimited by the 'sep' character
673 * from the source buffer 'buf' at offset indicated by 'bufptr' to
674 * the destination buffer 'dst' and sets 'bufptr' to the next byte
675 * after 'sep'.
676 */
677int
678icb_token(char *buf, int len, char **bufptr, char *dst, int dstlen, int sep,
679 int trim)
680{
681 char *start;
682 int i, ret;
683
684 if (buf == NULL || len <= 0 || dst == NULL || dstlen <= 0)
685 return (0);
686 if (*bufptr == NULL)
687 *bufptr = buf;
688 start = *bufptr;
689 for (i = *bufptr - buf; i < len; i++, (*bufptr)++) {
690 if (**bufptr == sep || **bufptr == '\0') {
691 /* copy and null terminate the token */
692 ret = strlcpy(dst, start,
693 MIN(*bufptr - start + 1, dstlen));
694 if (**bufptr != '\0')
695 (*bufptr)++;
696 if (ret > 0 && trim)
697 ret = icb_trim(dst, dstlen);
698 return (ret);
699 }
700 }
701 /*
702 * Reached the end of the buffer without finding a field separator
703 * nor the end of line character. If we have advanced our pointer
704 * we should copy the resulting single field out.
705 */
706 if (*bufptr - start > 0) {
707 ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dstlen));
708 if (ret > 0 && trim)
709 ret = icb_trim(dst, dstlen);
710 return (ret);
711 }
712 return (0);
713}
714
715/*
716 * icb_trim: trims trailing whitespace
717 */
718int
719icb_trim(char *buf, int len)
720{
721 char *p = buf;
722 int i;
723
724 for (i = 0; i < len && *p != '\0'; i++)
725 p++;
726 if (*p == '\0' && p - buf > 0)
727 p--;
728 while (p >= buf && isspace(*p)) {
729 *p = '\0';
730 i--;
731 if (p > buf)
732 p--;
733 }
734 return (i);
735}
736
737/*
738 * icb_vis: strnvis-like function that escapes percentages as well
739 */
740int
741icb_vis(char *dst, const char *src, size_t dstsize, int flags)
742{
743 int si = 0, di = 0, td;
744
745 while ((size_t)di < dstsize - 1 && src[si] != '\0') {
746 if (src[si] == '%') {
747 if ((size_t)di + 1 >= dstsize - 1)
748 break;
749 dst[di++] = '%', dst[di] = '%';
750 } else if (src[si] == ' ' && flags & VIS_SP)
751 dst[di] = '_';
752 else if (isgraph(src[si]) || src[si] == ' ')
753 dst[di] = src[si];
754 else {
755 td = snprintf(&dst[di], dstsize - di,
756 "\\%03o", (unsigned char)src[si]);
757 if (td == -1 || (size_t)td >= dstsize - di)
758 break;
759 di += td - 1;
760 }
761 si++, di++;
762 }
763 dst[MIN((size_t)di, dstsize - 1)] = '\0';
764 return (0);
765}
Note: See TracBrowser for help on using the repository browser.