Fixes, improvements

This commit is contained in:
Nikita Ivanov 2022-06-08 09:03:40 +05:00
parent 154a0bfd41
commit 1651ae8df9
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
8 changed files with 81 additions and 44 deletions

View File

@ -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);

View File

@ -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

41
ctpv.c
View File

@ -8,6 +8,7 @@
#include <sys/stat.h>
#include <openssl/md5.h>
#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",

8
ctpv.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef CTPV_H
#define CTPV_H
#define ANY_TYPE "*"
extern const char any_type[];
#endif

View File

@ -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;

View File

@ -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

16
utils.c
View File

@ -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];
}

View File

@ -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