summaryrefslogtreecommitdiff
path: root/src/read.c
blob: cd384eeb7ca4e2bbeb7788abed1921018c805864 (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
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
/*************************************************
*     xfpt - Simple ASCII->Docbook processor     *
*************************************************/

/* Copyright (c) University of Cambridge, 2008 */
/* Written by Philip Hazel. */

/* This module contains code for reading the input. */

#include "xfpt.h"




/*************************************************
*             Process macro line                 *
*************************************************/

/* This is the place where macro arguments are substituted. In a section
delimited by .eacharg/.endeach, the variable macro_argbase is set to the first
of the relative arguments. This function is also called from para.c in order to
handle inline macro calls.

Arguments:
  p         the macro input line
  b         where to put the result

Returns:    nothing
*/

void
read_process_macroline(uschar *p, uschar *b)
{
int optend = 0;

while (*p != 0)
  {
  int i;
  int argn = 0;
  argstr *argbase, *arg;

  /* If we are including an optional substring, when we get to the terminator,
  just skip it. */

  if (*p == optend)
    {
    optend = 0;
    p++;
    continue;
    }

  /* Until we hit a dollar, just copy verbatim */

  if (*p != '$') { *b++ = *p++; continue; }

  /* If the character after $ is another $, insert a literal $. */

  if (p[1] == '$') { p++; *b++ = *p++; continue; }

  /* If the character after $ is +, we are dealing with arguments
  relative to macro_arg0 in a ".eacharg" section. Otherwise, we are dealing
  with an absolute argument number. */

  if (p[1] == '+')
    {
    p++;
    if (macro_argbase == NULL)       /* Not in a .eacharg section */
      {
      error(18);
      *b++ = '+';
      *b++ = *p++;
      continue;
      }
    argbase = macro_argbase;
    }
  else argbase = macrocurrent->args;

  /* $= introduces an optional substring */

  if (p[1] == '=')
    {
    p++;
    if (!isdigit(p[1]))
      {
      error(17, p[1], "$=");
      *b++ = '$';
      *b++ = *p++;
      continue;
      }
    while (isdigit(*(++p))) argn = argn * 10 + *p - '0';

    optend = *p++;

    arg = argbase;
    for (i = 1; i < argn; i++)
      {
      if (arg == NULL) break;
      arg = arg->next;
      }

    if (arg == NULL || arg->string[0] == 0)
      {
      while (*p != 0 && *p != optend) p++;
      if (*p == optend) p++;
      }

    continue;
    }

  /* Not '=' after $; this is an argument substitution */

  if (!isdigit(p[1]))
    {
    error(17, p[1], "$");
    *b++ = *p++;
    continue;
    }

  while (isdigit(*(++p))) argn = argn * 10 + *p - '0';

  /* Handle $0 - currently no meaning */

  if (argn == 0)
    {
    continue;
    }

  /* Seek an argument in this invocation */

  arg = argbase;
  for (i = 1; i < argn; i++)
    {
    if (arg == NULL) break;
    arg = arg->next;
    }

  /* If not found, seek a default argument for an absolute substitution, but
  not for a relative one. */

  if (arg == NULL && argbase == macrocurrent->args)
    {
    arg = macrocurrent->macro->args;
    for (i = 1; i < argn; i++)
      {
      if (arg == NULL) break;
      arg = arg->next;
      }
    }

  /* If we have found an argument, substitute it. */

  if (arg != NULL) b += sprintf(CS b, "%s", arg->string);
  }

*b = 0;
}



/*************************************************
*         Get the next line of input             *
*************************************************/

/* There may be a saved line already in the buffer, following the reading of a
paragraph or a .nonl directive. Otherwise, take the next line from one of three
sources:

  (1) If popto is not negative, get an appropropriate line off the stack.
  (2) If we are in a macro, get the next macro line.
  (3) If we are in a file, read a new line from a file and handle any
      continuations.

There can be arbitrary nesting of macros and files, because a .include
directive may appear inside a macro. The current from_type vector is used to
keep track of what is current.

Arguments:  none
Returns:    pointer to the next line or NULL
*/

uschar *
read_nextline(void)
{
int len;
uschar *p, *q;

/* Handle a dot line that terminated a paragraph, or a .nonl line */

if (next_line != NULL)
  {
  uschar *yield = next_line;
  next_line = NULL;
  return yield;
  }

/* Handle a line off the stack */

if (popto == 0)
  {
  pushstr *ps = pushed;
  if (ps == NULL) error(12); else
    {
    popto = -1;
    (void)sprintf(CS inbuffer, "%s\n", ps->string);
    pushed = ps->next;
    free(ps);
    return inbuffer;
    }
  }

/* Handle a line off the stack when there is a matching line at the top or
below for the given letter. When we reach the matching line, stop popping. The
value of popto is set greater than zero only when it is known that there's a
matching line. */

if (popto > 0)
  {
  pushstr *ps = pushed;
  if (ps->letter == popto) popto = -1;
  (void)sprintf(CS inbuffer, "%s\n", ps->string);
  pushed = ps->next;
  free(ps);
  return inbuffer;
  }

/* Get the next line from the current macro or the current file. We need a loop
for handling the ends of macros and files. */

for (;;)
  {
  if (from_type[from_type_ptr] == FROM_MACRO)
    {
    if (macrocurrent->nextline == NULL)
      {
      macroexe *temp = macrocurrent;
      macrocurrent = macrocurrent->prev;
      free(temp);
      }
    else
      {
      read_process_macroline(macrocurrent->nextline->string, inbuffer);
      macrocurrent->nextline = macrocurrent->nextline->next;
      break;
      }
    }

  /* When reading from a file, handle continuation lines, but only within the
  single file. */

  else
    {
    if (istack == NULL) return NULL;
    if (Ufgets(inbuffer, INBUFFSIZE, istack->file) == NULL)
      {
      istackstr *prev = istack->prev;
      fclose(istack->file);
      free(istack);
      istack = prev;
      }
    else
      {
      istack->linenumber++;

      q = inbuffer;
      len = Ustrlen(q);

      for (;;)
        {
        p = q + len;
        while (p > q && isspace(p[-1])) p--;

        if (p - q < 3 || Ustrncmp(p - 3, "&&&", 3) != 0) break;

        q = p - 3;
        *q = 0;

        if (istack == NULL ||
            Ufgets(q, INBUFFSIZE - (q - inbuffer), istack->file) == NULL)
          break;

        istack->linenumber++;
        p = q;
        while (*p == ' ' || *p == '\t') p++;
        len = Ustrlen(p);
        if (p > q) memmove(q, p, len + 1);
        }

      break;
      }
    }

  /* We get here if the end of a macro or a file was reached. The appropriate
  chain has been popped. Back up the stack of input types before the loop
  repeats. */

  from_type_ptr--;
  }

return inbuffer;
}



/*************************************************
*        Complete the reading of a paragraph     *
*************************************************/

/* This function is called after a line has been identified as the start of a
paragraph. We need to read the rest so that flags can be matched across the
entire paragraph. (If there is nested material such as a footnote, this applies
only to the separate parts, not across the nesting.) The text is copied into
the paragraph buffer. Directives that are encountered in the paragraph are
processed, with two exceptions.

(1) For .literal, we set next_line so it is processed next, and exit. This is
the end of the paragraph.

(2) For .nest, we set *nest_info, according to whether it is the start or
end of a nested section, and exit.

Arguments:
  p           the first line
  nest_info   returns NEST_NO, NEST_START, or NEST_END

Returns:      the paragraph
*/


uschar *
read_paragraph(uschar *p, int *nest_info)
{
uschar *q = parabuffer;
int length = Ustrlen(p);

memcpy(q, p, length);
q += length;

*nest_info = NEST_NO;    /* Not hit .nest */

for (;;)
  {
  uschar *s;

  if ((p = read_nextline()) == NULL) break;

  if (Ustrncmp(p, ".literal ", 9) == 0)
    {
    next_line = p;
    break;
    }

  if (Ustrncmp(p, ".nest ", 6) == 0)
    {
    p += 6;
    while (isspace(*p)) p++;
    s = p + Ustrlen(p);
    while (s > p && isspace(s[-1])) s--;
    *s = 0;
    if (Ustrcmp(p, "begin") == 0) *nest_info = NEST_BEGIN;
    else if (Ustrcmp(p, "end") == 0) *nest_info = NEST_END;
    else error(26, p);
    break;
    }

  else if (*p == '.')
    {
    dot_process(p);
    continue;
    }

  /* End paragraph on encountering a completely blank line */

  for (s = p;  *s == ' ' || *s == '\t'; s++);
  if (*s == '\n') break;

  length = Ustrlen(p);
  memcpy(q, p, length);
  q += length;
  }

*q = 0;
return parabuffer;
}

/* End of read.c */