29 #define G_LOG_DOMAIN "Dialogs.Script"
82 G_GNUC_UNUSED
size_t length) {
83 gchar **extras = g_strsplit(buffer,
"\x1f", -1);
85 for (extra = extras; *extra != NULL && *(extra + 1) != NULL; extra += 2) {
87 gchar *value = *(extra + 1);
88 if (strcasecmp(key,
"icon") == 0) {
90 }
else if (strcasecmp(key,
"meta") == 0) {
92 }
else if (strcasecmp(key,
"info") == 0) {
94 }
else if (strcasecmp(key,
"nonselectable") == 0) {
111 ssize_t length_key = 0;
112 while (length_key < length && line[length_key] !=
'\x1f') {
116 if ((length_key + 1) < length) {
117 line[length_key] =
'\0';
118 char *value = line + length_key + 1;
119 if (strcasecmp(line,
"message") == 0) {
121 pd->
message = strlen(value) ? g_strdup(value) : NULL;
122 }
else if (strcasecmp(line,
"prompt") == 0) {
124 pd->
prompt = g_strdup(value);
126 }
else if (strcasecmp(line,
"markup-rows") == 0) {
127 pd->
do_markup = (strcasecmp(value,
"true") == 0);
128 }
else if (strcasecmp(line,
"urgent") == 0) {
130 }
else if (strcasecmp(line,
"active") == 0) {
132 }
else if (strcasecmp(line,
"delim") == 0) {
134 }
else if (strcasecmp(line,
"no-custom") == 0) {
135 pd->
no_custom = (strcasecmp(value,
"true") == 0);
136 }
else if (strcasecmp(line,
"use-hot-keys") == 0) {
143 unsigned int *length,
int value,
147 GError *error = NULL;
154 char **env = g_get_environ();
156 char *str_value = g_strdup_printf(
"%d", value);
157 env = g_environ_setenv(env,
"ROFI_RETV", str_value, TRUE);
160 str_value = g_strdup_printf(
"%d", (
int)getpid());
161 env = g_environ_setenv(env,
"ROFI_OUTSIDE", str_value, TRUE);
164 if (entry && entry->
info) {
165 env = g_environ_setenv(env,
"ROFI_INFO", entry->
info, TRUE);
168 if (g_shell_parse_argv(sw->
ed, &argc, &argv, &error)) {
169 argv = g_realloc(argv, (argc + 2) *
sizeof(
char *));
170 argv[argc] = g_strdup(arg);
171 argv[argc + 1] = NULL;
172 g_spawn_async_with_pipes(NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL, NULL,
173 NULL, NULL, &fd, NULL, &error);
177 char *msg = g_strdup_printf(
"Failed to execute: '%s'\nError: '%s'",
178 (
char *)sw->
ed, error->message);
186 FILE *inp = fdopen(fd,
"r");
189 size_t buffer_length = 0;
190 ssize_t read_length = 0;
191 size_t actual_size = 0;
192 while ((read_length = getdelim(&buffer, &buffer_length, pd->
delim, inp)) >
195 if (buffer[read_length - 1] == pd->
delim) {
196 buffer[read_length - 1] =
'\0';
198 if (buffer[0] ==
'\0') {
201 if (actual_size < ((*length) + 2)) {
206 size_t buf_length = strlen(buffer) + 1;
207 retv[(*length)].entry = g_memdup(buffer, buf_length);
208 retv[(*length)].icon_name = NULL;
209 retv[(*length)].meta = NULL;
210 retv[(*length)].info = NULL;
211 retv[(*length)].icon_fetch_uid = 0;
212 retv[(*length)].nonselectable = FALSE;
213 if (buf_length > 0 && (read_length > (ssize_t)buf_length)) {
216 read_length - buf_length);
218 retv[(*length) + 1].entry = NULL;
226 if (fclose(inp) != 0) {
227 g_warning(
"Failed to close stdout off executor script: '%s'",
272 unsigned int selected_line) {
276 unsigned int new_length = 0;
281 if (selected_line != UINT32_MAX) {
304 1, &(rmpd->
cmd_list[selected_line]));
316 if (new_list != NULL) {
348 static inline unsigned int get_index(
unsigned int length,
int index) {
352 if (((
unsigned int)-index) <= length) {
353 return length + index;
359 G_GNUC_UNUSED
int *state,
360 G_GNUC_UNUSED GList **list,
int get_entry) {
366 if (selected_line >=
start && selected_line <=
stop) {
374 if (selected_line >=
start && selected_line <=
stop) {
381 return get_entry ? g_strdup(pd->
cmd_list[selected_line].
entry) : NULL;
385 unsigned int index) {
389 for (
int j = 0; match && tokens != NULL && tokens[j] != NULL; j++) {
393 if (test == tokens[j]->invert && rmpd->
cmd_list[index].
meta) {
408 static cairo_surface_t *
412 g_return_val_if_fail(pd->
cmd_list != NULL, NULL);
426 Mode *sw = g_malloc0(
sizeof(*sw));
428 char *parse = g_strdup(str);
429 unsigned int index = 0;
430 const char *
const sep =
":";
431 for (
char *token = strtok_r(parse, sep, &endp); token != NULL;
432 token = strtok_r(NULL, sep, &endp)) {
434 sw->
name = g_strdup(token);
435 }
else if (index == 1) {
457 "The script command '%s' has %u options, but needs 2: <name>:<script>.",
464 return strchr(token,
':') != NULL;
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)
Mode * script_mode_parse_setup(const char *str)
gboolean script_mode_is_valid(const char *token)
int rofi_view_error_dialog(const char *msg, int markup)
static void script_mode_destroy(Mode *sw)
static cairo_surface_t * script_get_icon(const Mode *sw, unsigned int selected_line, int height)
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)
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 * _get_display_value(const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **list, int get_entry)
static char * script_get_message(const Mode *sw)
static DmenuScriptEntry * execute_executor(Mode *sw, char *arg, unsigned int *length, int value, DmenuScriptEntry *entry)
static void script_mode_reset_highlight(Mode *sw)
static int script_mode_init(Mode *sw)
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
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