Prepend every script with helpers.sh in runtime

This commit is contained in:
Nikita Ivanov 2022-05-28 22:33:18 +05:00
parent d921371610
commit 5bdf9d372b
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
7 changed files with 74 additions and 19 deletions

View File

@ -5,7 +5,7 @@ SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o) OBJ := $(SRC:.c=.o)
DEP := $(OBJ:.o=.d) DEP := $(OBJ:.o=.d)
PRE := $(wildcard prev/*.sh) PRE := $(wildcard prev/*.sh)
GEN := gen/prev/scripts.h gen/server.h GEN := gen/prev/scripts.h gen/server.h gen/helpers.h
CFLAGS += -Os -MD -Wall -Wextra -Wno-unused-parameter CFLAGS += -Os -MD -Wall -Wextra -Wno-unused-parameter
LDFLAGS += -lmagic -lcrypto LDFLAGS += -lmagic -lcrypto
@ -35,13 +35,17 @@ ctpv: $(OBJ)
ctpv.c: $(GEN) ctpv.c: $(GEN)
gen/prev/scripts.h: $(PRE) embed/embed helpers.sh gen/prev/scripts.h: $(PRE) embed/embed
@mkdir -p $(@D) @mkdir -p $(@D)
embed/embed -p prev_scr_ -h helpers.sh $(PRE) > $@ embed/embed -p prev_scr_ $(PRE) > $@
gen/server.h: clear.sh end.sh embed/embed helpers.sh gen/server.h: clear.sh end.sh embed/embed
@mkdir -p $(@D) @mkdir -p $(@D)
embed/embed -p scr_ -h helpers.sh clear.sh end.sh > $@ embed/embed -p scr_ clear.sh end.sh > $@
gen/helpers.h: helpers.sh embed/embed
@mkdir -p $(@D)
embed/embed -p scr_ helpers.sh > $@
embed/embed: make_embed embed/embed: make_embed
@: @:

View File

@ -123,9 +123,11 @@ static int run(Preview *p, int *exitcode)
int sp_arg[] = { pipe_fds[0], pipe_fds[1], STDERR_FILENO }; int sp_arg[] = { pipe_fds[0], pipe_fds[1], STDERR_FILENO };
char *args[] = SHELL_ARGS(p->script); char *script = prepend_helpers(p->script, p->script_len - 1);
char *args[] = SHELL_ARGS(script);
int ret = spawn(args, NULL, exitcode, spawn_redirect, sp_arg); int ret = spawn(args, NULL, exitcode, spawn_redirect, sp_arg);
free(script);
close(pipe_fds[1]); close(pipe_fds[1]);
if (*exitcode != FAILED_PREVIEW_EC) { if (*exitcode != FAILED_PREVIEW_EC) {

View File

@ -6,6 +6,7 @@
typedef struct { typedef struct {
char *name, *ext, *type, *subtype, *script; char *name, *ext, *type, *subtype, *script;
int priority; int priority;
size_t script_len;
} Preview; } Preview;
typedef struct { typedef struct {

View File

@ -1,14 +1,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include "gen/prev/scripts.h" #include "utils.h"
#include "preview.h" #include "preview.h"
#include "gen/prev/scripts.h"
/* /*
* This file is supposed to be included in ctpv.c * This file is supposed to be included in ctpv.c
*/ */
#define PP(e, t, s, n, p) { #n, e, t, s, prev_scr_##n##_sh, p } #define PNAME(n) prev_scr_##n##_sh
#define PP(e, t, s, n, p) { #n, e, t, s, PNAME(n), p, LEN(PNAME(n)) }
#define PR(e, t, s, n) PP(e, t, s, n, 0) #define PR(e, t, s, n) PP(e, t, s, n, 0)
Preview previews[] = { Preview previews[] = {
@ -21,3 +23,5 @@ Preview previews[] = {
PR(NULL, "video", NULL, video), PR(NULL, "video", NULL, video),
PR(NULL, "application", "pdf", pdf), PR(NULL, "application", "pdf", pdf),
}; };
/* vim: set nowrap: */

View File

@ -118,20 +118,26 @@ exit:
return ret; return ret;
} }
static int run_script(char *script, size_t script_len, char *arg)
{
int ret = OK;
char *s = prepend_helpers(script, script_len);
char *args[] = SHELL_ARGS(s, arg);
int exitcode;
ERRCHK_GOTO_OK(spawn(args, NULL, &exitcode, NULL, NULL), ret, cleanup);
cleanup:
free(s);
return ret;
}
int server_clear(void) int server_clear(void)
{ {
char *args[] = SHELL_ARGS(scr_clear_sh); return run_script(scr_clear_sh, LEN(scr_clear_sh)-1, "");
int exitcode;
ERRCHK_RET_OK(spawn(args, NULL, &exitcode, NULL, NULL));
return OK;
} }
int server_end(const char *id_s) int server_end(const char *id_s)
{ {
char *args[] = SHELL_ARGS(scr_end_sh, (char *)id_s); return run_script(scr_end_sh, LEN(scr_end_sh)-1, (char *)id_s);
int exitcode;
ERRCHK_RET_OK(spawn(args, NULL, &exitcode, NULL, NULL));
return OK;
} }

34
shell.c Normal file
View File

@ -0,0 +1,34 @@
#include <string.h>
#include "shell.h"
#include "error.h"
#include "gen/helpers.h"
/*
* Returns string with helpers.sh prepended
*
* User must call free()
*/
char *prepend_helpers(char *str, size_t len)
{
char *buf, *b;
size_t mlen;
if (!(buf = malloc(sizeof(*buf) * (LEN(scr_helpers_sh) + len)))) {
PRINTINTERR(FUNCFAILED("malloc"), ERRNOS);
abort();
}
b = buf;
mlen = LEN(scr_helpers_sh);
memcpy(b, scr_helpers_sh, mlen);
b += (mlen - 1) * sizeof(*str);
mlen = len;
memcpy(b, str, mlen);
b += mlen * sizeof(*str);
b[0] = '\0';
return buf;
}

View File

@ -1,7 +1,11 @@
#ifndef SHELL_H #ifndef SHELL_H
#define SHELL_H #define SHELL_H
#include "utils.h"
#define SHELL_ARGS(script, ...) \ #define SHELL_ARGS(script, ...) \
{ "sh", "-c", script, "sh", __VA_ARGS__ __VA_OPT__(,) NULL } { "/bin/sh", "-c", script, "/bin/sh", __VA_ARGS__ __VA_OPT__(,) NULL }
char *prepend_helpers(char *str, size_t len);
#endif #endif