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.
10 #include <lib-ui/lib-ui.h>
11 #include <lib-mx/mx.h>
14 #include <imap/imap.h>
19 /* given a partial pathname, this routine fills in as much of the rest of the
22 * return 0 if ok, -1 if no matches
24 int mutt_complete (char *s, ssize_t slen)
31 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
32 char filepart[_POSIX_PATH_MAX];
34 char imap_path[LONG_STRING];
37 if (option (OPTNEWS)) {
38 nntp_data_t *l = CurrentNewsSrv->list;
40 m_strcpy(filepart, sizeof(filepart), s);
43 * special case to handle when there is no filepart yet.
44 * find the first subscribed newsgroup
46 if ((len = m_strlen(filepart)) == 0) {
47 for (; l; l = l->next) {
49 m_strcpy(filepart, sizeof(filepart), l->group);
57 for (; l; l = l->next) {
58 if (l->subscribed && m_strncmp(l->group, filepart, len) == 0) {
60 for (i = 0; filepart[i] && l->group[i]; i++) {
61 if (filepart[i] != l->group[i]) {
68 m_strcpy(filepart, sizeof(filepart), l->group);
74 m_strcpy(s, slen, filepart);
76 return (init ? 0 : -1);
80 /* we can use '/' as a delimiter, imap_complete rewrites it */
81 if (*s == '=' || *s == '+' || *s == '!') {
82 const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
83 mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
86 m_strcpy(imap_path, sizeof(imap_path), s);
88 if (mx_get_magic (imap_path) == M_IMAP)
89 return imap_complete (s, slen, imap_path);
91 if (*s == '=' || *s == '+' || *s == '!') {
95 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Spoolfile));
97 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Maildir));
98 if ((p = strrchr (s, '/'))) {
99 char buf[_POSIX_PATH_MAX];
102 mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
103 m_strcpy(exp_dirpart, sizeof(exp_dirpart), buf);
104 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
105 m_strcpy(dirpart, sizeof(dirpart), buf);
106 m_strcpy(filepart, sizeof(filepart), p);
109 m_strcpy(filepart, sizeof(filepart), s + 1);
110 dirp = opendir (exp_dirpart);
113 if ((p = strrchr (s, '/'))) {
114 if (p == s) { /* absolute path */
116 m_strcpy(dirpart, sizeof(dirpart), "/");
118 m_strcpy(filepart, sizeof(filepart), p);
119 dirp = opendir (dirpart);
124 memcpy(dirpart, s, len);
127 m_strcpy(filepart, sizeof(filepart), p);
128 m_strcpy(exp_dirpart, sizeof(exp_dirpart), dirpart);
129 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
130 dirp = opendir (exp_dirpart);
134 /* no directory name, so assume current directory. */
136 m_strcpy(filepart, sizeof(filepart), s);
137 dirp = opendir (".");
146 * special case to handle when there is no filepart yet. find the first
147 * file/directory which is not ``.'' or ``..''
149 if ((len = m_strlen(filepart)) == 0) {
150 while ((de = readdir (dirp)) != NULL) {
151 if (m_strcmp(".", de->d_name) != 0
152 && m_strcmp("..", de->d_name) != 0) {
153 m_strcpy(filepart, sizeof(filepart), de->d_name);
160 while ((de = readdir (dirp)) != NULL) {
161 if (m_strncmp(de->d_name, filepart, len) == 0) {
163 for (i = 0; filepart[i] && de->d_name[i]; i++) {
164 if (filepart[i] != de->d_name[i]) {
172 char buf[_POSIX_PATH_MAX];
175 m_strcpy(filepart, sizeof(filepart), de->d_name);
177 /* check to see if it is a directory */
179 m_strcpy(buf, sizeof(buf), exp_dirpart);
180 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), "/");
184 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), filepart);
185 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
186 m_strcpy(filepart + m_strlen(filepart),
187 sizeof(filepart) - m_strlen(filepart), "/");
195 m_strcpy(s, slen, dirpart);
196 if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
197 && dirpart[0] != '+')
198 m_strcpy(s + m_strlen(s), slen - m_strlen(s), "/");
199 m_strcpy(s + m_strlen(s), slen - m_strlen(s), filepart);
202 m_strcpy(s, slen, filepart);
204 return (init ? 0 : -1);