2022-05-22 09:55:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2022-05-23 03:32:09 +02:00
|
|
|
#include <unistd.h>
|
2022-05-22 09:55:04 +02:00
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "preview.h"
|
|
|
|
|
|
|
|
#define PREVP_SIZE sizeof(Preview *)
|
|
|
|
|
|
|
|
static char shell[] = "sh";
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
static Preview **prevs;
|
|
|
|
static size_t prevs_len;
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
static int cmp_previews(const void *p1, const void *p2)
|
2022-05-22 09:55:04 +02:00
|
|
|
{
|
|
|
|
Preview *pr1 = *(Preview **)p1;
|
|
|
|
Preview *pr2 = *(Preview **)p2;
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
if (pr1->ext && pr2->ext)
|
|
|
|
return strcmp(pr1->ext, pr2->ext);
|
|
|
|
else if (!pr1->ext && pr2->ext)
|
2022-05-22 09:55:04 +02:00
|
|
|
return 1;
|
2022-05-23 04:45:06 +02:00
|
|
|
else if (pr1->ext && !pr2->ext)
|
2022-05-22 09:55:04 +02:00
|
|
|
return -1;
|
|
|
|
|
2022-05-22 11:56:44 +02:00
|
|
|
if (!pr1->type && pr2->type)
|
2022-05-22 09:55:04 +02:00
|
|
|
return 1;
|
2022-05-22 11:56:44 +02:00
|
|
|
else if (pr1->type && !pr2->type)
|
2022-05-22 09:55:04 +02:00
|
|
|
return -1;
|
2022-05-22 11:56:44 +02:00
|
|
|
else if (!pr1->type && !pr2->type)
|
|
|
|
return 0;
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-22 11:56:44 +02:00
|
|
|
if (!pr1->subtype && pr2->subtype)
|
2022-05-22 09:55:04 +02:00
|
|
|
return 1;
|
2022-05-22 11:56:44 +02:00
|
|
|
else if (pr1->subtype && !pr2->subtype)
|
2022-05-22 09:55:04 +02:00
|
|
|
return -1;
|
2022-05-22 11:56:44 +02:00
|
|
|
|
|
|
|
int ret = strcmp(pr1->type, pr2->type);
|
|
|
|
|
|
|
|
if (ret == 0 && pr1->subtype && pr2->subtype)
|
|
|
|
return strcmp(pr1->subtype, pr2->subtype);
|
|
|
|
|
|
|
|
return ret;
|
2022-05-22 09:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_previews(Preview *ps, size_t len)
|
|
|
|
{
|
2022-05-23 04:45:06 +02:00
|
|
|
prevs_len = len;
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
prevs = malloc(len * PREVP_SIZE);
|
|
|
|
if (!prevs) {
|
2022-05-22 09:55:04 +02:00
|
|
|
print_error("malloc() failed");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++)
|
2022-05-23 04:45:06 +02:00
|
|
|
prevs[i] = &ps[i];
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
qsort(prevs, len, PREVP_SIZE, cmp_previews);
|
2022-05-22 09:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_previews(void)
|
|
|
|
{
|
2022-05-23 04:45:06 +02:00
|
|
|
if (prevs) {
|
|
|
|
free(prevs);
|
|
|
|
prevs = NULL;
|
2022-05-22 09:55:04 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
prevs_len = 0;
|
2022-05-22 09:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void break_mimetype(char *mimetype, char **type, char **subtype)
|
|
|
|
{
|
|
|
|
*type = mimetype[0] == '\0' ? NULL : mimetype;
|
|
|
|
*subtype = NULL;
|
|
|
|
|
|
|
|
char *s = strchr(mimetype, '/');
|
|
|
|
if (!s) {
|
|
|
|
print_errorf("invalid mimetype: '%s'", mimetype);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = '\0';
|
|
|
|
*subtype = &s[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MIMETYPE_MAX 64
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
static Preview *find_preview(char const *mimetype, char const *ext, size_t *i)
|
2022-05-22 09:55:04 +02:00
|
|
|
{
|
|
|
|
Preview *p;
|
|
|
|
char mimetype_c[MIMETYPE_MAX], *t, *s;
|
|
|
|
|
|
|
|
strncpy(mimetype_c, mimetype, MIMETYPE_MAX - 1);
|
|
|
|
break_mimetype(mimetype_c, &t, &s);
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
for (; *i < prevs_len; (*i)++) {
|
|
|
|
p = prevs[*i];
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
if (!p->ext && !p->type)
|
2022-05-22 11:56:44 +02:00
|
|
|
return p;
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
if (p->ext && !ext)
|
2022-05-22 11:56:44 +02:00
|
|
|
continue;
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
if (p->ext && strcmp(ext, p->ext) == 0)
|
2022-05-22 11:56:44 +02:00
|
|
|
return p;
|
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
if (p->type && strcmp(t, p->type) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (p->type && !p->subtype)
|
|
|
|
return p;
|
|
|
|
|
|
|
|
if (p->subtype && strcmp(s, p->subtype) == 0)
|
2022-05-22 11:56:44 +02:00
|
|
|
return p;
|
2022-05-22 09:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-22 22:07:08 +02:00
|
|
|
static void check_init_previews(void)
|
2022-05-22 09:55:04 +02:00
|
|
|
{
|
2022-05-23 04:45:06 +02:00
|
|
|
if (!prevs) {
|
2022-05-22 09:55:04 +02:00
|
|
|
print_error("init_previews() not called");
|
|
|
|
abort();
|
|
|
|
}
|
2022-05-22 22:07:08 +02:00
|
|
|
}
|
|
|
|
|
2022-05-23 03:32:09 +02:00
|
|
|
static int run(Preview *p, int *exitcode)
|
2022-05-22 22:07:08 +02:00
|
|
|
{
|
2022-05-23 03:32:09 +02:00
|
|
|
char *args[] = { shell, "-c", p->script, shell, NULL };
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 03:32:09 +02:00
|
|
|
int *fds[] = { (int[]){ STDOUT_FILENO, STDERR_FILENO }, NULL };
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 03:32:09 +02:00
|
|
|
return spawn(args, NULL, exitcode, fds);
|
|
|
|
}
|
|
|
|
|
2022-05-22 09:55:04 +02:00
|
|
|
#define SET_PENV(n, v) \
|
|
|
|
do { \
|
|
|
|
if (v) \
|
|
|
|
ERRCHK_RET(setenv((n), (v), 1) != 0); \
|
|
|
|
} while (0)
|
|
|
|
|
2022-05-23 03:32:09 +02:00
|
|
|
int run_preview(const char *ext, const char *mimetype, PreviewArgs *pa)
|
2022-05-22 09:55:04 +02:00
|
|
|
{
|
|
|
|
SET_PENV("f", pa->f);
|
|
|
|
SET_PENV("w", pa->w);
|
|
|
|
SET_PENV("h", pa->h);
|
|
|
|
SET_PENV("x", pa->x);
|
|
|
|
SET_PENV("y", pa->y);
|
|
|
|
|
2022-05-23 03:32:09 +02:00
|
|
|
SET_PENV("m", mimetype);
|
|
|
|
SET_PENV("e", ext);
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 03:32:09 +02:00
|
|
|
check_init_previews();
|
2022-05-22 09:55:04 +02:00
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
Preview *p;
|
|
|
|
size_t i = 0;
|
|
|
|
int exitcode;
|
2022-05-23 03:32:09 +02:00
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
run:
|
|
|
|
p = find_preview(mimetype, ext, &i);
|
|
|
|
if (!p) {
|
|
|
|
puts("ctpv: no previews found");
|
2022-05-23 03:32:09 +02:00
|
|
|
return OK;
|
2022-05-23 04:45:06 +02:00
|
|
|
}
|
2022-05-23 03:32:09 +02:00
|
|
|
|
2022-05-23 04:45:06 +02:00
|
|
|
ERRCHK_RET_OK(run(p, &exitcode));
|
|
|
|
if (exitcode == 127) {
|
|
|
|
i++;
|
|
|
|
goto run;
|
|
|
|
}
|
2022-05-23 03:32:09 +02:00
|
|
|
|
|
|
|
return OK;
|
2022-05-22 09:55:04 +02:00
|
|
|
}
|
2022-05-22 22:07:08 +02:00
|
|
|
|
|
|
|
Preview **get_previews_list(size_t *len)
|
|
|
|
{
|
|
|
|
check_init_previews();
|
2022-05-23 04:45:06 +02:00
|
|
|
*len = prevs_len;
|
|
|
|
return prevs;
|
2022-05-22 22:07:08 +02:00
|
|
|
}
|