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.
25 #include "mutt_curses.h"
37 #include "mutt_crypt.h"
48 #include <sys/types.h>
51 BODY *mutt_new_body (void)
53 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
55 p->disposition = DISPATTACH;
61 /* Modified by blong to accept a "suggestion" for file name. If
62 * that file exists, then construct one with unique name but
63 * keep any extension. This might fail, I guess.
64 * Renamed to mutt_adv_mktemp so I only have to change where it's
65 * called, and not all possible cases.
67 void mutt_adv_mktemp (char *s, size_t l)
69 char buf[_POSIX_PATH_MAX];
70 char tmp[_POSIX_PATH_MAX];
75 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
76 mutt_expand_path (buf, sizeof (buf));
79 snprintf (s, l, "%s/muttXXXXXX", buf);
84 strfcpy (tmp, s, sizeof (tmp));
85 mutt_sanitize_filename (tmp, 1);
86 snprintf (s, l, "%s/%s", buf, tmp);
87 if (lstat (s, &sb) == -1 && errno == ENOENT)
89 if ((period = strrchr (tmp, '.')) != NULL)
91 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
97 strfcpy(s + sl, period, l - sl);
102 /* create a send-mode duplicate from a receive-mode body */
104 int mutt_copy_body (FILE *fp, BODY **tgt, BODY *src)
106 char tmp[_POSIX_PATH_MAX];
109 PARAMETER *par, **ppar;
116 strfcpy (tmp, src->filename, sizeof (tmp));
124 mutt_adv_mktemp (tmp, sizeof (tmp));
125 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
128 *tgt = mutt_new_body ();
131 memcpy (b, src, sizeof (BODY));
135 b->filename = safe_strdup (tmp);
136 b->use_disp = use_disp;
139 if (mutt_is_text_part (b))
142 b->xtype = safe_strdup (b->xtype);
143 b->subtype = safe_strdup (b->subtype);
144 b->form_name = safe_strdup (b->form_name);
145 b->filename = safe_strdup (b->filename);
146 b->d_filename = safe_strdup (b->d_filename);
147 b->description = safe_strdup (b->description);
150 * we don't seem to need the HEADER structure currently.
151 * XXX - this may change in the future
154 if (b->hdr) b->hdr = NULL;
156 /* copy parameters */
157 for (par = b->parameter, ppar = &b->parameter; par; ppar = &(*ppar)->next, par = par->next)
159 *ppar = mutt_new_parameter ();
160 (*ppar)->attribute = safe_strdup (par->attribute);
161 (*ppar)->value = safe_strdup (par->value);
164 mutt_stamp_attachment (b);
171 void mutt_free_body (BODY **p)
181 mutt_free_parameter (&b->parameter);
182 if (b->unlink && b->filename)
184 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
185 unlink (b->filename);
187 else if (b->filename)
188 dprint (1, (debugfile, "mutt_free_body: Not unlinking %s.\n", b->filename));
194 FREE (&b->description);
195 FREE (&b->form_name);
199 /* Don't free twice (b->hdr->content = b->parts) */
200 b->hdr->content = NULL;
201 mutt_free_header(&b->hdr);
205 mutt_free_body (&b->parts);
213 void mutt_free_parameter (PARAMETER **p)
220 FREE (&t->attribute);
229 LIST *mutt_add_list (LIST *head, const char *data)
233 for (tmp = head; tmp && tmp->next; tmp = tmp->next)
237 tmp->next = safe_malloc (sizeof (LIST));
241 head = tmp = safe_malloc (sizeof (LIST));
243 tmp->data = safe_strdup (data);
248 void mutt_free_list (LIST **list)
256 *list = (*list)->next;
262 HEADER *mutt_dup_header(HEADER *h)
266 hnew = mutt_new_header();
267 memcpy(hnew, h, sizeof (HEADER));
271 void mutt_free_header (HEADER **h)
273 if(!h || !*h) return;
274 mutt_free_envelope (&(*h)->env);
275 mutt_free_body (&(*h)->content);
276 FREE (&(*h)->maildir_flags);
280 mutt_free_list (&(*h)->chain);
282 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
288 /* returns true if the header contained in "s" is in list "t" */
289 int mutt_matches_ignore (const char *s, LIST *t)
291 for (; t; t = t->next)
293 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data)) || *t->data == '*')
299 /* prepend the path part of *path to *link */
300 void mutt_expand_link (char *newpath, const char *path, const char *link)
302 const char *lb = NULL;
305 /* link is full path */
308 strfcpy (newpath, link, _POSIX_PATH_MAX);
312 if ((lb = strrchr (path, '/')) == NULL)
314 /* no path in link */
315 strfcpy (newpath, link, _POSIX_PATH_MAX);
320 memcpy (newpath, path, len);
321 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
324 char *mutt_expand_path (char *s, size_t slen)
326 return _mutt_expand_path (s, slen, 0);
329 char *_mutt_expand_path (char *s, size_t slen, int rx)
331 char p[_POSIX_PATH_MAX] = "";
332 char q[_POSIX_PATH_MAX] = "";
333 char tmp[_POSIX_PATH_MAX];
348 if (*(s + 1) == '/' || *(s + 1) == 0)
350 strfcpy (p, NONULL(Homedir), sizeof (p));
356 if ((t = strchr (s + 1, '/')))
359 if ((pw = getpwnam (s + 1)))
361 strfcpy (p, pw->pw_dir, sizeof (p));
372 /* user not found! */
386 /* if folder = {host} or imap[s]://host/: don't append slash */
387 if (mx_is_imap (NONULL (Maildir)) &&
388 (Maildir[strlen (Maildir) - 1] == '}' ||
389 Maildir[strlen (Maildir) - 1] == '/'))
390 strfcpy (p, NONULL (Maildir), sizeof (p));
393 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
399 /* elm compatibility, @ expands alias to user name */
406 if ((alias = mutt_lookup_alias (s + 1)))
408 h = mutt_new_header();
409 h->env = mutt_new_envelope();
410 h->env->from = h->env->to = alias;
411 mutt_default_save (p, sizeof (p), h);
412 h->env->from = h->env->to = NULL;
413 mutt_free_header (&h);
414 /* Avoid infinite recursion if the resulting folder starts with '@' */
425 strfcpy (p, NONULL(Inbox), sizeof (p));
432 strfcpy (p, NONULL(Outbox), sizeof (p));
441 strfcpy (p, NONULL(LastFolder), sizeof (p));
446 strfcpy (p, NONULL(Spoolfile), sizeof (p));
454 strfcpy (p, NONULL(LastFolder), sizeof (p));
466 if (rx && *p && !recurse)
468 mutt_rx_sanitize_string (q, sizeof (q), p);
469 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
472 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
474 strfcpy (s, tmp, slen);
479 /* Rewrite IMAP path in canonical form - aids in string comparisons of
480 * folders. May possibly fail, in which case s should be the same. */
482 imap_expand_path (s, slen);
488 /* Extract the real name from /etc/passwd's GECOS field.
489 * When set, honor the regular expression in GecosMask,
490 * otherwise assume that the GECOS field is a
491 * comma-separated list.
492 * Replace "&" by a capitalized version of the user's login
496 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
498 regmatch_t pat_match[1];
503 if (!pw || !pw->pw_gecos)
506 memset (dest, 0, destlen);
510 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
511 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
512 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
514 else if ((p = strchr (pw->pw_gecos, ',')))
515 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
517 strfcpy (dest, pw->pw_gecos, destlen);
519 pwnl = strlen (pw->pw_name);
521 for (idx = 0; dest[idx]; idx++)
523 if (dest[idx] == '&')
525 memmove (&dest[idx + pwnl], &dest[idx + 1],
526 MAX(destlen - idx - pwnl - 1, 0));
527 memcpy (&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl));
528 dest[idx] = toupper ((unsigned char) dest[idx]);
536 char *mutt_get_parameter (const char *s, PARAMETER *p)
538 for (; p; p = p->next)
539 if (ascii_strcasecmp (s, p->attribute) == 0)
545 void mutt_set_parameter (const char *attribute, const char *value, PARAMETER **p)
551 mutt_delete_parameter (attribute, p);
555 for(q = *p; q; q = q->next)
557 if (ascii_strcasecmp (attribute, q->attribute) == 0)
559 mutt_str_replace (&q->value, value);
564 q = mutt_new_parameter();
565 q->attribute = safe_strdup(attribute);
566 q->value = safe_strdup(value);
571 void mutt_delete_parameter (const char *attribute, PARAMETER **p)
575 for (q = *p; q; p = &q->next, q = q->next)
577 if (ascii_strcasecmp (attribute, q->attribute) == 0)
581 mutt_free_parameter (&q);
587 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
588 int mutt_needs_mailcap (BODY *m)
594 if (!ascii_strcasecmp ("plain", m->subtype) ||
595 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
596 !ascii_strcasecmp ("enriched", m->subtype))
600 case TYPEAPPLICATION:
601 if((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(m))
603 if((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(m))
615 int mutt_is_text_part (BODY *b)
618 char *s = b->subtype;
620 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
626 if (t == TYPEMESSAGE)
628 if (!ascii_strcasecmp ("delivery-status", s))
632 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION)
634 if (!ascii_strcasecmp ("pgp-keys", s))
641 void mutt_free_envelope (ENVELOPE **p)
644 rfc822_free_address (&(*p)->return_path);
645 rfc822_free_address (&(*p)->from);
646 rfc822_free_address (&(*p)->to);
647 rfc822_free_address (&(*p)->cc);
648 rfc822_free_address (&(*p)->bcc);
649 rfc822_free_address (&(*p)->sender);
650 rfc822_free_address (&(*p)->reply_to);
651 rfc822_free_address (&(*p)->mail_followup_to);
653 FREE (&(*p)->list_post);
654 FREE (&(*p)->subject);
655 /* real_subj is just an offset to subject and shouldn't be freed */
656 FREE (&(*p)->message_id);
657 FREE (&(*p)->supersedes);
659 FREE (&(*p)->x_label);
660 FREE (&(*p)->organization);
662 FREE (&(*p)->newsgroups);
664 FREE (&(*p)->followup_to);
665 FREE (&(*p)->x_comment_to);
668 mutt_buffer_free (&(*p)->spam);
669 mutt_free_list (&(*p)->references);
670 mutt_free_list (&(*p)->in_reply_to);
671 mutt_free_list (&(*p)->userhdrs);
675 void _mutt_mktemp (char *s, const char *src, int line)
677 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir), NONULL(Hostname), (int) getuid(), (int) getpid (), Counter++);
678 dprint (1, (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
682 void mutt_free_alias (ALIAS **p)
691 rfc822_free_address (&t->addr);
696 /* collapse the pathname using ~ or = when possible */
697 void mutt_pretty_mailbox (char *s)
703 scheme = url_check_scheme (s);
706 if (scheme == U_IMAP || scheme == U_IMAPS)
708 imap_pretty_mailbox (s);
713 /* if s is an url, only collapse path component */
714 if (scheme != U_UNKNOWN)
716 p = strchr(s, ':')+1;
717 if (!strncmp (p, "//", 2))
718 q = strchr (p+2, '/');
720 q = strchr (p, '\0');
724 /* first attempt to collapse the pathname */
727 if (*p == '/' && p[1] == '/')
732 else if (p[0] == '/' && p[1] == '.' && p[2] == '/')
742 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
746 memmove (s, s + len, mutt_strlen (s + len) + 1);
748 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
752 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
756 void mutt_pretty_size (char *s, size_t len, long n)
759 strfcpy (s, "0K", len);
760 else if (n < 10189) /* 0.1K - 9.9K */
761 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
762 else if (n < 1023949) /* 10K - 999K */
764 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
765 snprintf (s, len, "%ldK", (n + 51) / 1024);
767 else if (n < 10433332) /* 1.0M - 9.9M */
768 snprintf (s, len, "%3.1fM", n / 1048576.0);
771 /* (10433332 + 52428) / 1048576 = 10 */
772 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
776 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
778 char tmp[LONG_STRING];
780 mutt_quote_filename (tmp, sizeof (tmp), src);
781 mutt_expand_fmt (dest, destlen, fmt, tmp);
784 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt, const char *src)
791 slen = mutt_strlen (src);
794 for (p = fmt, d = dest; destlen && *p; p++)
806 strfcpy (d, src, destlen + 1);
807 d += destlen > slen ? slen : destlen;
808 destlen -= destlen > slen ? slen : destlen;
826 if (!found && destlen > 0)
828 safe_strcat (dest, destlen, " ");
829 safe_strcat (dest, destlen, src);
834 /* return 0 on success, -1 on abort, 1 on error */
835 int mutt_check_overwrite (const char *attname, const char *path,
836 char *fname, size_t flen, int *append, char **directory)
839 char tmp[_POSIX_PATH_MAX];
842 strfcpy (fname, path, flen);
843 if (access (fname, F_OK) != 0)
845 if (stat (fname, &st) != 0)
847 if (S_ISDIR (st.st_mode))
851 switch (mutt_multi_choice
852 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"), _("yna")))
855 mutt_str_replace (directory, fname);
868 else if ((rc = mutt_yesorno (_("File is a directory, save under it?"), M_YES)) != M_YES)
869 return (rc == M_NO) ? 1 : -1;
871 if (!attname || !attname[0])
874 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
875 M_FILE | M_CLEAR) != 0 || !tmp[0])
877 mutt_concat_path (fname, path, tmp, flen);
880 mutt_concat_path (fname, path, mutt_basename (attname), flen);
883 if (*append == 0 && access (fname, F_OK) == 0)
885 switch (mutt_multi_choice
886 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
894 *append = M_SAVE_APPEND;
896 case 1: /* overwrite */
897 *append = M_SAVE_OVERWRITE;
904 void mutt_save_path (char *d, size_t dsize, ADDRESS *a)
908 strfcpy (d, a->mailbox, dsize);
909 if (!option (OPTSAVEADDRESS))
913 if ((p = strpbrk (d, "%@")))
922 void mutt_safe_path (char *s, size_t l, ADDRESS *a)
926 mutt_save_path (s, l, a);
928 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
933 void mutt_FormatString (char *dest, /* output buffer */
934 size_t destlen, /* output buffer len */
935 const char *src, /* template string */
936 format_t *callback, /* callback for processing */
937 unsigned long data, /* callback data */
938 format_flag flags) /* callback flags */
940 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
941 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
942 size_t wlen, count, len, col, wid;
945 destlen--; /* save room for the terminal \0 */
946 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
949 while (*src && wlen < destlen)
964 flags |= M_FORMAT_OPTIONAL;
969 flags &= ~M_FORMAT_OPTIONAL;
971 /* eat the format string */
974 while (count < sizeof (prefix) &&
975 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
984 break; /* bad format */
986 ch = *src++; /* save the character to switch on */
988 if (flags & M_FORMAT_OPTIONAL)
991 break; /* bad format */
994 /* eat the `if' part of the string */
997 while (count < sizeof (ifstring) && *src && *src != '?' && *src != '&')
1004 /* eat the `else' part of the string (optional) */
1006 src++; /* skip the & */
1009 while (count < sizeof (elsestring) && *src && *src != '?')
1017 break; /* bad format */
1019 src++; /* move past the trailing `?' */
1022 /* handle generic cases first */
1025 /* right justify to EOL */
1026 ch = *src++; /* pad char */
1027 /* calculate space left on line. if we've already written more data
1028 than will fit on the line, ignore the rest of the line */
1029 if ( DrawFullLine || option(OPTSTATUSONTOP))
1030 count = (COLS < destlen ? COLS : destlen);
1032 count = ((COLS-SidebarWidth) < destlen ? (COLS - SidebarWidth) : destlen);
1035 count -= col; /* how many columns left on this line */
1036 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1037 wid = mutt_strlen (buf);
1040 count -= wid; /* how many chars to pad */
1041 memset (wptr, ch, count);
1045 if (wid + wlen > destlen)
1046 len = destlen - wlen;
1049 memcpy (wptr, buf, len);
1052 col += mutt_strwidth (buf);
1054 break; /* skip rest of input */
1064 count = destlen - wlen;
1065 memset (wptr, ch, count);
1068 break; /* skip rest of input */
1075 while (ch == '_' || ch == ':')
1085 /* use callback function to handle this case */
1086 src = callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring, data, flags);
1089 mutt_strlower (buf);
1098 if ((len = mutt_strlen (buf)) + wlen > destlen)
1099 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1101 memcpy (wptr, buf, len);
1104 col += mutt_strwidth (buf);
1107 else if (*src == '\\')
1147 if (flags & M_FORMAT_MAKEPRINT)
1149 /* Make sure that the string is printable by changing all non-printable
1150 chars to dots, or spaces for non-printable whitespace */
1151 for (cp = dest ; *cp ; cp++)
1152 if (!IsPrint (*cp) &&
1153 !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1154 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1159 /* This function allows the user to specify a command to read stdout from in
1160 place of a normal file. If the last character in the string is a pipe (|),
1161 then we assume it is a commmand to run instead of a normal file. */
1162 FILE *mutt_open_read (const char *path, pid_t *thepid)
1167 int len = mutt_strlen (path);
1169 if (path[len - 1] == '|')
1171 /* read from a pipe */
1173 char *s = safe_strdup (path);
1177 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1182 if (stat (path, &s) < 0)
1184 if (S_ISDIR (s.st_mode))
1189 f = fopen (path, "r");
1195 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1196 int mutt_save_confirm (const char *s, struct stat *st)
1198 char tmp[_POSIX_PATH_MAX];
1203 magic = mx_get_magic (s);
1208 mutt_error _("Can't save message to POP mailbox.");
1214 if (magic == M_NNTP)
1216 mutt_error _("Can't save message to newsserver.");
1221 if (stat (s, st) != -1)
1225 mutt_error (_("%s is not a mailbox!"), s);
1229 if (option (OPTCONFIRMAPPEND) &&
1230 (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
1231 /* if we're appending to the trash, there's no point in asking */
1233 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1234 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1243 if (magic != M_IMAP)
1244 #endif /* execute the block unconditionally if we don't use imap */
1249 if (errno == ENOENT)
1251 if (option (OPTCONFIRMCREATE))
1253 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1254 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1268 CLEARLINE (LINES-1);
1272 void state_prefix_putc (char c, STATE *s)
1274 if (s->flags & M_PENDINGPREFIX)
1278 i = strlen (Quotebuf);
1281 if (i == sizeof (Quotebuf) - 1 || c == '\n')
1283 char buf[2 * SHORT_STRING];
1284 int j = 0, offset = 0;
1285 regmatch_t pmatch[1];
1287 state_reset_prefix (s);
1288 while (regexec ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch, 0) == 0)
1289 offset += pmatch->rm_eo;
1291 if (!option (OPTQUOTEEMPTY) && Quotebuf[0] == '\n')
1292 strcpy (buf, Quotebuf);
1293 else if (option (OPTQUOTEQUOTED) && offset)
1295 for (i = 0; i < offset; i++)
1296 if (Quotebuf[i] != ' ')
1298 strncpy (buf, Quotebuf, j + 1);
1299 strcpy (buf + j + 1, Quotebuf + j);
1302 snprintf (buf, sizeof (buf), "%s%s", NONULL(s->prefix), Quotebuf);
1304 state_puts (buf, s);
1312 state_set_prefix (s);
1317 int state_printf (STATE *s, const char *fmt, ...)
1323 rv = vfprintf (s->fpout, fmt, ap);
1329 void state_mark_attach (STATE *s)
1331 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1332 state_puts (AttachmentMarker, s);
1335 void state_attach_puts (const char *t, STATE *s)
1337 if (*t != '\n') state_mark_attach (s);
1341 if (*t++ == '\n' && *t)
1342 if (*t != '\n') state_mark_attach (s);
1346 void mutt_display_sanitize (char *s)
1355 void mutt_sleep (short s)
1364 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1365 * just initializes. Frees anything already in the buffer.
1367 * Disregards the 'destroy' flag, which seems reserved for caller.
1368 * This is bad, but there's no apparent protocol for it.
1370 BUFFER * mutt_buffer_init(BUFFER *b)
1374 b = safe_malloc(sizeof(BUFFER));
1382 memset(b, 0, sizeof(BUFFER));
1387 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1388 * just initializes. Frees anything already in the buffer. Copies in
1391 * Disregards the 'destroy' flag, which seems reserved for caller.
1392 * This is bad, but there's no apparent protocol for it.
1394 BUFFER * mutt_buffer_from(BUFFER *b, char *seed)
1399 b = mutt_buffer_init(b);
1400 b->data = safe_strdup (seed);
1401 b->dsize = mutt_strlen (seed);
1402 b->dptr = (char *) b->data + b->dsize;
1406 void mutt_buffer_addstr (BUFFER* buf, const char* s)
1408 mutt_buffer_add (buf, s, mutt_strlen (s));
1411 void mutt_buffer_addch (BUFFER* buf, char c)
1413 mutt_buffer_add (buf, &c, 1);
1416 void mutt_buffer_free (BUFFER **p)
1422 /* dptr is just an offset to data and shouldn't be freed */
1426 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1427 * Always one byte bigger than necessary for the null terminator, and
1428 * the buffer is always null-terminated */
1429 void mutt_buffer_add (BUFFER* buf, const char* s, size_t len)
1433 if (buf->dptr + len + 1 > buf->data + buf->dsize)
1435 offset = buf->dptr - buf->data;
1436 buf->dsize += len < 128 ? 128 : len + 1;
1437 safe_realloc ((void**) &buf->data, buf->dsize);
1438 buf->dptr = buf->data + offset;
1440 memcpy (buf->dptr, s, len);
1442 *(buf->dptr) = '\0';
1445 /* Decrease a file's modification time by 1 second */
1447 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1449 struct utimbuf utim;
1455 if (stat (f, &_st) == -1)
1460 if ((mtime = st->st_mtime) == time (NULL))
1463 utim.actime = mtime;
1464 utim.modtime = mtime;
1471 const char *mutt_make_version (void)
1473 static char vstring[STRING];
1474 snprintf (vstring, sizeof (vstring), "Mutt-ng %s (%s)",
1475 MUTT_VERSION, ReleaseDate);
1479 REGEXP *mutt_compile_regexp (const char *s, int flags)
1481 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1482 pp->pattern = safe_strdup (s);
1483 pp->rx = safe_calloc (sizeof (regex_t), 1);
1484 if (REGCOMP (pp->rx, NONULL(s), flags) != 0)
1485 mutt_free_regexp (&pp);
1490 void mutt_free_regexp (REGEXP **pp)
1492 FREE (&(*pp)->pattern);
1493 regfree ((*pp)->rx);
1498 void mutt_free_rx_list (RX_LIST **list)
1506 *list = (*list)->next;
1507 mutt_free_regexp (&p->rx);
1512 void mutt_free_spam_list (SPAM_LIST **list)
1520 *list = (*list)->next;
1521 mutt_free_regexp (&p->rx);
1522 safe_free(&p->template);
1527 int mutt_match_rx_list (const char *s, RX_LIST *l)
1531 for (; l; l = l->next)
1533 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0)
1535 dprint (5, (debugfile, "mutt_match_rx_list: %s matches %s\n", s, l->rx->pattern));
1543 int mutt_match_spam_list (const char *s, SPAM_LIST *l, char *text, int x)
1545 static regmatch_t *pmatch = NULL;
1546 static int nmatch = 0;
1554 for (; l; l = l->next)
1556 /* If this pattern needs more matches, expand pmatch. */
1557 if (l->nmatch > nmatch)
1559 safe_realloc (&pmatch, l->nmatch * sizeof(regmatch_t));
1563 /* Does this pattern match? */
1564 if (regexec (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch, (int) 0) == 0)
1566 dprint (5, (debugfile, "mutt_match_spam_list: %s matches %s\n", s, l->rx->pattern));
1567 dprint (5, (debugfile, "mutt_match_spam_list: %d subs\n", l->rx->rx->re_nsub));
1569 /* Copy template into text, with substitutions. */
1570 for (p = l->template; *p;)
1574 n = atoi(++p); /* find pmatch index */
1576 ++p; /* skip subst token */
1577 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1578 text[tlen++] = s[i];
1582 text[tlen++] = *p++;
1586 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));