rofi  1.5.1
run.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 
34 #define G_LOG_DOMAIN "Dialogs.Run"
35 
36 #include <config.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 
40 #include <unistd.h>
41 #include <limits.h>
42 #include <signal.h>
43 #include <sys/types.h>
44 #include <dirent.h>
45 #include <strings.h>
46 #include <string.h>
47 #include <errno.h>
48 
49 #include "rofi.h"
50 #include "settings.h"
51 #include "helper.h"
52 #include "history.h"
53 #include "dialogs/run.h"
54 
55 #include "mode-private.h"
56 
57 #include "timings.h"
61 #define RUN_CACHE_FILE "rofi-3.runcache"
62 
66 typedef struct
67 {
69  char **cmd_list;
71  unsigned int cmd_list_length;
73 
80 static void exec_cmd ( const char *cmd, int run_in_term )
81 {
82  GError *error = NULL;
83  if ( !cmd || !cmd[0] ) {
84  return;
85  }
86  gsize lf_cmd_size = 0;
87  gchar *lf_cmd = g_locale_from_utf8 ( cmd, -1, NULL, &lf_cmd_size, &error );
88  if ( error != NULL ) {
89  g_warning ( "Failed to convert command to locale encoding: %s", error->message );
90  g_error_free ( error );
91  return;
92  }
93 
94  char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
95  RofiHelperExecuteContext context = { .name = NULL };
96  // FIXME: assume startup notification support for terminals
97  if ( helper_execute_command ( NULL, lf_cmd, run_in_term, run_in_term ? &context : NULL ) ) {
103  history_set ( path, cmd );
104  }
105  else {
106  history_remove ( path, cmd );
107  }
108  g_free ( path );
109  g_free ( lf_cmd );
110 }
111 
117 static void delete_entry ( const char *cmd )
118 {
119  char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
120 
121  history_remove ( path, cmd );
122 
123  g_free ( path );
124 }
125 
135 static int sort_func ( const void *a, const void *b, G_GNUC_UNUSED void *data )
136 {
137  const char *astr = *( const char * const * ) a;
138  const char *bstr = *( const char * const * ) b;
139 
140  if ( astr == NULL && bstr == NULL ) {
141  return 0;
142  }
143  else if ( astr == NULL ) {
144  return 1;
145  }
146  else if ( bstr == NULL ) {
147  return -1;
148  }
149  return g_strcmp0 ( astr, bstr );
150 }
151 
155 static char ** get_apps_external ( char **retv, unsigned int *length, unsigned int num_favorites )
156 {
158  if ( fd >= 0 ) {
159  FILE *inp = fdopen ( fd, "r" );
160  if ( inp ) {
161  char *buffer = NULL;
162  size_t buffer_length = 0;
163 
164  while ( getline ( &buffer, &buffer_length, inp ) > 0 ) {
165  int found = 0;
166  // Filter out line-end.
167  if ( buffer[strlen ( buffer ) - 1] == '\n' ) {
168  buffer[strlen ( buffer ) - 1] = '\0';
169  }
170 
171  // This is a nice little penalty, but doable? time will tell.
172  // given num_favorites is max 25.
173  for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
174  if ( strcasecmp ( buffer, retv[j] ) == 0 ) {
175  found = 1;
176  }
177  }
178 
179  if ( found == 1 ) {
180  continue;
181  }
182 
183  // No duplicate, add it.
184  retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
185  retv[( *length )] = g_strdup ( buffer );
186 
187  ( *length )++;
188  }
189  if ( buffer != NULL ) {
190  free ( buffer );
191  }
192  if ( fclose ( inp ) != 0 ) {
193  g_warning ( "Failed to close stdout off executor script: '%s'",
194  g_strerror ( errno ) );
195  }
196  }
197  }
198  retv[( *length ) ] = NULL;
199  return retv;
200 }
201 
205 static char ** get_apps ( unsigned int *length )
206 {
207  GError *error = NULL;
208  char **retv = NULL;
209  unsigned int num_favorites = 0;
210  char *path;
211 
212  if ( g_getenv ( "PATH" ) == NULL ) {
213  return NULL;
214  }
215  TICK_N ( "start" );
216  path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
217  retv = history_get_list ( path, length );
218  g_free ( path );
219  // Keep track of how many where loaded as favorite.
220  num_favorites = ( *length );
221 
222  path = g_strdup ( g_getenv ( "PATH" ) );
223 
224  gsize l = 0;
225  gchar *homedir = g_locale_to_utf8 ( g_get_home_dir (), -1, NULL, &l, &error );
226  if ( error != NULL ) {
227  g_debug ( "Failed to convert homedir to UTF-8: %s", error->message );
228  g_clear_error ( &error );
229  g_free ( homedir );
230  return NULL;
231  }
232 
233  const char *const sep = ":";
234  char *strtok_savepointer = NULL;
235  for ( const char *dirname = strtok_r ( path, sep, &strtok_savepointer ); dirname != NULL; dirname = strtok_r ( NULL, sep, &strtok_savepointer ) ) {
236  char *fpath = rofi_expand_path ( dirname );
237  DIR *dir = opendir ( fpath );
238  g_debug ( "Checking path %s for executable.", fpath );
239  g_free ( fpath );
240 
241  if ( dir != NULL ) {
242  struct dirent *dent;
243  gsize dirn_len = 0;
244  gchar *dirn = g_locale_to_utf8 ( dirname, -1, NULL, &dirn_len, &error );
245  if ( error != NULL ) {
246  g_debug ( "Failed to convert directory name to UTF-8: %s", error->message );
247  g_clear_error ( &error );
248  closedir ( dir );
249  continue;
250  }
251  gboolean is_homedir = g_str_has_prefix ( dirn, homedir );
252  g_free ( dirn );
253 
254  while ( ( dent = readdir ( dir ) ) != NULL ) {
255  if ( dent->d_type != DT_REG && dent->d_type != DT_LNK && dent->d_type != DT_UNKNOWN ) {
256  continue;
257  }
258  // Skip dot files.
259  if ( dent->d_name[0] == '.' ) {
260  continue;
261  }
262  if ( is_homedir ) {
263  gchar *fpath = g_build_filename ( dirname, dent->d_name, NULL );
264  gboolean b = g_file_test ( fpath, G_FILE_TEST_IS_EXECUTABLE );
265  g_free ( fpath );
266  if ( !b ) {
267  continue;
268  }
269  }
270 
271  gsize name_len;
272  gchar *name = g_filename_to_utf8 ( dent->d_name, -1, NULL, &name_len, &error );
273  if ( error != NULL ) {
274  g_debug ( "Failed to convert filename to UTF-8: %s", error->message );
275  g_clear_error ( &error );
276  g_free ( name );
277  continue;
278  }
279  // This is a nice little penalty, but doable? time will tell.
280  // given num_favorites is max 25.
281  int found = 0;
282  for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
283  if ( g_strcmp0 ( name, retv[j] ) == 0 ) {
284  found = 1;
285  }
286  }
287 
288  if ( found == 1 ) {
289  g_free ( name );
290  continue;
291  }
292 
293  retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
294  retv[( *length )] = name;
295  retv[( *length ) + 1] = NULL;
296  ( *length )++;
297  }
298 
299  closedir ( dir );
300  }
301  }
302  g_free ( homedir );
303 
304  // Get external apps.
305  if ( config.run_list_command != NULL && config.run_list_command[0] != '\0' ) {
306  retv = get_apps_external ( retv, length, num_favorites );
307  }
308  // No sorting needed.
309  if ( ( *length ) == 0 ) {
310  return retv;
311  }
312  // TODO: check this is still fast enough. (takes 1ms on laptop.)
313  if ( ( *length ) > num_favorites ) {
314  g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func, NULL );
315  }
316  g_free ( path );
317 
318  unsigned int removed = 0;
319  for ( unsigned int index = num_favorites; index < ( ( *length ) - 1 ); index++ ) {
320  if ( g_strcmp0 ( retv[index], retv[index + 1] ) == 0 ) {
321  g_free ( retv[index] );
322  retv[index] = NULL;
323  removed++;
324  }
325  }
326 
327  if ( ( *length ) > num_favorites ) {
328  g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ),
329  sort_func,
330  NULL );
331  }
332  // Reduce array length;
333  ( *length ) -= removed;
334 
335  TICK_N ( "stop" );
336  return retv;
337 }
338 
339 static int run_mode_init ( Mode *sw )
340 {
341  if ( sw->private_data == NULL ) {
342  RunModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
343  sw->private_data = (void *) pd;
344  pd->cmd_list = get_apps ( &( pd->cmd_list_length ) );
345  }
346 
347  return TRUE;
348 }
349 static void run_mode_destroy ( Mode *sw )
350 {
352  if ( rmpd != NULL ) {
353  g_strfreev ( rmpd->cmd_list );
354  g_free ( rmpd );
355  sw->private_data = NULL;
356  }
357 }
358 
359 static unsigned int run_mode_get_num_entries ( const Mode *sw )
360 {
361  const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
362  return rmpd->cmd_list_length;
363 }
364 
365 static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
366 {
368  ModeMode retv = MODE_EXIT;
369 
370  gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
371 
372  if ( mretv & MENU_NEXT ) {
373  retv = NEXT_DIALOG;
374  }
375  else if ( mretv & MENU_PREVIOUS ) {
376  retv = PREVIOUS_DIALOG;
377  }
378  else if ( mretv & MENU_QUICK_SWITCH ) {
379  retv = ( mretv & MENU_LOWER_MASK );
380  }
381  else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
382  exec_cmd ( rmpd->cmd_list[selected_line], run_in_term );
383  }
384  else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
385  exec_cmd ( *input, run_in_term );
386  }
387  else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->cmd_list[selected_line] ) {
388  delete_entry ( rmpd->cmd_list[selected_line] );
389 
390  // Clear the list.
391  retv = RELOAD_DIALOG;
392  run_mode_destroy ( sw );
393  run_mode_init ( sw );
394  }
395  return retv;
396 }
397 
398 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 )
399 {
400  const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
401  return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
402 }
403 static int run_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
404 {
405  const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
406  return helper_token_match ( tokens, rmpd->cmd_list[index] );
407 }
408 
409 #include "mode-private.h"
411 {
412  .name = "run",
413  .cfg_name_key = "display-run",
414  ._init = run_mode_init,
415  ._get_num_entries = run_mode_get_num_entries,
416  ._result = run_mode_result,
417  ._destroy = run_mode_destroy,
418  ._token_match = run_token_match,
419  ._get_display_value = _get_display_value,
420  ._get_completion = NULL,
421  ._preprocess_input = NULL,
422  .private_data = NULL,
423  .free = NULL
424 };
const char * cache_dir
Definition: rofi.c:80
char ** cmd_list
Definition: run.c:69
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: run.c:398
Mode run_mode
Definition: run.c:410
Definition: mode.h:69
static ModeMode run_mode_result(Mode *sw, int mretv, char **input, unsigned int selected_line)
Definition: run.c:365
void history_set(const char *filename, const char *entry)
Definition: history.c:175
char ** history_get_list(const char *filename, unsigned int *length)
Definition: history.c:308
ModeMode
Definition: mode.h:49
gboolean helper_execute_command(const char *wd, const char *cmd, gboolean run_in_term, RofiHelperExecuteContext *context)
Definition: helper.c:1000
static int run_token_match(const Mode *sw, rofi_int_matcher **tokens, unsigned int index)
Definition: run.c:403
#define RUN_CACHE_FILE
Definition: run.c:61
Definition: mode.h:52
static char ** get_apps_external(char **retv, unsigned int *length, unsigned int num_favorites)
Definition: run.c:155
static void delete_entry(const char *cmd)
Definition: run.c:117
unsigned int cmd_list_length
Definition: run.c:71
void history_remove(const char *filename, const char *entry)
Definition: history.c:242
static int sort_func(const void *a, const void *b, G_GNUC_UNUSED void *data)
Definition: run.c:135
static int run_mode_init(Mode *sw)
Definition: run.c:339
int helper_token_match(rofi_int_matcher *const *tokens, const char *input)
Definition: helper.c:473
static unsigned int run_mode_get_num_entries(const Mode *sw)
Definition: run.c:359
static char ** get_apps(unsigned int *length)
Definition: run.c:205
void * private_data
Definition: mode-private.h:185
char * rofi_expand_path(const char *input)
Definition: helper.c:669
int execute_generator(const char *cmd)
Definition: helper.c:486
char * run_list_command
Definition: settings.h:85
const gchar * name
Definition: helper.h:269
Definition: mode.h:73
Settings config
#define TICK_N(a)
Definition: timings.h:94
static void exec_cmd(const char *cmd, int run_in_term)
Definition: run.c:80
char * name
Definition: mode-private.h:156
static void run_mode_destroy(Mode *sw)
Definition: run.c:349