~kevin8t8/mutt

ref: 2fbc29ec127aa3d699def01e574f14996db8cacc mutt/mutt_lisp.c -rw-r--r-- 10.0 KiB
2fbc29ecKevin McCarthy Allow my_var assignments to reference the $my_var in the value. 3 months ago
                                                                                
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * Copyright (C) 2020 Kevin J. McCarthy <kevin@8t8.us>
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program 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 General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "mutt.h"
#include "mutt_lisp.h"

#include <ctype.h>
#include <stdlib.h>

static int read_backticks (BUFFER *dest, BUFFER *line)
{
  char ch;
  int rc = -1, matched = 0;

  if (*line->dptr != '`')
    return -1;

  mutt_buffer_addch (dest, *line->dptr++);

  while (*line->dptr && !matched)
  {
    ch = *line->dptr;

    if (ch == '`')
      matched = 1;
    else if (ch == '\\')
    {
      mutt_buffer_addch (dest, *line->dptr++);
      if (!*line->dptr)
        break;
    }

    mutt_buffer_addch (dest, *line->dptr++);
  }

  if (matched)
    rc = 0;
  else
  {
    /* L10N:
       Printed when backticks in MuttLisp are not matched.
    */
    mutt_error (_("MuttLisp: unclosed backticks: %s"), mutt_b2s (line));
  }

  return rc;
}

static int read_list (BUFFER *list, BUFFER *line)
{
  int rc = -1, level = 0;
  char ch, quotechar = 0;

  mutt_buffer_clear (list);
  if (!line->dptr || !*line->dptr)
    return 0;

  SKIPWS (line->dptr);

  if (*line->dptr != '(')
    return -1;

  mutt_buffer_addch (list, *line->dptr++);
  level = 1;

  while (*line->dptr && level)
  {
    ch = *line->dptr;

    if (ch == quotechar)
      quotechar = 0;
    else if (ch == '\\' && quotechar != '\'')
    {
      mutt_buffer_addch (list, *line->dptr++);
      if (!*line->dptr)
        break;
    }
    else if (ch == '`' && (!quotechar || quotechar == '"'))
    {
      read_backticks (list, line);
      continue;
    }
    else if (!quotechar)
    {
      if (ch == '"' || ch == '\'')
        quotechar = ch;
      else if (ch == '(')
        level++;
      else if (ch == ')')
        level--;
    }

    mutt_buffer_addch (list, *line->dptr++);
  }

  if (level == 0)
    rc = 0;
  else
  {
    /* L10N:
       Printed when a MuttLisp list is missing a matching closing
       parenthesis.  For example: (a
    */
    mutt_error (_("MuttLisp: unclosed list: %s"), mutt_b2s (line));
  }

  return rc;
}

static int read_atom (BUFFER *atom, BUFFER *line)
{
  char ch, quotechar = 0;
  int rc = 0;

  mutt_buffer_clear (atom);
  if (!line->dptr || !*line->dptr)
    return 0;

  SKIPWS (line->dptr);

  while (*line->dptr)
  {
    ch = *line->dptr;

    if (ch == quotechar)
      quotechar = 0;
    else if (ch == '\\' && quotechar != '\'')
    {
      mutt_buffer_addch (atom, *line->dptr++);
      if (!*line->dptr)
        break;
    }
    else if (ch == '`' && (!quotechar || quotechar == '"'))
    {
      rc = read_backticks (atom, line);
      continue;
    }
    else if (!quotechar)
    {
      if (ISSPACE (ch) || ch == '(' || ch == ')')
        break;
      if (ch == '"' || ch == '\'')
        quotechar = ch;
    }

    mutt_buffer_addch (atom, *line->dptr++);
  }

  return rc;
}

/* returns -1 on an illegal sexp
 *         0 end of line (denoted by an empty sexp)
 *         1 valid sexp read in
 */
static int read_sexp (BUFFER *sexp, BUFFER *line)
{
  int rc = 0;

  mutt_buffer_clear (sexp);
  if (!line->dptr || !*line->dptr)
    return 0;

  SKIPWS (line->dptr);

  if (*line->dptr == '(')
    rc = read_list (sexp, line);
  else if (*line->dptr)
    rc = read_atom (sexp, line);

  if (rc == 0 && mutt_buffer_len (sexp))
    rc = 1;

  return rc;
}

/* note that a valid sexp might have been read in, but expand to an empty one.
 *
 * returns -1 on an illegal sexp
 *         0 end of line (denoted by an empty sexp)
 *         1 valid sexp read in
 */
static int read_eval_sexp (BUFFER *sexp, BUFFER *line)
{
  int rc = -1;
  BUFFER *temp_sexp = NULL;

  mutt_buffer_clear (sexp);
  if (!line->dptr || !*line->dptr)
    return 0;

  temp_sexp = mutt_buffer_new ();
  mutt_buffer_increase_size (temp_sexp, mutt_buffer_len (line));

  rc = read_sexp (temp_sexp, line);
  if (rc <= 0)
    goto cleanup;

  temp_sexp->dptr = temp_sexp->data;

  /* We send evaluation back through the muttrc parser.
   * Variables/expressions will be handled there, while list evaluation
   * will be done by calling mutt_lisp_eval_list().
   *
   * If we ever decide to add types (e.g. lists or variables), we
   * could instead directly handle their evaluation internally.  The
   * external mutt_lisp_eval_list() would always return the string
   * form, while the internal version could handle more.
   */
  if (mutt_extract_token (sexp, temp_sexp,
                          MUTT_TOKEN_LISP | MUTT_TOKEN_COMMENT |
                          MUTT_TOKEN_SEMICOLON) != 0)
  {
    rc = -1;
    goto cleanup;
  }

  /* It's possible mutt_extract_token() did not process the entire
   * temp_sexp.  For instance backticks might have generated multiple
   * words of output that weren't all processed. Prepend that to line
   * so it is processed later.
   */
  if (*temp_sexp->dptr)
  {
    BUFFER *extra = mutt_buffer_pool_get ();
    mutt_buffer_strcpy (extra, temp_sexp->dptr);
    mutt_buffer_addstr (extra, line->dptr);
    mutt_buffer_strcpy (line, mutt_b2s (extra));
    line->dptr = line->data;
    mutt_buffer_pool_release (&extra);
  }

  rc = 1;

cleanup:
  mutt_buffer_free (&temp_sexp);
  return rc;
}

static int lisp_concat (BUFFER *result, BUFFER *list)
{
  BUFFER *arg = NULL;
  int rc;

  mutt_buffer_clear (result);

  arg = mutt_buffer_new ();

  rc = read_eval_sexp (arg, list);
  while (rc > 0)
  {
    mutt_buffer_addstr (result, mutt_b2s (arg));
    rc = read_eval_sexp (arg, list);
  }

  mutt_buffer_free (&arg);

  return rc;
}

static int lisp_quote (BUFFER *result, BUFFER *list)
{
  mutt_buffer_strcpy (result, list->dptr);
  return 0;
}

static int lisp_equal (BUFFER *result, BUFFER *list)
{
  BUFFER *first_arg = NULL, *arg = NULL;
  int rc;

  mutt_buffer_clear (result);
  mutt_buffer_addch (result, 't');

  first_arg = mutt_buffer_new ();
  rc = read_eval_sexp (first_arg, list);
  if (rc <= 0)
    goto cleanup;

  arg = mutt_buffer_new ();
  rc = read_eval_sexp (arg, list);
  while (rc > 0)
  {
    if (mutt_strcmp (mutt_b2s (first_arg), mutt_b2s (arg)))
    {
      mutt_buffer_clear (result);
      break;
    }
    rc = read_eval_sexp (arg, list);
  }

cleanup:
  mutt_buffer_free (&first_arg);
  mutt_buffer_free (&arg);

  return rc;
}

static int lisp_not (BUFFER *result, BUFFER *list)
{
  BUFFER *arg = NULL;
  int rc;

  mutt_buffer_clear (result);

  arg = mutt_buffer_new ();

  rc = read_eval_sexp (arg, list);
  if (rc > 0 && !mutt_buffer_len (arg))
    mutt_buffer_addstr (result, "t");

  mutt_buffer_free (&arg);

  return rc;
}

static int lisp_and (BUFFER *result, BUFFER *list)
{
  BUFFER *arg = NULL;
  int rc;

  mutt_buffer_clear (result);
  mutt_buffer_addch (result, 't');

  arg = mutt_buffer_new ();

  rc = read_eval_sexp (arg, list);
  while (rc > 0)
  {
    mutt_buffer_strcpy (result, mutt_b2s (arg));
    if (!mutt_buffer_len (result))
      break;
    rc = read_eval_sexp (arg, list);
  }

  mutt_buffer_free (&arg);

  return rc;
}

static int lisp_or (BUFFER *result, BUFFER *list)
{
  BUFFER *arg = NULL;
  int rc;

  mutt_buffer_clear (result);

  arg = mutt_buffer_new ();

  rc = read_eval_sexp (arg, list);
  while (rc > 0)
  {
    mutt_buffer_strcpy (result, mutt_b2s (arg));
    if (mutt_buffer_len (result))
      break;
    rc = read_eval_sexp (arg, list);
  }

  mutt_buffer_free (&arg);

  return rc;
}

static int lisp_if (BUFFER *result, BUFFER *list)
{
  BUFFER *cond = NULL;
  int rc;

  mutt_buffer_clear (result);

  cond = mutt_buffer_new ();

  rc = read_eval_sexp (cond, list);
  if (rc <= 0)
  {
    /* L10N:
       An error printed for the 'if' function if the condition is missing.
       For example (if) by itself.
    */
    mutt_error (_("MuttLisp: missing if condition: %s"), mutt_b2s (list));
    goto cleanup;
  }

  if (!mutt_buffer_len (cond))
    read_sexp (result, list);
  rc = read_eval_sexp (result, list);

cleanup:
  mutt_buffer_free (&cond);

  return rc;
}

typedef struct
{
  char *name;
  int (*func) (BUFFER *, BUFFER *);
} lisp_func_t;

const lisp_func_t LispFunctions[] = {
  {"concat",       lisp_concat},
  {"quote",        lisp_quote},
  {"equal",        lisp_equal},
  {"not",          lisp_not},
  {"and",          lisp_and},
  {"or",           lisp_or},
  {"if",           lisp_if},
  {NULL,           NULL}
};

static int eval_function (BUFFER *result, const char *func, BUFFER *list)
{
  int i, rc = -1;

  for (i = 0; LispFunctions[i].name; i++)
  {
    if (!mutt_strcmp (func, LispFunctions[i].name))
    {
      rc = LispFunctions[i].func (result, list);
      break;
    }
  }
  if (!LispFunctions[i].name)
  {
    /* L10N:
       Printed when a function is called that is not recognized by MuttLisp.
    */
    mutt_error (_("MuttLisp: no such function %s"), NONULL (func));
  }

  return rc;
}

int mutt_lisp_eval_list (BUFFER *result, BUFFER *line)
{
  int rc = -1;
  BUFFER *list = NULL, *function = NULL;

  mutt_buffer_clear (result);
  if (!line->dptr || !*line->dptr)
    return 0;

  list = mutt_buffer_new ();
  mutt_buffer_increase_size (list, mutt_buffer_len (line));

  if (read_list (list, line) != 0)
    goto cleanup;

  /* Rewind list and trim the outer parens */
  *(list->dptr - 1) = '\0';
  list->dptr = list->data + 1;

  function = mutt_buffer_new ();
  if (read_sexp (function, list) <= 0)
    goto cleanup;
  SKIPWS (list->dptr);

  if (eval_function (result, mutt_b2s (function), list) < 0)
    goto cleanup;

  rc = 0;

cleanup:
  mutt_buffer_free (&list);
  mutt_buffer_free (&function);
  return rc;
}