source: code/icb.c@ e22d747

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

Improve groups list declaration

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