mirror of
https://github.com/NikitaIvanovV/ctpv.git
synced 2025-02-17 19:10:46 +01:00
Print file and linenum on errors
Add more macros for printing errors
This commit is contained in:
parent
ebee69678b
commit
f37e3a1885
23
ctpv.c
23
ctpv.c
@ -1,7 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <magic.h>
|
||||
|
||||
@ -32,10 +31,10 @@ static void cleanup(void) {
|
||||
|
||||
static int init_magic()
|
||||
{
|
||||
ERRCHK_RET(!(magic = magic_open(MAGIC_MIME_TYPE)),
|
||||
"magic_open() failed: %s", magic_error(magic));
|
||||
ERRCHK_RET(!(magic = magic_open(MAGIC_MIME_TYPE)), FUNCFAILED("magic_open"),
|
||||
magic_error(magic));
|
||||
|
||||
ERRCHK_RET(magic_load(magic, NULL) != 0, "magic_load() failed: %s",
|
||||
ERRCHK_RET(magic_load(magic, NULL) != 0, FUNCFAILED("magic_load"),
|
||||
magic_error(magic));
|
||||
|
||||
return OK;
|
||||
@ -50,7 +49,7 @@ static const char *get_mimetype(char const *path)
|
||||
{
|
||||
const char *r = magic_file(magic, path);
|
||||
if (!r) {
|
||||
print_errorf("magic_file() failed: %s", magic_error(magic));
|
||||
PRINTINTERR(FUNCFAILED("magic_file"), magic_error(magic));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -75,9 +74,15 @@ static const char *get_ext(char const *path)
|
||||
|
||||
static int check_file(char const *f)
|
||||
{
|
||||
ERRCHK_RET(!f, "file not given");
|
||||
ERRCHK_RET(access(f, R_OK) != 0, "failed to access '%s': %s", f,
|
||||
strerror(errno));
|
||||
if (!f) {
|
||||
print_error("file not given");
|
||||
return ERR;
|
||||
}
|
||||
|
||||
if (access(f, R_OK) != 0) {
|
||||
print_errorf("failed to access '%s': %s", f, ERRNOS);
|
||||
return ERR;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
@ -209,7 +214,7 @@ int main(int argc, char *argv[])
|
||||
ret = mime(argc, argv);
|
||||
break;
|
||||
default:
|
||||
print_errorf("unknowm mode: %d", ctpv.mode);
|
||||
PRINTINTERR("unknowm mode: %d", ctpv.mode);
|
||||
ret = ERR;
|
||||
break;
|
||||
}
|
||||
|
33
error.h
33
error.h
@ -1,19 +1,36 @@
|
||||
#ifndef ERROR_H
|
||||
#define ERROR_H
|
||||
|
||||
#define _ERRCHK_RET_PR(format, ...) \
|
||||
print_error##__VA_OPT__(f)(format __VA_OPT__(, ) __VA_ARGS__)
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#define ERRNOS strerror(errno)
|
||||
|
||||
#define FUNCFAILED(f) f "() failed: %s"
|
||||
|
||||
/*
|
||||
* Print internal error
|
||||
*/
|
||||
#define PRINTINTERR(format, ...) \
|
||||
print_error##__VA_OPT__(f)(ERRS(format) __VA_OPT__(, ) __VA_ARGS__)
|
||||
|
||||
/*
|
||||
* Add error source to error message
|
||||
*/
|
||||
#define ERRS(msg) (__FILE__ ":" STRINGIZE(__LINE__) ": " msg)
|
||||
|
||||
/*
|
||||
* If cond is true, return ERR. Also call print_error or
|
||||
* print_errorf if error message or format string is given.
|
||||
*/
|
||||
#define ERRCHK_RET(cond, ...) \
|
||||
do { \
|
||||
if (cond) { \
|
||||
__VA_OPT__(_ERRCHK_RET_PR(__VA_ARGS__);) \
|
||||
return ERR; \
|
||||
} \
|
||||
#define ERRCHK_RET(cond, ...) \
|
||||
do { \
|
||||
if (cond) { \
|
||||
__VA_OPT__(PRINTINTERR(__VA_ARGS__);) \
|
||||
return ERR; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
|
@ -1 +1,5 @@
|
||||
exiftool "$f"
|
||||
if exists exiftool; then
|
||||
exiftool "$f"
|
||||
else
|
||||
cat "$f"
|
||||
fi
|
||||
|
16
preview.c
16
preview.c
@ -1,5 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "utils.h"
|
||||
@ -41,7 +40,7 @@ void init_previews(Preview *ps, size_t len)
|
||||
|
||||
prevs = malloc(len * PREVP_SIZE);
|
||||
if (!prevs) {
|
||||
print_error("malloc() failed");
|
||||
PRINTINTERR(FUNCFAILED("malloc"), ERRNOS);
|
||||
abort();
|
||||
}
|
||||
|
||||
@ -68,7 +67,7 @@ static void break_mimetype(char *mimetype, char **type, char **subtype)
|
||||
|
||||
char *s = strchr(mimetype, '/');
|
||||
if (!s) {
|
||||
print_errorf("invalid mimetype: '%s'", mimetype);
|
||||
PRINTINTERR("invalid mimetype: '%s'", mimetype);
|
||||
abort();
|
||||
}
|
||||
|
||||
@ -107,7 +106,7 @@ static Preview *find_preview(char const *mimetype, char const *ext, size_t *i)
|
||||
static void check_init_previews(void)
|
||||
{
|
||||
if (!prevs) {
|
||||
print_error("init_previews() not called");
|
||||
PRINTINTERR("init_previews() not called");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
@ -121,10 +120,11 @@ static int run(Preview *p, int *exitcode)
|
||||
return spawn(args, NULL, exitcode, fds);
|
||||
}
|
||||
|
||||
#define SET_PENV(n, v) \
|
||||
do { \
|
||||
if (v) \
|
||||
ERRCHK_RET(setenv((n), (v), 1) != 0); \
|
||||
#define SET_PENV(n, v) \
|
||||
do { \
|
||||
if (v) \
|
||||
ERRCHK_RET(setenv((n), (v), 1) != 0, FUNCFAILED("setenv"), \
|
||||
ERRNOS); \
|
||||
} while (0)
|
||||
|
||||
int run_preview(const char *ext, const char *mimetype, PreviewArgs *pa)
|
||||
|
15
utils.c
15
utils.c
@ -1,7 +1,6 @@
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "error.h"
|
||||
@ -24,7 +23,7 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2])
|
||||
*exitcode = -1;
|
||||
|
||||
pid_t pid = fork();
|
||||
ERRCHK_RET(pid == -1, "fork() failed");
|
||||
ERRCHK_RET(pid == -1, FUNCFAILED("fork"), ERRNOS);
|
||||
|
||||
/* Child process */
|
||||
if (pid == 0) {
|
||||
@ -39,7 +38,7 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2])
|
||||
}
|
||||
|
||||
execvp(args[0], args);
|
||||
print_errorf("exec() failed: %s", strerror(errno));
|
||||
PRINTINTERR(FUNCFAILED("exec"), ERRNOS);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@ -47,8 +46,8 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2])
|
||||
*cpid = pid;
|
||||
} else {
|
||||
int stat;
|
||||
ERRCHK_RET(waitpid(pid, &stat, 0) == -1, "waitpid() failed: %s",
|
||||
strerror(errno));
|
||||
ERRCHK_RET(waitpid(pid, &stat, 0) == -1, FUNCFAILED("waitpid"),
|
||||
ERRNOS);
|
||||
|
||||
if (exitcode && WIFEXITED(stat))
|
||||
*exitcode = WEXITSTATUS(stat);
|
||||
@ -93,7 +92,7 @@ void char_v_append(CharVec *v, char c)
|
||||
if (!v->buf) {
|
||||
v->buf = malloc(v->cap * sizeof(v->buf[0]));
|
||||
if (!v->buf) {
|
||||
print_error("calloc() failed");
|
||||
PRINTINTERR(FUNCFAILED("malloc"), ERRNOS);
|
||||
abort();
|
||||
}
|
||||
v->buf[0] = '\0';
|
||||
@ -104,7 +103,7 @@ void char_v_append(CharVec *v, char c)
|
||||
v->cap *= 2;
|
||||
v->buf = realloc(v->buf, v->cap * sizeof(v->buf[0]));
|
||||
if (!v->buf) {
|
||||
print_error("realloc() failed");
|
||||
PRINTINTERR(FUNCFAILED("realloc"), ERRNOS);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user