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.
25 #include <sys/types.h>
29 /* given a partial pathname, this routine fills in as much of the rest of the
32 * return 0 if ok, -1 if no matches
34 int mutt_complete (char *s, size_t slen)
41 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
42 char filepart[_POSIX_PATH_MAX];
45 char imap_path[LONG_STRING];
48 dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
51 if (option (OPTNEWS)) {
52 LIST *l = CurrentNewsSrv->list;
54 strfcpy (filepart, s, sizeof (filepart));
57 * special case to handle when there is no filepart yet.
58 * find the first subscribed newsgroup
60 if ((len = mutt_strlen (filepart)) == 0) {
61 for (; l; l = l->next) {
62 NNTP_DATA *data = (NNTP_DATA *) l->data;
64 if (data && data->subscribed) {
65 strfcpy (filepart, data->group, sizeof (filepart));
73 for (; l; l = l->next) {
74 NNTP_DATA *data = (NNTP_DATA *) l->data;
76 if (data && data->subscribed &&
77 mutt_strncmp (data->group, filepart, len) == 0) {
79 for (i = 0; filepart[i] && data->group[i]; i++) {
80 if (filepart[i] != data->group[i]) {
88 strfcpy (filepart, data->group, sizeof (filepart));
96 return (init ? 0 : -1);
101 /* we can use '/' as a delimiter, imap_complete rewrites it */
102 if (*s == '=' || *s == '+' || *s == '!') {
104 p = NONULL (Spoolfile);
106 p = NONULL (Maildir);
108 mutt_concat_path (imap_path, p, s + 1, sizeof (imap_path));
111 strfcpy (imap_path, s, sizeof (imap_path));
113 if (mx_is_imap (imap_path))
114 return imap_complete (s, slen, imap_path);
117 if (*s == '=' || *s == '+' || *s == '!') {
121 strfcpy (exp_dirpart, NONULL (Spoolfile), sizeof (exp_dirpart));
123 strfcpy (exp_dirpart, NONULL (Maildir), sizeof (exp_dirpart));
124 if ((p = strrchr (s, '/'))) {
125 char buf[_POSIX_PATH_MAX];
128 mutt_concat_path (buf, exp_dirpart, s + 1, sizeof (buf));
129 strfcpy (exp_dirpart, buf, sizeof (exp_dirpart));
130 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
131 strfcpy (dirpart, buf, sizeof (dirpart));
132 strfcpy (filepart, p, sizeof (filepart));
135 strfcpy (filepart, s + 1, sizeof (filepart));
136 dirp = opendir (exp_dirpart);
139 if ((p = strrchr (s, '/'))) {
140 if (p == s) { /* absolute path */
142 strfcpy (dirpart, "/", sizeof (dirpart));
144 strfcpy (filepart, p, sizeof (filepart));
145 dirp = opendir (dirpart);
149 len = (size_t) (p - s);
150 strncpy (dirpart, s, len);
153 strfcpy (filepart, p, sizeof (filepart));
154 strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart));
155 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
156 dirp = opendir (exp_dirpart);
160 /* no directory name, so assume current directory. */
162 strfcpy (filepart, s, sizeof (filepart));
163 dirp = opendir (".");
169 (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart,
170 strerror (errno), errno));
175 * special case to handle when there is no filepart yet. find the first
176 * file/directory which is not ``.'' or ``..''
178 if ((len = mutt_strlen (filepart)) == 0) {
179 while ((de = readdir (dirp)) != NULL) {
180 if (mutt_strcmp (".", de->d_name) != 0
181 && mutt_strcmp ("..", de->d_name) != 0) {
182 strfcpy (filepart, de->d_name, sizeof (filepart));
189 while ((de = readdir (dirp)) != NULL) {
190 if (mutt_strncmp (de->d_name, filepart, len) == 0) {
192 for (i = 0; filepart[i] && de->d_name[i]; i++) {
193 if (filepart[i] != de->d_name[i]) {
201 char buf[_POSIX_PATH_MAX];
204 strfcpy (filepart, de->d_name, sizeof (filepart));
206 /* check to see if it is a directory */
208 strfcpy (buf, exp_dirpart, sizeof (buf));
209 strfcpy (buf + mutt_strlen (buf), "/", sizeof (buf) - mutt_strlen (buf));
213 strfcpy (buf + mutt_strlen (buf), filepart, sizeof (buf) - mutt_strlen (buf));
214 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
215 strfcpy (filepart + mutt_strlen (filepart), "/",
216 sizeof (filepart) - mutt_strlen (filepart));
224 strfcpy (s, dirpart, slen);
225 if (mutt_strcmp ("/", dirpart) != 0 && dirpart[0] != '='
226 && dirpart[0] != '+')
227 strfcpy (s + mutt_strlen (s), "/", slen - mutt_strlen (s));
228 strfcpy (s + mutt_strlen (s), filepart, slen - mutt_strlen (s));
231 strfcpy (s, filepart, slen);
233 return (init ? 0 : -1);