Merge branch '1790_mc_crash'
[midnight-commander.git] / src / panelize.c
blobe2287fb3bbe5ff464c59c70ea1e4f4cfda78b60c
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 "global.h"
38 #include "../src/skin/skin.h"
40 #include "dialog.h"
41 #include "widget.h"
42 #include "wtools.h" /* For common_dialog_repaint() */
43 #include "setup.h" /* For profile_bname */
44 #include "../src/mcconfig/mcconfig.h" /* Load/save directories panelize */
45 #include "dir.h"
46 #include "panel.h" /* current_panel */
47 #include "layout.h" /* repaint_screen() */
48 #include "main.h"
49 #include "panelize.h"
50 #include "history.h"
51 #include "strutil.h"
53 #define UX 5
54 #define UY 2
56 #define BX 5
57 #define BY 18
59 #define BUTTONS 4
60 #define LABELS 3
61 #define B_ADD B_USER
62 #define B_REMOVE (B_USER + 1)
64 static WListbox *l_panelize;
65 static Dlg_head *panelize_dlg;
66 static int last_listitem;
67 static WInput *pname;
69 static struct {
70 int ret_cmd, flags, y, x;
71 const char *text;
72 } panelize_but [BUTTONS] = {
73 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
74 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
75 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
76 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") },
79 static const char *panelize_section = "Panelize";
80 static void do_external_panelize (char *command);
82 /* Directory panelize */
83 static struct panelize {
84 char *command;
85 char *label;
86 struct panelize *next;
87 } *panelize = NULL;
89 static void
90 update_command (void)
92 if (l_panelize->pos != last_listitem) {
93 last_listitem = l_panelize->pos;
94 assign_text (pname,
95 ((struct panelize *) l_panelize->current->data)->command);
96 pname->point = 0;
97 update_input (pname, 1);
101 static cb_ret_t
102 panelize_callback (Dlg_head *h, dlg_msg_t msg, int parm)
104 switch (msg) {
105 case DLG_DRAW:
106 common_dialog_repaint (h);
107 tty_setcolor (COLOR_NORMAL);
108 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
109 return MSG_HANDLED;
111 case DLG_POST_KEY:
112 /* fall */
113 case DLG_INIT:
114 tty_setcolor (MENU_ENTRY_COLOR);
115 update_command ();
116 return MSG_HANDLED;
118 default:
119 return default_dlg_callback (h, msg, parm);
123 static void
124 init_panelize (void)
126 int i, panelize_cols = COLS - 6;
127 struct panelize *current = panelize;
129 #ifdef ENABLE_NLS
130 static int i18n_flag = 0;
131 static int maxlen = 0;
133 if (!i18n_flag) {
134 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
135 while (i--) {
136 panelize_but[i].text = _(panelize_but[i].text);
137 maxlen += str_term_width1 (panelize_but[i].text) + 5;
139 maxlen += 10;
141 i18n_flag = 1;
143 panelize_cols = max (panelize_cols, maxlen);
145 panelize_but[2].x =
146 panelize_but[3].x + str_term_width1 (panelize_but[3].text) + 7;
147 panelize_but[1].x =
148 panelize_but[2].x + str_term_width1 (panelize_but[2].text) + 5;
149 panelize_but[0].x =
150 panelize_cols - str_term_width1 (panelize_but[0].text) - 8 - BX;
152 #endif /* ENABLE_NLS */
154 last_listitem = 0;
156 do_refresh ();
158 panelize_dlg =
159 create_dlg (0, 0, 22, panelize_cols, dialog_colors,
160 panelize_callback, "[External panelize]",
161 _("External panelize"), DLG_CENTER | DLG_REVERSE);
163 for (i = 0; i < BUTTONS; i++)
164 add_widget (panelize_dlg,
165 button_new (BY + panelize_but[i].y,
166 BX + panelize_but[i].x,
167 panelize_but[i].ret_cmd,
168 panelize_but[i].flags,
169 panelize_but[i].text, 0));
171 pname =
172 input_new (UY + 14, UX, INPUT_COLOR, panelize_dlg->cols - 10, "",
173 "in", INPUT_COMPLETE_DEFAULT);
174 add_widget (panelize_dlg, pname);
176 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
178 /* get new listbox */
179 l_panelize =
180 listbox_new (UY + 1, UX + 1, 10, panelize_dlg->cols - 12, NULL);
182 while (current) {
183 listbox_add_item (l_panelize, 0, 0, current->label, current);
184 current = current->next;
187 /* add listbox to the dialogs */
188 add_widget (panelize_dlg, l_panelize);
190 listbox_select_entry (l_panelize,
191 listbox_search_text (l_panelize,
192 _("Other command")));
195 static void panelize_done (void)
197 destroy_dlg (panelize_dlg);
198 repaint_screen ();
201 static void add2panelize (char *label, char *command)
203 struct panelize *current, *old;
205 old = NULL;
206 current = panelize;
207 while (current && strcmp (current->label, label) <= 0){
208 old = current;
209 current = current->next;
212 if (old == NULL){
213 panelize = g_new (struct panelize, 1);
214 panelize->label = label;
215 panelize->command = command;
216 panelize->next = current;
217 } else {
218 struct panelize *new;
219 new = g_new (struct panelize, 1);
220 new->label = label;
221 new->command = command;
222 old->next = new;
223 new->next = current;
227 static void
228 add2panelize_cmd (void)
230 char *label;
232 if (pname->buffer && (*pname->buffer)) {
233 label = input_dialog (_(" Add to external panelize "),
234 _(" Enter command label: "),
235 MC_HISTORY_FM_PANELIZE_ADD,
236 "");
237 if (!label)
238 return;
239 if (!*label) {
240 g_free (label);
241 return;
244 add2panelize (label, g_strdup (pname->buffer));
248 static void remove_from_panelize (struct panelize *entry)
250 if (strcmp (entry->label, _("Other command")) != 0) {
251 if (entry == panelize) {
252 panelize = panelize->next;
253 } else {
254 struct panelize *current = panelize;
255 while (current && current->next != entry)
256 current = current->next;
257 if (current) {
258 current->next = entry->next;
262 g_free (entry->label);
263 g_free (entry->command);
264 g_free (entry);
268 void
269 external_panelize (void)
271 char *target = NULL;
273 if (!vfs_current_is_local ()){
274 message (D_ERROR, MSG_ERROR,
275 _(" Cannot run external panelize in a non-local directory "));
276 return;
279 init_panelize ();
281 /* display file info */
282 tty_setcolor (SELECTED_COLOR);
284 run_dlg (panelize_dlg);
286 switch (panelize_dlg->ret_value) {
287 case B_CANCEL:
288 break;
290 case B_ADD:
291 add2panelize_cmd ();
292 break;
294 case B_REMOVE:
295 remove_from_panelize (l_panelize->current->data);
296 break;
298 case B_ENTER:
299 target = pname->buffer;
300 if (target != NULL && *target) {
301 char *cmd = g_strdup (target);
302 destroy_dlg (panelize_dlg);
303 do_external_panelize (cmd);
304 g_free (cmd);
305 repaint_screen ();
306 return;
308 break;
311 panelize_done ();
314 void load_panelize (void)
316 gchar **profile_keys, **keys;
317 gsize len;
318 GIConv conv;
319 GString *buffer;
321 conv = str_crt_conv_from ("UTF-8");
323 profile_keys = keys = mc_config_get_keys (mc_main_config, panelize_section,&len);
325 add2panelize (g_strdup (_("Other command")), g_strdup (""));
327 if (!profile_keys || *profile_keys == NULL){
328 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
329 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
330 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
331 return;
334 while (*profile_keys){
336 if (utf8_display || conv == INVALID_CONV){
337 buffer = g_string_new (*profile_keys);
338 } else {
339 buffer = g_string_new ("");
340 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
342 g_string_free(buffer, TRUE);
343 buffer = g_string_new (*profile_keys);
347 add2panelize (
348 g_string_free(buffer, FALSE),
349 mc_config_get_string(mc_main_config,panelize_section,*profile_keys,"")
351 profile_keys++;
353 g_strfreev(keys);
354 str_close_conv (conv);
357 void save_panelize (void)
359 struct panelize *current = panelize;
361 mc_config_del_group (mc_main_config, panelize_section);
362 for (;current; current = current->next){
363 if (strcmp (current->label, _("Other command")))
364 mc_config_set_string(mc_main_config,
365 panelize_section,
366 current->label,
367 current->command);
369 mc_config_save_file (mc_main_config, NULL);
372 void done_panelize (void)
374 struct panelize *current = panelize;
375 struct panelize *next;
377 for (; current; current = next){
378 next = current->next;
379 g_free (current->label);
380 g_free (current->command);
381 g_free (current);
385 static void do_external_panelize (char *command)
387 int status, link_to_dir, stale_link;
388 int next_free = 0;
389 struct stat st;
390 dir_list *list = &current_panel->dir;
391 char line [MC_MAXPATHLEN];
392 char *name;
393 FILE *external;
395 open_error_pipe ();
396 external = popen (command, "r");
397 if (!external){
398 close_error_pipe (D_ERROR, _("Cannot invoke command."));
399 return;
401 /* Clear the counters and the directory list */
402 panel_clean_dir (current_panel);
404 while (1) {
405 clearerr(external);
406 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
407 if (ferror(external) && errno == EINTR)
408 continue;
409 else
410 break;
412 if (line[strlen(line)-1] == '\n')
413 line[strlen(line)-1] = 0;
414 if (strlen(line) < 1)
415 continue;
416 if (line [0] == '.' && line[1] == PATH_SEP)
417 name = line + 2;
418 else
419 name = line;
420 status = handle_path (list, name, &st, next_free, &link_to_dir,
421 &stale_link);
422 if (status == 0)
423 continue;
424 if (status == -1)
425 break;
426 list->list [next_free].fnamelen = strlen (name);
427 list->list [next_free].fname = g_strdup (name);
428 file_mark (current_panel, next_free, 0);
429 list->list [next_free].f.link_to_dir = link_to_dir;
430 list->list [next_free].f.stale_link = stale_link;
431 list->list [next_free].f.dir_size_computed = 0;
432 list->list [next_free].st = st;
433 list->list[next_free].sort_key = NULL;
434 list->list[next_free].second_sort_key = NULL;
435 next_free++;
436 if (!(next_free & 32))
437 rotate_dash ();
440 current_panel->is_panelized = 1;
441 if (next_free){
442 current_panel->count = next_free;
443 if (list->list [0].fname [0] == PATH_SEP){
444 strcpy (current_panel->cwd, PATH_SEP_STR);
445 chdir (PATH_SEP_STR);
447 } else {
448 current_panel->count = set_zero_dir (list) ? 1 : 0;
450 if (pclose (external) < 0)
451 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
452 close_error_pipe (D_NORMAL, NULL);
453 try_to_select (current_panel, NULL);
454 panel_re_sort (current_panel);