diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | README | LICENSE

commit 5ed6746f83511fc736afcf87e00c105ebd09b7f1
parent f34a79f14a26344a1d640efb7a52c5b97f72e75d
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Mon,  6 Dec 2021 23:26:07 +0100

replace hacky tmp_fmt_txt_file with pipe

Diffstat:
Msrc/diary.c | 105+++++++++++++++++++++++++++++++++++++------------------------------------------
Msrc/diary.h | 3+++
2 files changed, 52 insertions(+), 56 deletions(-)

diff --git a/src/diary.c b/src/diary.c @@ -81,69 +81,61 @@ void draw_calendar(WINDOW* number_pad, WINDOW* month_pad, const char* diary_dir, void display_entry(const char* dir, size_t dir_size, const struct tm* date, WINDOW* win, int width) { char path[100]; char* ppath = path; - FILE* fp; - int c; - char* entry; - char* fmtcmd; - size_t s, fmtcmd_s; - char* tmp_fmt_txt_file = "/tmp/diary_tmp_fmt_entry"; - - // get entry path - fpath(dir, dir_size, date, &ppath, sizeof path); - if (ppath == NULL) { - fprintf(stderr, "Error while retrieving file path for diary reading"); - return; - } wclear(win); if (date_has_entry(dir, dir_size, date)) { - wmove(win, 0, 0); - - if (strcmp(CONFIG.fmt_cmd, "") == 0) { - // no formatting command defined, read and print lines - fp = fopen(path, "r"); - while((c = getc(fp)) != EOF) waddch(win, c); - fclose(fp); - } else { - // construct the formatting command - fmtcmd_s = strlen(CONFIG.fmt_cmd) + strlen(path) + strlen(">") + strlen(tmp_fmt_txt_file) + 3; - fmtcmd = malloc(fmtcmd_s); - strcpy(fmtcmd, CONFIG.fmt_cmd); - strcat(fmtcmd, " "); - strcat(fmtcmd, path); - strcat(fmtcmd, " >"); - strcat(fmtcmd, tmp_fmt_txt_file); - - // format the entry - system(fmtcmd); - - // read formatted entry - FILE* fp = fopen(tmp_fmt_txt_file, "r"); - if (fp == NULL) perror("Error opening file"); - - fseek(fp, 0, SEEK_END); - long entry_bytes = ftell(fp); - entry = calloc(entry_bytes + 1, sizeof(char)); - rewind(fp); - - // read formatted entry - s = fread(entry, 1, entry_bytes, fp); - fclose(fp); - // remove(tmp_fmt_txt_file); - if (unlink(tmp_fmt_txt_file) == -1) { - perror("unlink tokenfile"); + // get entry path + fpath(dir, dir_size, date, &ppath, sizeof path); + if (ppath == NULL) { + fprintf(stderr, "Error while retrieving file path for diary reading"); + return; + } + + int pipeds[2]; + if(pipe(pipeds) == -1) + perror("Failed to crate pipeds"); + + pid_t pid1, pid2; + if((pid1 = fork()) == 0) { + dup2(pipeds[1], STDOUT_FILENO); // re-use stdout in 1 + close(pipeds[0]); // 1 silently closed by dup2 already + + int max_arg = 10; + char *argv[max_arg]; + int i = 0; + char* tok = strtok(CONFIG.fmt_cmd, " "); + while (tok != NULL && i < max_arg-1) { + argv[i++] = tok; + tok = strtok(NULL, " "); } - // display formatted entry - entry[s] = '\0'; - waddstr(win, entry); + argv[i++] = ppath; + argv[i] = (char*) NULL; - free(fmtcmd); - free(entry); + // use the 'p' style exec to read fmt_cmd from the PATH + if (execvp(argv[0], argv) > 0) + perror("execvp of --fmt-cmd failed"); + exit(0); + } + + if((pid2 = fork()) == 0) { + dup2(pipeds[0], STDIN_FILENO); // re-use stdin in 0 + close(pipeds[1]); // 0 silently closed by dup2 already + + char line[256]; + while (fgets(line, sizeof line, stdin)) { + // print formatted entry from 0 to screen + wprintw(win, line); + } + wrefresh(win); + exit(0); } - } - wrefresh(win); + close(pipeds[0]); + close(pipeds[1]); + wait(0); + wait(0); + } // end date_has_entry } /* Writes edit command for 'date' entry to 'rcmd'. '*rcmd' is NULL on error. */ @@ -732,4 +724,4 @@ int main(int argc, char** argv) { endwin(); system("clear"); return 0; -} +} +\ No newline at end of file diff --git a/src/diary.h b/src/diary.h @@ -4,6 +4,7 @@ #ifdef __MACH__ #include <CoreFoundation/CoreFoundation.h> #endif + #include <stdio.h> #include <stdint.h> #include <stdlib.h> @@ -16,6 +17,8 @@ #include <ncurses.h> #include <locale.h> #include <langinfo.h> +#include <sys/wait.h> + #include "utils.h" #include "import.h" #include "caldav.h"