Add --cmd option

This allows to kind of automate what profanity should do as first jobs,
e.g. `--cmd /foo --cmd /bar --cmd /quit` so one can easily check for memory
leaks.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
This commit is contained in:
Steffen Jaeckel
2025-03-06 21:16:06 +01:00
parent 0081db643f
commit 662a0be633
4 changed files with 63 additions and 22 deletions

View File

@@ -36,6 +36,13 @@ may be set to DEBUG, INFO (the default), WARN or ERROR.
.BI "\-f, \-\-logfile"
Specify a different logfile
.TP
.BI "\-r, \-\-cmd"
Specify the commands that should be run right after starting up.
This can be given multiple times, e.g.
.EX
profanity --cmd /foo --cmd "/sleep 10" --cmd /quit
.EE
.TP
.BI "\-t, \-\-theme "THEME
Specify which theme to use.
.I THEME

View File

@@ -55,16 +55,17 @@
#include "common.h"
#include "command/cmd_defs.h"
static gboolean version = FALSE;
static char* log = NULL;
static char* log_file = NULL;
static char* account_name = NULL;
static char* config_file = NULL;
static char* theme_name = NULL;
int
main(int argc, char** argv)
{
gboolean version = FALSE;
auto_gchar gchar* log = NULL;
auto_gchar gchar* log_file = NULL;
auto_gchar gchar* account_name = NULL;
auto_gchar gchar* config_file = NULL;
auto_gchar gchar* theme_name = NULL;
auto_gcharv gchar** commands = NULL;
if (argc == 2 && g_strcmp0(PACKAGE_STATUS, "development") == 0) {
if (g_strcmp0(argv[1], "docgen") == 0) {
command_docgen();
@@ -75,13 +76,14 @@ main(int argc, char** argv)
}
}
static GOptionEntry entries[] = {
GOptionEntry entries[] = {
{ "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Show version information", NULL },
{ "account", 'a', 0, G_OPTION_ARG_STRING, &account_name, "Auto connect to an account on startup" },
{ "log", 'l', 0, G_OPTION_ARG_STRING, &log, "Set logging levels, DEBUG, INFO, WARN (default), ERROR", "LEVEL" },
{ "config", 'c', 0, G_OPTION_ARG_STRING, &config_file, "Use an alternative configuration file", NULL },
{ "logfile", 'f', 0, G_OPTION_ARG_STRING, &log_file, "Specify log file", NULL },
{ "theme", 't', 0, G_OPTION_ARG_STRING, &theme_name, "Specify theme name", NULL },
{ "cmd", 'r', 0, G_OPTION_ARG_STRING_ARRAY, &commands, "First commands to run", NULL },
{ NULL }
};
@@ -171,14 +173,7 @@ main(int argc, char** argv)
}
/* Default logging WARN */
prof_run(log ? log : "WARN", account_name, config_file, log_file, theme_name);
/* Free resources allocated by GOptionContext */
g_free(log);
g_free(account_name);
g_free(config_file);
g_free(log_file);
g_free(theme_name);
prof_run(log ? log : "WARN", account_name, config_file, log_file, theme_name, commands);
return 0;
}

View File

@@ -34,6 +34,7 @@
*
*/
#include "config.h"
#include <unistd.h>
#ifdef HAVE_GTK
#include "ui/tray.h"
@@ -83,6 +84,8 @@
#include "omemo/omemo.h"
#endif
static unsigned int min_runtime = 5;
static void _init(char* log_level, char* config_file, char* log_file, char* theme_name);
static void _shutdown(void);
static void _connect_default(const char* const account);
@@ -91,7 +94,7 @@ pthread_mutex_t lock;
static gboolean force_quit = FALSE;
void
prof_run(char* log_level, char* account_name, char* config_file, char* log_file, char* theme_name)
prof_run(gchar* log_level, gchar* account_name, gchar* config_file, gchar* log_file, gchar* theme_name, gchar** commands)
{
gboolean cont = TRUE;
@@ -105,17 +108,50 @@ prof_run(char* log_level, char* account_name, char* config_file, char* log_file,
session_init_activity();
GTimer* runtime = g_timer_new();
char* line = NULL;
GTimer* waittimer = g_timer_new();
g_timer_stop(waittimer);
int waittime;
while (cont && !force_quit) {
log_stderr_handler();
session_check_autoaway();
line = inp_readline();
if (line) {
line = commands ? *commands : inp_readline();
if (commands && line && memcmp(line, "/sleep", 6) == 0) {
if (!g_timer_is_active(waittimer)) {
gchar* err_msg;
if (strtoi_range(line + 7, &waittime, 0, 300, &err_msg)) {
g_timer_start(waittimer);
/* Increase the minimal runtime by the waiting time
* so we can be sure there's runtime left after executing
* the last command.
*/
min_runtime += waittime;
} else {
log_error(err_msg);
g_free(err_msg);
commands = NULL;
}
} else if (g_timer_elapsed(waittimer, NULL) >= waittime) {
g_timer_stop(waittimer);
commands++;
if (!(*commands))
commands = NULL;
}
cont = TRUE;
} else if (line) {
ProfWin* window = wins_get_current();
cont = cmd_process_input(window, line);
free(line);
line = NULL;
if (commands) {
commands++;
if (!(*commands))
commands = NULL;
} else {
free(line);
line = NULL;
}
} else {
cont = TRUE;
}
@@ -132,6 +168,9 @@ prof_run(char* log_level, char* account_name, char* config_file, char* log_file,
tray_update();
#endif
}
g_timer_destroy(waittimer);
g_timer_elapsed(runtime, NULL) < min_runtime ? sleep(min_runtime) : (void)NULL;
g_timer_destroy(runtime);
}
gboolean

View File

@@ -40,7 +40,7 @@
#include <pthread.h>
#include <glib.h>
void prof_run(char* log_level, char* account_name, char* config_file, char* log_file, char* theme_name);
void prof_run(gchar* log_level, gchar* account_name, gchar* config_file, gchar* log_file, gchar* theme_name, gchar** commands);
gboolean prof_set_quit(void);
extern pthread_mutex_t lock;