tmus

TUI Music Player
git clone https://git.sinitax.com/sinitax/tmus
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit fa2f322580bba3ff71053c895f658ecb4116a456
parent 72de33c4f15144e7c597fad850510dd7da88a0f2
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 14 Feb 2022 12:46:09 +0100

Improve error handling, history bug fix

Diffstat:
Msrc/cmd.c | 6++++--
Msrc/data.c | 53++++++++++++++++++++++-------------------------------
Msrc/history.c | 11+++++++----
Msrc/listnav.c | 1+
Msrc/pane.c | 1+
Msrc/player.c | 4++--
Msrc/player_backend.c | 1+
Msrc/ref.h | 1+
Msrc/tui.c | 5+++--
Msrc/util.c | 23+++++++++++++++++++----
Msrc/util.h | 3+++
11 files changed, 64 insertions(+), 45 deletions(-)

diff --git a/src/cmd.c b/src/cmd.c @@ -59,7 +59,8 @@ cmd_move(const wchar_t *name) track = UPCAST(link, struct ref)->data; newpath = aprintf("%s/%s", tag->fpath, track->fname); - ASSERT(newpath != NULL); + OOM_CHECK(newpath); + move_file(track->fpath, newpath); free(track->fpath); track->fpath = newpath; @@ -87,7 +88,8 @@ cmd_add(const wchar_t *name) track = UPCAST(link, struct ref)->data; newpath = aprintf("%s/%s", tag->fpath, track->fname); - ASSERT(newpath != NULL); + OOM_CHECK(newpath); + copy_file(track->fpath, newpath); track->fpath = newpath; diff --git a/src/data.c b/src/data.c @@ -33,10 +33,11 @@ data_load(void) list_init(&tags_sel); datadir = getenv("TMUS_DATA"); - ASSERT(datadir != NULL); + if (!datadir) ERROR("TMUS_DATA not set!\n"); dir = opendir(datadir); - ASSERT(dir != NULL); + if (!dir) ERROR("Failed to access dir: %s\n", datadir); + while ((ent = readdir(dir))) { if (!strcmp(ent->d_name, ".")) continue; @@ -44,7 +45,7 @@ data_load(void) continue; path = aprintf("%s/%s", datadir, ent->d_name); - ASSERT(path != NULL); + OOM_CHECK(path); if (!stat(path, &st) && S_ISDIR(st.st_mode)) { tag = tag_init(datadir, ent->d_name); @@ -56,6 +57,7 @@ data_load(void) } closedir(dir); + /* TODO: ensure this is ok and remove */ ASSERT(!list_empty(&tags)); } @@ -117,15 +119,15 @@ index_update(struct tag *tag) int fid; path = aprintf("%s/index", tag->fpath); - ASSERT(path != NULL); + OOM_CHECK(path); + + dir = opendir(tag->fpath); + if (!dir) ERROR("Failed to access dir: %s\n", tag->fpath); file = fopen(path, "w+"); - ASSERT(file != NULL); + if (!file) ERROR("Failed to create file: %s\n", path); free(path); - dir = opendir(tag->fpath); - ASSERT(dir != NULL); - while ((ent = readdir(dir))) { if (!strcmp(ent->d_name, ".")) continue; @@ -139,7 +141,7 @@ index_update(struct tag *tag) continue; path = aprintf("%s/%s", tag->fpath, ent->d_name); - ASSERT(path != NULL); + OOM_CHECK(path); fid = get_fid(path); free(path); @@ -164,15 +166,12 @@ tracks_load(struct tag *tag) bool new_track; FILE *file; - printf("Loading files from %s", tag->fpath); - fflush(stdout); - index_path = aprintf("%s/index", tag->fpath); - ASSERT(index_path != NULL); + OOM_CHECK(index_path); file = fopen(index_path, "r"); if (file == NULL) { - index_update(tag); + index_update(tag); /* create index */ file = fopen(index_path, "r"); ASSERT(file != NULL); } @@ -182,7 +181,7 @@ tracks_load(struct tag *tag) if (sep) *sep = '\0'; sep = strchr(linebuf, ':'); - ASSERT(sep != NULL); + if (!sep) ERROR("Syntax error in index file: %s\n", index_path); *sep = '\0'; track_fid = atoi(linebuf); @@ -190,11 +189,11 @@ tracks_load(struct tag *tag) track = track_alloc(tag->fpath, track_name, track_fid); ref = ref_init(tag); - ASSERT(ref != NULL); + OOM_CHECK(ref); list_push_back(&track->tags, LINK(ref)); ref = ref_init(track); - ASSERT(ref != NULL); + OOM_CHECK(ref); list_push_back(&tag->tracks, LINK(ref)); new_track = true; @@ -206,14 +205,11 @@ tracks_load(struct tag *tag) if (new_track) { ref = ref_init(track); - ASSERT(ref != NULL); + OOM_CHECK(ref); list_push_back(&tracks, LINK(ref)); } } - /* clear line and reset cursor */ - printf("\x1b[0K\r"); - fclose(file); free(index_path); } @@ -228,22 +224,17 @@ tracks_save(struct tag *tag) /* write playlist back to index file */ - printf("Saving tracks to %s", tag->fpath); - index_path = aprintf("%s/index", tag->fpath); - ASSERT(index != NULL); + OOM_CHECK(index_path); file = fopen(index_path, "w+"); - ASSERT(file != NULL); + if (!file) ERROR("Failed to write to index file: %s\n", index_path); for (LIST_ITER(&tag->tracks, link)) { track = UPCAST(link, struct ref)->data; fprintf(file, "%i:%s\n", track->fid, track->fname); } - /* clear line and reset cursor */ - printf("\x1b[0K\r"); - fclose(file); free(index_path); } @@ -263,18 +254,18 @@ copy_file(const char *src, const char *dst) in = fopen(src, "r"); if (in == NULL) - PANIC("Failed to open file %s", src); + ERROR("Failed to read from file: %s\n", src); out = fopen(dst, "w+"); if (out == NULL) - PANIC("Failed to open file %s", dst); + ERROR("Failed to write to file: %s\n", dst); while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) { fwrite(buf, 1, nread, out); } if (nread < 0) - PANIC("Copy failed!", src, dst); + ERROR("Failed to copy file from %s to %s\n", src, dst); fclose(in); fclose(out); diff --git a/src/history.c b/src/history.c @@ -68,10 +68,12 @@ history_list_next(struct inputln *cur, const wchar_t *search) struct link *iter; struct inputln *ln; - for (iter = cur->link.next; iter; iter = iter->next) { + iter = cur->link.next; + while (LIST_INNER(iter)) { ln = UPCAST(iter, struct inputln); if (!search || !*search || wcsstr(ln->buf, search)) return ln; + iter = iter->next; } return cur; @@ -121,7 +123,7 @@ inputln_alloc(void) struct inputln *ln; ln = malloc(sizeof(struct inputln)); - ASSERT(ln != NULL); + OOM_CHECK(ln); inputln_init(ln); return ln; @@ -194,7 +196,7 @@ inputln_copy(struct inputln *dst, struct inputln *src) } dst->len = src->len; dst->buf = wcsdup(src->buf); - ASSERT(dst->buf != NULL); + OOM_CHECK(dst->buf); dst->cap = src->len + 1; dst->cur = dst->len; } @@ -216,7 +218,8 @@ inputln_resize(struct inputln *ln, size_t size) ln->cap = size; ln->buf = realloc(ln->buf, ln->cap * sizeof(wchar_t)); - ASSERT(ln->buf != NULL); + OOM_CHECK(ln->buf); ln->len = MIN(ln->len, ln->cap-1); ln->buf[ln->len] = '\0'; } + diff --git a/src/listnav.c b/src/listnav.c @@ -13,6 +13,7 @@ void listnav_update_bounds(struct listnav *nav, int min, int max) { ASSERT(max >= min); + nav->min = min; nav->max = max; listnav_update_wlen(nav, MIN(nav->wlen, nav->max - nav->min)); diff --git a/src/pane.c b/src/pane.c @@ -43,3 +43,4 @@ pane_free(struct pane *pane) { delwin(pane->win); } + diff --git a/src/player.c b/src/player.c @@ -88,10 +88,10 @@ void player_init(void) { player = malloc(sizeof(struct player)); - ASSERT(player != NULL); + OOM_CHECK(player); player->conn = mpd_connection_new(NULL, 0, 0); - ASSERT(player->conn != NULL); + OOM_CHECK(player->conn); list_init(&player->queue); list_init(&player->history); diff --git a/src/player_backend.c b/src/player_backend.c @@ -1,3 +1,4 @@ #include "player_backend.h" void foo(void); + diff --git a/src/ref.h b/src/ref.h @@ -14,3 +14,4 @@ void ref_free(void *ref); void refs_free(struct list *list); int refs_incl(struct list *list, void *data); void refs_rm(struct list *list, void *data); + diff --git a/src/tui.c b/src/tui.c @@ -157,7 +157,7 @@ track_name_gen(const wchar_t *text, int fwd, int reset) iter = fwd ? cur->next : cur->prev; } - while (iter && LIST_INNER(&tracks, iter)) { + while (LIST_INNER(iter)) { track = UPCAST(iter, struct ref)->data; if (wcsstr(track->name, text)) { cur = iter; @@ -183,7 +183,7 @@ tag_name_gen(const wchar_t *text, int fwd, int reset) iter = fwd ? cur->next : cur->prev; } - while (iter && LIST_INNER(&tags, iter)) { + while (LIST_INNER(iter)) { tag = UPCAST(iter, struct tag); if (wcsstr(tag->name, text)) { cur = iter; @@ -614,6 +614,7 @@ cmd_pane_vis(struct pane *pane, int sel) for (LIST_ITER(&history->list, iter)) { if (UPCAST(iter, struct inputln) == cmd) break; + index += 1; } line += swprintf(line, end - line, L"[%i] ", iter ? index : -1); diff --git a/src/util.c b/src/util.c @@ -68,6 +68,20 @@ assert(int cond, const char *file, int line, const char *condstr) exit(1); } +void +error(const char *fmtstr, ...) +{ + va_list ap; + + tui_restore(); + + va_start(ap, fmtstr); + vfprintf(stderr, fmtstr, ap); + va_end(ap); + + exit(1); +} + wchar_t * awprintf(const wchar_t *fmtstr, ...) { @@ -82,7 +96,7 @@ awprintf(const wchar_t *fmtstr, ...) va_end(ap); str = malloc((size + 1) * sizeof(wchar_t)); - ASSERT(str != NULL); + if (!str) return NULL; va_start(cpy, fmtstr); swprintf(str, size + 1, fmtstr, cpy); @@ -105,7 +119,7 @@ aprintf(const char *fmtstr, ...) va_end(ap); str = malloc(size + 1); - ASSERT(str != NULL); + if (!str) return NULL; va_start(cpy, fmtstr); vsnprintf(str, size + 1, fmtstr, cpy); @@ -128,7 +142,7 @@ appendstrf(char *alloc, const char *fmtstr, ...) prevlen = alloc ? strlen(alloc) : 0; alloc = realloc(alloc, prevlen + size + 1); - ASSERT(alloc != NULL); + if (!alloc) return NULL; va_start(cpy, fmtstr); vsnprintf(alloc + prevlen, size + 1, fmtstr, cpy); @@ -145,7 +159,8 @@ sanitized(const char *instr) int i; clean = strdup(instr); - ASSERT(clean != NULL); + OOM_CHECK(clean); + for (i = 0, p = instr; *p; p++) { if (strchr(allowed, *p)) clean[i++] = *p; diff --git a/src/util.h b/src/util.h @@ -9,6 +9,8 @@ #define PANIC(...) panic(__FILE__, __LINE__, "" __VA_ARGS__) #define ASSERT(x) assert((x), __FILE__, __LINE__, #x) +#define OOM_CHECK(x) assert((x) != NULL, __FILE__, __LINE__, "Out of Memory!") +#define ERROR(...) error("" __VA_ARGS__) #define LINK(p) (&(p)->link) #define UPCAST(iter, type) LINK_UPCAST(iter, type, link) @@ -17,6 +19,7 @@ int strnwidth(const char *s, int n); void panic(const char *file, int line, const char *msg, ...); void assert(int cond, const char *file, int line, const char *condstr); +void error(const char *fmtstr, ...); char *aprintf(const char *fmtstr, ...); wchar_t *awprintf(const wchar_t *fmtstr, ...);