102static gboolean
exec_cmd(
const char *cmd,
int run_in_term,
const char *orig) {
103 GError *error = NULL;
104 if (!cmd || !cmd[0]) {
107 gsize lf_cmd_size = 0;
108 gchar *lf_cmd = g_locale_from_utf8(cmd, -1, NULL, &lf_cmd_size, &error);
110 g_warning(
"Failed to convert command to locale encoding: %s",
118 char *hist = g_strdup_printf(
"%s\x1f%s", orig, cmd);
121 run_in_term ? &context : NULL)) {
184 unsigned int num_favorites) {
187 FILE *inp = fdopen(fd,
"r");
190 size_t buffer_length = 0;
192 while (getline(&buffer, &buffer_length, inp) > 0) {
195 if (buffer[strlen(buffer) - 1] ==
'\n') {
196 buffer[strlen(buffer) - 1] =
'\0';
201 for (
unsigned int j = 0; found == 0 && j < num_favorites; j++) {
202 if (strcasecmp(buffer, retv[j].entry) == 0) {
212 retv = g_realloc(retv, ((*length) + 2) *
sizeof(
RunEntry));
213 retv[(*length)].
entry = g_strdup(buffer);
214 retv[(*length)].
exec = g_shell_quote(buffer);
216 retv[(*length)].
icon = NULL;
222 if (buffer != NULL) {
225 if (fclose(inp) != 0) {
226 g_warning(
"Failed to close stdout off executor script: '%s'",
231 retv[(*length)].
entry = NULL;
232 retv[(*length)].
exec = NULL;
234 retv[(*length)].
icon = NULL;
244 GError *error = NULL;
246 unsigned int num_favorites = 0;
249 if (g_getenv(
"PATH") == NULL) {
256 for (
unsigned int i = 0; i < *length; i++) {
257 gchar **rs = g_strsplit(hretv[i],
"\x1f", 2);
258 retv[i].
entry = rs[0];
259 retv[i].
exec = rs[1];
260 if (retv[i].exec == NULL) {
261 retv[i].
exec = g_strdup(rs[0]);
269 num_favorites = (*length);
271 path = g_strdup(g_getenv(
"PATH"));
274 gchar *homedir = g_locale_to_utf8(g_get_home_dir(), -1, NULL, &l, &error);
276 g_debug(
"Failed to convert homedir to UTF-8: %s", error->message);
277 for (
unsigned int i = 0; retv[i].
entry != NULL; i++) {
278 g_free(retv[i].entry);
279 g_free(retv[i].exec);
282 g_clear_error(&error);
287 const char *
const sep =
":";
288 char *strtok_savepointer = NULL;
289 for (
const char *dirname = strtok_r(path, sep, &strtok_savepointer);
290 dirname != NULL; dirname = strtok_r(NULL, sep, &strtok_savepointer)) {
292 DIR *dir = opendir(fpath);
293 g_debug(
"Checking path %s for executable.", fpath);
299 gchar *dirn = g_locale_to_utf8(dirname, -1, NULL, &dirn_len, &error);
301 g_debug(
"Failed to convert directory name to UTF-8: %s",
303 g_clear_error(&error);
307 gboolean is_homedir = g_str_has_prefix(dirn, homedir);
310 while ((dent = readdir(dir)) != NULL) {
311 if (dent->d_type != DT_REG && dent->d_type != DT_LNK &&
312 dent->d_type != DT_UNKNOWN) {
316 if (dent->d_name[0] ==
'.') {
320 gchar *full_path = g_build_filename(dirname, dent->d_name, NULL);
321 gboolean b = g_file_test(full_path, G_FILE_TEST_IS_EXECUTABLE);
330 g_filename_to_utf8(dent->d_name, -1, NULL, &name_len, &error);
332 g_debug(
"Failed to convert filename to UTF-8: %s", error->message);
333 g_clear_error(&error);
340 for (
unsigned int j = 0; found == 0 && j < num_favorites; j++) {
341 if (g_strcmp0(name, retv[j].entry) == 0) {
351 retv = g_realloc(retv, ((*length) + 2) *
sizeof(
RunEntry));
352 retv[(*length)].
entry = name;
353 retv[(*length)].
exec = g_shell_quote(name);
355 retv[(*length)].
icon = NULL;
358 retv[(*length) + 1].
entry = NULL;
359 retv[(*length) + 1].
exec = NULL;
361 retv[(*length) + 1].
icon = NULL;
377 if ((*length) == 0) {
381 if ((*length) > num_favorites) {
382 g_qsort_with_data(&(retv[num_favorites]), (*length) - num_favorites,
387 unsigned int removed = 0;
388 for (
unsigned int index = num_favorites; index < ((*length) - 1); index++) {
389 if (g_strcmp0(retv[index].entry, retv[index + 1].entry) == 0) {
390 g_free(retv[index].entry);
391 retv[index].
entry = NULL;
392 g_free(retv[index].exec);
393 retv[index].
exec = NULL;
398 if ((*length) > num_favorites) {
399 g_qsort_with_data(&(retv[num_favorites]), (*length) - num_favorites,
403 (*length) -= removed;
450 unsigned int selected_line) {
479 char *epath = g_shell_quote(path);
480 char *arg = g_strdup_printf(
"%s %s", earg, epath);
499 if (!
exec_cmd(*input, run_in_term, *input)) {
514 if (selected_line < rmpd->cmd_list_length) {
void history_set(const char *filename, const char *entry)
void history_remove(const char *filename, const char *entry)
char ** history_get_list(const char *filename, unsigned int *length)
const Mode * rofi_get_completer(void)
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)