1/**
2 * Copyright (c) 2018, Timothy Stack
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of Timothy Stack nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31
32#include <string.h>
33
34#include "log_level.hh"
35
36log_level_t string2level(const char *levelstr, ssize_t len, bool exact)
37{
38    log_level_t retval = LEVEL_UNKNOWN;
39
40    if (len == (ssize_t)-1) {
41        len = strlen(levelstr);
42    }
43
44    if (((len == 1) || ((len > 1) && (levelstr[1] == ' '))) &&
45        (retval = abbrev2level(levelstr, 1)) != LEVEL_UNKNOWN) {
46        return retval;
47    }
48
49#   define YYCTYPE unsigned char
50#   define RET(tok) { \
51        return tok; \
52    }
53
54    const YYCTYPE *YYCURSOR = (const unsigned char *) levelstr;
55    const YYCTYPE *YYLIMIT = (const unsigned char *) levelstr + len;
56    const YYCTYPE *YYMARKER = YYCURSOR;
57    const YYCTYPE *debug_level = nullptr;
58
59#   define YYPEEK()    (YYCURSOR < YYLIMIT ? *YYCURSOR : 0)
60#   define YYSKIP()    ++YYCURSOR
61#   define YYBACKUP()  YYMARKER = YYCURSOR
62#   define YYRESTORE() YYCURSOR = YYMARKER
63#   define YYSTAGP(x)  x = YYCURSOR - 1
64
65    /*!stags:re2c format = 'const unsigned char *@@;'; */
66    loop:
67    /*!re2c
68     re2c:yyfill:enable = 0;
69     re2c:flags:input = custom;
70
71     EOF = "\x00";
72
73     EOF { RET(LEVEL_UNKNOWN); }
74     'trace' { RET(LEVEL_TRACE); }
75     'debug' [2-5]? @debug_level {
76         if (debug_level == nullptr) {
77             RET(LEVEL_DEBUG);
78         }
79         switch (*debug_level) {
80         case '2':
81             RET(LEVEL_DEBUG2);
82         case '3':
83             RET(LEVEL_DEBUG3);
84         case '4':
85             RET(LEVEL_DEBUG4);
86         case '5':
87             RET(LEVEL_DEBUG5);
88         default:
89             RET(LEVEL_DEBUG);
90         }
91     }
92     'info' { RET(LEVEL_INFO); }
93     'notice' { RET(LEVEL_NOTICE); }
94     'stats' { RET(LEVEL_STATS); }
95     'warn'|'warning' { RET(LEVEL_WARNING); }
96     'err'|'error' { RET(LEVEL_ERROR); }
97     'critical' { RET(LEVEL_CRITICAL); }
98     'severe' { RET(LEVEL_CRITICAL); }
99     'fatal' { RET(LEVEL_FATAL); }
100     * { goto loop; }
101
102     */
103}
104