rofi  1.5.4
script.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2017 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 <config.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <strings.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <assert.h>
39 #include <errno.h>
40 #include "rofi.h"
41 #include "dialogs/script.h"
42 #include "helper.h"
43 
44 #include "widgets/textbox.h"
45 
46 #include "mode-private.h"
47 
48 
49 #include "rofi-icon-fetcher.h"
50 
52 
53 
54 typedef struct
55 {
57  unsigned int id;
61  unsigned int cmd_list_length;
62 
65  unsigned int num_urgent_list;
68  unsigned int num_active_list;
70  char *message;
71  char *prompt;
72  gboolean do_markup;
74 
78 void dmenuscript_parse_entry_extras ( G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, size_t length )
79 {
80  size_t length_key = 0;//strlen ( line );
81  while ( length_key <= length && buffer[length_key] != '\x1f' ) {
82  length_key++;
83  }
84  if ( length_key < length ) {
85  buffer[length_key] = '\0';
86  char *value = buffer + length_key + 1;
87  if ( strcasecmp(buffer, "icon" ) == 0 ) {
88  entry->icon_name = g_strdup(value);
89  }
90  }
91 }
92 
97 static void parse_header_entry ( Mode *sw, char *line, ssize_t length )
98 {
100  ssize_t length_key = 0;//strlen ( line );
101  while ( length_key <= length && line[length_key] != '\x1f' ) {
102  length_key++;
103  }
104 
105  if ( length_key < length ) {
106  line[length_key] = '\0';
107  char *value = line + length_key + 1;
108  if ( strcasecmp ( line, "message" ) == 0 ) {
109  g_free ( pd->message );
110  pd->message = strlen ( value ) ? g_strdup ( value ) : NULL;
111  }
112  else if ( strcasecmp ( line, "prompt" ) == 0 ) {
113  g_free ( pd->prompt );
114  pd->prompt = g_strdup ( value );
115  sw->display_name = pd->prompt;
116  }
117  else if ( strcasecmp ( line, "markup-rows" ) == 0 ) {
118  pd->do_markup = ( strcasecmp ( value, "true" ) == 0 );
119  }
120  else if ( strcasecmp ( line, "urgent" ) == 0 ) {
121  parse_ranges ( value, &( pd->urgent_list ), &( pd->num_urgent_list ) );
122  }
123  else if ( strcasecmp ( line, "active" ) == 0 ) {
124  parse_ranges ( value, &( pd->active_list ), &( pd->num_active_list ) );
125  }
126  }
127 }
128 
129 static DmenuScriptEntry *get_script_output ( Mode *sw, char *command, char *arg, unsigned int *length )
130 {
131  int fd = -1;
132  GError *error = NULL;
133  DmenuScriptEntry *retv = NULL;
134  char **argv = NULL;
135  int argc = 0;
136  *length = 0;
137  if ( g_shell_parse_argv ( command, &argc, &argv, &error ) ) {
138  argv = g_realloc ( argv, ( argc + 2 ) * sizeof ( char* ) );
139  argv[argc] = g_strdup ( arg );
140  argv[argc + 1] = NULL;
141  g_spawn_async_with_pipes ( NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
142  }
143  if ( error != NULL ) {
144  char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", command, error->message );
145  rofi_view_error_dialog ( msg, FALSE );
146  g_free ( msg );
147  // print error.
148  g_error_free ( error );
149  fd = -1;
150  }
151  if ( fd >= 0 ) {
152  FILE *inp = fdopen ( fd, "r" );
153  if ( inp ) {
154  char *buffer = NULL;
155  size_t buffer_length = 0;
156  ssize_t read_length = 0;
157  size_t actual_size = 0;
158  while ( ( read_length = getline ( &buffer, &buffer_length, inp ) ) > 0 ) {
159  // Filter out line-end.
160  if ( buffer[read_length - 1] == '\n' ) {
161  buffer[read_length - 1] = '\0';
162  }
163  if ( buffer[0] == '\0' ) {
164  parse_header_entry ( sw, &buffer[1], read_length - 1 );
165  }
166  else {
167  if ( actual_size < ( ( *length ) + 2 ) ) {
168  actual_size += 256;
169  retv = g_realloc ( retv, ( actual_size ) * sizeof ( DmenuScriptEntry ) );
170  }
171  size_t buf_length = strlen(buffer)+1;
172  retv[( *length )].entry = g_memdup ( buffer, buf_length);
173  retv[( *length )].icon_name = NULL;
174  retv[(*length)].icon_fetch_uid = 0;
175  if ( buf_length > 0 && (read_length > (ssize_t)buf_length) ) {
176  dmenuscript_parse_entry_extras ( sw, &(retv[(*length)]), buffer+buf_length, read_length-buf_length);
177  }
178  retv[( *length ) + 1].entry = NULL;
179  ( *length )++;
180  }
181  }
182  if ( buffer ) {
183  free ( buffer );
184  }
185  if ( fclose ( inp ) != 0 ) {
186  g_warning ( "Failed to close stdout off executor script: '%s'",
187  g_strerror ( errno ) );
188  }
189  }
190  }
191  return retv;
192 }
193 
194 static DmenuScriptEntry *execute_executor ( Mode *sw, char *result, unsigned int *length )
195 {
196  DmenuScriptEntry *retv = get_script_output ( sw, sw->ed, result, length );
197  return retv;
198 }
199 
200 static void script_switcher_free ( Mode *sw )
201 {
202  if ( sw == NULL ) {
203  return;
204  }
205  g_free ( sw->name );
206  g_free ( sw->ed );
207  g_free ( sw );
208 }
209 
210 static int script_mode_init ( Mode *sw )
211 {
212  if ( sw->private_data == NULL ) {
213  ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
214  sw->private_data = (void *) pd;
215  pd->cmd_list = get_script_output ( sw, (char *) sw->ed, NULL, &( pd->cmd_list_length ) );
216  }
217  return TRUE;
218 }
219 static unsigned int script_mode_get_num_entries ( const Mode *sw )
220 {
221  const ScriptModePrivateData *rmpd = (const ScriptModePrivateData *) sw->private_data;
222  return rmpd->cmd_list_length;
223 }
224 
226 {
228 
229  rmpd->num_urgent_list = 0;
230  g_free ( rmpd->urgent_list );
231  rmpd->urgent_list = NULL;
232  rmpd->num_active_list = 0;
233  g_free ( rmpd->active_list );
234  rmpd->active_list = NULL;
235 }
236 
237 static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
238 {
240  ModeMode retv = MODE_EXIT;
241  DmenuScriptEntry *new_list = NULL;
242  unsigned int new_length = 0;
243 
244  if ( ( mretv & MENU_NEXT ) ) {
245  retv = NEXT_DIALOG;
246  }
247  else if ( ( mretv & MENU_PREVIOUS ) ) {
248  retv = PREVIOUS_DIALOG;
249  }
250  else if ( ( mretv & MENU_QUICK_SWITCH ) ) {
251  retv = ( mretv & MENU_LOWER_MASK );
252  }
253  else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) {
255  new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length );
256  }
257  else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
259  new_list = execute_executor ( sw, *input, &new_length );
260  }
261 
262  // If a new list was generated, use that an loop around.
263  if ( new_list != NULL ) {
264  for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ){
265  g_free ( rmpd->cmd_list[i].entry );
266  g_free ( rmpd->cmd_list[i].icon_name );
267  }
268  g_free ( rmpd->cmd_list );
269 
270  rmpd->cmd_list = new_list;
271  rmpd->cmd_list_length = new_length;
272  retv = RESET_DIALOG;
273  }
274  return retv;
275 }
276 
277 static void script_mode_destroy ( Mode *sw )
278 {
280  if ( rmpd != NULL ) {
281  for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ){
282  g_free ( rmpd->cmd_list[i].entry );
283  g_free ( rmpd->cmd_list[i].icon_name );
284  }
285  g_free ( rmpd->cmd_list );
286  g_free ( rmpd->message );
287  g_free ( rmpd->prompt );
288  g_free ( rmpd->urgent_list );
289  g_free ( rmpd->active_list );
290  g_free ( rmpd );
291  sw->private_data = NULL;
292  }
293 }
294 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 )
295 {
297  for ( unsigned int i = 0; i < pd->num_active_list; i++ ) {
298  if ( selected_line >= pd->active_list[i].start && selected_line <= pd->active_list[i].stop ) {
299  *state |= ACTIVE;
300  }
301  }
302  for ( unsigned int i = 0; i < pd->num_urgent_list; i++ ) {
303  if ( selected_line >= pd->urgent_list[i].start && selected_line <= pd->urgent_list[i].stop ) {
304  *state |= URGENT;
305  }
306  }
307  if ( pd->do_markup ) {
308  *state |= MARKUP;
309  }
310  return get_entry ? g_strdup ( pd->cmd_list[selected_line].entry ) : NULL;
311 }
312 
313 static int script_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
314 {
316  return helper_token_match ( tokens, rmpd->cmd_list[index].entry );
317 }
318 static char *script_get_message ( const Mode *sw )
319 {
321  return g_strdup ( pd->message );
322 }
323 static cairo_surface_t *script_get_icon ( const Mode *sw, unsigned int selected_line, int height )
324 {
326  g_return_val_if_fail ( pd->cmd_list != NULL, NULL );
327  DmenuScriptEntry *dr = &( pd->cmd_list[selected_line] );
328  if ( dr->icon_name == NULL ) {
329  return NULL;
330  }
331  if ( dr->icon_fetch_uid > 0 ) {
332  return rofi_icon_fetcher_get ( dr->icon_fetch_uid );
333  }
334  dr->icon_fetch_uid = rofi_icon_fetcher_query ( dr->icon_name, height );
335  return rofi_icon_fetcher_get ( dr->icon_fetch_uid );
336 }
337 
338 #include "mode-private.h"
339 Mode *script_switcher_parse_setup ( const char *str )
340 {
341  Mode *sw = g_malloc0 ( sizeof ( *sw ) );
342  char *endp = NULL;
343  char *parse = g_strdup ( str );
344  unsigned int index = 0;
345  const char *const sep = ":";
346  for ( char *token = strtok_r ( parse, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
347  if ( index == 0 ) {
348  sw->name = g_strdup ( token );
349  }
350  else if ( index == 1 ) {
351  sw->ed = (void *) rofi_expand_path ( token );
352  }
353  index++;
354  }
355  g_free ( parse );
356  if ( index == 2 ) {
358  sw->_init = script_mode_init;
365  sw->_get_completion = NULL,
366  sw->_preprocess_input = NULL,
368 
369  return sw;
370  }
371  fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.", str, index );
372  script_switcher_free ( sw );
373  return NULL;
374 }
375 
376 gboolean script_switcher_is_valid ( const char *token )
377 {
378  return strchr ( token, ':' ) != NULL;
379 }
RESET_DIALOG
@ RESET_DIALOG
Definition: mode.h:60
rofi_range_pair
Definition: rofi-types.h:226
MENU_QUICK_SWITCH
@ MENU_QUICK_SWITCH
Definition: mode.h:79
ACTIVE
@ ACTIVE
Definition: textbox.h:101
script_mode_result
static ModeMode script_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
Definition: script.c:237
rofi_mode::name
char * name
Definition: mode-private.h:156
ScriptModePrivateData::num_active_list
unsigned int num_active_list
Definition: script.c:68
MARKUP
@ MARKUP
Definition: textbox.h:105
rofi_mode::_init
__mode_init _init
Definition: mode-private.h:164
MENU_OK
@ MENU_OK
Definition: mode.h:69
rofi_int_matcher_t
Definition: rofi-types.h:235
DmenuScriptEntry::icon_fetch_uid
uint32_t icon_fetch_uid
Definition: dmenuscriptshared.h:10
rofi_mode::ed
void * ed
Definition: mode-private.h:194
_get_display_value
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:294
PREVIOUS_DIALOG
@ PREVIOUS_DIALOG
Definition: mode.h:58
rofi_view_error_dialog
int rofi_view_error_dialog(const char *msg, int markup)
Definition: view.c:1763
ScriptModePrivateData::do_markup
gboolean do_markup
Definition: script.c:72
MENU_PREVIOUS
@ MENU_PREVIOUS
Definition: mode.h:81
rofi_mode::_token_match
_mode_token_match _token_match
Definition: mode-private.h:172
NEXT_DIALOG
@ NEXT_DIALOG
Definition: mode.h:54
script_get_icon
static cairo_surface_t * script_get_icon(const Mode *sw, unsigned int selected_line, int height)
Definition: script.c:323
ScriptModePrivateData::id
unsigned int id
Definition: script.c:57
rofi_mode::_get_icon
_mode_get_icon _get_icon
Definition: mode-private.h:176
rofi_mode::_preprocess_input
_mode_preprocess_input _preprocess_input
Definition: mode-private.h:180
script_switcher_free
static void script_switcher_free(Mode *sw)
Definition: script.c:200
ScriptModePrivateData::message
char * message
Definition: script.c:70
script_mode_get_num_entries
static unsigned int script_mode_get_num_entries(const Mode *sw)
Definition: script.c:219
mode-private.h
ScriptModePrivateData::active_list
struct rofi_range_pair * active_list
Definition: script.c:67
script_switcher_parse_setup
Mode * script_switcher_parse_setup(const char *str)
Definition: script.c:339
DmenuScriptEntry::icon_name
char * icon_name
Definition: dmenuscriptshared.h:8
MODE_EXIT
@ MODE_EXIT
Definition: mode.h:52
ScriptModePrivateData
Definition: script.c:55
ScriptModePrivateData::prompt
char * prompt
Definition: script.c:71
dmenuscriptshared.h
rofi_icon_fetcher_get
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
Definition: rofi-icon-fetcher.c:216
rofi_mode::free
_mode_free free
Definition: mode-private.h:192
rofi_expand_path
char * rofi_expand_path(const char *input)
Definition: helper.c:658
URGENT
@ URGENT
Definition: textbox.h:99
rofi_mode
Definition: mode-private.h:152
DmenuScriptEntry::entry
char * entry
Definition: dmenuscriptshared.h:6
rofi.h
get_script_output
static DmenuScriptEntry * get_script_output(Mode *sw, char *command, char *arg, unsigned int *length)
Definition: script.c:129
rofi_range_pair::start
unsigned int start
Definition: rofi-types.h:227
rofi_mode::private_data
void * private_data
Definition: mode-private.h:185
ScriptModePrivateData::urgent_list
struct rofi_range_pair * urgent_list
Definition: script.c:64
rofi_mode::_destroy
__mode_destroy _destroy
Definition: mode-private.h:166
MENU_CUSTOM_INPUT
@ MENU_CUSTOM_INPUT
Definition: mode.h:75
rofi_mode::display_name
char * display_name
Definition: mode-private.h:158
mode_get_private_data
void * mode_get_private_data(const Mode *mode)
Definition: mode.c:128
dmenuscript_parse_entry_extras
void dmenuscript_parse_entry_extras(G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, size_t length)
Definition: script.c:78
parse_ranges
void parse_ranges(char *input, rofi_range_pair **list, unsigned int *length)
Definition: helper.c:1137
script_mode_destroy
static void script_mode_destroy(Mode *sw)
Definition: script.c:277
MENU_NEXT
@ MENU_NEXT
Definition: mode.h:73
rofi_mode::_get_display_value
_mode_get_display_value _get_display_value
Definition: mode-private.h:174
script_switcher_is_valid
gboolean script_switcher_is_valid(const char *token)
Definition: script.c:376
rofi_icon_fetcher_query
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
Definition: rofi-icon-fetcher.c:182
script_mode_init
static int script_mode_init(Mode *sw)
Definition: script.c:210
helper_token_match
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition: helper.c:445
textbox.h
rofi-icon-fetcher.h
script.h
script_token_match
static int script_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition: script.c:313
ModeMode
ModeMode
Definition: mode.h:50
DmenuScriptEntry
Definition: dmenuscriptshared.h:4
execute_executor
static DmenuScriptEntry * execute_executor(Mode *sw, char *result, unsigned int *length)
Definition: script.c:194
parse_header_entry
static void parse_header_entry(Mode *sw, char *line, ssize_t length)
Definition: script.c:97
script_mode_reset_highlight
static void script_mode_reset_highlight(Mode *sw)
Definition: script.c:225
MENU_LOWER_MASK
@ MENU_LOWER_MASK
Definition: mode.h:85
helper.h
ScriptModePrivateData::cmd_list_length
unsigned int cmd_list_length
Definition: script.c:61
ScriptModePrivateData::num_urgent_list
unsigned int num_urgent_list
Definition: script.c:65
script_get_message
static char * script_get_message(const Mode *sw)
Definition: script.c:318
rofi_mode::_result
_mode_result _result
Definition: mode-private.h:170
rofi_mode::_get_completion
_mode_get_completion _get_completion
Definition: mode-private.h:178
rofi_mode::_get_message
_mode_get_message _get_message
Definition: mode-private.h:182
ScriptModePrivateData::cmd_list
DmenuScriptEntry * cmd_list
Definition: script.c:59
rofi_mode::_get_num_entries
__mode_get_num_entries _get_num_entries
Definition: mode-private.h:168