1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <sys/types.h>
22 #include <sys/param.h>
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbutton.h"
34 #include "gtkfilesel.h"
39 #include "gtklistitem.h"
41 #include "gtkscrolledwindow.h"
42 #include "gtksignal.h"
45 #include "gtkmenuitem.h"
46 #include "gtkoptionmenu.h"
48 #include "gtkdialog.h"
51 #define DIR_LIST_WIDTH 180
52 #define DIR_LIST_HEIGHT 180
53 #define FILE_LIST_WIDTH 180
54 #define FILE_LIST_HEIGHT 180
56 /* I've put this here so it doesn't get confused with the
57 * file completion interface */
58 typedef struct _HistoryCallbackArg HistoryCallbackArg;
60 struct _HistoryCallbackArg
67 typedef struct _CompletionState CompletionState;
68 typedef struct _CompletionDir CompletionDir;
69 typedef struct _CompletionDirSent CompletionDirSent;
70 typedef struct _CompletionDirEntry CompletionDirEntry;
71 typedef struct _CompletionUserDir CompletionUserDir;
72 typedef struct _PossibleCompletion PossibleCompletion;
74 /* Non-external file completion decls and structures */
76 /* A contant telling PRCS how many directories to cache. Its actually
77 * kept in a list, so the geometry isn't important. */
78 #define CMPL_DIRECTORY_CACHE_SIZE 10
80 /* A constant used to determine whether a substring was an exact
81 * match by first_diff_index()
83 #define PATTERN_MATCH -1
84 /* The arguments used by all fnmatch() calls below
86 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
88 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
90 /* This structure contains all the useful information about a directory
91 * for the purposes of filename completion. These structures are cached
92 * in the CompletionState struct. CompletionDir's are reference counted.
94 struct _CompletionDirSent
101 gchar *name_buffer; /* memory segment containing names of all entries */
103 struct _CompletionDirEntry *entries;
106 struct _CompletionDir
108 CompletionDirSent *sent;
113 struct _CompletionDir *cmpl_parent;
118 /* This structure contains pairs of directory entry names with a flag saying
119 * whether or not they are a valid directory. NOTE: This information is used
120 * to provide the caller with information about whether to update its completions
121 * or try to open a file. Since directories are cached by the directory mtime,
122 * a symlink which points to an invalid file (which will not be a directory),
123 * will not be reevaluated if that file is created, unless the containing
124 * directory is touched. I consider this case to be worth ignoring (josh).
126 struct _CompletionDirEntry
132 struct _CompletionUserDir
138 struct _PossibleCompletion
140 /* accessible fields, all are accessed externally by functions
144 gint is_a_completion;
152 struct _CompletionState
154 gint last_valid_char;
156 gint updated_text_len;
157 gint updated_text_alloc;
160 gchar *user_dir_name_buffer;
161 gint user_directories_len;
162 gchar *user_home_dir;
164 gchar *last_completion_text;
166 gint user_completion_index; /* if >= 0, currently completing ~user */
168 struct _CompletionDir *completion_dir; /* directory completing from */
169 struct _CompletionDir *active_completion_dir;
171 struct _PossibleCompletion the_completion;
173 struct _CompletionDir *reference_dir; /* initial directory */
175 GList* directory_storage;
176 GList* directory_sent_storage;
178 struct _CompletionUserDir *user_directories;
182 /* File completion functions which would be external, were they used
183 * outside of this file.
186 static CompletionState* cmpl_init_state (void);
187 static void cmpl_free_state (CompletionState *cmpl_state);
188 static gint cmpl_state_okay (CompletionState* cmpl_state);
189 static gchar* cmpl_strerror (gint);
191 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
192 gchar **remaining_text,
193 CompletionState *cmpl_state);
195 /* Returns a name for consideration, possibly a completion, this name
196 * will be invalid after the next call to cmpl_next_completion.
198 static char* cmpl_this_completion (PossibleCompletion*);
200 /* True if this completion matches the given text. Otherwise, this
201 * output can be used to have a list of non-completions.
203 static gint cmpl_is_a_completion (PossibleCompletion*);
205 /* True if the completion is a directory
207 static gint cmpl_is_directory (PossibleCompletion*);
209 /* Obtains the next completion, or NULL
211 static PossibleCompletion* cmpl_next_completion (CompletionState*);
213 /* Updating completions: the return value of cmpl_updated_text() will
214 * be text_to_complete completed as much as possible after the most
215 * recent call to cmpl_completion_matches. For the present
216 * application, this is the suggested replacement for the user's input
217 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
220 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
222 /* After updating, to see if the completion was a directory, call
223 * this. If it was, you should consider re-calling completion_matches.
225 static gint cmpl_updated_dir (CompletionState* cmpl_state);
227 /* Current location: if using file completion, return the current
228 * directory, from which file completion begins. More specifically,
229 * the cwd concatenated with all exact completions up to the last
230 * directory delimiter('/').
232 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
234 /* backing up: if cmpl_completion_matches returns NULL, you may query
235 * the index of the last completable character into cmpl_updated_text.
237 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
239 /* When the user selects a non-directory, call cmpl_completion_fullname
240 * to get the full name of the selected file.
242 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
245 /* Directory operations. */
246 static CompletionDir* open_ref_dir (gchar* text_to_complete,
247 gchar** remaining_text,
248 CompletionState* cmpl_state);
249 static gboolean check_dir (gchar *dir_name,
251 gboolean *stat_subdirs);
252 static CompletionDir* open_dir (gchar* dir_name,
253 CompletionState* cmpl_state);
254 static CompletionDir* open_user_dir (gchar* text_to_complete,
255 CompletionState *cmpl_state);
256 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
257 CompletionState *cmpl_state);
258 static CompletionDirSent* open_new_dir (gchar* dir_name,
260 gboolean stat_subdirs);
261 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
262 static gint correct_parent (CompletionDir* cmpl_dir,
264 static gchar* find_parent_dir_fullname (gchar* dirname);
265 static CompletionDir* attach_dir (CompletionDirSent* sent,
267 CompletionState *cmpl_state);
268 static void free_dir_sent (CompletionDirSent* sent);
269 static void free_dir (CompletionDir *dir);
270 static void prune_memory_usage(CompletionState *cmpl_state);
272 /* Completion operations */
273 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
274 CompletionState *cmpl_state);
275 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
276 static CompletionDir* find_completion_dir(gchar* text_to_complete,
277 gchar** remaining_text,
278 CompletionState* cmpl_state);
279 static PossibleCompletion* append_completion_text(gchar* text,
280 CompletionState* cmpl_state);
281 static gint get_pwdb(CompletionState* cmpl_state);
282 static gint first_diff_index(gchar* pat, gchar* text);
283 static gint compare_user_dir(const void* a, const void* b);
284 static gint compare_cmpl_dir(const void* a, const void* b);
285 static void update_cmpl(PossibleCompletion* poss,
286 CompletionState* cmpl_state);
288 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
289 static void gtk_file_selection_init (GtkFileSelection *filesel);
290 static void gtk_file_selection_destroy (GtkObject *object);
291 static gint gtk_file_selection_key_press (GtkWidget *widget,
295 static void gtk_file_selection_file_button (GtkWidget *widget,
298 GdkEventButton *bevent,
301 static void gtk_file_selection_dir_button (GtkWidget *widget,
304 GdkEventButton *bevent,
307 static void gtk_file_selection_populate (GtkFileSelection *fs,
310 static void gtk_file_selection_abort (GtkFileSelection *fs);
312 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
315 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
316 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
317 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
321 static GtkWindowClass *parent_class = NULL;
323 /* Saves errno when something cmpl does fails. */
324 static gint cmpl_errno;
327 gtk_file_selection_get_type (void)
329 static GtkType file_selection_type = 0;
331 if (!file_selection_type)
333 static const GtkTypeInfo filesel_info =
336 sizeof (GtkFileSelection),
337 sizeof (GtkFileSelectionClass),
338 (GtkClassInitFunc) gtk_file_selection_class_init,
339 (GtkObjectInitFunc) gtk_file_selection_init,
340 /* reserved_1 */ NULL,
341 /* reserved_2 */ NULL,
342 (GtkClassInitFunc) NULL,
345 file_selection_type = gtk_type_unique (GTK_TYPE_WINDOW, &filesel_info);
348 return file_selection_type;
352 gtk_file_selection_class_init (GtkFileSelectionClass *class)
354 GtkObjectClass *object_class;
356 object_class = (GtkObjectClass*) class;
358 parent_class = gtk_type_class (GTK_TYPE_WINDOW);
360 object_class->destroy = gtk_file_selection_destroy;
364 gtk_file_selection_init (GtkFileSelection *filesel)
366 GtkWidget *entry_vbox;
368 GtkWidget *list_hbox;
369 GtkWidget *confirm_area;
370 GtkWidget *pulldown_hbox;
371 GtkWidget *scrolled_win;
374 char *file_title [2];
376 filesel->cmpl_state = cmpl_init_state ();
378 /* The dialog-sized vertical box */
379 filesel->main_vbox = gtk_vbox_new (FALSE, 10);
380 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
381 gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
382 gtk_widget_show (filesel->main_vbox);
384 /* The horizontal box containing create, rename etc. buttons */
385 filesel->button_area = gtk_hbutton_box_new ();
386 gtk_button_box_set_layout(GTK_BUTTON_BOX(filesel->button_area), GTK_BUTTONBOX_START);
387 gtk_button_box_set_spacing(GTK_BUTTON_BOX(filesel->button_area), 0);
388 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
390 gtk_widget_show (filesel->button_area);
392 gtk_file_selection_show_fileop_buttons(filesel);
394 /* hbox for pulldown menu */
395 pulldown_hbox = gtk_hbox_new (TRUE, 5);
396 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
397 gtk_widget_show (pulldown_hbox);
400 filesel->history_pulldown = gtk_option_menu_new ();
401 gtk_widget_show (filesel->history_pulldown);
402 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
405 /* The horizontal box containing the directory and file listboxes */
406 list_hbox = gtk_hbox_new (FALSE, 5);
407 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
408 gtk_widget_show (list_hbox);
410 /* The directories clist */
412 dir_title[0] = _("Directories");
414 filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
415 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
416 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
417 (GtkSignalFunc) gtk_file_selection_dir_button,
419 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
421 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
422 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
423 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
424 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
425 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
426 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
427 gtk_widget_show (filesel->dir_list);
428 gtk_widget_show (scrolled_win);
430 /* The files clist */
431 file_title[0] = _("Files");
432 file_title[1] = NULL;
433 filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
434 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
435 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
436 (GtkSignalFunc) gtk_file_selection_file_button,
438 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
440 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
441 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
442 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
443 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
444 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
445 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
446 gtk_widget_show (filesel->file_list);
447 gtk_widget_show (scrolled_win);
449 /* action area for packing buttons into. */
450 filesel->action_area = gtk_hbox_new (TRUE, 0);
451 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
453 gtk_widget_show (filesel->action_area);
455 /* The OK/Cancel button area */
456 confirm_area = gtk_hbutton_box_new ();
457 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
458 gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
459 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
460 gtk_widget_show (confirm_area);
463 filesel->ok_button = gtk_button_new_with_label (_("OK"));
464 GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
465 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
466 gtk_widget_grab_default (filesel->ok_button);
467 gtk_widget_show (filesel->ok_button);
469 /* The Cancel button */
470 filesel->cancel_button = gtk_button_new_with_label (_("Cancel"));
471 GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
472 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
473 gtk_widget_show (filesel->cancel_button);
475 /* The selection entry widget */
476 entry_vbox = gtk_vbox_new (FALSE, 2);
477 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
478 gtk_widget_show (entry_vbox);
480 filesel->selection_text = label = gtk_label_new ("");
481 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
482 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
483 gtk_widget_show (label);
485 filesel->selection_entry = gtk_entry_new ();
486 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
487 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
488 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
489 (GtkSignalFunc) gtk_widget_grab_default,
490 GTK_OBJECT (filesel->ok_button));
491 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
492 (GtkSignalFunc) gtk_button_clicked,
493 GTK_OBJECT (filesel->ok_button));
494 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
495 gtk_widget_show (filesel->selection_entry);
497 if (!cmpl_state_okay (filesel->cmpl_state))
501 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
503 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
507 gtk_file_selection_populate (filesel, "", FALSE);
510 gtk_widget_grab_focus (filesel->selection_entry);
514 gtk_file_selection_new (const gchar *title)
516 GtkFileSelection *filesel;
518 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
519 gtk_window_set_title (GTK_WINDOW (filesel), title);
521 return GTK_WIDGET (filesel);
525 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
527 g_return_if_fail (filesel != NULL);
528 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
530 /* delete, create directory, and rename */
531 if (!filesel->fileop_c_dir)
533 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
534 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
535 (GtkSignalFunc) gtk_file_selection_create_dir,
537 gtk_box_pack_start (GTK_BOX (filesel->button_area),
538 filesel->fileop_c_dir, TRUE, TRUE, 0);
539 gtk_widget_show (filesel->fileop_c_dir);
542 if (!filesel->fileop_del_file)
544 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
545 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
546 (GtkSignalFunc) gtk_file_selection_delete_file,
548 gtk_box_pack_start (GTK_BOX (filesel->button_area),
549 filesel->fileop_del_file, TRUE, TRUE, 0);
550 gtk_widget_show (filesel->fileop_del_file);
553 if (!filesel->fileop_ren_file)
555 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
556 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
557 (GtkSignalFunc) gtk_file_selection_rename_file,
559 gtk_box_pack_start (GTK_BOX (filesel->button_area),
560 filesel->fileop_ren_file, TRUE, TRUE, 0);
561 gtk_widget_show (filesel->fileop_ren_file);
564 gtk_widget_queue_resize(GTK_WIDGET(filesel));
568 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
570 g_return_if_fail (filesel != NULL);
571 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
573 if (filesel->fileop_ren_file)
575 gtk_widget_destroy (filesel->fileop_ren_file);
576 filesel->fileop_ren_file = NULL;
579 if (filesel->fileop_del_file)
581 gtk_widget_destroy (filesel->fileop_del_file);
582 filesel->fileop_del_file = NULL;
585 if (filesel->fileop_c_dir)
587 gtk_widget_destroy (filesel->fileop_c_dir);
588 filesel->fileop_c_dir = NULL;
595 gtk_file_selection_set_filename (GtkFileSelection *filesel,
596 const gchar *filename)
598 char buf[MAXPATHLEN];
599 const char *name, *last_slash;
601 g_return_if_fail (filesel != NULL);
602 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
603 g_return_if_fail (filename != NULL);
605 last_slash = strrchr (filename, '/');
614 gint len = MIN (MAXPATHLEN - 1, last_slash - filename + 1);
616 strncpy (buf, filename, len);
619 name = last_slash + 1;
622 gtk_file_selection_populate (filesel, buf, FALSE);
624 if (filesel->selection_entry)
625 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
629 gtk_file_selection_get_filename (GtkFileSelection *filesel)
631 static char nothing[2] = "";
635 g_return_val_if_fail (filesel != NULL, nothing);
636 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
638 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
641 filename = cmpl_completion_fullname (text, filesel->cmpl_state);
649 gtk_file_selection_complete (GtkFileSelection *filesel,
650 const gchar *pattern)
652 g_return_if_fail (filesel != NULL);
653 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
654 g_return_if_fail (pattern != NULL);
656 if (filesel->selection_entry)
657 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
658 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
662 gtk_file_selection_destroy (GtkObject *object)
664 GtkFileSelection *filesel;
666 HistoryCallbackArg *callback_arg;
668 g_return_if_fail (object != NULL);
669 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
671 filesel = GTK_FILE_SELECTION (object);
673 if (filesel->fileop_dialog)
674 gtk_widget_destroy (filesel->fileop_dialog);
676 if (filesel->history_list)
678 list = filesel->history_list;
681 callback_arg = list->data;
682 g_free (callback_arg->directory);
683 g_free (callback_arg);
686 g_list_free (filesel->history_list);
687 filesel->history_list = NULL;
690 cmpl_free_state (filesel->cmpl_state);
691 filesel->cmpl_state = NULL;
693 if (GTK_OBJECT_CLASS (parent_class)->destroy)
694 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
697 /* Begin file operations callbacks */
700 gtk_file_selection_fileop_error (gchar *error_message)
707 g_return_if_fail (error_message != NULL);
710 dialog = gtk_dialog_new ();
712 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
713 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
716 gtk_window_set_title (GTK_WINDOW (dialog), _("Error"));
717 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
719 vbox = gtk_vbox_new(FALSE, 0);
720 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
721 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
723 gtk_widget_show(vbox);
725 label = gtk_label_new(error_message);
726 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
727 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
728 gtk_widget_show(label);
730 /* yes, we free it */
731 g_free (error_message);
734 button = gtk_button_new_with_label (_("Close"));
735 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
736 (GtkSignalFunc) gtk_widget_destroy,
738 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
739 button, TRUE, TRUE, 0);
740 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
741 gtk_widget_grab_default(button);
742 gtk_widget_show (button);
744 gtk_widget_show (dialog);
748 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
750 GtkFileSelection *fs = data;
752 g_return_if_fail (fs != NULL);
753 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
755 fs->fileop_dialog = NULL;
760 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
762 GtkFileSelection *fs = data;
767 CompletionState *cmpl_state;
769 g_return_if_fail (fs != NULL);
770 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
772 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
773 cmpl_state = (CompletionState*) fs->cmpl_state;
774 path = cmpl_reference_position (cmpl_state);
776 full_path = g_strconcat (path, "/", dirname, NULL);
777 if ( (mkdir (full_path, 0755) < 0) )
779 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
780 g_strerror(errno), NULL);
781 gtk_file_selection_fileop_error (buf);
785 gtk_widget_destroy (fs->fileop_dialog);
786 gtk_file_selection_populate (fs, "", FALSE);
790 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
792 GtkFileSelection *fs = data;
798 g_return_if_fail (fs != NULL);
799 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
801 if (fs->fileop_dialog)
805 fs->fileop_dialog = dialog = gtk_dialog_new ();
806 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
807 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
809 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
810 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
812 /* If file dialog is grabbed, grab option dialog */
813 /* When option dialog is closed, file dialog will be grabbed again */
814 if (GTK_WINDOW(fs)->modal)
815 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
817 vbox = gtk_vbox_new(FALSE, 0);
818 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
819 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
821 gtk_widget_show(vbox);
823 label = gtk_label_new(_("Directory name:"));
824 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
825 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
826 gtk_widget_show(label);
828 /* The directory entry widget */
829 fs->fileop_entry = gtk_entry_new ();
830 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
832 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
833 gtk_widget_show (fs->fileop_entry);
836 button = gtk_button_new_with_label (_("Create"));
837 gtk_signal_connect (GTK_OBJECT (button), "clicked",
838 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
840 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
841 button, TRUE, TRUE, 0);
842 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
843 gtk_widget_show(button);
845 button = gtk_button_new_with_label (_("Cancel"));
846 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
847 (GtkSignalFunc) gtk_widget_destroy,
849 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
850 button, TRUE, TRUE, 0);
851 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
852 gtk_widget_grab_default(button);
853 gtk_widget_show (button);
855 gtk_widget_show (dialog);
859 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
861 GtkFileSelection *fs = data;
862 CompletionState *cmpl_state;
867 g_return_if_fail (fs != NULL);
868 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
870 cmpl_state = (CompletionState*) fs->cmpl_state;
871 path = cmpl_reference_position (cmpl_state);
873 full_path = g_strconcat (path, "/", fs->fileop_file, NULL);
874 if ( (unlink (full_path) < 0) )
876 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
877 g_strerror(errno), NULL);
878 gtk_file_selection_fileop_error (buf);
882 gtk_widget_destroy (fs->fileop_dialog);
883 gtk_file_selection_populate (fs, "", FALSE);
887 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
889 GtkFileSelection *fs = data;
897 g_return_if_fail (fs != NULL);
898 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
900 if (fs->fileop_dialog)
903 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
904 if (strlen(filename) < 1)
907 fs->fileop_file = filename;
910 fs->fileop_dialog = dialog = gtk_dialog_new ();
911 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
912 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
914 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
915 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
917 /* If file dialog is grabbed, grab option dialog */
918 /* When option dialog is closed, file dialog will be grabbed again */
919 if (GTK_WINDOW(fs)->modal)
920 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
922 vbox = gtk_vbox_new(FALSE, 0);
923 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
924 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
926 gtk_widget_show(vbox);
928 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
929 label = gtk_label_new(buf);
930 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
931 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
932 gtk_widget_show(label);
936 button = gtk_button_new_with_label (_("Delete"));
937 gtk_signal_connect (GTK_OBJECT (button), "clicked",
938 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
940 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
941 button, TRUE, TRUE, 0);
942 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
943 gtk_widget_show(button);
945 button = gtk_button_new_with_label (_("Cancel"));
946 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
947 (GtkSignalFunc) gtk_widget_destroy,
949 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
950 button, TRUE, TRUE, 0);
951 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
952 gtk_widget_grab_default(button);
953 gtk_widget_show (button);
955 gtk_widget_show (dialog);
960 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
962 GtkFileSelection *fs = data;
968 CompletionState *cmpl_state;
970 g_return_if_fail (fs != NULL);
971 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
973 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
974 cmpl_state = (CompletionState*) fs->cmpl_state;
975 path = cmpl_reference_position (cmpl_state);
977 new_filename = g_strconcat (path, "/", file, NULL);
978 old_filename = g_strconcat (path, "/", fs->fileop_file, NULL);
980 if ( (rename (old_filename, new_filename)) < 0)
982 buf = g_strconcat ("Error renaming file \"", file, "\": ",
983 g_strerror(errno), NULL);
984 gtk_file_selection_fileop_error (buf);
986 g_free (new_filename);
987 g_free (old_filename);
989 gtk_widget_destroy (fs->fileop_dialog);
990 gtk_file_selection_populate (fs, "", FALSE);
994 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
996 GtkFileSelection *fs = data;
1003 g_return_if_fail (fs != NULL);
1004 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1006 if (fs->fileop_dialog)
1009 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1010 if (strlen(fs->fileop_file) < 1)
1014 fs->fileop_dialog = dialog = gtk_dialog_new ();
1015 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1016 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1018 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1019 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1021 /* If file dialog is grabbed, grab option dialog */
1022 /* When option dialog closed, file dialog will be grabbed again */
1023 if (GTK_WINDOW(fs)->modal)
1024 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1026 vbox = gtk_vbox_new(FALSE, 0);
1027 gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
1028 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1030 gtk_widget_show(vbox);
1032 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1033 label = gtk_label_new(buf);
1034 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1035 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1036 gtk_widget_show(label);
1039 /* New filename entry */
1040 fs->fileop_entry = gtk_entry_new ();
1041 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1043 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
1044 gtk_widget_show (fs->fileop_entry);
1046 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1047 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1048 0, strlen (fs->fileop_file));
1051 button = gtk_button_new_with_label (_("Rename"));
1052 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1053 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1055 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1056 button, TRUE, TRUE, 0);
1057 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1058 gtk_widget_show(button);
1060 button = gtk_button_new_with_label (_("Cancel"));
1061 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1062 (GtkSignalFunc) gtk_widget_destroy,
1064 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1065 button, TRUE, TRUE, 0);
1066 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1067 gtk_widget_grab_default(button);
1068 gtk_widget_show (button);
1070 gtk_widget_show (dialog);
1075 gtk_file_selection_key_press (GtkWidget *widget,
1079 GtkFileSelection *fs;
1082 g_return_val_if_fail (widget != NULL, FALSE);
1083 g_return_val_if_fail (event != NULL, FALSE);
1085 if (event->keyval == GDK_Tab)
1087 fs = GTK_FILE_SELECTION (user_data);
1088 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1090 text = g_strdup (text);
1092 gtk_file_selection_populate (fs, text, TRUE);
1096 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1106 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
1108 GtkFileSelection *fs = data;
1109 HistoryCallbackArg *callback_arg;
1112 g_return_if_fail (fs != NULL);
1113 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1115 list = fs->history_list;
1118 callback_arg = list->data;
1120 if (callback_arg->menu_item == widget)
1122 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1131 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1132 gchar *current_directory)
1134 HistoryCallbackArg *callback_arg;
1135 GtkWidget *menu_item;
1141 g_return_if_fail (fs != NULL);
1142 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1143 g_return_if_fail (current_directory != NULL);
1145 list = fs->history_list;
1147 if (fs->history_menu)
1150 callback_arg = list->data;
1151 g_free (callback_arg->directory);
1152 g_free (callback_arg);
1155 g_list_free (fs->history_list);
1156 fs->history_list = NULL;
1158 gtk_widget_destroy (fs->history_menu);
1161 fs->history_menu = gtk_menu_new();
1163 current_dir = g_strdup (current_directory);
1165 dir_len = strlen (current_dir);
1167 for (i = dir_len; i >= 0; i--)
1169 /* the i == dir_len is to catch the full path for the first
1171 if ( (current_dir[i] == '/') || (i == dir_len))
1173 /* another small hack to catch the full path */
1175 current_dir[i + 1] = '\0';
1176 menu_item = gtk_menu_item_new_with_label (current_dir);
1178 callback_arg = g_new (HistoryCallbackArg, 1);
1179 callback_arg->menu_item = menu_item;
1181 /* since the autocompletion gets confused if you don't
1182 * supply a trailing '/' on a dir entry, set the full
1183 * (current) path to "" which just refreshes the filesel */
1185 callback_arg->directory = g_strdup ("");
1187 callback_arg->directory = g_strdup (current_dir);
1190 fs->history_list = g_list_append (fs->history_list, callback_arg);
1192 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1193 (GtkSignalFunc) gtk_file_selection_history_callback,
1195 gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
1196 gtk_widget_show (menu_item);
1200 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1202 g_free (current_dir);
1206 gtk_file_selection_file_button (GtkWidget *widget,
1209 GdkEventButton *bevent,
1212 GtkFileSelection *fs = NULL;
1213 gchar *filename, *temp = NULL;
1215 g_return_if_fail (GTK_IS_CLIST (widget));
1218 g_return_if_fail (fs != NULL);
1219 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1221 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1222 filename = g_strdup (temp);
1227 switch (bevent->type)
1229 case GDK_2BUTTON_PRESS:
1230 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1234 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1238 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1245 gtk_file_selection_dir_button (GtkWidget *widget,
1248 GdkEventButton *bevent,
1251 GtkFileSelection *fs = NULL;
1252 gchar *filename, *temp = NULL;
1254 g_return_if_fail (GTK_IS_CLIST (widget));
1256 fs = GTK_FILE_SELECTION (user_data);
1257 g_return_if_fail (fs != NULL);
1258 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1260 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1261 filename = g_strdup (temp);
1266 switch (bevent->type)
1268 case GDK_2BUTTON_PRESS:
1269 gtk_file_selection_populate (fs, filename, FALSE);
1273 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1277 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1284 gtk_file_selection_populate (GtkFileSelection *fs,
1288 CompletionState *cmpl_state;
1289 PossibleCompletion* poss;
1292 gchar* rem_path = rel_path;
1295 gint did_recurse = FALSE;
1296 gint possible_count = 0;
1297 gint selection_index = -1;
1298 gint file_list_width;
1299 gint dir_list_width;
1301 g_return_if_fail (fs != NULL);
1302 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1304 cmpl_state = (CompletionState*) fs->cmpl_state;
1305 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1307 if (!cmpl_state_okay (cmpl_state))
1309 /* Something went wrong. */
1310 gtk_file_selection_abort (fs);
1314 g_assert (cmpl_state->reference_dir);
1316 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1317 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1318 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1319 gtk_clist_clear (GTK_CLIST (fs->file_list));
1321 /* Set the dir_list to include ./ and ../ */
1324 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1327 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1329 /*reset the max widths of the lists*/
1330 dir_list_width = gdk_string_width(fs->dir_list->style->font,"../");
1331 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,dir_list_width);
1332 file_list_width = 1;
1333 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,file_list_width);
1337 if (cmpl_is_a_completion (poss))
1339 possible_count += 1;
1341 filename = cmpl_this_completion (poss);
1345 if (cmpl_is_directory (poss))
1347 if (strcmp (filename, "./") != 0 &&
1348 strcmp (filename, "../") != 0)
1350 int width = gdk_string_width(fs->dir_list->style->font,
1352 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1353 if(width > dir_list_width)
1355 dir_list_width = width;
1356 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,
1363 int width = gdk_string_width(fs->file_list->style->font,
1365 row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
1366 if(width > file_list_width)
1368 file_list_width = width;
1369 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
1375 poss = cmpl_next_completion (cmpl_state);
1378 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1379 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1381 /* File lists are set. */
1383 g_assert (cmpl_state->reference_dir);
1388 /* User is trying to complete filenames, so advance the user's input
1389 * string to the updated_text, which is the common leading substring
1390 * of all possible completions, and if its a directory attempt
1391 * attempt completions in it. */
1393 if (cmpl_updated_text (cmpl_state)[0])
1396 if (cmpl_updated_dir (cmpl_state))
1398 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1402 gtk_file_selection_populate (fs, dir_name, TRUE);
1408 if (fs->selection_entry)
1409 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1410 cmpl_updated_text (cmpl_state));
1415 selection_index = cmpl_last_valid_char (cmpl_state) -
1416 (strlen (rel_path) - strlen (rem_path));
1417 if (fs->selection_entry)
1418 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1423 if (fs->selection_entry)
1424 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1429 if (fs->selection_entry)
1430 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1432 if (fs->selection_entry)
1434 sel_text = g_new (char, strlen (cmpl_reference_position (cmpl_state)) +
1435 sizeof ("Selection: "));
1436 strcpy (sel_text, "Selection: ");
1437 strcat (sel_text, cmpl_reference_position (cmpl_state));
1439 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1443 if (fs->history_pulldown)
1445 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1452 gtk_file_selection_abort (GtkFileSelection *fs)
1456 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1458 /* BEEP gdk_beep(); */
1460 if (fs->selection_entry)
1461 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1464 /**********************************************************************/
1465 /* External Interface */
1466 /**********************************************************************/
1468 /* The four completion state selectors
1471 cmpl_updated_text (CompletionState* cmpl_state)
1473 return cmpl_state->updated_text;
1477 cmpl_updated_dir (CompletionState* cmpl_state)
1479 return cmpl_state->re_complete;
1483 cmpl_reference_position (CompletionState* cmpl_state)
1485 return cmpl_state->reference_dir->fullname;
1489 cmpl_last_valid_char (CompletionState* cmpl_state)
1491 return cmpl_state->last_valid_char;
1495 cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
1497 static char nothing[2] = "";
1499 if (!cmpl_state_okay (cmpl_state))
1503 else if (text[0] == '/')
1505 strcpy (cmpl_state->updated_text, text);
1507 else if (text[0] == '~')
1512 dir = open_user_dir (text, cmpl_state);
1516 /* spencer says just return ~something, so
1517 * for now just do it. */
1518 strcpy (cmpl_state->updated_text, text);
1523 strcpy (cmpl_state->updated_text, dir->fullname);
1525 slash = strchr (text, '/');
1528 strcat (cmpl_state->updated_text, slash);
1533 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1534 strcat (cmpl_state->updated_text, "/");
1535 strcat (cmpl_state->updated_text, text);
1538 return cmpl_state->updated_text;
1541 /* The three completion selectors
1544 cmpl_this_completion (PossibleCompletion* pc)
1550 cmpl_is_directory (PossibleCompletion* pc)
1552 return pc->is_directory;
1556 cmpl_is_a_completion (PossibleCompletion* pc)
1558 return pc->is_a_completion;
1561 /**********************************************************************/
1562 /* Construction, deletion */
1563 /**********************************************************************/
1565 static CompletionState*
1566 cmpl_init_state (void)
1568 gchar getcwd_buf[2*MAXPATHLEN];
1569 CompletionState *new_state;
1571 new_state = g_new (CompletionState, 1);
1573 /* We don't use getcwd() on SUNOS, because, it does a popen("pwd")
1574 * and, if that wasn't bad enough, hangs in doing so.
1576 #if defined(sun) && !defined(__SVR4)
1577 if (!getwd (getcwd_buf))
1579 if (!getcwd (getcwd_buf, MAXPATHLEN))
1582 /* Oh joy, we can't get the current directory. Um..., we should have
1583 * a root directory, right? Right? (Probably not portable to non-Unix)
1585 strcpy (getcwd_buf, "/");
1590 new_state->reference_dir = NULL;
1591 new_state->completion_dir = NULL;
1592 new_state->active_completion_dir = NULL;
1594 if ((new_state->user_home_dir = getenv("HOME")) != NULL)
1596 /* if this fails, get_pwdb will fill it in. */
1597 new_state->user_home_dir = g_strdup(new_state->user_home_dir);
1600 new_state->directory_storage = NULL;
1601 new_state->directory_sent_storage = NULL;
1602 new_state->last_valid_char = 0;
1603 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1604 new_state->updated_text_alloc = MAXPATHLEN;
1605 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1606 new_state->the_completion.text_alloc = MAXPATHLEN;
1607 new_state->user_dir_name_buffer = NULL;
1608 new_state->user_directories = NULL;
1610 new_state->reference_dir = open_dir (getcwd_buf, new_state);
1612 if (!new_state->reference_dir)
1614 /* Directories changing from underneath us, grumble */
1615 strcpy (getcwd_buf, "/");
1623 cmpl_free_dir_list(GList* dp0)
1628 free_dir (dp->data);
1636 cmpl_free_dir_sent_list(GList* dp0)
1641 free_dir_sent (dp->data);
1649 cmpl_free_state (CompletionState* cmpl_state)
1651 cmpl_free_dir_list (cmpl_state->directory_storage);
1652 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1654 if (cmpl_state->user_dir_name_buffer)
1655 g_free (cmpl_state->user_dir_name_buffer);
1656 if (cmpl_state->user_home_dir)
1657 g_free (cmpl_state->user_home_dir);
1658 if (cmpl_state->user_directories)
1659 g_free (cmpl_state->user_directories);
1660 if (cmpl_state->the_completion.text)
1661 g_free (cmpl_state->the_completion.text);
1662 if (cmpl_state->updated_text)
1663 g_free (cmpl_state->updated_text);
1665 g_free (cmpl_state);
1669 free_dir(CompletionDir* dir)
1671 g_free(dir->fullname);
1676 free_dir_sent(CompletionDirSent* sent)
1678 g_free(sent->name_buffer);
1679 g_free(sent->entries);
1684 prune_memory_usage(CompletionState *cmpl_state)
1686 GList* cdsl = cmpl_state->directory_sent_storage;
1687 GList* cdl = cmpl_state->directory_storage;
1691 for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1695 cmpl_free_dir_sent_list(cdsl->next);
1699 cmpl_state->directory_storage = NULL;
1701 if (cdl->data == cmpl_state->reference_dir)
1702 cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
1704 free_dir (cdl->data);
1711 /**********************************************************************/
1712 /* The main entrances. */
1713 /**********************************************************************/
1715 static PossibleCompletion*
1716 cmpl_completion_matches (gchar* text_to_complete,
1717 gchar** remaining_text,
1718 CompletionState* cmpl_state)
1721 PossibleCompletion *poss;
1723 prune_memory_usage(cmpl_state);
1725 g_assert (text_to_complete != NULL);
1727 cmpl_state->user_completion_index = -1;
1728 cmpl_state->last_completion_text = text_to_complete;
1729 cmpl_state->the_completion.text[0] = 0;
1730 cmpl_state->last_valid_char = 0;
1731 cmpl_state->updated_text_len = -1;
1732 cmpl_state->updated_text[0] = 0;
1733 cmpl_state->re_complete = FALSE;
1735 first_slash = strchr (text_to_complete, '/');
1737 if (text_to_complete[0] == '~' && !first_slash)
1739 /* Text starts with ~ and there is no slash, show all the
1740 * home directory completions.
1742 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
1744 update_cmpl(poss, cmpl_state);
1749 cmpl_state->reference_dir =
1750 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
1752 if(!cmpl_state->reference_dir)
1755 cmpl_state->completion_dir =
1756 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
1758 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
1760 if(!cmpl_state->completion_dir)
1763 cmpl_state->completion_dir->cmpl_index = -1;
1764 cmpl_state->completion_dir->cmpl_parent = NULL;
1765 cmpl_state->completion_dir->cmpl_text = *remaining_text;
1767 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
1769 cmpl_state->reference_dir = cmpl_state->completion_dir;
1771 poss = attempt_file_completion(cmpl_state);
1773 update_cmpl(poss, cmpl_state);
1778 static PossibleCompletion*
1779 cmpl_next_completion (CompletionState* cmpl_state)
1781 PossibleCompletion* poss = NULL;
1783 cmpl_state->the_completion.text[0] = 0;
1785 if(cmpl_state->user_completion_index >= 0)
1786 poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
1788 poss = attempt_file_completion(cmpl_state);
1790 update_cmpl(poss, cmpl_state);
1795 /**********************************************************************/
1796 /* Directory Operations */
1797 /**********************************************************************/
1799 /* Open the directory where completion will begin from, if possible. */
1800 static CompletionDir*
1801 open_ref_dir(gchar* text_to_complete,
1802 gchar** remaining_text,
1803 CompletionState* cmpl_state)
1806 CompletionDir *new_dir;
1808 first_slash = strchr(text_to_complete, '/');
1810 if (text_to_complete[0] == '/' || !cmpl_state->reference_dir)
1812 new_dir = open_dir("/", cmpl_state);
1815 *remaining_text = text_to_complete + 1;
1817 else if (text_to_complete[0] == '~')
1819 new_dir = open_user_dir(text_to_complete, cmpl_state);
1824 *remaining_text = first_slash + 1;
1826 *remaining_text = text_to_complete + strlen(text_to_complete);
1835 *remaining_text = text_to_complete;
1837 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
1842 new_dir->cmpl_index = -1;
1843 new_dir->cmpl_parent = NULL;
1849 /* open a directory by user name */
1850 static CompletionDir*
1851 open_user_dir(gchar* text_to_complete,
1852 CompletionState *cmpl_state)
1857 g_assert(text_to_complete && text_to_complete[0] == '~');
1859 first_slash = strchr(text_to_complete, '/');
1862 cmp_len = first_slash - text_to_complete - 1;
1864 cmp_len = strlen(text_to_complete + 1);
1869 if (!cmpl_state->user_home_dir &&
1870 !get_pwdb(cmpl_state))
1872 return open_dir(cmpl_state->user_home_dir, cmpl_state);
1877 char* copy = g_new(char, cmp_len + 1);
1879 strncpy(copy, text_to_complete + 1, cmp_len);
1881 pwd = getpwnam(copy);
1889 return open_dir(pwd->pw_dir, cmpl_state);
1893 /* open a directory relative the the current relative directory */
1894 static CompletionDir*
1895 open_relative_dir(gchar* dir_name,
1897 CompletionState *cmpl_state)
1899 gchar path_buf[2*MAXPATHLEN];
1901 if(dir->fullname_len + strlen(dir_name) + 2 >= MAXPATHLEN)
1903 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1907 strcpy(path_buf, dir->fullname);
1909 if(dir->fullname_len > 1)
1911 path_buf[dir->fullname_len] = '/';
1912 strcpy(path_buf + dir->fullname_len + 1, dir_name);
1916 strcpy(path_buf + dir->fullname_len, dir_name);
1919 return open_dir(path_buf, cmpl_state);
1922 /* after the cache lookup fails, really open a new directory */
1923 static CompletionDirSent*
1924 open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
1926 CompletionDirSent* sent;
1929 struct dirent *dirent_ptr;
1930 gint buffer_size = 0;
1931 gint entry_count = 0;
1933 struct stat ent_sbuf;
1934 char path_buf[MAXPATHLEN*2];
1937 sent = g_new(CompletionDirSent, 1);
1938 sent->mtime = sbuf->st_mtime;
1939 sent->inode = sbuf->st_ino;
1940 sent->device = sbuf->st_dev;
1942 path_buf_len = strlen(dir_name);
1944 if (path_buf_len > MAXPATHLEN)
1946 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1950 strcpy(path_buf, dir_name);
1952 directory = opendir(dir_name);
1960 while((dirent_ptr = readdir(directory)) != NULL)
1962 int entry_len = strlen(dirent_ptr->d_name);
1963 buffer_size += entry_len + 1;
1966 if(path_buf_len + entry_len + 2 >= MAXPATHLEN)
1968 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1969 closedir(directory);
1974 sent->name_buffer = g_new(gchar, buffer_size);
1975 sent->entries = g_new(CompletionDirEntry, entry_count);
1976 sent->entry_count = entry_count;
1978 buffer_ptr = sent->name_buffer;
1980 rewinddir(directory);
1982 for(i = 0; i < entry_count; i += 1)
1984 dirent_ptr = readdir(directory);
1989 closedir(directory);
1993 strcpy(buffer_ptr, dirent_ptr->d_name);
1994 sent->entries[i].entry_name = buffer_ptr;
1995 buffer_ptr += strlen(dirent_ptr->d_name);
1999 path_buf[path_buf_len] = '/';
2000 strcpy(path_buf + path_buf_len + 1, dirent_ptr->d_name);
2004 if(stat(path_buf, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
2005 sent->entries[i].is_dir = 1;
2007 /* stat may fail, and we don't mind, since it could be a
2008 * dangling symlink. */
2009 sent->entries[i].is_dir = 0;
2012 sent->entries[i].is_dir = 1;
2015 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
2017 closedir(directory);
2023 check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
2025 /* A list of directories that we know only contain other directories.
2026 * Trying to stat every file in these directories would be very
2033 struct stat statbuf;
2034 } no_stat_dirs[] = {
2035 { "/afs", FALSE, { 0 } },
2036 { "/net", FALSE, { 0 } }
2039 static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
2040 static gboolean initialized = FALSE;
2047 for (i = 0; i < n_no_stat_dirs; i++)
2049 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2050 no_stat_dirs[i].present = TRUE;
2054 if(stat(dir_name, result) < 0)
2060 *stat_subdirs = TRUE;
2061 for (i=0; i<n_no_stat_dirs; i++)
2063 if (no_stat_dirs[i].present &&
2064 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2065 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2067 *stat_subdirs = FALSE;
2075 /* open a directory by absolute pathname */
2076 static CompletionDir*
2077 open_dir(gchar* dir_name, CompletionState* cmpl_state)
2080 gboolean stat_subdirs;
2081 CompletionDirSent *sent;
2084 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2087 cdsl = cmpl_state->directory_sent_storage;
2093 if(sent->inode == sbuf.st_ino &&
2094 sent->mtime == sbuf.st_mtime &&
2095 sent->device == sbuf.st_dev)
2096 return attach_dir(sent, dir_name, cmpl_state);
2101 sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
2104 cmpl_state->directory_sent_storage =
2105 g_list_prepend(cmpl_state->directory_sent_storage, sent);
2107 return attach_dir(sent, dir_name, cmpl_state);
2113 static CompletionDir*
2114 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
2116 CompletionDir* new_dir;
2118 new_dir = g_new(CompletionDir, 1);
2120 cmpl_state->directory_storage =
2121 g_list_prepend(cmpl_state->directory_storage, new_dir);
2123 new_dir->sent = sent;
2124 new_dir->fullname = g_strdup(dir_name);
2125 new_dir->fullname_len = strlen(dir_name);
2131 correct_dir_fullname(CompletionDir* cmpl_dir)
2133 gint length = strlen(cmpl_dir->fullname);
2136 if (strcmp(cmpl_dir->fullname + length - 2, "/.") == 0)
2140 strcpy(cmpl_dir->fullname, "/");
2141 cmpl_dir->fullname_len = 1;
2144 cmpl_dir->fullname[length - 2] = 0;
2147 else if (strcmp(cmpl_dir->fullname + length - 3, "/./") == 0)
2148 cmpl_dir->fullname[length - 2] = 0;
2149 else if (strcmp(cmpl_dir->fullname + length - 3, "/..") == 0)
2153 strcpy(cmpl_dir->fullname, "/");
2154 cmpl_dir->fullname_len = 1;
2158 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2164 cmpl_dir->fullname[length - 2] = 0;
2166 if(!correct_parent(cmpl_dir, &sbuf))
2169 else if (strcmp(cmpl_dir->fullname + length - 4, "/../") == 0)
2173 strcpy(cmpl_dir->fullname, "/");
2174 cmpl_dir->fullname_len = 1;
2178 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2184 cmpl_dir->fullname[length - 3] = 0;
2186 if(!correct_parent(cmpl_dir, &sbuf))
2190 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
2196 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
2203 last_slash = strrchr(cmpl_dir->fullname, '/');
2205 g_assert(last_slash);
2207 if(last_slash != cmpl_dir->fullname)
2208 { /* last_slash[0] = 0; */ }
2215 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2221 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2222 /* it wasn't a link */
2228 last_slash[0] = '/'; */
2230 /* it was a link, have to figure it out the hard way */
2232 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2237 g_free(cmpl_dir->fullname);
2239 cmpl_dir->fullname = new_name;
2245 find_parent_dir_fullname(gchar* dirname)
2247 gchar buffer[MAXPATHLEN];
2248 gchar buffer2[MAXPATHLEN];
2250 #if defined(sun) && !defined(__SVR4)
2253 if(!getcwd(buffer, MAXPATHLEN))
2260 if(chdir(dirname) != 0 || chdir("..") != 0)
2266 #if defined(sun) && !defined(__SVR4)
2269 if(!getcwd(buffer2, MAXPATHLEN))
2278 if(chdir(buffer) != 0)
2284 return g_strdup(buffer2);
2287 /**********************************************************************/
2288 /* Completion Operations */
2289 /**********************************************************************/
2291 static PossibleCompletion*
2292 attempt_homedir_completion(gchar* text_to_complete,
2293 CompletionState *cmpl_state)
2297 if (!cmpl_state->user_dir_name_buffer &&
2298 !get_pwdb(cmpl_state))
2300 length = strlen(text_to_complete) - 1;
2302 cmpl_state->user_completion_index += 1;
2304 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2306 index = first_diff_index(text_to_complete + 1,
2307 cmpl_state->user_directories
2308 [cmpl_state->user_completion_index].login);
2315 if(cmpl_state->last_valid_char < (index + 1))
2316 cmpl_state->last_valid_char = index + 1;
2317 cmpl_state->user_completion_index += 1;
2321 cmpl_state->the_completion.is_a_completion = 1;
2322 cmpl_state->the_completion.is_directory = 1;
2324 append_completion_text("~", cmpl_state);
2326 append_completion_text(cmpl_state->
2327 user_directories[cmpl_state->user_completion_index].login,
2330 return append_completion_text("/", cmpl_state);
2333 if(text_to_complete[1] ||
2334 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2336 cmpl_state->user_completion_index = -1;
2341 cmpl_state->user_completion_index += 1;
2342 cmpl_state->the_completion.is_a_completion = 1;
2343 cmpl_state->the_completion.is_directory = 1;
2345 return append_completion_text("~/", cmpl_state);
2349 /* returns the index (>= 0) of the first differing character,
2350 * PATTERN_MATCH if the completion matches */
2352 first_diff_index(gchar* pat, gchar* text)
2356 while(*pat && *text && *text == *pat)
2366 return PATTERN_MATCH;
2369 static PossibleCompletion*
2370 append_completion_text(gchar* text, CompletionState* cmpl_state)
2374 if(!cmpl_state->the_completion.text)
2377 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2379 if(cmpl_state->the_completion.text_alloc > len)
2381 strcat(cmpl_state->the_completion.text, text);
2382 return &cmpl_state->the_completion;
2385 while(i < len) { i <<= 1; }
2387 cmpl_state->the_completion.text_alloc = i;
2389 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2391 if(!cmpl_state->the_completion.text)
2395 strcat(cmpl_state->the_completion.text, text);
2396 return &cmpl_state->the_completion;
2400 static CompletionDir*
2401 find_completion_dir(gchar* text_to_complete,
2402 gchar** remaining_text,
2403 CompletionState* cmpl_state)
2405 gchar* first_slash = strchr(text_to_complete, '/');
2406 CompletionDir* dir = cmpl_state->reference_dir;
2407 CompletionDir* next;
2408 *remaining_text = text_to_complete;
2412 gint len = first_slash - *remaining_text;
2414 gchar *found_name = NULL; /* Quiet gcc */
2416 gchar* pat_buf = g_new (gchar, len + 1);
2418 strncpy(pat_buf, *remaining_text, len);
2421 for(i = 0; i < dir->sent->entry_count; i += 1)
2423 if(dir->sent->entries[i].is_dir &&
2424 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2425 FNMATCH_FLAGS)!= FNM_NOMATCH)
2435 found_name = dir->sent->entries[i].entry_name;
2442 /* Perhaps we are trying to open an automount directory */
2443 found_name = pat_buf;
2446 next = open_relative_dir(found_name, dir, cmpl_state);
2454 next->cmpl_parent = dir;
2458 if(!correct_dir_fullname(dir))
2464 *remaining_text = first_slash + 1;
2465 first_slash = strchr(*remaining_text, '/');
2474 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2478 if(!poss || !cmpl_is_a_completion(poss))
2481 cmpl_len = strlen(cmpl_this_completion(poss));
2483 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2485 cmpl_state->updated_text =
2486 (gchar*)g_realloc(cmpl_state->updated_text,
2487 cmpl_state->updated_text_alloc);
2488 cmpl_state->updated_text_alloc = 2*cmpl_len;
2491 if(cmpl_state->updated_text_len < 0)
2493 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2494 cmpl_state->updated_text_len = cmpl_len;
2495 cmpl_state->re_complete = cmpl_is_directory(poss);
2497 else if(cmpl_state->updated_text_len == 0)
2499 cmpl_state->re_complete = FALSE;
2504 first_diff_index(cmpl_state->updated_text,
2505 cmpl_this_completion(poss));
2507 cmpl_state->re_complete = FALSE;
2509 if(first_diff == PATTERN_MATCH)
2512 if(first_diff > cmpl_state->updated_text_len)
2513 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2515 cmpl_state->updated_text_len = first_diff;
2516 cmpl_state->updated_text[first_diff] = 0;
2520 static PossibleCompletion*
2521 attempt_file_completion(CompletionState *cmpl_state)
2523 gchar *pat_buf, *first_slash;
2524 CompletionDir *dir = cmpl_state->active_completion_dir;
2526 dir->cmpl_index += 1;
2528 if(dir->cmpl_index == dir->sent->entry_count)
2530 if(dir->cmpl_parent == NULL)
2532 cmpl_state->active_completion_dir = NULL;
2538 cmpl_state->active_completion_dir = dir->cmpl_parent;
2540 return attempt_file_completion(cmpl_state);
2544 g_assert(dir->cmpl_text);
2546 first_slash = strchr(dir->cmpl_text, '/');
2550 gint len = first_slash - dir->cmpl_text;
2552 pat_buf = g_new (gchar, len + 1);
2553 strncpy(pat_buf, dir->cmpl_text, len);
2558 gint len = strlen(dir->cmpl_text);
2560 pat_buf = g_new (gchar, len + 2);
2561 strcpy(pat_buf, dir->cmpl_text);
2562 strcpy(pat_buf + len, "*");
2567 if(dir->sent->entries[dir->cmpl_index].is_dir)
2569 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2570 FNMATCH_FLAGS) != FNM_NOMATCH)
2572 CompletionDir* new_dir;
2574 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2583 new_dir->cmpl_parent = dir;
2585 new_dir->cmpl_index = -1;
2586 new_dir->cmpl_text = first_slash + 1;
2588 cmpl_state->active_completion_dir = new_dir;
2591 return attempt_file_completion(cmpl_state);
2596 return attempt_file_completion(cmpl_state);
2602 return attempt_file_completion(cmpl_state);
2607 if(dir->cmpl_parent != NULL)
2609 append_completion_text(dir->fullname +
2610 strlen(cmpl_state->completion_dir->fullname) + 1,
2612 append_completion_text("/", cmpl_state);
2615 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2617 cmpl_state->the_completion.is_a_completion =
2618 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2619 FNMATCH_FLAGS) != FNM_NOMATCH);
2621 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2622 if(dir->sent->entries[dir->cmpl_index].is_dir)
2623 append_completion_text("/", cmpl_state);
2626 return &cmpl_state->the_completion;
2632 get_pwdb(CompletionState* cmpl_state)
2634 struct passwd *pwd_ptr;
2636 gint len = 0, i, count = 0;
2638 if(cmpl_state->user_dir_name_buffer)
2642 while ((pwd_ptr = getpwent()) != NULL)
2644 len += strlen(pwd_ptr->pw_name);
2645 len += strlen(pwd_ptr->pw_dir);
2650 if (!cmpl_state->user_home_dir)
2652 /* the loser doesn't have $HOME set */
2655 pwd_ptr = getpwuid(getuid());
2661 /* Allocate this separately, since it might be filled in elsewhere */
2662 cmpl_state->user_home_dir = g_strdup (pwd_ptr->pw_dir);
2667 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2668 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2669 cmpl_state->user_directories_len = count;
2671 buf_ptr = cmpl_state->user_dir_name_buffer;
2673 for(i = 0; i < count; i += 1)
2675 pwd_ptr = getpwent();
2682 strcpy(buf_ptr, pwd_ptr->pw_name);
2683 cmpl_state->user_directories[i].login = buf_ptr;
2684 buf_ptr += strlen(buf_ptr);
2686 strcpy(buf_ptr, pwd_ptr->pw_dir);
2687 cmpl_state->user_directories[i].homedir = buf_ptr;
2688 buf_ptr += strlen(buf_ptr);
2692 qsort(cmpl_state->user_directories,
2693 cmpl_state->user_directories_len,
2694 sizeof(CompletionUserDir),
2703 if(cmpl_state->user_dir_name_buffer)
2704 g_free(cmpl_state->user_dir_name_buffer);
2705 if(cmpl_state->user_directories)
2706 g_free(cmpl_state->user_directories);
2708 cmpl_state->user_dir_name_buffer = NULL;
2709 cmpl_state->user_directories = NULL;
2715 compare_user_dir(const void* a, const void* b)
2717 return strcmp((((CompletionUserDir*)a))->login,
2718 (((CompletionUserDir*)b))->login);
2722 compare_cmpl_dir(const void* a, const void* b)
2724 return strcmp((((CompletionDirEntry*)a))->entry_name,
2725 (((CompletionDirEntry*)b))->entry_name);
2729 cmpl_state_okay(CompletionState* cmpl_state)
2731 return cmpl_state && cmpl_state->reference_dir;
2735 cmpl_strerror(gint err)
2737 if(err == CMPL_ERRNO_TOO_LONG)
2738 return "Name too long";
2740 return g_strerror (err);