scal

Simple Calendar
git clone git://git.wimdupont.com/scal.git
Log | Files | Refs | README | LICENSE

commit 5c54d29a9af96aef1188ddcfadde479c95f6e89c
parent 73f7f9f3fb1223faf72b7568e917c61c3ce3697b
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sun, 15 Jun 2025 18:19:24 +0200

updated flags

Diffstat:
MMakefile | 8+-------
Aconfig.mk | 11+++++++++++
Mscal.c | 88++++++++++++++++++++++++++++++++++++++++----------------------------------------
3 files changed, 56 insertions(+), 51 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,10 +1,4 @@ -CC = gcc - -NAME = scal -VERSION = 0.1 - -PREFIX = /usr/local -MANPREFIX = ${PREFIX}/share/man +include config.mk BIN = scal SRC = scal.c diff --git a/config.mk b/config.mk @@ -0,0 +1,11 @@ +NAME = scal + +VERSION = 0.1 + +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" +CFLAGS = -std=c99 -pedantic -Wall -Wextra -I/usr/local/include ${CPPFLAGS} + +PREFIX = /usr/local +MANPREFIX = ${PREFIX}/share/man + +CC = gcc diff --git a/scal.c b/scal.c @@ -13,6 +13,7 @@ #include "config.h" #define ctrl(x) ((x) & 0x1f) +#define arrlen(arr) (sizeof(arr)/sizeof(0[arr])) typedef enum {NO, YEARLY, MONTHLY, WEEKLY, DAILY} Recurring; @@ -22,7 +23,7 @@ typedef struct struct tm time; struct tm end; char *description; -} Note ; +} Note; static void init_screen(void); static void resize(void); @@ -31,7 +32,7 @@ static void print_cal(void); static void write_notes(void); static Note ** get_notes(void); static Note * get_note(char *line_buf, ssize_t line_size); -static Recurring get_relevant_recur(struct tm *tm); +static int get_relevant_recur(struct tm *tm); static void print_view(void); static void print_month(const int monthadd, int *y); static void combo_key(int keypress); @@ -39,8 +40,8 @@ static void combo_go(int keypress); static void combo_make(int keypress); static void add_note(struct tm *tm); static void edit_notes(void); -static void rm_note(struct tm *tm); -static int prompt_answer(char *buf, size_t size, const char *question, const char *mask, char *prefill); +static int prompt_answer(char *buf, int size, const char *question, const char *mask, char *prefill); +static void rm_note(void); static int get_curnotes(void); static int get_note_view_col(Recurring recurring); static int get_days(const struct tm *tm); @@ -72,7 +73,7 @@ static Note **notes, **curnotes = NULL; static char file[PATH_MAX], *editor; int -main(int argc, char **argv) +main(void) { char *userhome; struct passwd userinf; @@ -169,8 +170,8 @@ print_cal(void) void print_month(const int monthadd, int *y) { - int wcount, today, wnote, sindex, color, x = 0; - Recurring notecol; + int wcount, today, wnote, color, x = 0; + int notecol; struct tm otm = highltm; if (monthadd >= 0) @@ -190,14 +191,14 @@ print_month(const int monthadd, int *y) (*y)+=1; x=otm.tm_wday*3; - for (size_t i = 0; i < wcount; ++i) { + for (int i = 0; i < wcount; ++i) { otm.tm_mday=i+1; today = same_day(&otm, &curtm); notecol = get_relevant_recur(&otm); if (today) wattron(wwin, A_UNDERLINE); - if (wnote = notecol != -1) { + if ((wnote = (notecol != -1))) { color = get_note_view_col(notecol); wattron(wwin, COLOR_PAIR(color)); } @@ -325,7 +326,7 @@ combo_make(int key) add_note(&highltm); break; case KEY_COMBO_MAKE_RM: - rm_note(&highltm); + rm_note(); break; default: break; @@ -335,35 +336,35 @@ combo_make(int key) void add_note(struct tm *tm) { size_t len; - char answer[DESCLEN+20], buf[DESCLEN], rec[2], time[6], date[11], prefill[11], span[2]; - char end[11] = "2999-12-31\0"; + char answer[DESCLEN+20], desc[DESCLEN], rec[2], time[6], date[11], prefill[11], span[2]; + char end[11] = "2999-12-31"; if (tm != NULL) - snprintf(prefill, 11, "%04d-%02d-%02d\0", - tm->tm_year+1900, - tm->tm_mon+1, - tm->tm_mday); + snprintf(prefill, 11, "%04d-%02d-%02d", + (tm->tm_year+1900)%10000u, + (tm->tm_mon+1)%100u, + (tm->tm_mday)%100u); - if (prompt_answer(rec, 1, "Recurring? (0=NO;1=YEARLY;2=MONTHLY;3=WEEKLY;4=DAILY)", "4", "0")) + if (prompt_answer(rec, arrlen(rec), "Recurring? (0=NO;1=YEARLY;2=MONTHLY;3=WEEKLY;4=DAILY)", "4", "0")) return; - if (prompt_answer(date, 10, "Date?", "2999-19-39", prefill)) + if (prompt_answer(date, arrlen(date), "Date?", "2999-19-39", prefill)) return; - if (prompt_answer(time, 5, "Time?", "29:59", NULL)) + if (prompt_answer(time, arrlen(time), "Time?", "29:59", NULL)) return; if (rec[0] != '0') { - if (prompt_answer(span, 1, "Set end date? (0=NO;1=YES)", "1", "0")) + if (prompt_answer(span, arrlen(span), "Set end date? (0=NO;1=YES)", "1", "0")) return; if (span[0] == '1') { - if (prompt_answer(end, 10, "End date?", "2999-19-39", NULL)) + if (prompt_answer(end, arrlen(end), "End date?", "2999-19-39", NULL)) return; } } - if (prompt_answer(buf, DESCLEN, "Description?", NULL, NULL)) + if (prompt_answer(desc, DESCLEN, "Description?", NULL, NULL)) return; - snprintf(answer, DESCLEN+20, "%s|%10sT%s|%10s|%s", rec, date, time, end, buf); + snprintf(answer, DESCLEN+30, "%s|%10sT%s|%10s|%s", rec, date, time, end, desc); len = strlen(answer); @@ -413,7 +414,7 @@ edit_notes(void) } void -rm_note(struct tm *tm) { +rm_note(void) { int c, y, x; if (curnote_count == 0) @@ -466,7 +467,7 @@ write_notes(void) for (int i = 0; i < note_count; i++) fprintf(fp, "%d|%04d-%02d-%02dT%02d:%02d|%04d-%02d-%02d|%s\n", - notes[i]->recurring, + (int) notes[i]->recurring, notes[i]->time.tm_year+1900, notes[i]->time.tm_mon+1, notes[i]->time.tm_mday, @@ -531,7 +532,7 @@ get_note(char *line_buf, ssize_t line_size) fatal("Fatal: failed to allocate bytes for note.\n"); sscanf(line_buf, "%d|%[^|]|%[^|]|%[^\n]", - &note->recurring, + (int*) &note->recurring, timestr, enddate, note->description); @@ -558,10 +559,10 @@ get_note(char *line_buf, ssize_t line_size) return note; } -Recurring +int get_relevant_recur(struct tm *tm) { - Recurring relevant = -1; + int relevant = -1; for (int i = 0; i < note_count; i++) { if (is_relevant(notes[i], tm)) @@ -594,12 +595,11 @@ print_view(void) } int -prompt_answer(char *buf, size_t size, const char *question, const char *mask, char *prefill) +prompt_answer(char *buf, int size, const char *question, const char *mask, char *prefill) { - int c, y, x, nr, phlen = mask == NULL ? 0 : strlen(mask), status = 0; + int c, y, x, nr, phlen = mask == NULL ? 0 : strlen(mask), status = 0, len = 0; char placeholder[phlen]; char *startfill; - size_t len = 0; replace_digits(placeholder, mask, phlen); @@ -609,12 +609,8 @@ prompt_answer(char *buf, size_t size, const char *question, const char *mask, ch attron(COLOR_PAIR(PROMPT_COLOR)); addstr(question); attroff(COLOR_PAIR(PROMPT_COLOR)); - if (prefill != NULL) - startfill = prefill; - else if (placeholder != NULL) - startfill = placeholder; - else - startfill = ""; + + startfill = prefill != NULL ? prefill : placeholder; printw(" %s", startfill); @@ -638,14 +634,16 @@ prompt_answer(char *buf, size_t size, const char *question, const char *mask, ch case KEY_BACKSPACE: if (len > 0) { if (len <= phlen) { - buf[--(len)] = placeholder[len]; + --len; + buf[len] = placeholder[len]; move(y, --x); attron(COLOR_PAIR(PROMPT_COLOR)); addch(buf[len]); attroff(COLOR_PAIR(PROMPT_COLOR)); move(y, x); if (len > 0 && len <= phlen && placeholder[len] != '_') { - buf[--(len)] = placeholder[len]; + --len; + buf[len] = placeholder[len]; move(y, --x); attron(COLOR_PAIR(PROMPT_COLOR)); addch(buf[len]); @@ -660,7 +658,7 @@ prompt_answer(char *buf, size_t size, const char *question, const char *mask, ch } break; default: - if (len < size) { + if (len < size-1) { if (len < phlen) { if (!isdigit(c) || (nr = mask[len] - '0') < c - '0') { mvprintw(maxy-2, 1, "Only digits (max: %d)", nr); @@ -686,11 +684,12 @@ prompt_answer(char *buf, size_t size, const char *question, const char *mask, ch noecho(); curs_set(0); - if (strchr(buf, '_') || len < phlen) { + buf[phlen > 0 ? phlen : len] = '\0'; + + if (strchr(buf, '_') != NULL || len < phlen) { buf[0] = '\0'; status = 1; - } else - buf[phlen > 0 ? phlen : len] = '\0'; + } move(maxy-1, 1); clrtoeol(); @@ -703,7 +702,7 @@ prompt_answer(char *buf, size_t size, const char *question, const char *mask, ch int get_curnotes(void) { - if (curnote_count > 0) { + if (curnote_count > 0 || curnotes != NULL) { curnote_count = 0; free(curnotes); curnotes = NULL; @@ -723,6 +722,7 @@ get_curnotes(void) curnote_count++; } } + return 0; } int