summaryrefslogtreecommitdiff
path: root/src/mmo/config_parse.cpp
blob: 6e434712c61434b0221a25667089736690fbe7ae (plain) (blame)
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
#include "config_parse.hpp"
//    config_parse.cpp - Framework for per-server config parsers.
//
//    Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program 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 3 of the License, or
//    (at your option) any later version.
//
//    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

#include <algorithm>

#include "../strings/xstring.hpp"
#include "../strings/zstring.hpp"

#include "../compat/borrow.hpp"

#include "../io/cxxstdio.hpp"
#include "../io/extract.hpp"
#include "../io/line.hpp"

#include "version.hpp"

#include "../poison.hpp"


namespace tmwa
{
bool is_comment(XString line)
{
    return not line or line.startswith("//"_s);
}

template<class ZS>
static
bool do_split(io::Spanned<ZS> line, io::Spanned<XString> *key, io::Spanned<ZS> *value)
{
    typename ZS::iterator colon = std::find(line.data.begin(), line.data.end(), ':');
    if (colon == line.data.end())
        return false;
    key->data = line.data.xislice_h(colon);
    key->span = line.span;
    key->span.end.column = key->span.begin.column + key->data.size() - 1;
    ++colon;
    value->data = line.data.xislice_t(colon);
    value->span = line.span;
    value->span.begin.column = value->span.end.column - value->data.size() + 1;
    return true;
}

template<class ZS>
static
io::Spanned<ZS> do_lstrip(io::Spanned<ZS> value)
{
    io::Spanned<ZS> rv;
    rv.data = value.data.lstrip();
    rv.span = value.span;
    rv.span.begin.column += (value.data.size() - rv.data.size());
    return rv;
}

static
io::Spanned<XString> do_rstrip(io::Spanned<XString> value)
{
    io::Spanned<XString> rv;
    rv.data = value.data.rstrip();
    rv.span = value.span;
    rv.span.end.column -= (value.data.size() - rv.data.size());
    return rv;
}

static
io::Spanned<XString> do_strip(io::Spanned<XString> value)
{
    return do_lstrip(do_rstrip(value));
}

template<class ZS>
inline
bool config_split_impl(io::Spanned<ZS> line, io::Spanned<XString> *key, io::Spanned<ZS> *value)
{
    // unconditionally fail if line contains control characters
    if (std::find_if(line.data.begin(), line.data.end(),
                [](unsigned char c) { return c < ' '; }
                ) != line.data.end())
        return false;

    if (!do_split(line, key, value))
        return false;
    *key = do_strip(*key);
    *value = do_lstrip(*value);
    return true;
}

// eventually this should go away
// currently the only real offenders are io::FD::open and *PRINTF
bool config_split(io::Spanned<ZString> line, io::Spanned<XString> *key, io::Spanned<ZString> *value)
{
    return config_split_impl(line, key, value);
}

static
bool check_version(io::Spanned<XString> avers, Borrowed<bool> valid)
{
    enum
    {
        GE, LE, GT, LT
    } cmp;

    if (avers.data.startswith(">="_s))
    {
        cmp = GE;
        avers.data = avers.data.xslice_t(2);
        avers.span.begin.column += 2;
    }
    else if (avers.data.startswith('>'))
    {
        cmp = GT;
        avers.data = avers.data.xslice_t(1);
        avers.span.begin.column += 1;
    }
    else if (avers.data.startswith("<="_s))
    {
        cmp = LE;
        avers.data = avers.data.xslice_t(2);
        avers.span.begin.column += 2;
    }
    else if (avers.data.startswith('<'))
    {
        cmp = LT;
        avers.data = avers.data.xslice_t(1);
        avers.span.begin.column += 1;
    }
    else
    {
        avers.span.error("Version check must begin with one of: '>=', '>', '<=', '<'"_s);
        *valid = false;
        return false;
    }

    Version vers;
    if (!extract(avers.data, &vers))
    {
        avers.span.error("Bad value"_s);
        *valid = false;
        return false;
    }
    switch (cmp)
    {
    case GE:
        return CURRENT_VERSION >= vers;
    case GT:
        return CURRENT_VERSION > vers;
    case LE:
        return CURRENT_VERSION <= vers;
    case LT:
        return CURRENT_VERSION < vers;
    }
    abort();
}

/// Master config parser. This handles 'import' and 'version-ge' etc.
/// Then it defers to the inferior parser for a line it does not understand.
///
/// Note: old-style 'version-ge: 1.2.3' etc apply to the rest of the file, but
/// new-style 'version: >= 1.2.3' apply only up to the next 'version:'
bool load_config_file(ZString filename, ConfigItemParser slave)
{
    io::LineReader in(filename);
    if (!in.is_open())
    {
        PRINTF("Unable to open file: %s\n"_fmt, filename);
        return false;
    }
    io::Line line_;
    bool rv = true;
    bool good_version = true;
    while (in.read_line(line_))
    {
        if (is_comment(line_.text))
            continue;
        auto line = io::respan(line_.to_span(), ZString(line_.text));
        io::Spanned<XString> key;
        io::Spanned<ZString> value;
        if (!config_split(line, &key, &value))
        {
            line.span.error("Bad config line"_s);
            rv = false;
            continue;
        }
        if (key.data == "version"_s)
        {
            if (value.data == "all"_s)
            {
                good_version = true;
            }
            else
            {
                good_version = true;
                while (good_version && value.data)
                {
                    ZString::iterator it = std::find(value.data.begin(), value.data.end(), ' ');
                    io::Spanned<XString> value_head;
                    value_head.data = value.data.xislice_h(it);
                    value_head.span = value.span;
                    value.data = value.data.xislice_t(it).lstrip();

                    value_head.span.end.column = value_head.span.begin.column + value_head.data.size() - 1;
                    value.span.begin.column = value.span.end.column - value.data.size() + 1;

                    good_version &= check_version(value_head, borrow(rv));
                }
            }
            continue;
        }
        if (!good_version)
        {
            continue;
        }
        if (key.data == "import"_s)
        {
            if (!load_config_file(value.data, slave))
            {
                value.span.error("Failed to include file"_s);
                rv = false;
            }
            continue;
        }
        else if (key.data == "version-lt"_s)
        {
            Version vers;
            if (!extract(value.data, &vers))
            {
                value.span.error("Bad value"_s);
                rv = false;
                continue;
            }
            if (CURRENT_VERSION < vers)
                continue;
            break;
        }
        else if (key.data == "version-le"_s)
        {
            Version vers;
            if (!extract(value.data, &vers))
            {
                rv = false;
                value.span.error("Bad value"_s);
                continue;
            }
            if (CURRENT_VERSION <= vers)
                continue;
            break;
        }
        else if (key.data == "version-gt"_s)
        {
            Version vers;
            if (!extract(value.data, &vers))
            {
                rv = false;
                value.span.error("Bad value"_s);
                continue;
            }
            if (CURRENT_VERSION > vers)
                continue;
            break;
        }
        else if (key.data == "version-ge"_s)
        {
            Version vers;
            if (!extract(value.data, &vers))
            {
                rv = false;
                value.span.error("Bad value"_s);
                continue;
            }
            if (CURRENT_VERSION >= vers)
                continue;
            break;
        }
        else
        {
            rv &= slave(key, value);
        }
        // nothing to see here, move along
    }
    return rv;
}
} // namespace tmwa