1 /*
2     Simple text parsing library
3     Copyright (C) 2009-2014 Alexey Yakovenko
4 
5     This software is provided 'as-is', without any express or implied
6     warranty.  In no event will the authors be held liable for any damages
7     arising from the use of this software.
8 
9     Permission is granted to anyone to use this software for any purpose,
10     including commercial applications, and to alter it and redistribute it
11     freely, subject to the following restrictions:
12 
13     1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17 
18     2. Altered source versions must be plainly marked as such, and must not be
19      misrepresented as being the original software.
20 
21     3. This notice may not be removed or altered from any source distribution.
22 */
23 
24 #include <stdlib.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include "parser.h"
29 
30 // very basic parser, ripped from psynth, optimized, and extended to support
31 // quoted strings and extra special chars
32 int parser_line;
33 
34 void
parser_init(void)35 parser_init (void) {
36     parser_line = 1;
37 }
38 
39 const char *
skipws(const char * p)40 skipws (const char *p) {
41     while (*p <= ' ' && *p) {
42         if (*p == '\n') {
43             parser_line++;
44         }
45         p++;
46     }
47     if (!*p) {
48         return NULL;
49     }
50     return p;
51 }
52 
53 const char *
gettoken_ext(const char * p,char * tok,const char * specialchars)54 gettoken_ext (const char *p, char *tok, const char *specialchars) {
55     const char *c;
56     assert (p);
57     assert (tok);
58     int n = MAX_TOKEN-1;
59     if (!(p = skipws (p))) {
60         return NULL;
61     }
62     if (*p == '"') {
63         p++;
64         c = p;
65         while (n > 0 && *c && *c != '"') {
66             if (*c == '\n') {
67                 parser_line++;
68             }
69             if (*c == '\\' && (*(c+1) == '"' || *(c+1) == '\\')) {
70                 c++;
71             }
72             *tok++ = *c++;
73             n--;
74         }
75         if (*c) {
76             c++;
77         }
78         *tok = 0;
79         return c;
80     }
81     if (strchr (specialchars, *p)) {
82         *tok = *p;
83         tok[1] = 0;
84         return p+1;
85     }
86     c = p;
87     while (n > 0 && *c > ' ' && !strchr (specialchars, *c)) {
88         *tok++ = *c++;
89         n--;
90     }
91     *tok = 0;
92     return c;
93 }
94 
95 const char *
gettoken(const char * p,char * tok)96 gettoken (const char *p, char *tok) {
97     char specialchars[] = "{}();";
98     return gettoken_ext (p, tok, specialchars);
99 }
100 
101 const char *
gettoken_keyvalue(const char * p,char * key,char * val)102 gettoken_keyvalue (const char *p, char *key, char *val) {
103     char specialchars[] = "{}();=";
104     p = gettoken_ext (p, key, specialchars);
105     if (!p) {
106         return NULL;
107     }
108     p = gettoken_ext (p, val, specialchars);
109     if (!p || *val != '=') {
110         return NULL;
111     }
112     return gettoken_ext (p, val, specialchars);
113 }
114 
115 const char *
gettoken_warn_eof(const char * p,char * tok)116 gettoken_warn_eof (const char *p, char *tok) {
117     p = gettoken (p, tok);
118     if (!p) {
119         fprintf (stderr, "parser: unexpected eof at line %d\n", parser_line);
120     }
121     return p;
122 }
123 
124 const char *
gettoken_err_eof(const char * p,char * tok)125 gettoken_err_eof (const char *p, char *tok) {
126     p = gettoken (p, tok);
127     if (!p) {
128         fprintf (stderr, "parser: unexpected eof at line %d\n", parser_line);
129         exit (-1);
130     }
131     return p;
132 }
133 
134 char *
parser_escape_string(const char * in)135 parser_escape_string (const char *in) {
136     char *output;
137     size_t len = 0;
138     const char *p;
139     for (p = in; *p; p++, len++) {
140         if (*p == '"' || *p == '\\') {
141             len++;
142         }
143     }
144     output = malloc (len + 1);
145     char *out = output;
146     for (p = in; *p; p++) {
147         if (*p == '"' || *p == '\\') {
148             *out++ = '\\';
149         }
150         *out++ = *p;
151     }
152     *out = 0;
153     return output;
154 }
155 
156