File: latte-html-cli.cxx

package info (click to toggle)
latte 2.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,064 kB
  • ctags: 1,223
  • sloc: sh: 7,506; cpp: 5,831; ansic: 662; perl: 628; makefile: 350; yacc: 196; lisp: 114
file content (335 lines) | stat: -rw-r--r-- 9,632 bytes parent folder | download
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
// Copyright 1998,1999 Zanshin Inc.                  <http://www.zanshin.com/>

// The contents of this file are subject to the Zanshin Public License Version
// 1.0 (the "License"); you may not use this file except in compliance with the
// License.  You should have received a copy of the License with Latte; see
// the file COPYING.  You may also obtain a copy of the License at
// <http://www.zanshin.com/ZPL.html>.
// 
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
// 
// The Original Code is Latte.
// 
// The Initial Developer of the Original Code is Zanshin, Inc.

#include <latte-html.h>
#include <latte-log.h>
#include <latte-fstream.h>
#include <getopt.h>
#include <cstring>
#include <unistd.h>

using namespace std;

static void
report_backtrace(const Latte_Error &err)
{
  const latte_deque<Latte_FileLoc>::const_iterator end = err.backtrace_end();

  for (latte_deque<Latte_FileLoc>::const_iterator i = err.backtrace_begin();
       i != end;
       ++i)
    cerr << "  called from " << *i << endl;
}

main(int argc, char **argv)
{
  deque<latte_string> loads;
  bool no_default = 0;
  bool strict = 0;
  bool fragment = 0;
  bool close_par = 0;
  unsigned long log_flags = 0;
  const char *lang = 0;
  const char *outfilename = 0;

  int option_index = 0;
  while (1) {
    static struct option options[] = {
      { "load", required_argument, 0, 'l' },
      { "no-default", no_argument, 0, 'n' },
      { "debug", optional_argument, 0, 'd' },
      { "version", no_argument, 0, 'v' },
      { "help", no_argument, 0, 'h' },
      { "lang", required_argument, 0, 'L' },
      { "strict", no_argument, 0, 's' },
      { "fragment", no_argument, 0, 'f' },
      { "output", required_argument, 0, 'o' },
      { "closepar", no_argument, 0, 'p' },
      { 0, 0, 0, 0 }
    };
    static struct debug_val {
      const char *name;
      unsigned long val;
    } debug_vals[] = {
      { "eval", latte_log_EVAL },
      { "mem", latte_log_MEM },
      { 0, 0 }
    };

    int opt = getopt_long(argc, argv, "po:fsL:l:nd:vh",
                          options, &option_index);

    if (opt == -1)
      break;

    switch (opt) {
     case 'p':
      close_par = 1;
      break;

     case 'o':
      outfilename = optarg;
      break;

     case 'f':
      fragment = 1;
      break;

     case 's':
      strict = 1;
      break;

     case 'l':
      loads.push_back(latte_string(optarg));
      break;

     case 'n':
      no_default = 1;
      break;

     case 'd': {
       const char *p = optarg;
       do {
         const char *comma = strchr(p, ',');
         if (!comma)
           comma = p + strlen(p);
         for (const debug_val *dv = debug_vals; dv->name; ++dv) {
           if ((strlen(dv->name) == (comma - p))
               && !strncmp(dv->name, p, (comma - p)))
             log_flags |= dv->val;
         }
         p = comma;
       } while (*p++);
     }
     break;

     case 'L':
      lang = optarg;
      break;

     case 'v':
      cout << "This is latte-html version " << latte_version << endl;
      exit(0);
      break;                    // not reached

     case 'h':
     case '?':
      cout << "This is latte-html version " << latte_version << endl
           << "Copyright 1998,1999 Zanshin Inc." << endl;

      cout << "Usage: latte-html [options] FILE" << endl << endl;
      cout << "options are:" << endl << endl;
      cout << " -v, --version        Print latte-html version and exit"
           << endl;
      cout << " -h, --help           Print this help and exit"
           << endl;
      cout << " -l, --load=LIBRARY   Pre-load named LIBRARY"
           << endl;
      cout << " -n, --no-default     Do not load default libraries from "
           << latte_pkgdatadir
           << endl;
      cout << " -o, --output=FILE    Place output in FILE"
           << endl;
      cout << " -f, --fragment       Produce an HTML fragment"
           << endl;
      cout << " -p, --closepar       Emit </p>s"
           << endl;
      cout << " -s, --strict         Warn about using deprecated HTML forms"
           << endl;
      cout << " -L, --lang=LANG      Set HTML language code"
           << endl;
      cout << " -d, --debug=FLAG     Turn on FLAG debug flag"
           << endl;
      cout << "                      (flags are eval, mem)"
           << endl;

      cout << endl;
      cout << "This software is licensed solely for use under"
           << endl
           << "the terms of the Zanshin Public License, available at"
           << endl
           << latte_url
           << endl;

      exit(0);
      break;                    // not reached

     default:
      cerr << "latte-html: invalid argument (see `latte-html --help')" << endl;
      exit(1);
    }
  }

  const char *filename;
  istream *file_stream;

  if (optind >= argc) {
    filename = "[standard input]";
    file_stream = &cin;
  } else {
    filename = argv[optind];

    if (!strcmp(filename, "-")) {
      filename = "[standard input]";
      file_stream = &cin;
    } else {
      file_stream = new ifstream(filename);

      if (file_stream->fail()) {
        cerr << "Could not open " << filename << endl;
        exit(1);
      }
    }
  }

  ostream *out_file_stream = &cout;

  if (outfilename) {
    unlink(outfilename);        // ignore errors
    out_file_stream = new ofstream(outfilename);
    if (out_file_stream->fail()) {
      cerr << "Could not create " << outfilename << endl;
      exit(1);
    }
  }

  try {

    latte_html(lang, strict, fragment, no_default, close_par,
               log_flags, loads.begin(), loads.end(),
               filename, *file_stream, *out_file_stream);

  } catch (const Latte_Closure::Useless &err) {
    cerr << endl
         << "Useless subexpression in call to "
         << err.opname()
         << " at "
         << err.fileloc()
         << endl << "(see Latte manual or " << latte_url << ")"
         << endl;
  } catch (const Latte_Operator::OutOfRange &err) {
    cerr << endl
         << "Argument "
         << (1 + err.argno())
         << " out of range in call to "
         << err.opname()
         << " at "
         << err.fileloc()
         << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Operator::IllegalAssignment &err) {
    cerr << endl
         << "Illegal named parameter assignment ("
         << err.varname()
         << ") in call to function ("
         << err.opname()
         << ") at "
         << err.fileloc()
         << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Operator::InsufficientArgs &err) {
    cerr << endl
         << "A function ("
         << err.opname()
         << ") was called with too few arguments at "
         << err.fileloc()
         << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Operator::BadType &err) {
    cerr << endl
         << "Bad type in argument "
         << (1 + err.argno())
         << " in call to "
         << err.opname()
         << " at "
         << err.fileloc()
         << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_VarRef::Undefined &err) {
    cerr << endl << "Variable \"" << err.name() << "\" is undefined at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Reader::NotIdentifier &err) {
    cerr << endl << "Not an identifier where expected at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Reader::UnexpectedClose &err) {
    cerr << endl << "Unexpected close-brace at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Reader::IncompleteString &err) {
    cerr << endl << "Incomplete string begins at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Reader::IncompleteGroup &err) {
    cerr << endl << "Incomplete group begins at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Reader::IncompleteAssignment &err) {
    cerr << endl << "Incomplete parameter assignment begins at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Reader::Incomplete &err) {
    cerr << endl << "Processing incomplete at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Reader::SyntaxError &err) {
    cerr << endl << "Some kind of syntax error at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Lambda::NoParamList &err) {
    cerr << endl << "Attempt to define a closure with no parameter list at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_FileError &err) {
    cerr << endl << "File error ";
    if (!err.errfile().empty())
      cerr << "(file " << err.errfile() << ") ";
    cerr << "at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_SysError &err) {
    cerr << endl << "System error at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (const Latte_Error &err) {
    cerr << endl << "Fatal error at ";
    cerr << err.fileloc() << endl;
    report_backtrace(err);
    exit(1);
  } catch (...) {
    cerr << endl << "Fatal error, location unknown" << endl;
    exit(1);
  };

  exit(0);
}