From 1651ae8df93ed99babd1dbe93a929f18f88b4306 Mon Sep 17 00:00:00 2001 From: Nikita Ivanov Date: Wed, 8 Jun 2022 09:03:40 +0500 Subject: [PATCH] Fixes, improvements --- config.c | 41 ++++++++++++++++++++++++++++++++++------- config.h | 2 +- ctpv.c | 41 +++++++++++++---------------------------- ctpv.h | 8 ++++++++ preview.c | 8 ++++---- preview.h | 8 ++++---- utils.c | 16 ++++++++++++++++ utils.h | 1 + 8 files changed, 81 insertions(+), 44 deletions(-) create mode 100644 ctpv.h diff --git a/config.c b/config.c index eaaf451..170e1cd 100644 --- a/config.c +++ b/config.c @@ -1,3 +1,4 @@ +#include "ctpv.h" #include "lexer.h" #include "error.h" #include "preview.h" @@ -22,13 +23,18 @@ enum { static Lexer *lexer; static Token token; static VectorPreview *previews; -static char *any_type; + +static void any_type_null(char **s) +{ + if (*s && strcmp(*s, any_type) == 0) + *s = NULL; +} static void add_preview(char *name, char *script, char *type, char *subtype, char *ext) { - if (type && strcmp(type, any_type) == 0) - type = NULL; + any_type_null(&type); + any_type_null(&subtype); if (subtype && strcmp(subtype, any_type) == 0) subtype = NULL; @@ -160,6 +166,18 @@ static int command(void) return STAT_ERR; } +static int end(void) +{ + int ret; + + while (1) { + ret = accept(TOK_NEW_LN); + CHECK_OK(ret); + } + + return STAT_OK; +} + static int commands(void) { accept(TOK_NEW_LN); @@ -167,21 +185,31 @@ static int commands(void) while (1) { CHECK_NULL(accept(TOK_EOF)); CHECK_OK(command()); - CHECK_OK(accept(TOK_NEW_LN)); + CHECK_OK(end()); } } static int parse(void) { +#ifndef PARSE_DEBUG next_token(); - if (commands() == STAT_ERR) return ERR; +#endif + +#ifdef PARSE_DEBUG + while (1) { + next_token(); + if (token.type == TOK_EOF) + break; + printf("%s\n", lexer_token_type_str(token.type)); + } +#endif return OK; } -int config_load(VectorPreview *prevs, char *filename, char *any_type_) +int config_load(VectorPreview *prevs, char *filename) { int ret = OK; @@ -190,7 +218,6 @@ int config_load(VectorPreview *prevs, char *filename, char *any_type_) lexer = lexer_init(f); previews = prevs; - any_type = any_type_; ERRCHK_GOTO_OK(parse(), ret, file); diff --git a/config.h b/config.h index 0372521..47e0158 100644 --- a/config.h +++ b/config.h @@ -3,7 +3,7 @@ #include "preview.h" -int config_load(VectorPreview *prevs, char *filename, char *any_type_); +int config_load(VectorPreview *prevs, char *filename); void config_cleanup(void); #endif diff --git a/ctpv.c b/ctpv.c index c908661..9b59d80 100644 --- a/ctpv.c +++ b/ctpv.c @@ -8,6 +8,7 @@ #include #include +#include "ctpv.h" #include "error.h" #include "utils.h" #include "config.h" @@ -15,9 +16,7 @@ #include "preview.h" #include "previews.h" -#define ANY_TYPE "*" - -static char any_type[] = ANY_TYPE; +const char any_type[] = ANY_TYPE; static magic_t magic; @@ -37,7 +36,7 @@ static VectorPreview *previews; static void cleanup(void) { - cleanup_previews(); + previews_cleanup(); config_cleanup(); if (magic != NULL) magic_close(magic); @@ -78,17 +77,19 @@ static int get_config_file(char *buf, size_t len) return OK; } -static int init_previews_v(void) +static int init_previews(void) { - previews = vectorPreview_new(LEN(b_previews)); + /* 20 is some arbitrary number, it's here in order to + * to save one realloc() if user has less then 20 custom previews */ + previews = vectorPreview_new(LEN(b_previews) + 20); vectorPreview_append_arr(previews, b_previews, LEN(b_previews)); char config_file[FILENAME_MAX]; get_config_file(config_file, LEN(config_file)); - ERRCHK_RET_OK(config_load(previews, config_file, any_type)); + ERRCHK_RET_OK(config_load(previews, config_file)); - init_previews(previews->buf, previews->len); + previews_init(previews->buf, previews->len); return OK; } @@ -104,22 +105,6 @@ static const char *get_mimetype(const char *path) return r; } -static const char *get_ext(const char *path) -{ - const char *base; - - if ((base = strrchr(path, '/'))) - base += sizeof(*base); - else - base = path; - - const char *dot = strchr(base, '.'); - if (!dot || dot == base) - return NULL; - - return &dot[1]; -} - static int check_file(const char *f) { if (!f) { @@ -207,7 +192,7 @@ static int preview(int argc, char *argv[]) ERRCHK_RET_OK(init_magic()); - ERRCHK_RET_OK(init_previews_v()); + ERRCHK_RET_OK(init_previews()); const char *mimetype; ERRCHK_RET(!(mimetype = get_mimetype(f))); @@ -223,7 +208,7 @@ static int preview(int argc, char *argv[]) .cache_file = cache_file, .cache_valid = cache_valid, }; - return run_preview(get_ext(f), mimetype, &args); + return preview_run(get_ext(f), mimetype, &args); } static int server(void) @@ -243,10 +228,10 @@ static int end(void) static int list(void) { - ERRCHK_RET_OK(init_previews_v()); + ERRCHK_RET_OK(init_previews()); size_t len; - Preview p, **list = get_previews_list(&len); + Preview p, **list = previews_get(&len); const char *n, *e, *t, *s; const char header_name[] = "Name", header_ext[] = "Extension", diff --git a/ctpv.h b/ctpv.h new file mode 100644 index 0000000..5dde894 --- /dev/null +++ b/ctpv.h @@ -0,0 +1,8 @@ +#ifndef CTPV_H +#define CTPV_H + +#define ANY_TYPE "*" + +extern const char any_type[]; + +#endif diff --git a/preview.c b/preview.c index 33e42e9..34cc788 100644 --- a/preview.c +++ b/preview.c @@ -40,7 +40,7 @@ static int cmp_previews(const void *p1, const void *p2) return i; } -void init_previews(Preview *ps, size_t len) +void previews_init(Preview *ps, size_t len) { previews.len = len; @@ -56,7 +56,7 @@ void init_previews(Preview *ps, size_t len) qsort(previews.list, previews.len, PREVP_SIZE, cmp_previews); } -void cleanup_previews(void) +void previews_cleanup(void) { if (!previews.list) return; @@ -156,7 +156,7 @@ static int run(Preview *p, int *exitcode) ERRNOS); \ } while (0) -int run_preview(const char *ext, const char *mimetype, PreviewArgs *pa) +int preview_run(const char *ext, const char *mimetype, PreviewArgs *pa) { if (pa->id || (pa->id = getenv("id"))) ERRCHK_RET_OK(server_set_fifo_var(pa->id)); @@ -199,7 +199,7 @@ run: return exitcode == 0 ? OK : ERR; } -Preview **get_previews_list(size_t *len) +Preview **previews_get(size_t *len) { check_init_previews(); *len = previews.len; diff --git a/preview.h b/preview.h index d388250..b96ac7c 100644 --- a/preview.h +++ b/preview.h @@ -19,9 +19,9 @@ typedef struct { int cache_valid; } PreviewArgs; -void init_previews(Preview *ps, size_t len); -void cleanup_previews(void); -int run_preview(const char *ext, const char *mimetype, PreviewArgs *pa); -Preview **get_previews_list(size_t *len); +void previews_init(Preview *ps, size_t len); +void previews_cleanup(void); +int preview_run(const char *ext, const char *mimetype, PreviewArgs *pa); +Preview **previews_get(size_t *len); #endif diff --git a/utils.c b/utils.c index 2f22f26..c59298f 100644 --- a/utils.c +++ b/utils.c @@ -130,3 +130,19 @@ int mkpath(char* file_path, mode_t mode) return 0; } + +const char *get_ext(const char *path) +{ + const char *base; + + if ((base = strrchr(path, '/'))) + base += sizeof(*base); + else + base = path; + + const char *dot = strchr(base, '.'); + if (!dot || dot == base) + return NULL; + + return &dot[1]; +} diff --git a/utils.h b/utils.h index 31091a4..97c7d9c 100644 --- a/utils.h +++ b/utils.h @@ -35,5 +35,6 @@ int get_cache_dir(char *buf, size_t len, char *name); int get_config_dir(char *buf, size_t len, char *name); int mkpath(char* file_path, mode_t mode); +const char *get_ext(const char *path); #endif