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-lib/lib-lib.h>
11 #include <lib-mx/mx.h>
15 #include <imap/imap.h>
20 /* given a partial pathname, this routine fills in as much of the rest of the
23 * return 0 if ok, -1 if no matches
25 int mutt_complete (char *s, ssize_t slen)
32 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
33 char filepart[_POSIX_PATH_MAX];
35 char imap_path[LONG_STRING];
38 if (option (OPTNEWS)) {
39 nntp_data_t *l = CurrentNewsSrv->list;
41 m_strcpy(filepart, sizeof(filepart), s);
44 * special case to handle when there is no filepart yet.
45 * find the first subscribed newsgroup
47 if ((len = m_strlen(filepart)) == 0) {
48 for (; l; l = l->next) {
50 m_strcpy(filepart, sizeof(filepart), l->group);
58 for (; l; l = l->next) {
59 if (l->subscribed && m_strncmp(l->group, filepart, len) == 0) {
61 for (i = 0; filepart[i] && l->group[i]; i++) {
62 if (filepart[i] != l->group[i]) {
69 m_strcpy(filepart, sizeof(filepart), l->group);
75 m_strcpy(s, slen, filepart);
77 return (init ? 0 : -1);
81 /* we can use '/' as a delimiter, imap_complete rewrites it */
82 if (*s == '=' || *s == '+' || *s == '!') {
83 const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
84 mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
87 m_strcpy(imap_path, sizeof(imap_path), s);
89 if (mx_get_magic (imap_path) == M_IMAP)
90 return imap_complete (s, slen, imap_path);
92 if (*s == '=' || *s == '+' || *s == '!') {
96 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Spoolfile));
98 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Maildir));
99 if ((p = strrchr (s, '/'))) {
100 char buf[_POSIX_PATH_MAX];
103 mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
104 m_strcpy(exp_dirpart, sizeof(exp_dirpart), buf);
105 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
106 m_strcpy(dirpart, sizeof(dirpart), buf);
107 m_strcpy(filepart, sizeof(filepart), p);
110 m_strcpy(filepart, sizeof(filepart), s + 1);
111 dirp = opendir (exp_dirpart);
114 if ((p = strrchr (s, '/'))) {
115 if (p == s) { /* absolute path */
117 m_strcpy(dirpart, sizeof(dirpart), "/");
119 m_strcpy(filepart, sizeof(filepart), p);
120 dirp = opendir (dirpart);
125 memcpy(dirpart, s, len);
128 m_strcpy(filepart, sizeof(filepart), p);
129 m_strcpy(exp_dirpart, sizeof(exp_dirpart), dirpart);
130 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
131 dirp = opendir (exp_dirpart);
135 /* no directory name, so assume current directory. */
137 m_strcpy(filepart, sizeof(filepart), s);
138 dirp = opendir (".");
147 * special case to handle when there is no filepart yet. find the first
148 * file/directory which is not ``.'' or ``..''
150 if ((len = m_strlen(filepart)) == 0) {
151 while ((de = readdir (dirp)) != NULL) {
152 if (m_strcmp(".", de->d_name) != 0
153 && m_strcmp("..", de->d_name) != 0) {
154 m_strcpy(filepart, sizeof(filepart), de->d_name);
161 while ((de = readdir (dirp)) != NULL) {
162 if (m_strncmp(de->d_name, filepart, len) == 0) {
164 for (i = 0; filepart[i] && de->d_name[i]; i++) {
165 if (filepart[i] != de->d_name[i]) {
173 char buf[_POSIX_PATH_MAX];
176 m_strcpy(filepart, sizeof(filepart), de->d_name);
178 /* check to see if it is a directory */
180 m_strcpy(buf, sizeof(buf), exp_dirpart);
181 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), "/");
185 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), filepart);
186 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
187 m_strcpy(filepart + m_strlen(filepart),
188 sizeof(filepart) - m_strlen(filepart), "/");
196 m_strcpy(s, slen, dirpart);
197 if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
198 && dirpart[0] != '+')
199 m_strcpy(s + m_strlen(s), slen - m_strlen(s), "/");
200 m_strcpy(s + m_strlen(s), slen - m_strlen(s), filepart);
203 m_strcpy(s, slen, filepart);
205 return (init ? 0 : -1);