summaryrefslogtreecommitdiff
path: root/libmailutils/string/expvar.c
blob: 86687d9c9935a0578836426f7efa0f00c24d991b (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999-2021 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/>. */

#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/io.h>
#include <mailutils/nls.h>
#include <mailutils/assoc.h>
#include <mailutils/cstr.h>
#include <mailutils/cctype.h>
#include <mailutils/wordsplit.h>

static int
exp_getvar (char **ret, const char *vptr, size_t vlen, void *data)
{
  int rc;
  char *varname, *s = NULL;
  mu_assoc_t assoc = data;
  
  if (mu_assoc_is_empty (assoc))
    return MU_WRDSE_UNDEF;

  varname = malloc (vlen + 1);
  if (!varname)
    return MU_WRDSE_NOSPACE;
  memcpy (varname, vptr, vlen);
  varname[vlen] = 0;

  rc = mu_assoc_lookup (assoc, varname, &s);
  free (varname);

  switch (rc)
    {
    case 0:
      rc = MU_WRDSE_OK;
      break;
      
    case MU_ERR_NOENT:
      rc = MU_WRDSE_UNDEF;
      break;

    case MU_ERR_BUFSPACE:
    case ENOMEM:
      rc = MU_WRDSE_NOSPACE;
      break;

    default:
      s = (char*) mu_strerror (rc);
      rc = MU_WRDSE_USERERR;
    }

  if (s)
    {
      s = strdup (s);
      if (!s)
	return MU_WRDSE_NOSPACE;
      *ret = s;
    }
  else
    rc = MU_WRDSE_UNDEF;
  return rc;
}

static int
exp_localpart (int argc, char **argv, char **result)
{
  size_t len = strcspn (argv[1], "@");
  char *s;

  s = malloc (len + 1);
  if (!s)
    return MU_WRDSE_NOSPACE;
  memcpy (s, argv[1], len);
  s[len] = 0;

  *result = s;
  return MU_WRDSE_OK;
}

static int
exp_domainpart (int argc, char **argv, char **result)
{
  char *s = strchr (argv[1], '@');

  if (s)
    s++;
  else
    s = "";

  s = strdup (s);
  if (!s)
    return MU_WRDSE_NOSPACE;
  
  *result = s;
  
  return MU_WRDSE_OK;
}

static int
exp_shell (char **ret, char const *str, size_t len, void *closure)
{
  FILE *fp;
  char *cmd;
  int c, lastc;
  char *buffer = NULL;
  size_t bufsize = 0;
  size_t buflen = 0;
  
  cmd = malloc (len + 1);
  if (!cmd)
    return MU_WRDSE_NOSPACE;
  memcpy (cmd, str, len);
  cmd[len] = 0;

  fp = popen (cmd, "r");
  if (!fp)
    {
      ret = NULL;
      if (mu_asprintf (ret, "can't run %s: %s", cmd, mu_strerror (errno)))
	return MU_WRDSE_NOSPACE;
      else
	return MU_WRDSE_USERERR;
    }

  while ((c = fgetc (fp)) != EOF)
    {
      lastc = c;
      if (c == '\n')
	c = ' ';
      if (buflen == bufsize)
	{
	  char *p;
	  
	  if (bufsize == 0)
	    bufsize = 80;
	  else
	    bufsize *= 2;
	  p = realloc (buffer, bufsize);
	  if (!p)
	    {
	      free (buffer);
	      free (cmd);
	      return MU_WRDSE_NOSPACE;
	    }
	  buffer = p;
	}
      buffer[buflen++] = c;
    }

  if (buffer)
    {
      if (lastc == '\n')
	--buflen;
      buffer[buflen] = 0;
    }

  pclose (fp);
  free (cmd);

  *ret = buffer;
  return MU_WRDSE_OK;  
}

struct exp_command
{
  char *name;
  int minarg;
  int maxarg;
  int (*exp) (int argc, char **argv, char **ret);
};

static struct exp_command exp_command_tab[] = {
  { "localpart", 2, 2, exp_localpart },
  { "domainpart", 2, 2, exp_domainpart },
  { NULL }
};

static struct exp_command *
findcom (char const *name)
{
  struct exp_command *cp;

  for (cp = exp_command_tab; cp->name; cp++)
    if (strcmp (name, cp->name) == 0)
      return cp;

  return NULL;
}

static int
checkargc (struct exp_command *cmd, int argc)
{
  if (cmd->minarg && argc < cmd->minarg)
    return 1;
  else if (cmd->maxarg && argc > cmd->maxarg)
    return 1;
  return 0;
}

#define SHELL_CMD "shell"

static int
exp_runcmd (char **ret, const char *str, size_t len, char **argv, void *closure)
{
  int argc;
  struct exp_command *cmd;
  char *result = NULL;
  int rc;

  if (strcmp (argv[0], SHELL_CMD) == 0)
    {
      len -= sizeof SHELL_CMD - 1;
      str += sizeof SHELL_CMD - 1;
      while (len > 0 && mu_isspace (*str))
	{
	  len--;
	  str++;
	}
      
      if (len == 0)
	{
	  if (mu_asprintf (ret, _("%s: bad number of arguments"), argv[0]))
	    return MU_WRDSE_NOSPACE;
	  return MU_WRDSE_USERERR;
	}
      return exp_shell (ret, str, len, closure);
    }
  
  cmd = findcom (argv[0]);
  if (!cmd)
    {
      if (mu_asprintf (ret, _("%s: unknown function"), argv[0]))
	return MU_WRDSE_NOSPACE;
      return MU_WRDSE_USERERR;
    }
  
  for (argc = 0; argv[argc]; argc++)
    ;

  if (checkargc (cmd, argc))
    {
      if (mu_asprintf (ret, _("%s: bad number of arguments"), argv[0]))
	return MU_WRDSE_NOSPACE;
      return MU_WRDSE_USERERR;
    }

  rc = cmd->exp (argc, argv, &result);
  if (rc == MU_WRDSE_USERERR && result == NULL)
    {
      if (mu_asprintf (ret, _("%s: command expansion error"), argv[0]))
	return MU_WRDSE_NOSPACE;
      return MU_WRDSE_USERERR;
    }

  if (rc == MU_WRDSE_OK || rc == MU_WRDSE_USERERR)
    *ret = result;
  
  return rc;
}
  
int
mu_str_expand (char **output, char const *input, mu_assoc_t assoc)
{
  struct mu_wordsplit ws;
  int rc = 0;
  
  ws.ws_getvar = exp_getvar;
  ws.ws_command = exp_runcmd;
  ws.ws_closure = assoc;

  if (mu_wordsplit (input, &ws,
		    MU_WRDSF_NOSPLIT | MU_WRDSF_GETVAR | MU_WRDSF_CLOSURE))
    {
      if (ws.ws_errno == MU_WRDSE_NOSPACE)
	rc = ENOMEM;
      else
	{
	  char *p = strdup (mu_wordsplit_strerror (&ws));
	  if (!p)
	    rc = ENOMEM;
	  else
	    {
	      *output = p;
	      rc = MU_ERR_FAILURE;
	    }
	}
    }
  else if (ws.ws_wordc == 0)
    {
      *output = strdup ("");
      if (!*output)
	rc = ENOMEM;
    }
  else
    {
      *output = ws.ws_wordv[0];
      ws.ws_wordv[0] = NULL;
    }
  mu_wordsplit_free (&ws);
  return rc;
}

int
mu_str_vexpand (char **output, char const *input, ...)
{
  int rc;
  mu_assoc_t assoc;
  char *p[2];
  int i;
  va_list ap;
  
  rc = mu_assoc_create (&assoc, 0);
  if (rc)
    return rc;

  va_start (ap, input);
  i = 0;
  while ((p[i] = va_arg (ap, char *)) != NULL)
    {
      if (i == 1)
	{
	  rc = mu_assoc_install (assoc, p[0], p[1]);
	  if (rc)
	    {
	      mu_assoc_destroy (&assoc);
	      return rc;
	    }
	}
      i = (i + 1) % 2;
    }
  va_end (ap);

  rc = mu_str_expand (output, input, assoc);
  mu_assoc_destroy (&assoc);
  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.