aboutsummaryrefslogtreecommitdiff
path: root/utils.c
blob: 60e12eb60cdf4df974c33da25a2e94666bf82bae (plain)
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
//
// utils.c - Some helper code for debugging and error logging.
//

#include <ctype.h>
#include <err.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>

#include "utils.h"

//
// Helper function to skip past all spaces (and comments)
// Returns a pointer to the first non-space character.
//
const char *after_spaces(const char *str)
{
    // Skip whitespace and comments:
  skip_whitespace:
    switch (*str) {
        case ' ': case '\r': case '\n': case '\t': {
            ++str;
            goto skip_whitespace;
        }
        case '#': {
            while (*str && *str != '\n') ++str;
            goto skip_whitespace;
        }
        default: break;
    }
    return str;
}

//
// Return the first character after a valid BP name, or NULL if none is
// found.
//
const char *after_name(const char *str)
{
    if (*str == '|') return &str[1];
    if (*str == '^' || *str == '_' || *str == '$') {
        return (str[1] == *str) ? &str[2] : &str[1];
    }
    if (!isalpha(*str)) return NULL;
    for (++str; *str; ++str) {
        if (!(isalnum(*str) || *str == '-'))
            break;
    }
    return str;
}

//
// Check if a character is found and if so, move past it.
//
bool matchchar(const char **str, char c)
{
    const char *next = after_spaces(*str);
    if (*next == c) {
        *str = &next[1];
        return true;
    }
    return false;
}

//
// Check if a string is found and if so, move past it.
//
bool matchstr(const char **str, const char *target)
{
    const char *next = after_spaces(*str);
    if (strncmp(next, target, strlen(target)) == 0) {
        *str = &next[strlen(target)];
        return true;
    }
    return false;
}

//
// Process a string escape sequence for a character and return the
// character that was escaped.
// Set *end = the first character past the end of the escape sequence.
//
char unescapechar(const char *escaped, const char **end)
{
    size_t len = 1;
    unsigned char ret = (unsigned char)*escaped;
    switch (*escaped) {
        case 'a': ret = '\a'; break; case 'b': ret = '\b'; break;
        case 'n': ret = '\n'; break; case 'r': ret = '\r'; break;
        case 't': ret = '\t'; break; case 'v': ret = '\v'; break;
        case 'e': ret = '\033'; break; case '\\': ret = '\\'; break;
        case 'x': { // Hex
            static const unsigned char hextable[255] = {
                ['0']=0x10, ['1']=0x1, ['2']=0x2, ['3']=0x3, ['4']=0x4,
                ['5']=0x5, ['6']=0x6, ['7']=0x7, ['8']=0x8, ['9']=0x9,
                ['a']=0xa, ['b']=0xb, ['c']=0xc, ['d']=0xd, ['e']=0xe, ['f']=0xf,
                ['A']=0xa, ['B']=0xb, ['C']=0xc, ['D']=0xd, ['E']=0xe, ['F']=0xf,
            };
            if (hextable[(int)escaped[1]] && hextable[(int)escaped[2]]) {
                ret = (hextable[(int)escaped[1]] << 4) | (hextable[(int)escaped[2]] & 0xF);
                len = 3;
            }
            break;
        }
        case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': { // Octal
            ret = (unsigned char)(escaped[0] - '0');
            if ('0' <= escaped[1] && escaped[1] <= '7') {
                ++len;
                ret = (ret << 3) | (escaped[1] - '0');
                if ('0' <= escaped[2] && escaped[2] <= '7') {
                    ++len;
                    ret = (ret << 3) | (escaped[2] - '0');
                }
            }
            break;
        }
        default: {
            if (end) *end = escaped;
            return (char)0;
        }
    }
    if (end) *end = &escaped[len];
    return (char)ret;
}

//
// If the given argument is NULL, print the error message and exit with
// failure. Otherwise return the given argument.
//
void *check_nonnull(void *p, const char *err_msg, ...)
{
    if (p == NULL) {
        va_list args;
        va_start(args, err_msg);
        verr(EXIT_FAILURE, err_msg, args);
        va_end(args);
    }
    return p;
}

//
// If the given argument is negative, print the error message and exit with
// failure. Otherwise return the given argument.
//
int check_nonnegative(int i, const char *err_msg, ...)
{
    if (i < 0) {
        va_list args;
        va_start(args, err_msg);
        verr(EXIT_FAILURE, err_msg, args);
        va_end(args);
    }
    return i;
}

//
// Case-insensitive memory comparison
//
int memicmp(const void *v1, const void *v2, size_t n)
{
    int result = 0;
    const char *s1 = (const char*)v1, *s2 = (const char*)v2;
    while (n-- > 0 && (result = tolower(*(s1++)) - tolower(*(s2++))) == 0)
        ;
    return result;
}

//
// Free memory, but also set the pointer to NULL for safety
//
void xfree(void *p)
{
    if (*(void**)p == NULL)
        errx(EXIT_FAILURE, "attempt to free(NULL)");
    free(*(void**)p);
    *((void**)p) = NULL;
}

// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1