small typo
[midnight-commander.git] / src / panelize.c
bloba03d45b5d6542f4f4752eed1adb86a4b953e8d84
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 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 int
102 panelize_callback (Dlg_head * h, int Par, int Msg)
104 switch (Msg) {
105 case DLG_DRAW:
106 common_dialog_repaint (h);
107 attrset (COLOR_NORMAL);
108 draw_box (h, UY, UX, h->lines - 10, h->cols - 10);
109 break;
111 case DLG_POST_KEY:
112 /* fall */
113 case DLG_INIT:
114 attrset (MENU_ENTRY_COLOR);
115 update_command ();
116 break;
118 return 0;
121 static int l_call (void *data)
123 return listbox_nothing;
126 static void init_panelize (void)
128 int i, panelize_cols = COLS - 6;
129 struct panelize *current = panelize;
131 #ifdef ENABLE_NLS
132 static int i18n_flag = 0;
133 static int maxlen = 0;
135 if (!i18n_flag)
137 i = sizeof(panelize_but) / sizeof(panelize_but[0]);
138 while (i--)
140 panelize_but [i].text = _(panelize_but [i].text);
141 maxlen += strlen (panelize_but [i].text) + 5;
143 maxlen += 10;
145 i18n_flag = 1;
147 panelize_cols = max(panelize_cols, maxlen);
149 panelize_but [2].x = panelize_but [3].x
150 + strlen (panelize_but [3].text) + 7;
151 panelize_but [1].x = panelize_but [2].x
152 + strlen (panelize_but [2].text) + 5;
153 panelize_but [0].x = panelize_cols
154 - strlen (panelize_but[0].text) - 8 - BX;
156 #endif /* ENABLE_NLS */
158 last_listitem = 0;
160 do_refresh ();
162 panelize_dlg = create_dlg (0, 0, 22, panelize_cols, dialog_colors,
163 panelize_callback, "[External panelize]",
164 _("External panelize"), DLG_CENTER);
166 #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
168 for (i = 0; i < BUTTONS; i++)
169 add_widget(panelize_dlg, button_new (XTRACT (i)));
171 pname = input_new (UY+14, UX, INPUT_COLOR, panelize_dlg->cols-10, "", "in");
172 add_widget (panelize_dlg, pname);
174 add_widget (panelize_dlg, label_new (UY+13, UX, _("Command"), "label-command"));
176 /* get new listbox */
177 l_panelize = listbox_new (UY + 1, UX + 1, panelize_dlg->cols-12, 10, 0, l_call, "li");
179 while (current){
180 listbox_add_item (l_panelize, 0, 0, current->label, current);
181 current = current->next;
184 /* add listbox to the dialogs */
185 add_widget (panelize_dlg, l_panelize);
187 listbox_select_entry (l_panelize,
188 listbox_search_text (l_panelize, _("Other command")));
191 static void panelize_done (void)
193 destroy_dlg (panelize_dlg);
194 repaint_screen ();
197 static void add2panelize (char *label, char *command)
199 struct panelize *current, *old;
201 old = NULL;
202 current = panelize;
203 while (current && strcmp (current->label, label) <= 0){
204 old = current;
205 current = current->next;
208 if (old == NULL){
209 panelize = g_new (struct panelize, 1);
210 panelize->label = label;
211 panelize->command = command;
212 panelize->next = current;
213 } else {
214 struct panelize *new;
215 new = g_new (struct panelize, 1);
216 new->label = label;
217 new->command = command;
218 old->next = new;
219 new->next = current;
223 void
224 add2panelize_cmd (void)
226 char *label;
228 if (pname->buffer && (*pname->buffer)) {
229 label = input_dialog (_(" Add to external panelize "),
230 _(" Enter command label: "),
231 "");
232 if (!label)
233 return;
234 if (!*label) {
235 g_free (label);
236 return;
239 add2panelize (label, g_strdup (pname->buffer));
243 static void remove_from_panelize (struct panelize *entry)
245 if (strcmp (entry->label, _("Other command")) != 0) {
246 if (entry == panelize) {
247 panelize = panelize->next;
248 } else {
249 struct panelize *current = panelize;
250 while (current && current->next != entry)
251 current = current->next;
252 if (current) {
253 current->next = entry->next;
257 g_free (entry->label);
258 g_free (entry->command);
259 g_free (entry);
263 void
264 external_panelize (void)
266 char *target = NULL;
268 if (!vfs_current_is_local ()){
269 message (1, _(" Oops... "),
270 _(" I can't run external panelize while logged on a non local directory "));
271 return;
274 init_panelize ();
276 /* display file info */
277 attrset (SELECTED_COLOR);
279 run_dlg (panelize_dlg);
281 switch (panelize_dlg->ret_value) {
282 case B_CANCEL:
283 break;
285 case B_ADD:
286 add2panelize_cmd ();
287 break;
289 case B_REMOVE:
290 remove_from_panelize (l_panelize->current->data);
291 break;
293 case B_ENTER:
294 target = pname->buffer;
295 if (target != NULL && *target) {
296 char *cmd = g_strdup (target);
297 destroy_dlg (panelize_dlg);
298 do_external_panelize (cmd);
299 g_free (cmd);
300 repaint_screen ();
301 return;
303 break;
306 panelize_done ();
309 void load_panelize (void)
311 void *profile_keys;
312 char *key, *value;
314 profile_keys = profile_init_iterator (panelize_section, profile_name);
316 add2panelize (g_strdup (_("Other command")), g_strdup (""));
318 if (!profile_keys){
319 add2panelize (g_strdup (_("Find rejects after patching")), g_strdup ("find . -name \\*.rej -print"));
320 add2panelize (g_strdup (_("Find *.orig after patching")), g_strdup ("find . -name \\*.orig -print"));
321 add2panelize (g_strdup (_("Find SUID and SGID programs")), g_strdup ("find . \\( \\( -perm -04000 -a -perm +011 \\) -o \\( -perm -02000 -a -perm +01 \\) \\) -print"));
322 return;
325 while (profile_keys){
326 profile_keys = profile_iterator_next (profile_keys, &key, &value);
327 add2panelize (g_strdup (key), g_strdup (value));
331 void save_panelize (void)
333 struct panelize *current = panelize;
335 profile_clean_section (panelize_section, profile_name);
336 for (;current; current = current->next){
337 if (strcmp (current->label, _("Other command")))
338 WritePrivateProfileString (panelize_section,
339 current->label,
340 current->command,
341 profile_name);
343 sync_profiles ();
346 void done_panelize (void)
348 struct panelize *current = panelize;
349 struct panelize *next;
351 for (; current; current = next){
352 next = current->next;
353 g_free (current->label);
354 g_free (current->command);
355 g_free (current);
359 void do_external_panelize (char *command)
361 int status, link_to_dir, stalled_link;
362 int next_free = 0;
363 struct stat buf;
364 dir_list *list = &cpanel->dir;
365 char line [MC_MAXPATHLEN];
366 char *name;
367 FILE *external;
369 open_error_pipe ();
370 external = popen (command, "r");
371 if (!external){
372 close_error_pipe (1, _("Cannot invoke command."));
373 return;
375 /* Clear the counters and the directory list */
376 panel_clean_dir (cpanel);
378 while (1) {
379 clearerr(external);
380 if (fgets (line, MC_MAXPATHLEN, external) == NULL) {
381 if (ferror(external) && errno == EINTR)
382 continue;
383 else
384 break;
386 if (line[strlen(line)-1] == '\n')
387 line[strlen(line)-1] = 0;
388 if (strlen(line) < 1)
389 continue;
390 if (line [0] == '.' && line[1] == PATH_SEP)
391 name = line + 2;
392 else
393 name = line;
394 status = handle_path (list, name, &buf, next_free, &link_to_dir,
395 &stalled_link);
396 if (status == 0)
397 continue;
398 if (status == -1)
399 break;
400 list->list [next_free].fnamelen = strlen (name);
401 list->list [next_free].fname = g_strdup (name);
402 file_mark (cpanel, next_free, 0);
403 list->list [next_free].f.link_to_dir = link_to_dir;
404 list->list [next_free].f.stalled_link = stalled_link;
405 list->list [next_free].f.dir_size_computed = 0;
406 list->list [next_free].buf = buf;
407 next_free++;
408 if (!(next_free & 32))
409 rotate_dash ();
412 cpanel->is_panelized = 1;
413 if (next_free){
414 cpanel->count = next_free;
415 if (list->list [0].fname [0] == PATH_SEP){
416 strcpy (cpanel->cwd, PATH_SEP_STR);
417 chdir (PATH_SEP_STR);
419 } else {
420 cpanel->count = set_zero_dir (list);
422 #ifndef SCO_FLAVOR
423 if (pclose (external) < 0)
424 #else /* SCO_FLAVOR */
425 if (WEXITSTATUS(pclose (external)) < 0)
426 #endif /* SCO_FLAVOR */
427 message (0, _("External panelize"), _("Pipe close failed"));
428 close_error_pipe (0, 0);
429 try_to_select (cpanel, NULL);
430 panel_re_sort (cpanel);
431 paint_panel (cpanel);