| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * Babeltrace CTF writer - Utilities |
| 7 | */ |
| 8 | |
| 9 | #define BT_LOG_TAG "CTF-WRITER/UTILS" |
| 10 | #include "logging.h" |
| 11 | |
| 12 | #include <glib.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #include <babeltrace2-ctf-writer/utils.h> |
| 16 | #include <babeltrace2-ctf-writer/object.h> |
| 17 | |
| 18 | #include "common/assert.h" |
| 19 | |
| 20 | #include "clock-class.h" |
| 21 | #include "field-types.h" |
| 22 | |
| 23 | static |
| 24 | const char * const reserved_keywords_str[] = {"align", "callsite", |
| 25 | "const", "char", "clock", "double", "enum", "env", "event", |
| 26 | "floating_point", "float", "integer", "int", "long", "short", "signed", |
| 27 | "stream", "string", "struct", "trace", "typealias", "typedef", |
| 28 | "unsigned", "variant", "void", "_Bool", "_Complex", "_Imaginary"}; |
| 29 | |
| 30 | static GHashTable *reserved_keywords_set; |
| 31 | static int init_done; |
| 32 | |
| 33 | static |
| 34 | void try_init_reserved_keywords(void) |
| 35 | { |
| 36 | size_t i; |
| 37 | const size_t reserved_keywords_count = |
| 38 | sizeof(reserved_keywords_str) / sizeof(char *); |
| 39 | |
| 40 | if (reserved_keywords_set) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | reserved_keywords_set = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 45 | BT_ASSERT_DBG(reserved_keywords_set); |
| 46 | |
| 47 | for (i = 0; i < reserved_keywords_count; i++) { |
| 48 | gpointer quark = GINT_TO_POINTER(g_quark_from_string( |
| 49 | reserved_keywords_str[i])); |
| 50 | |
| 51 | g_hash_table_insert(reserved_keywords_set, quark, quark); |
| 52 | } |
| 53 | |
| 54 | init_done = 1; |
| 55 | } |
| 56 | |
| 57 | static __attribute__((destructor)) |
| 58 | void trace_finalize(void) |
| 59 | { |
| 60 | if (reserved_keywords_set) { |
| 61 | g_hash_table_destroy(reserved_keywords_set); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | bt_ctf_bool bt_ctf_identifier_is_valid(const char *identifier) |
| 66 | { |
| 67 | bt_ctf_bool is_valid = BT_CTF_TRUE; |
| 68 | char *string = NULL; |
| 69 | char *save_ptr, *token; |
| 70 | |
| 71 | if (!identifier) { |
| 72 | BT_LOGT_STR("Invalid parameter: input string is NULL."); |
| 73 | is_valid = BT_CTF_FALSE; |
| 74 | goto end; |
| 75 | } |
| 76 | |
| 77 | try_init_reserved_keywords(); |
| 78 | |
| 79 | if (identifier[0] == '\0') { |
| 80 | is_valid = BT_CTF_FALSE; |
| 81 | goto end; |
| 82 | } |
| 83 | |
| 84 | string = strdup(identifier); |
| 85 | if (!string) { |
| 86 | BT_LOGE("strdup() failed."); |
| 87 | is_valid = BT_CTF_FALSE; |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 91 | token = strtok_r(string, " ", &save_ptr); |
| 92 | while (token) { |
| 93 | if (g_hash_table_lookup_extended(reserved_keywords_set, |
| 94 | GINT_TO_POINTER(g_quark_from_string(token)), |
| 95 | NULL, NULL)) { |
| 96 | is_valid = BT_CTF_FALSE; |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | token = strtok_r(NULL, " ", &save_ptr); |
| 101 | } |
| 102 | end: |
| 103 | free(string); |
| 104 | return is_valid; |
| 105 | } |