int capture_output_to_menu(FileView *view, const char cmd[], menu_info *m) { FILE *file, *err; char *line = NULL; int x; pid_t pid; LOG_INFO_MSG("Capturing output of the command to a menu: %s", cmd); pid = background_and_capture((char *)cmd, &file, &err); if(pid == (pid_t)-1) { show_error_msgf("Trouble running command", "Unable to run: %s", cmd); return 0; } show_progress("", 0); ui_cancellation_reset(); ui_cancellation_enable(); wait_for_data_from(pid, file, 0); x = 0; while((line = read_line(file, line)) != NULL) { char *expanded_line; show_progress("Loading menu", 1000); m->items = realloc(m->items, sizeof(char *)*(x + 1)); expanded_line = expand_tabulation_a(line, cfg.tab_stop); if(expanded_line != NULL) { m->items[x++] = expanded_line; } wait_for_data_from(pid, file, 0); } m->len = x; ui_cancellation_disable(); fclose(file); print_errors(err); if(ui_cancellation_requested()) { append_to_string(&m->title, "(cancelled) "); append_to_string(&m->empty_msg, " (cancelled)"); } return display_menu(m, view); }
/* Executes the cmd ignoring its output. */ static void output_to_nowhere(const char cmd[]) { FILE *file, *err; int error; setup_shellout_env(); error = (background_and_capture((char *)cmd, 1, &file, &err) == (pid_t)-1); cleanup_shellout_env(); if(error) { show_error_msgf("Trouble running command", "Unable to run: %s", cmd); return; } /* FIXME: better way of doing this would be to redirect these streams to * /dev/null rather than closing them, but not sure about Windows (NUL * device might work). */ fclose(file); fclose(err); }
/* Executes the cmd and displays its output on the status bar. */ static void output_to_statusbar(const char cmd[]) { FILE *file, *err; char buf[2048]; char *lines; size_t len; int error; setup_shellout_env(); error = (background_and_capture((char *)cmd, 1, &file, &err) == (pid_t)-1); cleanup_shellout_env(); if(error) { show_error_msgf("Trouble running command", "Unable to run: %s", cmd); return; } lines = NULL; len = 0; while(fgets(buf, sizeof(buf), file) == buf) { char *p; chomp(buf); p = realloc(lines, len + 1 + strlen(buf) + 1); if(p != NULL) { lines = p; len += sprintf(lines + len, "%s%s", (len == 0) ? "": "\n", buf); } } fclose(file); fclose(err); status_bar_message((lines == NULL) ? "" : lines); free(lines); }