ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
env.h File Reference

Environment variable utilities with stack-based prompt responses. More...

Go to the source code of this file.

Functions

bool env_pop_prompt_response (char *response_out, size_t response_size)
 Pop the first response from ASCII_CHAT_QUESTION_PROMPT_RESPONSE stack.
bool env_has_prompt_response (void)
 Check if ASCII_CHAT_QUESTION_PROMPT_RESPONSE has any responses available.
bool env_validate_prompt_response_format (void)
 Validate ASCII_CHAT_QUESTION_PROMPT_RESPONSE format.

Detailed Description

Environment variable utilities with stack-based prompt responses.

Definition in file env.h.

Function Documentation

◆ env_has_prompt_response()

bool env_has_prompt_response ( void )

Check if ASCII_CHAT_QUESTION_PROMPT_RESPONSE has any responses available.

Returns
true if at least one response is available, false otherwise

Definition at line 215 of file env.c.

215 {
216 const char *env_value = SAFE_GETENV("ASCII_CHAT_QUESTION_PROMPT_RESPONSE");
217 if (!env_value || env_value[0] == '\0' || env_value[0] == ';') {
218 return false;
219 }
220
221 // Check if it's all semicolons
222 for (const char *p = env_value; *p; p++) {
223 if (*p != ';') {
224 return true;
225 }
226 }
227
228 return false;
229}
#define SAFE_GETENV(name)

References SAFE_GETENV.

◆ env_pop_prompt_response()

bool env_pop_prompt_response ( char * response_out,
size_t response_size )

Pop the first response from ASCII_CHAT_QUESTION_PROMPT_RESPONSE stack.

Format: "response1;response2;response3" or "response1;response2;response3;"

Examples:

  • "y;n;123" -> returns "y", sets env to "n;123"
  • "password" -> returns "password", sets env to ""
  • "y;" -> returns "y", sets env to ""
Parameters
response_outBuffer to store the popped response (must be at least 256 bytes)
response_sizeSize of response_out buffer
Returns
true if a response was successfully popped, false otherwise

Format: "response1;response2;response3" or "response1;response2;response3;" This function extracts the first response and updates the environment variable to contain the remaining responses.

Escaping: Use backslash to include literal semicolons or backslashes:

  • \; → ; (literal semicolon)
  • \ → \ (literal backslash)

Examples:

  • "y;n;123" -> returns "y", sets env to "n;123"
  • "password" -> returns "password", sets env to ""
  • "y;" -> returns "y", sets env to ""
  • "pass\;word;y" -> returns "pass;word", sets env to "y"
  • "path\\to\\file;n" -> returns "path\to\file", sets env to "n"
  • "" -> returns NULL
  • ";;y" -> returns NULL (invalid format)
  • ";y" -> returns NULL (invalid format)
Parameters
response_outBuffer to store the popped response (must be at least 256 bytes)
response_sizeSize of response_out buffer
Returns
true if a response was successfully popped, false otherwise

Definition at line 98 of file env.c.

98 {
99 if (!response_out || response_size < 2) {
100 return false;
101 }
102
103 const char *env_value = SAFE_GETENV("ASCII_CHAT_QUESTION_PROMPT_RESPONSE");
104 if (!env_value || env_value[0] == '\0') {
105 return false;
106 }
107
108 // Validate: no leading semicolon (unless escaped)
109 if (env_value[0] == ';') {
110 log_warn("Invalid ASCII_CHAT_QUESTION_PROMPT_RESPONSE format: leading "
111 "semicolon");
112 return false;
113 }
114
115 // Find the first unescaped semicolon
116 const char *semicolon = find_unescaped_semicolon(env_value);
117
118 if (semicolon == NULL) {
119 // No semicolon - this is the last (or only) response
120 size_t len = strlen(env_value);
121 if (len == 0) {
122 return false;
123 }
124
125 // Validate: ensure it's not empty
126 if (len >= response_size) {
127 log_warn("Response too long: %zu bytes (max: %zu)", len,
128 response_size - 1);
129 return false;
130 }
131
132 // Copy the response
133 SAFE_STRNCPY(response_out, env_value, response_size);
134
135 // Unescape the response (\; → ; and \\ → \‍)
136 unescape_response(response_out);
137
138 // Clear the environment variable
139 platform_unsetenv("ASCII_CHAT_QUESTION_PROMPT_RESPONSE");
140
141 log_dev("Popped last response from stack: '%s' (cleared env)",
142 response_out);
143 return true;
144 }
145
146 // Calculate response length
147 size_t response_len = (size_t)(semicolon - env_value);
148
149 // Validate: no empty segment
150 if (response_len == 0) {
151 log_warn(
152 "Invalid ASCII_CHAT_QUESTION_PROMPT_RESPONSE format: empty segment");
153 return false;
154 }
155
156 if (response_len >= response_size) {
157 log_warn("Response too long: %zu bytes (max: %zu)", response_len,
158 response_size - 1);
159 return false;
160 }
161
162 // Copy the first response
163 memcpy(response_out, env_value, response_len);
164 response_out[response_len] = '\0';
165
166 // Unescape the response (\; → ; and \\ → \‍)
167 unescape_response(response_out);
168
169 // Calculate remaining stack (skip the semicolon)
170 const char *remaining = semicolon + 1;
171
172 // Validate: if remaining is empty or only semicolons, clear the env
173 bool remaining_valid = false;
174 for (const char *p = remaining; *p; p++) {
175 if (*p != ';') {
176 remaining_valid = true;
177 break;
178 }
179 }
180
181 // Update environment variable with remaining responses
182 if (remaining_valid && remaining[0] != '\0') {
183 char new_env[UTIL_ENV_NEW_ENV_BUFFER_SIZE];
184 int written = snprintf(new_env, sizeof(new_env),
185 "ASCII_CHAT_QUESTION_PROMPT_RESPONSE=%s", remaining);
186 if (written < 0 || (size_t)written >= sizeof(new_env)) {
187 log_warn("Failed to format new environment variable");
188 return false;
189 }
190
191 if (platform_setenv("ASCII_CHAT_QUESTION_PROMPT_RESPONSE", remaining) !=
192 0) {
193 log_warn("Failed to update ASCII_CHAT_QUESTION_PROMPT_RESPONSE");
194 return false;
195 }
196
197 log_debug("Popped response from stack: '%s' (remaining: '%s')",
198 response_out, remaining);
199 } else {
200 // No valid remaining responses - clear the env
201 platform_unsetenv("ASCII_CHAT_QUESTION_PROMPT_RESPONSE");
202
203 log_debug("Popped response from stack: '%s' (cleared env)", response_out);
204 }
205
206 return true;
207}
#define UTIL_ENV_NEW_ENV_BUFFER_SIZE
Definition env.c:7
#define SAFE_STRNCPY(dst, src, size)
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:551
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
int platform_setenv(const char *name, const char *value)
Definition system.c:56
int platform_unsetenv(const char *name)
Definition system.c:62

References log_debug, log_dev, log_warn, platform_setenv(), platform_unsetenv(), SAFE_GETENV, SAFE_STRNCPY, and UTIL_ENV_NEW_ENV_BUFFER_SIZE.

Referenced by ui_input_password().

◆ env_validate_prompt_response_format()

bool env_validate_prompt_response_format ( void )

Validate ASCII_CHAT_QUESTION_PROMPT_RESPONSE format.

Valid formats: "y", "y;n", "y;n;123", "y;n;123;" Invalid formats: "", ";", ";y", "y;;n", ";;"

Returns
true if format is valid, false otherwise

Valid formats:

  • "y"
  • "y;n"
  • "y;n;123"
  • "y;n;123;"
  • "pass\;word;y" (escaped semicolon)
  • "path\\to\\file" (escaped backslash)

Invalid formats:

  • ""
  • ";"
  • ";y"
  • "y;;n" (empty segment)
  • ";;"
Returns
true if format is valid, false otherwise

Definition at line 251 of file env.c.

251 {
252 const char *env_value = SAFE_GETENV("ASCII_CHAT_QUESTION_PROMPT_RESPONSE");
253 if (!env_value) {
254 return true; // Not set is valid (no automated responses)
255 }
256
257 if (env_value[0] == '\0') {
258 return true; // Empty is valid (no automated responses)
259 }
260
261 // Invalid: leading semicolon (unless escaped)
262 if (env_value[0] == ';') {
263 return false;
264 }
265
266 // Check for consecutive unescaped semicolons or empty segments
267 const char *p = env_value;
268 bool last_was_semicolon = false;
269
270 while (*p) {
271 if (*p == '\\' && *(p + 1)) {
272 // Skip escaped character
273 last_was_semicolon = false;
274 p += 2;
275 continue;
276 }
277 if (*p == ';') {
278 if (last_was_semicolon) {
279 return false; // Consecutive unescaped semicolons (empty segment)
280 }
281 last_was_semicolon = true;
282 } else {
283 last_was_semicolon = false;
284 }
285 p++;
286 }
287
288 // Trailing semicolon is allowed
289 return true;
290}

References SAFE_GETENV.