diary

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

commit 030645d5e19023baea021545910f62d5979331a6
parent 1e22fc437762c325b8b08faa974981dc1b93362a
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Tue,  7 Dec 2021 08:35:31 +0100

add handling for no fmt_cmd again

Diffstat:
Msrc/diary.c | 92+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 53 insertions(+), 39 deletions(-)

diff --git a/src/diary.c b/src/diary.c @@ -81,6 +81,8 @@ 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; wclear(win); @@ -92,50 +94,62 @@ void display_entry(const char* dir, size_t dir_size, const struct tm* date, WIND 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, " "); - } - argv[i++] = ppath; - argv[i] = (char*) NULL; + if (strcmp(CONFIG.fmt_cmd, "") == 0) { + // no formatting command defined, read and print lines + fp = fopen(ppath, "r"); + while((c = getc(fp)) != EOF) waddch(win, c); + fclose(fp); + wrefresh(win); + } else { + 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, " "); + } + argv[i++] = ppath; + argv[i] = (char*) NULL; - // 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); - } + // 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 + 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); + 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); - exit(0); - } - close(pipeds[0]); - close(pipeds[1]); - wait(0); - wait(0); - } // end date_has_entry + close(pipeds[0]); + close(pipeds[1]); + wait(0); + wait(0); + } // end no fmt_cmd + } else { + // date has no entry + wrefresh(win); + } + } /* Writes edit command for 'date' entry to 'rcmd'. '*rcmd' is NULL on error. */