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/lib-lib.h>
24 #include <imap/imap.h>
26 #include <nntp/nntp.h>
29 /* given a partial pathname, this routine fills in as much of the rest of the
32 * return 0 if ok, -1 if no matches
34 int mutt_complete (char *s, ssize_t slen)
41 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
42 char filepart[_POSIX_PATH_MAX];
44 char imap_path[LONG_STRING];
47 if (option (OPTNEWS)) {
48 string_list_t *l = CurrentNewsSrv->list;
50 m_strcpy(filepart, sizeof(filepart), s);
53 * special case to handle when there is no filepart yet.
54 * find the first subscribed newsgroup
56 if ((len = m_strlen(filepart)) == 0) {
57 for (; l; l = l->next) {
58 NNTP_DATA *data = (NNTP_DATA *) l->data;
60 if (data && data->subscribed) {
61 m_strcpy(filepart, sizeof(filepart), data->group);
69 for (; l; l = l->next) {
70 NNTP_DATA *data = (NNTP_DATA *) l->data;
72 if (data && data->subscribed &&
73 m_strncmp(data->group, filepart, len) == 0) {
75 for (i = 0; filepart[i] && data->group[i]; i++) {
76 if (filepart[i] != data->group[i]) {
84 m_strcpy(filepart, sizeof(filepart), data->group);
92 return (init ? 0 : -1);
96 /* we can use '/' as a delimiter, imap_complete rewrites it */
97 if (*s == '=' || *s == '+' || *s == '!') {
98 const char *q = NONULL(*s == '!' ? Spoolfile : Maildir);
99 mutt_concat_path(imap_path, sizeof(imap_path), q, s + 1);
102 m_strcpy(imap_path, sizeof(imap_path), s);
104 if (mx_get_magic (imap_path) == M_IMAP)
105 return imap_complete (s, slen, imap_path);
107 if (*s == '=' || *s == '+' || *s == '!') {
111 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Spoolfile));
113 m_strcpy(exp_dirpart, sizeof(exp_dirpart), NONULL(Maildir));
114 if ((p = strrchr (s, '/'))) {
115 char buf[_POSIX_PATH_MAX];
118 mutt_concat_path(buf, sizeof(buf), exp_dirpart, s + 1);
119 m_strcpy(exp_dirpart, sizeof(exp_dirpart), buf);
120 snprintf (buf, sizeof (buf), "%s%s/", dirpart, s + 1);
121 m_strcpy(dirpart, sizeof(dirpart), buf);
122 m_strcpy(filepart, sizeof(filepart), p);
125 m_strcpy(filepart, sizeof(filepart), s + 1);
126 dirp = opendir (exp_dirpart);
129 if ((p = strrchr (s, '/'))) {
130 if (p == s) { /* absolute path */
132 m_strcpy(dirpart, sizeof(dirpart), "/");
134 m_strcpy(filepart, sizeof(filepart), p);
135 dirp = opendir (dirpart);
140 memcpy(dirpart, s, len);
143 m_strcpy(filepart, sizeof(filepart), p);
144 m_strcpy(exp_dirpart, sizeof(exp_dirpart), dirpart);
145 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
146 dirp = opendir (exp_dirpart);
150 /* no directory name, so assume current directory. */
152 m_strcpy(filepart, sizeof(filepart), s);
153 dirp = opendir (".");
162 * special case to handle when there is no filepart yet. find the first
163 * file/directory which is not ``.'' or ``..''
165 if ((len = m_strlen(filepart)) == 0) {
166 while ((de = readdir (dirp)) != NULL) {
167 if (m_strcmp(".", de->d_name) != 0
168 && m_strcmp("..", de->d_name) != 0) {
169 m_strcpy(filepart, sizeof(filepart), de->d_name);
176 while ((de = readdir (dirp)) != NULL) {
177 if (m_strncmp(de->d_name, filepart, len) == 0) {
179 for (i = 0; filepart[i] && de->d_name[i]; i++) {
180 if (filepart[i] != de->d_name[i]) {
188 char buf[_POSIX_PATH_MAX];
191 m_strcpy(filepart, sizeof(filepart), de->d_name);
193 /* check to see if it is a directory */
195 m_strcpy(buf, sizeof(buf), exp_dirpart);
196 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), "/");
200 m_strcpy(buf + m_strlen(buf), sizeof(buf) - m_strlen(buf), filepart);
201 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
202 m_strcpy(filepart + m_strlen(filepart),
203 sizeof(filepart) - m_strlen(filepart), "/");
211 m_strcpy(s, slen, dirpart);
212 if (m_strcmp("/", dirpart) != 0 && dirpart[0] != '='
213 && dirpart[0] != '+')
214 m_strcpy(s + m_strlen(s), slen - m_strlen(s), "/");
215 m_strcpy(s + m_strlen(s), slen - m_strlen(s), filepart);
218 m_strcpy(s, slen, filepart);
220 return (init ? 0 : -1);