2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
24 #include "lib/debug.h"
28 #include <sys/types.h>
32 /* given a partial pathname, this routine fills in as much of the rest of the
35 * return 0 if ok, -1 if no matches
37 int mutt_complete (char *s, size_t slen)
44 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
45 char filepart[_POSIX_PATH_MAX];
48 char imap_path[LONG_STRING];
51 debug_print (2, ("completing %s\n", s));
54 if (option (OPTNEWS)) {
55 LIST *l = CurrentNewsSrv->list;
57 strfcpy (filepart, s, sizeof (filepart));
60 * special case to handle when there is no filepart yet.
61 * find the first subscribed newsgroup
63 if ((len = str_len (filepart)) == 0) {
64 for (; l; l = l->next) {
65 NNTP_DATA *data = (NNTP_DATA *) l->data;
67 if (data && data->subscribed) {
68 strfcpy (filepart, data->group, sizeof (filepart));
76 for (; l; l = l->next) {
77 NNTP_DATA *data = (NNTP_DATA *) l->data;
79 if (data && data->subscribed &&
80 str_ncmp (data->group, filepart, len) == 0) {
82 for (i = 0; filepart[i] && data->group[i]; i++) {
83 if (filepart[i] != data->group[i]) {
91 strfcpy (filepart, data->group, sizeof (filepart));
99 return (init ? 0 : -1);
104 /* we can use '/' as a delimiter, imap_complete rewrites it */
105 if (*s == '=' || *s == '+' || *s == '!') {
106 const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
107 mutt_concat_path (imap_path, q, s + 1, sizeof (imap_path));
110 strfcpy (imap_path, s, sizeof (imap_path));
112 if (mx_get_magic (imap_path) == M_IMAP)
113 return imap_complete (s, slen, imap_path);
116 if (*s == '=' || *s == '+' || *s == '!') {
120 strfcpy (exp_dirpart, NONULL (Spoolfile), sizeof (exp_dirpart));
122 strfcpy (exp_dirpart, NONULL (Maildir), sizeof (exp_dirpart));
123 if ((p = strrchr (s, '/'))) {
124 char buf[_POSIX_PATH_MAX];
127 mutt_concat_path (buf, exp_dirpart, s + 1, sizeof (buf));
128 strfcpy (exp_dirpart, buf, sizeof (exp_dirpart));
129 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
130 strfcpy (dirpart, buf, sizeof (dirpart));
131 strfcpy (filepart, p, sizeof (filepart));
134 strfcpy (filepart, s + 1, sizeof (filepart));
135 dirp = opendir (exp_dirpart);
138 if ((p = strrchr (s, '/'))) {
139 if (p == s) { /* absolute path */
141 strfcpy (dirpart, "/", sizeof (dirpart));
143 strfcpy (filepart, p, sizeof (filepart));
144 dirp = opendir (dirpart);
148 len = (size_t) (p - s);
149 strncpy (dirpart, s, len);
152 strfcpy (filepart, p, sizeof (filepart));
153 strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart));
154 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
155 dirp = opendir (exp_dirpart);
159 /* no directory name, so assume current directory. */
161 strfcpy (filepart, s, sizeof (filepart));
162 dirp = opendir (".");
167 debug_print (1, ("%s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno));
172 * special case to handle when there is no filepart yet. find the first
173 * file/directory which is not ``.'' or ``..''
175 if ((len = str_len (filepart)) == 0) {
176 while ((de = readdir (dirp)) != NULL) {
177 if (str_cmp (".", de->d_name) != 0
178 && str_cmp ("..", de->d_name) != 0) {
179 strfcpy (filepart, de->d_name, sizeof (filepart));
186 while ((de = readdir (dirp)) != NULL) {
187 if (str_ncmp (de->d_name, filepart, len) == 0) {
189 for (i = 0; filepart[i] && de->d_name[i]; i++) {
190 if (filepart[i] != de->d_name[i]) {
198 char buf[_POSIX_PATH_MAX];
201 strfcpy (filepart, de->d_name, sizeof (filepart));
203 /* check to see if it is a directory */
205 strfcpy (buf, exp_dirpart, sizeof (buf));
206 strfcpy (buf + str_len (buf), "/", sizeof (buf) - str_len (buf));
210 strfcpy (buf + str_len (buf), filepart, sizeof (buf) - str_len (buf));
211 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
212 strfcpy (filepart + str_len (filepart), "/",
213 sizeof (filepart) - str_len (filepart));
221 strfcpy (s, dirpart, slen);
222 if (str_cmp ("/", dirpart) != 0 && dirpart[0] != '='
223 && dirpart[0] != '+')
224 strfcpy (s + str_len (s), "/", slen - str_len (s));
225 strfcpy (s + str_len (s), filepart, slen - str_len (s));
228 strfcpy (s, filepart, slen);
230 return (init ? 0 : -1);