2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
16 #include "mutt_curses.h"
28 #include "mutt_crypt.h"
39 #include <sys/types.h>
42 BODY *mutt_new_body (void)
44 BODY *p = (BODY *) safe_calloc (1, sizeof (BODY));
46 p->disposition = DISPATTACH;
52 /* Modified by blong to accept a "suggestion" for file name. If
53 * that file exists, then construct one with unique name but
54 * keep any extension. This might fail, I guess.
55 * Renamed to mutt_adv_mktemp so I only have to change where it's
56 * called, and not all possible cases.
58 void mutt_adv_mktemp (char *s, size_t l)
60 char buf[_POSIX_PATH_MAX];
61 char tmp[_POSIX_PATH_MAX];
66 strfcpy (buf, NONULL (Tempdir), sizeof (buf));
67 mutt_expand_path (buf, sizeof (buf));
69 snprintf (s, l, "%s/muttXXXXXX", buf);
73 strfcpy (tmp, s, sizeof (tmp));
74 mutt_sanitize_filename (tmp, 1);
75 snprintf (s, l, "%s/%s", buf, tmp);
76 if (lstat (s, &sb) == -1 && errno == ENOENT)
78 if ((period = strrchr (tmp, '.')) != NULL)
80 snprintf (s, l, "%s/%s.XXXXXX", buf, tmp);
85 strfcpy (s + sl, period, l - sl);
90 /* create a send-mode duplicate from a receive-mode body */
92 int mutt_copy_body (FILE * fp, BODY ** tgt, BODY * src)
94 char tmp[_POSIX_PATH_MAX];
97 PARAMETER *par, **ppar;
103 strfcpy (tmp, src->filename, sizeof (tmp));
110 mutt_adv_mktemp (tmp, sizeof (tmp));
111 if (mutt_save_attachment (fp, src, tmp, 0, NULL) == -1)
114 *tgt = mutt_new_body ();
117 memcpy (b, src, sizeof (BODY));
121 b->filename = safe_strdup (tmp);
122 b->use_disp = use_disp;
125 if (mutt_is_text_part (b))
128 b->xtype = safe_strdup (b->xtype);
129 b->subtype = safe_strdup (b->subtype);
130 b->form_name = safe_strdup (b->form_name);
131 b->filename = safe_strdup (b->filename);
132 b->d_filename = safe_strdup (b->d_filename);
133 b->description = safe_strdup (b->description);
136 * we don't seem to need the HEADER structure currently.
137 * XXX - this may change in the future
143 /* copy parameters */
144 for (par = b->parameter, ppar = &b->parameter; par;
145 ppar = &(*ppar)->next, par = par->next) {
146 *ppar = mutt_new_parameter ();
147 (*ppar)->attribute = safe_strdup (par->attribute);
148 (*ppar)->value = safe_strdup (par->value);
151 mutt_stamp_attachment (b);
158 void mutt_free_body (BODY ** p)
167 mutt_free_parameter (&b->parameter);
168 if (b->unlink && b->filename) {
169 dprint (1, (debugfile, "mutt_free_body: Unlinking %s.\n", b->filename));
170 unlink (b->filename);
172 else if (b->filename)
174 (debugfile, "mutt_free_body: Not unlinking %s.\n",
181 FREE (&b->description);
182 FREE (&b->form_name);
185 /* Don't free twice (b->hdr->content = b->parts) */
186 b->hdr->content = NULL;
187 mutt_free_header (&b->hdr);
191 mutt_free_body (&b->parts);
199 void mutt_free_parameter (PARAMETER ** p)
205 FREE (&t->attribute);
214 LIST *mutt_add_list (LIST * head, const char *data)
218 for (tmp = head; tmp && tmp->next; tmp = tmp->next);
220 tmp->next = safe_malloc (sizeof (LIST));
224 head = tmp = safe_malloc (sizeof (LIST));
226 tmp->data = safe_strdup (data);
231 void mutt_free_list (LIST ** list)
239 *list = (*list)->next;
245 HEADER *mutt_dup_header (HEADER * h)
249 hnew = mutt_new_header ();
250 memcpy (hnew, h, sizeof (HEADER));
254 void mutt_free_header (HEADER ** h)
258 mutt_free_envelope (&(*h)->env);
259 mutt_free_body (&(*h)->content);
260 FREE (&(*h)->maildir_flags);
264 mutt_free_list (&(*h)->chain);
266 #if defined USE_POP || defined USE_IMAP || defined USE_NNTP
272 /* returns true if the header contained in "s" is in list "t" */
273 int mutt_matches_ignore (const char *s, LIST * t)
275 for (; t; t = t->next) {
276 if (!ascii_strncasecmp (s, t->data, mutt_strlen (t->data))
283 /* prepend the path part of *path to *link */
284 void mutt_expand_link (char *newpath, const char *path, const char *link)
286 const char *lb = NULL;
289 /* link is full path */
291 strfcpy (newpath, link, _POSIX_PATH_MAX);
295 if ((lb = strrchr (path, '/')) == NULL) {
296 /* no path in link */
297 strfcpy (newpath, link, _POSIX_PATH_MAX);
302 memcpy (newpath, path, len);
303 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
306 char *mutt_expand_path (char *s, size_t slen)
308 return _mutt_expand_path (s, slen, 0);
311 char *_mutt_expand_path (char *s, size_t slen, int rx)
313 char p[_POSIX_PATH_MAX] = "";
314 char q[_POSIX_PATH_MAX] = "";
315 char tmp[_POSIX_PATH_MAX];
328 if (*(s + 1) == '/' || *(s + 1) == 0) {
329 strfcpy (p, NONULL (Homedir), sizeof (p));
335 if ((t = strchr (s + 1, '/')))
338 if ((pw = getpwnam (s + 1))) {
339 strfcpy (p, pw->pw_dir, sizeof (p));
348 /* user not found! */
362 /* if folder = {host} or imap[s]://host/: don't append slash */
363 if (mx_is_imap (NONULL (Maildir)) &&
364 (Maildir[mutt_strlen (Maildir) - 1] == '}' ||
365 Maildir[mutt_strlen (Maildir) - 1] == '/'))
366 strfcpy (p, NONULL (Maildir), sizeof (p));
369 snprintf (p, sizeof (p), "%s/", NONULL (Maildir));
375 /* elm compatibility, @ expands alias to user name */
382 if ((alias = mutt_lookup_alias (s + 1))) {
383 h = mutt_new_header ();
384 h->env = mutt_new_envelope ();
385 h->env->from = h->env->to = alias;
386 mutt_default_save (p, sizeof (p), h);
387 h->env->from = h->env->to = NULL;
388 mutt_free_header (&h);
389 /* Avoid infinite recursion if the resulting folder starts with '@' */
400 strfcpy (p, NONULL (Inbox), sizeof (p));
407 strfcpy (p, NONULL (Outbox), sizeof (p));
414 if (*(s + 1) == '!') {
415 strfcpy (p, NONULL (LastFolder), sizeof (p));
419 strfcpy (p, NONULL (Spoolfile), sizeof (p));
427 strfcpy (p, NONULL (LastFolder), sizeof (p));
434 strfcpy (p, NONULL (CurrentFolder), sizeof (p));
446 if (rx && *p && !recurse) {
447 mutt_rx_sanitize_string (q, sizeof (q), p);
448 snprintf (tmp, sizeof (tmp), "%s%s", q, tail);
451 snprintf (tmp, sizeof (tmp), "%s%s", p, tail);
453 strfcpy (s, tmp, slen);
458 /* Rewrite IMAP path in canonical form - aids in string comparisons of
459 * folders. May possibly fail, in which case s should be the same. */
461 imap_expand_path (s, slen);
467 /* Extract the real name from /etc/passwd's GECOS field.
468 * When set, honor the regular expression in GecosMask,
469 * otherwise assume that the GECOS field is a
470 * comma-separated list.
471 * Replace "&" by a capitalized version of the user's login
475 char *mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
477 regmatch_t pat_match[1];
482 if (!pw || !pw->pw_gecos)
485 memset (dest, 0, destlen);
488 if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
489 strfcpy (dest, pw->pw_gecos + pat_match[0].rm_so,
490 MIN (pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
492 else if ((p = strchr (pw->pw_gecos, ',')))
493 strfcpy (dest, pw->pw_gecos, MIN (destlen, p - pw->pw_gecos + 1));
495 strfcpy (dest, pw->pw_gecos, destlen);
497 pwnl = mutt_strlen (pw->pw_name);
499 for (idx = 0; dest[idx]; idx++) {
500 if (dest[idx] == '&') {
501 memmove (&dest[idx + pwnl], &dest[idx + 1],
502 MAX (destlen - idx - pwnl - 1, 0));
503 memcpy (&dest[idx], pw->pw_name, MIN (destlen - idx - 1, pwnl));
504 dest[idx] = toupper ((unsigned char) dest[idx]);
512 char *mutt_get_parameter (const char *s, PARAMETER * p)
514 for (; p; p = p->next)
515 if (ascii_strcasecmp (s, p->attribute) == 0)
521 void mutt_set_parameter (const char *attribute, const char *value,
527 mutt_delete_parameter (attribute, p);
531 for (q = *p; q; q = q->next) {
532 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
533 mutt_str_replace (&q->value, value);
538 q = mutt_new_parameter ();
539 q->attribute = safe_strdup (attribute);
540 q->value = safe_strdup (value);
545 void mutt_delete_parameter (const char *attribute, PARAMETER ** p)
549 for (q = *p; q; p = &q->next, q = q->next) {
550 if (ascii_strcasecmp (attribute, q->attribute) == 0) {
553 mutt_free_parameter (&q);
559 /* returns 1 if Mutt can't display this type of data, 0 otherwise */
560 int mutt_needs_mailcap (BODY * m)
565 if (!ascii_strcasecmp ("plain", m->subtype) ||
566 !ascii_strcasecmp ("rfc822-headers", m->subtype) ||
567 !ascii_strcasecmp ("enriched", m->subtype))
571 case TYPEAPPLICATION:
572 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (m))
574 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (m))
586 int mutt_is_text_part (BODY * b)
589 char *s = b->subtype;
591 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
597 if (t == TYPEMESSAGE) {
598 if (!ascii_strcasecmp ("delivery-status", s))
602 if ((WithCrypto & APPLICATION_PGP) && t == TYPEAPPLICATION) {
603 if (!ascii_strcasecmp ("pgp-keys", s))
610 void mutt_free_envelope (ENVELOPE ** p)
614 rfc822_free_address (&(*p)->return_path);
615 rfc822_free_address (&(*p)->from);
616 rfc822_free_address (&(*p)->to);
617 rfc822_free_address (&(*p)->cc);
618 rfc822_free_address (&(*p)->bcc);
619 rfc822_free_address (&(*p)->sender);
620 rfc822_free_address (&(*p)->reply_to);
621 rfc822_free_address (&(*p)->mail_followup_to);
623 FREE (&(*p)->list_post);
624 FREE (&(*p)->subject);
625 /* real_subj is just an offset to subject and shouldn't be freed */
626 FREE (&(*p)->message_id);
627 FREE (&(*p)->supersedes);
629 FREE (&(*p)->x_label);
630 FREE (&(*p)->organization);
632 FREE (&(*p)->newsgroups);
634 FREE (&(*p)->followup_to);
635 FREE (&(*p)->x_comment_to);
638 mutt_buffer_free (&(*p)->spam);
639 mutt_free_list (&(*p)->references);
640 mutt_free_list (&(*p)->in_reply_to);
641 mutt_free_list (&(*p)->userhdrs);
645 void _mutt_mktemp (char *s, const char *src, int line)
647 snprintf (s, _POSIX_PATH_MAX, "%s/mutt-%s-%d-%d-%d", NONULL (Tempdir),
648 NONULL (Hostname), (int) getuid (), (int) getpid (), Counter++);
650 (debugfile, "%s:%d: mutt_mktemp returns \"%s\".\n", src, line, s));
654 void mutt_free_alias (ALIAS ** p)
662 rfc822_free_address (&t->addr);
667 /* collapse the pathname using ~ or = when possible */
668 void mutt_pretty_mailbox (char *s)
674 scheme = url_check_scheme (s);
677 if (scheme == U_IMAP || scheme == U_IMAPS) {
678 imap_pretty_mailbox (s);
683 /* if s is an url, only collapse path component */
684 if (scheme != U_UNKNOWN) {
685 p = strchr (s, ':') + 1;
686 if (!strncmp (p, "//", 2))
687 q = strchr (p + 2, '/');
689 q = strchr (p, '\0');
693 /* first attempt to collapse the pathname */
695 if (*p == '/' && p[1] == '/') {
699 else if (p[0] == '/' && p[1] == '.' && p[2] == '/') {
708 if (mutt_strncmp (s, Maildir, (len = mutt_strlen (Maildir))) == 0 &&
711 memmove (s, s + len, mutt_strlen (s + len) + 1);
713 else if (mutt_strncmp (s, Homedir, (len = mutt_strlen (Homedir))) == 0 &&
716 memmove (s, s + len - 1, mutt_strlen (s + len - 1) + 1);
720 void mutt_pretty_size (char *s, size_t len, long n)
723 strfcpy (s, "0K", len);
724 else if (n < 10189) /* 0.1K - 9.9K */
725 snprintf (s, len, "%3.1fK", (n < 103) ? 0.1 : n / 1024.0);
726 else if (n < 1023949) { /* 10K - 999K */
727 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
728 snprintf (s, len, "%ldK", (n + 51) / 1024);
730 else if (n < 10433332) /* 1.0M - 9.9M */
731 snprintf (s, len, "%3.1fM", n / 1048576.0);
734 /* (10433332 + 52428) / 1048576 = 10 */
735 snprintf (s, len, "%ldM", (n + 52428) / 1048576);
739 void mutt_expand_file_fmt (char *dest, size_t destlen, const char *fmt,
742 char tmp[LONG_STRING];
744 mutt_quote_filename (tmp, sizeof (tmp), src);
745 mutt_expand_fmt (dest, destlen, fmt, tmp);
748 void mutt_expand_fmt (char *dest, size_t destlen, const char *fmt,
756 slen = mutt_strlen (src);
759 for (p = fmt, d = dest; destlen && *p; p++) {
768 strfcpy (d, src, destlen + 1);
769 d += destlen > slen ? slen : destlen;
770 destlen -= destlen > slen ? slen : destlen;
787 if (!found && destlen > 0) {
788 safe_strcat (dest, destlen, " ");
789 safe_strcat (dest, destlen, src);
794 /* return 0 on success, -1 on abort, 1 on error */
795 int mutt_check_overwrite (const char *attname, const char *path,
796 char *fname, size_t flen, int *append,
800 char tmp[_POSIX_PATH_MAX];
803 strfcpy (fname, path, flen);
804 if (access (fname, F_OK) != 0)
806 if (stat (fname, &st) != 0)
808 if (S_ISDIR (st.st_mode)) {
810 switch (mutt_multi_choice
811 (_("File is a directory, save under it? [(y)es, (n)o, (a)ll]"),
814 mutt_str_replace (directory, fname);
829 mutt_yesorno (_("File is a directory, save under it?"),
831 return (rc == M_NO) ? 1 : -1;
833 if (!attname || !attname[0]) {
835 if (mutt_get_field (_("File under directory: "), tmp, sizeof (tmp),
836 M_FILE | M_CLEAR) != 0 || !tmp[0])
838 mutt_concat_path (fname, path, tmp, flen);
841 mutt_concat_path (fname, path, mutt_basename (attname), flen);
844 if (*append == 0 && access (fname, F_OK) == 0) {
845 switch (mutt_multi_choice
846 (_("File exists, (o)verwrite, (a)ppend, or (c)ancel?"), _("oac")))
854 *append = M_SAVE_APPEND;
856 case 1: /* overwrite */
857 *append = M_SAVE_OVERWRITE;
864 void mutt_save_path (char *d, size_t dsize, ADDRESS * a)
866 if (a && a->mailbox) {
867 strfcpy (d, a->mailbox, dsize);
868 if (!option (OPTSAVEADDRESS)) {
871 if ((p = strpbrk (d, "%@")))
880 void mutt_safe_path (char *s, size_t l, ADDRESS * a)
884 mutt_save_path (s, l, a);
886 if (*p == '/' || ISSPACE (*p) || !IsPrint ((unsigned char) *p))
890 /* counts how many characters in s can be skipped while none of the
891 * characters of c appears */
892 int mutt_skipchars (const char *s, const char *c)
898 register const char *t = c;
908 return (mutt_strlen (p));
911 void mutt_FormatString (char *dest, /* output buffer */
912 size_t destlen, /* output buffer len */
913 const char *src, /* template string */
914 format_t * callback, /* callback for processing */
915 unsigned long data, /* callback data */
917 { /* callback flags */
918 char prefix[SHORT_STRING], buf[LONG_STRING], *cp, *wptr = dest, ch;
919 char ifstring[SHORT_STRING], elsestring[SHORT_STRING];
920 size_t wlen, count, len, col, wid;
923 destlen--; /* save room for the terminal \0 */
924 wlen = (flags & M_FORMAT_ARROWCURSOR && option (OPTARROWCURSOR)) ? 3 : 0;
927 while (*src && wlen < destlen) {
938 flags |= M_FORMAT_OPTIONAL;
942 flags &= ~M_FORMAT_OPTIONAL;
944 /* eat the format string */
947 while (count < sizeof (prefix) &&
948 (isdigit ((unsigned char) *src) || *src == '.' || *src == '-'))
957 break; /* bad format */
959 ch = *src++; /* save the character to switch on */
961 if (flags & M_FORMAT_OPTIONAL) {
963 break; /* bad format */
966 /* eat the `if' part of the string */
969 while (count < sizeof (ifstring) && *src && *src != '?'
976 /* eat the `else' part of the string (optional) */
978 src++; /* skip the & */
981 while (count < sizeof (elsestring) && *src && *src != '?') {
988 break; /* bad format */
990 src++; /* move past the trailing `?' */
993 /* handle generic cases first */
995 /* right justify to EOL */
996 ch = *src++; /* pad char */
997 /* calculate space left on line. if we've already written more data
998 than will fit on the line, ignore the rest of the line */
999 if (DrawFullLine || option (OPTSTATUSONTOP))
1000 count = (COLS < destlen ? COLS : destlen);
1003 ((COLS - SidebarWidth) <
1004 destlen ? (COLS - SidebarWidth) : destlen);
1006 count -= col; /* how many columns left on this line */
1007 mutt_FormatString (buf, sizeof (buf), src, callback, data, flags);
1008 wid = mutt_strlen (buf);
1010 count -= wid; /* how many chars to pad */
1011 memset (wptr, ch, count);
1015 if (wid + wlen > destlen)
1016 len = destlen - wlen;
1019 memcpy (wptr, buf, len);
1022 col += mutt_strwidth (buf);
1024 break; /* skip rest of input */
1026 else if (ch == '|') {
1031 if (destlen > wlen) {
1032 count = destlen - wlen;
1033 memset (wptr, ch, count);
1036 break; /* skip rest of input */
1042 while (ch == '_' || ch == ':') {
1051 /* use callback function to handle this case */
1053 callback (buf, sizeof (buf), ch, src, prefix, ifstring, elsestring,
1057 mutt_strlower (buf);
1066 if ((len = mutt_strlen (buf)) + wlen > destlen)
1067 len = (destlen - wlen > 0) ? (destlen - wlen) : 0;
1069 memcpy (wptr, buf, len);
1072 col += mutt_strwidth (buf);
1075 else if (*src == '\\') {
1104 unsigned int bar = mutt_skipchars (src, "%\\");
1105 char *bar2 = safe_malloc (bar + 1);
1107 strfcpy (bar2, src, bar + 1);
1112 col += mutt_strwidth (bar2);
1119 if (flags & M_FORMAT_MAKEPRINT) {
1120 /* Make sure that the string is printable by changing all non-printable
1121 chars to dots, or spaces for non-printable whitespace */
1122 for (cp = dest; *cp; cp++)
1123 if (!IsPrint (*cp) && !((flags & M_FORMAT_TREE) && (*cp <= M_TREE_MAX)))
1124 *cp = isspace ((unsigned char) *cp) ? ' ' : '.';
1129 /* This function allows the user to specify a command to read stdout from in
1130 place of a normal file. If the last character in the string is a pipe (|),
1131 then we assume it is a commmand to run instead of a normal file. */
1132 FILE *mutt_open_read (const char *path, pid_t * thepid)
1137 int len = mutt_strlen (path);
1139 if (path[len - 1] == '|') {
1140 /* read from a pipe */
1142 char *s = safe_strdup (path);
1146 *thepid = mutt_create_filter (s, NULL, &f, NULL);
1150 if (stat (path, &s) < 0)
1152 if (S_ISDIR (s.st_mode)) {
1156 f = fopen (path, "r");
1162 /* returns 0 if OK to proceed, -1 to abort, 1 to retry */
1163 int mutt_save_confirm (const char *s, struct stat *st)
1165 char tmp[_POSIX_PATH_MAX];
1170 magic = mx_get_magic (s);
1173 if (magic == M_POP) {
1174 mutt_error _("Can't save message to POP mailbox.");
1181 if (magic == M_NNTP) {
1182 mutt_error _("Can't save message to newsserver.");
1188 if (stat (s, st) != -1) {
1190 mutt_error (_("%s is not a mailbox!"), s);
1194 if (option (OPTCONFIRMAPPEND) &&
1195 (!TrashPath || (mutt_strcmp (s, TrashPath) != 0)))
1196 /* if we're appending to the trash, there's no point in asking */
1198 snprintf (tmp, sizeof (tmp), _("Append messages to %s?"), s);
1199 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1207 if (magic != M_IMAP)
1208 #endif /* execute the block unconditionally if we don't use imap */
1213 if (errno == ENOENT) {
1214 if (option (OPTCONFIRMCREATE)) {
1215 snprintf (tmp, sizeof (tmp), _("Create %s?"), s);
1216 if ((rc = mutt_yesorno (tmp, M_YES)) == M_NO)
1229 CLEARLINE (LINES - 1);
1233 void state_prefix_putc (char c, STATE * s)
1235 if (s->flags & M_PENDINGPREFIX) {
1238 i = mutt_strlen (Quotebuf);
1241 if (i == sizeof (Quotebuf) - 1 || c == '\n') {
1242 char buf[2 * SHORT_STRING];
1243 int j = 0, offset = 0;
1244 regmatch_t pmatch[1];
1246 state_reset_prefix (s);
1248 ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch,
1250 offset += pmatch->rm_eo;
1252 if (!option (OPTQUOTEEMPTY) && Quotebuf[offset] == '\n') {
1256 else if (option (OPTQUOTEQUOTED) && offset) {
1257 for (i = 0; i < offset; i++)
1258 if (Quotebuf[i] != ' ')
1260 strncpy (buf, Quotebuf, j + 1);
1261 strcpy (buf + j + 1, Quotebuf + j);
1264 snprintf (buf, sizeof (buf), "%s%s", NONULL (s->prefix), Quotebuf);
1266 state_puts (buf, s);
1273 state_set_prefix (s);
1278 int state_printf (STATE * s, const char *fmt, ...)
1284 rv = vfprintf (s->fpout, fmt, ap);
1290 void state_mark_attach (STATE * s)
1292 if ((s->flags & M_DISPLAY) && !mutt_strcmp (Pager, "builtin"))
1293 state_puts (AttachmentMarker, s);
1296 void state_attach_puts (const char *t, STATE * s)
1299 state_mark_attach (s);
1302 if (*t++ == '\n' && *t)
1304 state_mark_attach (s);
1308 void mutt_display_sanitize (char *s)
1316 void mutt_sleep (short s)
1325 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1326 * just initializes. Frees anything already in the buffer.
1328 * Disregards the 'destroy' flag, which seems reserved for caller.
1329 * This is bad, but there's no apparent protocol for it.
1331 BUFFER *mutt_buffer_init (BUFFER * b)
1334 b = safe_malloc (sizeof (BUFFER));
1339 safe_free (b->data);
1341 memset (b, 0, sizeof (BUFFER));
1346 * Creates and initializes a BUFFER*. If passed an existing BUFFER*,
1347 * just initializes. Frees anything already in the buffer. Copies in
1350 * Disregards the 'destroy' flag, which seems reserved for caller.
1351 * This is bad, but there's no apparent protocol for it.
1353 BUFFER *mutt_buffer_from (BUFFER * b, char *seed)
1358 b = mutt_buffer_init (b);
1359 b->data = safe_strdup (seed);
1360 b->dsize = mutt_strlen (seed);
1361 b->dptr = (char *) b->data + b->dsize;
1365 void mutt_buffer_addstr (BUFFER * buf, const char *s)
1367 mutt_buffer_add (buf, s, mutt_strlen (s));
1370 void mutt_buffer_addch (BUFFER * buf, char c)
1372 mutt_buffer_add (buf, &c, 1);
1375 void mutt_buffer_free (BUFFER ** p)
1381 /* dptr is just an offset to data and shouldn't be freed */
1385 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.
1386 * Always one byte bigger than necessary for the null terminator, and
1387 * the buffer is always null-terminated */
1388 void mutt_buffer_add (BUFFER * buf, const char *s, size_t len)
1392 if (buf->dptr + len + 1 > buf->data + buf->dsize) {
1393 offset = buf->dptr - buf->data;
1394 buf->dsize += len < 128 ? 128 : len + 1;
1395 safe_realloc ((void **) &buf->data, buf->dsize);
1396 buf->dptr = buf->data + offset;
1398 memcpy (buf->dptr, s, len);
1400 *(buf->dptr) = '\0';
1403 /* Decrease a file's modification time by 1 second */
1405 time_t mutt_decrease_mtime (const char *f, struct stat *st)
1407 struct utimbuf utim;
1412 if (stat (f, &_st) == -1)
1417 if ((mtime = st->st_mtime) == time (NULL)) {
1419 utim.actime = mtime;
1420 utim.modtime = mtime;
1427 const char *mutt_make_version (void)
1429 static char vstring[STRING];
1431 snprintf (vstring, sizeof (vstring), "Mutt-ng %s (%s)",
1432 MUTT_VERSION, ReleaseDate);
1436 REGEXP *mutt_compile_regexp (const char *s, int flags)
1438 REGEXP *pp = safe_calloc (sizeof (REGEXP), 1);
1440 pp->pattern = safe_strdup (s);
1441 pp->rx = safe_calloc (sizeof (regex_t), 1);
1442 if (REGCOMP (pp->rx, NONULL (s), flags) != 0)
1443 mutt_free_regexp (&pp);
1448 void mutt_free_regexp (REGEXP ** pp)
1450 FREE (&(*pp)->pattern);
1451 regfree ((*pp)->rx);
1456 void mutt_free_rx_list (RX_LIST ** list)
1464 *list = (*list)->next;
1465 mutt_free_regexp (&p->rx);
1470 void mutt_free_spam_list (SPAM_LIST ** list)
1478 *list = (*list)->next;
1479 mutt_free_regexp (&p->rx);
1480 safe_free (&p->template);
1485 int mutt_match_rx_list (const char *s, RX_LIST * l)
1490 for (; l; l = l->next) {
1491 if (regexec (l->rx->rx, s, (size_t) 0, (regmatch_t *) 0, (int) 0) == 0) {
1493 (debugfile, "mutt_match_rx_list: %s matches %s\n", s,
1502 int mutt_match_spam_list (const char *s, SPAM_LIST * l, char *text, int x)
1504 static regmatch_t *pmatch = NULL;
1505 static int nmatch = 0;
1514 for (; l; l = l->next) {
1515 /* If this pattern needs more matches, expand pmatch. */
1516 if (l->nmatch > nmatch) {
1517 safe_realloc (&pmatch, l->nmatch * sizeof (regmatch_t));
1521 /* Does this pattern match? */
1523 (l->rx->rx, s, (size_t) l->nmatch, (regmatch_t *) pmatch,
1526 (debugfile, "mutt_match_spam_list: %s matches %s\n", s,
1529 (debugfile, "mutt_match_spam_list: %d subs\n",
1530 l->rx->rx->re_nsub));
1532 /* Copy template into text, with substitutions. */
1533 for (p = l->template; *p;) {
1535 n = atoi (++p); /* find pmatch index */
1536 while (isdigit (*p))
1537 ++p; /* skip subst token */
1538 for (i = pmatch[n].rm_so; (i < pmatch[n].rm_eo) && (tlen < x); i++)
1539 text[tlen++] = s[i];
1542 text[tlen++] = *p++;
1546 dprint (5, (debugfile, "mutt_match_spam_list: \"%s\"\n", text));