Refactor: use enum Result

This commit is contained in:
Nikita Ivanov 2022-07-25 00:38:02 +05:00
parent 5e76a260d2
commit eb6714d9d4
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
16 changed files with 87 additions and 71 deletions

7
src/attrs.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef ATTRS_H
#define ATTRS_H
#define UNUSED __attribute__((unused))
#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#endif

View File

@ -97,7 +97,7 @@ static void add_preview(Parser *ctx, char *name, char *script, struct TypeSet *s
}
}
static int add_priority(Parser *ctx, char *name, int priority)
static RESULT add_priority(Parser *ctx, char *name, int priority)
{
if (!ctx->previews)
return OK;
@ -115,7 +115,7 @@ static int add_priority(Parser *ctx, char *name, int priority)
return found ? OK : ERR;
}
static int remove_preview(Parser *ctx, char *name)
static RESULT remove_preview(Parser *ctx, char *name)
{
if (!ctx->previews)
return OK;
@ -367,7 +367,7 @@ static int commands(Parser *ctx)
}
}
static int parse(Parser *ctx)
static RESULT parse(Parser *ctx)
{
#ifdef DEBUG_LEXER
while (1) {
@ -397,9 +397,9 @@ static int parse(Parser *ctx)
return OK;
}
int config_load(Parser **ctx, VectorPreview *prevs, char *filename)
RESULT config_load(Parser **ctx, VectorPreview *prevs, char *filename)
{
int ret = OK;
enum Result ret = OK;
FILE *f;
ERRCHK_GOTO_ERN(!(f = fopen(filename, "r")), ret, exit);

View File

@ -5,7 +5,7 @@
typedef struct Parser Parser;
int config_load(Parser **ctx, VectorPreview *prevs, char *filename);
RESULT config_load(Parser **ctx, VectorPreview *prevs, char *filename);
void config_cleanup(Parser *ctx);
#endif

View File

@ -41,7 +41,7 @@ static void cleanup(void)
vectorPreview_free(previews);
}
static int init_magic(void)
static RESULT init_magic(void)
{
ERRCHK_RET_MSG(!(magic = magic_open(MAGIC_MIME_TYPE)), magic_error(magic));
@ -50,7 +50,7 @@ static int init_magic(void)
return OK;
}
static int create_dir(char *buf, size_t len)
static RESULT create_dir(char *buf, size_t len)
{
char dir[len];
strncpy(dir, buf, LEN(dir) - 1);
@ -59,7 +59,7 @@ static int create_dir(char *buf, size_t len)
return OK;
}
static int get_config_file(char *buf, size_t len)
static RESULT get_config_file(char *buf, size_t len)
{
ERRCHK_RET_OK(get_config_dir(buf, len, "ctpv/"));
ERRCHK_RET_OK(create_dir(buf, len));
@ -72,7 +72,7 @@ static int get_config_file(char *buf, size_t len)
return OK;
}
static int config(int prevs)
static RESULT config(int prevs)
{
char config_file[FILENAME_MAX];
ERRCHK_RET_OK(get_config_file(config_file, LEN(config_file)));
@ -82,7 +82,7 @@ static int config(int prevs)
return OK;
}
static int init_previews(void)
static RESULT init_previews(void)
{
/* 20 is some arbitrary number, it's here in order to
* to save one realloc() if user has less then 20 custom previews */
@ -112,7 +112,7 @@ static inline void file_access_err(char *f, int errno_)
print_errorf("failed to access '%s': %s", f, strerror(errno_));
}
static int get_input_file(struct InputFile *input_f, char *f)
static RESULT get_input_file(struct InputFile *input_f, char *f)
{
if (!f) {
print_error("file not given");
@ -137,7 +137,7 @@ static int get_input_file(struct InputFile *input_f, char *f)
return OK;
}
static int is_newer(int *resp, char *f1, char *f2)
static RESULT is_newer(int *resp, char *f1, char *f2)
{
struct stat stat1, stat2;
ERRCHK_RET_ERN(lstat(f1, &stat1) == -1);
@ -171,8 +171,8 @@ static void md5_string(char *buf, size_t len, char *s)
}
}
static int get_cache_file(char *dir, size_t dir_len, char *filename,
size_t filename_len, char *file)
static RESULT get_cache_file(char *dir, size_t dir_len, char *filename,
size_t filename_len, char *file)
{
ERRCHK_RET_OK(get_cache_dir(dir, dir_len, "ctpv/"));
ERRCHK_RET_OK(create_dir(dir, dir_len));
@ -189,7 +189,7 @@ static int get_cache_file(char *dir, size_t dir_len, char *filename,
return OK;
}
static int check_cache(int *resp, char *file, char *cache_file)
static RESULT check_cache(int *resp, char *file, char *cache_file)
{
if (access(cache_file, F_OK) != 0) {
*resp = 0;
@ -201,7 +201,7 @@ static int check_cache(int *resp, char *file, char *cache_file)
#define GET_PARG(a, i) (a) = (argc > (i) ? argv[i] : NULL)
static int preview(int argc, char *argv[])
static RESULT preview(int argc, char *argv[])
{
char *f, *w, *h, *x, *y, *id;
char y_buf[4], h_buf[4];
@ -261,24 +261,24 @@ static int preview(int argc, char *argv[])
return preview_run(get_ext(input_f.path), mimetype, &args);
}
static int server(void)
static RESULT server(void)
{
return server_listen(ctpv.server_id_s);
}
static int clear(void)
static RESULT clear(void)
{
ERRCHK_RET_OK(config(0));
return server_clear(ctpv.server_id_s);
}
static int end(void)
static RESULT end(void)
{
ERRCHK_RET_OK(config(0));
return server_end(ctpv.server_id_s);
}
static int list(void)
static RESULT list(void)
{
ERRCHK_RET_OK(init_previews());
@ -338,7 +338,7 @@ static int list(void)
return OK;
}
static int mime(int argc, char *argv[])
static RESULT mime(int argc, char *argv[])
{
const char *mimetype;
struct InputFile input_f;
@ -399,7 +399,7 @@ int main(int argc, char *argv[])
argc -= optind;
argv = &argv[optind];
int ret;
enum Result ret;
switch (ctpv.mode) {
case MODE_PREVIEW:
ret = preview(argc, argv);

View File

@ -59,11 +59,6 @@
#define ERRCHK_RET_OK(e) ERRCHK_RET((e) != OK)
#define ERRCHK_GOTO_OK(e, r, l) ERRCHK_GOTO((e) != OK, r, l)
enum {
OK,
ERR,
};
void print_error(const char *error_msg);
void print_errorf(const char *format, ...);
void print_int_error(const char *file, unsigned long line, const char *msg);

View File

@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#define PARSEERROR(c, format, ...) \
#define PARSEERROR(c, format, ...) \
print_errorf("config:%u:%u: " format, (c).line, \
(c).col __VA_OPT__(, ) __VA_ARGS__)

View File

@ -125,14 +125,14 @@ static void check_init_previews(void)
}
}
static int run(Preview *p, int *exitcode, int *signal)
static RESULT run(Preview *p, int *exitcode, int *signal)
{
int pipe_fds[2];
ERRCHK_RET_ERN(pipe(pipe_fds) == -1);
int sp_arg[] = { pipe_fds[0], pipe_fds[1], STDERR_FILENO };
int ret = run_script(p->script, p->script_len, exitcode, signal, spawn_redirect, sp_arg);
enum Result ret = run_script(p->script, p->script_len, exitcode, signal, spawn_redirect, sp_arg);
close(pipe_fds[1]);
@ -160,7 +160,7 @@ static int run(Preview *p, int *exitcode, int *signal)
ERRCHK_RET_ERN(setenv((n), (v), 1) != 0); \
} while (0)
int preview_run(const char *ext, const char *mimetype, PreviewArgs *pa)
RESULT 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));

View File

@ -4,6 +4,7 @@
#include <stdlib.h>
#include "vector.h"
#include "result.h"
#define MIMETYPE_MAX 64
@ -29,7 +30,7 @@ typedef struct {
void previews_init(Preview *ps, size_t len);
void previews_cleanup(void);
int preview_run(const char *ext, const char *mimetype, PreviewArgs *pa);
RESULT preview_run(const char *ext, const char *mimetype, PreviewArgs *pa);
Preview **previews_get(size_t *len);
#endif

13
src/result.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef RESULT_H
#define RESULT_H
#include "attrs.h"
#define RESULT enum Result WARN_UNUSED_RESULT
enum Result {
OK,
ERR,
};
#endif

View File

@ -5,6 +5,7 @@
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "error.h"
#include "utils.h"
@ -26,7 +27,7 @@ static void kill_ueberzug(void)
FUNCFAILED("kill", strerror(errno));
}
spawn_wait(ueberzug_pid, NULL, NULL);
waitpid(ueberzug_pid, NULL, 0);
}
static void sig_handler_exit(int s)
@ -34,16 +35,16 @@ static void sig_handler_exit(int s)
do_exit = 1;
}
static int open_fifo(int *fd, char *f)
static RESULT open_fifo(int *fd, char *f)
{
ERRCHK_RET_ERN((*fd = open(f, O_RDONLY | O_NONBLOCK)) == -1);
return OK;
}
static int listen(char *fifo)
static RESULT listen(char *fifo)
{
int ret = OK;
enum Result ret = OK;
struct pollfd pollfd = { .fd = -1, .events = POLLIN };
ERRCHK_GOTO_OK(open_fifo(&pollfd.fd, fifo), ret, exit);
@ -117,7 +118,7 @@ exit:
return ret;
}
static int check_ueberzug(int *exitcode)
static RESULT check_ueberzug(int *exitcode)
{
char *args[] = SHELL_ARGS("command -v ueberzug > /dev/null");
return spawn(args, NULL, exitcode, NULL, NULL, NULL);
@ -128,9 +129,9 @@ static void get_fifo_name(char *buf, size_t len, const char *id_s)
snprintf(buf, len-1, "/tmp/ctpvfifo.%s", id_s);
}
int server_listen(const char *id_s)
RESULT server_listen(const char *id_s)
{
int ret = OK;
enum Result ret = OK;
int exitcode;
ERRCHK_GOTO_OK(check_ueberzug(&exitcode), ret, exit);
@ -168,7 +169,7 @@ static inline int run_server_script(char *script, size_t script_len, char *arg)
return run_script(script, script_len, NULL, NULL, NULL, NULL);
}
int server_set_fifo_var(const char *id_s)
RESULT server_set_fifo_var(const char *id_s)
{
char fifo[FIFO_FILENAME_SIZE];
get_fifo_name(fifo, LEN(fifo), id_s);
@ -177,14 +178,14 @@ int server_set_fifo_var(const char *id_s)
return OK;
}
int server_clear(const char *id_s)
RESULT server_clear(const char *id_s)
{
ERRCHK_RET_OK(server_set_fifo_var(id_s));
return run_server_script(scr_clear_sh, LEN(scr_clear_sh), (char *)id_s);
}
int server_end(const char *id_s)
RESULT server_end(const char *id_s)
{
ERRCHK_RET_OK(server_set_fifo_var(id_s));

View File

@ -1,9 +1,9 @@
#ifndef SERVER_H
#define SERVER_H
int server_listen(const char *id_s);
int server_set_fifo_var(const char *id_s);
int server_clear(const char *id_s);
int server_end(const char *id_s);
RESULT server_listen(const char *id_s);
RESULT server_set_fifo_var(const char *id_s);
RESULT server_clear(const char *id_s);
RESULT server_end(const char *id_s);
#endif

View File

@ -34,8 +34,8 @@ static char *prepend_helpers(char *str, size_t len)
#define OPT_SETENV(name) \
ERRCHK_RET_ERN(setenv((#name), ctpv.opts.name ? "1" : "", 1) == -1)
int run_script(char *script, size_t script_len, int *exitcode, int *signal,
SpawnProg sp, void *sp_arg)
RESULT run_script(char *script, size_t script_len, int *exitcode, int *signal,
SpawnProg sp, void *sp_arg)
{
OPT_SETENV(forcekitty);
OPT_SETENV(forcekittyanim);
@ -46,7 +46,7 @@ int run_script(char *script, size_t script_len, int *exitcode, int *signal,
char *scr = prepend_helpers(script, script_len);
char *args[] = SHELL_ARGS(scr);
int ret = spawn(args, NULL, exitcode, signal, sp, sp_arg);
enum Result ret = spawn(args, NULL, exitcode, signal, sp, sp_arg);
free(scr);

View File

@ -6,8 +6,7 @@
#define SHELL_ARGS(script, ...) \
{ "/bin/sh", "-c", script, "/bin/sh", __VA_ARGS__ __VA_OPT__(,) NULL }
int run_script(char *script, size_t script_len, int *exitcode, int *signal,
SpawnProg sp, void *sp_arg);
RESULT run_script(char *script, size_t script_len, int *exitcode, int *signal,
SpawnProg sp, void *sp_arg);
#endif

View File

@ -11,7 +11,7 @@
char *program = NULL;
int spawn_redirect(const void *arg)
RESULT spawn_redirect(const void *arg)
{
int *fds = (int *)arg;
@ -21,7 +21,7 @@ int spawn_redirect(const void *arg)
return OK;
}
int spawn_wait(int pid, int *exitcode, int *signal)
RESULT spawn_wait(int pid, int *exitcode, int *signal)
{
int stat;
ERRCHK_RET_ERN(waitpid(pid, &stat, 0) == -1);
@ -51,8 +51,8 @@ int spawn_wait(int pid, int *exitcode, int *signal)
*
* cfunc is a function to call when child process is created
*/
int spawn(char *args[], int *cpid, int *exitcode, int *signal, SpawnProg cfunc,
const void *carg)
RESULT spawn(char *args[], int *cpid, int *exitcode, int *signal,
SpawnProg cfunc, const void *carg)
{
if (exitcode)
*exitcode = -1;
@ -99,7 +99,7 @@ int strlennull(const char *s)
return s ? strlen(s) : 0;
}
static int get_xdg_dir(char *buf, size_t len, char *var, char *var_sub, char *name)
static RESULT get_xdg_dir(char *buf, size_t len, char *var, char *var_sub, char *name)
{
char *home, *dir, dir_buf[FILENAME_MAX];
@ -115,12 +115,12 @@ static int get_xdg_dir(char *buf, size_t len, char *var, char *var_sub, char *na
return OK;
}
int get_cache_dir(char *buf, size_t len, char *name)
RESULT get_cache_dir(char *buf, size_t len, char *name)
{
return get_xdg_dir(buf, len, "XDG_CACHE_HOME", ".cache", name);
}
int get_config_dir(char *buf, size_t len, char *name)
RESULT get_config_dir(char *buf, size_t len, char *name)
{
return get_xdg_dir(buf, len, "XDG_CONFIG_HOME", ".config", name);
}
@ -160,7 +160,7 @@ const char *get_ext(const char *path)
return dot;
}
int register_signal(int sig, SigHandler handler)
RESULT register_signal(int sig, SigHandler handler)
{
ERRCHK_RET_ERN(signal(sig, handler) == SIG_ERR);
return OK;

View File

@ -4,6 +4,8 @@
#include <stdlib.h>
#include <stdarg.h>
#include "result.h"
#define NOTEXIST_EC 127
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
@ -21,28 +23,26 @@
va_end(args); \
} while (0)
#define UNUSED __attribute__((unused))
typedef int (*SpawnProg)(const void *);
typedef enum Result (*SpawnProg)(const void *);
typedef void (*SigHandler)(int);
extern char *program;
int spawn_redirect(const void *arg);
int spawn_wait(int pid, int *exitcode, int *signal);
int spawn(char *args[], int *cpid, int *exitcode, int *signal,
RESULT spawn_redirect(const void *arg);
RESULT spawn_wait(int pid, int *exitcode, int *signal);
RESULT spawn(char *args[], int *cpid, int *exitcode, int *signal,
SpawnProg cfunc, const void *carg);
int strcmpnull(const char *s1, const char *s2);
int strlennull(const char *s);
int get_cache_dir(char *buf, size_t len, char *name);
int get_config_dir(char *buf, size_t len, char *name);
RESULT get_cache_dir(char *buf, size_t len, char *name);
RESULT get_config_dir(char *buf, size_t len, char *name);
int mkpath(char* file_path, int mode);
const char *get_ext(const char *path);
int register_signal(int sig, SigHandler handler);
RESULT register_signal(int sig, SigHandler handler);
#endif

View File

@ -3,7 +3,7 @@
#include <stdlib.h>
#include "utils.h"
#include "attrs.h"
#define VECTOR_TYPE(name, type) \
typedef struct { \