rofi 1.7.5
script.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2022 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
29#define G_LOG_DOMAIN "Modes.Script"
30
31#include "modes/script.h"
32#include "helper.h"
33#include "rofi.h"
34#include <assert.h>
35#include <ctype.h>
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <strings.h>
41#include <unistd.h>
42
43#include "widgets/textbox.h"
44
45#include "mode-private.h"
46
47#include "rofi-icon-fetcher.h"
48
50
51typedef struct {
53 unsigned int id;
57 unsigned int cmd_list_length;
58
61 unsigned int num_urgent_list;
64 unsigned int num_active_list;
66 char *message;
67 char *prompt;
68 char *data;
69 gboolean do_markup;
72 char delim;
74 gboolean no_custom;
75
76 gboolean use_hot_keys;
78
82void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw,
83 DmenuScriptEntry *entry, char *buffer,
84 G_GNUC_UNUSED size_t length) {
85 gchar **extras = g_strsplit(buffer, "\x1f", -1);
86 gchar **extra;
87 for (extra = extras; *extra != NULL && *(extra + 1) != NULL; extra += 2) {
88 gchar *key = *extra;
89 gchar *value = *(extra + 1);
90 if (strcasecmp(key, "icon") == 0) {
91 entry->icon_name = value;
92 } else if (strcasecmp(key, "meta") == 0) {
93 entry->meta = value;
94 } else if (strcasecmp(key, "info") == 0) {
95 entry->info = value;
96 } else if (strcasecmp(key, "nonselectable") == 0) {
97 entry->nonselectable = strcasecmp(value, "true") == 0;
98 g_free(value);
99 } else {
100 g_free(value);
101 }
102 g_free(key);
103 }
104 g_free(extras);
105}
106
111static void parse_header_entry(Mode *sw, char *line, ssize_t length) {
113 ssize_t length_key = 0; // strlen ( line );
114 while (length_key < length && line[length_key] != '\x1f') {
115 length_key++;
116 }
117
118 if ((length_key + 1) < length) {
119 line[length_key] = '\0';
120 char *value = line + length_key + 1;
121 if (strcasecmp(line, "message") == 0) {
122 g_free(pd->message);
123 pd->message = strlen(value) ? g_strdup(value) : NULL;
124 } else if (strcasecmp(line, "prompt") == 0) {
125 g_free(pd->prompt);
126 pd->prompt = g_strdup(value);
127 sw->display_name = pd->prompt;
128 } else if (strcasecmp(line, "markup-rows") == 0) {
129 pd->do_markup = (strcasecmp(value, "true") == 0);
130 } else if (strcasecmp(line, "urgent") == 0) {
131 parse_ranges(value, &(pd->urgent_list), &(pd->num_urgent_list));
132 } else if (strcasecmp(line, "active") == 0) {
133 parse_ranges(value, &(pd->active_list), &(pd->num_active_list));
134 } else if (strcasecmp(line, "delim") == 0) {
135 pd->delim = helper_parse_char(value);
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) {
139 pd->use_hot_keys = (strcasecmp(value, "true") == 0);
140 } else if (strcasecmp(line, "keep-selection") == 0) {
141 pd->keep_selection = (strcasecmp(value, "true") == 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) {
145 g_free(pd->data);
146 pd->data = g_strdup(value);
147 } else if (strcasecmp(line, "theme") == 0) {
148 if (rofi_theme_parse_string((const char *)value)) {
149 g_warning("Failed to parse: '%s'", value);
151 }
152 }
153 }
154}
155
157 unsigned int *length, int value,
158 DmenuScriptEntry *entry) {
160 int fd = -1;
161 GError *error = NULL;
162 DmenuScriptEntry *retv = NULL;
163 char **argv = NULL;
164 int argc = 0;
165 *length = 0;
166 // Reset these between runs.
167 pd->new_selection = -1;
168 pd->keep_selection = -1;
169 // Environment
170 char **env = g_get_environ();
171
172 char *str_value = g_strdup_printf("%d", value);
173 env = g_environ_setenv(env, "ROFI_RETV", str_value, TRUE);
174 g_free(str_value);
175
176 str_value = g_strdup_printf("%d", (int)getpid());
177 env = g_environ_setenv(env, "ROFI_OUTSIDE", str_value, TRUE);
178 g_free(str_value);
179
180 if (entry && entry->info) {
181 env = g_environ_setenv(env, "ROFI_INFO", entry->info, TRUE);
182 }
183 if (pd->data) {
184 env = g_environ_setenv(env, "ROFI_DATA", pd->data, TRUE);
185 }
186
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);
193 }
194 g_strfreev(env);
195 if (error != NULL) {
196 char *msg = g_strdup_printf("Failed to execute: '%s'\nError: '%s'",
197 (char *)sw->ed, error->message);
198 rofi_view_error_dialog(msg, FALSE);
199 g_free(msg);
200 // print error.
201 g_error_free(error);
202 fd = -1;
203 }
204 if (fd >= 0) {
205 FILE *inp = fdopen(fd, "r");
206 if (inp) {
207 char *buffer = NULL;
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)) >
212 0) {
213 // Filter out line-end.
214 if (buffer[read_length - 1] == pd->delim) {
215 buffer[read_length - 1] = '\0';
216 }
217 if (buffer[0] == '\0') {
218 parse_header_entry(sw, &buffer[1], read_length - 1);
219 } else {
220 if (actual_size < ((*length) + 2)) {
221 actual_size += 256;
222 retv = g_realloc(retv, (actual_size) * sizeof(DmenuScriptEntry));
223 }
224 if (retv) {
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);
228#else
229 retv[(*length)].entry = g_memdup(buffer, buf_length);
230#endif
231 retv[(*length)].icon_name = NULL;
232 retv[(*length)].meta = NULL;
233 retv[(*length)].info = NULL;
234 retv[(*length)].icon_fetch_uid = 0;
235 retv[(*length)].icon_fetch_size = 0;
236 retv[(*length)].nonselectable = FALSE;
237 if (buf_length > 0 && (read_length > (ssize_t)buf_length)) {
238 dmenuscript_parse_entry_extras(sw, &(retv[(*length)]),
239 buffer + buf_length,
240 read_length - buf_length);
241 }
242 retv[(*length) + 1].entry = NULL;
243 (*length)++;
244 }
245 }
246 }
247 if (buffer) {
248 free(buffer);
249 }
250 if (fclose(inp) != 0) {
251 g_warning("Failed to close stdout off executor script: '%s'",
252 g_strerror(errno));
253 }
254 }
255 }
256 g_strfreev(argv);
257 return retv;
258}
259
260static void script_switcher_free(Mode *sw) {
261 if (sw == NULL) {
262 return;
263 }
264 g_free(sw->name);
265 g_free(sw->ed);
266 g_free(sw);
267}
268
269static int script_mode_init(Mode *sw) {
270 if (sw->private_data == NULL) {
271 ScriptModePrivateData *pd = g_malloc0(sizeof(*pd));
272 pd->delim = '\n';
273 sw->private_data = (void *)pd;
274 pd->cmd_list = execute_executor(sw, NULL, &(pd->cmd_list_length), 0, NULL);
275 }
276 return TRUE;
277}
278static unsigned int script_mode_get_num_entries(const Mode *sw) {
279 const ScriptModePrivateData *rmpd =
281 return rmpd->cmd_list_length;
282}
283
286
287 rmpd->num_urgent_list = 0;
288 g_free(rmpd->urgent_list);
289 rmpd->urgent_list = NULL;
290 rmpd->num_active_list = 0;
291 g_free(rmpd->active_list);
292 rmpd->active_list = NULL;
293}
294
295static ModeMode script_mode_result(Mode *sw, int mretv, char **input,
296 unsigned int selected_line) {
298 ModeMode retv = MODE_EXIT;
299 DmenuScriptEntry *new_list = NULL;
300 unsigned int new_length = 0;
301
302 if ((mretv & MENU_CUSTOM_COMMAND)) {
303 if (rmpd->use_hot_keys) {
305 if (selected_line != UINT32_MAX) {
306 new_list = execute_executor(sw, rmpd->cmd_list[selected_line].entry,
307 &new_length, 10 + (mretv & MENU_LOWER_MASK),
308 &(rmpd->cmd_list[selected_line]));
309 } else {
310 if (rmpd->no_custom == FALSE) {
311 new_list = execute_executor(sw, *input, &new_length,
312 10 + (mretv & MENU_LOWER_MASK), NULL);
313 } else {
314 return RELOAD_DIALOG;
315 }
316 }
317 } else {
318 retv = (mretv & MENU_LOWER_MASK);
319 return retv;
320 }
321 } else if ((mretv & MENU_OK) && rmpd->cmd_list[selected_line].entry != NULL) {
322 if (rmpd->cmd_list[selected_line].nonselectable) {
323 return RELOAD_DIALOG;
324 }
326 new_list =
327 execute_executor(sw, rmpd->cmd_list[selected_line].entry, &new_length,
328 1, &(rmpd->cmd_list[selected_line]));
329 } else if ((mretv & MENU_CUSTOM_INPUT) && *input != NULL &&
330 *input[0] != '\0') {
331 if (rmpd->no_custom == FALSE) {
333 new_list = execute_executor(sw, *input, &new_length, 2, NULL);
334 } else {
335 return RELOAD_DIALOG;
336 }
337 }
338
339 // If a new list was generated, use that an loop around.
340 if (new_list != NULL) {
341 for (unsigned int i = 0; i < rmpd->cmd_list_length; i++) {
342 g_free(rmpd->cmd_list[i].entry);
343 g_free(rmpd->cmd_list[i].icon_name);
344 g_free(rmpd->cmd_list[i].meta);
345 }
346 g_free(rmpd->cmd_list);
347
348 rmpd->cmd_list = new_list;
349 rmpd->cmd_list_length = new_length;
350 if (rmpd->keep_selection) {
351 if (rmpd->new_selection >= 0 &&
352 rmpd->new_selection < rmpd->cmd_list_length) {
354 rmpd->new_selection);
355 } else {
357 }
358 g_free(*input);
359 *input = NULL;
360 retv = RELOAD_DIALOG;
361 } else {
362 retv = RESET_DIALOG;
363 }
364 }
365 return retv;
366}
367
368static void script_mode_destroy(Mode *sw) {
370 if (rmpd != NULL) {
371 for (unsigned int i = 0; i < rmpd->cmd_list_length; i++) {
372 g_free(rmpd->cmd_list[i].entry);
373 g_free(rmpd->cmd_list[i].icon_name);
374 g_free(rmpd->cmd_list[i].meta);
375 }
376 g_free(rmpd->cmd_list);
377 g_free(rmpd->message);
378 g_free(rmpd->prompt);
379 g_free(rmpd->data);
380 g_free(rmpd->urgent_list);
381 g_free(rmpd->active_list);
382 g_free(rmpd);
383 sw->private_data = NULL;
384 }
385}
386static inline unsigned int get_index(unsigned int length, int index) {
387 if (index >= 0) {
388 return index;
389 }
390 if (((unsigned int)-index) <= length) {
391 return length + index;
392 }
393 // Out of range.
394 return UINT_MAX;
395}
396static char *_get_display_value(const Mode *sw, unsigned int selected_line,
397 G_GNUC_UNUSED int *state,
398 G_GNUC_UNUSED GList **list, int get_entry) {
400 for (unsigned int i = 0; i < pd->num_active_list; i++) {
401 unsigned int start =
403 unsigned int stop = get_index(pd->cmd_list_length, pd->active_list[i].stop);
404 if (selected_line >= start && selected_line <= stop) {
405 *state |= ACTIVE;
406 }
407 }
408 for (unsigned int i = 0; i < pd->num_urgent_list; i++) {
409 unsigned int start =
411 unsigned int stop = get_index(pd->cmd_list_length, pd->urgent_list[i].stop);
412 if (selected_line >= start && selected_line <= stop) {
413 *state |= URGENT;
414 }
415 }
416 if (pd->do_markup) {
417 *state |= MARKUP;
418 }
419 return get_entry ? g_strdup(pd->cmd_list[selected_line].entry) : NULL;
420}
421
422static int script_token_match(const Mode *sw, rofi_int_matcher **tokens,
423 unsigned int index) {
425 int match = 1;
426 if (tokens) {
427 for (int j = 0; match && tokens[j] != NULL; j++) {
428 rofi_int_matcher *ftokens[2] = {tokens[j], NULL};
429 int test = 0;
430 test = helper_token_match(ftokens, rmpd->cmd_list[index].entry);
431 if (test == tokens[j]->invert && rmpd->cmd_list[index].meta) {
432 test = helper_token_match(ftokens, rmpd->cmd_list[index].meta);
433 }
434
435 if (test == 0) {
436 match = 0;
437 }
438 }
439 }
440 return match;
441}
442static char *script_get_message(const Mode *sw) {
444 return g_strdup(pd->message);
445}
446static cairo_surface_t *script_get_icon(const Mode *sw,
447 unsigned int selected_line,
448 unsigned int height) {
451 g_return_val_if_fail(pd->cmd_list != NULL, NULL);
452 DmenuScriptEntry *dr = &(pd->cmd_list[selected_line]);
453 if (dr->icon_name == NULL) {
454 return NULL;
455 }
456 if (dr->icon_fetch_uid > 0 && dr->icon_fetch_size == height) {
458 }
460 dr->icon_fetch_size = height;
462}
463
464#include "mode-private.h"
465
466typedef struct ScriptUser {
467 char *name;
468 char *path;
470
472size_t num_scripts = 0;
473
475 for (size_t i = 0; i < num_scripts; i++) {
476 g_free(user_scripts[i].name);
477 g_free(user_scripts[i].path);
478 }
479 g_free(user_scripts);
480}
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) ==
485 FALSE) {
486 g_free(script_dir);
487 return;
488 }
489 GDir *sd = g_dir_open(script_dir, 0, NULL);
490 if (sd) {
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);
495 g_realloc(user_scripts, sizeof(ScriptUser) * (num_scripts + 1));
497 user_scripts[num_scripts].name = g_strdup(file);
498 char *dot = strrchr(user_scripts[num_scripts].name, '.');
499 if (dot) {
500 *dot = '\0';
501 }
502 num_scripts++;
503 }
504 }
505
506 g_free(script_dir);
507}
508
509static int script_mode_has_user_script(char const *const user) {
510
511 for (size_t i = 0; i < num_scripts; i++) {
512 if (g_strcmp0(user_scripts[i].name, user) == 0) {
513 return i;
514 }
515 }
516 return -1;
517}
518
519Mode *script_mode_parse_setup(const char *str) {
520 int ui = 0;
521 if ((ui = script_mode_has_user_script(str)) >= 0) {
522 Mode *sw = g_malloc0(sizeof(*sw));
523 sw->name = g_strdup(user_scripts[ui].name);
524 sw->ed = g_strdup(user_scripts[ui].path);
533 sw->_get_completion = NULL, sw->_preprocess_input = NULL,
535 return sw;
536 }
537 Mode *sw = g_malloc0(sizeof(*sw));
538 char *endp = NULL;
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)) {
544 if (index == 0) {
545 sw->name = g_strdup(token);
546 } else if (index == 1) {
547 sw->ed = (void *)rofi_expand_path(token);
548 }
549 index++;
550 }
551 g_free(parse);
552 if (index == 2) {
561 sw->_get_completion = NULL, sw->_preprocess_input = NULL,
563
564 return sw;
565 }
566 fprintf(
567 stderr,
568 "The script command '%s' has %u options, but needs 2: <name>:<script>.",
569 str, index);
571 return NULL;
572}
573
574gboolean script_mode_is_valid(const char *token) {
575 if (script_mode_has_user_script(token) >= 0) {
576 return TRUE;
577 }
578 return strchr(token, ':') != NULL;
579}
580
581void script_user_list(gboolean is_term) {
582
583 for (unsigned int i = 0; i < num_scripts; i++) {
584 printf(" • %s%s%s (%s)\n", is_term ? (color_bold) : "",
585 user_scripts[i].name, is_term ? color_reset : "",
586 user_scripts[i].path);
587 }
588}
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
Definition helper.c:1182
char helper_parse_char(const char *arg)
Definition helper.c:360
char * rofi_expand_path(const char *input)
Definition helper.c:740
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition helper.c:518
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)
Definition mode.c:159
ModeMode
Definition mode.h:49
@ MENU_CUSTOM_COMMAND
Definition mode.h:79
@ MENU_LOWER_MASK
Definition mode.h:87
@ MENU_OK
Definition mode.h:67
@ MENU_CUSTOM_INPUT
Definition mode.h:73
@ MODE_EXIT
Definition mode.h:51
@ RELOAD_DIALOG
Definition mode.h:55
@ RESET_DIALOG
Definition mode.h:59
#define color_reset
Definition rofi.h:107
#define color_bold
Definition rofi.h:109
void rofi_clear_error_messages(void)
Definition rofi.c:93
void script_mode_gather_user_scripts(void)
Definition script.c:481
gboolean script_mode_is_valid(const char *token)
Definition script.c:574
void script_user_list(gboolean is_term)
Definition script.c:581
void script_mode_cleanup(void)
Definition script.c:474
Mode * script_mode_parse_setup(const char *str)
Definition script.c:519
@ URGENT
Definition textbox.h:104
@ ACTIVE
Definition textbox.h:106
@ MARKUP
Definition textbox.h:110
int rofi_view_error_dialog(const char *msg, int markup)
Definition view.c:2191
RofiViewState * rofi_view_get_active(void)
Definition view.c:549
void rofi_view_set_selected_line(RofiViewState *state, unsigned int selected_line)
Definition view.c:581
static void script_mode_destroy(Mode *sw)
Definition script.c:368
static void script_switcher_free(Mode *sw)
Definition script.c:260
static void parse_header_entry(Mode *sw, char *line, ssize_t length)
Definition script.c:111
static ModeMode script_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
Definition script.c:295
static cairo_surface_t * script_get_icon(const Mode *sw, unsigned int selected_line, unsigned int height)
Definition script.c:446
void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, G_GNUC_UNUSED size_t length)
Definition script.c:82
static unsigned int get_index(unsigned int length, int index)
Definition script.c:386
static char * script_get_message(const Mode *sw)
Definition script.c:442
ScriptUser * user_scripts
Definition script.c:471
static int script_mode_has_user_script(char const *const user)
Definition script.c:509
size_t num_scripts
Definition script.c:472
static void script_mode_reset_highlight(Mode *sw)
Definition script.c:284
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)
Definition script.c:396
static int script_mode_init(Mode *sw)
Definition script.c:269
static DmenuScriptEntry * execute_executor(Mode *sw, char *arg, unsigned int *length, int value, DmenuScriptEntry *entry)
Definition script.c:156
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition script.c:422
struct ScriptUser ScriptUser
static unsigned int script_mode_get_num_entries(const Mode *sw)
Definition script.c:278
struct rofi_range_pair * urgent_list
Definition script.c:60
gboolean no_custom
Definition script.c:74
struct rofi_range_pair * active_list
Definition script.c:63
gboolean use_hot_keys
Definition script.c:76
gboolean do_markup
Definition script.c:69
unsigned int num_urgent_list
Definition script.c:61
int64_t new_selection
Definition script.c:71
unsigned int id
Definition script.c:53
unsigned int cmd_list_length
Definition script.c:57
unsigned int num_active_list
Definition script.c:64
DmenuScriptEntry * cmd_list
Definition script.c:55
gboolean keep_selection
Definition script.c:70
char * name
Definition script.c:467
char * path
Definition script.c:468
_mode_result _result
__mode_get_num_entries _get_num_entries
__mode_destroy _destroy
_mode_preprocess_input _preprocess_input
char * display_name
_mode_free free
_mode_token_match _token_match
_mode_get_display_value _get_display_value
_mode_get_icon _get_icon
_mode_get_completion _get_completion
__mode_init _init
char * name
_mode_get_message _get_message
void * private_data
gboolean rofi_theme_parse_string(const char *string)