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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 #include "mutt_curses.h"
33 #include "mutt_crypt.h"
44 #include <sys/types.h>
47 BODY *mutt_new_body (void)
49 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
51 p->disposition = DISPATTACH;
57 /* Modified by blong to accept a "suggestion" for file name. If
58 * that file exists, then construct one with unique name but
59 * keep any extension. This might fail, I guess.
60 * Renamed to mutt_adv_mktemp so I only have to change where it's
61 * called, and not all possible cases.
63 void mutt_adv_mktemp (char *s, size_t l)
65 char buf[_POSIX_PATH_MAX];
66 char tmp[_POSIX_PATH_MAX];
71 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
72 mutt_expand_path (buf, sizeof (buf));
75 snprintf (s, l, "%s/muttXXXXXX", buf);
80 strfcpy (tmp, s, sizeof (tmp));
81 mutt_sanitize_filename (tmp, 1);
82 snprintf (s, l, "%s/%s", buf, tmp);
83 if (lstat (s, &sb) == -1 && errno == ENOENT)
85 if ((period = strrchr (tmp, '.')) != NULL)
87 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
93 strfcpy(s + sl, period, l - sl);
98 /* create a send-mode duplicate from a receive-mode body */
100 int mutt_copy_body (FILE *fp, BODY **tgt, BODY *src)
102 char tmp[_POSIX_PATH_MAX];
105 PARAMETER *par, **ppar;
112 strfcpy (tmp, src->filename, sizeof (tmp));
120 mutt_adv_mktemp (tmp, sizeof (tmp));
121 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
124 *tgt = mutt_new_body ();
127 memcpy (b, src, sizeof (BODY));
131 b->filename = safe_strdup (tmp);
132 b->use_disp = use_disp;
135 if (mutt_is_text_part (b))
138 b->xtype = safe_strdup (b->xtype);
139 b->subtype = safe_strdup (b->subtype);
140 b->form_name = safe_strdup (b->form_name);
141 b->filename = safe_strdup (b->filename);
142 b->d_filename = safe_strdup (b->d_filename);
143 b->description = safe_strdup (b->description);
146 * we don't seem to need the HEADER structure currently.
147 * XXX - this may change in the future
150 if (b->hdr) b->hdr = NULL;
152 /* copy parameters */
153 for (par = b->parameter, ppar = &b->parameter; par; ppar = &(*ppar)->next, par = par->next)
155 *ppar = mutt_new_parameter ();
156 (*ppar)->attribute = safe_strdup (par->attribute);
157 (*ppar)->value = safe_strdup (par->value);
160 mutt_stamp_attachment (b);
167 void mutt_free_body (BODY **p)
177 mutt_free_parameter (&b->parameter);
178 if (b->unlink && b->filename)
180 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
181 unlink (b->filename);
183 else if (b->filename)
184 dprint (1, (debugfile, "mutt_free_body: Not unlinking %s.\n", b->filename));
190 FREE (&b->description);
191 FREE (&b->form_name);
195 /* Don't free twice (b->hdr->content = b->parts) */
196 b->hdr->content = NULL;
197 mutt_free_header(&b->hdr);
201 mutt_free_body (&b->parts);
209 void mutt_free_parameter (PARAMETER **p)
216 FREE (&t->attribute);
225 LIST *mutt_add_list (LIST *head, const char *data)
229 for (tmp = head; tmp && tmp->next; tmp = tmp->next)
233 tmp->next = safe_malloc (sizeof (LIST));
237 head = tmp = safe_malloc (sizeof (LIST));
239 tmp->data = safe_strdup (data);
244 void mutt_free_list (LIST **list)
252 *list = (*list)->next;
258 HEADER *mutt_dup_header(HEADER *h)
262 hnew = mutt_new_header();
263 memcpy(hnew, h, sizeof (HEADER));
267 void mutt_free_header (HEADER **h)
269 if(!h || !*h) return;
270 mutt_free_envelope (&(*h)->env);
271 mutt_free_body (&(*h)->content);
272 FREE (&(*h)->maildir_flags);
276 mutt_free_list (&(*h)->chain);
278 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
284 /* returns true if the header contained in "s" is in list "t" */
285 int mutt_matches_ignore (const char *s, LIST *t)
287 for (; t; t = t->next)
289 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data)) || *t->data == '*')
295 /* prepend the path part of *path to *link */
296 void mutt_expand_link (char *newpath, const char *path, const char *link)
298 const char *lb = NULL;
301 /* link is full path */
304 strfcpy (newpath, link, _POSIX_PATH_MAX);
308 if ((lb = strrchr (path, '/')) == NULL)
310 /* no path in link */
311 strfcpy (newpath, link, _POSIX_PATH_MAX);
316 memcpy (newpath, path, len);
317 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
320 char *mutt_expand_path (char *s, size_t slen)
322 return _mutt_expand_path (s, slen, 0);
325 char *_mutt_expand_path (char *s, size_t slen, int rx)
327 char p[_POSIX_PATH_MAX] = "";
328 char q[_POSIX_PATH_MAX] = "";
329 char tmp[_POSIX_PATH_MAX];
344 if (*(s + 1) == '/' || *(s + 1) == 0)
346 strfcpy (p, NONULL(Homedir), sizeof (p));
352 if ((t = strchr (s + 1, '/')))
355 if ((pw = getpwnam (s + 1)))
357 strfcpy (p, pw->pw_dir, sizeof (p));
368 /* user not found! */
382 /* if folder = {host} or imap[s]://host/: don't append slash */
383 if (mx_is_imap (NONULL (Maildir)) &&
384 (Maildir[strlen (Maildir) - 1] == '}' ||
385 Maildir[strlen (Maildir) - 1] == '/'))
386 strfcpy (p, NONULL (Maildir), sizeof (p));
389 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
395 /* elm compatibility, @ expands alias to user name */
402 if ((alias = mutt_lookup_alias (s + 1)))
404 h = mutt_new_header();
405 h->env = mutt_new_envelope();
406 h->env->from = h->env->to = alias;
407 mutt_default_save (p, sizeof (p), h);
408 h->env->from = h->env->to = NULL;
409 mutt_free_header (&h);
410 /* Avoid infinite recursion if the resulting folder starts with '@' */
421 strfcpy (p, NONULL(Inbox), sizeof (p));
428 strfcpy (p, NONULL(Outbox), sizeof (p));
437 strfcpy (p, NONULL(LastFolder), sizeof (p));
442 strfcpy (p, NONULL(Spoolfile), sizeof (p));
450 strfcpy (p, NONULL(LastFolder), sizeof (p));
462 if (rx && *p && !recurse)
464 mutt_rx_sanitize_string (q, sizeof (q), p);
465 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
468 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
470 strfcpy (s, tmp, slen);
475 /* Rewrite IMAP path in canonical form - aids in string comparisons of
476 * folders. May possibly fail, in which case s should be the same. */
478 imap_expand_path (s, slen);
484 /* Extract the real name from /etc/passwd's GECOS field.
485 * When set, honor the regular expression in GecosMask,
486 * otherwise assume that the GECOS field is a
487 * comma-separated list.
488 * Replace "&" by a capitalized version of the user's login
492 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
494 regmatch_t pat_match[1];
499 if (!pw || !pw->pw_gecos)
502 memset (dest, 0, destlen);
506 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
507 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
508 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
510 else if ((p = strchr (pw->pw_gecos, ',')))
511 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
513 strfcpy (dest, pw->pw_gecos, destlen);
515 pwnl = strlen (pw->pw_name);
517 for (idx = 0; dest[idx]; idx++)
519 if (dest[idx] == '&')
521 memmove (&dest[idx + pwnl], &dest[idx + 1],
522 MAX(destlen - idx - pwnl - 1, 0));
523 memcpy (&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl));
524 dest[idx] = toupper ((unsigned char) dest[idx]);
532 char *mutt_get_parameter (const char *s, PARAMETER *p)
534 for (; p; p = p->next)
535 if (ascii_strcasecmp (s, p->attribute) == 0)
541 void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p)
547 mutt_delete_parameter (attribute, p);
551 for(q = *p; q; q = q->next)
553 if (ascii_strcasecmp (attribute, q->attribute) == 0)
555 mutt_str_replace (&q->value, value);
560 q = mutt_new_parameter();
561 q->attribute = safe_strdup(attribute);
562 q->value = safe_strdup(value);
567 void mutt_delete_parameter (const char *attribute, PARAMETER **p)
571 for (q = *p; q; p = &q->next, q = q->next)
573 if (ascii_strcasecmp (attribute, q->attribute) == 0)
577 mutt_free_parameter (&q);
583 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
584 int mutt_needs_mailcap (BODY *m)
590 if (!ascii_strcasecmp ("plain", m->subtype) ||
591 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
592 !ascii_strcasecmp ("enriched", m->subtype))
596 case TYPEAPPLICATION:
597 if((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(m))
599 if((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(m))
611 int mutt_is_text_part (BODY *b)
614 char *s = b->subtype;
616 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
622 if (t == TYPEMESSAGE)
624 if (!ascii_strcasecmp ("delivery-status", s))
628 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION)
630 if (!ascii_strcasecmp ("pgp-keys", s))
637 void mutt_free_envelope (ENVELOPE **p)
640 rfc822_free_address (&(*p)->return_path);
641 rfc822_free_address (&(*p)->from);
642 rfc822_free_address (&(*p)->to);
643 rfc822_free_address (&(*p)->cc);
644 rfc822_free_address (&(*p)->bcc);
645 rfc822_free_address (&(*p)->sender);
646 rfc822_free_address (&(*p)->reply_to);
647 rfc822_free_address (&(*p)->mail_followup_to);
649 FREE (&(*p)->list_post);
650 FREE (&(*p)->subject);
651 /* real_subj is just an offset to subject and shouldn't be freed */
652 FREE (&(*p)->message_id);
653 FREE (&(*p)->supersedes);
655 FREE (&(*p)->x_label);
656 FREE (&(*p)->organization);
658 FREE (&(*p)->newsgroups);
660 FREE (&(*p)->followup_to);
661 FREE (&(*p)->x_comment_to);
664 mutt_buffer_free (&(*p)->spam);
665 mutt_free_list (&(*p)->references);
666 mutt_free_list (&(*p)->in_reply_to);
667 mutt_free_list (&(*p)->userhdrs);
671 void _mutt_mktemp (char *s, const char *src, int line)
673 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir), NONULL(Hostname), (int) getuid(), (int) getpid (), Counter++);
674 dprint (1, (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
678 void mutt_free_alias (ALIAS **p)
687 rfc822_free_address (&t->addr);
692 /* collapse the pathname using ~ or = when possible */
693 void mutt_pretty_mailbox (char *s)
699 scheme = url_check_scheme (s);
702 if (scheme == U_IMAP || scheme == U_IMAPS)
704 imap_pretty_mailbox (s);
709 /* if s is an url, only collapse path component */
710 if (scheme != U_UNKNOWN)
712 p = strchr(s, ':')+1;
713 if (!strncmp (p, "//", 2))
714 q = strchr (p+2, '/');
716 q = strchr (p, '\0');
720 /* first attempt to collapse the pathname */
723 if (*p == '/' && p[1] == '/')
728 else if (p[0] == '/' && p[1] == '.' && p[2] == '/')
738 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
742 memmove (s, s + len, mutt_strlen (s + len) + 1);
744 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
748 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
752 void mutt_pretty_size (char *s, size_t len, long n)
755 strfcpy (s, "0K", len);
756 else if (n < 10189) /* 0.1K - 9.9K */
757 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
758 else if (n < 1023949) /* 10K - 999K */
760 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
761 snprintf (s, len, "%ldK", (n + 51) / 1024);
763 else if (n < 10433332) /* 1.0M - 9.9M */
764 snprintf (s, len, "%3.1fM", n / 1048576.0);
767 /* (10433332 + 52428) / 1048576 = 10 */
768 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
772 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
774 char tmp[LONG_STRING];
776 mutt_quote_filename (tmp, sizeof (tmp), src);
777 mutt_expand_fmt (dest, destlen, fmt, tmp);
780 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
787 slen = mutt_strlen (src);
790 for (p = fmt, d = dest; destlen && *p; p++)
802 strfcpy (d, src, destlen + 1);
803 d += destlen > slen ? slen : destlen;
804 destlen -= destlen > slen ? slen : destlen;
822 if (!found && destlen > 0)
824 safe_strcat (dest, destlen, " ");
825 safe_strcat (dest, destlen, src);
830 /* return 0 on success, -1 on abort, 1 on error */
831 int mutt_check_overwrite (const char *attname, const char *path,
832 char *fname, size_t flen, int *append, char **directory)
835 char tmp[_POSIX_PATH_MAX];
838 strfcpy (fname, path, flen);
839 if (access (fname, F_OK) != 0)
841 if (stat (fname, &st) != 0)
843 if (S_ISDIR (st.st_mode))
847 switch (mutt_multi_choice
848 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"), _("yna")))
851 mutt_str_replace (directory, fname);
864 else if ((rc = mutt_yesorno (_("File is a directory, save under it?"), M_YES)) != M_YES)
865 return (rc == M_NO) ? 1 : -1;
867 if (!attname || !attname[0])
870 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
871 M_FILE | M_CLEAR) != 0 || !tmp[0])
873 mutt_concat_path (fname, path, tmp, flen);
876 mutt_concat_path (fname, path, mutt_basename (attname), flen);
879 if (*append == 0 && access (fname, F_OK) == 0)
881 switch (mutt_multi_choice
882 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
890 *append = M_SAVE_APPEND;
892 case 1: /* overwrite */
893 *append = M_SAVE_OVERWRITE;
900 void mutt_save_path (char *d, size_t dsize, ADDRESS *a)
904 strfcpy (d, a->mailbox, dsize);
905 if (!option (OPTSAVEADDRESS))
909 if ((p = strpbrk (d, "%@")))
918 void mutt_safe_path (char *s, size_t l, ADDRESS *a)
922 mutt_save_path (s, l, a);
924 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
929 void mutt_FormatString (char *dest, /* output buffer */
930 size_t destlen, /* output buffer len */
931 const char *src, /* template string */
932 format_t *callback, /* callback for processing */
933 unsigned long data, /* callback data */
934 format_flag flags) /* callback flags */
936 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
937 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
938 size_t wlen, count, len;
941 destlen--; /* save room for the terminal \0 */
942 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
944 while (*src && wlen < destlen)
958 flags |= M_FORMAT_OPTIONAL;
963 flags &= ~M_FORMAT_OPTIONAL;
965 /* eat the format string */
968 while (count < sizeof (prefix) &&
969 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
978 break; /* bad format */
980 ch = *src++; /* save the character to switch on */
982 if (flags & M_FORMAT_OPTIONAL)
985 break; /* bad format */
988 /* eat the `if' part of the string */
991 while (count < sizeof (ifstring) && *src && *src != '?' && *src != '&')
998 /* eat the `else' part of the string (optional) */
1000 src++; /* skip the & */
1003 while (count < sizeof (elsestring) && *src && *src != '?')
1011 break; /* bad format */
1013 src++; /* move past the trailing `?' */
1016 /* handle generic cases first */
1019 /* right justify to EOL */
1020 ch = *src++; /* pad char */
1021 /* calculate space left on line. if we've already written more data
1022 than will fit on the line, ignore the rest of the line */
1024 count = (COLS < destlen ? COLS : destlen);
1026 count = ((COLS-SidebarWidth) < destlen ? COLS - SidebarWidth : destlen);
1029 count -= wlen; /* how many chars left on this line */
1030 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1031 len = mutt_strlen (buf);
1034 count -= len; /* how many chars to pad */
1035 memset (wptr, ch, count);
1039 if (len + wlen > destlen)
1040 len = destlen - wlen;
1041 memcpy (wptr, buf, len);
1045 break; /* skip rest of input */
1055 count = destlen - wlen;
1056 memset (wptr, ch, count);
1059 break; /* skip rest of input */
1066 while (ch == '_' || ch == ':')
1076 /* use callback function to handle this case */
1077 src = callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring, data, flags);
1080 mutt_strlower (buf);
1089 if ((len = mutt_strlen (buf)) + wlen > destlen)
1090 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1092 memcpy (wptr, buf, len);
1097 else if (*src == '\\')
1135 if (flags & M_FORMAT_MAKEPRINT)
1137 /* Make sure that the string is printable by changing all non-printable
1138 chars to dots, or spaces for non-printable whitespace */
1139 for (cp = dest ; *cp ; cp++)
1140 if (!IsPrint (*cp) &&
1141 !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1142 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1147 /* This function allows the user to specify a command to read stdout from in
1148 place of a normal file. If the last character in the string is a pipe (|),
1149 then we assume it is a commmand to run instead of a normal file. */
1150 FILE *mutt_open_read (const char *path, pid_t *thepid)
1155 int len = mutt_strlen (path);
1157 if (path[len - 1] == '|')
1159 /* read from a pipe */
1161 char *s = safe_strdup (path);
1165 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1170 if (stat (path, &s) < 0)
1172 if (S_ISDIR (s.st_mode))
1177 f = fopen (path, "r");
1183 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1184 int mutt_save_confirm (const char *s, struct stat *st)
1186 char tmp[_POSIX_PATH_MAX];
1191 magic = mx_get_magic (s);
1196 mutt_error _("Can't save message to POP mailbox.");
1202 if (magic == M_NNTP)
1204 mutt_error _("Can't save message to newsserver.");
1209 if (stat (s, st) != -1)
1213 mutt_error (_("%s is not a mailbox!"), s);
1217 if (option (OPTCONFIRMAPPEND) &&
1218 (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
1219 /* if we're appending to the trash, there's no point in asking */
1221 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1222 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1231 if (magic != M_IMAP)
1232 #endif /* execute the block unconditionally if we don't use imap */
1237 if (errno == ENOENT)
1239 if (option (OPTCONFIRMCREATE))
1241 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1242 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1256 CLEARLINE (LINES-1);
1260 void state_prefix_putc (char c, STATE *s)
1262 if (s->flags & M_PENDINGPREFIX)
1266 i = strlen (Quotebuf);
1269 if (i == sizeof (Quotebuf) - 1 || c == '\n')
1271 char buf[2 * SHORT_STRING];
1272 int j = 0, offset = 0;
1273 regmatch_t pmatch[1];
1275 state_reset_prefix (s);
1276 while (regexec ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch, 0) == 0)
1277 offset += pmatch->rm_eo;
1279 if (!option (OPTQUOTEEMPTY) && Quotebuf[0] == '\n')
1280 strcpy (buf, Quotebuf);
1281 else if (option (OPTQUOTEQUOTED) && offset)
1283 for (i = 0; i < offset; i++)
1284 if (Quotebuf[i] != ' ')
1286 strncpy (buf, Quotebuf, j + 1);
1287 strcpy (buf + j + 1, Quotebuf + j);
1290 snprintf (buf, sizeof (buf), "%s%s", NONULL(s->prefix), Quotebuf);
1292 state_puts (buf, s);
1300 state_set_prefix (s);
1305 int state_printf (STATE *s, const char *fmt, ...)
1311 rv = vfprintf (s->fpout, fmt, ap);
1317 void state_mark_attach (STATE *s)
1319 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1320 state_puts (AttachmentMarker, s);
1323 void state_attach_puts (const char *t, STATE *s)
1325 if (*t != '\n') state_mark_attach (s);
1329 if (*t++ == '\n' && *t)
1330 if (*t != '\n') state_mark_attach (s);
1334 void mutt_display_sanitize (char *s)
1343 void mutt_sleep (short s)
1352 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1353 * just initializes. Frees anything already in the buffer.
1355 * Disregards the 'destroy' flag, which seems reserved for caller.
1356 * This is bad, but there's no apparent protocol for it.
1358 BUFFER * mutt_buffer_init(BUFFER *b)
1362 b = safe_malloc(sizeof(BUFFER));
1370 memset(b, 0, sizeof(BUFFER));
1375 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1376 * just initializes. Frees anything already in the buffer. Copies in
1379 * Disregards the 'destroy' flag, which seems reserved for caller.
1380 * This is bad, but there's no apparent protocol for it.
1382 BUFFER * mutt_buffer_from(BUFFER *b, char *seed)
1387 b = mutt_buffer_init(b);
1388 b->data = safe_strdup (seed);
1389 b->dsize = mutt_strlen (seed);
1390 b->dptr = (char *) b->data + b->dsize;
1394 void mutt_buffer_addstr (BUFFER* buf, const char* s)
1396 mutt_buffer_add (buf, s, mutt_strlen (s));
1399 void mutt_buffer_addch (BUFFER* buf, char c)
1401 mutt_buffer_add (buf, &c, 1);
1404 void mutt_buffer_free (BUFFER **p)
1410 /* dptr is just an offset to data and shouldn't be freed */
1414 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1415 * Always one byte bigger than necessary for the null terminator, and
1416 * the buffer is always null-terminated */
1417 void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
1421 if (buf->dptr + len + 1 > buf->data + buf->dsize)
1423 offset = buf->dptr - buf->data;
1424 buf->dsize += len < 128 ? 128 : len + 1;
1425 safe_realloc ((void**) &buf->data, buf->dsize);
1426 buf->dptr = buf->data + offset;
1428 memcpy (buf->dptr, s, len);
1430 *(buf->dptr) = '\0';
1433 /* Decrease a file's modification time by 1 second */
1435 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1437 struct utimbuf utim;
1443 if (stat (f, &_st) == -1)
1448 if ((mtime = st->st_mtime) == time (NULL))
1451 utim.actime = mtime;
1452 utim.modtime = mtime;
1459 const char *mutt_make_version (void)
1461 static char vstring[STRING];
1462 snprintf (vstring, sizeof (vstring), "Mutt %s (%s)",
1463 MUTT_VERSION, ReleaseDate);
1467 REGEXP *mutt_compile_regexp (const char *s, int flags)
1469 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1470 pp->pattern = safe_strdup (s);
1471 pp->rx = safe_calloc (sizeof (regex_t), 1);
1472 if (REGCOMP (pp->rx, NONULL(s), flags) != 0)
1473 mutt_free_regexp (&pp);
1478 void mutt_free_regexp (REGEXP **pp)
1480 FREE (&(*pp)->pattern);
1481 regfree ((*pp)->rx);
1486 void mutt_free_rx_list (RX_LIST **list)
1494 *list = (*list)->next;
1495 mutt_free_regexp (&p->rx);
1500 void mutt_free_spam_list (SPAM_LIST **list)
1508 *list = (*list)->next;
1509 mutt_free_regexp (&p->rx);
1510 safe_free(&p->template);
1515 int mutt_match_rx_list (const char *s, RX_LIST *l)
1519 for (; l; l = l->next)
1521 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0)
1523 dprint (5, (debugfile, "mutt_match_rx_list: %s matches %s\n", s, l->rx->pattern));
1531 int mutt_match_spam_list (const char *s, SPAM_LIST *l, char *text, int x)
1533 static regmatch_t *pmatch = NULL;
1534 static int nmatch = 0;
1542 for (; l; l = l->next)
1544 /* If this pattern needs more matches, expand pmatch. */
1545 if (l->nmatch > nmatch)
1547 safe_realloc (&pmatch, l->nmatch * sizeof(regmatch_t));
1551 /* Does this pattern match? */
1552 if (regexec (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch, (int) 0) == 0)
1554 dprint (5, (debugfile, "mutt_match_spam_list: %s matches %s\n", s, l->rx->pattern));
1555 dprint (5, (debugfile, "mutt_match_spam_list: %d subs\n", l->rx->rx->re_nsub));
1557 /* Copy template into text, with substitutions. */
1558 for (p = l->template; *p;)
1562 n = atoi(++p); /* find pmatch index */
1564 ++p; /* skip subst token */
1565 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1566 text[tlen++] = s[i];
1570 text[tlen++] = *p++;
1574 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));