2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_curses.h"
35 #include "mutt_crypt.h"
46 #include <sys/types.h>
49 BODY *mutt_new_body (void)
51 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
53 p->disposition = DISPATTACH;
59 /* Modified by blong to accept a "suggestion" for file name. If
60 * that file exists, then construct one with unique name but
61 * keep any extension. This might fail, I guess.
62 * Renamed to mutt_adv_mktemp so I only have to change where it's
63 * called, and not all possible cases.
65 void mutt_adv_mktemp (char *s, size_t l)
67 char buf[_POSIX_PATH_MAX];
68 char tmp[_POSIX_PATH_MAX];
73 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
74 mutt_expand_path (buf, sizeof (buf));
77 snprintf (s, l, "%s/muttXXXXXX", buf);
82 strfcpy (tmp, s, sizeof (tmp));
83 mutt_sanitize_filename (tmp, 1);
84 snprintf (s, l, "%s/%s", buf, tmp);
85 if (lstat (s, &sb) == -1 && errno == ENOENT)
87 if ((period = strrchr (tmp, '.')) != NULL)
89 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
95 strfcpy(s + sl, period, l - sl);
100 /* create a send-mode duplicate from a receive-mode body */
102 int mutt_copy_body (FILE *fp, BODY **tgt, BODY *src)
104 char tmp[_POSIX_PATH_MAX];
107 PARAMETER *par, **ppar;
114 strfcpy (tmp, src->filename, sizeof (tmp));
122 mutt_adv_mktemp (tmp, sizeof (tmp));
123 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
126 *tgt = mutt_new_body ();
129 memcpy (b, src, sizeof (BODY));
133 b->filename = safe_strdup (tmp);
134 b->use_disp = use_disp;
137 if (mutt_is_text_part (b))
140 b->xtype = safe_strdup (b->xtype);
141 b->subtype = safe_strdup (b->subtype);
142 b->form_name = safe_strdup (b->form_name);
143 b->filename = safe_strdup (b->filename);
144 b->d_filename = safe_strdup (b->d_filename);
145 b->description = safe_strdup (b->description);
148 * we don't seem to need the HEADER structure currently.
149 * XXX - this may change in the future
152 if (b->hdr) b->hdr = NULL;
154 /* copy parameters */
155 for (par = b->parameter, ppar = &b->parameter; par; ppar = &(*ppar)->next, par = par->next)
157 *ppar = mutt_new_parameter ();
158 (*ppar)->attribute = safe_strdup (par->attribute);
159 (*ppar)->value = safe_strdup (par->value);
162 mutt_stamp_attachment (b);
169 void mutt_free_body (BODY **p)
179 mutt_free_parameter (&b->parameter);
180 if (b->unlink && b->filename)
182 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
183 unlink (b->filename);
185 else if (b->filename)
186 dprint (1, (debugfile, "mutt_free_body: Not unlinking %s.\n", b->filename));
192 FREE (&b->description);
193 FREE (&b->form_name);
197 /* Don't free twice (b->hdr->content = b->parts) */
198 b->hdr->content = NULL;
199 mutt_free_header(&b->hdr);
203 mutt_free_body (&b->parts);
211 void mutt_free_parameter (PARAMETER **p)
218 FREE (&t->attribute);
227 LIST *mutt_add_list (LIST *head, const char *data)
229 size_t len = mutt_strlen (data);
231 return mutt_add_list_n (head, data, len ? len + 1 : 0);
234 LIST *mutt_add_list_n (LIST *head, const void *data, size_t len)
238 for (tmp = head; tmp && tmp->next; tmp = tmp->next)
242 tmp->next = safe_malloc (sizeof (LIST));
246 head = tmp = safe_malloc (sizeof (LIST));
248 tmp->data = safe_malloc (len);
250 memcpy (tmp->data, data, len);
255 void mutt_free_list (LIST **list)
263 *list = (*list)->next;
269 HEADER *mutt_dup_header(HEADER *h)
273 hnew = mutt_new_header();
274 memcpy(hnew, h, sizeof (HEADER));
278 void mutt_free_header (HEADER **h)
280 if(!h || !*h) return;
281 mutt_free_envelope (&(*h)->env);
282 mutt_free_body (&(*h)->content);
283 FREE (&(*h)->maildir_flags);
287 mutt_free_list (&(*h)->chain);
289 #if defined USE_POP || defined USE_IMAP
292 FREE (h); /* __FREE_CHECKED__ */
295 /* returns true if the header contained in "s" is in list "t" */
296 int mutt_matches_ignore (const char *s, LIST *t)
298 for (; t; t = t->next)
300 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data)) || *t->data == '*')
306 /* prepend the path part of *path to *link */
307 void mutt_expand_link (char *newpath, const char *path, const char *link)
309 const char *lb = NULL;
312 /* link is full path */
315 strfcpy (newpath, link, _POSIX_PATH_MAX);
319 if ((lb = strrchr (path, '/')) == NULL)
321 /* no path in link */
322 strfcpy (newpath, link, _POSIX_PATH_MAX);
327 memcpy (newpath, path, len);
328 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
331 char *mutt_expand_path (char *s, size_t slen)
333 return _mutt_expand_path (s, slen, 0);
336 char *_mutt_expand_path (char *s, size_t slen, int rx)
338 char p[_POSIX_PATH_MAX] = "";
339 char q[_POSIX_PATH_MAX] = "";
340 char tmp[_POSIX_PATH_MAX];
355 if (*(s + 1) == '/' || *(s + 1) == 0)
357 strfcpy (p, NONULL(Homedir), sizeof (p));
363 if ((t = strchr (s + 1, '/')))
366 if ((pw = getpwnam (s + 1)))
368 strfcpy (p, pw->pw_dir, sizeof (p));
379 /* user not found! */
393 /* if folder = {host} or imap[s]://host/: don't append slash */
394 if (mx_is_imap (NONULL (Maildir)) &&
395 (Maildir[strlen (Maildir) - 1] == '}' ||
396 Maildir[strlen (Maildir) - 1] == '/'))
397 strfcpy (p, NONULL (Maildir), sizeof (p));
400 if (Maildir && *Maildir && Maildir[strlen (Maildir) - 1] == '/')
401 strfcpy (p, NONULL (Maildir), sizeof (p));
403 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
409 /* elm compatibility, @ expands alias to user name */
416 if ((alias = mutt_lookup_alias (s + 1)))
418 h = mutt_new_header();
419 h->env = mutt_new_envelope();
420 h->env->from = h->env->to = alias;
421 mutt_default_save (p, sizeof (p), h);
422 h->env->from = h->env->to = NULL;
423 mutt_free_header (&h);
424 /* Avoid infinite recursion if the resulting folder starts with '@' */
435 strfcpy (p, NONULL(Inbox), sizeof (p));
442 strfcpy (p, NONULL(Outbox), sizeof (p));
451 strfcpy (p, NONULL(LastFolder), sizeof (p));
456 strfcpy (p, NONULL(Spoolfile), sizeof (p));
464 strfcpy (p, NONULL(LastFolder), sizeof (p));
471 strfcpy (p, NONULL(CurrentFolder), sizeof (p));
483 if (rx && *p && !recurse)
485 mutt_rx_sanitize_string (q, sizeof (q), p);
486 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
489 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
491 strfcpy (s, tmp, slen);
496 /* Rewrite IMAP path in canonical form - aids in string comparisons of
497 * folders. May possibly fail, in which case s should be the same. */
499 imap_expand_path (s, slen);
505 /* Extract the real name from /etc/passwd's GECOS field.
506 * When set, honor the regular expression in GecosMask,
507 * otherwise assume that the GECOS field is a
508 * comma-separated list.
509 * Replace "&" by a capitalized version of the user's login
513 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
515 regmatch_t pat_match[1];
520 if (!pw || !pw->pw_gecos)
523 memset (dest, 0, destlen);
527 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
528 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
529 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
531 else if ((p = strchr (pw->pw_gecos, ',')))
532 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
534 strfcpy (dest, pw->pw_gecos, destlen);
536 pwnl = strlen (pw->pw_name);
538 for (idx = 0; dest[idx]; idx++)
540 if (dest[idx] == '&')
542 memmove (&dest[idx + pwnl], &dest[idx + 1],
543 MAX((ssize_t)(destlen - idx - pwnl - 1), 0));
544 memcpy (&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl));
545 dest[idx] = toupper ((unsigned char) dest[idx]);
553 char *mutt_get_parameter (const char *s, PARAMETER *p)
555 for (; p; p = p->next)
556 if (ascii_strcasecmp (s, p->attribute) == 0)
562 void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p)
568 mutt_delete_parameter (attribute, p);
572 for(q = *p; q; q = q->next)
574 if (ascii_strcasecmp (attribute, q->attribute) == 0)
576 mutt_str_replace (&q->value, value);
581 q = mutt_new_parameter();
582 q->attribute = safe_strdup(attribute);
583 q->value = safe_strdup(value);
588 void mutt_delete_parameter (const char *attribute, PARAMETER **p)
592 for (q = *p; q; p = &q->next, q = q->next)
594 if (ascii_strcasecmp (attribute, q->attribute) == 0)
598 mutt_free_parameter (&q);
604 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
605 int mutt_needs_mailcap (BODY *m)
611 if (!ascii_strcasecmp ("plain", m->subtype) ||
612 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
613 !ascii_strcasecmp ("enriched", m->subtype))
617 case TYPEAPPLICATION:
618 if((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(m))
620 if((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(m))
632 int mutt_is_text_part (BODY *b)
635 char *s = b->subtype;
637 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
643 if (t == TYPEMESSAGE)
645 if (!ascii_strcasecmp ("delivery-status", s))
649 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION)
651 if (!ascii_strcasecmp ("pgp-keys", s))
658 void mutt_free_envelope (ENVELOPE **p)
661 rfc822_free_address (&(*p)->return_path);
662 rfc822_free_address (&(*p)->from);
663 rfc822_free_address (&(*p)->to);
664 rfc822_free_address (&(*p)->cc);
665 rfc822_free_address (&(*p)->bcc);
666 rfc822_free_address (&(*p)->sender);
667 rfc822_free_address (&(*p)->reply_to);
668 rfc822_free_address (&(*p)->mail_followup_to);
670 FREE (&(*p)->list_post);
671 FREE (&(*p)->subject);
672 /* real_subj is just an offset to subject and shouldn't be freed */
673 FREE (&(*p)->message_id);
674 FREE (&(*p)->supersedes);
676 FREE (&(*p)->x_label);
678 mutt_buffer_free (&(*p)->spam);
680 mutt_free_list (&(*p)->references);
681 mutt_free_list (&(*p)->in_reply_to);
682 mutt_free_list (&(*p)->userhdrs);
683 FREE (p); /* __FREE_CHECKED__ */
686 /* move all the headers from extra not present in base into base */
687 void mutt_merge_envelopes(ENVELOPE* base, ENVELOPE** extra)
689 /* copies each existing element if necessary, and sets the element
690 * to NULL in the source so that mutt_free_envelope doesn't leave us
691 * with dangling pointers. */
692 #define MOVE_ELEM(h) if (!base->h) { base->h = (*extra)->h; (*extra)->h = NULL; }
693 MOVE_ELEM(return_path);
700 MOVE_ELEM(mail_followup_to);
701 MOVE_ELEM(list_post);
702 MOVE_ELEM(message_id);
703 MOVE_ELEM(supersedes);
706 if (!base->refs_changed)
708 MOVE_ELEM(references);
710 if (!base->irt_changed)
712 MOVE_ELEM(in_reply_to);
715 /* real_subj is subordinate to subject */
718 base->subject = (*extra)->subject;
719 base->real_subj = (*extra)->real_subj;
720 (*extra)->subject = NULL;
721 (*extra)->real_subj = NULL;
723 /* spam and user headers should never be hashed, and the new envelope may
724 * have better values. Use new versions regardless. */
725 mutt_buffer_free (&base->spam);
726 mutt_free_list (&base->userhdrs);
731 mutt_free_envelope(extra);
734 void _mutt_mktemp (char *s, const char *src, int line)
736 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir), NONULL(Hostname), (int) getuid(), (int) getpid (), Counter++);
737 dprint (1, (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
741 void mutt_free_alias (ALIAS **p)
750 rfc822_free_address (&t->addr);
755 /* collapse the pathname using ~ or = when possible */
756 void mutt_pretty_mailbox (char *s)
762 scheme = url_check_scheme (s);
765 if (scheme == U_IMAP || scheme == U_IMAPS)
767 imap_pretty_mailbox (s);
772 /* if s is an url, only collapse path component */
773 if (scheme != U_UNKNOWN)
775 p = strchr(s, ':')+1;
776 if (!strncmp (p, "//", 2))
777 q = strchr (p+2, '/');
779 q = strchr (p, '\0');
783 /* first attempt to collapse the pathname */
786 if (*p == '/' && p[1] == '/')
791 else if (p[0] == '/' && p[1] == '.' && p[2] == '/')
801 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
805 memmove (s, s + len, mutt_strlen (s + len) + 1);
807 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
811 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
815 void mutt_pretty_size (char *s, size_t len, LOFF_T n)
818 strfcpy (s, "0K", len);
819 else if (n < 10189) /* 0.1K - 9.9K */
820 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
821 else if (n < 1023949) /* 10K - 999K */
823 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
824 snprintf (s, len, OFF_T_FMT "K", (n + 51) / 1024);
826 else if (n < 10433332) /* 1.0M - 9.9M */
827 snprintf (s, len, "%3.1fM", n / 1048576.0);
830 /* (10433332 + 52428) / 1048576 = 10 */
831 snprintf (s, len, OFF_T_FMT "M", (n + 52428) / 1048576);
835 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
837 char tmp[LONG_STRING];
839 mutt_quote_filename (tmp, sizeof (tmp), src);
840 mutt_expand_fmt (dest, destlen, fmt, tmp);
843 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
850 slen = mutt_strlen (src);
853 for (p = fmt, d = dest; destlen && *p; p++)
865 strfcpy (d, src, destlen + 1);
866 d += destlen > slen ? slen : destlen;
867 destlen -= destlen > slen ? slen : destlen;
885 if (!found && destlen > 0)
887 safe_strcat (dest, destlen, " ");
888 safe_strcat (dest, destlen, src);
893 /* return 0 on success, -1 on abort, 1 on error */
894 int mutt_check_overwrite (const char *attname, const char *path,
895 char *fname, size_t flen, int *append, char **directory)
898 char tmp[_POSIX_PATH_MAX];
901 strfcpy (fname, path, flen);
902 if (access (fname, F_OK) != 0)
904 if (stat (fname, &st) != 0)
906 if (S_ISDIR (st.st_mode))
910 switch (mutt_multi_choice
911 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"), _("yna")))
914 mutt_str_replace (directory, fname);
917 FREE (directory); /* __FREE_CHECKED__ */
920 FREE (directory); /* __FREE_CHECKED__ */
923 FREE (directory); /* __FREE_CHECKED__ */
927 else if ((rc = mutt_yesorno (_("File is a directory, save under it?"), M_YES)) != M_YES)
928 return (rc == M_NO) ? 1 : -1;
930 if (!attname || !attname[0])
933 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
934 M_FILE | M_CLEAR) != 0 || !tmp[0])
936 mutt_concat_path (fname, path, tmp, flen);
939 mutt_concat_path (fname, path, mutt_basename (attname), flen);
942 if (*append == 0 && access (fname, F_OK) == 0)
944 switch (mutt_multi_choice
945 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
953 *append = M_SAVE_APPEND;
955 case 1: /* overwrite */
956 *append = M_SAVE_OVERWRITE;
963 void mutt_save_path (char *d, size_t dsize, ADDRESS *a)
967 strfcpy (d, a->mailbox, dsize);
968 if (!option (OPTSAVEADDRESS))
972 if ((p = strpbrk (d, "%@")))
981 void mutt_safe_path (char *s, size_t l, ADDRESS *a)
985 mutt_save_path (s, l, a);
987 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
992 void mutt_FormatString (char *dest, /* output buffer */
993 size_t destlen, /* output buffer len */
994 size_t col, /* starting column (nonzero when called recursively) */
995 const char *src, /* template string */
996 format_t *callback, /* callback for processing */
997 unsigned long data, /* callback data */
998 format_flag flags) /* callback flags */
1000 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
1001 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
1002 size_t wlen, count, len, wid;
1009 destlen--; /* save room for the terminal \0 */
1010 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
1013 if ((flags & M_FORMAT_NOFILTER) == 0)
1017 /* Do not consider filters if no pipe at end */
1018 n = mutt_strlen(src);
1019 if (n > 1 && src[n-1] == '|')
1021 /* Scan backwards for backslashes */
1023 while (off > 0 && src[off-2] == '\\')
1027 /* If number of backslashes is even, the pipe is real. */
1028 /* n-off is the number of backslashes. */
1029 if (off > 0 && ((n-off) % 2) == 0)
1031 BUFFER *srcbuf, *word, *command;
1032 char srccopy[LONG_STRING];
1037 dprint(3, (debugfile, "fmtpipe = %s\n", src));
1039 strncpy(srccopy, src, n);
1040 srccopy[n-1] = '\0';
1042 /* prepare BUFFERs */
1043 srcbuf = mutt_buffer_from(NULL, srccopy);
1044 srcbuf->dptr = srcbuf->data;
1045 word = mutt_buffer_init(NULL);
1046 command = mutt_buffer_init(NULL);
1048 /* Iterate expansions across successive arguments */
1052 /* Extract the command name and copy to command line */
1053 dprint(3, (debugfile, "fmtpipe +++: %s\n", srcbuf->dptr));
1056 mutt_extract_token(word, srcbuf, 0);
1057 dprint(3, (debugfile, "fmtpipe %2d: %s\n", i++, word->data));
1058 mutt_buffer_addch(command, '\'');
1059 mutt_FormatString(buf, sizeof(buf), 0, word->data, callback, data,
1060 flags | M_FORMAT_NOFILTER);
1061 for (p = buf; p && *p; p++)
1064 /* shell quoting doesn't permit escaping a single quote within
1065 * single-quoted material. double-quoting instead will lead
1066 * shell variable expansions, so break out of the single-quoted
1067 * span, insert a double-quoted single quote, and resume. */
1068 mutt_buffer_addstr(command, "'\"'\"'");
1070 mutt_buffer_addch(command, *p);
1072 mutt_buffer_addch(command, '\'');
1073 mutt_buffer_addch(command, ' ');
1074 } while (MoreArgs(srcbuf));
1076 dprint(3, (debugfile, "fmtpipe > %s\n", command->data));
1078 col -= wlen; /* reset to passed in value */
1079 wptr = dest; /* reset write ptr */
1080 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
1081 if ((pid = mutt_create_filter(command->data, NULL, &filter, NULL)))
1083 n = fread(dest, 1, destlen /* already decremented */, filter);
1086 while (dest[n-1] == '\n' || dest[n-1] == '\r')
1088 dprint(3, (debugfile, "fmtpipe < %s\n", dest));
1091 mutt_wait_filter(pid);
1093 /* If the result ends with '%', this indicates that the filter
1094 * generated %-tokens that mutt can expand. Eliminate the '%'
1095 * marker and recycle the string through mutt_FormatString().
1096 * To literally end with "%", use "%%". */
1097 if (dest[--n] == '%')
1099 dest[n] = '\0'; /* remove '%' */
1100 if (dest[--n] != '%')
1102 recycler = safe_strdup(dest);
1105 mutt_FormatString(dest, destlen++, col, recycler, callback, data, flags);
1113 /* Filter failed; erase write buffer */
1117 mutt_buffer_free(&command);
1118 mutt_buffer_free(&srcbuf);
1119 mutt_buffer_free(&word);
1124 while (*src && wlen < destlen)
1139 flags |= M_FORMAT_OPTIONAL;
1144 flags &= ~M_FORMAT_OPTIONAL;
1146 /* eat the format string */
1149 while (count < sizeof (prefix) &&
1150 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-' || *src == '='))
1159 break; /* bad format */
1161 ch = *src++; /* save the character to switch on */
1163 if (flags & M_FORMAT_OPTIONAL)
1166 break; /* bad format */
1169 /* eat the `if' part of the string */
1172 while (count < sizeof (ifstring) && *src && *src != '?' && *src != '&')
1179 /* eat the `else' part of the string (optional) */
1181 src++; /* skip the & */
1184 while (count < sizeof (elsestring) && *src && *src != '?')
1192 break; /* bad format */
1194 src++; /* move past the trailing `?' */
1197 /* handle generic cases first */
1198 if (ch == '>' || ch == '*')
1200 /* %>X: right justify to EOL, left takes precedence
1201 * %*X: right justify to EOL, right takes precedence */
1202 int soft = ch == '*';
1204 if ((pl = mutt_charlen (src, &pw)) <= 0)
1207 /* see if there's room to add content, else ignore */
1208 if ((col < COLS && wlen < destlen) || soft)
1212 /* get contents after padding */
1213 mutt_FormatString (buf, sizeof (buf), 0, src + pl, callback, data, flags);
1214 len = mutt_strlen (buf);
1215 wid = mutt_strwidth (buf);
1217 /* try to consume as many columns as we can, if we don't have
1218 * memory for that, use as much memory as possible */
1219 pad = (COLS - col - wid) / pw;
1220 if (pad > 0 && wlen + (pad * pl) + len > destlen)
1221 pad = ((signed)(destlen - wlen - len)) / pl;
1226 memcpy (wptr, src, pl);
1232 else if (soft && pad < 0)
1234 /* \0-terminate dest for length computation in mutt_wstr_trunc() */
1236 /* make sure right part is at most as wide as display */
1237 len = mutt_wstr_trunc (buf, destlen, COLS, &wid);
1238 /* truncate left so that right part fits completely in */
1239 wlen = mutt_wstr_trunc (dest, destlen - len, col + pad, &col);
1242 if (len + wlen > destlen)
1243 len = mutt_wstr_trunc (buf, destlen - wlen, COLS - col, NULL);
1244 memcpy (wptr, buf, len);
1250 break; /* skip rest of input */
1256 if ((pl = mutt_charlen (src, &pw)) <= 0)
1259 /* see if there's room to add content, else ignore */
1260 if (col < COLS && wlen < destlen)
1262 c = (COLS - col) / pw;
1263 if (c > 0 && wlen + (c * pl) > destlen)
1264 c = ((signed)(destlen - wlen)) / pl;
1267 memcpy (wptr, src, pl);
1275 break; /* skip rest of input */
1282 while (ch == '_' || ch == ':')
1292 /* use callback function to handle this case */
1293 src = callback (buf, sizeof (buf), col, ch, src, prefix, ifstring, elsestring, data, flags);
1296 mutt_strlower (buf);
1305 if ((len = mutt_strlen (buf)) + wlen > destlen)
1306 len = mutt_wstr_trunc (buf, destlen - wlen, COLS - col, NULL);
1308 memcpy (wptr, buf, len);
1311 col += mutt_strwidth (buf);
1314 else if (*src == '\\')
1347 /* in case of error, simply copy byte */
1348 if ((tmp = mutt_charlen (src, &w)) < 0)
1350 if (tmp > 0 && wlen + tmp < destlen)
1352 memcpy (wptr, src, tmp);
1360 src += destlen - wlen;
1368 if (flags & M_FORMAT_MAKEPRINT)
1370 /* Make sure that the string is printable by changing all non-printable
1371 chars to dots, or spaces for non-printable whitespace */
1372 for (cp = dest ; *cp ; cp++)
1373 if (!IsPrint (*cp) &&
1374 !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1375 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1380 /* This function allows the user to specify a command to read stdout from in
1381 place of a normal file. If the last character in the string is a pipe (|),
1382 then we assume it is a commmand to run instead of a normal file. */
1383 FILE *mutt_open_read (const char *path, pid_t *thepid)
1388 int len = mutt_strlen (path);
1390 if (path[len - 1] == '|')
1392 /* read from a pipe */
1394 char *s = safe_strdup (path);
1398 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1403 if (stat (path, &s) < 0)
1405 if (S_ISDIR (s.st_mode))
1410 f = fopen (path, "r");
1416 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1417 int mutt_save_confirm (const char *s, struct stat *st)
1419 char tmp[_POSIX_PATH_MAX];
1424 magic = mx_get_magic (s);
1429 mutt_error _("Can't save message to POP mailbox.");
1434 if (magic > 0 && !mx_access (s, W_OK))
1436 if (option (OPTCONFIRMAPPEND))
1438 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1439 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1446 if (stat (s, st) != -1)
1450 mutt_error (_("%s is not a mailbox!"), s);
1456 if (magic != M_IMAP)
1457 #endif /* execute the block unconditionally if we don't use imap */
1462 if (errno == ENOENT)
1464 if (option (OPTCONFIRMCREATE))
1466 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1467 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1480 CLEARLINE (LINES-1);
1484 void state_prefix_putc (char c, STATE *s)
1486 if (s->flags & M_PENDINGPREFIX)
1488 state_reset_prefix (s);
1490 state_puts (s->prefix, s);
1496 state_set_prefix (s);
1499 int state_printf (STATE *s, const char *fmt, ...)
1505 rv = vfprintf (s->fpout, fmt, ap);
1511 void state_mark_attach (STATE *s)
1513 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1514 state_puts (AttachmentMarker, s);
1517 void state_attach_puts (const char *t, STATE *s)
1519 if (*t != '\n') state_mark_attach (s);
1523 if (*t++ == '\n' && *t)
1524 if (*t != '\n') state_mark_attach (s);
1528 void mutt_display_sanitize (char *s)
1537 void mutt_sleep (short s)
1546 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1547 * just initializes. Frees anything already in the buffer.
1549 * Disregards the 'destroy' flag, which seems reserved for caller.
1550 * This is bad, but there's no apparent protocol for it.
1552 BUFFER * mutt_buffer_init(BUFFER *b)
1556 b = safe_malloc(sizeof(BUFFER));
1564 memset(b, 0, sizeof(BUFFER));
1569 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1570 * just initializes. Frees anything already in the buffer. Copies in
1573 * Disregards the 'destroy' flag, which seems reserved for caller.
1574 * This is bad, but there's no apparent protocol for it.
1576 BUFFER * mutt_buffer_from(BUFFER *b, char *seed)
1581 b = mutt_buffer_init(b);
1582 b->data = safe_strdup (seed);
1583 b->dsize = mutt_strlen (seed);
1584 b->dptr = (char *) b->data + b->dsize;
1588 int mutt_buffer_printf (BUFFER* buf, const char* fmt, ...)
1590 va_list ap, ap_retry;
1591 int len, blen, doff;
1594 va_copy (ap_retry, ap);
1596 doff = buf->dptr - buf->data;
1597 blen = buf->dsize - doff;
1598 /* solaris 9 vsnprintf barfs when blen is 0 */
1603 safe_realloc (&buf->data, buf->dsize);
1604 buf->dptr = buf->data + doff;
1606 if ((len = vsnprintf (buf->dptr, blen, fmt, ap)) >= blen)
1608 blen = ++len - blen;
1612 safe_realloc (&buf->data, buf->dsize);
1613 buf->dptr = buf->data + doff;
1614 len = vsnprintf (buf->dptr, len, fmt, ap_retry);
1625 void mutt_buffer_addstr (BUFFER* buf, const char* s)
1627 mutt_buffer_add (buf, s, mutt_strlen (s));
1630 void mutt_buffer_addch (BUFFER* buf, char c)
1632 mutt_buffer_add (buf, &c, 1);
1635 void mutt_buffer_free (BUFFER **p)
1641 /* dptr is just an offset to data and shouldn't be freed */
1642 FREE(p); /* __FREE_CHECKED__ */
1645 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1646 * Always one byte bigger than necessary for the null terminator, and
1647 * the buffer is always null-terminated */
1648 void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
1652 if (buf->dptr + len + 1 > buf->data + buf->dsize)
1654 offset = buf->dptr - buf->data;
1655 buf->dsize += len < 128 ? 128 : len + 1;
1656 /* suppress compiler aliasing warning */
1657 safe_realloc ((void**) (void*) &buf->data, buf->dsize);
1658 buf->dptr = buf->data + offset;
1660 memcpy (buf->dptr, s, len);
1662 *(buf->dptr) = '\0';
1665 /* Decrease a file's modification time by 1 second */
1667 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1669 struct utimbuf utim;
1675 if (stat (f, &_st) == -1)
1680 if ((mtime = st->st_mtime) == time (NULL))
1683 utim.actime = mtime;
1684 utim.modtime = mtime;
1691 /* sets mtime of 'to' to mtime of 'from' */
1692 void mutt_set_mtime (const char* from, const char* to)
1694 struct utimbuf utim;
1697 if (stat (from, &st) != -1)
1699 utim.actime = st.st_mtime;
1700 utim.modtime = st.st_mtime;
1705 const char *mutt_make_version (void)
1707 static char vstring[STRING];
1708 snprintf (vstring, sizeof (vstring), "Mutt %s (%s)",
1709 MUTT_VERSION, ReleaseDate);
1713 REGEXP *mutt_compile_regexp (const char *s, int flags)
1715 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1716 pp->pattern = safe_strdup (s);
1717 pp->rx = safe_calloc (sizeof (regex_t), 1);
1718 if (REGCOMP (pp->rx, NONULL(s), flags) != 0)
1719 mutt_free_regexp (&pp);
1724 void mutt_free_regexp (REGEXP **pp)
1726 FREE (&(*pp)->pattern);
1727 regfree ((*pp)->rx);
1729 FREE (pp); /* __FREE_CHECKED__ */
1732 void mutt_free_rx_list (RX_LIST **list)
1740 *list = (*list)->next;
1741 mutt_free_regexp (&p->rx);
1746 void mutt_free_spam_list (SPAM_LIST **list)
1754 *list = (*list)->next;
1755 mutt_free_regexp (&p->rx);
1756 FREE (&p->template);
1761 int mutt_match_rx_list (const char *s, RX_LIST *l)
1765 for (; l; l = l->next)
1767 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0)
1769 dprint (5, (debugfile, "mutt_match_rx_list: %s matches %s\n", s, l->rx->pattern));
1777 int mutt_match_spam_list (const char *s, SPAM_LIST *l, char *text, int x)
1779 static regmatch_t *pmatch = NULL;
1780 static int nmatch = 0;
1788 for (; l; l = l->next)
1790 /* If this pattern needs more matches, expand pmatch. */
1791 if (l->nmatch > nmatch)
1793 safe_realloc (&pmatch, l->nmatch * sizeof(regmatch_t));
1797 /* Does this pattern match? */
1798 if (regexec (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch, (int) 0) == 0)
1800 dprint (5, (debugfile, "mutt_match_spam_list: %s matches %s\n", s, l->rx->pattern));
1801 dprint (5, (debugfile, "mutt_match_spam_list: %d subs\n", (int)l->rx->rx->re_nsub));
1803 /* Copy template into text, with substitutions. */
1804 for (p = l->template; *p;)
1808 n = atoi(++p); /* find pmatch index */
1809 while (isdigit((unsigned char)*p))
1810 ++p; /* skip subst token */
1811 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1812 text[tlen++] = s[i];
1816 text[tlen++] = *p++;
1820 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));