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>
14 #include <imap/imap.h>
16 #include <nntp/nntp.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 string_list_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) {
48 NNTP_DATA *data = (NNTP_DATA *) l->data;
50 if (data && data->subscribed) {
51 m_strcpy(filepart, sizeof(filepart), data->group);
59 for (; l; l = l->next) {
60 NNTP_DATA *data = (NNTP_DATA *) l->data;
62 if (data && data->subscribed &&
63 m_strncmp(data->group, filepart, len) == 0) {
65 for (i = 0; filepart[i] && data->group[i]; i++) {
66 if (filepart[i] != data->group[i]) {
74 m_strcpy(filepart, sizeof(filepart), data->group);
82 return (init ? 0 : -1);
86 /* we can use '/' as a delimiter, imap_complete rewrites it */
87 if (*s == '=' || *s == '+' || *s == '!') {
88 const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
89 mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
92 m_strcpy(imap_path, sizeof(imap_path), s);
94 if (mx_get_magic (imap_path) == M_IMAP)
95 return imap_complete (s, slen, imap_path);
97 if (*s == '=' || *s == '+' || *s == '!') {
101 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Spoolfile));
103 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Maildir));
104 if ((p = strrchr (s, '/'))) {
105 char buf[_POSIX_PATH_MAX];
108 mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
109 m_strcpy(exp_dirpart, sizeof(exp_dirpart), buf);
110 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
111 m_strcpy(dirpart, sizeof(dirpart), buf);
112 m_strcpy(filepart, sizeof(filepart), p);
115 m_strcpy(filepart, sizeof(filepart), s + 1);
116 dirp = opendir (exp_dirpart);
119 if ((p = strrchr (s, '/'))) {
120 if (p == s) { /* absolute path */
122 m_strcpy(dirpart, sizeof(dirpart), "/");
124 m_strcpy(filepart, sizeof(filepart), p);
125 dirp = opendir (dirpart);
130 memcpy(dirpart, s, len);
133 m_strcpy(filepart, sizeof(filepart), p);
134 m_strcpy(exp_dirpart, sizeof(exp_dirpart), dirpart);
135 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
136 dirp = opendir (exp_dirpart);
140 /* no directory name, so assume current directory. */
142 m_strcpy(filepart, sizeof(filepart), s);
143 dirp = opendir (".");
152 * special case to handle when there is no filepart yet. find the first
153 * file/directory which is not ``.'' or ``..''
155 if ((len = m_strlen(filepart)) == 0) {
156 while ((de = readdir (dirp)) != NULL) {
157 if (m_strcmp(".", de->d_name) != 0
158 && m_strcmp("..", de->d_name) != 0) {
159 m_strcpy(filepart, sizeof(filepart), de->d_name);
166 while ((de = readdir (dirp)) != NULL) {
167 if (m_strncmp(de->d_name, filepart, len) == 0) {
169 for (i = 0; filepart[i] && de->d_name[i]; i++) {
170 if (filepart[i] != de->d_name[i]) {
178 char buf[_POSIX_PATH_MAX];
181 m_strcpy(filepart, sizeof(filepart), de->d_name);
183 /* check to see if it is a directory */
185 m_strcpy(buf, sizeof(buf), exp_dirpart);
186 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), "/");
190 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), filepart);
191 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
192 m_strcpy(filepart + m_strlen(filepart),
193 sizeof(filepart) - m_strlen(filepart), "/");
201 m_strcpy(s, slen, dirpart);
202 if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
203 && dirpart[0] != '+')
204 m_strcpy(s + m_strlen(s), slen - m_strlen(s), "/");
205 m_strcpy(s + m_strlen(s), slen - m_strlen(s), filepart);
208 m_strcpy(s, slen, filepart);
210 return (init ? 0 : -1);