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 <imap/imap.h>
21 #include <lib-lib/str.h>
22 #include <lib-lib/file.h>
23 #include "lib/debug.h"
27 #include <sys/types.h>
31 /* given a partial pathname, this routine fills in as much of the rest of the
34 * return 0 if ok, -1 if no matches
36 int mutt_complete (char *s, size_t slen)
43 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
44 char filepart[_POSIX_PATH_MAX];
46 char imap_path[LONG_STRING];
48 debug_print (2, ("completing %s\n", s));
51 if (option (OPTNEWS)) {
52 LIST *l = CurrentNewsSrv->list;
54 m_strcpy(filepart, sizeof(filepart), s);
57 * special case to handle when there is no filepart yet.
58 * find the first subscribed newsgroup
60 if ((len = m_strlen(filepart)) == 0) {
61 for (; l; l = l->next) {
62 NNTP_DATA *data = (NNTP_DATA *) l->data;
64 if (data && data->subscribed) {
65 m_strcpy(filepart, sizeof(filepart), data->group);
73 for (; l; l = l->next) {
74 NNTP_DATA *data = (NNTP_DATA *) l->data;
76 if (data && data->subscribed &&
77 m_strncmp(data->group, filepart, len) == 0) {
79 for (i = 0; filepart[i] && data->group[i]; i++) {
80 if (filepart[i] != data->group[i]) {
88 m_strcpy(filepart, sizeof(filepart), data->group);
96 return (init ? 0 : -1);
100 /* we can use '/' as a delimiter, imap_complete rewrites it */
101 if (*s == '=' || *s == '+' || *s == '!') {
102 const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
103 mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
106 m_strcpy(imap_path, sizeof(imap_path), s);
108 if (mx_get_magic (imap_path) == M_IMAP)
109 return imap_complete (s, slen, imap_path);
111 if (*s == '=' || *s == '+' || *s == '!') {
115 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Spoolfile));
117 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Maildir));
118 if ((p = strrchr (s, '/'))) {
119 char buf[_POSIX_PATH_MAX];
122 mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
123 m_strcpy(exp_dirpart, sizeof(exp_dirpart), buf);
124 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
125 m_strcpy(dirpart, sizeof(dirpart), buf);
126 m_strcpy(filepart, sizeof(filepart), p);
129 m_strcpy(filepart, sizeof(filepart), s + 1);
130 dirp = opendir (exp_dirpart);
133 if ((p = strrchr (s, '/'))) {
134 if (p == s) { /* absolute path */
136 m_strcpy(dirpart, sizeof(dirpart), "/");
138 m_strcpy(filepart, sizeof(filepart), p);
139 dirp = opendir (dirpart);
143 len = (size_t) (p - s);
144 memcpy(dirpart, s, len);
147 m_strcpy(filepart, sizeof(filepart), p);
148 m_strcpy(exp_dirpart, sizeof(exp_dirpart), dirpart);
149 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
150 dirp = opendir (exp_dirpart);
154 /* no directory name, so assume current directory. */
156 m_strcpy(filepart, sizeof(filepart), s);
157 dirp = opendir (".");
162 debug_print (1, ("%s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno));
167 * special case to handle when there is no filepart yet. find the first
168 * file/directory which is not ``.'' or ``..''
170 if ((len = m_strlen(filepart)) == 0) {
171 while ((de = readdir (dirp)) != NULL) {
172 if (m_strcmp(".", de->d_name) != 0
173 && m_strcmp("..", de->d_name) != 0) {
174 m_strcpy(filepart, sizeof(filepart), de->d_name);
181 while ((de = readdir (dirp)) != NULL) {
182 if (m_strncmp(de->d_name, filepart, len) == 0) {
184 for (i = 0; filepart[i] && de->d_name[i]; i++) {
185 if (filepart[i] != de->d_name[i]) {
193 char buf[_POSIX_PATH_MAX];
196 m_strcpy(filepart, sizeof(filepart), de->d_name);
198 /* check to see if it is a directory */
200 m_strcpy(buf, sizeof(buf), exp_dirpart);
201 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), "/");
205 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), filepart);
206 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
207 m_strcpy(filepart + m_strlen(filepart),
208 sizeof(filepart) - m_strlen(filepart), "/");
216 m_strcpy(s, slen, dirpart);
217 if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
218 && dirpart[0] != '+')
219 m_strcpy(s + m_strlen(s), slen - m_strlen(s), "/");
220 m_strcpy(s + m_strlen(s), slen - m_strlen(s), filepart);
223 m_strcpy(s, slen, filepart);
225 return (init ? 0 : -1);