2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
30 #include <sys/types.h>
34 /* given a partial pathname, this routine fills in as much of the rest of the
37 * return 0 if ok, -1 if no matches
39 int mutt_complete (char *s, size_t slen)
46 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
47 char filepart[_POSIX_PATH_MAX];
49 char imap_path[LONG_STRING];
52 dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
57 LIST *l = CurrentNewsSrv->list;
59 strfcpy (filepart, s, sizeof (filepart));
62 * special case to handle when there is no filepart yet.
63 * find the first subscribed newsgroup
65 if ((len = mutt_strlen (filepart)) == 0)
67 for (; l; l = l->next)
69 NNTP_DATA *data = (NNTP_DATA *)l->data;
71 if (data && data->subscribed)
73 strfcpy (filepart, data->group, sizeof (filepart));
81 for (; l; l = l->next)
83 NNTP_DATA *data = (NNTP_DATA *)l->data;
85 if (data && data->subscribed &&
86 mutt_strncmp (data->group, filepart, len) == 0)
90 for (i = 0; filepart[i] && data->group[i]; i++)
92 if (filepart[i] != data->group[i])
102 strfcpy (filepart, data->group, sizeof (filepart));
108 strcpy (s, filepart);
110 return (init ? 0 : -1);
115 /* we can use '/' as a delimiter, imap_complete rewrites it */
116 if (*s == '=' || *s == '+' || *s == '!')
119 p = NONULL (Spoolfile);
121 p = NONULL (Maildir);
123 mutt_concat_path (imap_path, p, s+1, sizeof (imap_path));
126 strfcpy (imap_path, s, sizeof(imap_path));
128 if (mx_is_imap (imap_path))
129 return imap_complete (s, slen, imap_path);
132 if (*s == '=' || *s == '+' || *s == '!')
137 strfcpy (exp_dirpart, NONULL (Spoolfile), sizeof (exp_dirpart));
139 strfcpy (exp_dirpart, NONULL (Maildir), sizeof (exp_dirpart));
140 if ((p = strrchr (s, '/')))
142 char buf[_POSIX_PATH_MAX];
144 mutt_concat_path (buf, exp_dirpart, s + 1, sizeof (buf));
145 strfcpy (exp_dirpart, buf, sizeof (exp_dirpart));
146 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s+1);
147 strfcpy (dirpart, buf, sizeof (dirpart));
148 strfcpy (filepart, p, sizeof (filepart));
151 strfcpy (filepart, s + 1, sizeof (filepart));
152 dirp = opendir (exp_dirpart);
156 if ((p = strrchr (s, '/')))
158 if (p == s) /* absolute path */
161 strfcpy (dirpart, "/", sizeof (dirpart));
163 strfcpy (filepart, p, sizeof (filepart));
164 dirp = opendir (dirpart);
169 len = (size_t)(p - s);
170 strncpy (dirpart, s, len);
173 strfcpy (filepart, p, sizeof (filepart));
174 strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart));
175 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
176 dirp = opendir (exp_dirpart);
181 /* no directory name, so assume current directory. */
183 strfcpy (filepart, s, sizeof (filepart));
184 dirp = opendir (".");
190 dprint (1, (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno));
195 * special case to handle when there is no filepart yet. find the first
196 * file/directory which is not ``.'' or ``..''
198 if ((len = mutt_strlen (filepart)) == 0)
200 while ((de = readdir (dirp)) != NULL)
202 if (mutt_strcmp (".", de->d_name) != 0 && mutt_strcmp ("..", de->d_name) != 0)
204 strfcpy (filepart, de->d_name, sizeof (filepart));
211 while ((de = readdir (dirp)) != NULL)
213 if (mutt_strncmp (de->d_name, filepart, len) == 0)
217 for (i=0; filepart[i] && de->d_name[i]; i++)
219 if (filepart[i] != de->d_name[i])
229 char buf[_POSIX_PATH_MAX];
232 strfcpy (filepart, de->d_name, sizeof(filepart));
234 /* check to see if it is a directory */
237 strfcpy (buf, exp_dirpart, sizeof (buf));
238 strfcpy (buf + strlen (buf), "/", sizeof (buf) - strlen (buf));
242 strfcpy (buf + strlen (buf), filepart, sizeof (buf) - strlen (buf));
243 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
244 strfcpy (filepart + strlen (filepart), "/",
245 sizeof (filepart) - strlen (filepart));
254 strfcpy (s, dirpart, slen);
255 if (mutt_strcmp ("/", dirpart) != 0 && dirpart[0] != '=' && dirpart[0] != '+')
256 strfcpy (s + strlen (s), "/", slen - strlen (s));
257 strfcpy (s + strlen (s), filepart, slen - strlen (s));
260 strfcpy (s, filepart, slen);
262 return (init ? 0 : -1);