diff options
| author | Tristan Riehs <tristan.riehs@inria.fr> | 2026-07-05 18:31:01 +0200 |
|---|---|---|
| committer | Tristan Riehs <tristan.riehs@inria.fr> | 2026-07-05 18:31:16 +0200 |
| commit | 9b4d346d7d5831a2db2da9335509bd93b69e0840 (patch) | |
| tree | 710ba2b6a286efa941052242e4f7b8d5253be993 /src/system.c | |
| parent | 430bca8a0f166df268e2fa8fb207286640bd0602 (diff) | |
Outline of "ftag tag edit"
Some things are not finished yet, such as the handling of non-utf8
characters in SQL strings.
Diffstat (limited to 'src/system.c')
| -rw-r--r-- | src/system.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/system.c b/src/system.c index 2f95661..ccb7ccd 100644 --- a/src/system.c +++ b/src/system.c @@ -122,3 +122,49 @@ int ftag_execvp(char *const *cmd, int can_exit) } return status; } + +char *edit(const char *initial_content) +{ + char tmp_file[32]; + int tmp_fd; + int rc; + char *env_editor; + char editor_cmd[512]; + char *cmd[] = { + "sh", + "-c", + editor_cmd, + NULL + }; + char *new_content; + strcpy(tmp_file, "/tmp/ftag-XXXXXX"); + tmp_fd = mkstemp(tmp_file); + sys_check(tmp_fd != -1, "mkstemp"); + rc = write(tmp_fd, initial_content, strlen(initial_content)); + sys_check(rc != -1, "write"); + env_editor = getenv("EDITOR"); + if (env_editor) + strbuild(editor_cmd, "%s %s", env_editor, tmp_file); + else + strbuild(editor_cmd, "%s %s", "nano", tmp_file); + ftag_execvp(cmd, 1); + new_content = file_to_string(tmp_fd); + close(tmp_fd); + return new_content; +} + +char *file_to_string(int fd) +{ + struct stat st; + char *content; + int rc; + rc = lseek(fd, 0L, SEEK_SET); + sys_check(rc != -1, "lseek"); + rc = fstat(fd, &st); + sys_check(rc != -1, "stat"); + content = malloc(st.st_size + 1); + rc = read(fd, content, st.st_size); + sys_check(rc != -1, "read"); + content[st.st_size] = '\0'; + return content; +} |
