Corrected a misdeleted row that created a crash on f1-f1 key pressing...
[midnight-commander.git] / src / panelize.c
blob62f41d3c6c84acc5a1df959c12e0e66a0b7deb39
1 /* External panelize
2 Copyright (C) 1995 The Free Software Foundation
4 Written by: 1995 Janne Kukonlehto
5 1995 Jakub Jelinek
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <config.h>
23 #ifdef HAVE_UNISTD_H
24 # include <unistd.h>
25 #endif
26 #include <string.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
32 /* Needed for the extern declarations of integer parameters */
33 #define DIR_H_INCLUDE_HANDLE_DIRENT
34 #include "global.h"
35 #include "tty.h"
36 #include "win.h"
37 #include "color.h"
38 #include "dlg.h"
39 #include "widget.h"
40 #include "dialog.h" /* For do_refresh() */
41 #include "wtools.h" /* For common_dialog_repaint() */
42 #include "setup.h" /* For profile_bname */
43 #include "profile.h" /* Load/save directories panelize */
44 #include "dir.h"
45 #include "panel.h" /* Needed for the externs */
46 #include "file.h"
47 #include "main.h"
48 #include "../vfs/vfs.h"
49 #include "panelize.h"
51 void do_external_panelize (char *command);
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 char *text;
72 char *tkname;
73 } panelize_but [BUTTONS] = {
74 { B_CANCEL, NORMAL_BUTTON, 0, 53, N_("&Cancel"), "c"},
75 { B_ADD, NORMAL_BUTTON, 0, 28, N_("&Add new"), "a"},
76 { B_REMOVE, NORMAL_BUTTON, 0, 16, N_("&Remove"), "r"},
77 { B_ENTER, DEFPUSH_BUTTON, 0, 0, N_("Pane&lize"),"l"},
80 static char *panelize_section = "Panelize";
82 /* Directory panelize */
83 static struct panelize {
84 char *command;
85 char *label;
86 struct panelize *next;
87 } *panelize = NULL;
89 static char* panelize_title = N_(" External panelize ");
91 static void
92 update_command (void)
94 if (l_panelize->pos != last_listitem) {
95 last_listitem = l_panelize->pos;
96 assign_text (pname,
97 ((struct panelize *) l_panelize->current->data)->command);
98 pname->point = 0;
99 update_input (pname, 1);
103 static int
104 panelize_callback (Dlg_head * h, int Par, int Msg)
106 switch (Msg) {
107 case DLG_DRAW:
108 common_dialog_repaint (h);
109 attrset (COLOR_NORMAL);
110 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
111 break;
113 case DLG_POST_KEY:
114 /* fall */
115 case DLG_INIT:
116 attrset (MENU_ENTRY_COLOR);
117 update_command ();
118 break;
120 return 0;
123 static int l_call (void *data)
125 return listbox_nothing;
128 static void init_panelize (void)
130 int i, panelize_cols = COLS - 6;
131 struct panelize *current = panelize;
133 #ifdef ENABLE_NLS
134 static int i18n_flag = 0;
135 static int maxlen = 0;
137 if (!i18n_flag)
139 i = sizeof(panelize_but) / sizeof(panelize_but[0]);
140 while (i--)
142 panelize_but [i].text = _(panelize_but [i].text);
143 maxlen += strlen (panelize_but [i].text) + 5;
145 maxlen += 10;
146 panelize_title = _(panelize_title);
148 i18n_flag = 1;
150 panelize_cols = max(panelize_cols, maxlen);
152 panelize_but [2].x = panelize_but [3].x
153 + strlen (panelize_but [3].text) + 7;
154 panelize_but [1].x = panelize_but [2].x
155 + strlen (panelize_but [2].text) + 5;
156 panelize_but [0].x = panelize_cols
157 - strlen (panelize_but[0].text) - 8 - BX;
159 #endif /* ENABLE_NLS */
161 last_listitem = 0;
163 do_refresh ();
165 panelize_dlg = create_dlg (0, 0, 22, panelize_cols, dialog_colors,
166 panelize_callback, "[External panelize]",
167 _("External panelize"), DLG_CENTER);
169 #define XTRACT(i) BY+panelize_but[i].y, BX+panelize_but[i].x, panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, 0, 0, panelize_but[i].tkname
171 for (i = 0; i < BUTTONS; i++)
172 add_widget(panelize_dlg, button_new (XTRACT (i)));
174 pname = input_new (UY+14, UX, INPUT_COLOR, panelize_dlg->cols-10, "", "in");
175 add_widget (panelize_dlg, pname);
177 add_widget (panelize_dlg, label_new (UY+13, UX, _("Command"), "label-command"));
179 /* get new listbox */
180 l_panelize = listbox_new (UY + 1, UX + 1, panelize_dlg->cols-12, 10, 0, l_call, "li");
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, _("Other command")));
194 static void panelize_done (void)
196 destroy_dlg (panelize_dlg);
197 repaint_screen ();
200 static void add2panelize (char *label, char *command)
202 struct panelize *current, *old;
204 old = NULL;
205 current = panelize;
206 while (current && strcmp (current->label, label) <= 0){
207 old = current;
208 current = current->next;
211 if (old == NULL){
212 panelize = g_new (struct panelize, 1);
213 panelize->label = label;
214 panelize->command = command;
215 panelize->next = current;
216 } else {
217 struct panelize *new;
218 new = g_new (struct panelize, 1);
219 new->label = label;
220 new->command = command;
221 old->next = new;
222 new->next = current;
226 void
227 add2panelize_cmd (void)
229 char *label;
231 if (pname->buffer && (*pname->buffer)) {
232 label = input_dialog (_(" Add to external panelize "),
233 _(" Enter command label: "),
234 "");
235 if (!label)
236 return;
237 if (!*label) {
238 g_free (label);
239 return;
242 add2panelize (label, g_strdup (pname->buffer));
246 static void remove_from_panelize (struct panelize *entry)
248 if (strcmp (entry->label, _("Other command")) != 0) {
249 if (entry == panelize) {
250 panelize = panelize->next;
251 } else {
252 struct panelize *current = panelize;
253 while (current && current->next != entry)
254 current = current->next;
255 if (current) {
256 current->next = entry->next;
260 g_free (entry->label);
261 g_free (entry->command);
262 g_free (entry);
266 void
267 external_panelize (void)
269 char *target = NULL;
271 if (!vfs_current_is_local ()){
272 message (1, _(" Oops... "),
273 _(" I can't run external panelize while logged on a non local directory "));
274 return;
277 init_panelize ();
279 /* display file info */
280 attrset (SELECTED_COLOR);
282 run_dlg (panelize_dlg);
284 switch (panelize_dlg->ret_value) {
285 case B_CANCEL:
286 break;
288 case B_ADD:
289 add2panelize_cmd ();
290 break;
292 case B_REMOVE:
293 remove_from_panelize (l_panelize->current->data);
294 break;
296 case B_ENTER:
297 target = pname->buffer;
298 if (target != NULL && *target) {
299 char *cmd = g_strdup (target);
300 destroy_dlg (panelize_dlg);
301 do_external_panelize (cmd);
302 g_free (cmd);
303 repaint_screen ();
304 return;
306 break;
309 panelize_done ();
312 void load_panelize (void)
314 void *profile_keys;
315 char *key, *value;
317 profile_keys = profile_init_iterator (panelize_section, profile_name);
319 add2panelize (g_strdup (_("Other command")), g_strdup (""));
321 if (!profile_keys){
322 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
323 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
324 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
325 return;
328 while (profile_keys){
329 profile_keys = profile_iterator_next (profile_keys, &key, &value);
330 add2panelize (g_strdup (key), g_strdup (value));
334 void save_panelize (void)
336 struct panelize *current = panelize;
338 profile_clean_section (panelize_section, profile_name);
339 for (;current; current = current->next){
340 if (strcmp (current->label, _("Other command")))
341 WritePrivateProfileString (panelize_section,
342 current->label,
343 current->command,
344 profile_name);
346 sync_profiles ();
349 void done_panelize (void)
351 struct panelize *current = panelize;
352 struct panelize *next;
354 for (; current; current = next){
355 next = current->next;
356 g_free (current->label);
357 g_free (current->command);
358 g_free (current);
362 void do_external_panelize (char *command)
364 int status, link_to_dir, stalled_link;
365 int next_free = 0;
366 struct stat buf;
367 dir_list *list = &cpanel->dir;
368 char line [MC_MAXPATHLEN];
369 char *name;
370 FILE *external;
372 open_error_pipe ();
373 external = popen (command, "r");
374 if (!external){
375 close_error_pipe (1, _("Cannot invoke command."));
376 return;
378 /* Clear the counters and the directory list */
379 panel_clean_dir (cpanel);
381 while (1) {
382 clearerr(external);
383 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
384 if (ferror(external) && errno == EINTR)
385 continue;
386 else
387 break;
389 if (line[strlen(line)-1] == '\n')
390 line[strlen(line)-1] = 0;
391 if (strlen(line) < 1)
392 continue;
393 if (line [0] == '.' && line[1] == PATH_SEP)
394 name = line + 2;
395 else
396 name = line;
397 status = handle_path (list, name, &buf, next_free, &link_to_dir,
398 &stalled_link);
399 if (status == 0)
400 continue;
401 if (status == -1)
402 break;
403 list->list [next_free].fnamelen = strlen (name);
404 list->list [next_free].fname = g_strdup (name);
405 file_mark (cpanel, next_free, 0);
406 list->list [next_free].f.link_to_dir = link_to_dir;
407 list->list [next_free].f.stalled_link = stalled_link;
408 list->list [next_free].f.dir_size_computed = 0;
409 list->list [next_free].buf = buf;
410 next_free++;
411 if (!(next_free & 32))
412 rotate_dash ();
415 cpanel->is_panelized = 1;
416 if (next_free){
417 cpanel->count = next_free;
418 if (list->list [0].fname [0] == PATH_SEP){
419 strcpy (cpanel->cwd, PATH_SEP_STR);
420 chdir (PATH_SEP_STR);
422 } else {
423 cpanel->count = set_zero_dir (list);
425 #ifndef SCO_FLAVOR
426 if (pclose (external) < 0)
427 #else /* SCO_FLAVOR */
428 if (WEXITSTATUS(pclose (external)) < 0)
429 #endif /* SCO_FLAVOR */
430 message (0, _("External panelize"), _("Pipe close failed"));
431 close_error_pipe (0, 0);
432 try_to_select (cpanel, NULL);
433 panel_re_sort (cpanel);
434 paint_panel (cpanel);