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

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

/* This module contains code for processing a paragraph by looking for flag
characters and also dealing with literals that must be escaped. */

#include "xfpt.h"


/*************************************************
*         Process an inline macro call           *
*************************************************/

/* This function is called when we encounter & followed by a name and an
opening parenthesis. This significies an inline macro call.

Arguments:
  p         points to the start of the macro name
  q         points to the opening parenthesis

Returns:    updated value for p to continue processing
*/

static uschar *
para_macro_process(uschar *p, uschar *q)
{
int length = q - p;
argstr **pp;
macrodef *md;
macroexe *me;

for (md = macrolist; md != NULL; md = md->next)
  {
  if (length == md->namelength && Ustrncmp(p, md->name, length) == 0) break;
  }

if (md == NULL)
  {
  error(23, length, p);
  (void)fprintf(outfile, "&");
  return p;
  }

/* Set up the macro and its arguments on the input stack, just as we do for a
macro called as a directive, though the arguments are comma-separated here. */

me = misc_malloc(sizeof(macroexe));
me->prev = macrocurrent;
macrocurrent = me;
me->macro = md;
me->nextline = md->lines;

me->args = NULL;
pp = &(me->args);

while (isspace(*(++q)));
while (*q != 0 && *q != ')')
  {
  argstr *as = misc_malloc(sizeof(argstr));
  as->next = NULL;
  *pp = as;
  pp = &(as->next);
  as->string = misc_readitem(q, US",)", &length, NULL, 0);
  q += length;
  if (*q == ',') while (isspace(*(++q)));
  }

if (*q != ')')
  {
  error(24, p);
  (void)fprintf(outfile, "&");
  return p;
  }

/* Bump the count indicating that we are in an inline macro, and then process
the lines of the macro. It's a count rather than a flag, because the macro data
may also reference inline macros. Each line is processed and output, but
without the terminating newline. */

para_inline_macro++;

for (;;)
  {
  uschar buffer[INBUFFSIZE];

  read_process_macroline(macrocurrent->nextline->string, buffer);

  /* A directive such as .eacharg can skip to the end of the macro if there
  is no .endeach. Detect this by looking for a change of macrocurrent value,
  because there may be an enclosing macro. */

  if (*buffer == '.')
    {
    dot_process(buffer);
    if (macrocurrent != me) break;
    }

  /* Process a data line */

  else
    {
    uschar *qq = buffer + Ustrlen(buffer);
    while (qq > buffer && isspace(qq[-1])) qq--;
    *qq = 0;
    para_process(buffer);
    }

  /* Advance to the next macro line, exiting the loop when we hit the
  end of the macro. */

  macrocurrent->nextline = macrocurrent->nextline->next;
  if (macrocurrent->nextline == NULL)
    {
    macroexe *temp = macrocurrent;
    macrocurrent = macrocurrent->prev;
    free(temp);
    break;
    }
  }

/* Unstack one level of inline macro, and return the position to carry on
from in the original input. */

para_inline_macro--;
return q + 1;
}




/*************************************************
*        Check a flag string for literal         *
*************************************************/

/* This function is called to scan flag replacement strings to check for
<literal> and <literal/> so that we can avoid messing with single quotes in
literal text.

Arguments:
  s           the flag string
  b           a boolean that is set TRUE, FALSE, or left alone

Returns:      nothing
*/

static void
check_literal(uschar *s, BOOL *b)
{
while (*s != 0)
  {
  s = Ustrchr(s, '<');
  if (s == NULL) return;

  if (Ustrncmp(s, "<literal", 8) == 0 && (s[8] == '>' || isspace(s[8])))
    *b = TRUE;
  else if (Ustrncmp(s, "</literal", 9) == 0 && (s[9] == '>' || isspace(s[9])))
    *b = FALSE;

  while (*s != 0 && *s != '>')
    {
    if (*s == '"' || *s == '\'')
      {
      int t = *s++;
      while (*s != 0 && *s != t) s++;
      if (*s == 0) return;
      }
    s++;
    }

  if (*s++ == 0) return;
  }
}



/*************************************************
*             Process a paragraph                *
*************************************************/

/* This is used both for a complete paragraph that may consist of many lines,
and for literal layout lines that nevertheless need to be scanned for flags.
However, it is not used for literal text.

Argument:  the text to be processed
Returns:   nothing
*/

void
para_process(uschar *p)
{
flagstr *f;
flagstr *fstack[FLAGSTACKSIZE];
int fstackcount = 0;
BOOL inliteraltext = FALSE;

while (*p != 0)
  {
  int c, i;

  /* Check for the closing flag sequence for any outstanding flag pairs. If we
  find one that isn't at the top of the stack, there's a nesting error. */

  for (i = fstackcount - 1; i >= 0; i--)
    {
    f = fstack[i];
    if (Ustrncmp(f->flag2, p, f->length2) == 0)
      {
      int j;
      for (j = i + 1; j < fstackcount; j++)
        error(8, fstack[j]->flag2, f->flag2);
      fstackcount = i;
      (void)fprintf(outfile, "%s", CS f->rep2);
      check_literal(f->rep2, &inliteraltext);
      p += f->length2;
      i = fstackcount;   /* Reset in case another follows immediately */
      continue;
      }
    }

  /* We may be at the end of string if we've just passed a closing flag
  sequence. */

  if (*p == 0) break;

  /* Otherwise, scan character by character. Angle brackets are escaped,
  single quotes are mapped except in literal text, and then everything other
  than ampersand is treated literally. */

  c = *p++;
  if (c == '<')  { (void)fprintf(outfile, "&lt;"); continue; }
  if (c == '>')  { (void)fprintf(outfile, "&gt;"); continue; }

  if (!inliteraltext)
    {
    if (c == '`')
      {
      (void)fprintf(outfile, "&#x2018;");
      continue;
      }

    if (c == '\'')
      {
      (void)fprintf(outfile, "&#x2019;");
      continue;
      }
    }

  if (c != '&')  { (void)fputc(c, outfile); continue; }

  /* Ampersand must be followed by something. */

  if (*p == 0 || *p == '\n')
    {
    error(25);
    continue;
    }

  /* Handle all the fancy stuff that starts with ampersand. First, all the
  cases where a letter is next. */

  if (isalpha(*p))
    {
    int entlen;
    uschar *q = p + 1;
    while (isalnum(*q) || *q == '.') q++;

    /* Check for an inline macro call; handle out-of line as the code is
    non-trivial. */

    if (*q == '(')
      {
      p = para_macro_process(p, q);
      continue;
      }

    /* Otherwise, if it is not XML entity reference syntax there's an error. We
    support some special entities that start with "&xfpt." for inserting local
    data. We also allow local entities to be defined. If we don't recognize an
    entity name, it is passed through untouched, assuming it is a defined XML
    entity. */

    entlen = q - p;

    if (*q != ';')
      {
      error (3, entlen, p);
      (void)fprintf(outfile, "&");
      continue;
      }

    /* This special provides support for the .revision directive. */

    if (Ustrncmp(p, "xfpt.rev", entlen) == 0)
      {
      if (revision != NULL && *revision != 0)
        (void)fprintf(outfile, " revisionflag=\"%s\"", revision);
      }

    /* Search for a locally defined entitity */

    else
      {
      tree_node *t;
      *q = 0;
      t = tree_search(entities, p);
      *q = ';';
      if (t != NULL)
        (void)fprintf(outfile, "%s", CS t->data);
      else
        (void)fprintf(outfile, "&%.*s;", entlen, p);
      }

    if (*q == ';') q++;
    p = q;
    continue;
    }

  /* Ampersand followed by # might be an XML numerical entity. If not, we fall
  through in case it's a flag. */

  if (*p == '#')
    {
    uschar *q = p + 1;
    if (isdigit(*q))
      {
      for (q++; isdigit(*q); q++);
      if (*q == ';')
        {
        (void)fprintf(outfile, "&%.*s", q - p, p);
        p = q;
        continue;
        }
      }

    else if (*q == 'x')
      {
      for (q++; isxdigit(*q); q++);
      if (*q == ';')
        {
        (void)fprintf(outfile, "&%.*s", q - p, p);
        p = q;
        continue;
        }
      }
    }

  /* If not an XML entity, search out defined flag sequences */

  for (f = flaglist; f != NULL; f = f->next)
    { if (Ustrncmp(p, f->flag1, f->length1) == 0) break; }

  if (f == NULL)
    {
    error(6, *p);
    (void)fprintf(outfile, "&amp;");
    continue;
    }

  /* If the flag is part of a pair, put it onto a stack. Then write out the
  replacement for the first flag, and move past the flag characters. */

  if (f->length2 != 0) fstack[fstackcount++] = f;
  (void)fprintf(outfile, "%s", CS f->rep1);
  check_literal(f->rep1, &inliteraltext);
  p += f->length1;
  }

/* If there is anything left on the stack at the end of the string, there is a
missing flag partner. */

while (fstackcount > 0)
  {
  f = fstack[--fstackcount];
  error(7, f->flag2);
  }
}


/* End of para.c */