summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorPanu Matilainen <pmatilai@redhat.com>2021-12-17 14:03:35 +0200
committerPanu Matilainen <pmatilai@redhat.com>2021-12-17 14:57:14 +0200
commitbf58cfd08f344872d30082c123a0035429d51742 (patch)
tree378d638002142004feb422c6698079dbf3e3a6a2 /tools
parent67280003444e25e42bbc97170e9b12a5d7fb1cc6 (diff)
downloadrpm-bf58cfd08f344872d30082c123a0035429d51742.tar.gz
Add rpmlua wrapper command for running our Lua interpreter standalone
Being able to run stuff easily in rpm context helps developing and debugging scriptlets and macros too. Supports running one-liner statements from the cli, regular scripts and an interactive session. This is placed into a separate executable for, well, separation and simplicity, but it'll also give us means to link to readline without dragging that to main rpm dependencies (but that's left for later).
Diffstat (limited to 'tools')
-rw-r--r--tools/rpmlua.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/tools/rpmlua.c b/tools/rpmlua.c
new file mode 100644
index 000000000..b179e1c54
--- /dev/null
+++ b/tools/rpmlua.c
@@ -0,0 +1,73 @@
+#include "system.h"
+
+#include <popt.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <rpm/rpmcli.h>
+#include "rpmio/rpmlua.h"
+#include "rpmio/rpmio_internal.h"
+#include "debug.h"
+
+static char *opts = NULL;
+static char *execute = NULL;
+static int interactive = 0;
+
+static struct poptOption optionsTable[] = {
+
+ { "opts", 'o', POPT_ARG_STRING, &opts, 0,
+ N_("getopt string for scripts options"), NULL },
+ { "execute", 'e', POPT_ARG_STRING, &execute, 0,
+ N_("execute statement"), NULL },
+ { "interactive", 'i', POPT_ARG_VAL, &interactive, 1,
+ N_("interactive session"), NULL },
+
+ POPT_AUTOALIAS
+ POPT_AUTOHELP
+ POPT_TABLEEND
+};
+
+int main(int argc, char *argv[])
+{
+ int ec = EXIT_FAILURE;
+ poptContext optCon = NULL;
+ ARGV_t args = NULL;
+ char *buf = NULL;
+ ssize_t blen = 0;
+ const char *script = NULL;
+ const char *name = "<cli>"
+
+ xsetprogname(argv[0]);
+
+ optCon = rpmcliInit(argc, argv, optionsTable);
+
+ if (optCon == NULL) {
+ poptPrintUsage(optCon, stderr, 0);
+ goto exit;
+ }
+
+ args = (ARGV_t)poptGetArgs(optCon);
+ if (execute) {
+ script = execute;
+ } else if (argc >= 1) {
+ if (rpmioSlurp(args[0], (uint8_t **) &buf, &blen)) {
+ fprintf(stderr, "reading %s failed: %s\n",
+ args[0], strerror(errno));
+ goto exit;
+ }
+ script = buf;
+ name = args[0];
+ }
+
+ if (script)
+ ec = rpmluaRunScript(NULL, script, name, opts, args);
+
+ if (interactive)
+ rpmluaInteractive(NULL);
+
+exit:
+ free(buf);
+ rpmcliFini(optCon);
+ return ec;
+}