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

« back to all changes in this revision

Viewing changes to src/util/Tokenizer.cxx

  • 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-2013 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.hxx"
 
22
#include "CharUtil.hxx"
 
23
#include "StringUtil.hxx"
 
24
#include "Error.hxx"
 
25
#include "Domain.hxx"
 
26
 
 
27
#include <glib.h>
 
28
 
 
29
#include <assert.h>
 
30
#include <string.h>
 
31
 
 
32
static constexpr Domain tokenizer_domain("tokenizer");
 
33
 
 
34
static inline bool
 
35
valid_word_first_char(char ch)
 
36
{
 
37
        return IsAlphaASCII(ch);
 
38
}
 
39
 
 
40
static inline bool
 
41
valid_word_char(char ch)
 
42
{
 
43
        return IsAlphaNumericASCII(ch) || ch == '_';
 
44
}
 
45
 
 
46
char *
 
47
Tokenizer::NextWord(Error &error)
 
48
{
 
49
        char *const word = input;
 
50
 
 
51
        if (*input == 0)
 
52
                return nullptr;
 
53
 
 
54
        /* check the first character */
 
55
 
 
56
        if (!valid_word_first_char(*input)) {
 
57
                error.Set(tokenizer_domain, "Letter expected");
 
58
                return nullptr;
 
59
        }
 
60
 
 
61
        /* now iterate over the other characters until we find a
 
62
           whitespace or end-of-string */
 
63
 
 
64
        while (*++input != 0) {
 
65
                if (IsWhitespaceOrNull(*input)) {
 
66
                        /* a whitespace: the word ends here */
 
67
                        *input = 0;
 
68
                        /* skip all following spaces, too */
 
69
                        input = strchug_fast(input + 1);
 
70
                        break;
 
71
                }
 
72
 
 
73
                if (!valid_word_char(*input)) {
 
74
                        error.Set(tokenizer_domain, "Invalid word character");
 
75
                        return nullptr;
 
76
                }
 
77
        }
 
78
 
 
79
        /* end of string: the string is already null-terminated
 
80
           here */
 
81
 
 
82
        return word;
 
83
}
 
84
 
 
85
static inline bool
 
86
valid_unquoted_char(char ch)
 
87
{
 
88
        return (unsigned char)ch > 0x20 && ch != '"' && ch != '\'';
 
89
}
 
90
 
 
91
char *
 
92
Tokenizer::NextUnquoted(Error &error)
 
93
{
 
94
        char *const word = input;
 
95
 
 
96
        if (*input == 0)
 
97
                return nullptr;
 
98
 
 
99
        /* check the first character */
 
100
 
 
101
        if (!valid_unquoted_char(*input)) {
 
102
                error.Set(tokenizer_domain, "Invalid unquoted character");
 
103
                return nullptr;
 
104
        }
 
105
 
 
106
        /* now iterate over the other characters until we find a
 
107
           whitespace or end-of-string */
 
108
 
 
109
        while (*++input != 0) {
 
110
                if (IsWhitespaceOrNull(*input)) {
 
111
                        /* a whitespace: the word ends here */
 
112
                        *input = 0;
 
113
                        /* skip all following spaces, too */
 
114
                        input = strchug_fast(input + 1);
 
115
                        break;
 
116
                }
 
117
 
 
118
                if (!valid_unquoted_char(*input)) {
 
119
                        error.Set(tokenizer_domain,
 
120
                                  "Invalid unquoted character");
 
121
                        return nullptr;
 
122
                }
 
123
        }
 
124
 
 
125
        /* end of string: the string is already null-terminated
 
126
           here */
 
127
 
 
128
        return word;
 
129
}
 
130
 
 
131
char *
 
132
Tokenizer::NextString(Error &error)
 
133
{
 
134
        char *const word = input, *dest = input;
 
135
 
 
136
        if (*input == 0)
 
137
                /* end of line */
 
138
                return nullptr;
 
139
 
 
140
        /* check for the opening " */
 
141
 
 
142
        if (*input != '"') {
 
143
                error.Set(tokenizer_domain, "'\"' expected");
 
144
                return nullptr;
 
145
        }
 
146
 
 
147
        ++input;
 
148
 
 
149
        /* copy all characters */
 
150
 
 
151
        while (*input != '"') {
 
152
                if (*input == '\\')
 
153
                        /* the backslash escapes the following
 
154
                           character */
 
155
                        ++input;
 
156
 
 
157
                if (*input == 0) {
 
158
                        /* return input-1 so the caller can see the
 
159
                           difference between "end of line" and
 
160
                           "error" */
 
161
                        --input;
 
162
                        error.Set(tokenizer_domain, "Missing closing '\"'");
 
163
                        return nullptr;
 
164
                }
 
165
 
 
166
                /* copy one character */
 
167
                *dest++ = *input++;
 
168
        }
 
169
 
 
170
        /* the following character must be a whitespace (or end of
 
171
           line) */
 
172
 
 
173
        ++input;
 
174
        if (!IsWhitespaceOrNull(*input)) {
 
175
                error.Set(tokenizer_domain,
 
176
                          "Space expected after closing '\"'");
 
177
                return nullptr;
 
178
        }
 
179
 
 
180
        /* finish the string and return it */
 
181
 
 
182
        *dest = 0;
 
183
        input = strchug_fast(input);
 
184
        return word;
 
185
}
 
186
 
 
187
char *
 
188
Tokenizer::NextParam(Error &error)
 
189
{
 
190
        if (*input == '"')
 
191
                return NextString(error);
 
192
        else
 
193
                return NextUnquoted(error);
 
194
}