1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2025 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdlib.h>
#include <mailutils/sys/mailcap.h>
#include <mailutils/wordsplit.h>
#include <mailutils/locus.h>
#include <mailutils/nls.h>
#include <mailutils/filter.h>
#include <mailutils/list.h>
#include <mailutils/stream.h>
#include <mailutils/opool.h>
#include <mailutils/cctype.h>
static int
create_entry (mu_mailcap_t mp, char *input, mu_mailcap_entry_t *pent)
{
mu_mailcap_entry_t ent;
struct mu_wordsplit ws;
int rc;
size_t i;
ws.ws_delim = ";";
ws.ws_escape[0] = ";\\";
ws.ws_escape[1] = "";
if (mu_wordsplit (input, &ws,
MU_WRDSF_DELIM
| MU_WRDSF_NOCMD
| MU_WRDSF_NOVAR
| MU_WRDSF_WS
| MU_WRDSF_ESCAPE))
{
if (mp->error.error)
mp->error.error (mp->error.data, &mp->locus,
mu_wordsplit_strerror (&ws));
return MU_ERR_FAILURE;
}
if (ws.ws_wordc < 2)
{
if (mp->error.error)
mp->error.error (mp->error.data, &mp->locus, _("not enough fields"));
mu_wordsplit_free (&ws);
return MU_ERR_PARSE;
}
rc = mu_mailcap_entry_create (&ent, ws.ws_wordv[0], ws.ws_wordv[1]);
if (rc == 0)
{
for (i = 2; i < ws.ws_wordc; i++)
{
char *p = strchr (ws.ws_wordv[i], '=');
if (p)
{
*p++ = 0;
rc = mu_mailcap_entry_set_string (ent, ws.ws_wordv[i], p);
}
else
rc = mu_mailcap_entry_set_bool (ent, ws.ws_wordv[i]);
if (rc)
break;
}
}
mu_wordsplit_free (&ws);
if (rc == 0)
{
if (mp->flags & MU_MAILCAP_FLAG_LOCUS)
{
ent->lrp = calloc (1, sizeof ent->lrp[0]);
if (ent->lrp)
{
mu_locus_range_init (ent->lrp);
rc = mu_locus_range_copy (ent->lrp, &mp->locus);
}
else
rc = errno;
}
}
if (rc)
{
if (mp->error.error)
mp->error.error (mp->error.data, &mp->locus, mu_strerror (rc));
mu_mailcap_entry_destroy (&ent);
}
else
*pent = ent;
return rc;
}
int
mu_mailcap_parse (mu_mailcap_t mailcap, mu_stream_t input,
struct mu_locus_point const *pt)
{
int rc;
char *buffer = NULL;
size_t bufsize = 0;
size_t nread;
mu_opool_t acc = NULL;
mu_stream_t flt;
const char *argv[] = { "inline-comment", "#", "-i", "#", NULL };
int err = 0;
rc = mu_filter_create_args (&flt, input,
argv[0],
MU_ARRAY_SIZE (argv) - 1, argv,
MU_FILTER_DECODE, MU_STREAM_READ);
if (rc)
return rc;
mu_locus_range_init (&mailcap->locus);
if (pt)
{
mu_locus_point_copy (&mailcap->locus.beg, pt);
mu_locus_point_copy (&mailcap->locus.end, pt);
}
while ((rc = mu_stream_getline (flt, &buffer, &bufsize, &nread)) == 0)
{
mu_mailcap_entry_t entry;
int cont = acc && mu_opool_size (acc) > 0;
if (nread > 0)
{
buffer[--nread] = 0;
if (buffer[0] == '#')
{
unsigned long n;
char *p;
errno = 0;
n = strtoul (buffer + 2, &p, 10);
if (errno == 0 && (*p == 0 || mu_isspace (*p)))
mailcap->locus.beg.mu_line = mailcap->locus.end.mu_line = n;
continue;
}
else if (nread && buffer[nread-1] == '\\')
{
if (--nread > 0)
{
if (!acc)
{
rc = mu_opool_create (&acc, MU_OPOOL_DEFAULT);
if (rc)
break;
}
rc = mu_opool_append (acc, buffer, nread);
if (rc)
break;
}
mailcap->locus.end.mu_line++;
continue;
}
else if (cont)
{
char *p;
rc = mu_opool_append (acc, buffer, nread);
if (rc)
break;
rc = mu_opool_append_char (acc, 0);
if (rc)
break;
p = mu_opool_finish (acc, NULL);
rc = create_entry (mailcap, p, &entry);
mu_opool_clear (acc);
mailcap->locus.beg.mu_line = ++mailcap->locus.end.mu_line;
}
else if (nread == 0)
{
mailcap->locus.beg.mu_line = ++mailcap->locus.end.mu_line;
continue;
}
else
{
rc = create_entry (mailcap, buffer, &entry);
mailcap->locus.beg.mu_line = ++mailcap->locus.end.mu_line;
}
}
else if (cont)
{
/* Missing trailing newline */
char *p;
rc = mu_opool_append_char (acc, 0);
if (rc)
break;
p = mu_opool_finish (acc, NULL);
rc = create_entry (mailcap, p, &entry);
mu_opool_clear (acc);
}
else
break;
if (rc == 0)
{
if (mailcap->selector.selector
&& mailcap->selector.selector (entry, mailcap->selector.data) != 0)
{
mu_mailcap_entry_destroy (&entry);
}
else
{
mu_list_append (mailcap->elist, entry);
}
}
else if (rc == MU_ERR_PARSE)
err = 1;
else
break;
}
mu_stream_destroy (&flt);
mu_locus_range_deinit (&mailcap->locus);
mu_opool_destroy (&acc);
if (rc && err)
rc = MU_ERR_PARSE;
return rc;
}
|