summaryrefslogtreecommitdiff
path: root/server/src/macroparser.cc
blob: fe7f6e313c322a9cf6d866eff61486011090d4cc (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            macroparser.cc
 *
 *  Wed Jun  4 11:57:38 CEST 2008
 *  Copyright 2008 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pracro is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "debug.h"
#include "macroparser.h"
#include "configuration.h"

#include <stdio.h>

// For assert
#include <assert.h>

// For open and friends
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

// For vprintf and friends
#include <stdarg.h>

#ifndef XML
// For XML
#include <config.h>
#endif/*XML*/

#include <errno.h>
#include <string.h>

#include "exception.h"

void MacroParser::error(const char* fmt, ...)
{
  ERR_LOG(macro, "Error in MacroParser: ");

  {
    va_list argp;
    va_start(argp, fmt);
    ERR_LOG_VA(macro, fmt, argp);
    va_end(argp);

    fprintf(stderr, "\n");
  }

  {
    char *p;
    va_list argp;
    va_start(argp, fmt);
    if(vasprintf(&p, fmt, argp) != -1) {
      throw Exception("Error in MacroParser: " + std::string(p));
      free(p);
    }
    va_end(argp);
  }

}

MacroParser::MacroParser(std::string macrofile)
{
  state = UNDEFINED;
  m = NULL;
  current_map = NULL;
  current_script = NULL;

  file = macrofile;

  DEBUG(macro, "Using macro file: %s\n", file.c_str());

  fd = open(file.c_str(), O_RDONLY);
  if(fd == -1) error("Could not open file %s", file.c_str());
}

MacroParser::~MacroParser()
{
  if(fd != -1) close(fd);
  if(m) delete m;
}

void MacroParser::characterData(std::string &data)
{
  if(state == RESUME) {
    assert(m); // No macro present!
    m->resume.attributes["format"].append(data);
  }

  if(state == MAP) {
    assert(current_map); // No map present!
    current_map->attributes["lua"].append(data);
  }

  if(state == SCRIPT) {
    assert(current_script); // No script present!
    current_script->attributes["code"].append(data);
  }
}

void MacroParser::startTag(std::string name, std::map< std::string, std::string> attributes)
{
  // Create macro and enable parsing of queries, maps and widgets
  if(name == "macro") {
    if(state != UNDEFINED) error("macro found not root tag.");
    state = MACRO;

    assert(!m); // A Macro has already been allocated, cannot create macro!
    
    m = new Macro();
    m->attributes = attributes;

    return;
  }

  // Enable resume parsing
  if(name == "resume") {
    if(state != MACRO) error("resume found outside macro.");
    state = RESUME;

    m->resume.attributes = attributes;

    assert(m); // No macro is currently available, cannot create resume!

    return;
  }

  // Enable Query parsing
  if(name == "queries") {
    if(state != MACRO) error("queries found outside macro.");
    state = QUERIES;
    
    assert(m); // No macro is currently available, cannot create queries!

    return;
  }

  // Create Query
  if(name == "query") {
    if(state != QUERIES) error("query found outside queries.");
    state = QUERY;

    assert(m); // No macro is currently available, cannot create query!

    Query q;
    q.attributes = attributes;
    m->queries.push_back(q);

    return;
  }

  // Enable Map parsing
  if(name == "maps") {
    if(state != MACRO) error("maps found outside macro.");
    state = MAPS;

    assert(m); // No macro is currently available, cannot create maps!

    return;
  }

  // Create Query
  if(name == "map") {
    if(state != MAPS) error("map found outside maps.");
    state = MAP;

    assert(m); // No macro is currently available, cannot create map!

    Map map;
    map.attributes = attributes;
    m->maps.push_back(map);
    current_map = &(m->maps.back());

    return;
  }

  // Enable script parsing
  if(name == "scripts") {
    if(state != MACRO) error("scripts found outside macro.");
    state = SCRIPTS;

    assert(m); // No macro is currently available, cannot create maps!

    return;
  }

  // Create Query
  if(name == "script") {
    if(state != SCRIPTS) error("script found outside scripts.");
    state = SCRIPT;

    assert(m); // No macro is currently available, cannot create map!

    Script s;
    s.attributes = attributes;
    m->scripts.push_back(s);
    current_script = &(m->scripts.back());

    return;
  }

  // Enable widget parsing
  if(name == "widgets") {

    if(state != MACRO) error("widgets found outside macro.");
    state = WIDGETS;

    assert(m); // No macro is currently available, cannot create widgets!
  
    m->widgets.attributes = attributes;
    m->widgets.attributes["tagname"] = name;

    Widget *current = &(m->widgets);
    widgetstack.push_back(current);

    return;
  }

  // TODO: We need to parse some (maybe even all) widgets in order to
  //       make db lookup of the previous values.
  if(state == WIDGETS) {

    assert(widgetstack.size()); // Widget stack is empty, cannot create!

    Widget w;
    w.attributes = attributes;
    w.attributes["tagname"] = name;

    Widget *parent = widgetstack.back();
    parent->widgets.push_back(w);
    
    Widget *current = &(parent->widgets.back());
    widgetstack.push_back(current);

    return;
  }


  // Handle include
  if(name == "include") {
    return;
  }

  error("Unknown/illegal tag: %s", name.c_str());
}

void MacroParser::endTag(std::string name)
{
  if(name == "macro") {
    state = UNDEFINED;
  }
  if(name == "resume") state = MACRO;
  if(name == "queries") state = MACRO;
  if(name == "query") state = QUERIES;
  if(name == "maps") state = MACRO;
  if(name == "map") {
    current_map = NULL;
    state = MAPS;
  }
  if(name == "scripts") state = MACRO;
  if(name == "script") {
    current_script = NULL;
    state = SCRIPTS;
  }
  if(name == "widgets") state = MACRO;
  
  if(state == WIDGETS) {
    assert(widgetstack.size()); // Widget stack is empty, cannot pop!
    widgetstack.pop_back();
    if(widgetstack.size() == 0) state = MACRO;
  }
}

int MacroParser::readData(char *data, size_t size)
{
  if(fd == -1) {
    ERR_LOG(macro, "Invalid file descriptor.\n");
    return 0;
  }
  ssize_t r = read(fd, data, size);
  if(r == -1) {
    ERR_LOG(macro, "Could not read...%s\n", strerror(errno));
    return 0;
  }
  return r;
}

void MacroParser::parseError(const char *buf, size_t len, std::string error, int lineno)
{
  ERR_LOG(macro, "MacroParser[%s] error at line %d: %s\n",
          file.c_str(), lineno, error.c_str());
  ERR_LOG(macro, "\tBuffer %u bytes: [", len);
  if(fwrite(buf, len, 1, stderr) != len) {}
  ERR_LOG(macro, "]\n");

  char *slineno;
  if(asprintf(&slineno, " at line %d\n", lineno) != -1) {
    throw Exception(error + slineno);
    free(slineno);
  }
}

Macro *MacroParser::getMacro()
{
  return m;
}

#ifdef TEST_MACROPARSER

#define XMLFILE "/tmp/test_macroheaderparser.xml"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <memory.h>

static char xml[] =
"<?xml version='1.0' encoding='UTF-8'?>\n"
"<macro name=\"testmacro\" version=\"1.0\">\n"
" <resume/>\n"
" <queries/>\n"
" <maps/>\n"
" <scripts/>\n"
" <widgets/>\n"
"</macro>"
;

static char xml_nonmacro[] =
"<?xml version='1.0' encoding='UTF-8'?>\n"
"<dims name=\"testmacro\" version=\"1.0\">\n"
" <sometag/>\n"
" <someothertag/>\n"
"</dims>"
;

static char xml_fail[] =
"<?xml version='1.0' encoding='UTF-8'?>\n"
"<macro name\"testmacro\" version=\"1.0\">\n"
" <sometag/>\n"
" <someothertag/>\n"
"</macro>"
;

int main()
{
  FILE *fp = fopen(XMLFILE, "w");
  if(!fp) {
    printf("Could not write to %s\n", XMLFILE);
    return 1;
  }
  fprintf(fp, xml);
  fclose(fp);

  {
    // Test parsing of correct macro xml data.
    MacroParser parser(XMLFILE);
    try {
      parser.parse();
    } catch(Exception &e) {
      printf("Failed to parse: %s\n", e.what());
      return 1;
    }
  }  

  fp = fopen(XMLFILE, "w");
  if(!fp) {
    printf("Could not write to %s\n", XMLFILE);
    return 1;
  }
  fprintf(fp, xml_nonmacro);
  fclose(fp);

  // Test parsing of correct xml data, but not macro (should throw an exception).
  {
    MacroParser parser(XMLFILE);
    try {
      parser.parse();
    } catch(Exception &e) {
      printf("Failed to parse: %s\n", e.what());
      goto onandon;
    }
    return 1;
  }
  onandon:

  fp = fopen(XMLFILE, "w");
  if(!fp) {
    printf("Could not write to %s\n", XMLFILE);
    return 1;
  }
  fprintf(fp, xml_fail);
  fclose(fp);

  // Test parsing of invalid xml data (should throw an exception).
  {
    MacroParser parser(XMLFILE);
    try {
      parser.parse();
    } catch(Exception &e) {
      printf("Failed to parse: %s\n", e.what());
      goto yetonandon;
    }
    return 1;
  }
  yetonandon:

  unlink(XMLFILE);

  return 0;
}

#endif/*TEST_MACROPARSER*/