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

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

/* This module contains the main program and initialization functions. */

#include "xfpt.h"



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

static uschar *xfpt_filename = NULL;
static uschar *out_filename = NULL;


/*************************************************
*                  Usage                         *
*************************************************/

static void
usage(void)
{
(void)fprintf(stderr,
  "Usage: xfpt [-help]\n"
  "            [-o <output-file>]\n"
  "            [-S <share-directory>]\n"
  "            [-v]\n"
  "            [input-file]\n");
}




/*************************************************
*          Command line argument decoding        *
*************************************************/

/* Arguments: as for main()
   Returns:   TRUE if OK
*/

static BOOL
xfpt_decode_arg(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++)
  {
  uschar *arg = US argv[i];
  if (*arg != '-') break;
  if (Ustrcmp(arg, "-o") == 0)
    {
    out_filename = US argv[++i];
    if (out_filename == NULL) { usage(); return FALSE; }
    }
  else if (Ustrcmp(arg, "-S") == 0)
    {
    xfpt_share = US argv[++i];
    if (xfpt_share == NULL) { usage(); return FALSE; }
    }
  else if (Ustrcmp(arg, "-help") == 0 || Ustrcmp(arg, "--help") == 0)
    {
    usage();
    return FALSE;
    }
  else if (Ustrcmp(arg, "-v") == 0)
    {
    (void)fprintf(stdout, "xpft version %s\n", xfpt_version);
    exit(0);
    }
  else
    {
    (void)fprintf(stderr, "xfpt: unknown option \"%s\"\n", arg);
    usage();
    return FALSE;
    }
  }

/* Require there to be either 0 or 1 command line argument left. */

if (argc > i + 1)
  {
  usage();
  return FALSE;
  }

/* This will set NULL if there is no file name. If there is a file name and no
output file is specified, default it to the input name with a .xml extension. */

xfpt_filename = US argv[i];
if (xfpt_filename != NULL && out_filename == NULL)
  {
  uschar *p;
  int len = Ustrlen(xfpt_filename);
  out_filename = misc_malloc(len + 5);
  Ustrcpy(out_filename, xfpt_filename);
  if ((p = Ustrrchr(out_filename, '.')) != NULL) len = p - out_filename;
  Ustrcpy(out_filename + len, ".xml");
  }

return TRUE;
}



/*************************************************
*          Entry point and main program          *
*************************************************/


int
main(int argc, char **argv)
{
uschar *p, *q;

if (!xfpt_decode_arg(argc, argv)) return EXIT_FAILURE;

inbuffer = misc_malloc(INBUFFSIZE);
parabuffer = misc_malloc(PARABUFFSIZE);

/* Set up the first file */

istack = misc_malloc(sizeof(istackstr));
istack->prev = NULL;
istack->linenumber = 0;

if (xfpt_filename == NULL)
  {
  istack->file = stdin;
  Ustrcpy(istack->filename, US"(stdin)");
  }
else
  {
  Ustrcpy(istack->filename, xfpt_filename);
  istack->file = Ufopen(xfpt_filename, "rb");
  if (istack->file == NULL)
    error(0, istack->filename, strerror(errno));    /* Hard */
  }

/* Set up the output file. */

if (out_filename == NULL || Ustrcmp(out_filename, "-") == 0)
  {
  outfile = stdout;
  }
else
  {
  outfile = Ufopen(out_filename, "wb");
  if (outfile == NULL)
    error(0, out_filename, strerror(errno));   /* Hard error */
  }

/* Process the input */

while ((p = read_nextline()) != NULL)
  {
  if (*p == '.') dot_process(p); else switch (literal_state)
    {
    case LITERAL_LAYOUT:
    para_process(p);
    break;

    case LITERAL_TEXT:
    literal_process(p);
    break;

    case LITERAL_XML:
    (void)fprintf(outfile, "%s", CS p);
    break;

    default:
    case LITERAL_OFF:
    q = p;
    while (isspace(*q)) q++;
    if (*q != 0)
      {
      p = read_paragraph(p);
      (void)fprintf(outfile, "<");
      para_process(US"para&xfpt.rev;");
      (void)fprintf(outfile, ">\n");
      para_process(p);
      (void)fprintf(outfile, "</para>\n");
      }
    break;
    }
  }

/* Empty the pushed stack, close the output, and we are done */

while (pushed != 0)
  {
  para_process(pushed->string);
  (void)fprintf(outfile, "\n");
  pushed = pushed->next;
  }

(void)fclose(outfile);

return return_code;
}

/* End of xfpt.c */