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

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

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

#include "xfpt.h"



/*************************************************
*            Static variables                    *
*************************************************/

static uschar *next_line = NULL;



/*************************************************
*             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 from the current file    *
*************************************************/

/* There may be a stack of included files. This function makes them look like a
single source of input.

Arguments:
  buffer        where to read
  size          size of buffer

Returns:        buffer pointer or NULL
*/

static uschar *
read_nextfileline(uschar *buffer, int size)
{
if (istack == NULL) return NULL;
if (Ufgets(buffer, size, istack->file) == NULL)
  {
  istackstr *prev = istack->prev;
  fclose(istack->file);
  free(istack);
  istack = prev;
  return (istack == NULL)? NULL : read_nextfileline(buffer, size);
  }

istack->linenumber++;
return buffer;
}



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

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

  (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) Read a new line from a file and handle any continuations.

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 */

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;
  }

/* Handle the next macro line. */

while (macrocurrent != NULL)
  {
  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;
    return inbuffer;
    }
  }

/* Get a line from an input file */

if (read_nextfileline(inbuffer, INBUFFSIZE) == NULL) return NULL;

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 (read_nextfileline(q, INBUFFSIZE - (q - inbuffer)) == NULL) break;

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

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. The whole is copied into the paragraph buffer. Directives
that are encountered in the paragraph are processed, with the exception of
.literal, which terminates it. We leave a .literal directive in the input
buffer and set next_line to point to it, so that it is processed later.

Arguments:  the first line
Returns:    the paragraph
*/


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

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

for (;;)
  {
  uschar *s;

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

  if (Ustrncmp(p, ".literal ", 9) == 0)
    {
    next_line = 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 */