mandriva: Srpm support for extfs
[midnight-commander.git] / src / panelize.c
blob5bbf643f76128ba25f3994cd80670ba88d2dafba
1 /* External panelize
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 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 #include <config.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include "global.h"
33 #include "tty.h" /* attrset() */
34 #include "win.h"
35 #include "color.h"
36 #include "dialog.h"
37 #include "widget.h"
38 #include "wtools.h" /* For common_dialog_repaint() */
39 #include "setup.h" /* For profile_bname */
40 #include "profile.h" /* Load/save directories panelize */
41 #include "dir.h"
42 #include "panel.h" /* current_panel */
43 #include "main.h" /* repaint_screen */
44 #include "panelize.h"
45 #include "history.h"
47 #define UX 5
48 #define UY 2
50 #define BX 5
51 #define BY 18
53 #define BUTTONS 4
54 #define LABELS 3
55 #define B_ADD B_USER
56 #define B_REMOVE (B_USER + 1)
58 static WListbox *l_panelize;
59 static Dlg_head *panelize_dlg;
60 static int last_listitem;
61 static WInput *pname;
63 static struct {
64 int ret_cmd, flags, y, x;
65 const char *text;
66 } panelize_but [BUTTONS] = {
67 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel") },
68 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new") },
69 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove") },
70 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize") },
73 static const char *panelize_section = "Panelize";
74 static void do_external_panelize (char *command);
76 /* Directory panelize */
77 static struct panelize {
78 char *command;
79 char *label;
80 struct panelize *next;
81 } *panelize = NULL;
83 static void
84 update_command (void)
86 if (l_panelize->pos != last_listitem) {
87 last_listitem = l_panelize->pos;
88 assign_text (pname,
89 ((struct panelize *) l_panelize->current->data)->command);
90 pname->point = 0;
91 update_input (pname, 1);
95 static cb_ret_t
96 panelize_callback (Dlg_head *h, dlg_msg_t msg, int parm)
98 switch (msg) {
99 case DLG_DRAW:
100 common_dialog_repaint (h);
101 attrset (COLOR_NORMAL);
102 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
103 return MSG_HANDLED;
105 case DLG_POST_KEY:
106 /* fall */
107 case DLG_INIT:
108 attrset (MENU_ENTRY_COLOR);
109 update_command ();
110 return MSG_HANDLED;
112 default:
113 return default_dlg_callback (h, msg, parm);
117 static void
118 init_panelize (void)
120 int i, panelize_cols = COLS - 6;
121 struct panelize *current = panelize;
123 #ifdef ENABLE_NLS
124 static int i18n_flag = 0;
125 static int maxlen = 0;
127 if (!i18n_flag) {
128 i = sizeof (panelize_but) / sizeof (panelize_but[0]);
129 while (i--) {
130 panelize_but[i].text = _(panelize_but[i].text);
131 maxlen += strlen (panelize_but[i].text) + 5;
133 maxlen += 10;
135 i18n_flag = 1;
137 panelize_cols = max (panelize_cols, maxlen);
139 panelize_but[2].x =
140 panelize_but[3].x + strlen (panelize_but[3].text) + 7;
141 panelize_but[1].x =
142 panelize_but[2].x + strlen (panelize_but[2].text) + 5;
143 panelize_but[0].x =
144 panelize_cols - strlen (panelize_but[0].text) - 8 - BX;
146 #endif /* ENABLE_NLS */
148 last_listitem = 0;
150 do_refresh ();
152 panelize_dlg =
153 create_dlg (0, 0, 22, panelize_cols, dialog_colors,
154 panelize_callback, "[External panelize]",
155 _("External panelize"), DLG_CENTER | DLG_REVERSE);
157 for (i = 0; i < BUTTONS; i++)
158 add_widget (panelize_dlg,
159 button_new (BY + panelize_but[i].y,
160 BX + panelize_but[i].x,
161 panelize_but[i].ret_cmd,
162 panelize_but[i].flags,
163 panelize_but[i].text, 0));
165 pname =
166 input_new (UY + 14, UX, INPUT_COLOR, panelize_dlg->cols - 10, "",
167 "in", INPUT_COMPLETE_DEFAULT);
168 add_widget (panelize_dlg, pname);
170 add_widget (panelize_dlg, label_new (UY + 13, UX, _("Command")));
172 /* get new listbox */
173 l_panelize =
174 listbox_new (UY + 1, UX + 1, panelize_dlg->cols - 12, 10, NULL);
176 while (current) {
177 listbox_add_item (l_panelize, 0, 0, current->label, current);
178 current = current->next;
181 /* add listbox to the dialogs */
182 add_widget (panelize_dlg, l_panelize);
184 listbox_select_entry (l_panelize,
185 listbox_search_text (l_panelize,
186 _("Other command")));
189 static void panelize_done (void)
191 destroy_dlg (panelize_dlg);
192 repaint_screen ();
195 static void add2panelize (char *label, char *command)
197 struct panelize *current, *old;
199 old = NULL;
200 current = panelize;
201 while (current && strcmp (current->label, label) <= 0){
202 old = current;
203 current = current->next;
206 if (old == NULL){
207 panelize = g_new (struct panelize, 1);
208 panelize->label = label;
209 panelize->command = command;
210 panelize->next = current;
211 } else {
212 struct panelize *new;
213 new = g_new (struct panelize, 1);
214 new->label = label;
215 new->command = command;
216 old->next = new;
217 new->next = current;
221 static void
222 add2panelize_cmd (void)
224 char *label;
226 if (pname->buffer && (*pname->buffer)) {
227 label = input_dialog (_(" Add to external panelize "),
228 _(" Enter command label: "),
229 MC_HISTORY_FM_PANELIZE_ADD,
230 "");
231 if (!label)
232 return;
233 if (!*label) {
234 g_free (label);
235 return;
238 add2panelize (label, g_strdup (pname->buffer));
242 static void remove_from_panelize (struct panelize *entry)
244 if (strcmp (entry->label, _("Other command")) != 0) {
245 if (entry == panelize) {
246 panelize = panelize->next;
247 } else {
248 struct panelize *current = panelize;
249 while (current && current->next != entry)
250 current = current->next;
251 if (current) {
252 current->next = entry->next;
256 g_free (entry->label);
257 g_free (entry->command);
258 g_free (entry);
262 void
263 external_panelize (void)
265 char *target = NULL;
267 if (!vfs_current_is_local ()){
268 message (D_ERROR, MSG_ERROR,
269 _(" Cannot run external panelize in a non-local directory "));
270 return;
273 init_panelize ();
275 /* display file info */
276 attrset (SELECTED_COLOR);
278 run_dlg (panelize_dlg);
280 switch (panelize_dlg->ret_value) {
281 case B_CANCEL:
282 break;
284 case B_ADD:
285 add2panelize_cmd ();
286 break;
288 case B_REMOVE:
289 remove_from_panelize (l_panelize->current->data);
290 break;
292 case B_ENTER:
293 target = pname->buffer;
294 if (target != NULL && *target) {
295 char *cmd = g_strdup (target);
296 destroy_dlg (panelize_dlg);
297 do_external_panelize (cmd);
298 g_free (cmd);
299 repaint_screen ();
300 return;
302 break;
305 panelize_done ();
308 void load_panelize (void)
310 void *profile_keys;
311 char *key, *value;
313 profile_keys = profile_init_iterator (panelize_section, profile_name);
315 add2panelize (g_strdup (_("Other command")), g_strdup (""));
317 if (!profile_keys){
318 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
319 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
320 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
321 return;
324 while (profile_keys){
325 profile_keys = profile_iterator_next (profile_keys, &key, &value);
326 add2panelize (g_strdup (key), g_strdup (value));
330 void save_panelize (void)
332 struct panelize *current = panelize;
334 profile_clean_section (panelize_section, profile_name);
335 for (;current; current = current->next){
336 if (strcmp (current->label, _("Other command")))
337 WritePrivateProfileString (panelize_section,
338 current->label,
339 current->command,
340 profile_name);
342 sync_profiles ();
345 void done_panelize (void)
347 struct panelize *current = panelize;
348 struct panelize *next;
350 for (; current; current = next){
351 next = current->next;
352 g_free (current->label);
353 g_free (current->command);
354 g_free (current);
358 static void do_external_panelize (char *command)
360 int status, link_to_dir, stale_link;
361 int next_free = 0;
362 struct stat st;
363 dir_list *list = &current_panel->dir;
364 char line [MC_MAXPATHLEN];
365 char *name;
366 FILE *external;
368 open_error_pipe ();
369 external = popen (command, "r");
370 if (!external){
371 close_error_pipe (D_ERROR, _("Cannot invoke command."));
372 return;
374 /* Clear the counters and the directory list */
375 panel_clean_dir (current_panel);
377 while (1) {
378 clearerr(external);
379 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
380 if (ferror(external) && errno == EINTR)
381 continue;
382 else
383 break;
385 if (line[strlen(line)-1] == '\n')
386 line[strlen(line)-1] = 0;
387 if (strlen(line) < 1)
388 continue;
389 if (line [0] == '.' && line[1] == PATH_SEP)
390 name = line + 2;
391 else
392 name = line;
393 status = handle_path (list, name, &st, next_free, &link_to_dir,
394 &stale_link);
395 if (status == 0)
396 continue;
397 if (status == -1)
398 break;
399 list->list [next_free].fnamelen = strlen (name);
400 list->list [next_free].fname = g_strdup (name);
401 file_mark (current_panel, next_free, 0);
402 list->list [next_free].f.link_to_dir = link_to_dir;
403 list->list [next_free].f.stale_link = stale_link;
404 list->list [next_free].f.dir_size_computed = 0;
405 list->list [next_free].st = st;
406 next_free++;
407 if (!(next_free & 32))
408 rotate_dash ();
411 current_panel->is_panelized = 1;
412 if (next_free){
413 current_panel->count = next_free;
414 if (list->list [0].fname [0] == PATH_SEP){
415 strcpy (current_panel->cwd, PATH_SEP_STR);
416 chdir (PATH_SEP_STR);
418 } else {
419 current_panel->count = set_zero_dir (list);
421 if (pclose (external) < 0)
422 message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
423 close_error_pipe (D_NORMAL, NULL);
424 try_to_select (current_panel, NULL);
425 panel_re_sort (current_panel);