Cleanup reading of argv from files

This commit is contained in:
Kovid Goyal 2025-04-28 09:52:26 +05:30
parent 5c9c8aa424
commit 2f8c392571
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 15 additions and 11 deletions

View file

@ -12,7 +12,6 @@
#include <os/log.h>
#endif
void
free_argv_array(argv_array *a) {
if (a && a->needs_free) {
@ -61,10 +60,10 @@ get_argv_from(const char *filename, const char *argv0, argv_array *final_ans) {
argv_array ans = {0};
bool ok = false;
ans.buf = malloc(src_sz + strlen(argv0) + 64);
if (!ans.buf) { errno = ENOMEM; goto end; }
if (!ans.buf) goto oom;
ans.needs_free = true;
if (!add_to_argv(&ans, argv0, strlen(argv0))) goto end;
if (!alloc_shlex_state(&s, src, src_sz, false)) { errno = ENOMEM; goto end; }
if (!add_to_argv(&ans, argv0, strlen(argv0))) goto oom;
if (!alloc_shlex_state(&s, src, src_sz, false)) goto oom;
bool keep_going = true;
while (keep_going) {
ssize_t q = next_word(&s);
@ -73,18 +72,19 @@ get_argv_from(const char *filename, const char *argv0, argv_array *final_ans) {
case -2: keep_going = false; break;
default:
if (ans.count == 1 && strcmp(s.buf, "kitty") == 0) continue;
if (!add_to_argv(&ans, s.buf, q)) { goto end; }
if (!add_to_argv(&ans, s.buf, q)) goto oom;
break;
}
}
ok = true;
oom:
if (!ok) {
errno = ENOMEM;
fprintf(stderr, "Failed to read from %s ", filename); perror("with error");
}
end:
free(src); dealloc_shlex_state(&s);
if (ok) *final_ans = ans;
else {
free_argv_array(&ans);
fprintf(stderr, "Failed to read from %s ", filename); perror("with error");
}
return ok;
}

View file

@ -513,7 +513,6 @@ main(int argc_, char *argv_[], char* envp[]) {
RAII_CLISpec(cli_spec);
bool handle_fast_commandline_called = delegate_to_kitten_if_possible(argva.count, argva.argv, exe_dir);
bool ok = parse_and_check_kitty_cli(&cli_spec, argva.count, argva.argv);
free_argv_array(&argva);
if (!ok) return 1;
if (!handle_fast_commandline_called) handle_fast_commandline(&cli_spec, NULL, -1);
int ret=0;
@ -529,6 +528,7 @@ main(int argc_, char *argv_[], char* envp[]) {
};
if (being_tested) output_test_data(&run_data);
else ret = run_embedded(&run_data);
free_argv_array(&argva);
single_instance_main(-1, NULL, NULL);
if (!being_tested) Py_FinalizeEx();
return ret;

View file

@ -502,7 +502,11 @@ class TestDataTypes(BaseTest):
env = os.environ.copy()
env['KITTY_CONFIG_DIRECTORY'] = tdir
env['KITTY_LAUNCHED_BY_LAUNCH_SERVICES'] = '1'
actual = subprocess.check_output([kitty_exe(), '+runpy', 'import json, sys; print(json.dumps(sys.argv))'], env=env).strip().decode()
cp = subprocess.run([kitty_exe(), '+runpy', 'import json, sys; print(json.dumps(sys.argv))'], env=env, stdout=subprocess.PIPE)
actual = cp.stdout.strip().decode()
if cp.returncode != 0:
print(actual)
raise AssertionError(f'kitty +runpy failed with return code: {cp.returncode}')
self.ae('next-line', actual)
os.makedirs(tdir + '/good/kitty')
open(tdir + '/good/kitty/kitty.conf', 'w').close()