mk-conf.sh, auxlily.c: fix va_copy(3) detection with -std=c89
[s-mailx.git] / folder.c
blob7d7707f5b01d2de5e0e18f5a0af1cf29a96af03a
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Folder (mailbox) initialization, newmail announcement and related.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE folder
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #include <pwd.h>
44 /* Update mailname (if name != NULL) and displayname, return whether displayname
45 * was large enough to swallow mailname */
46 static bool_t _update_mailname(char const *name);
47 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
48 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
49 #endif
51 #ifdef HAVE_C90AMEND1
52 SINLINE size_t
53 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
55 int err;
56 size_t i, ok;
57 NYD_ENTER;
59 for (err = ok = i = 0; cpl > maxl || err;) {
60 int ml = mblen(cp, cpl);
61 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
62 (void)mblen(NULL, 0);
63 err = 1;
64 ml = 1;
65 } else {
66 if (!err)
67 ok = i;
68 err = 0;
69 if (ml == 0)
70 break;
72 cp += ml;
73 i += ml;
74 cpl -= ml;
76 NYD_LEAVE;
77 return ok;
79 #endif /* HAVE_C90AMEND1 */
81 static bool_t
82 _update_mailname(char const *name)
84 char *mailp, *dispp;
85 size_t i, j;
86 bool_t rv = TRU1;
87 NYD_ENTER;
89 /* Don't realpath(3) if it's only an update request */
90 if (name != NULL) {
91 #ifdef HAVE_REALPATH
92 enum protocol p = which_protocol(name);
94 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
95 if (realpath(name, mailname) == NULL && errno != ENOENT) {
96 n_err(_("Can't canonicalize %s\n"), n_shexp_quote_cp(name, FAL0));
97 rv = FAL0;
98 goto jdocopy;
100 } else
101 jdocopy:
102 #endif
103 n_strscpy(mailname, name, sizeof(mailname));
106 mailp = mailname;
107 dispp = displayname;
109 /* Don't display an absolute path but "+FOLDER" if under *folder* */
110 /* C99 */{
111 char const *folderp;
113 if(*(folderp = folder_query()) != '\0'){
114 i = strlen(folderp);
115 if(!strncmp(folderp, mailp, i)){
116 mailp += i;
117 *dispp++ = '+';
122 /* We want to see the name of the folder .. on the screen */
123 i = strlen(mailp);
124 if (i < sizeof(displayname) -1)
125 memcpy(dispp, mailp, i +1);
126 else {
127 rv = FAL0;
128 /* Avoid disrupting multibyte sequences (if possible) */
129 #ifndef HAVE_C90AMEND1
130 j = sizeof(displayname) / 3 - 1;
131 i -= sizeof(displayname) - (1/* + */ + 3) - j;
132 #else
133 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
134 i = j + __narrow_suffix(mailp + j, i - j,
135 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
136 #endif
137 snprintf(dispp, sizeof(displayname), "%.*s...%s",
138 (int)j, mailp, mailp + i);
140 NYD_LEAVE;
141 return rv;
144 FL int
145 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
147 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
148 * necessary to make a mailbox accessible by a different user, and if that
149 * has happened, let's just let the usual file perms decide */
150 static int shudclob;
152 struct stat stb;
153 size_t offset;
154 char const *who;
155 int rv, omsgCount = 0;
156 FILE *ibuf = NULL, *lckfp = NULL;
157 bool_t isdevnull = FAL0;
158 NYD_ENTER;
160 pstate &= ~PS_SETFILE_OPENED;
162 /* C99 */{
163 enum fexp_mode fexpm;
165 if((who = shortcut_expand(name)) != NULL){
166 fexpm = FEXP_NSHORTCUT | FEXP_NSHELL;
167 name = who;
168 }else
169 fexpm = FEXP_NSHELL;
171 if(name[0] == '%'){
172 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
173 if(*(who = &name[1]) == ':')
174 ++who;
175 if(*who == '\0')
176 who = myname;
177 }else
178 who = myname;
180 if ((name = fexpand(name, fexpm)) == NULL)
181 goto jem1;
184 /* For at least substdate() users */
185 time_current_update(&time_current, FAL0);
187 switch (which_protocol(name)) {
188 case PROTO_FILE:
189 if (temporary_protocol_ext != NULL)
190 name = savecat(name, temporary_protocol_ext);
191 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
192 #ifdef HAVE_REALPATH
193 do { /* TODO we need objects, goes away then */
194 # ifdef HAVE_REALPATH_NULL
195 char *cp;
197 if ((cp = realpath(name, NULL)) != NULL) {
198 name = savestr(cp);
199 (free)(cp);
201 # else
202 char cbuf[PATH_MAX];
204 if (realpath(name, cbuf) != NULL)
205 name = savestr(cbuf);
206 # endif
207 } while (0);
208 #endif
209 rv = 1;
210 break;
211 case PROTO_MAILDIR:
212 shudclob = 1;
213 rv = maildir_setfile(name, fm);
214 goto jleave;
215 #ifdef HAVE_POP3
216 case PROTO_POP3:
217 shudclob = 1;
218 rv = pop3_setfile(name, fm);
219 goto jleave;
220 #endif
221 default:
222 n_err(_("Cannot handle protocol: %s\n"), name);
223 goto jem1;
226 if ((ibuf = Zopen(name, "r")) == NULL) {
227 if ((fm & FEDIT_SYSBOX) && errno == ENOENT) {
228 if (strcmp(who, myname) && getpwnam(who) == NULL) {
229 n_err(_("%s is not a user of this system\n"),
230 n_shexp_quote_cp(who, FAL0));
231 goto jem2;
233 if (!(options & OPT_QUICKRUN_MASK) && ok_blook(bsdcompat))
234 n_err(_("No mail for %s\n"), who);
236 if (fm & FEDIT_NEWMAIL)
237 goto jleave;
239 mb.mb_type = MB_VOID;
240 if (ok_blook(emptystart)) {
241 if (!(options & OPT_QUICKRUN_MASK) && !ok_blook(bsdcompat))
242 n_perr(name, 0);
243 /* We must avoid returning -1 and causing program exit */
244 rv = 1;
245 goto jleave;
247 n_perr(name, 0);
248 goto jem1;
251 if (fstat(fileno(ibuf), &stb) == -1) {
252 if (fm & FEDIT_NEWMAIL)
253 goto jleave;
254 n_perr(_("fstat"), 0);
255 goto jem1;
258 if (S_ISREG(stb.st_mode) || isdevnull) {
259 /* EMPTY */
260 } else {
261 if (fm & FEDIT_NEWMAIL)
262 goto jleave;
263 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
264 n_perr(name, 0);
265 goto jem1;
268 /* Looks like all will be well. We must now relinquish our hold on the
269 * current set of stuff. Must hold signals while we are reading the new
270 * file, else we will ruin the message[] data structure */
272 hold_sigs(); /* TODO note on this one in quit.c:quit() */
273 if (shudclob && !(fm & FEDIT_NEWMAIL) && !quit()) {
274 rele_sigs();
275 goto jem2;
278 #ifdef HAVE_SOCKETS
279 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
280 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
281 #endif
283 /* TODO There is no intermediate VOID box we've switched to: name may
284 * TODO point to the same box that we just have written, so any updates
285 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
286 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
287 /* TODO We may uselessly open twice but quit() doesn't say whether we were
288 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
289 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
290 name = mailname;
291 Fclose(ibuf);
293 if ((ibuf = Zopen(name, "r")) == NULL ||
294 fstat(fileno(ibuf), &stb) == -1 ||
295 (!S_ISREG(stb.st_mode) && !isdevnull)) {
296 n_perr(name, 0);
297 rele_sigs();
298 goto jem2;
302 /* Copy the messages into /tmp and set pointers */
303 if (!(fm & FEDIT_NEWMAIL)) {
304 if (isdevnull) {
305 mb.mb_type = MB_VOID;
306 mb.mb_perm = 0;
307 } else {
308 mb.mb_type = MB_FILE;
309 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
310 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
311 if (shudclob) {
312 if (mb.mb_itf) {
313 fclose(mb.mb_itf);
314 mb.mb_itf = NULL;
316 if (mb.mb_otf) {
317 fclose(mb.mb_otf);
318 mb.mb_otf = NULL;
322 shudclob = 1;
323 if (fm & FEDIT_SYSBOX)
324 pstate &= ~PS_EDIT;
325 else
326 pstate |= PS_EDIT;
327 initbox(name);
328 offset = 0;
329 } else {
330 fseek(mb.mb_otf, 0L, SEEK_END);
331 /* TODO Doing this without holding a lock is.. And not err checking.. */
332 fseek(ibuf, mailsize, SEEK_SET);
333 offset = mailsize;
334 omsgCount = msgCount;
337 if (isdevnull)
338 lckfp = (FILE*)-1;
339 else if (!(pstate & PS_EDIT))
340 lckfp = n_dotlock(name, fileno(ibuf), FLT_READ, offset,0,
341 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX));
342 else if (n_file_lock(fileno(ibuf), FLT_READ, offset,0,
343 (fm & FEDIT_NEWMAIL ? 0 : UIZ_MAX)))
344 lckfp = (FILE*)-1;
346 if (lckfp == NULL) {
347 if (!(fm & FEDIT_NEWMAIL)) {
348 char const *emsg = (pstate & PS_EDIT)
349 ? N_("Unable to lock mailbox, aborting operation")
350 : N_("Unable to (dot) lock mailbox, aborting operation");
351 n_perr(V_(emsg), 0);
353 rele_sigs();
354 if (!(fm & FEDIT_NEWMAIL))
355 goto jem1;
356 goto jleave;
359 mailsize = fsize(ibuf);
361 /* TODO This is too simple minded? We should regenerate an index file
362 * TODO to be able to truly tell whether *anything* has changed! */
363 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
364 rele_sigs();
365 goto jleave;
367 setptr(ibuf, offset);
368 setmsize(msgCount);
369 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
370 mb.mb_threaded = 0;
371 c_sort((void*)-1);
374 Fclose(ibuf);
375 ibuf = NULL;
376 if (lckfp != NULL && lckfp != (FILE*)-1) {
377 Pclose(lckfp, FAL0);
378 /*lckfp = NULL;*/
380 rele_sigs();
382 if (!(fm & FEDIT_NEWMAIL)) {
383 pstate &= ~PS_SAW_COMMAND;
384 pstate |= PS_SETFILE_OPENED;
387 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
388 rv = (msgCount == 0);
389 goto jleave;
392 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
393 if (!(fm & FEDIT_NEWMAIL)) {
394 if (!ok_blook(emptystart))
395 n_err(_("No mail for %s\n"), who);
397 goto jleave;
400 if (fm & FEDIT_NEWMAIL)
401 newmailinfo(omsgCount);
403 rv = 0;
404 jleave:
405 if (ibuf != NULL) {
406 Fclose(ibuf);
407 if (lckfp != NULL && lckfp != (FILE*)-1)
408 Pclose(lckfp, FAL0);
410 NYD_LEAVE;
411 return rv;
412 jem2:
413 mb.mb_type = MB_VOID;
414 jem1:
415 rv = -1;
416 goto jleave;
419 FL int
420 newmailinfo(int omsgCount)
422 int mdot, i;
423 NYD_ENTER;
425 for (i = 0; i < omsgCount; ++i)
426 message[i].m_flag &= ~MNEWEST;
428 if (msgCount > omsgCount) {
429 for (i = omsgCount; i < msgCount; ++i)
430 message[i].m_flag |= MNEWEST;
431 printf(_("New mail has arrived.\n"));
432 if ((i = msgCount - omsgCount) == 1)
433 printf(_("Loaded 1 new message.\n"));
434 else
435 printf(_("Loaded %d new messages.\n"), i);
436 } else
437 printf(_("Loaded %d messages.\n"), msgCount);
439 check_folder_hook(TRU1);
441 mdot = getmdot(1);
443 if (ok_blook(header))
444 print_headers(omsgCount + 1, msgCount, FAL0);
445 NYD_LEAVE;
446 return mdot;
449 FL void
450 setmsize(int sz)
452 NYD_ENTER;
453 if (n_msgvec != NULL)
454 free(n_msgvec);
455 n_msgvec = scalloc(sz + 1, sizeof *n_msgvec);
456 NYD_LEAVE;
459 FL void
460 print_header_summary(char const *Larg)
462 size_t bot, top, i, j;
463 NYD_ENTER;
465 if (Larg != NULL) {
466 /* Avoid any messages XXX add a make_mua_silent() and use it? */
467 if ((options & (OPT_VERB | OPT_EXISTONLY)) == OPT_EXISTONLY) {
468 freopen("/dev/null", "w", stdout);
469 freopen("/dev/null", "w", stderr);
471 assert(n_msgvec != NULL);
472 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), n_msgvec, 0) <= 0);
473 if (options & OPT_EXISTONLY) {
474 exit_status = (int)i;
475 goto jleave;
477 if (i)
478 goto jleave;
479 for (bot = msgCount, top = 0, i = 0; (j = n_msgvec[i]) != 0; ++i) {
480 if (bot > j)
481 bot = j;
482 if (top < j)
483 top = j;
485 } else
486 bot = 1, top = msgCount;
487 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
488 jleave:
489 NYD_LEAVE;
492 FL void
493 announce(int printheaders)
495 int vec[2], mdot;
496 NYD_ENTER;
498 mdot = newfileinfo();
499 vec[0] = mdot;
500 vec[1] = 0;
501 dot = message + mdot - 1;
502 if (printheaders && msgCount > 0 && ok_blook(header)) {
503 print_header_group(vec); /* XXX errors? */
505 NYD_LEAVE;
508 FL int
509 newfileinfo(void)
511 struct message *mp;
512 int u, n, mdot, d, s, hidden, moved;
513 NYD_ENTER;
515 if (mb.mb_type == MB_VOID) {
516 mdot = 1;
517 goto jleave;
520 mdot = getmdot(0);
521 s = d = hidden = moved =0;
522 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
523 if (mp->m_flag & MNEW)
524 ++n;
525 if ((mp->m_flag & MREAD) == 0)
526 ++u;
527 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
528 ++moved;
529 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
530 ++d;
531 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
532 ++s;
533 if (mp->m_flag & MHIDDEN)
534 ++hidden;
537 /* If displayname gets truncated the user effectively has no option to see
538 * the full pathname of the mailbox, so print it at least for '? fi' */
539 printf(_("%s: "), n_shexp_quote_cp(
540 (_update_mailname(NULL) ? displayname : mailname), FAL0));
541 if (msgCount == 1)
542 printf(_("1 message"));
543 else
544 printf(_("%d messages"), msgCount);
545 if (n > 0)
546 printf(_(" %d new"), n);
547 if (u-n > 0)
548 printf(_(" %d unread"), u);
549 if (d > 0)
550 printf(_(" %d deleted"), d);
551 if (s > 0)
552 printf(_(" %d saved"), s);
553 if (moved > 0)
554 printf(_(" %d moved"), moved);
555 if (hidden > 0)
556 printf(_(" %d hidden"), hidden);
557 else if (mb.mb_perm == 0)
558 printf(_(" [Read only]"));
559 printf("\n");
560 jleave:
561 NYD_LEAVE;
562 return mdot;
565 FL int
566 getmdot(int nmail)
568 struct message *mp;
569 char *cp;
570 int mdot;
571 enum mflag avoid = MHIDDEN | MDELETED;
572 NYD_ENTER;
574 if (!nmail) {
575 if (ok_blook(autothread)) {
576 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
577 c_thread(NULL);
578 } else if ((cp = ok_vlook(autosort)) != NULL) {
579 if (mb.mb_sorted != NULL)
580 free(mb.mb_sorted);
581 mb.mb_sorted = sstrdup(cp);
582 c_sort(NULL);
585 if (mb.mb_type == MB_VOID) {
586 mdot = 1;
587 goto jleave;
590 if (nmail)
591 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
592 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
593 break;
595 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
596 if (mb.mb_threaded) {
597 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
598 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
599 break;
600 } else {
601 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
602 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
603 break;
607 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
608 if (mb.mb_threaded) {
609 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
610 if (mp->m_flag & MFLAGGED)
611 break;
612 } else {
613 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
614 if (mp->m_flag & MFLAGGED)
615 break;
619 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
620 if (mb.mb_threaded) {
621 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
622 if (!(mp->m_flag & (MREAD | avoid)))
623 break;
624 } else {
625 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
626 if (!(mp->m_flag & (MREAD | avoid)))
627 break;
631 if (nmail &&
632 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
633 mdot = (int)PTR2SIZE(mp - message + 1);
634 else if (ok_blook(showlast)) {
635 if (mb.mb_threaded) {
636 for (mp = this_in_thread(threadroot, -1); mp;
637 mp = prev_in_thread(mp))
638 if (!(mp->m_flag & avoid))
639 break;
640 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
641 } else {
642 for (mp = message + msgCount - 1; mp >= message; --mp)
643 if (!(mp->m_flag & avoid))
644 break;
645 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
647 } else if (!nmail &&
648 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
649 mdot = (int)PTR2SIZE(mp - message + 1);
650 else if (mb.mb_threaded) {
651 for (mp = threadroot; mp; mp = next_in_thread(mp))
652 if (!(mp->m_flag & avoid))
653 break;
654 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
655 } else {
656 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
657 if (!(mp->m_flag & avoid))
658 break;
659 mdot = PTRCMP(mp, <, message + msgCount)
660 ? (int)PTR2SIZE(mp - message + 1) : 1;
662 jleave:
663 NYD_LEAVE;
664 return mdot;
667 FL void
668 initbox(char const *name)
670 char *tempMesg;
671 NYD_ENTER;
673 if (mb.mb_type != MB_VOID)
674 n_strscpy(prevfile, mailname, PATH_MAX);
676 /* TODO name always NE mailname (but goes away for objects anyway)
677 * TODO Well, not true no more except that in parens */
678 _update_mailname((name != mailname) ? name : NULL);
680 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
681 NULL) {
682 n_perr(_("temporary mail message file"), 0);
683 exit(EXIT_ERR);
685 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
686 n_perr(_("temporary mail message file"), 0);
687 exit(EXIT_ERR);
689 Ftmp_release(&tempMesg);
691 message_reset();
692 mb.mb_threaded = 0;
693 if (mb.mb_sorted != NULL) {
694 free(mb.mb_sorted);
695 mb.mb_sorted = NULL;
697 dot = prevdot = threadroot = NULL;
698 pstate &= ~PS_DID_PRINT_DOT;
699 NYD_LEAVE;
702 FL char const *
703 folder_query(void){
704 struct n_string s, *sp = &s;
705 char *cp;
706 char const *rv;
707 bool_t err;
708 NYD_ENTER;
710 sp = n_string_creat_auto(sp);
712 /* *folder* is linked with *_folder_resolved*: we only use the latter */
713 for(err = FAL0;;){
714 if((rv = ok_vlook(_folder_resolved)) != NULL)
715 break;
717 /* POSIX says:
718 * If directory does not start with a <slash> ('/'), the contents
719 * of HOME shall be prefixed to it.
720 * And:
721 * If folder is unset or set to null, [.] filenames beginning with
722 * '+' shall refer to files in the current directory.
723 * We may have the result already */
724 rv = "";
725 err = FAL0;
727 if((cp = ok_vlook(folder)) == NULL)
728 goto jset;
730 /* Expand the *folder*; skip %: prefix for simplicity of use */
731 if(cp[0] == '%' && cp[1] == ':')
732 cp += 2;
733 if((err = (cp = fexpand(cp, FEXP_NSPECIAL | FEXP_NFOLDER | FEXP_NSHELL)
734 ) == NULL) || *cp == '\0')
735 goto jset;
737 switch(which_protocol(cp)){
738 case PROTO_POP3:
739 n_err(_("*folder* can't be set to a flat, readonly POP3 account\n"));
740 err = TRU1;
741 goto jset;
742 default:
743 /* Further expansion desired */
744 break;
747 /* Prefix HOME as necessary */
748 if(*cp != '/'){ /* XXX path_is_absolute() */
749 size_t l1, l2;
750 char const *home;
752 home = ok_vlook(HOME);
753 l1 = strlen(home);
754 l2 = strlen(cp);
756 sp = n_string_reserve(sp, l1 + 1 + l2 +1);
757 sp = n_string_push_buf(sp, home, l1);
758 sp = n_string_push_c(sp, '/');
759 sp = n_string_push_buf(sp, cp, l2);
760 cp = n_string_cp(sp);
761 sp = n_string_drop_ownership(sp);
764 rv = cp;
766 /* TODO Since our visual mailname is resolved via realpath(3) if available
767 * TODO to avoid that we loose track of our currently open folder in case
768 * TODO we chdir away, but still checks the leading path portion against
769 * TODO folder_query() to be able to abbreviate to the +FOLDER syntax if
770 * TODO possible, we need to realpath(3) the folder, too */
771 #ifdef HAVE_REALPATH
772 assert(sp->s_len == 0 && sp->s_dat == NULL);
773 # ifndef HAVE_REALPATH_NULL
774 sp = n_string_reserve(sp, PATH_MAX +1);
775 # endif
777 if((sp->s_dat = realpath(cp, sp->s_dat)) != NULL){
778 # ifdef HAVE_REALPATH_NULL
779 n_string_cp(sp = n_string_assign_cp(sp, cp = sp->s_dat));
780 (free)(cp);
781 # endif
782 rv = sp->s_dat;
783 }else if(errno == ENOENT)
784 rv = cp;
785 else{
786 n_err(_("Can't canonicalize *folder*: %s\n"),
787 n_shexp_quote_cp(cp, FAL0));
788 err = TRU1;
789 rv = "";
791 sp = n_string_drop_ownership(sp);
792 #endif /* HAVE_REALPATH */
794 /* Always append a solidus to our result path upon success */
795 if(!err){
796 size_t i;
798 if(rv[(i = strlen(rv)) - 1] != '/'){
799 sp = n_string_reserve(sp, i + 1 +1);
800 sp = n_string_push_buf(sp, rv, i);
801 sp = n_string_push_c(sp, '/');
802 rv = n_string_cp(sp);
803 sp = n_string_drop_ownership(sp);
807 jset:
808 /* C99 */{
809 bool_t reset = !(pstate & PS_ROOT);
811 pstate |= PS_ROOT;
812 ok_vset(_folder_resolved, rv);
813 if(reset)
814 pstate &= ~PS_ROOT;
818 if(err){
819 n_err(_("*folder* is not resolvable, using CWD\n"));
820 assert(rv != NULL && *rv == '\0');
822 NYD_LEAVE;
823 return rv;
826 /* s-it-mode */