Merge branch '1691_debian_man_patches'
[midnight-commander.git] / src / find.c
bloba8df52eee6f81578ae88abeac525a269c211251a
1 /* Find file command for the Midnight Commander
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
4 Written 1995 by Miguel de Icaza
6 Complete rewrote.
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. */
22 /** \file find.c
23 * \brief Source: Find file command
26 #include <config.h>
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
35 #include "global.h"
37 #include "../src/tty/tty.h"
38 #include "../src/skin/skin.h"
39 #include "../src/tty/key.h"
41 #include "../src/search/search.h"
43 #include "setup.h"
44 #include "find.h"
45 #include "strutil.h"
46 #include "dialog.h"
47 #include "widget.h"
48 #include "dir.h"
49 #include "panel.h" /* current_panel */
50 #include "main.h" /* do_cd, try_to_select */
51 #include "wtools.h"
52 #include "cmd.h" /* view_file_at_line */
53 #include "boxes.h"
54 #include "history.h" /* MC_HISTORY_SHARED_SEARCH */
55 #include "layout.h" /* mc_refresh() */
57 /* Size of the find parameters window */
58 #if HAVE_CHARSET
59 static int FIND_Y = 16;
60 #else
61 static int FIND_Y = 15;
62 #endif
63 static int FIND_X = 68;
65 /* Size of the find window */
66 #define FIND2_Y (LINES - 4)
68 static int FIND2_X = 64;
69 #define FIND2_X_USE (FIND2_X - 20)
71 /* A couple of extra messages we need */
72 enum {
73 B_STOP = B_USER + 1,
74 B_AGAIN,
75 B_PANELIZE,
76 B_TREE,
77 B_VIEW
80 typedef enum {
81 FIND_CONT = 0,
82 FIND_SUSPEND,
83 FIND_ABORT
84 } FindProgressStatus;
86 /* List of directories to be ignored, separated by ':' */
87 char *find_ignore_dirs = NULL;
89 /* static variables to remember find parameters */
90 static WInput *in_start; /* Start path */
91 static WInput *in_name; /* Filename */
92 static WInput *in_with; /* Text inside filename */
93 static WCheck *file_case_sens_cbox; /* "case sensitive" checkbox */
94 static WCheck *file_pattern_cbox; /* File name is glob or regexp */
95 static WCheck *recursively_cbox;
96 static WCheck *skip_hidden_cbox;
97 static WCheck *content_case_sens_cbox; /* "case sensitive" checkbox */
98 static WCheck *content_regexp_cbox; /* "find regular expression" checkbox */
99 static WCheck *content_first_hit_cbox; /* "First hit" checkbox" */
100 static WCheck *content_whole_words_cbox; /* "whole words" checkbox */
101 #ifdef HAVE_CHARSET
102 static WCheck *file_all_charsets_cbox;
103 static WCheck *content_all_charsets_cbox;
104 #endif
106 static gboolean running = FALSE; /* nice flag */
107 static char *find_pattern = NULL; /* Pattern to search */
108 static char *content_pattern = NULL; /* pattern to search inside files; if
109 content_regexp_flag is true, it contains the
110 regex pattern, else the search string. */
111 static unsigned long matches; /* Number of matches */
112 static gboolean is_start = FALSE; /* Status of the start/stop toggle button */
113 static char *old_dir = NULL;
115 /* Where did we stop */
116 static int resuming;
117 static int last_line;
118 static int last_pos;
120 static Dlg_head *find_dlg; /* The dialog */
121 static WButton *stop_button; /* pointer to the stop button */
122 static WLabel *status_label; /* Finished, Searching etc. */
123 static WLabel *found_num_label; /* Number of found items */
124 static WListbox *find_list; /* Listbox with the file list */
126 /* This keeps track of the directory stack */
127 #if GLIB_CHECK_VERSION (2, 14, 0)
128 static GQueue dir_queue = G_QUEUE_INIT;
129 #else
130 typedef struct dir_stack {
131 char *name;
132 struct dir_stack *prev;
133 } dir_stack;
135 static dir_stack *dir_stack_base = 0;
136 #endif /* GLIB_CHECK_VERSION */
138 static struct {
139 const char* text;
140 int len; /* length including space and brackets */
141 int x;
142 } fbuts [] = {
143 { N_("&Suspend"), 11, 29 },
144 { N_("Con&tinue"), 12, 29 },
145 { N_("&Chdir"), 11, 3 },
146 { N_("&Again"), 9, 17 },
147 { N_("&Quit"), 8, 43 },
148 { N_("Pane&lize"), 12, 3 },
149 { N_("&View - F3"), 13, 20 },
150 { N_("&Edit - F4"), 13, 38 }
153 static char *in_start_dir = INPUT_LAST_TEXT;
155 static mc_search_t *search_file_handle = NULL;
156 static gboolean skip_hidden_flag = FALSE;
157 static gboolean file_pattern_flag = TRUE;
158 static gboolean file_all_charsets_flag = FALSE;
159 static gboolean file_case_sens_flag = TRUE;
160 static gboolean find_recurs_flag = TRUE;
162 static mc_search_t *search_content_handle = NULL;
163 static gboolean content_regexp_flag = FALSE;
164 static gboolean content_all_charsets_flag = FALSE;
165 static gboolean content_case_sens_flag = TRUE;
166 static gboolean content_first_hit_flag = FALSE;
167 static gboolean content_whole_words = FALSE;
169 static inline char *
170 add_to_list (const char *text, void *data)
172 return listbox_add_item (find_list, LISTBOX_APPEND_AT_END, 0, text, data);
175 static inline void
176 stop_idle (void *data)
178 set_idle_proc (data, 0);
181 static inline void
182 status_update (const char *text)
184 label_set_text (status_label, text);
187 static void
188 found_num_update (void)
190 char buffer [BUF_TINY];
191 g_snprintf (buffer, sizeof (buffer), _("Found: %ld"), matches);
192 label_set_text (found_num_label, buffer);
195 static void
196 get_list_info (char **file, char **dir)
198 listbox_get_current (find_list, file, dir);
201 /* check regular expression */
202 static gboolean
203 find_check_regexp (const char *r)
205 mc_search_t *search;
206 gboolean regexp_ok = FALSE;
208 search = mc_search_new (r, -1);
210 if (search != NULL) {
211 search->search_type = MC_SEARCH_T_REGEX;
212 regexp_ok = mc_search_prepare (search);
213 mc_search_free (search);
216 return regexp_ok;
220 * Callback for the parameter dialog.
221 * Validate regex, prevent closing the dialog if it's invalid.
223 static cb_ret_t
224 find_parm_callback (Dlg_head *h, Widget *sender,
225 dlg_msg_t msg, int parm, void *data)
227 switch (msg) {
228 case DLG_VALIDATE:
229 if (h->ret_value != B_ENTER)
230 return MSG_HANDLED;
232 /* check filename regexp */
233 if (!(file_pattern_cbox->state & C_BOOL)
234 && (in_name->buffer[0] != '\0')
235 && !find_check_regexp (in_name->buffer)) {
236 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
237 dlg_select_widget (in_name);
238 h->running = 1; /* Don't stop the dialog */
239 return MSG_HANDLED;
242 /* check content regexp */
243 if ((content_regexp_cbox->state & C_BOOL)
244 && (in_with->buffer[0] != '\0')
245 && !find_check_regexp (in_with->buffer)) {
246 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
247 dlg_select_widget (in_with);
248 h->running = 1; /* Don't stop the dialog */
249 return MSG_HANDLED;
252 return MSG_HANDLED;
254 default:
255 return default_dlg_callback (h, sender, msg, parm, data);
260 * find_parameters: gets information from the user
262 * If the return value is true, then the following holds:
264 * START_DIR and PATTERN are pointers to char * and upon return they
265 * contain the information provided by the user.
267 * CONTENT holds a strdup of the contents specified by the user if he
268 * asked for them or 0 if not (note, this is different from the
269 * behavior for the other two parameters.
272 static int
273 find_parameters (char **start_dir, char **pattern, char **content)
275 int return_value;
276 char *temp_dir = NULL;
278 /* file name */
279 const char *file_case_label = N_("Cas&e sensitive");
280 const char *file_pattern_label = N_("&Using shell patterns");
281 const char *file_recurs_label = N_("&Find recursively");
282 const char *file_skip_hidden_label = N_("S&kip hidden");
283 #ifdef HAVE_CHARSET
284 const char *file_all_charsets_label = N_("&All charsets");
285 #endif
287 /* file content */
288 const char *content_case_label = N_("Case sens&itive");
289 const char *content_regexp_label = N_("Re&gular expression");
290 const char *content_first_hit_label = N_("Fir&st hit");
291 const char *content_whole_words_label = N_("&Whole words");
292 #ifdef HAVE_CHARSET
293 const char *content_all_charsets_label = N_("All cha&rsets");
294 #endif
296 const char *buts[] = { N_("&OK"), N_("&Cancel"), N_("&Tree") };
298 int b0, b1, b2;
300 #ifdef ENABLE_NLS
302 int i = sizeof (buts) / sizeof (buts[0]);
303 while (i-- != 0)
304 buts[i] = _(buts[i]);
306 file_case_label = _(file_case_label);
307 file_pattern_label = _(file_pattern_label);
308 file_recurs_label = _(file_recurs_label);
309 file_skip_hidden_label = _(file_skip_hidden_label);
310 #ifdef HAVE_CHARSET
311 file_all_charsets_label = _(file_all_charsets_label);
312 content_all_charsets_label = _(content_all_charsets_label);
313 #endif
314 content_case_label = _(content_case_label);
315 content_regexp_label = _(content_regexp_label);
316 content_first_hit_label = _(content_first_hit_label);
317 content_whole_words_label = _(content_whole_words_label);
319 #endif /* ENABLE_NLS */
321 b0 = str_term_width1 (buts[0]) + 6; /* default button */
322 b1 = str_term_width1 (buts[1]) + 4;
323 b2 = str_term_width1 (buts[2]) + 4;
325 find_par_start:
326 if (in_start_dir == NULL)
327 in_start_dir = g_strdup (".");
329 find_dlg =
330 create_dlg (0, 0, FIND_Y, FIND_X, dialog_colors,
331 find_parm_callback, "[Find File]", _("Find File"),
332 DLG_CENTER | DLG_REVERSE);
334 add_widget (find_dlg,
335 button_new (FIND_Y - 3, FIND_X * 3/4 - b1/2, B_CANCEL, NORMAL_BUTTON, buts[1], 0));
336 add_widget (find_dlg,
337 button_new (FIND_Y - 3, FIND_X/4 - b0/2, B_ENTER, DEFPUSH_BUTTON, buts[0], 0));
339 #ifdef HAVE_CHARSET
340 content_all_charsets_cbox = check_new (11, FIND_X / 2 + 1,
341 content_all_charsets_flag, content_all_charsets_label);
342 add_widget (find_dlg, content_all_charsets_cbox);
343 #endif
345 content_whole_words_cbox = check_new (10, FIND_X / 2 + 1, content_whole_words, content_whole_words_label);
346 add_widget (find_dlg, content_whole_words_cbox);
348 content_first_hit_cbox = check_new (9, FIND_X / 2 + 1, content_first_hit_flag, content_first_hit_label);
349 add_widget (find_dlg, content_first_hit_cbox);
351 content_regexp_cbox = check_new (8, FIND_X / 2 + 1, content_regexp_flag, content_regexp_label);
352 add_widget (find_dlg, content_regexp_cbox);
354 content_case_sens_cbox = check_new (7, FIND_X / 2 + 1, content_case_sens_flag, content_case_label);
355 add_widget (find_dlg, content_case_sens_cbox);
357 #ifdef HAVE_CHARSET
358 file_all_charsets_cbox = check_new (11, 3,
359 file_all_charsets_flag, file_all_charsets_label);
360 add_widget (find_dlg, file_all_charsets_cbox);
361 #endif
363 skip_hidden_cbox = check_new (10, 3, skip_hidden_flag, file_skip_hidden_label);
364 add_widget (find_dlg, skip_hidden_cbox);
366 recursively_cbox = check_new (9, 3, find_recurs_flag, file_recurs_label);
367 add_widget (find_dlg, recursively_cbox);
369 file_pattern_cbox = check_new (8, 3, file_pattern_flag, file_pattern_label);
370 add_widget (find_dlg, file_pattern_cbox);
372 file_case_sens_cbox = check_new (7, 3, file_case_sens_flag, file_case_label);
373 add_widget (find_dlg, file_case_sens_cbox);
375 in_with = input_new (6, FIND_X / 2 + 1, INPUT_COLOR, FIND_X / 2 - 4, INPUT_LAST_TEXT,
376 MC_HISTORY_SHARED_SEARCH, INPUT_COMPLETE_DEFAULT);
377 add_widget (find_dlg, in_with);
378 add_widget (find_dlg, label_new (5, FIND_X / 2 + 1, _("Content:")));
380 in_name = input_new (6, 3, INPUT_COLOR, FIND_X / 2 - 4, INPUT_LAST_TEXT, "name",
381 INPUT_COMPLETE_DEFAULT);
382 add_widget (find_dlg, in_name);
383 add_widget (find_dlg, label_new (5, 3, _("File name:")));
385 add_widget (find_dlg,
386 button_new (3, FIND_X - b2 - 2, B_TREE, NORMAL_BUTTON, buts[2], 0));
388 in_start = input_new (3, 3, INPUT_COLOR, FIND_X - b2 - 6, in_start_dir, "start",
389 INPUT_COMPLETE_DEFAULT);
390 add_widget (find_dlg, in_start);
391 add_widget (find_dlg, label_new (2, 3, _("Start at:")));
393 dlg_select_widget (in_name);
395 switch (run_dlg (find_dlg)) {
396 case B_CANCEL:
397 return_value = 0;
398 break;
400 case B_TREE:
401 #ifdef HAVE_CHARSET
402 file_all_charsets_flag = file_all_charsets_cbox->state & C_BOOL;
403 content_all_charsets_flag = content_all_charsets_cbox->state & C_BOOL;
404 #endif
405 content_case_sens_flag = content_case_sens_cbox->state & C_BOOL;
406 content_regexp_flag = content_regexp_cbox->state & C_BOOL;
407 content_first_hit_flag = content_first_hit_cbox->state & C_BOOL;
408 content_whole_words = content_whole_words_cbox->state & C_BOOL;
409 file_pattern_flag = file_pattern_cbox->state & C_BOOL;
410 file_case_sens_flag = file_case_sens_cbox->state & C_BOOL;
411 find_recurs_flag = recursively_cbox->state & C_BOOL;
412 skip_hidden_flag = skip_hidden_cbox->state & C_BOOL;
414 destroy_dlg (find_dlg);
415 if (in_start_dir != INPUT_LAST_TEXT)
416 g_free (in_start_dir);
417 temp_dir = g_strdup (in_start->buffer);
418 if ((temp_dir[0] == '.') && (temp_dir[1] == '\0')) {
419 g_free (temp_dir);
420 temp_dir = g_strdup (current_panel->cwd);
422 in_start_dir = tree_box (temp_dir);
423 if (in_start_dir != NULL)
424 g_free (temp_dir);
425 else
426 in_start_dir = temp_dir;
427 /* Warning: Dreadful goto */
428 goto find_par_start;
429 break;
431 default:
432 #ifdef HAVE_CHARSET
433 file_all_charsets_flag = file_all_charsets_cbox->state & C_BOOL;
434 content_all_charsets_flag = content_all_charsets_cbox->state & C_BOOL;
435 #endif
436 content_case_sens_flag = content_case_sens_cbox->state & C_BOOL;
437 content_regexp_flag = content_regexp_cbox->state & C_BOOL;
438 content_first_hit_flag = content_first_hit_cbox->state & C_BOOL;
439 content_whole_words = content_whole_words_cbox->state & C_BOOL;
440 find_recurs_flag = recursively_cbox->state & C_BOOL;
441 file_pattern_flag = file_pattern_cbox->state & C_BOOL;
442 file_case_sens_flag = file_case_sens_cbox->state & C_BOOL;
443 skip_hidden_flag = skip_hidden_cbox->state & C_BOOL;
445 *content = (in_with->buffer[0] != '\0') ? g_strdup (in_with->buffer) : NULL;
446 *start_dir = g_strdup ((in_start->buffer[0] != '\0') ? in_start->buffer : ".");
447 *pattern = g_strdup (in_name->buffer);
448 if (in_start_dir != INPUT_LAST_TEXT)
449 g_free (in_start_dir);
450 in_start_dir = g_strdup (*start_dir);
451 return_value = 1;
454 destroy_dlg (find_dlg);
456 return return_value;
459 #if GLIB_CHECK_VERSION (2, 14, 0)
461 static inline void
462 push_directory (const char *dir)
464 g_queue_push_head (&dir_queue, (void *) dir);
467 static inline char *
468 pop_directory (void)
470 return (char *) g_queue_pop_tail (&dir_queue);
473 /* Remove all the items in the stack */
474 static void
475 clear_stack (void)
477 g_queue_foreach (&dir_queue, (GFunc) g_free, NULL);
478 g_queue_clear (&dir_queue);
481 #else /* GLIB_CHAECK_VERSION */
483 static void
484 push_directory (const char *dir)
486 dir_stack *new;
488 new = g_new (dir_stack, 1);
489 new->name = str_unconst (dir);
490 new->prev = dir_stack_base;
491 dir_stack_base = new;
494 static char *
495 pop_directory (void)
497 char *name = NULL;
499 if (dir_stack_base != NULL) {
500 dir_stack *next;
501 name = dir_stack_base->name;
502 next = dir_stack_base->prev;
503 g_free (dir_stack_base);
504 dir_stack_base = next;
507 return name;
510 /* Remove all the items in the stack */
511 static void
512 clear_stack (void)
514 char *dir = NULL;
515 while ((dir = pop_directory ()) != NULL)
516 g_free (dir);
519 #endif /* GLIB_CHAECK_VERSION */
521 static void
522 insert_file (const char *dir, const char *file)
524 char *tmp_name = NULL;
525 static char *dirname = NULL;
527 while (dir [0] == PATH_SEP && dir [1] == PATH_SEP)
528 dir++;
530 if (old_dir){
531 if (strcmp (old_dir, dir)){
532 g_free (old_dir);
533 old_dir = g_strdup (dir);
534 dirname = add_to_list (dir, NULL);
536 } else {
537 old_dir = g_strdup (dir);
538 dirname = add_to_list (dir, NULL);
541 tmp_name = g_strdup_printf (" %s", file);
542 add_to_list (tmp_name, dirname);
543 g_free (tmp_name);
546 static void
547 find_add_match (const char *dir, const char *file)
549 insert_file (dir, file);
551 /* Don't scroll */
552 if (matches == 0)
553 listbox_select_by_number (find_list, 0);
554 send_message (&find_list->widget, WIDGET_DRAW, 0);
556 matches++;
557 found_num_update ();
561 * get_line_at:
563 * Returns malloced null-terminated line from file file_fd.
564 * Input is buffered in buf_size long buffer.
565 * Current pos in buf is stored in pos.
566 * n_read - number of read chars.
567 * has_newline - is there newline ?
569 static char *
570 get_line_at (int file_fd, char *buf, int buf_size, int *pos, int *n_read,
571 gboolean *has_newline)
573 char *buffer = NULL;
574 int buffer_size = 0;
575 char ch = 0;
576 int i = 0;
578 for (;;) {
579 if (*pos >= *n_read) {
580 *pos = 0;
581 *n_read = mc_read (file_fd, buf, buf_size);
582 if (*n_read <= 0)
583 break;
586 ch = buf[(*pos)++];
587 if (ch == '\0') {
588 /* skip possible leading zero(s) */
589 if (i == 0)
590 continue;
591 else
592 break;
595 if (i >= buffer_size - 1) {
596 buffer = g_realloc (buffer, buffer_size += 80);
598 /* Strip newline */
599 if (ch == '\n')
600 break;
602 buffer[i++] = ch;
605 *has_newline = (ch != '\0');
607 if (buffer != NULL)
608 buffer[i] = '\0';
610 return buffer;
613 static FindProgressStatus
614 check_find_events(Dlg_head *h)
616 Gpm_Event event;
617 int c;
619 event.x = -1;
620 c = tty_get_event (&event, h->mouse_status == MOU_REPEAT, FALSE);
621 if (c != EV_NONE) {
622 dlg_process_event (h, c, &event);
623 if (h->ret_value == B_ENTER
624 || h->ret_value == B_CANCEL
625 || h->ret_value == B_AGAIN
626 || h->ret_value == B_PANELIZE) {
627 /* dialog terminated */
628 return FIND_ABORT;
630 if (!(h->flags & DLG_WANT_IDLE)) {
631 /* searching suspended */
632 return FIND_SUSPEND;
636 return FIND_CONT;
640 * search_content:
642 * Search the content_pattern string in the DIRECTORY/FILE.
643 * It will add the found entries to the find listbox.
645 * returns FALSE if do_search should look for another file
646 * TRUE if do_search should exit and proceed to the event handler
648 static gboolean
649 search_content (Dlg_head *h, const char *directory, const char *filename)
651 struct stat s;
652 char buffer [BUF_4K];
653 char *fname = NULL;
654 int file_fd;
655 gboolean ret_val = FALSE;
657 fname = concat_dir_and_file (directory, filename);
659 if (mc_stat (fname, &s) != 0 || !S_ISREG (s.st_mode)){
660 g_free (fname);
661 return 0;
664 file_fd = mc_open (fname, O_RDONLY);
665 g_free (fname);
667 if (file_fd == -1)
668 return 0;
670 g_snprintf (buffer, sizeof (buffer), _("Grepping in %s"), str_trunc (filename, FIND2_X_USE));
672 status_update (buffer);
673 mc_refresh ();
675 tty_enable_interrupt_key ();
676 tty_got_interrupt ();
679 int line = 1;
680 int pos = 0;
681 int n_read = 0;
682 gboolean has_newline;
683 char *p = NULL;
684 gboolean found = FALSE;
685 gsize found_len;
686 char result [BUF_MEDIUM];
688 if (resuming) {
689 /* We've been previously suspended, start from the previous position */
690 resuming = 0;
691 line = last_line;
692 pos = last_pos;
694 while (!ret_val
695 && (p = get_line_at (file_fd, buffer, sizeof (buffer),
696 &pos, &n_read, &has_newline)) != NULL) {
697 if (!found /* Search in binary line once */
698 && mc_search_run (search_content_handle,
699 (const void *) p, 0, strlen (p), &found_len)) {
700 g_snprintf (result, sizeof (result), "%d:%s", line, filename);
701 find_add_match (directory, result);
702 found = TRUE;
704 g_free (p);
706 if (found && content_first_hit_flag)
707 break;
709 if (has_newline) {
710 found = FALSE;
711 line++;
714 if ((line & 0xff) == 0) {
715 FindProgressStatus res;
716 res = check_find_events(h);
717 switch (res) {
718 case FIND_ABORT:
719 stop_idle (h);
720 ret_val = TRUE;
721 break;
722 case FIND_SUSPEND:
723 resuming = 1;
724 last_line = line;
725 last_pos = pos;
726 ret_val = TRUE;
727 break;
728 default:
729 break;
735 tty_disable_interrupt_key ();
736 mc_close (file_fd);
737 return ret_val;
740 static int
741 do_search (struct Dlg_head *h)
743 static struct dirent *dp = NULL;
744 static DIR *dirp = NULL;
745 static char *directory = NULL;
746 struct stat tmp_stat;
747 static int pos = 0;
748 static int subdirs_left = 0;
749 gsize bytes_found;
750 unsigned long count; /* Number of files displayed */
752 if (!h) { /* someone forces me to close dirp */
753 if (dirp) {
754 mc_closedir (dirp);
755 dirp = NULL;
757 g_free (directory);
758 directory = NULL;
759 dp = NULL;
760 return 1;
763 search_content_handle = mc_search_new(content_pattern, -1);
764 if (search_content_handle) {
765 search_content_handle->search_type = (content_regexp_flag) ? MC_SEARCH_T_REGEX : MC_SEARCH_T_NORMAL;
766 search_content_handle->is_case_sentitive = content_case_sens_flag;
767 search_content_handle->whole_words = content_whole_words;
768 search_content_handle->is_all_charsets = content_all_charsets_flag;
770 search_file_handle = mc_search_new(find_pattern, -1);
771 search_file_handle->search_type = (file_pattern_flag) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
772 search_file_handle->is_case_sentitive = file_case_sens_flag;
773 search_file_handle->is_all_charsets = file_all_charsets_flag;
774 search_file_handle->is_entire_line = file_pattern_flag;
776 count = 0;
778 do_search_begin:
779 while (!dp){
780 if (dirp){
781 mc_closedir (dirp);
782 dirp = 0;
785 while (!dirp){
786 char *tmp = NULL;
788 tty_setcolor (REVERSE_COLOR);
789 while (1) {
790 char *temp_dir = NULL;
791 gboolean found;
793 tmp = pop_directory ();
794 if (tmp == NULL) {
795 running = FALSE;
796 status_update (_("Finished"));
797 stop_idle (h);
798 mc_search_free (search_file_handle);
799 search_file_handle = NULL;
800 mc_search_free (search_content_handle);
801 search_content_handle = NULL;
802 return 0;
805 if ((find_ignore_dirs == NULL) || (find_ignore_dirs[0] == '\0'))
806 break;
808 temp_dir = g_strdup_printf (":%s:", tmp);
809 found = strstr (find_ignore_dirs, temp_dir) != 0;
810 g_free (temp_dir);
812 if (!found)
813 break;
815 g_free (tmp);
818 g_free (directory);
819 directory = tmp;
821 if (verbose){
822 char buffer [BUF_SMALL];
824 g_snprintf (buffer, sizeof (buffer), _("Searching %s"),
825 str_trunc (directory, FIND2_X_USE));
826 status_update (buffer);
828 /* mc_stat should not be called after mc_opendir
829 because vfs_s_opendir modifies the st_nlink
831 if (!mc_stat (directory, &tmp_stat))
832 subdirs_left = tmp_stat.st_nlink - 2;
833 else
834 subdirs_left = 0;
836 dirp = mc_opendir (directory);
837 } /* while (!dirp) */
839 /* skip invalid filenames */
840 while ((dp = mc_readdir (dirp)) != NULL
841 && !str_is_valid_string (dp->d_name))
843 } /* while (!dp) */
845 if (strcmp (dp->d_name, ".") == 0 ||
846 strcmp (dp->d_name, "..") == 0){
847 dp = mc_readdir (dirp);
848 /* skip invalid filenames */
849 while (dp != NULL && !str_is_valid_string (dp->d_name))
850 dp = mc_readdir (dirp);
852 mc_search_free(search_file_handle);
853 search_file_handle = NULL;
854 mc_search_free(search_content_handle);
855 search_content_handle = NULL;
856 return 1;
859 if (!(skip_hidden_flag && (dp->d_name[0] == '.'))) {
860 gboolean search_ok;
862 if ((subdirs_left != 0) && find_recurs_flag
863 && (directory != NULL)) { /* Can directory be NULL ? */
864 char *tmp_name = concat_dir_and_file (directory, dp->d_name);
865 if (!mc_lstat (tmp_name, &tmp_stat)
866 && S_ISDIR (tmp_stat.st_mode)) {
867 push_directory (tmp_name);
868 subdirs_left--;
869 } else
870 g_free (tmp_name);
873 search_ok = mc_search_run (search_file_handle, dp->d_name,
874 0, strlen (dp->d_name), &bytes_found);
876 if (search_ok) {
877 if (content_pattern == NULL)
878 find_add_match (directory, dp->d_name);
879 else if (search_content (h, directory, dp->d_name)) {
880 mc_search_free(search_file_handle);
881 search_file_handle = NULL;
882 mc_search_free(search_content_handle);
883 search_content_handle = NULL;
884 return 1;
889 /* skip invalid filenames */
890 while ((dp = mc_readdir (dirp)) != NULL
891 && !str_is_valid_string (dp->d_name))
894 /* Displays the nice dot */
895 count++;
896 if (!(count & 31)){
897 /* For nice updating */
898 const char rotating_dash[] = "|/-\\";
900 if (verbose){
901 pos = (pos + 1) % 4;
902 tty_setcolor (DLG_NORMALC (h));
903 dlg_move (h, FIND2_Y - 7, FIND2_X - 4);
904 tty_print_char (rotating_dash [pos]);
905 mc_refresh ();
907 } else
908 goto do_search_begin;
910 mc_search_free (search_file_handle);
911 search_file_handle = NULL;
912 mc_search_free (search_content_handle);
913 search_content_handle = NULL;
914 return 1;
917 static void
918 init_find_vars (void)
920 g_free (old_dir);
921 old_dir = NULL;
922 matches = 0;
924 /* Remove all the items in the stack */
925 clear_stack ();
928 static char *
929 make_fullname (const char *dirname, const char *filename)
932 if (strcmp(dirname, ".") == 0 || strcmp(dirname, "."PATH_SEP_STR) == 0)
933 return g_strdup (filename);
934 if (strncmp(dirname, "."PATH_SEP_STR, 2) == 0)
935 return concat_dir_and_file (dirname + 2, filename);
936 return concat_dir_and_file (dirname, filename);
939 static void
940 find_do_view_edit (int unparsed_view, int edit, char *dir, char *file)
942 char *fullname = NULL;
943 const char *filename = NULL;
944 int line;
946 if (content_pattern != NULL) {
947 filename = strchr (file + 4, ':') + 1;
948 line = atoi (file + 4);
949 } else {
950 filename = file + 4;
951 line = 0;
954 fullname = make_fullname (dir, filename);
955 if (edit)
956 do_edit_at_line (fullname, line);
957 else
958 view_file_at_line (fullname, unparsed_view, use_internal_view, line);
959 g_free (fullname);
962 static cb_ret_t
963 view_edit_currently_selected_file (int unparsed_view, int edit)
965 WLEntry *entry = find_list->current;
966 char *dir = NULL;
968 if (!entry)
969 return MSG_NOT_HANDLED;
971 dir = entry->data;
973 if (!entry->text || !dir)
974 return MSG_NOT_HANDLED;
976 find_do_view_edit (unparsed_view, edit, dir, entry->text);
977 return MSG_HANDLED;
980 static cb_ret_t
981 find_callback (struct Dlg_head *h, Widget *sender,
982 dlg_msg_t msg, int parm, void *data)
984 switch (msg) {
985 case DLG_KEY:
986 if (parm == KEY_F (3) || parm == KEY_F (13)) {
987 int unparsed_view = (parm == KEY_F (13));
988 return view_edit_currently_selected_file (unparsed_view, 0);
990 if (parm == KEY_F (4)) {
991 return view_edit_currently_selected_file (0, 1);
993 return MSG_NOT_HANDLED;
995 case DLG_IDLE:
996 do_search (h);
997 return MSG_HANDLED;
999 default:
1000 return default_dlg_callback (h, sender, msg, parm, data);
1004 /* Handles the Stop/Start button in the find window */
1005 static int
1006 start_stop (int button)
1008 (void) button;
1010 running = is_start;
1011 set_idle_proc (find_dlg, running);
1012 is_start = !is_start;
1014 status_update (is_start ? _("Stopped") : _("Searching"));
1015 button_set_text (stop_button, fbuts [is_start ? 1 : 0].text);
1017 return 0;
1020 /* Handle view command, when invoked as a button */
1021 static int
1022 find_do_view_file (int button)
1024 (void) button;
1026 view_edit_currently_selected_file (0, 0);
1027 return 0;
1030 /* Handle edit command, when invoked as a button */
1031 static int
1032 find_do_edit_file (int button)
1034 (void) button;
1036 view_edit_currently_selected_file (0, 1);
1037 return 0;
1040 static void
1041 setup_gui (void)
1043 #ifdef ENABLE_NLS
1044 static gboolean i18n_flag = FALSE;
1046 if (!i18n_flag) {
1047 int i = sizeof (fbuts) / sizeof (fbuts[0]);
1048 while (i-- != 0) {
1049 fbuts[i].text = _(fbuts[i].text);
1050 fbuts[i].len = str_term_width1 (fbuts[i].text) + 3;
1053 fbuts[2].len += 2; /* DEFPUSH_BUTTON */
1054 i18n_flag = TRUE;
1056 #endif /* ENABLE_NLS */
1059 * Dynamically place buttons centered within current window size
1062 int l0 = max (fbuts[0].len, fbuts[1].len);
1063 int l1 = fbuts[2].len + fbuts[3].len + l0 + fbuts[4].len;
1064 int l2 = fbuts[5].len + fbuts[6].len + fbuts[7].len;
1065 int r1, r2;
1067 /* Check, if both button rows fit within FIND2_X */
1068 FIND2_X = max (l1 + 9, COLS - 16);
1069 FIND2_X = max (l2 + 8, FIND2_X);
1071 /* compute amount of space between buttons for each row */
1072 r1 = (FIND2_X - 4 - l1) % 5;
1073 l1 = (FIND2_X - 4 - l1) / 5;
1074 r2 = (FIND2_X - 4 - l2) % 4;
1075 l2 = (FIND2_X - 4 - l2) / 4;
1077 /* ...and finally, place buttons */
1078 fbuts[2].x = 2 + r1 / 2 + l1;
1079 fbuts[3].x = fbuts[2].x + fbuts[2].len + l1;
1080 fbuts[0].x = fbuts[3].x + fbuts[3].len + l1;
1081 fbuts[4].x = fbuts[0].x + l0 + l1;
1082 fbuts[5].x = 2 + r2 / 2 + l2;
1083 fbuts[6].x = fbuts[5].x + fbuts[5].len + l2;
1084 fbuts[7].x = fbuts[6].x + fbuts[6].len + l2;
1087 find_dlg =
1088 create_dlg (0, 0, FIND2_Y, FIND2_X, dialog_colors, find_callback,
1089 "[Find File]", _("Find File"), DLG_CENTER | DLG_REVERSE);
1091 add_widget (find_dlg,
1092 button_new (FIND2_Y - 3, fbuts[7].x, B_VIEW, NORMAL_BUTTON,
1093 fbuts[7].text, find_do_edit_file));
1094 add_widget (find_dlg,
1095 button_new (FIND2_Y - 3, fbuts[6].x, B_VIEW, NORMAL_BUTTON,
1096 fbuts[6].text, find_do_view_file));
1097 add_widget (find_dlg,
1098 button_new (FIND2_Y - 3, fbuts[5].x, B_PANELIZE,
1099 NORMAL_BUTTON, fbuts[5].text, 0));
1101 add_widget (find_dlg,
1102 button_new (FIND2_Y - 4, fbuts[4].x, B_CANCEL,
1103 NORMAL_BUTTON, fbuts[4].text, 0));
1104 stop_button =
1105 button_new (FIND2_Y - 4, fbuts[0].x, B_STOP, NORMAL_BUTTON,
1106 fbuts[0].text, start_stop);
1107 add_widget (find_dlg, stop_button);
1108 add_widget (find_dlg,
1109 button_new (FIND2_Y - 4, fbuts[3].x, B_AGAIN,
1110 NORMAL_BUTTON, fbuts[3].text, 0));
1111 add_widget (find_dlg,
1112 button_new (FIND2_Y - 4, fbuts[2].x, B_ENTER,
1113 DEFPUSH_BUTTON, fbuts[2].text, 0));
1115 status_label = label_new (FIND2_Y - 7, 4, _("Searching"));
1116 add_widget (find_dlg, status_label);
1118 found_num_label = label_new (FIND2_Y - 6, 4, "");
1119 add_widget (find_dlg, found_num_label);
1121 find_list =
1122 listbox_new (2, 2, FIND2_Y - 10, FIND2_X - 4, NULL);
1123 add_widget (find_dlg, find_list);
1126 static int
1127 run_process (void)
1129 resuming = 0;
1130 set_idle_proc (find_dlg, 1);
1131 return run_dlg (find_dlg);
1134 static void
1135 kill_gui (void)
1137 set_idle_proc (find_dlg, 0);
1138 destroy_dlg (find_dlg);
1141 static int
1142 find_file (const char *start_dir, const char *pattern, const char *content,
1143 char **dirname, char **filename)
1145 int return_value = 0;
1146 char *dir_tmp = NULL, *file_tmp = NULL;
1148 setup_gui ();
1150 /* FIXME: Need to cleanup this, this ought to be passed non-globaly */
1151 find_pattern = str_unconst (pattern);
1152 content_pattern = (content != NULL && str_is_valid_string (content))
1153 ? g_strdup(content)
1154 : NULL;
1156 init_find_vars ();
1157 push_directory (start_dir);
1159 return_value = run_process ();
1161 /* Remove all the items in the stack */
1162 clear_stack ();
1164 get_list_info (&file_tmp, &dir_tmp);
1166 if (dir_tmp)
1167 *dirname = g_strdup (dir_tmp);
1168 if (file_tmp)
1169 *filename = g_strdup (file_tmp);
1171 if (return_value == B_PANELIZE && *filename) {
1172 int status, link_to_dir, stale_link;
1173 int next_free = 0;
1174 int i;
1175 struct stat st;
1176 WLEntry *entry = find_list->list;
1177 dir_list *list = &current_panel->dir;
1178 char *name = NULL;
1180 for (i = 0; entry != NULL && i < find_list->count;
1181 entry = entry->next, i++) {
1182 const char *lc_filename = NULL;
1184 if (!entry->text || !entry->data)
1185 continue;
1187 if (content_pattern != NULL)
1188 lc_filename = strchr (entry->text + 4, ':') + 1;
1189 else
1190 lc_filename = entry->text + 4;
1192 name = make_fullname (entry->data, lc_filename);
1193 status =
1194 handle_path (list, name, &st, next_free, &link_to_dir,
1195 &stale_link);
1196 if (status == 0) {
1197 g_free (name);
1198 continue;
1200 if (status == -1) {
1201 g_free (name);
1202 break;
1205 /* don't add files more than once to the panel */
1206 if (content_pattern != NULL && next_free > 0
1207 && strcmp (list->list[next_free - 1].fname, name) == 0) {
1208 g_free (name);
1209 continue;
1212 if (!next_free) /* first turn i.e clean old list */
1213 panel_clean_dir (current_panel);
1214 list->list[next_free].fnamelen = strlen (name);
1215 list->list[next_free].fname = name;
1216 list->list[next_free].f.marked = 0;
1217 list->list[next_free].f.link_to_dir = link_to_dir;
1218 list->list[next_free].f.stale_link = stale_link;
1219 list->list[next_free].f.dir_size_computed = 0;
1220 list->list[next_free].st = st;
1221 list->list[next_free].sort_key = NULL;
1222 list->list[next_free].second_sort_key = NULL;
1223 next_free++;
1224 if (!(next_free & 15))
1225 rotate_dash ();
1228 if (next_free) {
1229 current_panel->count = next_free;
1230 current_panel->is_panelized = 1;
1232 if (start_dir[0] == PATH_SEP) {
1233 strcpy (current_panel->cwd, PATH_SEP_STR);
1234 chdir (PATH_SEP_STR);
1239 g_free (content_pattern);
1240 kill_gui ();
1241 do_search (NULL); /* force do_search to release resources */
1242 g_free (old_dir);
1243 old_dir = NULL;
1245 return return_value;
1248 void
1249 do_find (void)
1251 char *start_dir = NULL, *pattern = NULL, *content = NULL;
1252 char *filename = NULL, *dirname = NULL;
1253 int v;
1254 gboolean dir_and_file_set;
1256 while (find_parameters (&start_dir, &pattern, &content)){
1257 if (pattern [0] == '\0')
1258 break; /* nothing search*/
1260 dirname = filename = NULL;
1261 is_start = FALSE;
1262 v = find_file (start_dir, pattern, content, &dirname, &filename);
1263 g_free (pattern);
1265 if (v == B_ENTER){
1266 if (dirname || filename){
1267 if (dirname){
1268 do_cd (dirname, cd_exact);
1269 if (filename)
1270 try_to_select (current_panel, filename + (content ?
1271 (strchr (filename + 4, ':') - filename + 1) : 4) );
1272 } else if (filename)
1273 do_cd (filename, cd_exact);
1274 select_item (current_panel);
1276 g_free (dirname);
1277 g_free (filename);
1278 break;
1281 g_free (content);
1282 dir_and_file_set = dirname && filename;
1283 g_free (dirname);
1284 g_free (filename);
1286 if (v == B_CANCEL)
1287 break;
1289 if (v == B_PANELIZE){
1290 if (dir_and_file_set){
1291 try_to_select (current_panel, NULL);
1292 panel_re_sort (current_panel);
1293 try_to_select (current_panel, NULL);
1295 break;