29#define G_LOG_DOMAIN "Modes.Script"
84 G_GNUC_UNUSED
size_t length) {
85 gchar **extras = g_strsplit(buffer,
"\x1f", -1);
87 for (extra = extras; *extra != NULL && *(extra + 1) != NULL; extra += 2) {
89 gchar *value = *(extra + 1);
90 if (strcasecmp(key,
"icon") == 0) {
92 }
else if (strcasecmp(key,
"meta") == 0) {
94 }
else if (strcasecmp(key,
"info") == 0) {
96 }
else if (strcasecmp(key,
"nonselectable") == 0) {
113 ssize_t length_key = 0;
114 while (length_key < length && line[length_key] !=
'\x1f') {
118 if ((length_key + 1) < length) {
119 line[length_key] =
'\0';
120 char *value = line + length_key + 1;
121 if (strcasecmp(line,
"message") == 0) {
123 pd->
message = strlen(value) ? g_strdup(value) : NULL;
124 }
else if (strcasecmp(line,
"prompt") == 0) {
126 pd->
prompt = g_strdup(value);
128 }
else if (strcasecmp(line,
"markup-rows") == 0) {
129 pd->
do_markup = (strcasecmp(value,
"true") == 0);
130 }
else if (strcasecmp(line,
"urgent") == 0) {
132 }
else if (strcasecmp(line,
"active") == 0) {
134 }
else if (strcasecmp(line,
"delim") == 0) {
136 }
else if (strcasecmp(line,
"no-custom") == 0) {
137 pd->
no_custom = (strcasecmp(value,
"true") == 0);
138 }
else if (strcasecmp(line,
"use-hot-keys") == 0) {
140 }
else if (strcasecmp(line,
"keep-selection") == 0) {
142 }
else if (strcasecmp(line,
"new-selection") == 0) {
143 pd->
new_selection = (int64_t)g_ascii_strtoll(value, NULL, 0);
144 }
else if (strcasecmp(line,
"data") == 0) {
146 pd->
data = g_strdup(value);
147 }
else if (strcasecmp(line,
"theme") == 0) {
149 g_warning(
"Failed to parse: '%s'", value);
157 unsigned int *length,
int value,
161 GError *error = NULL;
170 char **env = g_get_environ();
172 char *str_value = g_strdup_printf(
"%d", value);
173 env = g_environ_setenv(env,
"ROFI_RETV", str_value, TRUE);
176 str_value = g_strdup_printf(
"%d", (
int)getpid());
177 env = g_environ_setenv(env,
"ROFI_OUTSIDE", str_value, TRUE);
180 if (entry && entry->
info) {
181 env = g_environ_setenv(env,
"ROFI_INFO", entry->
info, TRUE);
184 env = g_environ_setenv(env,
"ROFI_DATA", pd->
data, TRUE);
187 if (g_shell_parse_argv(sw->
ed, &argc, &argv, &error)) {
188 argv = g_realloc(argv, (argc + 2) *
sizeof(
char *));
189 argv[argc] = g_strdup(arg);
190 argv[argc + 1] = NULL;
191 g_spawn_async_with_pipes(NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL, NULL,
192 NULL, NULL, &fd, NULL, &error);
196 char *msg = g_strdup_printf(
"Failed to execute: '%s'\nError: '%s'",
197 (
char *)sw->
ed, error->message);
205 FILE *inp = fdopen(fd,
"r");
208 size_t buffer_length = 0;
209 ssize_t read_length = 0;
210 size_t actual_size = 0;
211 while ((read_length = getdelim(&buffer, &buffer_length, pd->
delim, inp)) >
214 if (buffer[read_length - 1] == pd->
delim) {
215 buffer[read_length - 1] =
'\0';
217 if (buffer[0] ==
'\0') {
220 if (actual_size < ((*length) + 2)) {
225 size_t buf_length = strlen(buffer) + 1;
226#if GLIB_CHECK_VERSION(2, 68, 0)
227 retv[(*length)].
entry = g_memdup2(buffer, buf_length);
229 retv[(*length)].
entry = g_memdup(buffer, buf_length);
232 retv[(*length)].
meta = NULL;
233 retv[(*length)].
info = NULL;
237 if (buf_length > 0 && (read_length > (ssize_t)buf_length)) {
240 read_length - buf_length);
242 retv[(*length) + 1].
entry = NULL;
250 if (fclose(inp) != 0) {
251 g_warning(
"Failed to close stdout off executor script: '%s'",
296 unsigned int selected_line) {
300 unsigned int new_length = 0;
305 if (selected_line != UINT32_MAX) {
328 1, &(rmpd->
cmd_list[selected_line]));
340 if (new_list != NULL) {
386static inline unsigned int get_index(
unsigned int length,
int index) {
390 if (((
unsigned int)-index) <= length) {
391 return length + index;
397 G_GNUC_UNUSED
int *state,
398 G_GNUC_UNUSED GList **list,
int get_entry) {
404 if (selected_line >=
start && selected_line <=
stop) {
412 if (selected_line >=
start && selected_line <=
stop) {
419 return get_entry ? g_strdup(pd->
cmd_list[selected_line].
entry) : NULL;
423 unsigned int index) {
427 for (
int j = 0; match && tokens[j] != NULL; j++) {
431 if (test == tokens[j]->invert && rmpd->
cmd_list[index].
meta) {
447 unsigned int selected_line,
448 unsigned int height) {
451 g_return_val_if_fail(pd->
cmd_list != NULL, NULL);
482 const char *cpath = g_get_user_config_dir();
483 char *script_dir = g_build_filename(cpath,
"rofi",
"scripts", NULL);
484 if (g_file_test(script_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ==
489 GDir *sd = g_dir_open(script_dir, 0, NULL);
491 const char *file = NULL;
492 while ((file = g_dir_read_name(sd)) != NULL) {
493 char *sp = g_build_filename(cpath,
"rofi",
"scripts", file, NULL);
522 Mode *sw = g_malloc0(
sizeof(*sw));
537 Mode *sw = g_malloc0(
sizeof(*sw));
539 char *parse = g_strdup(str);
540 unsigned int index = 0;
541 const char *
const sep =
":";
542 for (
char *token = strtok_r(parse, sep, &endp); token != NULL;
543 token = strtok_r(NULL, sep, &endp)) {
545 sw->
name = g_strdup(token);
546 }
else if (index == 1) {
568 "The script command '%s' has %u options, but needs 2: <name>:<script>.",
578 return strchr(token,
':') != NULL;
584 printf(
" • %s%s%s (%s)\n", is_term ? (
color_bold) :
"",
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
char helper_parse_char(const char *arg)
char * rofi_expand_path(const char *input)
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
void * mode_get_private_data(const Mode *mode)
void rofi_clear_error_messages(void)
void script_mode_gather_user_scripts(void)
gboolean script_mode_is_valid(const char *token)
void script_user_list(gboolean is_term)
void script_mode_cleanup(void)
Mode * script_mode_parse_setup(const char *str)
int rofi_view_error_dialog(const char *msg, int markup)
RofiViewState * rofi_view_get_active(void)
void rofi_view_set_selected_line(RofiViewState *state, unsigned int selected_line)
static void script_mode_destroy(Mode *sw)
static void script_switcher_free(Mode *sw)
static void parse_header_entry(Mode *sw, char *line, ssize_t length)
static ModeMode script_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
static cairo_surface_t * script_get_icon(const Mode *sw, unsigned int selected_line, unsigned int height)
void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, G_GNUC_UNUSED size_t length)
static unsigned int get_index(unsigned int length, int index)
static char * script_get_message(const Mode *sw)
ScriptUser * user_scripts
static int script_mode_has_user_script(char const *const user)
static void script_mode_reset_highlight(Mode *sw)
static char * _get_display_value(const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **list, int get_entry)
static int script_mode_init(Mode *sw)
static DmenuScriptEntry * execute_executor(Mode *sw, char *arg, unsigned int *length, int value, DmenuScriptEntry *entry)
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
struct ScriptUser ScriptUser
static unsigned int script_mode_get_num_entries(const Mode *sw)
struct rofi_range_pair * urgent_list
struct rofi_range_pair * active_list
unsigned int num_urgent_list
unsigned int cmd_list_length
unsigned int num_active_list
DmenuScriptEntry * cmd_list
__mode_get_num_entries _get_num_entries
_mode_preprocess_input _preprocess_input
_mode_token_match _token_match
_mode_get_display_value _get_display_value
_mode_get_completion _get_completion
_mode_get_message _get_message
gboolean rofi_theme_parse_string(const char *string)