Markus Armbruster | f5bebbb | 2014-09-30 13:59:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Dealing with identifiers |
| 3 | * |
| 4 | * Copyright (C) 2014 Red Hat, Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Markus Armbruster <armbru@redhat.com>, |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 |
| 10 | * or later. See the COPYING.LIB file in the top-level directory. |
| 11 | */ |
| 12 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Markus Armbruster | 856dfd8 | 2019-05-23 16:35:06 +0200 | [diff] [blame] | 14 | #include "qemu/ctype.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 15 | #include "qemu/id.h" |
Markus Armbruster | f5bebbb | 2014-09-30 13:59:30 +0200 | [diff] [blame] | 16 | |
| 17 | bool id_wellformed(const char *id) |
| 18 | { |
| 19 | int i; |
| 20 | |
| 21 | if (!qemu_isalpha(id[0])) { |
| 22 | return false; |
| 23 | } |
| 24 | for (i = 1; id[i]; i++) { |
| 25 | if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { |
| 26 | return false; |
| 27 | } |
| 28 | } |
| 29 | return true; |
| 30 | } |
Jeff Cody | a0f1913 | 2015-10-12 19:36:49 -0400 | [diff] [blame] | 31 | |
| 32 | #define ID_SPECIAL_CHAR '#' |
| 33 | |
John Snow | 624533e | 2015-11-25 16:03:37 -0500 | [diff] [blame] | 34 | static const char *const id_subsys_str[ID_MAX] = { |
Jeff Cody | a0f1913 | 2015-10-12 19:36:49 -0400 | [diff] [blame] | 35 | [ID_QDEV] = "qdev", |
| 36 | [ID_BLOCK] = "block", |
Marc-André Lureau | 1e419ee | 2019-10-21 16:31:31 +0200 | [diff] [blame] | 37 | [ID_CHR] = "chr", |
Thomas Huth | 27eb372 | 2021-02-15 10:02:25 +0100 | [diff] [blame] | 38 | [ID_NET] = "net", |
Jeff Cody | a0f1913 | 2015-10-12 19:36:49 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * Generates an ID of the form PREFIX SUBSYSTEM NUMBER |
| 43 | * where: |
| 44 | * |
| 45 | * - PREFIX is the reserved character '#' |
| 46 | * - SUBSYSTEM identifies the subsystem creating the ID |
| 47 | * - NUMBER is a decimal number unique within SUBSYSTEM. |
| 48 | * |
| 49 | * Example: "#block146" |
| 50 | * |
| 51 | * Note that these IDs do not satisfy id_wellformed(). |
| 52 | * |
| 53 | * The caller is responsible for freeing the returned string with g_free() |
| 54 | */ |
| 55 | char *id_generate(IdSubSystems id) |
| 56 | { |
| 57 | static uint64_t id_counters[ID_MAX]; |
| 58 | uint32_t rnd; |
| 59 | |
John Snow | 624533e | 2015-11-25 16:03:37 -0500 | [diff] [blame] | 60 | assert(id < ARRAY_SIZE(id_subsys_str)); |
Jeff Cody | a0f1913 | 2015-10-12 19:36:49 -0400 | [diff] [blame] | 61 | assert(id_subsys_str[id]); |
| 62 | |
| 63 | rnd = g_random_int_range(0, 100); |
| 64 | |
| 65 | return g_strdup_printf("%c%s%" PRIu64 "%02" PRId32, ID_SPECIAL_CHAR, |
| 66 | id_subsys_str[id], |
| 67 | id_counters[id]++, |
| 68 | rnd); |
| 69 | } |