- Start rosapps rearrange and cleanup process.
[reactos.git] / rosapps / applications / mc / src / panelize.c
1 /* External panelize
2 Copyright (C) 1995 The Free Software Foundation
3
4 Written by: 1995 Janne Kukonlehto
5 1995 Jakub Jelinek
6
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.
11
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.
16
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.
20 */
21
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 <stdlib.h> /* For malloc() */
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #ifdef HAVE_SYS_WAIT_H
34 # include <sys/wait.h>
35 #endif
36 #ifndef OS2_NT
37 # include <grp.h>
38 # include <pwd.h>
39 #endif
40 #include "tty.h"
41 #include "mad.h"
42 #include "util.h" /* Needed for the externs */
43 #include "win.h"
44 #include "color.h"
45 #include "dlg.h"
46 #include "widget.h"
47 #include "dialog.h" /* For do_refresh() */
48 #include "setup.h" /* For profile_bname */
49 #include "profile.h" /* Load/save directories panelize */
50 #include "fs.h"
51
52 /* Needed for the extern declarations of integer parameters */
53 #define DIR_H_INCLUDE_HANDLE_DIRENT
54 #include "dir.h"
55 #include "panel.h" /* Needed for the externs */
56 #include "file.h"
57 #include "main.h"
58 #include "global.h"
59 #include "../vfs/vfs.h"
60
61 void do_external_panelize (char *command);
62
63 #define UX 5
64 #define UY 2
65
66 #define BX 5
67 #define BY 18
68
69 #define BUTTONS 4
70 #define LABELS 3
71 #define B_ADD B_USER
72 #define B_REMOVE B_USER + 1
73
74 static WListbox *l_panelize;
75
76 static Dlg_head *panelize_dlg;
77
78 static WInput *pname;
79
80 static char *panelize_section = "Panelize";
81
82 static int last_listitem;
83
84 struct {
85 int ret_cmd, flags, y, x;
86 char *text;
87 char *tkname;
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"},
93 };
94
95 /* Directory panelize */
96 static struct panelize{
97 char *command;
98 char *label;
99 struct panelize *next;
100 } *panelize = NULL;
101
102 static char* panelize_title = N_(" External panelize ");
103
104 #ifndef HAVE_X
105 static void
106 panelize_refresh (void)
107 {
108 attrset (COLOR_NORMAL);
109 dlg_erase (panelize_dlg);
110
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);
113
114 attrset (COLOR_HOT_NORMAL);
115 dlg_move (panelize_dlg, 1, (panelize_dlg->cols - strlen(panelize_title)) / 2);
116 addstr (panelize_title);
117 }
118 #endif
119
120 static void
121 update_command ()
122 {
123 if (l_panelize->pos != last_listitem) {
124 last_listitem = l_panelize->pos;
125 assign_text (pname,
126 ((struct panelize *) l_panelize->current->data)->command);
127 pname->point = 0;
128 update_input (pname, 1);
129 }
130 }
131
132 static int
133 panelize_callback (Dlg_head * h, int Par, int Msg)
134 {
135 switch (Msg) {
136 #ifndef HAVE_X
137 case DLG_DRAW:
138 panelize_refresh ();
139 break;
140 #endif
141
142 case DLG_POST_KEY:
143 /* fall */
144 case DLG_INIT:
145 attrset (MENU_ENTRY_COLOR);
146 update_command ();
147 break;
148 }
149 return 0;
150 }
151
152 static int l_call (void *data)
153 {
154 return listbox_nothing;
155 }
156
157 static void init_panelize (void)
158 {
159 int i, panelize_cols = COLS - 6;
160 struct panelize *current = panelize;
161
162 #ifdef ENABLE_NLS
163 static int i18n_flag = 0;
164 static int maxlen = 0;
165
166 if (!i18n_flag)
167 {
168 i = sizeof(panelize_but) / sizeof(panelize_but[0]);
169 while (i--)
170 {
171 panelize_but [i].text = _(panelize_but [i].text);
172 maxlen += strlen (panelize_but [i].text) + 5;
173 }
174 maxlen += 10;
175 panelize_title = _(panelize_title);
176
177 i18n_flag = 1;
178 }
179 panelize_cols = max(panelize_cols, maxlen);
180
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;
187
188 #endif /* ENABLE_NLS */
189
190 last_listitem = 0;
191
192 do_refresh ();
193
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"));
198
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
200
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);
204
205 pname = input_new (UY+14, UX, INPUT_COLOR, panelize_dlg->cols-10, "", "in");
206 add_widgetl (panelize_dlg, pname, XV_WLAY_RIGHTOF);
207
208 add_widgetl (panelize_dlg, label_new (UY+13, UX, _("Command"), "label-command"), XV_WLAY_NEXTROW);
209
210 /* get new listbox */
211 l_panelize = listbox_new (UY + 1, UX + 1, panelize_dlg->cols-12, 10, 0, l_call, "li");
212
213 while (current){
214 listbox_add_item (l_panelize, 0, 0, current->label, current);
215 current = current->next;
216 }
217
218 /* add listbox to the dialogs */
219 add_widgetl (panelize_dlg, l_panelize, XV_WLAY_EXTENDWIDTH);
220
221 listbox_select_entry (l_panelize,
222 listbox_search_text (l_panelize, _("Other command")));
223 }
224
225 static void panelize_done (void)
226 {
227 destroy_dlg (panelize_dlg);
228 repaint_screen ();
229 }
230
231 static void add2panelize (char *label, char *command)
232 {
233 struct panelize *current, *old;
234
235 old = NULL;
236 current = panelize;
237 while (current && strcmp (current->label, label) <= 0){
238 old = current;
239 current = current->next;
240 }
241
242 if (old == NULL){
243 panelize = malloc (sizeof (struct panelize));
244 panelize->label = label;
245 panelize->command = command;
246 panelize->next = current;
247 } else {
248 struct panelize *new;
249 new = malloc (sizeof (struct panelize));
250 new->label = label;
251 new->command = command;
252 old->next = new;
253 new->next = current;
254 }
255 }
256
257 void add2panelize_cmd (void)
258 {
259 char *label;
260
261 if (pname->buffer && (*pname->buffer)) {
262 label = input_dialog (_(" Add to external panelize "),
263 _(" Enter command label: "),
264 "");
265 if (!label)
266 return;
267 if (!*label) {
268 free (label);
269 return;
270 }
271
272 add2panelize (label, strdup(pname->buffer));
273 }
274 }
275
276 static void remove_from_panelize (struct panelize *entry)
277 {
278 if (strcmp (entry->label, _("Other command")) != 0) {
279 if (entry == panelize) {
280 panelize = panelize->next;
281 } else {
282 struct panelize *current = panelize;
283 while (current && current->next != entry)
284 current = current->next;
285 if (current) {
286 current->next = entry->next;
287 }
288 }
289
290 free (entry->label);
291 free (entry->command);
292 free (entry);
293 }
294 }
295
296 void external_panelize (void)
297 {
298 char *target = NULL;
299
300 if (!vfs_current_is_local ()){
301 message (1, _(" Oops... "),
302 _(" I can't run external panelize while logged on a non local directory "));
303 return;
304 }
305
306 init_panelize ();
307
308 /* display file info */
309 attrset (SELECTED_COLOR);
310
311 run_dlg (panelize_dlg);
312
313 switch (panelize_dlg->ret_value) {
314 case B_CANCEL:
315 break;
316
317 case B_ADD:
318 add2panelize_cmd ();
319 break;
320
321 case B_REMOVE:
322 remove_from_panelize (l_panelize->current->data);
323 break;
324
325 case B_ENTER:
326 target = pname->buffer;
327 if (target != NULL && *target) {
328 char *cmd = strdup (target);
329 destroy_dlg (panelize_dlg);
330 do_external_panelize (cmd);
331 free (cmd);
332 repaint_screen ();
333 return;
334 }
335 break;
336 }
337
338 panelize_done ();
339 }
340
341 void load_panelize (void)
342 {
343 void *profile_keys;
344 char *key, *value;
345
346 profile_keys = profile_init_iterator (panelize_section, profile_name);
347
348 add2panelize (strdup (_("Other command")), strdup (""));
349
350 if (!profile_keys){
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"));
354 return;
355 }
356
357 while (profile_keys){
358 profile_keys = profile_iterator_next (profile_keys, &key, &value);
359 add2panelize (strdup (key), strdup (value));
360 }
361 }
362
363 void save_panelize (void)
364 {
365 struct panelize *current = panelize;
366
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,
371 current->label,
372 current->command,
373 profile_name);
374 }
375 sync_profiles ();
376 }
377
378 void done_panelize (void)
379 {
380 struct panelize *current = panelize;
381 struct panelize *next;
382
383 for (; current; current = next){
384 next = current->next;
385 free (current->label);
386 free (current->command);
387 free (current);
388 }
389 }
390
391 void do_external_panelize (char *command)
392 {
393 int status, link_to_dir, stalled_link;
394 int next_free = 0;
395 struct stat buf;
396 dir_list *list = &cpanel->dir;
397 char line [MC_MAXPATHLEN];
398 char *name;
399 FILE *external;
400
401 open_error_pipe ();
402 external = popen (command, "r");
403 if (!external){
404 close_error_pipe (1, _("Cannot invoke command."));
405 return;
406 }
407 clean_dir (list, cpanel->count);
408
409 /* Clear the counters */
410 cpanel->total = cpanel->dirs_marked = cpanel->marked = 0;
411 cpanel->has_dir_sizes = 0;
412 while (1) {
413 clearerr(external);
414 if (fgets (line, MC_MAXPATHLEN, external) == NULL)
415 if (ferror(external) && errno == EINTR)
416 continue;
417 else
418 break;
419 if (line[strlen(line)-1] == '\n')
420 line[strlen(line)-1] = 0;
421 if (strlen(line) < 1)
422 continue;
423 if (line [0] == '.' && line[1] == PATH_SEP)
424 name = line + 2;
425 else
426 name = line;
427 status = handle_path (list, name, &buf, next_free, &link_to_dir,
428 &stalled_link);
429 if (status == 0)
430 continue;
431 if (status == -1)
432 break;
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;
440 next_free++;
441 if (!(next_free & 32))
442 rotate_dash ();
443 }
444 if (next_free){
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);
450 }
451 } else {
452 cpanel->count = set_zero_dir (list);
453 }
454 #ifndef SCO_FLAVOR
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);
463 }