Branch data Line data Source code
1 : : // Copyright 2012-2024 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include <zix/environment.h>
5 : :
6 : : #include <zix/allocator.h>
7 : :
8 : : #include <stdbool.h>
9 : : #include <stdlib.h>
10 : : #include <string.h>
11 : :
12 : : static bool
13 : 8 : is_path_delim(const char c)
14 : : {
15 [ + + + + : 8 : return c == '/' || c == ':' || c == '\0';
+ + ]
16 : : }
17 : :
18 : : static bool
19 : 67 : is_var_name_char(const char c)
20 : : {
21 [ + + + + : 67 : return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c == '_');
+ + + + +
+ ]
22 : : }
23 : :
24 : : // Append suffix to dst, update dst_len, and return the realloc'd result
25 : : static char*
26 : 32 : append_str(ZixAllocator* const allocator,
27 : : size_t* const dst_len,
28 : : char* const dst,
29 : : const size_t suffix_len,
30 : : const char* const suffix)
31 : : {
32 : 32 : const size_t out_len = *dst_len + suffix_len;
33 : 32 : char* const out = (char*)zix_realloc(allocator, dst, out_len + 1U);
34 [ + + ]: 32 : if (!out) {
35 : 6 : zix_free(allocator, dst);
36 : 6 : return NULL;
37 : : }
38 : :
39 : 26 : memcpy(out + *dst_len, suffix, suffix_len);
40 : 26 : out[(*dst_len += suffix_len)] = '\0';
41 : 26 : return out;
42 : : }
43 : :
44 : : // Append the value of an environment variable to dst if it's set
45 : : static char*
46 : 13 : append_var(ZixAllocator* const allocator,
47 : : size_t* const dst_len,
48 : : char* const dst,
49 : : const size_t ref_len,
50 : : const char* const ref)
51 : : {
52 : : // Get value from environment
53 : 13 : const char* const val = getenv(ref + 1U); // NOLINT(concurrency-mt-unsafe)
54 [ + + ]: 13 : if (val) {
55 : 8 : return append_str(allocator, dst_len, dst, strlen(val), val);
56 : : }
57 : :
58 : : // No value found, append variable reference as-is
59 : 5 : return append_str(allocator, dst_len, dst, ref_len, ref);
60 : : }
61 : :
62 : : // Copy a variable reference string like $NAME to a null-terminated string
63 : : static char*
64 : 8 : set_ref(ZixAllocator* const allocator,
65 : : char** const buffer,
66 : : const size_t ref_len,
67 : : const char* const ref)
68 : : {
69 : 8 : char* const out = (char*)zix_realloc(allocator, *buffer, ref_len + 1U);
70 : :
71 [ + + ]: 8 : if (out) {
72 : 7 : memcpy(out, ref, ref_len);
73 : 7 : out[ref_len] = '\0';
74 : : } else {
75 : 1 : zix_free(allocator, *buffer);
76 : : }
77 : :
78 : 8 : return out;
79 : : }
80 : :
81 : : char*
82 : 25 : zix_expand_environment_strings(ZixAllocator* const allocator,
83 : : const char* const string)
84 : : {
85 : 25 : char* ref = NULL; // Reference string like $NAME
86 : 25 : char* out = NULL; // Expanded result
87 : 25 : size_t len = 0U; // Length of expanded result
88 : :
89 : 25 : size_t start = 0U; // Start of current chunk to copy
90 [ + + ]: 170 : for (size_t s = 0U; string[s];) {
91 : 151 : const char c = string[s];
92 [ + + + + ]: 151 : if (c == '$' && is_var_name_char(string[s + 1U])) {
93 : : // Hit $ (variable reference like $VAR_NAME)
94 : 59 : for (size_t t = 1U;; ++t) {
95 [ + + ]: 53 : if (!is_var_name_char(string[s + t])) {
96 : 9 : const char* const prefix = string + start;
97 : 9 : const size_t prefix_len = s - start;
98 [ + + + + ]: 12 : if ((prefix_len &&
99 : 3 : !(out = append_str(allocator, &len, out, prefix_len, prefix))) ||
100 [ + + + + ]: 15 : !(ref = set_ref(allocator, &ref, t, string + s)) ||
101 : 7 : !(out = append_var(allocator, &len, out, t, ref))) {
102 : 3 : zix_free(allocator, ref);
103 : 3 : zix_free(allocator, out);
104 : 3 : return NULL;
105 : : }
106 : 6 : start = s = t;
107 : 6 : break;
108 : : }
109 : : }
110 [ + + + + ]: 142 : } else if (c == '~' && is_path_delim(string[s + 1U])) {
111 : : // Hit ~ before delimiter or end of string (home directory reference)
112 : 7 : const char* const prefix = string + start;
113 : 7 : const size_t prefix_len = s - start;
114 [ + + + + ]: 10 : if ((prefix_len &&
115 [ + + ]: 9 : !(out = append_str(allocator, &len, out, prefix_len, prefix))) ||
116 : 6 : !(out = append_var(allocator, &len, out, 5U, "$HOME"))) {
117 : 3 : zix_free(allocator, ref);
118 : 3 : return NULL;
119 : : }
120 : 4 : start = ++s;
121 : : } else {
122 : 135 : ++s;
123 : : }
124 : : }
125 : :
126 [ + + ]: 19 : if (string[start]) {
127 : 13 : const char* const tail = string + start;
128 : 13 : const size_t tail_len = strlen(tail);
129 : 13 : out = append_str(allocator, &len, out, tail_len, tail);
130 : : }
131 : :
132 : 19 : zix_free(allocator, ref);
133 : 19 : return out;
134 : : }
|