Removed src/pipethrough.* files.
[midnight-commander.git] / src / panelize.c
blobfb7aa85e915e8a854ce234368faf98e8f4bde0db
1 /* External panelize
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1995 Janne Kukonlehto
6 1995 Jakub Jelinek
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 /** \file panelize.c
24 * \brief Source: External panelization module
27 #include <config.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
36 #include "lib/global.h"
38 #include "lib/skin.h"
39 #include "lib/vfs/mc-vfs/vfs.h"
40 #include "lib/mcconfig.h" /* Load/save directories panelize */
41 #include "lib/strutil.h"
43 #include "dialog.h"
44 #include "widget.h"
45 #include "wtools.h" /* For common_dialog_repaint() */
46 #include "setup.h" /* For profile_bname */
47 #include "dir.h"
48 #include "panel.h" /* current_panel */
49 #include "layout.h" /* repaint_screen() */
50 #include "main.h"
51 #include "panelize.h"
52 #include "history.h"
54 #define UX 5
55 #define UY 2
57 #define BX 5
58 #define BY 18
60 #define BUTTONS 4
61 #define LABELS 3
62 #define B_ADD B_USER
63 #define B_REMOVE (B_USER + 1)
65 static WListbox *l_panelize;
66 static Dlg_head *panelize_dlg;
67 static int last_listitem;
68 static WInput *pname;
70 static struct {
71 int ret_cmd, flags, y, x;
72 const char *text;
73 } panelize_but [BUTTONS] = {
74 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
75 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
76 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
77 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") },
80 static const char *panelize_section = "Panelize";
81 static void do_external_panelize (char *command);
83 /* Directory panelize */
84 static struct panelize {
85 char *command;
86 char *label;
87 struct panelize *next;
88 } *panelize = NULL;
90 static void
91 update_command (void)
93 if (l_panelize->pos != last_listitem) {
94 last_listitem = l_panelize->pos;
95 assign_text (pname,
96 ((struct panelize *) l_panelize->current->data)->command);
97 pname->point = 0;
98 update_input (pname, 1);
102 static cb_ret_t
103 panelize_callback (Dlg_head *h, Widget *sender,
104 dlg_msg_t msg, int parm, void *data)
106 switch (msg) {
107 case DLG_INIT:
108 case DLG_POST_KEY:
109 tty_setcolor (MENU_ENTRY_COLOR);
110 update_command ();
111 return MSG_HANDLED;
113 case DLG_DRAW:
114 common_dialog_repaint (h);
115 tty_setcolor (COLOR_NORMAL);
116 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
117 return MSG_HANDLED;
119 default:
120 return default_dlg_callback (h, sender, msg, parm, data);
124 static void
125 init_panelize (void)
127 int i, panelize_cols = COLS - 6;
128 struct panelize *current = panelize;
130 #ifdef ENABLE_NLS
131 static int i18n_flag = 0;
132 static int maxlen = 0;
134 if (!i18n_flag) {
135 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
136 while (i--) {
137 panelize_but[i].text = _(panelize_but[i].text);
138 maxlen += str_term_width1 (panelize_but[i].text) + 5;
140 maxlen += 10;
142 i18n_flag = 1;
144 panelize_cols = max (panelize_cols, maxlen);
146 panelize_but[2].x =
147 panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
148 panelize_but[1].x =
149 panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
150 panelize_but[0].x =
151 panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
153 #endif /* ENABLE_NLS */
155 last_listitem = 0;
157 do_refresh ();
159 panelize_dlg =
160 create_dlg (0, 0, 22, panelize_cols, dialog_colors,
161 panelize_callback, "[External panelize]",
162 _("External panelize"), DLG_CENTER | DLG_REVERSE);
164 for (i = 0; i < BUTTONS; i++)
165 add_widget (panelize_dlg,
166 button_new (BY + panelize_but[i].y,
167 BX + panelize_but[i].x,
168 panelize_but[i].ret_cmd,
169 panelize_but[i].flags,
170 panelize_but[i].text, 0));
172 pname =
173 input_new (UY + 14, UX, INPUT_COLOR, panelize_dlg->cols - 10, "",
174 "in", INPUT_COMPLETE_DEFAULT);
175 add_widget (panelize_dlg, pname);
177 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
179 /* get new listbox */
180 l_panelize =
181 listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, NULL);
183 while (current) {
184 listbox_add_item (l_panelize, 0, 0, current->label, current);
185 current = current->next;
188 /* add listbox to the dialogs */
189 add_widget (panelize_dlg, l_panelize);
191 listbox_select_entry (l_panelize,
192 listbox_search_text (l_panelize,
193 _("Other command")));
196 static void panelize_done (void)
198 destroy_dlg (panelize_dlg);
199 repaint_screen ();
202 static void add2panelize (char *label, char *command)
204 struct panelize *current, *old;
206 old = NULL;
207 current = panelize;
208 while (current && strcmp (current->label, label) <= 0){
209 old = current;
210 current = current->next;
213 if (old == NULL){
214 panelize = g_new (struct panelize, 1);
215 panelize->label = label;
216 panelize->command = command;
217 panelize->next = current;
218 } else {
219 struct panelize *new;
220 new = g_new (struct panelize, 1);
221 new->label = label;
222 new->command = command;
223 old->next = new;
224 new->next = current;
228 static void
229 add2panelize_cmd (void)
231 char *label;
233 if (pname->buffer && (*pname->buffer)) {
234 label = input_dialog (_(" Add to external panelize "),
235 _(" Enter command label: "),
236 MC_HISTORY_FM_PANELIZE_ADD,
237 "");
238 if (!label)
239 return;
240 if (!*label) {
241 g_free (label);
242 return;
245 add2panelize (label, g_strdup (pname->buffer));
249 static void remove_from_panelize (struct panelize *entry)
251 if (strcmp (entry->label, _("Other command")) != 0) {
252 if (entry == panelize) {
253 panelize = panelize->next;
254 } else {
255 struct panelize *current = panelize;
256 while (current && current->next != entry)
257 current = current->next;
258 if (current) {
259 current->next = entry->next;
263 g_free (entry->label);
264 g_free (entry->command);
265 g_free (entry);
269 void
270 external_panelize (void)
272 char *target = NULL;
274 if (!vfs_current_is_local ()){
275 message (D_ERROR, MSG_ERROR,
276 _(" Cannot run external panelize in a non-local directory "));
277 return;
280 init_panelize ();
282 /* display file info */
283 tty_setcolor (SELECTED_COLOR);
285 run_dlg (panelize_dlg);
287 switch (panelize_dlg->ret_value) {
288 case B_CANCEL:
289 break;
291 case B_ADD:
292 add2panelize_cmd ();
293 break;
295 case B_REMOVE:
296 remove_from_panelize (l_panelize->current->data);
297 break;
299 case B_ENTER:
300 target = pname->buffer;
301 if (target != NULL && *target) {
302 char *cmd = g_strdup (target);
303 destroy_dlg (panelize_dlg);
304 do_external_panelize (cmd);
305 g_free (cmd);
306 repaint_screen ();
307 return;
309 break;
312 panelize_done ();
315 void load_panelize (void)
317 gchar **profile_keys, **keys;
318 gsize len;
319 GIConv conv;
320 GString *buffer;
322 conv = str_crt_conv_from ("UTF-8");
324 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section,&len);
326 add2panelize (g_strdup (_("Other command")), g_strdup (""));
328 if (!profile_keys || *profile_keys == NULL){
329 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
330 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
331 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
332 return;
335 while (*profile_keys){
337 if (utf8_display || conv == INVALID_CONV){
338 buffer = g_string_new (*profile_keys);
339 } else {
340 buffer = g_string_new ("");
341 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
343 g_string_free(buffer, TRUE);
344 buffer = g_string_new (*profile_keys);
348 add2panelize (
349 g_string_free(buffer, FALSE),
350 mc_config_get_string(mc_main_config,panelize_section,*profile_keys,"")
352 profile_keys++;
354 g_strfreev(keys);
355 str_close_conv (conv);
358 void save_panelize (void)
360 struct panelize *current = panelize;
362 mc_config_del_group (mc_main_config, panelize_section);
363 for (;current; current = current->next){
364 if (strcmp (current->label, _("Other command")))
365 mc_config_set_string(mc_main_config,
366 panelize_section,
367 current->label,
368 current->command);
370 mc_config_save_file (mc_main_config, NULL);
373 void done_panelize (void)
375 struct panelize *current = panelize;
376 struct panelize *next;
378 for (; current; current = next){
379 next = current->next;
380 g_free (current->label);
381 g_free (current->command);
382 g_free (current);
386 static void do_external_panelize (char *command)
388 int status, link_to_dir, stale_link;
389 int next_free = 0;
390 struct stat st;
391 dir_list *list = &current_panel->dir;
392 char line [MC_MAXPATHLEN];
393 char *name;
394 FILE *external;
396 open_error_pipe ();
397 external = popen (command, "r");
398 if (!external){
399 close_error_pipe (D_ERROR, _("Cannot invoke command."));
400 return;
402 /* Clear the counters and the directory list */
403 panel_clean_dir (current_panel);
405 while (1) {
406 clearerr(external);
407 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
408 if (ferror(external) && errno == EINTR)
409 continue;
410 else
411 break;
413 if (line[strlen(line)-1] == '\n')
414 line[strlen(line)-1] = 0;
415 if (strlen(line) < 1)
416 continue;
417 if (line [0] == '.' && line[1] == PATH_SEP)
418 name = line + 2;
419 else
420 name = line;
421 status = handle_path (list, name, &st, next_free, &link_to_dir,
422 &stale_link);
423 if (status == 0)
424 continue;
425 if (status == -1)
426 break;
427 list->list [next_free].fnamelen = strlen (name);
428 list->list [next_free].fname = g_strdup (name);
429 file_mark (current_panel, next_free, 0);
430 list->list [next_free].f.link_to_dir = link_to_dir;
431 list->list [next_free].f.stale_link = stale_link;
432 list->list [next_free].f.dir_size_computed = 0;
433 list->list [next_free].st = st;
434 list->list[next_free].sort_key = NULL;
435 list->list[next_free].second_sort_key = NULL;
436 next_free++;
437 if (!(next_free & 32))
438 rotate_dash ();
441 current_panel->is_panelized = 1;
442 if (next_free){
443 current_panel->count = next_free;
444 if (list->list [0].fname [0] == PATH_SEP){
445 strcpy (current_panel->cwd, PATH_SEP_STR);
446 chdir (PATH_SEP_STR);
448 } else {
449 current_panel->count = set_zero_dir (list) ? 1 : 0;
451 if (pclose (external) < 0)
452 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
453 close_error_pipe (D_NORMAL, NULL);
454 try_to_select (current_panel, NULL);
455 panel_re_sort (current_panel);