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.
16 #include <sys/types.h>
20 #include <lib-lib/str.h>
21 #include <lib-lib/file.h>
25 #include <imap/imap.h>
27 #include <nntp/nntp.h>
30 /* given a partial pathname, this routine fills in as much of the rest of the
33 * return 0 if ok, -1 if no matches
35 int mutt_complete (char *s, ssize_t slen)
42 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
43 char filepart[_POSIX_PATH_MAX];
45 char imap_path[LONG_STRING];
48 if (option (OPTNEWS)) {
49 string_list_t *l = CurrentNewsSrv->list;
51 m_strcpy(filepart, sizeof(filepart), s);
54 * special case to handle when there is no filepart yet.
55 * find the first subscribed newsgroup
57 if ((len = m_strlen(filepart)) == 0) {
58 for (; l; l = l->next) {
59 NNTP_DATA *data = (NNTP_DATA *) l->data;
61 if (data && data->subscribed) {
62 m_strcpy(filepart, sizeof(filepart), data->group);
70 for (; l; l = l->next) {
71 NNTP_DATA *data = (NNTP_DATA *) l->data;
73 if (data && data->subscribed &&
74 m_strncmp(data->group, filepart, len) == 0) {
76 for (i = 0; filepart[i] && data->group[i]; i++) {
77 if (filepart[i] != data->group[i]) {
85 m_strcpy(filepart, sizeof(filepart), data->group);
93 return (init ? 0 : -1);
97 /* we can use '/' as a delimiter, imap_complete rewrites it */
98 if (*s == '=' || *s == '+' || *s == '!') {
99 const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
100 mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
103 m_strcpy(imap_path, sizeof(imap_path), s);
105 if (mx_get_magic (imap_path) == M_IMAP)
106 return imap_complete (s, slen, imap_path);
108 if (*s == '=' || *s == '+' || *s == '!') {
112 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Spoolfile));
114 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Maildir));
115 if ((p = strrchr (s, '/'))) {
116 char buf[_POSIX_PATH_MAX];
119 mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
120 m_strcpy(exp_dirpart, sizeof(exp_dirpart), buf);
121 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
122 m_strcpy(dirpart, sizeof(dirpart), buf);
123 m_strcpy(filepart, sizeof(filepart), p);
126 m_strcpy(filepart, sizeof(filepart), s + 1);
127 dirp = opendir (exp_dirpart);
130 if ((p = strrchr (s, '/'))) {
131 if (p == s) { /* absolute path */
133 m_strcpy(dirpart, sizeof(dirpart), "/");
135 m_strcpy(filepart, sizeof(filepart), p);
136 dirp = opendir (dirpart);
141 memcpy(dirpart, s, len);
144 m_strcpy(filepart, sizeof(filepart), p);
145 m_strcpy(exp_dirpart, sizeof(exp_dirpart), dirpart);
146 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
147 dirp = opendir (exp_dirpart);
151 /* no directory name, so assume current directory. */
153 m_strcpy(filepart, sizeof(filepart), s);
154 dirp = opendir (".");
163 * special case to handle when there is no filepart yet. find the first
164 * file/directory which is not ``.'' or ``..''
166 if ((len = m_strlen(filepart)) == 0) {
167 while ((de = readdir (dirp)) != NULL) {
168 if (m_strcmp(".", de->d_name) != 0
169 && m_strcmp("..", de->d_name) != 0) {
170 m_strcpy(filepart, sizeof(filepart), de->d_name);
177 while ((de = readdir (dirp)) != NULL) {
178 if (m_strncmp(de->d_name, filepart, len) == 0) {
180 for (i = 0; filepart[i] && de->d_name[i]; i++) {
181 if (filepart[i] != de->d_name[i]) {
189 char buf[_POSIX_PATH_MAX];
192 m_strcpy(filepart, sizeof(filepart), de->d_name);
194 /* check to see if it is a directory */
196 m_strcpy(buf, sizeof(buf), exp_dirpart);
197 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), "/");
201 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), filepart);
202 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
203 m_strcpy(filepart + m_strlen(filepart),
204 sizeof(filepart) - m_strlen(filepart), "/");
212 m_strcpy(s, slen, dirpart);
213 if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
214 && dirpart[0] != '+')
215 m_strcpy(s + m_strlen(s), slen - m_strlen(s), "/");
216 m_strcpy(s + m_strlen(s), slen - m_strlen(s), filepart);
219 m_strcpy(s, slen, filepart);
221 return (init ? 0 : -1);