From 1d5e5f26014ceefd824382acec732f326d8d6ce2 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 7 Aug 2024 13:52:10 -0400 Subject: Refactor ini getter and setter usage (#19) * Add handler for space-delimited lists * This needs attention, however. The INI writer has no way to know a list with spaces is a list; this happens in the value conversion functions. * Add type_hint member to INIData structure. At some point support with be added for all INIVAL_TYPE_* defines. Right now it's only used with arrays. * Zero out line buffer in ini_open after each iteration * Do not strip raw INI data. Let the conversion functions handle it * Add spaces to key value pairs in rendered INI output. * Add ini_getvar_TYPE() functions * These replace the functionality of static conv_TYPE() functions in delivery.c * Add support for missing types: U/CHAR, U/SHORT, STRLIST * ini_getval: expand template variables immediately before processing the output * Strip leading space to avoid issues with string comparisons against the result * ini_getval: Return copies, not the original. * This forces one to use ini_setval to replace/append values to the data array(s). It's safer this way. * fix_tox_conf(): Use ini_getval and ini_setval instead of modifying the original pointers directly * Tests: Free resources * Replace ini_getval(), ini_getval_required() and conv_*() usage * Now using ini_getval_TYPE() functions and ini_setval() * Remove unused helper functions and variables * download() returns long, not int * actions: update apt cache --- src/utils.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 2143c52..f721581 100644 --- a/src/utils.c +++ b/src/utils.c @@ -579,6 +579,7 @@ int xml_pretty_print_in_place(const char *filename, const char *pretty_print_pro goto pretty_print_failed; } + fclose(fp); guard_free(tempfile); guard_free(result); return 0; @@ -638,24 +639,31 @@ int fix_tox_conf(const char *filename, char **result) { for (size_t i = 0; i < toxini->section_count; i++) { struct INISection *section = toxini->section[i]; if (section) { - if (startswith(section->key, "testenv")) { - for (size_t k = 0; k < section->data_count; k++) { - struct INIData *data = section->data[k]; - if (data) { - if (!strcmp(data->key, "commands") && (startswith(data->value, "pytest") && !strstr(data->value, "{posargs}"))) { - strip(data->value); + char *section_name = section->key; + for (size_t k = 0; k < section->data_count; k++) { + struct INIData *data = section->data[k]; + if (data) { + int err = 0; + char *key = data->key; + char *value = ini_getval_str(toxini, section->key, data->key, &err); + if (key && value) { + if (startswith(value, "pytest") && !strstr(value, "{posargs}")) { + strip(value); char *tmp; - tmp = realloc(data->value, strlen(data->value) + strlen(with_posargs) + 1); + tmp = realloc(value, strlen(value) + strlen(with_posargs) + 1); if (!tmp) { - SYSERROR("failed to increase data->value size to +%zu bytes", strlen(data->value) + strlen(with_posargs) + 1); + SYSERROR("failed to increase size to +%zu bytes", + strlen(value) + strlen(with_posargs) + 1); guard_free(*result); return -1; - } else if (tmp != data->value) { - data->value = tmp; + } else if (tmp != value) { + value = tmp; } - strcat(data->value, with_posargs); + strcat(value, with_posargs); + ini_setval(&toxini, INI_SETVAL_REPLACE, section_name, key, value); } } + guard_free(value); } } } -- cgit