configglue.c 2.15 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

Jonathan Jenkins's avatar
Jonathan Jenkins committed
7
#include <assert.h>
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
#include <stdio.h>
#include <string.h>
#include "configglue.h"

int cfgp_lex_error (ParserParams * p, int lineno, int colno, const char * msg)
{
   char buf[512];
   snprintf (buf, sizeof(buf), "lexer error (line %i, column %i): %s", lineno,
           colno, msg);
   p->lexer_error_string = strdup (buf);
   p->lexer_error_code = 2;
   return -1;
}

int cfgp_parser_error(ParserParams * p, const char * err, 
      unsigned int first_line, unsigned int first_column,
      unsigned int last_line, unsigned int last_column)
{
   char location[128];
   char buf[512];

   if (first_line)
   {
      snprintf (location, sizeof(location), "%i:%i until %i:%i",
            first_line, first_column, last_line, last_column);
   }
   else if (last_line)
   {
      snprintf (location, sizeof(location), "line %i, column %i", 
            last_line, last_column);
   }
   else
   {
      strcpy (buf, "unknown location");
   }

   snprintf (buf, sizeof(buf), "Parser error (%s): %s", location, err);
   p->parser_error_code =1;
   p->parser_error_string = strdup (buf);
   return -1;
}

50
void cfgp_initparams (ParserParams * p, struct ConfigVTable * h)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
{
   p->configfile = h;
   p->stacktop = 0;
   p->sectionstack[0] = 0;
   p->parser_error_code = 0;
   p->parser_error_string = 0;
   p->lexer_error_code=0;
   p->lexer_error_string=0;
}

void cfgp_freeparams (ParserParams * p)
{
   free (p->lexer_error_string);
   free (p->parser_error_string);
}

int cfgp_parse_ok (const ParserParams * p, char * buf, int bufsize)
{
   /* doublecheck that if an error string is present, the error code is also
    * set */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
71 72
   assert(!p->lexer_error_string || p->lexer_error_code);
   assert(!p->parser_error_string || p->parser_error_code);
73 74 75

   if (p->lexer_error_code)
   {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
76
      strncpy (buf, p->lexer_error_string, bufsize);
77 78 79 80
      return 0;
   }
   if (p->parser_error_code)
   {
Jonathan Jenkins's avatar
Jonathan Jenkins committed
81
      strncpy (buf, p->parser_error_string, bufsize);
82 83 84 85
      return 0;
   }
   return 1;
}
Philip Carns's avatar
Philip Carns committed
86 87 88 89 90 91 92 93 94

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */