diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-11-12 00:43:13 +0300 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2023-11-12 00:43:13 +0300 |
commit | 2e164ac9ed33b9426c5a4b3e9c4ab0a32e3200ea (patch) | |
tree | 85aaeda5ec02538c794e5d0c9eed00970817ab5d | |
parent | 1a6ad201df8ef071cc394d4c915d7d54e187ba51 (diff) | |
download | applause2-2e164ac9ed33b9426c5a4b3e9c4ab0a32e3200ea.tar.gz |
support running scripts in batch mode
* This should be largely compatible with the standard Lua interpreter.
The `arg` array is also initialized.
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | applause.c | 103 |
2 files changed, 67 insertions, 38 deletions
@@ -40,6 +40,8 @@ Example (one channel): > Stream.SinOsc(440):play() +You can also run standalone scripts (batch mode), just like the standard Lua interpreter. + # Applause Clients (Editor Integration) echo -ne "25 \nStream.SinOsc(440):play()" | socat -,ignoreeof TCP:127.0.0.1:10000" @@ -822,7 +822,7 @@ static const NativeMethod native_methods[] = { static void usage(const char *program) { - printf("%s [-h] [-o OUTPUT] [-b SIZE]\n" + printf("%s [-h] [-o OUTPUT] [-b SIZE] [SCRIPT [ARGS]]\n" "\tOUTPUT\tNumber of output ports to reserve (default: %d)\n" "\tSIZE\tMinimum size of the output buffer in milliseconds (default: %dms)\n", program, @@ -871,12 +871,6 @@ main(int argc, char **argv) signal_action.sa_handler = SIG_IGN; sigaction(SIGPIPE, &signal_action, NULL); - /* - * Load the libhistory file. - */ - using_history(); - read_history(APPLAUSE_HISTORY); - L = luaL_newstate(); if (!L) { fprintf(stderr, "Error creating Lua state.\n"); @@ -920,42 +914,81 @@ main(int argc, char **argv) lua_pushinteger(L, (lua_Integer)jack_get_sample_rate(client)); lua_setglobal(L, "samplerate"); - /* - * Launch the command server. - */ - if (pthread_create(&command_server_thread, NULL, command_server, L)) { - perror("pthread_create"); - exit(EXIT_FAILURE); - } + if (optind < argc) { + /* + * Execute script + */ + lua_createtable(L, argc-optind, 0); + for (int i = optind; i < argc; i++) { + lua_pushinteger(L, i-optind); + lua_pushstring(L, argv[i]); + lua_settable(L, -3); + } + lua_setglobal(L, "arg"); - /* - * Main REPL loop. - * Since the command server has been launched, all lua state access - * must be synchronized using lua_mutex. - */ - for (;;) { + lua_pushcfunction(L, traceback); + + if (luaL_loadfile(L, argv[optind]) || lua_pcall(L, 0, 0, -2)) { + fprintf(stderr, "Error running %s: %s\n", + argv[optind], lua_tostring(L, -1)); + exit(EXIT_FAILURE); + } + + /* remove traceback function */ + lua_remove(L, -1); + } else { /* - * FIXME: Get global _PROMPT or _PROMPT2 + * Load the libhistory file. */ - char *line = readline("> "); + using_history(); + read_history(APPLAUSE_HISTORY); - if (!line) { - putchar('\n'); - break; + /* + * Launch the command server. + */ + if (pthread_create(&command_server_thread, NULL, command_server, L)) { + perror("pthread_create"); + exit(EXIT_FAILURE); } - pthread_mutex_lock(&lua_mutex); - do_command(L, line); - pthread_mutex_unlock(&lua_mutex); + /* + * Main REPL loop. + * Since the command server has been launched, all lua state access + * must be synchronized using lua_mutex. + */ + for (;;) { + /* + * FIXME: Get global _PROMPT or _PROMPT2 + */ + char *line = readline("> "); + + if (!line) { + putchar('\n'); + break; + } + + pthread_mutex_lock(&lua_mutex); + do_command(L, line); + pthread_mutex_unlock(&lua_mutex); - if (*line) - add_history(line); + if (*line) + add_history(line); - free(line); + free(line); + } + + /* + * Write libhistory file + */ + if (write_history(APPLAUSE_HISTORY)) + perror("write_history"); + + /* + * FIXME: Shut down connection server. + */ } /* - * FIXME: Shut down connection server. * FIXME: Clean up properly. */ #if 0 @@ -966,11 +999,5 @@ main(int argc, char **argv) lua_close(L); - /* - * Write libhistory file - */ - if (write_history(APPLAUSE_HISTORY)) - perror("write_history"); - return 0; } |