]> diplodocus.org Git - nmh/blob - uip/mhical.c
picksbr.c: fprintf(3) function pointer using `%p' and union.
[nmh] / uip / mhical.c
1 /* mhical.c -- operate on an iCalendar request
2 *
3 * This code is Copyright (c) 2014, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "h/icalendar.h"
10 #include "sbr/icalparse.h"
11 #include <h/fmt_scan.h>
12 #include "h/addrsbr.h"
13 #include "h/mts.h"
14 #include "h/done.h"
15 #include "h/utils.h"
16 #include <time.h>
17
18 typedef enum act {
19 ACT_NONE,
20 ACT_ACCEPT,
21 ACE_DECLINE,
22 ACT_TENTATIVE,
23 ACT_DELEGATE,
24 ACT_CANCEL
25 } act;
26
27 static void convert_to_reply (contentline *, act);
28 static void convert_to_cancellation (contentline *);
29 static void convert_common (contentline *, act);
30 static void dump_unfolded (FILE *, contentline *);
31 static void output (FILE *, contentline *, int);
32 static void display (FILE *, contentline *, char *);
33 static const char *identity (const contentline *) PURE;
34 static char *format_params (char *, param_list *);
35 static char *fold (char *, int);
36
37 #define MHICAL_SWITCHES \
38 X("reply accept|decline|tentative", 0, REPLYSW) \
39 X("cancel", 0, CANCELSW) \
40 X("form formatfile", 0, FORMSW) \
41 X("format string", 5, FMTSW) \
42 X("infile", 0, INFILESW) \
43 X("outfile", 0, OUTFILESW) \
44 X("contenttype", 0, CONTENTTYPESW) \
45 X("nocontenttype", 0, NCONTENTTYPESW) \
46 X("unfold", 0, UNFOLDSW) \
47 X("debug", 0, DEBUGSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
50
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MHICAL);
53 #undef X
54
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MHICAL, switches);
57 #undef X
58
59 vevent vevents = { NULL, NULL, NULL};
60 int parser_status = 0;
61
62 int
63 main (int argc, char *argv[]) {
64 /* RFC 5322 § 3.3 date-time format, including the optional
65 day-of-week and not including the optional seconds. The
66 zone is required by the RFC but not always output by this
67 format, because RFC 5545 § 3.3.5 allows date-times not
68 bound to any time zone. */
69
70 act action = ACT_NONE;
71 char *infile = NULL, *outfile = NULL;
72 FILE *inputfile = NULL, *outputfile = NULL;
73 int contenttype = 0, unfold = 0;
74 vevent *v, *nextvevent;
75 char *form = "mhical.24hour", *format = NULL;
76 char **argp, **arguments, *cp;
77
78 icaldebug = 0; /* Global provided by bison (with name-prefix "ical"). */
79
80 if (nmh_init(argv[0], 2)) { return 1; }
81
82 arguments = getarguments (invo_name, argc, argv, 1);
83 argp = arguments;
84
85 /*
86 * Parse arguments
87 */
88 while ((cp = *argp++)) {
89 if (*cp == '-') {
90 switch (smatch (++cp, switches)) {
91 case AMBIGSW:
92 ambigsw (cp, switches);
93 done (1);
94 case UNKWNSW:
95 adios (NULL, "-%s unknown", cp);
96
97 case HELPSW: {
98 char buf[128];
99 snprintf (buf, sizeof buf, "%s [switches]", invo_name);
100 print_help (buf, switches, 1);
101 done (0);
102 }
103 case VERSIONSW:
104 print_version(invo_name);
105 done (0);
106 case DEBUGSW:
107 icaldebug = 1;
108 continue;
109
110 case REPLYSW:
111 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
112 adios (NULL, "missing argument to %s", argp[-2]);
113 if (! strcasecmp (cp, "accept")) {
114 action = ACT_ACCEPT;
115 } else if (! strcasecmp (cp, "decline")) {
116 action = ACE_DECLINE;
117 } else if (! strcasecmp (cp, "tentative")) {
118 action = ACT_TENTATIVE;
119 } else if (! strcasecmp (cp, "delegate")) {
120 action = ACT_DELEGATE;
121 } else {
122 adios (NULL, "Unknown action: %s", cp);
123 }
124 continue;
125
126 case CANCELSW:
127 action = ACT_CANCEL;
128 continue;
129
130 case FORMSW:
131 if (! (form = *argp++) || *form == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
133 format = NULL;
134 continue;
135 case FMTSW:
136 if (! (format = *argp++) || *format == '-')
137 adios (NULL, "missing argument to %s", argp[-2]);
138 form = NULL;
139 continue;
140
141 case INFILESW:
142 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
143 adios (NULL, "missing argument to %s", argp[-2]);
144 infile = *cp == '-' ? mh_xstrdup(cp) : path (cp, TFILE);
145 continue;
146 case OUTFILESW:
147 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
148 adios (NULL, "missing argument to %s", argp[-2]);
149 outfile = *cp == '-' ? mh_xstrdup(cp) : path (cp, TFILE);
150 continue;
151
152 case CONTENTTYPESW:
153 contenttype = 1;
154 continue;
155 case NCONTENTTYPESW:
156 contenttype = 0;
157 continue;
158
159 case UNFOLDSW:
160 unfold = 1;
161 continue;
162 }
163 }
164 }
165
166 free (arguments);
167
168 if (infile) {
169 if ((inputfile = fopen (infile, "r"))) {
170 icalset_inputfile (inputfile);
171 } else {
172 adios (infile, "error opening");
173 }
174 } else {
175 inputfile = stdin;
176 }
177
178 if (outfile) {
179 if ((outputfile = fopen (outfile, "w"))) {
180 icalset_outputfile (outputfile);
181 } else {
182 adios (outfile, "error opening");
183 }
184 } else {
185 outputfile = stdout;
186 }
187
188 vevents.last = &vevents;
189 /* vevents is accessed by parser as global. */
190 icalparse ();
191
192 for (v = &vevents; v; v = nextvevent) {
193 if (! unfold && v != &vevents && v->contentlines &&
194 v->contentlines->name &&
195 strcasecmp (v->contentlines->name, "END") &&
196 v->contentlines->value &&
197 strcasecmp (v->contentlines->value, "VCALENDAR")) {
198 /* Output blank line between vevents. Not before
199 first vevent and not after last. */
200 putc ('\n', outputfile);
201 }
202
203 if (action == ACT_NONE) {
204 if (unfold) {
205 dump_unfolded (outputfile, v->contentlines);
206 } else {
207 char *nfs = new_fs (form, format, NULL);
208
209 display (outputfile, v->contentlines, nfs);
210 free_fs ();
211 }
212 } else {
213 if (action == ACT_CANCEL) {
214 convert_to_cancellation (v->contentlines);
215 } else {
216 convert_to_reply (v->contentlines, action);
217 }
218 output (outputfile, v->contentlines, contenttype);
219 }
220
221 free_contentlines (v->contentlines);
222 nextvevent = v->next;
223 if (v != &vevents) {
224 free (v);
225 }
226 }
227
228 if (infile) {
229 if (fclose (inputfile) != 0) {
230 advise (infile, "error closing");
231 }
232 free (infile);
233 }
234 if (outfile) {
235 if (fclose (outputfile) != 0) {
236 advise (outfile, "error closing");
237 }
238 free (outfile);
239 }
240
241 return parser_status;
242 }
243
244 /*
245 * - Change METHOD from REQUEST to REPLY.
246 * - Change PRODID.
247 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
248 * - For the user's ATTENDEE line:
249 * - Remove ROLE and RSVP parameters.
250 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
251 * DECLINED, or TENTATIVE.
252 * - Insert action at beginning of SUMMARY value.
253 * - Remove all X- lines.
254 * - Update DTSTAMP with current timestamp.
255 * - Remove all DESCRIPTION lines.
256 * - Excise VALARM sections.
257 */
258 static void
259 convert_to_reply (contentline *clines, act action) {
260 char *partstat = NULL;
261 int found_my_attendee_line = 0;
262 contentline *node;
263
264 convert_common (clines, action);
265
266 switch (action) {
267 case ACT_ACCEPT:
268 partstat = "ACCEPTED";
269 break;
270 case ACE_DECLINE:
271 partstat = "DECLINED";
272 break;
273 case ACT_TENTATIVE:
274 partstat = "TENTATIVE";
275 break;
276 default:
277 ;
278 }
279
280 /* Call find_contentline () with node as argument to find multiple
281 matching contentlines. */
282 for (node = clines;
283 (node = find_contentline (node, "ATTENDEE", 0));
284 node = node->next) {
285 param_list *p;
286
287 ismymbox (NULL); /* need to prime ismymbox() */
288
289 /* According to RFC 5545 § 3.3.3, an email address in the
290 value must be a mailto URI. */
291 if (! strncasecmp (node->value, "mailto:", 7)) {
292 char *addr = node->value + 7;
293 struct mailname *mn;
294
295 /* Skip any leading whitespace. */
296 for ( ; isspace ((unsigned char) *addr); ++addr) { continue; }
297
298 addr = getname (addr);
299 mn = getm (addr, NULL, 0, NULL, 0);
300
301 /* Need to flush getname after use. */
302 while (getname ("")) { continue; }
303
304 if (ismymbox (mn)) {
305 found_my_attendee_line = 1;
306 for (p = node->params; p && p->param_name; p = p->next) {
307 value_list *v;
308
309 for (v = p->values; v; v = v->next) {
310 if (! strcasecmp (p->param_name, "ROLE") ||
311 ! strcasecmp (p->param_name, "RSVP")) {
312 remove_value (v);
313 } else if (! strcasecmp (p->param_name, "PARTSTAT")) {
314 free (v->value);
315 v->value = strdup (partstat);
316 }
317 }
318 }
319 } else {
320 remove_contentline (node);
321 }
322
323 mnfree (mn);
324 }
325 }
326
327 if (! found_my_attendee_line) {
328 /* Generate and attach an ATTENDEE line for me. */
329 contentline *node;
330
331 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
332 if ((node = find_contentline (clines, "ORGANIZER", 0)) ||
333 (node = find_contentline (clines, "BEGIN", "VEVENT"))) {
334 contentline *new_node = add_contentline (node, "ATTENDEE");
335
336 add_param_name (new_node, mh_xstrdup ("PARTSTAT"));
337 add_param_value (new_node, mh_xstrdup (partstat));
338 add_param_name (new_node, mh_xstrdup ("CN"));
339 add_param_value (new_node, mh_xstrdup (getfullname ()));
340 new_node->value = concat ("MAILTO:", getlocalmbox (), NULL);
341 }
342 }
343
344 /* Call find_contentline () with node as argument to find multiple
345 matching contentlines. */
346 for (node = clines;
347 (node = find_contentline (node, "DESCRIPTION", 0));
348 node = node->next) {
349 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
350 remove_contentline (node);
351 }
352 }
353
354 /*
355 * - Change METHOD from REQUEST to CANCEL.
356 * - Change PRODID.
357 * - Insert action at beginning of SUMMARY value.
358 * - Remove all X- lines.
359 * - Update DTSTAMP with current timestamp.
360 * - Change STATUS from CONFIRMED to CANCELLED.
361 * - Increment value of SEQUENCE.
362 * - Excise VALARM sections.
363 */
364 static void
365 convert_to_cancellation (contentline *clines) {
366 contentline *node;
367
368 convert_common (clines, ACT_CANCEL);
369
370 if ((node = find_contentline (clines, "STATUS", 0)) &&
371 ! strcasecmp (node->value, "CONFIRMED")) {
372 free (node->value);
373 node->value = mh_xstrdup ("CANCELLED");
374 }
375
376 if ((node = find_contentline (clines, "SEQUENCE", 0))) {
377 int sequence = atoi (node->value);
378 char buf[32];
379
380 (void) snprintf (buf, sizeof buf, "%d", sequence + 1);
381 free (node->value);
382 node->value = mh_xstrdup (buf);
383 }
384 }
385
386 static void
387 convert_common (contentline *clines, act action) {
388 contentline *node;
389 int in_valarm;
390
391 if ((node = find_contentline (clines, "METHOD", 0))) {
392 free (node->value);
393 node->value = mh_xstrdup (action == ACT_CANCEL ? "CANCEL" : "REPLY");
394 }
395
396 if ((node = find_contentline (clines, "PRODID", 0))) {
397 free (node->value);
398 node->value = mh_xstrdup ("nmh mhical v0.1");
399 }
400
401 if ((node = find_contentline (clines, "VERSION", 0))) {
402 if (! node->value) {
403 inform("Version property is missing value, assume 2.0, continuing...");
404 node->value = mh_xstrdup ("2.0");
405 }
406
407 if (strcmp (node->value, "2.0")) {
408 inform("supports the Version 2.0 specified by RFC 5545 "
409 "but iCalendar object has Version %s, continuing...",
410 node->value);
411 node->value = mh_xstrdup ("2.0");
412 }
413 }
414
415 if ((node = find_contentline (clines, "SUMMARY", 0))) {
416 char *insert = NULL;
417
418 switch (action) {
419 case ACT_ACCEPT:
420 insert = "Accepted: ";
421 break;
422 case ACE_DECLINE:
423 insert = "Declined: ";
424 break;
425 case ACT_TENTATIVE:
426 insert = "Tentative: ";
427 break;
428 case ACT_DELEGATE:
429 adios (NULL, "Delegate replies are not supported");
430 break;
431 case ACT_CANCEL:
432 insert = "Cancelled:";
433 break;
434 default:
435 ;
436 }
437
438 if (insert) {
439 const size_t len = strlen (insert) + strlen (node->value) + 1;
440 char *tmp = mh_xmalloc (len);
441
442 (void) strncpy (tmp, insert, len);
443 (void) strncat (tmp, node->value, len - strlen (insert) - 1);
444 free (node->value);
445 node->value = tmp;
446 } else {
447 /* Should never get here. */
448 adios (NULL, "Unknown action: %d", action);
449 }
450 }
451
452 if ((node = find_contentline (clines, "DTSTAMP", 0))) {
453 const time_t now = time (NULL);
454 struct tm now_tm;
455
456 if (gmtime_r (&now, &now_tm)) {
457 /* 17 would be sufficient given that RFC 5545 § 3.3.4
458 supports only a 4 digit year. */
459 char buf[32];
460
461 if (strftime (buf, sizeof buf, "%Y%m%dT%H%M%SZ", &now_tm)) {
462 free (node->value);
463 node->value = mh_xstrdup (buf);
464 } else {
465 inform("strftime unable to format current time, continuing...");
466 }
467 } else {
468 inform("gmtime_r failed on current time, continuing...");
469 }
470 }
471
472 /* Excise X- lines and VALARM section(s). */
473 in_valarm = 0;
474 for (node = clines; node; node = node->next) {
475 /* node->name will be NULL if the line was deleted. */
476 if (! node->name) { continue; }
477
478 if (in_valarm) {
479 if (! strcasecmp ("END", node->name) &&
480 ! strcasecmp ("VALARM", node->value)) {
481 in_valarm = 0;
482 }
483 remove_contentline (node);
484 } else {
485 if (! strcasecmp ("BEGIN", node->name) &&
486 ! strcasecmp ("VALARM", node->value)) {
487 in_valarm = 1;
488 remove_contentline (node);
489 } else if (! strncasecmp ("X-", node->name, 2)) {
490 remove_contentline (node);
491 }
492 }
493 }
494 }
495
496 /* Echo the input, but with unfolded lines. */
497 static void
498 dump_unfolded (FILE *file, contentline *clines) {
499 contentline *node;
500
501 for (node = clines; node; node = node->next) {
502 fputs (node->input_line, file);
503 }
504 }
505
506 static void
507 output (FILE *file, contentline *clines, int contenttype) {
508 contentline *node;
509
510 if (contenttype) {
511 /* Generate a Content-Type header to pass the method parameter
512 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
513 UTF-8. But we don't attempt to do any conversion of the
514 input. */
515 if ((node = find_contentline (clines, "METHOD", 0))) {
516 fprintf (file,
517 "Content-Type: text/calendar; method=\"%s\"; "
518 "charset=\"UTF-8\"\n\n",
519 node->value);
520 }
521 }
522
523 for (node = clines; node; node = node->next) {
524 if (node->name) {
525 char *line = NULL;
526 size_t len;
527
528 line = mh_xstrdup (node->name);
529 line = format_params (line, node->params);
530
531 len = strlen (line);
532 line = mh_xrealloc (line, len + 2);
533 line[len] = ':';
534 line[len + 1] = '\0';
535
536 line = fold (add (node->value, line),
537 clines->cr_before_lf == CR_BEFORE_LF);
538
539 fputs(line, file);
540 if (clines->cr_before_lf != LF_ONLY)
541 putc('\r', file);
542 putc('\n', file);
543 free (line);
544 }
545 }
546 }
547
548 /*
549 * Display these fields of the iCalendar event:
550 * - method
551 * - organizer
552 * - summary
553 * - description, except for "\n\n" and in VALARM
554 * - location
555 * - dtstart in local timezone
556 * - dtend in local timezone
557 * - attendees (limited to number specified in initialization)
558 */
559 static void
560 display (FILE *file, contentline *clines, char *nfs) {
561 tzdesc_t timezones = load_timezones (clines);
562 int in_vtimezone;
563 int in_valarm;
564 contentline *node;
565 struct format *fmt;
566 int dat[5] = { 0, 0, 0, INT_MAX, 0 };
567 struct comp *c;
568 charstring_t buffer = charstring_create (BUFSIZ);
569 charstring_t attendees = charstring_create (BUFSIZ);
570 const unsigned int max_attendees = 20;
571 unsigned int num_attendees;
572
573 /* Don't call on the END:VCALENDAR line. */
574 if (clines && clines->next) {
575 (void) fmt_compile (nfs, &fmt, 1);
576 }
577
578 if ((c = fmt_findcomp ("method"))) {
579 if ((node = find_contentline (clines, "METHOD", 0)) && node->value) {
580 c->c_text = mh_xstrdup (node->value);
581 }
582 }
583
584 if ((c = fmt_findcomp ("organizer"))) {
585 if ((node = find_contentline (clines, "ORGANIZER", 0)) &&
586 node->value) {
587 c->c_text = mh_xstrdup (identity (node));
588 }
589 }
590
591 if ((c = fmt_findcomp ("summary"))) {
592 if ((node = find_contentline (clines, "SUMMARY", 0)) && node->value) {
593 c->c_text = mh_xstrdup (node->value);
594 }
595 }
596
597 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
598 in_valarm = 0;
599 if ((c = fmt_findcomp ("description"))) {
600 for (node = clines; node; node = node->next) {
601 /* node->name will be NULL if the line was deleted. */
602 if (node->name && node->value && ! in_valarm &&
603 ! strcasecmp ("DESCRIPTION", node->name) &&
604 strcasecmp (node->value, "\\n\\n")) {
605 c->c_text = mh_xstrdup (node->value);
606 } else if (in_valarm) {
607 if (! strcasecmp ("END", node->name) &&
608 ! strcasecmp ("VALARM", node->value)) {
609 in_valarm = 0;
610 }
611 } else {
612 if (! strcasecmp ("BEGIN", node->name) &&
613 ! strcasecmp ("VALARM", node->value)) {
614 in_valarm = 1;
615 }
616 }
617 }
618 }
619
620 if ((c = fmt_findcomp ("location"))) {
621 if ((node = find_contentline (clines, "LOCATION", 0)) &&
622 node->value) {
623 c->c_text = mh_xstrdup (node->value);
624 }
625 }
626
627 if ((c = fmt_findcomp ("dtstart"))) {
628 /* Find DTSTART outsize of a VTIMEZONE section. */
629 in_vtimezone = 0;
630 for (node = clines; node; node = node->next) {
631 /* node->name will be NULL if the line was deleted. */
632 if (! node->name) { continue; }
633
634 if (in_vtimezone) {
635 if (! strcasecmp ("END", node->name) &&
636 ! strcasecmp ("VTIMEZONE", node->value)) {
637 in_vtimezone = 0;
638 }
639 } else {
640 if (! strcasecmp ("BEGIN", node->name) &&
641 ! strcasecmp ("VTIMEZONE", node->value)) {
642 in_vtimezone = 1;
643 } else if (! strcasecmp ("DTSTART", node->name)) {
644 /* Got it: DTSTART outside of a VTIMEZONE section. */
645 char *datetime = format_datetime (timezones, node);
646 c->c_text = datetime ? datetime : mh_xstrdup(node->value);
647 }
648 }
649 }
650 }
651
652 if ((c = fmt_findcomp ("dtend"))) {
653 if ((node = find_contentline (clines, "DTEND", 0)) && node->value) {
654 char *datetime = format_datetime (timezones, node);
655 c->c_text = datetime ? datetime : strdup(node->value);
656 } else if ((node = find_contentline (clines, "DTSTART", 0)) &&
657 node->value) {
658 /* There is no DTEND. If there's a DTSTART, use it. If it
659 doesn't have a time, assume that the event is for the
660 entire day and append 23:59:59 to it so that it signifies
661 the end of the day. And assume local timezone. */
662 if (strchr(node->value, 'T')) {
663 char * datetime = format_datetime (timezones, node);
664 c->c_text = datetime ? datetime : strdup(node->value);
665 } else {
666 char *datetime;
667 contentline node_copy;
668
669 node_copy = *node;
670 node_copy.value = concat(node_copy.value, "T235959", NULL);
671 datetime = format_datetime (timezones, &node_copy);
672 c->c_text = datetime ? datetime : strdup(node_copy.value);
673 free(node_copy.value);
674 }
675 }
676 }
677
678 if ((c = fmt_findcomp ("attendees"))) {
679 /* Call find_contentline () with node as argument to find multiple
680 matching contentlines. */
681 charstring_append_cstring (attendees, "Attendees: ");
682 for (node = clines, num_attendees = 0;
683 (node = find_contentline (node, "ATTENDEE", 0)) &&
684 num_attendees++ < max_attendees;
685 node = node->next) {
686 const char *id = identity (node);
687
688 if (num_attendees > 1) {
689 charstring_append_cstring (attendees, ", ");
690 }
691 charstring_append_cstring (attendees, id);
692 }
693
694 if (num_attendees >= max_attendees) {
695 unsigned int not_shown = 0;
696
697 for ( ;
698 (node = find_contentline (node, "ATTENDEE", 0));
699 node = node->next) {
700 ++not_shown;
701 }
702
703 if (not_shown > 0) {
704 char buf[32];
705
706 (void) snprintf (buf, sizeof buf, ", and %d more", not_shown);
707 charstring_append_cstring (attendees, buf);
708 }
709 }
710
711 if (num_attendees > 0) {
712 c->c_text = charstring_buffer_copy (attendees);
713 }
714 }
715
716 /* Don't call on the END:VCALENDAR line. */
717 if (clines && clines->next) {
718 (void) fmt_scan (fmt, buffer, INT_MAX, dat, NULL);
719 fputs (charstring_buffer (buffer), file);
720 fmt_free (fmt, 1);
721 }
722
723 charstring_free (attendees);
724 charstring_free (buffer);
725 free_timezones (timezones);
726 }
727
728 static const char *
729 identity (const contentline *node) {
730 /* According to RFC 5545 § 3.3.3, an email address in the value
731 must be a mailto URI. */
732 if (! strncasecmp (node->value, "mailto:", 7)) {
733 char *addr;
734 param_list *p;
735
736 for (p = node->params; p && p->param_name; p = p->next) {
737 value_list *v;
738
739 for (v = p->values; v; v = v->next) {
740 if (! strcasecmp (p->param_name, "CN")) {
741 return v->value;
742 }
743 }
744 }
745
746 /* Did not find a CN parameter, so output the address. */
747 addr = node->value + 7;
748
749 /* Skip any leading whitespace. */
750 for ( ; isspace ((unsigned char) *addr); ++addr) { continue; }
751
752 return addr;
753 }
754
755 return "unknown";
756 }
757
758 static char *
759 format_params (char *line, param_list *p) {
760 for ( ; p && p->param_name; p = p->next) {
761 value_list *v;
762 size_t num_values = 0;
763
764 for (v = p->values; v; v = v->next) {
765 if (v->value) { ++num_values; }
766 }
767
768 if (num_values) {
769 size_t len = strlen (line);
770
771 line = mh_xrealloc (line, len + 2);
772 line[len] = ';';
773 line[len + 1] = '\0';
774
775 line = add (p->param_name, line);
776
777 for (v = p->values; v; v = v->next) {
778 len = strlen (line);
779 line = mh_xrealloc (line, len + 2);
780 line[len] = v == p->values ? '=' : ',';
781 line[len + 1] = '\0';
782
783 line = add (v->value, line);
784 }
785 }
786 }
787
788 return line;
789 }
790
791 static char *
792 fold (char *line, int uses_cr) {
793 size_t remaining = strlen (line);
794 size_t current_line_len = 0;
795 charstring_t folded_line = charstring_create (2 * remaining);
796 const char *cp = line;
797
798 #ifdef MULTIBYTE_SUPPORT
799 if (mbtowc (NULL, NULL, 0)) {} /* reset shift state */
800 #endif
801
802 while (*cp && remaining > 0) {
803 #ifdef MULTIBYTE_SUPPORT
804 int char_len = mbtowc (NULL, cp, (size_t) MB_CUR_MAX < remaining
805 ? (size_t) MB_CUR_MAX
806 : remaining);
807 if (char_len == -1) { char_len = 1; }
808 #else
809 const int char_len = 1;
810 #endif
811
812 charstring_push_back_chars (folded_line, cp, char_len, 1);
813 remaining -= max(char_len, 1);
814
815 /* remaining must be > 0 to pass the loop condition above, so
816 if it's not > 1, it is == 1. */
817 if (++current_line_len >= 75) {
818 if (remaining > 1 || (*(cp+1) != '\0' && *(cp+1) != '\r' &&
819 *(cp+1) != '\n')) {
820 /* fold */
821 if (uses_cr) { charstring_push_back (folded_line, '\r'); }
822 charstring_push_back (folded_line, '\n');
823 charstring_push_back (folded_line, ' ');
824 current_line_len = 0;
825 }
826 }
827
828 cp += max(char_len, 1);
829 }
830
831 free (line);
832 line = charstring_buffer_copy (folded_line);
833 charstring_free (folded_line);
834
835 return line;
836 }