~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/tokenizer.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License along
16
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include "tokenizer.h"
22
 
#include "string_util.h"
23
 
 
24
 
#include <stdbool.h>
25
 
#include <assert.h>
26
 
#include <string.h>
27
 
 
28
 
G_GNUC_CONST
29
 
static GQuark
30
 
tokenizer_quark(void)
31
 
{
32
 
        return g_quark_from_static_string("tokenizer");
33
 
}
34
 
 
35
 
static inline bool
36
 
valid_word_first_char(char ch)
37
 
{
38
 
        return g_ascii_isalpha(ch);
39
 
}
40
 
 
41
 
static inline bool
42
 
valid_word_char(char ch)
43
 
{
44
 
        return g_ascii_isalnum(ch) || ch == '_';
45
 
}
46
 
 
47
 
char *
48
 
tokenizer_next_word(char **input_p, GError **error_r)
49
 
{
50
 
        char *word, *input;
51
 
 
52
 
        assert(input_p != NULL);
53
 
        assert(*input_p != NULL);
54
 
 
55
 
        word = input = *input_p;
56
 
 
57
 
        if (*input == 0)
58
 
                return NULL;
59
 
 
60
 
        /* check the first character */
61
 
 
62
 
        if (!valid_word_first_char(*input)) {
63
 
                g_set_error(error_r, tokenizer_quark(), 0,
64
 
                            "Letter expected");
65
 
                return NULL;
66
 
        }
67
 
 
68
 
        /* now iterate over the other characters until we find a
69
 
           whitespace or end-of-string */
70
 
 
71
 
        while (*++input != 0) {
72
 
                if (g_ascii_isspace(*input)) {
73
 
                        /* a whitespace: the word ends here */
74
 
                        *input = 0;
75
 
                        /* skip all following spaces, too */
76
 
                        input = strchug_fast(input + 1);
77
 
                        break;
78
 
                }
79
 
 
80
 
                if (!valid_word_char(*input)) {
81
 
                        *input_p = input;
82
 
                        g_set_error(error_r, tokenizer_quark(), 0,
83
 
                                    "Invalid word character");
84
 
                        return NULL;
85
 
                }
86
 
        }
87
 
 
88
 
        /* end of string: the string is already null-terminated
89
 
           here */
90
 
 
91
 
        *input_p = input;
92
 
        return word;
93
 
}
94
 
 
95
 
static inline bool
96
 
valid_unquoted_char(char ch)
97
 
{
98
 
        return (unsigned char)ch > 0x20 && ch != '"' && ch != '\'';
99
 
}
100
 
 
101
 
char *
102
 
tokenizer_next_unquoted(char **input_p, GError **error_r)
103
 
{
104
 
        char *word, *input;
105
 
 
106
 
        assert(input_p != NULL);
107
 
        assert(*input_p != NULL);
108
 
 
109
 
        word = input = *input_p;
110
 
 
111
 
        if (*input == 0)
112
 
                return NULL;
113
 
 
114
 
        /* check the first character */
115
 
 
116
 
        if (!valid_unquoted_char(*input)) {
117
 
                g_set_error(error_r, tokenizer_quark(), 0,
118
 
                            "Invalid unquoted character");
119
 
                return NULL;
120
 
        }
121
 
 
122
 
        /* now iterate over the other characters until we find a
123
 
           whitespace or end-of-string */
124
 
 
125
 
        while (*++input != 0) {
126
 
                if (g_ascii_isspace(*input)) {
127
 
                        /* a whitespace: the word ends here */
128
 
                        *input = 0;
129
 
                        /* skip all following spaces, too */
130
 
                        input = strchug_fast(input + 1);
131
 
                        break;
132
 
                }
133
 
 
134
 
                if (!valid_unquoted_char(*input)) {
135
 
                        *input_p = input;
136
 
                        g_set_error(error_r, tokenizer_quark(), 0,
137
 
                                    "Invalid unquoted character");
138
 
                        return NULL;
139
 
                }
140
 
        }
141
 
 
142
 
        /* end of string: the string is already null-terminated
143
 
           here */
144
 
 
145
 
        *input_p = input;
146
 
        return word;
147
 
}
148
 
 
149
 
char *
150
 
tokenizer_next_string(char **input_p, GError **error_r)
151
 
{
152
 
        char *word, *dest, *input;
153
 
 
154
 
        assert(input_p != NULL);
155
 
        assert(*input_p != NULL);
156
 
 
157
 
        word = dest = input = *input_p;
158
 
 
159
 
        if (*input == 0)
160
 
                /* end of line */
161
 
                return NULL;
162
 
 
163
 
        /* check for the opening " */
164
 
 
165
 
        if (*input != '"') {
166
 
                g_set_error(error_r, tokenizer_quark(), 0,
167
 
                            "'\"' expected");
168
 
                return NULL;
169
 
        }
170
 
 
171
 
        ++input;
172
 
 
173
 
        /* copy all characters */
174
 
 
175
 
        while (*input != '"') {
176
 
                if (*input == '\\')
177
 
                        /* the backslash escapes the following
178
 
                           character */
179
 
                        ++input;
180
 
 
181
 
                if (*input == 0) {
182
 
                        /* return input-1 so the caller can see the
183
 
                           difference between "end of line" and
184
 
                           "error" */
185
 
                        *input_p = input - 1;
186
 
                        g_set_error(error_r, tokenizer_quark(), 0,
187
 
                                    "Missing closing '\"'");
188
 
                        return NULL;
189
 
                }
190
 
 
191
 
                /* copy one character */
192
 
                *dest++ = *input++;
193
 
        }
194
 
 
195
 
        /* the following character must be a whitespace (or end of
196
 
           line) */
197
 
 
198
 
        ++input;
199
 
        if (*input != 0 && !g_ascii_isspace(*input)) {
200
 
                *input_p = input;
201
 
                g_set_error(error_r, tokenizer_quark(), 0,
202
 
                            "Space expected after closing '\"'");
203
 
                return NULL;
204
 
        }
205
 
 
206
 
        /* finish the string and return it */
207
 
 
208
 
        *dest = 0;
209
 
        *input_p = strchug_fast(input);
210
 
        return word;
211
 
}
212
 
 
213
 
char *
214
 
tokenizer_next_param(char **input_p, GError **error_r)
215
 
{
216
 
        assert(input_p != NULL);
217
 
        assert(*input_p != NULL);
218
 
 
219
 
        if (**input_p == '"')
220
 
                return tokenizer_next_string(input_p, error_r);
221
 
        else
222
 
                return tokenizer_next_unquoted(input_p, error_r);
223
 
}