rofi  1.7.0
script.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2021 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 "Dialogs.Script"
30 
31 #include "dialogs/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 
51 typedef 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  gboolean do_markup;
69  char delim;
71  gboolean no_custom;
72 
73  gboolean use_hot_keys;
75 
79 void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw,
80  DmenuScriptEntry *entry, char *buffer,
81  G_GNUC_UNUSED size_t length) {
82  gchar **extras = g_strsplit(buffer, "\x1f", -1);
83  gchar **extra;
84  for (extra = extras; *extra != NULL && *(extra + 1) != NULL; extra += 2) {
85  gchar *key = *extra;
86  gchar *value = *(extra + 1);
87  if (strcasecmp(key, "icon") == 0) {
88  entry->icon_name = value;
89  } else if (strcasecmp(key, "meta") == 0) {
90  entry->meta = value;
91  } else if (strcasecmp(key, "info") == 0) {
92  entry->info = value;
93  } else if (strcasecmp(key, "nonselectable") == 0) {
94  entry->nonselectable = strcasecmp(value, "true") == 0;
95  g_free(value);
96  } else {
97  g_free(value);
98  }
99  g_free(key);
100  }
101  g_free(extras);
102 }
103 
108 static void parse_header_entry(Mode *sw, char *line, ssize_t length) {
110  ssize_t length_key = 0; // strlen ( line );
111  while (length_key < length && line[length_key] != '\x1f') {
112  length_key++;
113  }
114 
115  if ((length_key + 1) < length) {
116  line[length_key] = '\0';
117  char *value = line + length_key + 1;
118  if (strcasecmp(line, "message") == 0) {
119  g_free(pd->message);
120  pd->message = strlen(value) ? g_strdup(value) : NULL;
121  } else if (strcasecmp(line, "prompt") == 0) {
122  g_free(pd->prompt);
123  pd->prompt = g_strdup(value);
124  sw->display_name = pd->prompt;
125  } else if (strcasecmp(line, "markup-rows") == 0) {
126  pd->do_markup = (strcasecmp(value, "true") == 0);
127  } else if (strcasecmp(line, "urgent") == 0) {
128  parse_ranges(value, &(pd->urgent_list), &(pd->num_urgent_list));
129  } else if (strcasecmp(line, "active") == 0) {
130  parse_ranges(value, &(pd->active_list), &(pd->num_active_list));
131  } else if (strcasecmp(line, "delim") == 0) {
132  pd->delim = helper_parse_char(value);
133  } else if (strcasecmp(line, "no-custom") == 0) {
134  pd->no_custom = (strcasecmp(value, "true") == 0);
135  } else if (strcasecmp(line, "use-hot-keys") == 0) {
136  pd->use_hot_keys = (strcasecmp(value, "true") == 0);
137  }
138  }
139 }
140 
141 static DmenuScriptEntry *execute_executor(Mode *sw, char *arg,
142  unsigned int *length, int value,
143  DmenuScriptEntry *entry) {
145  int fd = -1;
146  GError *error = NULL;
147  DmenuScriptEntry *retv = NULL;
148  char **argv = NULL;
149  int argc = 0;
150  *length = 0;
151 
152  // Environment
153  char **env = g_get_environ();
154 
155  char *str_value = g_strdup_printf("%d", value);
156  env = g_environ_setenv(env, "ROFI_RETV", str_value, TRUE);
157  g_free(str_value);
158 
159  str_value = g_strdup_printf("%d", (int)getpid());
160  env = g_environ_setenv(env, "ROFI_OUTSIDE", str_value, TRUE);
161  g_free(str_value);
162 
163  if (entry && entry->info) {
164  env = g_environ_setenv(env, "ROFI_INFO", entry->info, TRUE);
165  }
166 
167  if (g_shell_parse_argv(sw->ed, &argc, &argv, &error)) {
168  argv = g_realloc(argv, (argc + 2) * sizeof(char *));
169  argv[argc] = g_strdup(arg);
170  argv[argc + 1] = NULL;
171  g_spawn_async_with_pipes(NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL, NULL,
172  NULL, NULL, &fd, NULL, &error);
173  }
174  g_strfreev(env);
175  if (error != NULL) {
176  char *msg = g_strdup_printf("Failed to execute: '%s'\nError: '%s'",
177  (char *)sw->ed, error->message);
178  rofi_view_error_dialog(msg, FALSE);
179  g_free(msg);
180  // print error.
181  g_error_free(error);
182  fd = -1;
183  }
184  if (fd >= 0) {
185  FILE *inp = fdopen(fd, "r");
186  if (inp) {
187  char *buffer = NULL;
188  size_t buffer_length = 0;
189  ssize_t read_length = 0;
190  size_t actual_size = 0;
191  while ((read_length = getdelim(&buffer, &buffer_length, pd->delim, inp)) >
192  0) {
193  // Filter out line-end.
194  if (buffer[read_length - 1] == pd->delim) {
195  buffer[read_length - 1] = '\0';
196  }
197  if (buffer[0] == '\0') {
198  parse_header_entry(sw, &buffer[1], read_length - 1);
199  } else {
200  if (actual_size < ((*length) + 2)) {
201  actual_size += 256;
202  retv = g_realloc(retv, (actual_size) * sizeof(DmenuScriptEntry));
203  }
204  size_t buf_length = strlen(buffer) + 1;
205  retv[(*length)].entry = g_memdup(buffer, buf_length);
206  retv[(*length)].icon_name = NULL;
207  retv[(*length)].meta = NULL;
208  retv[(*length)].info = NULL;
209  retv[(*length)].icon_fetch_uid = 0;
210  retv[(*length)].nonselectable = FALSE;
211  if (buf_length > 0 && (read_length > (ssize_t)buf_length)) {
212  dmenuscript_parse_entry_extras(sw, &(retv[(*length)]),
213  buffer + buf_length,
214  read_length - buf_length);
215  }
216  retv[(*length) + 1].entry = NULL;
217  (*length)++;
218  }
219  }
220  if (buffer) {
221  free(buffer);
222  }
223  if (fclose(inp) != 0) {
224  g_warning("Failed to close stdout off executor script: '%s'",
225  g_strerror(errno));
226  }
227  }
228  }
229  g_strfreev(argv);
230  return retv;
231 }
232 
233 static void script_switcher_free(Mode *sw) {
234  if (sw == NULL) {
235  return;
236  }
237  g_free(sw->name);
238  g_free(sw->ed);
239  g_free(sw);
240 }
241 
242 static int script_mode_init(Mode *sw) {
243  if (sw->private_data == NULL) {
244  ScriptModePrivateData *pd = g_malloc0(sizeof(*pd));
245  pd->delim = '\n';
246  sw->private_data = (void *)pd;
247  pd->cmd_list = execute_executor(sw, NULL, &(pd->cmd_list_length), 0, NULL);
248  }
249  return TRUE;
250 }
251 static unsigned int script_mode_get_num_entries(const Mode *sw) {
252  const ScriptModePrivateData *rmpd =
253  (const ScriptModePrivateData *)sw->private_data;
254  return rmpd->cmd_list_length;
255 }
256 
259 
260  rmpd->num_urgent_list = 0;
261  g_free(rmpd->urgent_list);
262  rmpd->urgent_list = NULL;
263  rmpd->num_active_list = 0;
264  g_free(rmpd->active_list);
265  rmpd->active_list = NULL;
266 }
267 
268 static ModeMode script_mode_result(Mode *sw, int mretv, char **input,
269  unsigned int selected_line) {
271  ModeMode retv = MODE_EXIT;
272  DmenuScriptEntry *new_list = NULL;
273  unsigned int new_length = 0;
274 
275  if ((mretv & MENU_CUSTOM_COMMAND)) {
276  if (rmpd->use_hot_keys) {
278  if (selected_line != UINT32_MAX) {
279  new_list = execute_executor(sw, rmpd->cmd_list[selected_line].entry,
280  &new_length, 10 + (mretv & MENU_LOWER_MASK),
281  &(rmpd->cmd_list[selected_line]));
282  } else {
283  if (rmpd->no_custom == FALSE) {
284  new_list = execute_executor(sw, *input, &new_length,
285  10 + (mretv & MENU_LOWER_MASK), NULL);
286  } else {
287  return RELOAD_DIALOG;
288  }
289  }
290  } else {
291  retv = (mretv & MENU_LOWER_MASK);
292  return retv;
293  }
294  } else if ((mretv & MENU_OK) && rmpd->cmd_list[selected_line].entry != NULL) {
295  if (rmpd->cmd_list[selected_line].nonselectable) {
296  return RELOAD_DIALOG;
297  }
299  new_list =
300  execute_executor(sw, rmpd->cmd_list[selected_line].entry, &new_length,
301  1, &(rmpd->cmd_list[selected_line]));
302  } else if ((mretv & MENU_CUSTOM_INPUT) && *input != NULL &&
303  *input[0] != '\0') {
304  if (rmpd->no_custom == FALSE) {
306  new_list = execute_executor(sw, *input, &new_length, 2, NULL);
307  } else {
308  return RELOAD_DIALOG;
309  }
310  }
311 
312  // If a new list was generated, use that an loop around.
313  if (new_list != NULL) {
314  for (unsigned int i = 0; i < rmpd->cmd_list_length; i++) {
315  g_free(rmpd->cmd_list[i].entry);
316  g_free(rmpd->cmd_list[i].icon_name);
317  g_free(rmpd->cmd_list[i].meta);
318  }
319  g_free(rmpd->cmd_list);
320 
321  rmpd->cmd_list = new_list;
322  rmpd->cmd_list_length = new_length;
323  retv = RESET_DIALOG;
324  }
325  return retv;
326 }
327 
328 static void script_mode_destroy(Mode *sw) {
330  if (rmpd != NULL) {
331  for (unsigned int i = 0; i < rmpd->cmd_list_length; i++) {
332  g_free(rmpd->cmd_list[i].entry);
333  g_free(rmpd->cmd_list[i].icon_name);
334  g_free(rmpd->cmd_list[i].meta);
335  }
336  g_free(rmpd->cmd_list);
337  g_free(rmpd->message);
338  g_free(rmpd->prompt);
339  g_free(rmpd->urgent_list);
340  g_free(rmpd->active_list);
341  g_free(rmpd);
342  sw->private_data = NULL;
343  }
344 }
345 static inline unsigned int get_index(unsigned int length, int index) {
346  if (index >= 0) {
347  return index;
348  }
349  if (((unsigned int)-index) <= length) {
350  return length + index;
351  }
352  // Out of range.
353  return UINT_MAX;
354 }
355 static char *_get_display_value(const Mode *sw, unsigned int selected_line,
356  G_GNUC_UNUSED int *state,
357  G_GNUC_UNUSED GList **list, int get_entry) {
359  for (unsigned int i = 0; i < pd->num_active_list; i++) {
360  unsigned int start =
362  unsigned int stop = get_index(pd->cmd_list_length, pd->active_list[i].stop);
363  if (selected_line >= start && selected_line <= stop) {
364  *state |= ACTIVE;
365  }
366  }
367  for (unsigned int i = 0; i < pd->num_urgent_list; i++) {
368  unsigned int start =
370  unsigned int stop = get_index(pd->cmd_list_length, pd->urgent_list[i].stop);
371  if (selected_line >= start && selected_line <= stop) {
372  *state |= URGENT;
373  }
374  }
375  if (pd->do_markup) {
376  *state |= MARKUP;
377  }
378  return get_entry ? g_strdup(pd->cmd_list[selected_line].entry) : NULL;
379 }
380 
381 static int script_token_match(const Mode *sw, rofi_int_matcher **tokens,
382  unsigned int index) {
384  int match = 1;
385  if (tokens) {
386  for (int j = 0; match && tokens != NULL && tokens[j] != NULL; j++) {
387  rofi_int_matcher *ftokens[2] = {tokens[j], NULL};
388  int test = 0;
389  test = helper_token_match(ftokens, rmpd->cmd_list[index].entry);
390  if (test == tokens[j]->invert && rmpd->cmd_list[index].meta) {
391  test = helper_token_match(ftokens, rmpd->cmd_list[index].meta);
392  }
393 
394  if (test == 0) {
395  match = 0;
396  }
397  }
398  }
399  return match;
400 }
401 static char *script_get_message(const Mode *sw) {
403  return g_strdup(pd->message);
404 }
405 static cairo_surface_t *
406 script_get_icon(const Mode *sw, unsigned int selected_line, int height) {
409  g_return_val_if_fail(pd->cmd_list != NULL, NULL);
410  DmenuScriptEntry *dr = &(pd->cmd_list[selected_line]);
411  if (dr->icon_name == NULL) {
412  return NULL;
413  }
414  if (dr->icon_fetch_uid > 0) {
416  }
419 }
420 
421 #include "mode-private.h"
423  Mode *sw = g_malloc0(sizeof(*sw));
424  char *endp = NULL;
425  char *parse = g_strdup(str);
426  unsigned int index = 0;
427  const char *const sep = ":";
428  for (char *token = strtok_r(parse, sep, &endp); token != NULL;
429  token = strtok_r(NULL, sep, &endp)) {
430  if (index == 0) {
431  sw->name = g_strdup(token);
432  } else if (index == 1) {
433  sw->ed = (void *)rofi_expand_path(token);
434  }
435  index++;
436  }
437  g_free(parse);
438  if (index == 2) {
440  sw->_init = script_mode_init;
447  sw->_get_completion = NULL, sw->_preprocess_input = NULL,
449 
450  return sw;
451  }
452  fprintf(
453  stderr,
454  "The script command '%s' has %u options, but needs 2: <name>:<script>.",
455  str, index);
457  return NULL;
458 }
459 
460 gboolean script_switcher_is_valid(const char *token) {
461  return strchr(token, ':') != NULL;
462 }
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
Definition: helper.c:1146
char helper_parse_char(const char *arg)
Definition: helper.c:359
char * rofi_expand_path(const char *input)
Definition: helper.c:713
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition: helper.c:494
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:131
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
Mode * script_switcher_parse_setup(const char *str)
Definition: script.c:422
gboolean script_switcher_is_valid(const char *token)
Definition: script.c:460
@ URGENT
Definition: textbox.h:105
@ ACTIVE
Definition: textbox.h:107
@ MARKUP
Definition: textbox.h:111
int rofi_view_error_dialog(const char *msg, int markup)
Definition: view.c:2018
static void script_mode_destroy(Mode *sw)
Definition: script.c:328
static cairo_surface_t * script_get_icon(const Mode *sw, unsigned int selected_line, int height)
Definition: script.c:406
static void script_switcher_free(Mode *sw)
Definition: script.c:233
static void parse_header_entry(Mode *sw, char *line, ssize_t length)
Definition: script.c:108
static ModeMode script_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
Definition: script.c:268
void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, G_GNUC_UNUSED size_t length)
Definition: script.c:79
static unsigned int get_index(unsigned int length, int index)
Definition: script.c:345
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:355
static char * script_get_message(const Mode *sw)
Definition: script.c:401
static DmenuScriptEntry * execute_executor(Mode *sw, char *arg, unsigned int *length, int value, DmenuScriptEntry *entry)
Definition: script.c:141
static void script_mode_reset_highlight(Mode *sw)
Definition: script.c:257
static int script_mode_init(Mode *sw)
Definition: script.c:242
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition: script.c:381
static unsigned int script_mode_get_num_entries(const Mode *sw)
Definition: script.c:251
struct rofi_range_pair * urgent_list
Definition: script.c:60
gboolean no_custom
Definition: script.c:71
struct rofi_range_pair * active_list
Definition: script.c:63
gboolean use_hot_keys
Definition: script.c:73
gboolean do_markup
Definition: script.c:68
unsigned int num_urgent_list
Definition: script.c:61
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
_mode_result _result
Definition: mode-private.h:177
__mode_get_num_entries _get_num_entries
Definition: mode-private.h:175
__mode_destroy _destroy
Definition: mode-private.h:173
_mode_preprocess_input _preprocess_input
Definition: mode-private.h:187
char * display_name
Definition: mode-private.h:165
_mode_free free
Definition: mode-private.h:199
_mode_token_match _token_match
Definition: mode-private.h:179
_mode_get_display_value _get_display_value
Definition: mode-private.h:181
_mode_get_icon _get_icon
Definition: mode-private.h:183
_mode_get_completion _get_completion
Definition: mode-private.h:185
void * ed
Definition: mode-private.h:201
__mode_init _init
Definition: mode-private.h:171
char * name
Definition: mode-private.h:163
_mode_get_message _get_message
Definition: mode-private.h:189
void * private_data
Definition: mode-private.h:192