diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/argconfig.c | 6 | ||||
-rw-r--r-- | util/argconfig.h | 1 | ||||
-rw-r--r-- | util/base64.c | 2 | ||||
-rw-r--r-- | util/base64.h | 1 | ||||
-rw-r--r-- | util/cleanup.c | 1 | ||||
-rw-r--r-- | util/cleanup.h | 1 | ||||
-rw-r--r-- | util/json.c | 406 | ||||
-rw-r--r-- | util/json.h | 123 | ||||
-rw-r--r-- | util/meson.build | 3 | ||||
-rw-r--r-- | util/parser.c | 284 | ||||
-rw-r--r-- | util/parser.h | 34 | ||||
-rw-r--r-- | util/suffix.c | 1 | ||||
-rw-r--r-- | util/suffix.h | 1 |
13 files changed, 67 insertions, 797 deletions
diff --git a/util/argconfig.c b/util/argconfig.c index 59d8f1d..d09ba9d 100644 --- a/util/argconfig.c +++ b/util/argconfig.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright 2014 PMC-Sierra, Inc. * @@ -135,7 +136,7 @@ void argconfig_print_help(const char *program_desc, return; printf("\n\033[1mOptions:\033[0m\n"); - for (s = options; (s->option != NULL) && (s != NULL); s++) + for (s = options; (s != NULL) && (s->option != NULL); s++) show_option(s); } @@ -528,7 +529,8 @@ void argconfig_register_help_func(argconfig_help_func * f) for (i = 0; i < MAX_HELP_FUNC; i++) { if (help_funcs[i] == NULL) { help_funcs[i] = f; - help_funcs[i + 1] = NULL; + if (i < MAX_HELP_FUNC - 1) + help_funcs[i + 1] = NULL; break; } } diff --git a/util/argconfig.h b/util/argconfig.h index a11334b..de715b1 100644 --- a/util/argconfig.h +++ b/util/argconfig.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * * Copyright 2014 PMC-Sierra, Inc. diff --git a/util/base64.c b/util/base64.c index e386c11..07f975c 100644 --- a/util/base64.c +++ b/util/base64.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * base64.c - RFC4648-compliant base64 encoding * @@ -22,6 +23,7 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <sys/types.h> static const char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; diff --git a/util/base64.h b/util/base64.h index 609a877..c0f62e2 100644 --- a/util/base64.h +++ b/util/base64.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef _BASE64_H #define _BASE64_H diff --git a/util/cleanup.c b/util/cleanup.c index 0d5d910..d6ac7c6 100644 --- a/util/cleanup.c +++ b/util/cleanup.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later #include <stdlib.h> #include "cleanup.h" diff --git a/util/cleanup.h b/util/cleanup.h index 89a4984..575a25d 100644 --- a/util/cleanup.h +++ b/util/cleanup.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef __CLEANUP_H #define __CLEANUP_H diff --git a/util/json.c b/util/json.c index df0aefb..fd52861 100644 --- a/util/json.c +++ b/util/json.c @@ -1,407 +1,19 @@ -#include <stdlib.h> -#include <string.h> +// SPDX-License-Identifier: GPL-2.0-or-later #include <stdio.h> -#include <errno.h> -#include <stdarg.h> -#include "json.h" - -static inline void fail_and_notify(void) -{ - fprintf(stderr, "Allocation of memory for json object failed, aborting.\n"); - abort(); -} - -struct json_object *json_create_object(void) -{ - void *test = calloc(1, sizeof(struct json_object)); - if (!test) - fail_and_notify(); - return test; -} - -struct json_object *json_create_array(void) -{ - void *test = calloc(1, sizeof(struct json_object)); - if (!test) - fail_and_notify(); - return test; -} - -static struct json_pair *json_create_pair(const char *name, struct json_value *value) -{ - struct json_pair *pair = malloc(sizeof(struct json_pair)); - if (pair) { - pair->name = strdup(name); - pair->value = value; - - value->parent_type = JSON_PARENT_TYPE_PAIR; - value->parent_pair = pair; - } else - fail_and_notify(); - - return pair; -} - -static struct json_value *json_create_value_int(long long number) -{ - struct json_value *value = malloc(sizeof(struct json_value)); - - if (value) { - value->type = JSON_TYPE_INTEGER; - value->integer_number = number; - } else - fail_and_notify(); - - return value; -} - -static struct json_value *json_create_value_uint(unsigned long long number) -{ - struct json_value *value = malloc(sizeof(struct json_value)); - - if (value) { - value->type = JSON_TYPE_UINT; - value->uint_number = number; - } else - fail_and_notify(); - - return value; -} - -static struct json_value *json_create_value_float(long double number) -{ - struct json_value *value = malloc(sizeof(struct json_value)); - - if (value) { - value->type = JSON_TYPE_FLOAT; - value->float_number = number; - } else - fail_and_notify(); - return value; -} +#include "json.h" -static char *strdup_escape(const char *str) +struct json_object *util_json_object_new_double(long double d) { - const char *input = str; - char *p, *ret; - int escapes; + struct json_object *obj; + char *str; - if (!strlen(str)) + if (asprintf(&str, "%Lf", d) < 0) return NULL; - escapes = 0; - while ((input = strpbrk(input, "\\\"")) != NULL) { - escapes++; - input++; - } - - p = ret = malloc(strlen(str) + escapes + 1); - if (!ret) - fail_and_notify(); - - while (*str) { - if (*str == '\\' || *str == '\"') - *p++ = '\\'; - *p++ = *str++; - } - *p = '\0'; - - return ret; -} - -/* - * Valid JSON strings must escape '"' and '/' with a preceding '/' - */ -static struct json_value *json_create_value_string(const char *str) -{ - struct json_value *value = malloc(sizeof(struct json_value)); - - if (value) { - value->type = JSON_TYPE_STRING; - value->string = strdup_escape(str ? str : "(null)"); - if (!value->string) { - free(value); - value = NULL; - return value; - } - } - if (!value) - fail_and_notify(); - - return value; -} - -static struct json_value *json_create_value_object(struct json_object *obj) -{ - struct json_value *value = malloc(sizeof(struct json_value)); - - if (value) { - value->type = JSON_TYPE_OBJECT; - value->object = obj; - obj->parent = value; - } else - fail_and_notify(); + obj = json_object_new_string(str); - return value; -} - -static struct json_value *json_create_value_array(struct json_object *array) -{ - struct json_value *value = malloc(sizeof(struct json_value)); - - if (value) { - value->type = JSON_TYPE_ARRAY; - value->array = array; - array->parent = value; - } else - fail_and_notify(); - - return value; -} - -static void json_free_pair(struct json_pair *pair); -static void json_free_value(struct json_value *value); + free(str); + return obj; -void json_free_object(struct json_object *obj) -{ - int i; - - for (i = 0; i < obj->pair_cnt; i++) - json_free_pair(obj->pairs[i]); - free(obj->pairs); - free(obj); -} - -void json_free_array(struct json_object *array) -{ - int i; - - for (i = 0; i < array->value_cnt; i++) - json_free_value(array->values[i]); - free(array->values); - free(array); -} - -static void json_free_pair(struct json_pair *pair) -{ - json_free_value(pair->value); - free(pair->name); - free(pair); -} - -static void json_free_value(struct json_value *value) -{ - switch (value->type) { - case JSON_TYPE_STRING: - free(value->string); - break; - case JSON_TYPE_OBJECT: - json_free_object(value->object); - break; - case JSON_TYPE_ARRAY: - json_free_array(value->array); - break; - } - free(value); -} - -static int json_array_add_value(struct json_object *array, struct json_value *value) -{ - struct json_value **values = realloc(array->values, - sizeof(struct json_value *) * (array->value_cnt + 1)); - - if (!values) - return ENOMEM; - values[array->value_cnt] = value; - array->value_cnt++; - array->values = values; - - value->parent_type = JSON_PARENT_TYPE_ARRAY; - value->parent_array = array; - return 0; -} - -static int json_object_add_pair(struct json_object *obj, struct json_pair *pair) -{ - struct json_pair **pairs = realloc(obj->pairs, - sizeof(struct json_pair *) * (obj->pair_cnt + 1)); - if (!pairs) - return ENOMEM; - pairs[obj->pair_cnt] = pair; - obj->pair_cnt++; - obj->pairs = pairs; - - pair->parent = obj; - return 0; -} - -int json_object_add_value_type(struct json_object *obj, const char *name, int type, ...) -{ - struct json_value *value; - struct json_pair *pair; - va_list args; - int ret; - - va_start(args, type); - if (type == JSON_TYPE_STRING) - value = json_create_value_string(va_arg(args, char *)); - else if (type == JSON_TYPE_INTEGER) - value = json_create_value_int(va_arg(args, long long)); - else if (type == JSON_TYPE_UINT) - value = json_create_value_uint(va_arg(args, unsigned long long)); - else if (type == JSON_TYPE_FLOAT) - value = json_create_value_float(va_arg(args, long double)); - else if (type == JSON_TYPE_OBJECT) - value = json_create_value_object(va_arg(args, struct json_object *)); - else - value = json_create_value_array(va_arg(args, struct json_object *)); - va_end(args); - - if (!value) - return ENOMEM; - - pair = json_create_pair(name, value); - if (!pair) { - json_free_value(value); - return ENOMEM; - } - ret = json_object_add_pair(obj, pair); - if (ret) { - json_free_pair(pair); - return ENOMEM; - } - return 0; -} - -static void json_print_array(struct json_object *array, void *); -int json_array_add_value_type(struct json_object *array, int type, ...) -{ - struct json_value *value; - va_list args; - int ret; - - va_start(args, type); - if (type == JSON_TYPE_STRING) - value = json_create_value_string(va_arg(args, char *)); - else if (type == JSON_TYPE_INTEGER) - value = json_create_value_int(va_arg(args, long long)); - else if (type == JSON_TYPE_UINT) - value = json_create_value_uint(va_arg(args, unsigned long long)); - else if (type == JSON_TYPE_FLOAT) - value = json_create_value_float(va_arg(args, double)); - else if (type == JSON_TYPE_OBJECT) - value = json_create_value_object(va_arg(args, struct json_object *)); - else - value = json_create_value_array(va_arg(args, struct json_object *)); - va_end(args); - - if (!value) - return ENOMEM; - - ret = json_array_add_value(array, value); - if (ret) { - json_free_value(value); - return ENOMEM; - } - return 0; -} - -static int json_value_level(struct json_value *value); -static int json_pair_level(struct json_pair *pair); -static int json_array_level(struct json_object *array); -static int json_object_level(struct json_object *object) -{ - if (object->parent == NULL) - return 0; - return json_value_level(object->parent); -} - -static int json_pair_level(struct json_pair *pair) -{ - return json_object_level(pair->parent) + 1; -} - -static int json_array_level(struct json_object *array) -{ - return json_value_level(array->parent); -} - -static int json_value_level(struct json_value *value) -{ - if (value->parent_type == JSON_PARENT_TYPE_PAIR) - return json_pair_level(value->parent_pair); - else - return json_array_level(value->parent_array) + 1; -} - -static void json_print_level(int level, void *out) -{ - while (level-- > 0) - printf(" "); -} - -static void json_print_pair(struct json_pair *pair, void *); -static void json_print_array(struct json_object *array, void *); -static void json_print_value(struct json_value *value, void *); -void json_print_object(struct json_object *obj, void *out) -{ - int i; - - printf("{\n"); - for (i = 0; i < obj->pair_cnt; i++) { - if (i > 0) - printf(",\n"); - json_print_pair(obj->pairs[i], out); - } - printf("\n"); - json_print_level(json_object_level(obj), out); - printf("}"); -} - -static void json_print_pair(struct json_pair *pair, void *out) -{ - json_print_level(json_pair_level(pair), out); - printf("\"%s\" : ", pair->name); - json_print_value(pair->value, out); -} - -static void json_print_array(struct json_object *array, void *out) -{ - int i; - - printf("[\n"); - for (i = 0; i < array->value_cnt; i++) { - if (i > 0) - printf(",\n"); - json_print_level(json_value_level(array->values[i]), out); - json_print_value(array->values[i], out); - } - printf("\n"); - json_print_level(json_array_level(array), out); - printf("]"); -} - -static void json_print_value(struct json_value *value, void *out) -{ - switch (value->type) { - case JSON_TYPE_STRING: - printf( "\"%s\"", value->string); - break; - case JSON_TYPE_INTEGER: - printf( "%lld", value->integer_number); - break; - case JSON_TYPE_UINT: - printf( "%llu", value->uint_number); - break; - case JSON_TYPE_FLOAT: - printf( "%.0Lf", value->float_number); - break; - case JSON_TYPE_OBJECT: - json_print_object(value->object, out); - break; - case JSON_TYPE_ARRAY: - json_print_array(value->array, out); - break; - } } diff --git a/util/json.h b/util/json.h index 2ef8632..2b8022a 100644 --- a/util/json.h +++ b/util/json.h @@ -1,84 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef __JSON__H #define __JSON__H -struct json_object; -struct json_pair; - -#define JSON_TYPE_STRING 0 -#define JSON_TYPE_INTEGER 1 -#define JSON_TYPE_FLOAT 2 -#define JSON_TYPE_OBJECT 3 -#define JSON_TYPE_ARRAY 4 -#define JSON_TYPE_UINT 5 -#define JSON_PARENT_TYPE_PAIR 0 -#define JSON_PARENT_TYPE_ARRAY 1 -struct json_value { - int type; - union { - long long integer_number; - unsigned long long uint_number; - long double float_number; - char *string; - struct json_object *object; - struct json_object *array; - }; - int parent_type; - union { - struct json_pair *parent_pair; - struct json_object *parent_array; - }; -}; - -struct json_object { - struct json_value **values; - int value_cnt; - struct json_pair **pairs; - int pair_cnt; - struct json_value *parent; -}; - -struct json_pair { - char *name; - struct json_value *value; - struct json_object *parent; -}; - -struct json_object *json_create_object(void); -struct json_object *json_create_array(void); - -void json_free_object(struct json_object *obj); -void json_free_array(struct json_object *array); - -int json_object_add_value_type(struct json_object *obj, const char *name, int type, ...); -#define json_object_add_value_int(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_INTEGER, (long long) (val)) -#define json_object_add_value_uint(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_UINT, (unsigned long long) (val)) -#define json_object_add_value_uint64 json_object_add_value_uint -#define json_object_add_value_float(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_FLOAT, (val)) -#define json_object_add_value_string(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_STRING, (val)) -#define json_object_add_value_object(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_OBJECT, (val)) -#define json_object_add_value_array(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_ARRAY, (val)) -int json_array_add_value_type(struct json_object *array, int type, ...); -#define json_array_add_value_int(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_INTEGER, (val)) -#define json_array_add_value_uint(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_UINT, (val)) -#define json_array_add_value_float(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_FLOAT, (val)) -#define json_array_add_value_string(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_STRING, (val)) -#define json_array_add_value_object(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_OBJECT, (val)) -#define json_array_add_value_array(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_ARRAY, (val)) - -#define json_array_last_value_object(obj) \ - (obj->values[obj->value_cnt - 1]->object) +#include <json.h> + +/* Wrappers around json-c's API */ + +#define json_create_object(o) json_object_new_object(o) +#define json_create_array(a) json_object_new_array(a) +#define json_free_object(o) json_object_put(o) +#define json_free_array(a) json_object_put(a) +#define json_object_add_value_uint(o, k, v) \ + json_object_object_add(o, k, json_object_new_int(v)) +#define json_object_add_value_int(o, k, v) \ + json_object_object_add(o, k, json_object_new_int(v)) +#ifdef CONFIG_JSONC_14 +#define json_object_add_value_uint64(o, k, v) \ + json_object_object_add(o, k, json_object_new_uint64(v)) +#else +#define json_object_add_value_uint64(o, k, v) \ + if ((v) > UINT_MAX) { \ + fprintf(stderr, "Value overflow in %s", k); \ + json_object_object_add(o, k, json_object_new_int(-1)); \ + } else \ + json_object_object_add(o, k, json_object_new_int(v)) +#endif +#define json_object_add_value_double(o, k, v) \ + json_object_object_add(o, k, util_json_object_new_double(v)) +#define json_object_add_value_float(o, k, v) \ + json_object_object_add(o, k, json_object_new_double(v)) +#define json_object_add_value_string(o, k, v) \ + json_object_object_add(o, k, json_object_new_string(v)) +#define json_object_add_value_array(o, k, v) \ + json_object_object_add(o, k, v) +#define json_object_add_value_object(o, k, v) \ + json_object_object_add(o, k, v) +#define json_array_add_value_object(o, k) \ + json_object_array_add(o, k) +#define json_array_add_value_string(o, v) \ + json_object_array_add(o, json_object_new_string(v)) +#define json_print_object(o, u) \ + printf("%s", json_object_to_json_string_ext(o, \ + JSON_C_TO_STRING_PRETTY | \ + JSON_C_TO_STRING_NOSLASHESCAPE)) + +struct json_object *util_json_object_new_double(long double d); -void json_print_object(struct json_object *obj, void *); #endif diff --git a/util/meson.build b/util/meson.build index e525f0e..a999b80 100644 --- a/util/meson.build +++ b/util/meson.build @@ -1,8 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + sources += [ 'util/argconfig.c', 'util/cleanup.c', 'util/json.c', - 'util/parser.c', 'util/suffix.c', 'util/base64.c', ] diff --git a/util/parser.c b/util/parser.c deleted file mode 100644 index bd74822..0000000 --- a/util/parser.c +++ /dev/null @@ -1,284 +0,0 @@ -/* - * lib/parser.c - simple parser for mount, etc. options. - * - * This source code is licensed under the GNU General Public License, - * Version 2. See the file COPYING for more details. - */ - -#include <ctype.h> -#include <string.h> -#include <stdlib.h> -#include <stdbool.h> -#include <errno.h> -#include <limits.h> -#include <sys/types.h> -#include "parser.h" - -/** - * match_one: - Determines if a string matches a simple pattern - * @s: the string to examine for presence of the pattern - * @p: the string containing the pattern - * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match - * locations. - * - * Description: Determines if the pattern @p is present in string @s. Can only - * match extremely simple token=arg style patterns. If the pattern is found, - * the location(s) of the arguments will be returned in the @args array. - */ -static int match_one(char *s, const char *p, substring_t args[]) -{ - char *meta; - int argc = 0; - - if (!p) - return 1; - - while(1) { - int len = -1; - meta = strchr(p, '%'); - if (!meta) - return strcmp(p, s) == 0; - - if (strncmp(p, s, meta-p)) - return 0; - - s += meta - p; - p = meta + 1; - - if (isdigit(*p)) - len = strtoul(p, (char **) &p, 10); - else if (*p == '%') { - if (*s++ != '%') - return 0; - p++; - continue; - } - - if (argc >= MAX_OPT_ARGS) - return 0; - - args[argc].from = s; - switch (*p++) { - case 's': { - size_t str_len = strlen(s); - - if (str_len == 0) - return 0; - if (len == -1 || len > str_len) - len = str_len; - args[argc].to = s + len; - break; - } - case 'd': - strtol(s, &args[argc].to, 0); - goto num; - case 'u': - strtoul(s, &args[argc].to, 0); - goto num; - case 'o': - strtoul(s, &args[argc].to, 8); - goto num; - case 'x': - strtoul(s, &args[argc].to, 16); - num: - if (args[argc].to == args[argc].from) - return 0; - break; - default: - return 0; - } - s = args[argc].to; - argc++; - } -} - -/** - * match_token: - Find a token (and optional args) in a string - * @s: the string to examine for token/argument pairs - * @table: match_table_t describing the set of allowed option tokens and the - * arguments that may be associated with them. Must be terminated with a - * &struct match_token whose pattern is set to the NULL pointer. - * @args: array of %MAX_OPT_ARGS &substring_t elements. Used to return match - * locations. - * - * Description: Detects which if any of a set of token strings has been passed - * to it. Tokens can include up to MAX_OPT_ARGS instances of basic c-style - * format identifiers which will be taken into account when matching the - * tokens, and whose locations will be returned in the @args array. - */ -int match_token(char *s, const match_table_t table, substring_t args[]) -{ - const struct match_token *p; - - for (p = table; !match_one(s, p->pattern, args) ; p++) - ; - - return p->token; -} - -/** - * match_number: scan a number in the given base from a substring_t - * @s: substring to be scanned - * @result: resulting integer on success - * @base: base to use when converting string - * - * Description: Given a &substring_t and a base, attempts to parse the substring - * as a number in that base. On success, sets @result to the integer represented - * by the string and returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure. - */ -static int match_number(substring_t *s, int *result, int base) -{ - char *endp; - char *buf; - int ret; - long val; - size_t len = s->to - s->from; - - buf = malloc(len + 1); - if (!buf) - return -ENOMEM; - memcpy(buf, s->from, len); - buf[len] = '\0'; - - ret = 0; - val = strtol(buf, &endp, base); - if (endp == buf) - ret = -EINVAL; - else if (val < (long)INT_MIN || val > (long)INT_MAX) - ret = -ERANGE; - else - *result = (int) val; - free(buf); - return ret; -} - -/** - * match_int: - scan a decimal representation of an integer from a substring_t - * @s: substring_t to be scanned - * @result: resulting integer on success - * - * Description: Attempts to parse the &substring_t @s as a decimal integer. On - * success, sets @result to the integer represented by the string and returns 0. - * Returns -ENOMEM, -EINVAL, or -ERANGE on failure. - */ -int match_int(substring_t *s, int *result) -{ - return match_number(s, result, 0); -} - -/** - * match_octal: - scan an octal representation of an integer from a substring_t - * @s: substring_t to be scanned - * @result: resulting integer on success - * - * Description: Attempts to parse the &substring_t @s as an octal integer. On - * success, sets @result to the integer represented by the string and returns - * 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure. - */ -int match_octal(substring_t *s, int *result) -{ - return match_number(s, result, 8); -} - -/** - * match_hex: - scan a hex representation of an integer from a substring_t - * @s: substring_t to be scanned - * @result: resulting integer on success - * - * Description: Attempts to parse the &substring_t @s as a hexadecimal integer. - * On success, sets @result to the integer represented by the string and - * returns 0. Returns -ENOMEM, -EINVAL, or -ERANGE on failure. - */ -int match_hex(substring_t *s, int *result) -{ - return match_number(s, result, 16); -} - -/** - * match_wildcard: - parse if a string matches given wildcard pattern - * @pattern: wildcard pattern - * @str: the string to be parsed - * - * Description: Parse the string @str to check if matches wildcard - * pattern @pattern. The pattern may contain two type wildcardes: - * '*' - matches zero or more characters - * '?' - matches one character - * If it's matched, return true, else return false. - */ -bool match_wildcard(const char *pattern, const char *str) -{ - const char *s = str; - const char *p = pattern; - bool star = false; - - while (*s) { - switch (*p) { - case '?': - s++; - p++; - break; - case '*': - star = true; - str = s; - if (!*++p) - return true; - pattern = p; - break; - default: - if (*s == *p) { - s++; - p++; - } else { - if (!star) - return false; - str++; - s = str; - p = pattern; - } - break; - } - } - - if (*p == '*') - ++p; - return !*p; -} - -/** - * match_strlcpy: - Copy the characters from a substring_t to a sized buffer - * @dest: where to copy to - * @src: &substring_t to copy - * @size: size of destination buffer - * - * Description: Copy the characters in &substring_t @src to the - * c-style string @dest. Copy no more than @size - 1 characters, plus - * the terminating NUL. Return length of @src. - */ -size_t match_strlcpy(char *dest, const substring_t *src, size_t size) -{ - size_t ret = src->to - src->from; - - if (size) { - size_t len = ret >= size ? size - 1 : ret; - memcpy(dest, src->from, len); - dest[len] = '\0'; - } - return ret; -} - -/** - * match_strdup: - allocate a new string with the contents of a substring_t - * @s: &substring_t to copy - * - * Description: Allocates and returns a string filled with the contents of - * the &substring_t @s. The caller is responsible for freeing the returned - * string with free(). - */ -char *match_strdup(const substring_t *s) -{ - size_t sz = s->to - s->from + 1; - char *p = malloc(sz); - if (p) - match_strlcpy(p, s, sz); - return p; -} diff --git a/util/parser.h b/util/parser.h deleted file mode 100644 index 39d5b79..0000000 --- a/util/parser.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * linux/include/linux/parser.h - * - * Header for lib/parser.c - * Intended use of these functions is parsing filesystem argument lists, - * but could potentially be used anywhere else that simple option=arg - * parsing is required. - */ - - -/* associates an integer enumerator with a pattern string. */ -struct match_token { - int token; - const char *pattern; -}; - -typedef struct match_token match_table_t[]; - -/* Maximum number of arguments that match_token will find in a pattern */ -enum {MAX_OPT_ARGS = 3}; - -/* Describe the location within a string of a substring */ -typedef struct { - char *from; - char *to; -} substring_t; - -int match_token(char *, const match_table_t table, substring_t args[]); -int match_int(substring_t *, int *result); -int match_octal(substring_t *, int *result); -int match_hex(substring_t *, int *result); -bool match_wildcard(const char *pattern, const char *str); -size_t match_strlcpy(char *, const substring_t *, size_t); -char *match_strdup(const substring_t *); diff --git a/util/suffix.c b/util/suffix.c index bf1485f..6cd4f0b 100644 --- a/util/suffix.c +++ b/util/suffix.c @@ -1,3 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * * Copyright 2014 PMC-Sierra, Inc. diff --git a/util/suffix.h b/util/suffix.h index 278f472..4f8e041 100644 --- a/util/suffix.h +++ b/util/suffix.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * * Copyright 2014 PMC-Sierra, Inc. |