Add shell option

This commit is contained in:
Nikita Ivanov 2023-03-11 00:13:55 +01:00
parent 7f33a48138
commit 6f364cf383
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
7 changed files with 60 additions and 11 deletions

View File

@ -267,8 +267,9 @@ Example:
.
.IP
.EX
# Set an option
# Set some options
set forcekitty
set shell "/usr/bin/bash"
.sp
# Add a new preview
preview cow .moo {{
@ -288,6 +289,16 @@ An option can be set using
command.
.
.TP
.BR shell \~\c
.I path
Use
.I path
as a path to a shell to run previews with.
Use it if you have a non-POSIX compliant shell installed as a default shell.
The setting defaults to
.BR /bin/sh .
.
.TP
.B forcekitty
Always use
.I Kitty

View File

@ -1,3 +1,5 @@
#include <unistd.h>
#include "ctpv.h"
#include "lexer.h"
#include "error.h"
@ -62,6 +64,7 @@ static struct Option options[] = {
DEF_OPTION_BOOL(nosymlinkinfo),
DEF_OPTION_BOOL(autochafa),
DEF_OPTION_BOOL(showgpg),
DEF_OPTION_STR(shell),
};
static void any_type_null(char **s)
@ -415,6 +418,8 @@ RESULT config_load(Parser **ctx, VectorPreview *prevs, char *filename)
ERRCHK_GOTO_OK(parse(*ctx), ret, file);
ERRCHK_GOTO((access(ctpv.opts.shell, F_OK) != 0), ret, file, "shell '%s' was not found", ctpv.opts.shell);
file:
fclose(f);

View File

@ -394,6 +394,8 @@ int main(int argc, char *argv[])
{
program = argc > 0 ? argv[0] : "ctpv";
ctpv.opts.shell = "/bin/sh";
int c;
while ((c = getopt(argc, argv, "s:c:e:lmv")) != -1) {
switch (c) {

View File

@ -17,6 +17,7 @@ struct CTPV {
struct {
int forcekitty, forcekittyanim, forcechafa, noimages, nosymlinkinfo;
int autochafa, showgpg;
char *shell;
} opts;
};

View File

@ -240,6 +240,11 @@ static inline int issymbol(int c)
return isalnum(c) || c == '_' || c == '-';
}
static inline int isnotquote(int c)
{
return (c != '"');
}
static inline Token read_symbol(Lexer *ctx)
{
char c = peek_char(ctx);
@ -256,6 +261,25 @@ static inline Token read_symbol(Lexer *ctx)
return tok;
}
static inline Token read_string(Lexer *ctx)
{
char c = next_char(ctx);
if (isnotquote(c))
return get_tok(ctx, TOK_NULL);
record_text(ctx);
read_while(ctx, isnotquote, 1);
Token tok = get_tok(ctx, TOK_STR);
tok.val.s = get_text(ctx);
// Skip ending quote
next_char(ctx);
return tok;
}
static inline Token read_int(Lexer *ctx)
{
int positive = 1;
@ -395,6 +419,7 @@ Token lexer_get_token(Lexer *ctx)
ATTEMPT_READ(ctx, read_symbol);
ATTEMPT_READ(ctx, read_int);
ATTEMPT_READ(ctx, read_block);
ATTEMPT_READ(ctx, read_string);
PARSEERROR((*ctx), "cannot handle character: %c", peek_char(ctx));
return get_tok(ctx, TOK_ERR);

View File

@ -31,19 +31,23 @@ static char *prepend_helpers(char *str, size_t len)
return buf;
}
#define OPT_SETENV(name) \
ERRCHK_RET_ERN(setenv((#name), ctpv.opts.name ? "1" : "", 1) == -1)
#define OPT_SETENV_INT(name) \
ERRCHK_RET_ERN(setenv((#name), (ctpv.opts.name ? "1" : ""), 1) == -1)
#define OPT_SETENV_STR(name) \
ERRCHK_RET_ERN(setenv((#name), (ctpv.opts.name ? ctpv.opts.name : ""), 1) == -1)
RESULT run_script(char *script, size_t script_len, int *exitcode, int *signal,
SpawnProg sp, void *sp_arg)
{
OPT_SETENV(forcekitty);
OPT_SETENV(forcekittyanim);
OPT_SETENV(forcechafa);
OPT_SETENV(noimages);
OPT_SETENV(nosymlinkinfo);
OPT_SETENV(autochafa);
OPT_SETENV(showgpg);
OPT_SETENV_INT(forcekitty);
OPT_SETENV_INT(forcekittyanim);
OPT_SETENV_INT(forcechafa);
OPT_SETENV_INT(noimages);
OPT_SETENV_INT(nosymlinkinfo);
OPT_SETENV_INT(autochafa);
OPT_SETENV_INT(showgpg);
OPT_SETENV_STR(shell);
char *scr = prepend_helpers(script, script_len);
char *args[] = SHELL_ARGS(scr);

View File

@ -2,9 +2,10 @@
#define SHELL_H
#include "utils.h"
#include "ctpv.h"
#define SHELL_ARGS(script, ...) \
{ "/bin/sh", "-c", script, "/bin/sh", __VA_ARGS__ __VA_OPT__(,) NULL }
{ ctpv.opts.shell, "-c", script, ctpv.opts.shell, __VA_ARGS__ __VA_OPT__(,) NULL }
RESULT run_script(char *script, size_t script_len, int *exitcode, int *signal,
SpawnProg sp, void *sp_arg);