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