2 Copyright (C) 1995 The Free Software Foundation
4 Written by: 1995 Janne Kukonlehto
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <stdlib.h> /* For malloc() */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #ifdef HAVE_SYS_WAIT_H
34 # include <sys/wait.h>
42 #include "util.h" /* Needed for the externs */
47 #include "dialog.h" /* For do_refresh() */
48 #include "setup.h" /* For profile_bname */
49 #include "profile.h" /* Load/save directories panelize */
52 /* Needed for the extern declarations of integer parameters */
53 #define DIR_H_INCLUDE_HANDLE_DIRENT
55 #include "panel.h" /* Needed for the externs */
59 #include "../vfs/vfs.h"
61 void do_external_panelize (char *command
);
72 #define B_REMOVE B_USER + 1
74 static WListbox
*l_panelize
;
76 static Dlg_head
*panelize_dlg
;
80 static char *panelize_section
= "Panelize";
82 static int last_listitem
;
85 int ret_cmd
, flags
, y
, x
;
88 } panelize_but
[BUTTONS
] = {
89 { B_CANCEL
, NORMAL_BUTTON
, 0, 53, N_("&Cancel"), "c"},
90 { B_ADD
, NORMAL_BUTTON
, 0, 28, N_("&Add new"), "a"},
91 { B_REMOVE
, NORMAL_BUTTON
, 0, 16, N_("&Remove"), "r"},
92 { B_ENTER
, DEFPUSH_BUTTON
, 0, 0, N_("Pane&lize"),"l"},
95 /* Directory panelize */
96 static struct panelize
{
99 struct panelize
*next
;
102 static char* panelize_title
= N_(" External panelize ");
106 panelize_refresh (void)
108 attrset (COLOR_NORMAL
);
109 dlg_erase (panelize_dlg
);
111 draw_box (panelize_dlg
, 1, 2, panelize_dlg
->lines
-2, panelize_dlg
->cols
-4);
112 draw_box (panelize_dlg
, UY
, UX
, panelize_dlg
->lines
-10, panelize_dlg
->cols
-10);
114 attrset (COLOR_HOT_NORMAL
);
115 dlg_move (panelize_dlg
, 1, (panelize_dlg
->cols
- strlen(panelize_title
)) / 2);
116 addstr (panelize_title
);
123 if (l_panelize
->pos
!= last_listitem
) {
124 last_listitem
= l_panelize
->pos
;
126 ((struct panelize
*) l_panelize
->current
->data
)->command
);
128 update_input (pname
, 1);
133 panelize_callback (Dlg_head
* h
, int Par
, int Msg
)
145 attrset (MENU_ENTRY_COLOR
);
152 static int l_call (void *data
)
154 return listbox_nothing
;
157 static void init_panelize (void)
159 int i
, panelize_cols
= COLS
- 6;
160 struct panelize
*current
= panelize
;
163 static int i18n_flag
= 0;
164 static int maxlen
= 0;
168 i
= sizeof(panelize_but
) / sizeof(panelize_but
[0]);
171 panelize_but
[i
].text
= _(panelize_but
[i
].text
);
172 maxlen
+= strlen (panelize_but
[i
].text
) + 5;
175 panelize_title
= _(panelize_title
);
179 panelize_cols
= max(panelize_cols
, maxlen
);
181 panelize_but
[2].x
= panelize_but
[3].x
182 + strlen (panelize_but
[3].text
) + 7;
183 panelize_but
[1].x
= panelize_but
[2].x
184 + strlen (panelize_but
[2].text
) + 5;
185 panelize_but
[0].x
= panelize_cols
186 - strlen (panelize_but
[0].text
) - 8 - BX
;
188 #endif /* ENABLE_NLS */
194 panelize_dlg
= create_dlg (0, 0, 22, panelize_cols
, dialog_colors
,
195 panelize_callback
, "[External panelize]", "panelize",
196 DLG_CENTER
|DLG_GRID
);
197 x_set_dialog_title (panelize_dlg
, _("External panelize"));
199 #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
201 for (i
= 0; i
< BUTTONS
; i
++)
202 add_widgetl (panelize_dlg
, button_new (XTRACT (i
)), (i
== BUTTONS
- 1) ?
203 XV_WLAY_CENTERROW
: XV_WLAY_RIGHTOF
);
205 pname
= input_new (UY
+14, UX
, INPUT_COLOR
, panelize_dlg
->cols
-10, "", "in");
206 add_widgetl (panelize_dlg
, pname
, XV_WLAY_RIGHTOF
);
208 add_widgetl (panelize_dlg
, label_new (UY
+13, UX
, _("Command"), "label-command"), XV_WLAY_NEXTROW
);
210 /* get new listbox */
211 l_panelize
= listbox_new (UY
+ 1, UX
+ 1, panelize_dlg
->cols
-12, 10, 0, l_call
, "li");
214 listbox_add_item (l_panelize
, 0, 0, current
->label
, current
);
215 current
= current
->next
;
218 /* add listbox to the dialogs */
219 add_widgetl (panelize_dlg
, l_panelize
, XV_WLAY_EXTENDWIDTH
);
221 listbox_select_entry (l_panelize
,
222 listbox_search_text (l_panelize
, _("Other command")));
225 static void panelize_done (void)
227 destroy_dlg (panelize_dlg
);
231 static void add2panelize (char *label
, char *command
)
233 struct panelize
*current
, *old
;
237 while (current
&& strcmp (current
->label
, label
) <= 0){
239 current
= current
->next
;
243 panelize
= malloc (sizeof (struct panelize
));
244 panelize
->label
= label
;
245 panelize
->command
= command
;
246 panelize
->next
= current
;
248 struct panelize
*new;
249 new = malloc (sizeof (struct panelize
));
251 new->command
= command
;
257 void add2panelize_cmd (void)
261 if (pname
->buffer
&& (*pname
->buffer
)) {
262 label
= input_dialog (_(" Add to external panelize "),
263 _(" Enter command label: "),
272 add2panelize (label
, strdup(pname
->buffer
));
276 static void remove_from_panelize (struct panelize
*entry
)
278 if (strcmp (entry
->label
, _("Other command")) != 0) {
279 if (entry
== panelize
) {
280 panelize
= panelize
->next
;
282 struct panelize
*current
= panelize
;
283 while (current
&& current
->next
!= entry
)
284 current
= current
->next
;
286 current
->next
= entry
->next
;
291 free (entry
->command
);
296 void external_panelize (void)
300 if (!vfs_current_is_local ()){
301 message (1, _(" Oops... "),
302 _(" I can't run external panelize while logged on a non local directory "));
308 /* display file info */
309 attrset (SELECTED_COLOR
);
311 run_dlg (panelize_dlg
);
313 switch (panelize_dlg
->ret_value
) {
322 remove_from_panelize (l_panelize
->current
->data
);
326 target
= pname
->buffer
;
327 if (target
!= NULL
&& *target
) {
328 char *cmd
= strdup (target
);
329 destroy_dlg (panelize_dlg
);
330 do_external_panelize (cmd
);
341 void load_panelize (void)
346 profile_keys
= profile_init_iterator (panelize_section
, profile_name
);
348 add2panelize (strdup (_("Other command")), strdup (""));
351 add2panelize (strdup (_("Find rejects after patching")), strdup ("find . -name \\*.rej -print"));
352 add2panelize (strdup (_("Find *.orig after patching")), strdup ("find . -name \\*.orig -print"));
353 add2panelize (strdup (_("Find SUID and SGID programs")), strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
357 while (profile_keys
){
358 profile_keys
= profile_iterator_next (profile_keys
, &key
, &value
);
359 add2panelize (strdup (key
), strdup (value
));
363 void save_panelize (void)
365 struct panelize
*current
= panelize
;
367 profile_clean_section (panelize_section
, profile_name
);
368 for (;current
; current
= current
->next
){
369 if (strcmp (current
->label
, _("Other command")))
370 WritePrivateProfileString (panelize_section
,
378 void done_panelize (void)
380 struct panelize
*current
= panelize
;
381 struct panelize
*next
;
383 for (; current
; current
= next
){
384 next
= current
->next
;
385 free (current
->label
);
386 free (current
->command
);
391 void do_external_panelize (char *command
)
393 int status
, link_to_dir
, stalled_link
;
396 dir_list
*list
= &cpanel
->dir
;
397 char line
[MC_MAXPATHLEN
];
402 external
= popen (command
, "r");
404 close_error_pipe (1, _("Cannot invoke command."));
407 clean_dir (list
, cpanel
->count
);
409 /* Clear the counters */
410 cpanel
->total
= cpanel
->dirs_marked
= cpanel
->marked
= 0;
411 cpanel
->has_dir_sizes
= 0;
414 if (fgets (line
, MC_MAXPATHLEN
, external
) == NULL
)
415 if (ferror(external
) && errno
== EINTR
)
419 if (line
[strlen(line
)-1] == '\n')
420 line
[strlen(line
)-1] = 0;
421 if (strlen(line
) < 1)
423 if (line
[0] == '.' && line
[1] == PATH_SEP
)
427 status
= handle_path (list
, name
, &buf
, next_free
, &link_to_dir
,
433 list
->list
[next_free
].fnamelen
= strlen (name
);
434 list
->list
[next_free
].fname
= strdup (name
);
435 list
->list
[next_free
].cache
= NULL
;
436 file_mark (cpanel
, next_free
, 0);
437 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
438 list
->list
[next_free
].f
.stalled_link
= stalled_link
;
439 list
->list
[next_free
].buf
= buf
;
441 if (!(next_free
& 32))
445 cpanel
->count
= next_free
;
446 cpanel
->is_panelized
= 1;
447 if (list
->list
[0].fname
[0] == PATH_SEP
){
448 strcpy (cpanel
->cwd
, PATH_SEP_STR
);
449 chdir (PATH_SEP_STR
);
452 cpanel
->count
= set_zero_dir (list
);
455 if (pclose (external
) < 0)
456 #else /* SCO_FLAVOR */
457 if (WEXITSTATUS(pclose (external
)) < 0)
458 #endif /* SCO_FLAVOR */
459 message (0, _("External panelize"), _("Pipe close failed"));
460 close_error_pipe (0, 0);
461 try_to_select (cpanel
, NULL
);
462 paint_panel (cpanel
);