THANKS: Salvatore Bonaccorso
[s-mailx.git] / fio.c
blob9b24fd675d530dbb5b7dfb613d6250a1a213a913
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ File I/O, including resource file loading etc.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 fio
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #include <sys/wait.h>
44 #ifdef HAVE_SOCKETS
45 # include <sys/socket.h>
47 # include <netdb.h>
49 # include <netinet/in.h>
51 # ifdef HAVE_ARPA_INET_H
52 # include <arpa/inet.h>
53 # endif
54 #endif
56 #ifdef HAVE_WORDEXP
57 # include <wordexp.h>
58 #endif
60 #ifdef HAVE_OPENSSL
61 # include <openssl/err.h>
62 # include <openssl/rand.h>
63 # include <openssl/ssl.h>
64 # include <openssl/x509v3.h>
65 # include <openssl/x509.h>
66 #endif
68 #ifdef HAVE_DOTLOCK
69 # include "dotlock.h"
70 #endif
72 struct fio_stack {
73 FILE *s_file; /* File we were in. */
74 void *s_cond; /* Saved state of conditional stack */
75 int s_loading; /* Loading .mailrc, etc. */
78 struct shvar_stack {
79 struct shvar_stack *shs_next;
80 char const *shs_value; /* Remaining value to expand */
81 size_t shs_len; /* gth of .shs_dat this level */
82 char const *shs_dat; /* Result data of this level */
85 /* Slots in ::message */
86 static size_t _message_space;
88 /* XXX Our Popen() main() takes void, temporary global data store */
89 #ifdef HAVE_DOTLOCK
90 static enum file_lock_type _dotlock_flt;
91 static int _dotlock_fd;
92 struct dotlock_info * _dotlock_dip;
93 #endif
95 /* */
96 static struct fio_stack _fio_stack[FIO_STACK_SIZE];
97 static size_t _fio_stack_size;
98 static FILE * _fio_input;
100 /* Locate the user's mailbox file (where new, unread mail is queued) */
101 static void _findmail(char *buf, size_t bufsize, char const *user,
102 bool_t force);
104 /* Perform shell variable expansion */
105 static char * _shvar_exp(char const *name);
106 static char * __shvar_exp(struct shvar_stack *shsp);
108 /* Perform shell meta character expansion TODO obsolete (INSECURE!) */
109 static char * _globname(char const *name, enum fexp_mode fexpm);
111 /* line is a buffer with the result of fgets(). Returns the first newline or
112 * the last character read */
113 static size_t _length_of_line(char const *line, size_t linesize);
115 /* Read a line, one character at a time */
116 static char * _fgetline_byone(char **line, size_t *linesize, size_t *llen,
117 FILE *fp, int appendnl, size_t n SMALLOC_DEBUG_ARGS);
119 /* Take the data out of the passed ghost file and toss it into a dynamically
120 * allocated message structure */
121 static void makemessage(void);
123 static enum okay get_header(struct message *mp);
125 /* Workhorse */
126 static bool_t _file_lock(int fd, enum file_lock_type ft,
127 off_t off, off_t len);
129 /* main() of fork(2)ed dot file locker */
130 #ifdef HAVE_DOTLOCK
131 static int _dotlock_main(void);
132 #endif
134 /* Write to socket fd, restarting on EINTR, unless anything is written */
135 #ifdef HAVE_SOCKETS
136 static long xwrite(int fd, char const *data, size_t sz);
137 #endif
139 /* `source' and `source_if' */
140 static bool_t _source_file(char const *file, bool_t silent_error);
142 static void
143 _findmail(char *buf, size_t bufsize, char const *user, bool_t force)
145 char *cp;
146 NYD_ENTER;
148 if (!strcmp(user, myname) && !force && (cp = ok_vlook(folder)) != NULL) {
149 switch (which_protocol(cp)) {
150 case PROTO_IMAP:
151 if (strcmp(cp, protbase(cp)))
152 goto jcopy;
153 snprintf(buf, bufsize, "%s/INBOX", cp);
154 goto jleave;
155 default:
156 break;
160 if (force || (cp = ok_vlook(MAIL)) == NULL)
161 snprintf(buf, bufsize, "%s/%s", MAILSPOOL, user);
162 else
163 jcopy:
164 n_strlcpy(buf, cp, bufsize);
165 jleave:
166 NYD_LEAVE;
169 static char *
170 _shvar_exp(char const *name)
172 struct shvar_stack top;
173 char *rv;
174 NYD2_ENTER;
176 memset(&top, 0, sizeof top);
177 top.shs_value = name;
178 rv = __shvar_exp(&top);
180 NYD2_LEAVE;
181 return rv;
184 static char *
185 __shvar_exp(struct shvar_stack *shsp)
187 struct shvar_stack next, *np, *tmp;
188 char const *vp;
189 char lc, c, *cp, *rv;
190 size_t i;
191 NYD2_ENTER;
193 if (*(vp = shsp->shs_value) != '$') {
194 union {bool_t hadbs; char c;} u = {FAL0};
196 shsp->shs_dat = vp;
197 for (lc = '\0', i = 0; ((c = *vp) != '\0'); ++i, ++vp) {
198 if (c == '$' && lc != '\\')
199 break;
200 lc = (lc == '\\') ? (u.hadbs = TRU1, '\0') : c;
202 shsp->shs_len = i;
204 if (u.hadbs) {
205 shsp->shs_dat = cp = savestrbuf(shsp->shs_dat, i);
207 for (lc = '\0', rv = cp; (u.c = *cp++) != '\0';) {
208 if (u.c != '\\' || lc == '\\')
209 *rv++ = u.c;
210 lc = (lc == '\\') ? '\0' : u.c;
212 *rv = '\0';
214 shsp->shs_len = PTR2SIZE(rv - shsp->shs_dat);
216 } else {
217 if ((lc = (*++vp == '{')))
218 ++vp;
220 shsp->shs_dat = vp;
221 for (i = 0; (c = *vp) != '\0'; ++i, ++vp)
222 if (!alnumchar(c) && c != '_')
223 break;
225 if (lc) {
226 if (c != '}') {
227 n_err(_("Variable name misses closing \"}\": \"%s\"\n"),
228 shsp->shs_value);
229 shsp->shs_len = strlen(shsp->shs_value);
230 shsp->shs_dat = shsp->shs_value;
231 goto junroll;
233 c = *++vp;
236 shsp->shs_len = i;
237 if ((cp = vok_vlook(savestrbuf(shsp->shs_dat, i))) != NULL)
238 shsp->shs_len = strlen(shsp->shs_dat = cp);
240 if (c != '\0')
241 goto jrecurse;
243 /* That level made the great and completed encoding. Build result */
244 junroll:
245 for (i = 0, np = shsp, shsp = NULL; np != NULL;) {
246 i += np->shs_len;
247 tmp = np->shs_next;
248 np->shs_next = shsp;
249 shsp = np;
250 np = tmp;
253 cp = rv = salloc(i +1);
254 while (shsp != NULL) {
255 np = shsp;
256 shsp = shsp->shs_next;
257 memcpy(cp, np->shs_dat, np->shs_len);
258 cp += np->shs_len;
260 *cp = '\0';
262 jleave:
263 NYD2_LEAVE;
264 return rv;
265 jrecurse:
266 memset(&next, 0, sizeof next);
267 next.shs_next = shsp;
268 next.shs_value = vp;
269 rv = __shvar_exp(&next);
270 goto jleave;
273 static char *
274 _globname(char const *name, enum fexp_mode fexpm)
276 #ifdef HAVE_WORDEXP
277 wordexp_t we;
278 char *cp = NULL;
279 sigset_t nset;
280 int i;
281 NYD_ENTER;
283 /* Mac OS X Snow Leopard and Linux don't init fields on error, causing
284 * SIGSEGV in wordfree(3); so let's just always zero it ourselfs */
285 memset(&we, 0, sizeof we);
287 /* Some systems (notably Open UNIX 8.0.0) fork a shell for wordexp()
288 * and wait, which will fail if our SIGCHLD handler is active */
289 sigemptyset(&nset);
290 sigaddset(&nset, SIGCHLD);
291 sigprocmask(SIG_BLOCK, &nset, NULL);
292 # ifndef WRDE_NOCMD
293 # define WRDE_NOCMD 0
294 # endif
295 i = wordexp(name, &we, WRDE_NOCMD);
296 sigprocmask(SIG_UNBLOCK, &nset, NULL);
298 switch (i) {
299 case 0:
300 break;
301 #ifdef WRDE_CMDSUB
302 case WRDE_CMDSUB:
303 if (!(fexpm & FEXP_SILENT))
304 n_err(_("\"%s\": Command substitution not allowed\n"), name);
305 goto jleave;
306 #endif
307 case WRDE_NOSPACE:
308 if (!(fexpm & FEXP_SILENT))
309 n_err(_("\"%s\": Expansion buffer overflow\n"), name);
310 goto jleave;
311 case WRDE_BADCHAR:
312 case WRDE_SYNTAX:
313 default:
314 if (!(fexpm & FEXP_SILENT))
315 n_err(_("Syntax error in \"%s\"\n"), name);
316 goto jleave;
319 switch (we.we_wordc) {
320 case 1:
321 cp = savestr(we.we_wordv[0]);
322 break;
323 case 0:
324 if (!(fexpm & FEXP_SILENT))
325 n_err(_("\"%s\": No match\n"), name);
326 break;
327 default:
328 if (fexpm & FEXP_MULTIOK) {
329 size_t j, l;
331 for (l = 0, j = 0; j < we.we_wordc; ++j)
332 l += strlen(we.we_wordv[j]) + 1;
333 ++l;
334 cp = salloc(l);
335 for (l = 0, j = 0; j < we.we_wordc; ++j) {
336 size_t x = strlen(we.we_wordv[j]);
337 memcpy(cp + l, we.we_wordv[j], x);
338 l += x;
339 cp[l++] = ' ';
341 cp[l] = '\0';
342 } else if (!(fexpm & FEXP_SILENT))
343 n_err(_("\"%s\": Ambiguous\n"), name);
344 break;
346 jleave:
347 wordfree(&we);
348 NYD_LEAVE;
349 return cp;
351 #else /* HAVE_WORDEXP */
352 struct stat sbuf;
353 char xname[PATH_MAX], cmdbuf[PATH_MAX], /* also used for files */
354 *shellp, *cp = NULL;
355 int pivec[2], pid, l, waits;
356 NYD_ENTER;
358 if (pipe(pivec) < 0) {
359 n_perr(_("pipe"), 0);
360 goto jleave;
362 snprintf(cmdbuf, sizeof cmdbuf, "echo %s", name);
363 if ((shellp = ok_vlook(SHELL)) == NULL)
364 shellp = UNCONST(XSHELL);
365 pid = start_command(shellp, NULL, -1, pivec[1], "-c", cmdbuf, NULL, NULL);
366 if (pid < 0) {
367 close(pivec[0]);
368 close(pivec[1]);
369 goto jleave;
371 close(pivec[1]);
373 jagain:
374 l = read(pivec[0], xname, sizeof xname);
375 if (l < 0) {
376 if (errno == EINTR)
377 goto jagain;
378 n_perr(_("read"), 0);
379 close(pivec[0]);
380 goto jleave;
382 close(pivec[0]);
383 if (!wait_child(pid, &waits) && WTERMSIG(waits) != SIGPIPE) {
384 if (!(fexpm & FEXP_SILENT))
385 n_err(_("\"%s\": Expansion failed\n"), name);
386 goto jleave;
388 if (l == 0) {
389 if (!(fexpm & FEXP_SILENT))
390 n_err(_("\"%s\": No match\n"), name);
391 goto jleave;
393 if (l == sizeof xname) {
394 if (!(fexpm & FEXP_SILENT))
395 n_err(_("\"%s\": Expansion buffer overflow\n"), name);
396 goto jleave;
398 xname[l] = 0;
399 for (cp = xname + l - 1; *cp == '\n' && cp > xname; --cp)
401 cp[1] = '\0';
402 if (!(fexpm & FEXP_MULTIOK) && strchr(xname, ' ') != NULL &&
403 stat(xname, &sbuf) < 0) {
404 if (!(fexpm & FEXP_SILENT))
405 n_err(_("\"%s\": Ambiguous\n"), name);
406 cp = NULL;
407 goto jleave;
409 cp = savestr(xname);
410 jleave:
411 NYD_LEAVE;
412 return cp;
413 #endif /* !HAVE_WORDEXP */
416 static size_t
417 _length_of_line(char const *line, size_t linesize)
419 size_t i;
420 NYD2_ENTER;
422 /* Last character is always '\0' and was added by fgets() */
423 for (--linesize, i = 0; i < linesize; i++)
424 if (line[i] == '\n')
425 break;
426 i = (i < linesize) ? i + 1 : linesize;
427 NYD2_LEAVE;
428 return i;
431 static char *
432 _fgetline_byone(char **line, size_t *linesize, size_t *llen, FILE *fp,
433 int appendnl, size_t n SMALLOC_DEBUG_ARGS)
435 char *rv;
436 int c;
437 NYD2_ENTER;
439 assert(*linesize == 0 || *line != NULL);
440 for (rv = *line;;) {
441 if (*linesize <= LINESIZE || n >= *linesize - 128) {
442 *linesize += ((rv == NULL) ? LINESIZE + n + 1 : 256);
443 *line = rv = (srealloc)(rv, *linesize SMALLOC_DEBUG_ARGSCALL);
445 c = getc(fp);
446 if (c != EOF) {
447 rv[n++] = c;
448 rv[n] = '\0';
449 if (c == '\n')
450 break;
451 } else {
452 if (n > 0) {
453 if (appendnl) {
454 rv[n++] = '\n';
455 rv[n] = '\0';
457 break;
458 } else {
459 rv = NULL;
460 goto jleave;
464 if (llen)
465 *llen = n;
466 jleave:
467 NYD2_LEAVE;
468 return rv;
471 static void
472 makemessage(void)
474 NYD_ENTER;
475 if (msgCount == 0)
476 message_append(NULL);
477 setdot(message);
478 message[msgCount].m_size = 0;
479 message[msgCount].m_lines = 0;
480 NYD_LEAVE;
483 static enum okay
484 get_header(struct message *mp)
486 enum okay rv;
487 NYD_ENTER;
488 UNUSED(mp);
490 switch (mb.mb_type) {
491 case MB_FILE:
492 case MB_MAILDIR:
493 rv = OKAY;
494 break;
495 #ifdef HAVE_POP3
496 case MB_POP3:
497 rv = pop3_header(mp);
498 break;
499 #endif
500 #ifdef HAVE_IMAP
501 case MB_IMAP:
502 case MB_CACHE:
503 rv = imap_header(mp);
504 break;
505 #endif
506 case MB_VOID:
507 default:
508 rv = STOP;
509 break;
511 NYD_LEAVE;
512 return rv;
515 static bool_t
516 _file_lock(int fd, enum file_lock_type flt, off_t off, off_t len)
518 struct flock flp;
519 bool_t rv;
520 NYD2_ENTER;
522 memset(&flp, 0, sizeof flp);
524 switch (flt) {
525 default:
526 case FLT_READ: rv = F_RDLCK; break;
527 case FLT_WRITE: rv = F_WRLCK; break;
529 flp.l_type = rv;
530 flp.l_start = off;
531 flp.l_whence = SEEK_SET;
532 flp.l_len = len;
534 rv = (fcntl(fd, F_SETLK, &flp) != -1);
535 NYD2_LEAVE;
536 return rv;
539 #ifdef HAVE_DOTLOCK
540 static int
541 _dotlock_main(void)
543 struct dotlock_info di;
544 struct stat stb, fdstb;
545 char name[NAME_MAX];
546 enum dotlock_state dls;
547 char const *cp;
548 int fd;
549 enum file_lock_type flt;
550 NYD_ENTER;
552 /* Ignore SIGPIPE, we'll see EPIPE and "fall through" */
553 safe_signal(SIGPIPE, SIG_IGN);
555 /* Get the arguments "passed to us" */
556 flt = _dotlock_flt;
557 fd = _dotlock_fd;
558 UNUSED(fd);
559 di = *_dotlock_dip;
561 /* chdir(2)? */
562 jislink:
563 dls = DLS_CANT_CHDIR | DLS_ABANDON;
565 if ((cp = strrchr(di.di_file_name, '/')) != NULL) {
566 char const *fname = cp + 1;
568 while (PTRCMP(cp - 1, >, di.di_file_name) && cp[-1] == '/')
569 --cp;
570 cp = savestrbuf(di.di_file_name, PTR2SIZE(cp - di.di_file_name));
571 if (chdir(cp))
572 goto jmsg;
574 di.di_file_name = fname;
577 /* So we're here, but then again the file can be a symbolic link!
578 * This is however only true if we do not have realpath(3) available since
579 * that'll have resolved the path already otherwise; nonetheless, let
580 * readlink(2) be a precondition for dotlocking and keep this code */
581 if (lstat(cp = di.di_file_name, &stb) == -1)
582 goto jmsg;
583 if (S_ISLNK(stb.st_mode)) {
584 /* Use salloc() and hope we stay in builtin buffer.. */
585 char *x;
586 size_t i;
587 ssize_t sr;
589 for (x = NULL, i = PATH_MAX;; i += PATH_MAX) {
590 x = salloc(i +1);
591 sr = readlink(cp, x, i);
592 if (sr <= 0) {
593 dls = DLS_FISHY | DLS_ABANDON;
594 goto jmsg;
596 if (UICMP(z, sr, <, i)) {
597 x[sr] = '\0';
598 i = (size_t)sr;
599 break;
602 di.di_file_name = x;
603 goto jislink;
606 dls = DLS_FISHY | DLS_ABANDON;
608 /* Bail out if the file has changed its identity in the meanwhile */
609 if (fstat(fd, &fdstb) == -1 ||
610 fdstb.st_dev != stb.st_dev || fdstb.st_ino != stb.st_ino ||
611 fdstb.st_uid != stb.st_uid || fdstb.st_gid != stb.st_gid ||
612 fdstb.st_mode != stb.st_mode)
613 goto jmsg;
615 /* Be aware, even if the error is false! */
617 int i = snprintf(name, sizeof name, "%s.lock", di.di_file_name);
618 if (i < 0 || UICMP(z, NAME_MAX, <=, (uiz_t)i + 1 +1)) {
619 dls = DLS_NAMETOOLONG | DLS_ABANDON;
620 goto jmsg;
622 # ifdef HAVE_PATHCONF
623 else {
624 /* fd is a file, not portable to use for _PC_NAME_MAX */
625 long pc;
627 if ((pc = pathconf(".", _PC_NAME_MAX)) == -1 || pc <= (long)i + 1 +1) {
628 dls = DLS_NAMETOOLONG | DLS_ABANDON;
629 goto jmsg;
632 # endif
633 di.di_lock_name = name;
636 /* We are in the directory of the mailbox for which we have to create
637 * a dotlock file for. We don't know wether we have realpath(3) available,
638 * and manually resolving the path is due especially given that S-nail
639 * supports the special "%:" syntax to warp any file into a "system
640 * mailbox"; there may also be multiple system mailbox directories...
641 * So what we do is that we fstat(2) the mailbox and check its UID and
642 * GID against that of our own process: if any of those mismatch we must
643 * either assume a directory we are not allowed to write in, or that we run
644 * via -u/$USER/%USER as someone else, in which case we favour our
645 * privilege-separated dotlock process */
646 assert(cp != NULL); /* Ugly: avoid a useless var and reuse that one */
647 if (access(".", W_OK)) {
648 /* This may however also indicate a read-only filesystem, which is not
649 * really an error from our point of view since the mailbox will degrade
650 * to a readonly one for which no dotlock is needed, then, and errors
651 * may arise only due to actions which require box modifications */
652 if (errno == EROFS) {
653 dls = DLS_ROFS | DLS_ABANDON;
654 goto jmsg;
656 cp = NULL;
658 if (cp == NULL || stb.st_uid != user_id || stb.st_gid != group_id) {
659 char itoabuf[64];
660 char const *args[13];
662 snprintf(itoabuf, sizeof itoabuf, "%" PRIuZ, di.di_pollmsecs);
663 args[ 0] = PRIVSEP;
664 args[ 1] = (flt == FLT_READ ? "rdotlock" : "wdotlock");
665 args[ 2] = "mailbox"; args[ 3] = di.di_file_name;
666 args[ 4] = "name"; args[ 5] = di.di_lock_name;
667 args[ 6] = "hostname"; args[ 7] = di.di_hostname;
668 args[ 8] = "randstr"; args[ 9] = di.di_randstr;
669 args[10] = "pollmsecs"; args[11] = itoabuf;
670 args[12] = NULL;
671 execv(LIBEXECDIR "/" UAGENT "-privsep", UNCONST(args));
673 dls = DLS_NOEXEC;
674 write(STDOUT_FILENO, &dls, sizeof dls);
675 /* But fall through and try it with normal privileges! */
678 /* So let's try and call it ourselfs! Note that we don't block signals just
679 * like our privsep child does, the user will anyway be able to remove his
680 * file again, and if we're in -u/$USER mode then we are allowed to access
681 * the user's box: shall we leave behind a stale dotlock then at least we
682 * start a friendly human conversation. Since we cannot handle SIGKILL and
683 * SIGSTOP malicious things could happen whatever we do */
684 safe_signal(SIGHUP, SIG_IGN);
685 safe_signal(SIGINT, SIG_IGN);
686 safe_signal(SIGQUIT, SIG_IGN);
687 safe_signal(SIGTERM, SIG_IGN);
689 NYD;
690 dls = _dotlock_create(&di);
691 NYD;
693 /* Finally: notify our parent about the actual lock state.. */
694 jmsg:
695 write(STDOUT_FILENO, &dls, sizeof dls);
696 close(STDOUT_FILENO);
698 /* ..then eventually wait until we shall remove the lock again, which will
699 * be notified via the read returning */
700 if (dls == DLS_NONE) {
701 read(STDIN_FILENO, &dls, sizeof dls);
703 unlink(name);
705 NYD_LEAVE;
706 return EXIT_OK;
708 #endif /* HAVE_DOTLOCK */
710 #ifdef HAVE_SOCKETS
711 static long
712 xwrite(int fd, char const *data, size_t sz)
714 long rv = -1, wo;
715 size_t wt = 0;
716 NYD_ENTER;
718 do {
719 if ((wo = write(fd, data + wt, sz - wt)) < 0) {
720 if (errno == EINTR)
721 continue;
722 else
723 goto jleave;
725 wt += wo;
726 } while (wt < sz);
727 rv = (long)sz;
728 jleave:
729 NYD_LEAVE;
730 return rv;
732 #endif /* HAVE_SOCKETS */
734 static bool_t
735 _source_file(char const *file, bool_t silent_error)
737 char *cp;
738 FILE *fi = NULL;
739 NYD_ENTER;
741 if ((cp = fexpand(file, FEXP_LOCAL)) == NULL)
742 goto jleave;
743 if ((fi = Fopen(cp, "r")) == NULL) {
744 if (!silent_error || (options & OPT_D_V))
745 n_perr(cp, 0);
746 goto jleave;
749 if (temporary_localopts_store != NULL) {
750 n_err(_("Before v15 you cannot `source' from within macros, sorry\n"));
751 goto jeclose;
753 if (_fio_stack_size >= NELEM(_fio_stack)) {
754 n_err(_("Too many `source' recursions\n"));
755 jeclose:
756 Fclose(fi);
757 fi = NULL;
758 goto jleave;
761 _fio_stack[_fio_stack_size].s_file = _fio_input;
762 _fio_stack[_fio_stack_size].s_cond = condstack_release();
763 _fio_stack[_fio_stack_size].s_loading = !!(pstate & PS_LOADING);
764 ++_fio_stack_size;
765 pstate &= ~PS_LOADING;
766 pstate |= PS_SOURCING;
767 _fio_input = fi;
768 jleave:
769 NYD_LEAVE;
770 return (fi != NULL);
773 FL char *
774 (fgetline)(char **line, size_t *linesize, size_t *cnt, size_t *llen, FILE *fp,
775 int appendnl SMALLOC_DEBUG_ARGS)
777 size_t i_llen, sz;
778 char *rv;
779 NYD2_ENTER;
781 if (cnt == NULL) {
782 /* Without count, we can't determine where the chars returned by fgets()
783 * end if there's no newline. We have to read one character by one */
784 rv = _fgetline_byone(line, linesize, llen, fp, appendnl, 0
785 SMALLOC_DEBUG_ARGSCALL);
786 goto jleave;
789 if ((rv = *line) == NULL || *linesize < LINESIZE)
790 *line = rv = (srealloc)(rv, *linesize = LINESIZE SMALLOC_DEBUG_ARGSCALL);
791 sz = (*linesize <= *cnt) ? *linesize : *cnt + 1;
792 if (sz <= 1 || fgets(rv, sz, fp) == NULL) {
793 /* Leave llen untouched; it is used to determine whether the last line
794 * was \n-terminated in some callers */
795 rv = NULL;
796 goto jleave;
799 i_llen = _length_of_line(rv, sz);
800 *cnt -= i_llen;
801 while (rv[i_llen - 1] != '\n') {
802 *line = rv = (srealloc)(rv, *linesize += 256 SMALLOC_DEBUG_ARGSCALL);
803 sz = *linesize - i_llen;
804 sz = (sz <= *cnt) ? sz : *cnt + 1;
805 if (sz <= 1 || fgets(rv + i_llen, sz, fp) == NULL) {
806 if (appendnl) {
807 rv[i_llen++] = '\n';
808 rv[i_llen] = '\0';
810 break;
812 sz = _length_of_line(rv + i_llen, sz);
813 i_llen += sz;
814 *cnt -= sz;
816 if (llen)
817 *llen = i_llen;
818 jleave:
819 NYD2_LEAVE;
820 return rv;
823 FL int
824 (readline_restart)(FILE *ibuf, char **linebuf, size_t *linesize, size_t n
825 SMALLOC_DEBUG_ARGS)
827 /* TODO readline_restart(): always *appends* LF just to strip it again;
828 * TODO should be configurable just as for fgetline(); ..or whatever.. */
829 int rv = -1;
830 long sz;
831 NYD2_ENTER;
833 clearerr(ibuf);
835 /* Interrupts will cause trouble if we are inside a stdio call. As this is
836 * only relevant if input is from tty, bypass it by read(), then */
837 if (fileno(ibuf) == 0 && (options & OPT_TTYIN)) {
838 assert(*linesize == 0 || *linebuf != NULL);
839 for (;;) {
840 if (*linesize <= LINESIZE || n >= *linesize - 128) {
841 *linesize += ((*linebuf == NULL) ? LINESIZE + n + 1 : 256);
842 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
844 jagain:
845 sz = read(0, *linebuf + n, *linesize - n - 1);
846 if (sz > 0) {
847 n += sz;
848 (*linebuf)[n] = '\0';
849 if (n > 0 && (*linebuf)[n - 1] == '\n')
850 break;
851 } else {
852 if (sz < 0 && errno == EINTR)
853 goto jagain;
854 if (n > 0) {
855 if ((*linebuf)[n - 1] != '\n') {
856 (*linebuf)[n++] = '\n';
857 (*linebuf)[n] = '\0';
859 break;
860 } else
861 goto jleave;
864 } else {
865 /* Not reading from standard input or standard input not a terminal. We
866 * read one char at a time as it is the only way to get lines with
867 * embedded NUL characters in standard stdio */
868 if (_fgetline_byone(linebuf, linesize, &n, ibuf, 1, n
869 SMALLOC_DEBUG_ARGSCALL) == NULL)
870 goto jleave;
872 if (n > 0 && (*linebuf)[n - 1] == '\n')
873 (*linebuf)[--n] = '\0';
874 rv = (int)n;
875 jleave:
876 NYD2_LEAVE;
877 return rv;
880 FL int
881 (readline_input)(char const *prompt, bool_t nl_escape, char **linebuf,
882 size_t *linesize, char const *string SMALLOC_DEBUG_ARGS)
884 /* TODO readline: linebuf pool! */
885 FILE *ifile = (_fio_input != NULL) ? _fio_input : stdin;
886 bool_t doprompt, dotty;
887 int n, nold;
888 NYD2_ENTER;
890 doprompt = (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE));
891 dotty = (doprompt && !ok_blook(line_editor_disable));
892 if (!doprompt)
893 prompt = NULL;
894 else if (prompt == NULL)
895 prompt = getprompt();
897 /* Ensure stdout is flushed first anyway */
898 if (!dotty && prompt == NULL)
899 fflush(stdout);
901 for (nold = n = 0;;) {
902 if (dotty) {
903 assert(ifile == stdin);
904 if (string != NULL && (n = (int)strlen(string)) > 0) {
905 if (*linesize > 0)
906 *linesize += n +1;
907 else
908 *linesize = (size_t)n + LINESIZE +1;
909 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
910 memcpy(*linebuf, string, (size_t)n +1);
912 string = NULL;
913 /* TODO if nold>0, don't redisplay the entire line!
914 * TODO needs complete redesign ... */
915 n = (tty_readline)(prompt, linebuf, linesize, n
916 SMALLOC_DEBUG_ARGSCALL);
917 } else {
918 if (prompt != NULL) {
919 if (*prompt != '\0')
920 fputs(prompt, stdout);
921 fflush(stdout);
923 n = (readline_restart)(ifile, linebuf, linesize, n
924 SMALLOC_DEBUG_ARGSCALL);
926 if (n > 0 && nold > 0) {
927 int i = 0;
928 char const *cp = *linebuf + nold;
930 while (blankspacechar(*cp) && nold + i < n)
931 ++cp, ++i;
932 if (i > 0) {
933 memmove(*linebuf + nold, cp, n - nold - i);
934 n -= i;
935 (*linebuf)[n] = '\0';
939 if (n <= 0)
940 break;
942 /* POSIX says:
943 * An unquoted <backslash> at the end of a command line shall
944 * be discarded and the next line shall continue the command */
945 if (!nl_escape || n == 0 || (*linebuf)[n - 1] != '\\')
946 break;
947 (*linebuf)[nold = --n] = '\0';
948 if (prompt != NULL && *prompt != '\0')
949 prompt = ".. "; /* XXX PS2 .. */
952 if (n >= 0 && (options & OPT_D_VV))
953 n_err(_("%s %d bytes <%.*s>\n"),
954 ((pstate & PS_LOADING) ? "LOAD"
955 : (pstate & PS_SOURCING) ? "SOURCE" : "READ"),
956 n, n, *linebuf);
957 NYD2_LEAVE;
958 return n;
961 FL char *
962 n_input_cp_addhist(char const *prompt, char const *string, bool_t isgabby)
964 /* FIXME n_input_cp_addhist(): leaks on sigjmp without linepool */
965 size_t linesize = 0;
966 char *linebuf = NULL, *rv = NULL;
967 int n;
968 NYD2_ENTER;
970 n = readline_input(prompt, FAL0, &linebuf, &linesize, string);
971 if (n > 0 && *(rv = savestrbuf(linebuf, (size_t)n + 1)) != '\0' &&
972 (options & OPT_INTERACTIVE))
973 tty_addhist(rv, isgabby);
975 if (linebuf != NULL)
976 free(linebuf);
977 NYD2_LEAVE;
978 return rv;
981 FL void
982 setptr(FILE *ibuf, off_t offset)
984 struct message self;
985 char *cp, *linebuf = NULL;
986 char const *cp2;
987 int c, maybe = 1, inhead = 0, selfcnt = 0;
988 size_t linesize = 0, filesize, cnt;
989 NYD_ENTER;
991 memset(&self, 0, sizeof self);
992 self.m_flag = MUSED | MNEW | MNEWEST;
993 filesize = mailsize - offset;
994 offset = ftell(mb.mb_otf);
996 for (;;) {
997 if (fgetline(&linebuf, &linesize, &filesize, &cnt, ibuf, 0) == NULL) {
998 self.m_xsize = self.m_size;
999 self.m_xlines = self.m_lines;
1000 self.m_have = HAVE_HEADER | HAVE_BODY;
1001 if (selfcnt > 0)
1002 message_append(&self);
1003 makemessage();
1004 if (linebuf)
1005 free(linebuf);
1006 break;
1009 #ifdef notdef
1010 if (linebuf[0] == '\0')
1011 linebuf[0] = '.';
1012 #endif
1013 /* XXX Convert CRLF to LF; this should be rethought in that
1014 * XXX CRLF input should possibly end as CRLF output? */
1015 if (cnt >= 2 && linebuf[cnt - 1] == '\n' && linebuf[cnt - 2] == '\r')
1016 linebuf[--cnt - 1] = '\n';
1017 fwrite(linebuf, sizeof *linebuf, cnt, mb.mb_otf);
1018 if (ferror(mb.mb_otf)) {
1019 n_perr(_("/tmp"), 0);
1020 exit(EXIT_ERR);
1022 if (linebuf[cnt - 1] == '\n')
1023 linebuf[cnt - 1] = '\0';
1024 if (maybe && linebuf[0] == 'F' && is_head(linebuf, cnt, FAL0)) {
1025 /* TODO char date[FROM_DATEBUF];
1026 * TODO extract_date_from_from_(linebuf, cnt, date);
1027 * TODO self.m_time = 10000; */
1028 self.m_xsize = self.m_size;
1029 self.m_xlines = self.m_lines;
1030 self.m_have = HAVE_HEADER | HAVE_BODY;
1031 if (selfcnt++ > 0)
1032 message_append(&self);
1033 msgCount++;
1034 self.m_flag = MUSED | MNEW | MNEWEST;
1035 self.m_size = 0;
1036 self.m_lines = 0;
1037 self.m_block = mailx_blockof(offset);
1038 self.m_offset = mailx_offsetof(offset);
1039 inhead = 1;
1040 } else if (linebuf[0] == 0) {
1041 inhead = 0;
1042 } else if (inhead) {
1043 for (cp = linebuf, cp2 = "status";; ++cp) {
1044 if ((c = *cp2++) == 0) {
1045 while (c = *cp++, whitechar(c))
1047 if (cp[-1] != ':')
1048 break;
1049 while ((c = *cp++) != '\0')
1050 if (c == 'R')
1051 self.m_flag |= MREAD;
1052 else if (c == 'O')
1053 self.m_flag &= ~MNEW;
1054 break;
1056 if (*cp != c && *cp != upperconv(c))
1057 break;
1059 for (cp = linebuf, cp2 = "x-status";; ++cp) {
1060 if ((c = *cp2++) == 0) {
1061 while ((c = *cp++, whitechar(c)))
1063 if (cp[-1] != ':')
1064 break;
1065 while ((c = *cp++) != '\0')
1066 if (c == 'F')
1067 self.m_flag |= MFLAGGED;
1068 else if (c == 'A')
1069 self.m_flag |= MANSWERED;
1070 else if (c == 'T')
1071 self.m_flag |= MDRAFTED;
1072 break;
1074 if (*cp != c && *cp != upperconv(c))
1075 break;
1078 offset += cnt;
1079 self.m_size += cnt;
1080 ++self.m_lines;
1081 maybe = linebuf[0] == 0;
1083 NYD_LEAVE;
1086 FL int
1087 putline(FILE *obuf, char *linebuf, size_t cnt)
1089 int rv = -1;
1090 NYD_ENTER;
1092 fwrite(linebuf, sizeof *linebuf, cnt, obuf);
1093 putc('\n', obuf);
1094 if (!ferror(obuf))
1095 rv = (int)(cnt + 1);
1096 NYD_LEAVE;
1097 return rv;
1100 FL FILE *
1101 setinput(struct mailbox *mp, struct message *m, enum needspec need)
1103 FILE *rv = NULL;
1104 enum okay ok = STOP;
1105 NYD_ENTER;
1107 switch (need) {
1108 case NEED_HEADER:
1109 ok = (m->m_have & HAVE_HEADER) ? OKAY : get_header(m);
1110 break;
1111 case NEED_BODY:
1112 ok = (m->m_have & HAVE_BODY) ? OKAY : get_body(m);
1113 break;
1114 case NEED_UNSPEC:
1115 ok = OKAY;
1116 break;
1118 if (ok != OKAY)
1119 goto jleave;
1121 fflush(mp->mb_otf);
1122 if (fseek(mp->mb_itf, (long)mailx_positionof(m->m_block, m->m_offset),
1123 SEEK_SET) < 0) {
1124 n_perr(_("fseek"), 0);
1125 n_panic(_("temporary file seek"));
1127 rv = mp->mb_itf;
1128 jleave:
1129 NYD_LEAVE;
1130 return rv;
1133 FL void
1134 message_reset(void)
1136 NYD_ENTER;
1137 if (message != NULL) {
1138 free(message);
1139 message = NULL;
1141 msgCount = 0;
1142 _message_space = 0;
1143 NYD_LEAVE;
1146 FL void
1147 message_append(struct message *mp)
1149 NYD_ENTER;
1150 if (UICMP(z, msgCount + 1, >=, _message_space)) {
1151 /* XXX remove _message_space magics (or use s_Vector) */
1152 _message_space = (_message_space >= 128 && _message_space <= 1000000)
1153 ? _message_space << 1 : _message_space + 64;
1154 message = srealloc(message, _message_space * sizeof *message);
1156 if (msgCount > 0) {
1157 if (mp != NULL)
1158 message[msgCount - 1] = *mp;
1159 else
1160 memset(message + msgCount - 1, 0, sizeof *message);
1162 NYD_LEAVE;
1165 FL bool_t
1166 message_match(struct message *mp, struct search_expr const *sep,
1167 bool_t with_headers)
1169 char **line;
1170 size_t *linesize, cnt;
1171 FILE *fp;
1172 bool_t rv = FAL0;
1173 NYD_ENTER;
1175 if ((fp = Ftmp(NULL, "mpmatch", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
1176 NULL)
1177 goto j_leave;
1179 if (sendmp(mp, fp, NULL, NULL, SEND_TOSRCH, NULL) < 0)
1180 goto jleave;
1181 fflush_rewind(fp);
1183 cnt = fsize(fp);
1184 line = &termios_state.ts_linebuf; /* XXX line pool */
1185 linesize = &termios_state.ts_linesize; /* XXX line pool */
1187 if (!with_headers)
1188 while (fgetline(line, linesize, &cnt, NULL, fp, 0))
1189 if (**line == '\n')
1190 break;
1192 while (fgetline(line, linesize, &cnt, NULL, fp, 0)) {
1193 #ifdef HAVE_REGEX
1194 if (sep->ss_sexpr == NULL) {
1195 if (regexec(&sep->ss_regex, *line, 0,NULL, 0) == REG_NOMATCH)
1196 continue;
1197 } else
1198 #endif
1199 if (!substr(*line, sep->ss_sexpr))
1200 continue;
1201 rv = TRU1;
1202 break;
1205 jleave:
1206 Fclose(fp);
1207 j_leave:
1208 NYD_LEAVE;
1209 return rv;
1212 FL struct message *
1213 setdot(struct message *mp)
1215 NYD_ENTER;
1216 if (dot != mp) {
1217 prevdot = dot;
1218 pstate &= ~PS_DID_PRINT_DOT;
1220 dot = mp;
1221 uncollapse1(dot, 0);
1222 NYD_LEAVE;
1223 return dot;
1226 FL int
1227 rm(char const *name)
1229 struct stat sb;
1230 int rv = -1;
1231 NYD_ENTER;
1233 if (stat(name, &sb) < 0)
1235 else if (!S_ISREG(sb.st_mode))
1236 errno = EISDIR;
1237 else
1238 rv = unlink(name);
1239 NYD_LEAVE;
1240 return rv;
1243 FL off_t
1244 fsize(FILE *iob)
1246 struct stat sbuf;
1247 off_t rv;
1248 NYD_ENTER;
1250 rv = (fstat(fileno(iob), &sbuf) < 0) ? 0 : sbuf.st_size;
1251 NYD_LEAVE;
1252 return rv;
1255 FL char *
1256 fexpand(char const *name, enum fexp_mode fexpm)
1258 char cbuf[PATH_MAX];
1259 char const *res;
1260 struct str s;
1261 bool_t dyn;
1262 NYD_ENTER;
1264 /* The order of evaluation is "%" and "#" expand into constants.
1265 * "&" can expand into "+". "+" can expand into shell meta characters.
1266 * Shell meta characters expand into constants.
1267 * This way, we make no recursive expansion */
1268 if ((fexpm & FEXP_NSHORTCUT) || (res = shortcut_expand(name)) == NULL)
1269 res = UNCONST(name);
1271 if (fexpm & FEXP_SHELL) {
1272 dyn = FAL0;
1273 goto jshell;
1275 jnext:
1276 dyn = FAL0;
1277 switch (*res) {
1278 case '%':
1279 if (res[1] == ':' && res[2] != '\0') {
1280 res = &res[2];
1281 goto jnext;
1283 _findmail(cbuf, sizeof cbuf, (res[1] != '\0' ? res + 1 : myname),
1284 (res[1] != '\0' || (options & OPT_u_FLAG)));
1285 res = cbuf;
1286 goto jislocal;
1287 case '#':
1288 if (res[1] != '\0')
1289 break;
1290 if (prevfile[0] == '\0') {
1291 n_err(_("No previous file\n"));
1292 res = NULL;
1293 goto jleave;
1295 res = prevfile;
1296 goto jislocal;
1297 case '&':
1298 if (res[1] == '\0') {
1299 if ((res = ok_vlook(MBOX)) == NULL)
1300 res = UNCONST("~/mbox"); /* XXX no magics (POSIX though) */
1301 else if (res[0] != '&' || res[1] != '\0')
1302 goto jnext;
1304 break;
1307 if (res[0] == '@' && which_protocol(mailname) == PROTO_IMAP) {
1308 res = str_concat_csvl(&s, protbase(mailname), "/", res + 1, NULL)->s;
1309 dyn = TRU1;
1312 if (res[0] == '+' && getfold(cbuf, sizeof cbuf)) {
1313 size_t i = strlen(cbuf);
1315 res = str_concat_csvl(&s, cbuf,
1316 ((i > 0 && cbuf[i - 1] == '/') ? "" : "/"), res + 1, NULL)->s;
1317 dyn = TRU1;
1319 if (res[0] == '%' && res[1] == ':') {
1320 res += 2;
1321 goto jnext;
1325 /* Catch the most common shell meta character */
1326 jshell:
1327 if (res[0] == '~' && (res[1] == '/' || res[1] == '\0')) {
1328 res = str_concat_csvl(&s, homedir, res + 1, NULL)->s;
1329 dyn = TRU1;
1331 if (anyof(res, "|&;<>{}()[]*?$`'\"\\"))
1332 switch (which_protocol(res)) {
1333 case PROTO_FILE:
1334 case PROTO_MAILDIR:
1335 res = (fexpm & FEXP_NSHELL) ? _shvar_exp(res) : _globname(res, fexpm);
1336 dyn = TRU1;
1337 goto jleave;
1338 default:
1339 break;
1341 jislocal:
1342 if (fexpm & FEXP_LOCAL)
1343 switch (which_protocol(res)) {
1344 case PROTO_FILE:
1345 case PROTO_MAILDIR:
1346 break;
1347 default:
1348 n_err(_("Not a local file or directory: \"%s\"\n"), name);
1349 res = NULL;
1350 break;
1352 jleave:
1353 if (res && !dyn)
1354 res = savestr(res);
1355 NYD_LEAVE;
1356 return UNCONST(res);
1359 FL char *
1360 fexpand_nshell_quote(char const *name)
1362 size_t i, j;
1363 char *rv, c;
1364 NYD_ENTER;
1366 for (i = j = 0; (c = name[i]) != '\0'; ++i)
1367 if (c == '\\')
1368 ++j;
1370 if (j == 0)
1371 rv = savestrbuf(name, i);
1372 else {
1373 rv = salloc(i + j +1);
1374 for (i = j = 0; (c = name[i]) != '\0'; ++i) {
1375 rv[j++] = c;
1376 if (c == '\\')
1377 rv[j++] = c;
1379 rv[j] = '\0';
1381 NYD_LEAVE;
1382 return rv;
1385 FL bool_t
1386 var_folder_updated(char const *name, char **store)
1388 char rv = TRU1;
1389 char *folder, *unres = NULL, *res = NULL;
1390 NYD_ENTER;
1392 if ((folder = UNCONST(name)) == NULL)
1393 goto jleave;
1395 /* Expand the *folder*; skip %: prefix for simplicity of use */
1396 /* XXX This *only* works because we do NOT
1397 * XXX update environment variables via the "set" mechanism */
1398 if (folder[0] == '%' && folder[1] == ':')
1399 folder += 2;
1400 if ((folder = fexpand(folder, FEXP_FULL)) == NULL) /* XXX error? */
1401 goto jleave;
1403 switch (which_protocol(folder)) {
1404 case PROTO_POP3:
1405 n_err(_("*folder* cannot be set to a flat, readonly POP3 account\n"));
1406 rv = FAL0;
1407 goto jleave;
1408 case PROTO_IMAP:
1409 /* Simply assign what we have, even including `%:' prefix */
1410 if (folder != name)
1411 goto jvcopy;
1412 goto jleave;
1413 default:
1414 /* Further expansion desired */
1415 break;
1418 /* All non-absolute paths are relative to our home directory */
1419 if (*folder != '/') {
1420 size_t l1 = strlen(homedir), l2 = strlen(folder);
1421 unres = ac_alloc(l1 + l2 + 1 +1);
1422 memcpy(unres, homedir, l1);
1423 unres[l1] = '/';
1424 memcpy(unres + l1 + 1, folder, l2);
1425 unres[l1 + 1 + l2] = '\0';
1426 folder = unres;
1429 /* Since lex.c:_update_mailname() uses realpath(3) if available to
1430 * avoid that we loose track of our currently open folder in case we
1431 * chdir away, but still checks the leading path portion against
1432 * getfold() to be able to abbreviate to the +FOLDER syntax if
1433 * possible, we need to realpath(3) the folder, too */
1434 #ifdef HAVE_REALPATH
1435 res = ac_alloc(PATH_MAX +1);
1436 if (realpath(folder, res) == NULL)
1437 n_err(_("Can't canonicalize \"%s\"\n"), folder);
1438 else
1439 folder = res;
1440 #endif
1442 jvcopy:
1443 *store = sstrdup(folder);
1445 if (res != NULL)
1446 ac_free(res);
1447 if (unres != NULL)
1448 ac_free(unres);
1449 jleave:
1450 NYD_LEAVE;
1451 return rv;
1454 FL bool_t
1455 getfold(char *name, size_t size)
1457 char const *folder;
1458 NYD_ENTER;
1460 if ((folder = ok_vlook(folder)) != NULL)
1461 n_strlcpy(name, folder, size);
1462 NYD_LEAVE;
1463 return (folder != NULL);
1466 FL char const *
1467 getdeadletter(void) /* XXX should that be in auxlily.c? */
1469 char const *cp;
1470 NYD_ENTER;
1472 if ((cp = ok_vlook(DEAD)) == NULL || (cp = fexpand(cp, FEXP_LOCAL)) == NULL)
1473 cp = fexpand("~/dead.letter", FEXP_LOCAL | FEXP_SHELL);
1474 else if (*cp != '/') {
1475 size_t sz = strlen(cp) + 2 +1;
1476 char *buf = ac_alloc(sz);
1478 snprintf(buf, sz, "~/%s", cp);
1479 cp = fexpand(buf, FEXP_LOCAL | FEXP_SHELL);
1480 ac_free(buf);
1483 if (cp == NULL)
1484 cp = "dead.letter"; /* XXX magic -> nail.h (POSIX thing though) */
1485 NYD_LEAVE;
1486 return cp;
1489 FL enum okay
1490 get_body(struct message *mp)
1492 enum okay rv;
1493 NYD_ENTER;
1494 UNUSED(mp);
1496 switch (mb.mb_type) {
1497 case MB_FILE:
1498 case MB_MAILDIR:
1499 rv = OKAY;
1500 break;
1501 #ifdef HAVE_POP3
1502 case MB_POP3:
1503 rv = pop3_body(mp);
1504 break;
1505 #endif
1506 #ifdef HAVE_IMAP
1507 case MB_IMAP:
1508 case MB_CACHE:
1509 rv = imap_body(mp);
1510 break;
1511 #endif
1512 case MB_VOID:
1513 default:
1514 rv = STOP;
1515 break;
1517 NYD_LEAVE;
1518 return rv;
1521 FL bool_t
1522 file_lock(int fd, enum file_lock_type flt, off_t off, off_t len,
1523 size_t pollmsecs)
1525 size_t tries;
1526 bool_t rv;
1527 NYD_ENTER;
1529 for (tries = 0; tries <= FILE_LOCK_TRIES; ++tries)
1530 if ((rv = _file_lock(fd, flt, off, len)) || pollmsecs == 0)
1531 break;
1532 else
1533 sleep(1); /* TODO pollmsecs -> use finer grain */
1534 NYD_LEAVE;
1535 return rv;
1538 FL FILE *
1539 dot_lock(char const *fname, int fd, enum file_lock_type flt,
1540 off_t off, off_t len, size_t pollmsecs)
1542 #undef _DOMSG
1543 #ifdef HAVE_DOTLOCK
1544 # define _DOMSG() n_err(_("Creating dotlock for \"%s\" "), fname)
1545 #else
1546 # define _DOMSG() n_err(_("Trying to lock file \"%s\" "), fname)
1547 #endif
1549 #ifdef HAVE_DOTLOCK
1550 int cpipe[2];
1551 struct dotlock_info di;
1552 enum dotlock_state dls;
1553 char const *emsg = NULL;
1554 #endif
1555 int UNINIT(serrno, 0);
1556 union {size_t tries; int (*ptf)(void); char const *sh; ssize_t r;} u;
1557 bool_t flocked, didmsg = FAL0;
1558 FILE *rv = NULL;
1559 NYD_ENTER;
1561 if (options & OPT_D_VV) {
1562 _DOMSG();
1563 didmsg = TRUM1;
1566 flocked = FAL0;
1567 for (u.tries = 0; !_file_lock(fd, flt, off, len);)
1568 switch ((serrno = errno)) {
1569 case EACCES:
1570 case EAGAIN:
1571 case ENOLCK:
1572 if (pollmsecs > 0 && ++u.tries < FILE_LOCK_TRIES) {
1573 if (!didmsg)
1574 _DOMSG();
1575 n_err(".");
1576 didmsg = TRUM1;
1577 sleep(1); /* TODO pollmsecs -> use finer grain */
1578 continue;
1580 /* FALLTHRU */
1581 default:
1582 goto jleave;
1584 flocked = TRU1;
1586 #ifndef HAVE_DOTLOCK
1587 jleave:
1588 if (didmsg == TRUM1)
1589 n_err("\n");
1590 if (flocked)
1591 rv = (FILE*)-1;
1592 else
1593 errno = serrno;
1594 NYD_LEAVE;
1595 return rv;
1597 #else
1598 /* Create control-pipe for our dot file locker process, which will remove
1599 * the lock and terminate once the pipe is closed, for whatever reason */
1600 if (pipe_cloexec(cpipe) == -1) {
1601 serrno = errno;
1602 emsg = N_(" Can't create file lock control pipe\n");
1603 goto jemsg;
1606 /* And the locker process itself; it'll be a (rather cheap) thread only
1607 * unless the lock has to be placed in the system spool and we have our
1608 * privilege-separated dotlock program available, in which case that will be
1609 * executed and do "it" with changed group-id */
1610 di.di_file_name = fname;
1611 di.di_pollmsecs = pollmsecs;
1612 /* Initialize some more stuff; query the two strings in the parent in order
1613 * to cache the result of the former and anyway minimalize child page-ins.
1614 * Especially uname(3) may hang for multiple seconds when it is called the
1615 * first time! */
1616 di.di_hostname = nodename(FAL0);
1617 di.di_randstr = getrandstring(16);
1618 _dotlock_flt = flt;
1619 _dotlock_fd = fd;
1620 _dotlock_dip = &di;
1622 u.ptf = &_dotlock_main;
1623 rv = Popen((char*)-1, "W", u.sh, NULL, cpipe[1]);
1624 serrno = errno;
1626 close(cpipe[1]);
1627 if (rv == NULL) {
1628 close(cpipe[0]);
1629 emsg = N_(" Can't create file lock process\n");
1630 goto jemsg;
1633 /* Let's check wether we were able to create the dotlock file */
1634 for (;;) {
1635 u.r = read(cpipe[0], &dls, sizeof dls);
1636 if (UICMP(z, u.r, !=, sizeof dls)) {
1637 serrno = (u.r != -1) ? EAGAIN : errno;
1638 dls = DLS_DUNNO | DLS_ABANDON;
1639 } else
1640 serrno = 0;
1642 if (dls == DLS_NONE || (dls & DLS_ABANDON))
1643 close(cpipe[0]);
1645 switch (dls & ~DLS_ABANDON) {
1646 case DLS_NONE:
1647 goto jleave;
1648 case DLS_CANT_CHDIR:
1649 if (options & OPT_D_V)
1650 emsg = N_(" Can't change to directory! Please check permissions\n");
1651 serrno = EACCES;
1652 break;
1653 case DLS_NAMETOOLONG:
1654 emsg = N_("Resulting dotlock filename would be too long\n");
1655 serrno = EACCES;
1656 break;
1657 case DLS_ROFS:
1658 assert(dls & DLS_ABANDON);
1659 if (options & OPT_D_V)
1660 emsg = N_(" Read-only filesystem, not creating lock file\n");
1661 serrno = EROFS;
1662 break;
1663 case DLS_NOPERM:
1664 if (options & OPT_D_V)
1665 emsg = N_(" Can't create a lock file! Please check permissions\n"
1666 " (Maybe setting *dotlock-ignore-error* variable helps.)\n");
1667 serrno = EACCES;
1668 break;
1669 case DLS_NOEXEC:
1670 if (options & OPT_D_V)
1671 emsg = N_(" Can't find privilege-separated file lock program\n");
1672 serrno = ENOENT;
1673 break;
1674 case DLS_PRIVFAILED:
1675 emsg = N_(" Privilege-separated file lock program can't change "
1676 "privileges\n");
1677 serrno = EPERM;
1678 break;
1679 case DLS_EXIST:
1680 emsg = N_(" It seems there is a stale dotlock file?\n"
1681 " Please remove the lock file manually, then retry\n");
1682 serrno = EEXIST;
1683 break;
1684 case DLS_FISHY:
1685 emsg = N_(" Fishy! Is someone trying to \"steal\" foreign files?\n"
1686 " Please check the mailbox file etc. manually, then retry\n");
1687 serrno = EAGAIN; /* ? Hack to ignore *dotlock-ignore-error* xxx */
1688 break;
1689 default:
1690 case DLS_DUNNO:
1691 emsg = N_(" Unspecified dotlock file control process error.\n"
1692 " Like broken I/O pipe; this one is unlikely to happen\n");
1693 if (serrno != EAGAIN)
1694 serrno = EINVAL;
1695 break;
1696 case DLS_PING:
1697 if (!didmsg)
1698 _DOMSG();
1699 n_err(".");
1700 didmsg = TRUM1;
1701 continue;
1704 if (emsg != NULL) {
1705 if (!didmsg) {
1706 _DOMSG();
1707 didmsg = TRUM1;
1709 if (didmsg == TRUM1)
1710 n_err("\n");
1711 didmsg = TRU1;
1712 n_err(V_(emsg));
1713 emsg = NULL;
1716 if (dls & DLS_ABANDON) {
1717 Pclose(rv, FAL0);
1718 rv = NULL;
1719 break;
1723 jleave:
1724 if (didmsg == TRUM1)
1725 n_err("\n");
1726 if (rv == NULL) {
1727 if (flocked && (serrno == EROFS ||
1728 (serrno != EAGAIN && serrno != EEXIST &&
1729 ok_blook(dotlock_ignore_error))))
1730 rv = (FILE*)-1;
1731 else
1732 errno = serrno;
1734 NYD_LEAVE;
1735 return rv;
1736 jemsg:
1737 if (!didmsg)
1738 _DOMSG();
1739 n_err("\n");
1740 didmsg = TRU1;
1741 n_err(V_(emsg));
1742 goto jleave;
1743 #endif /* HAVE_DOTLOCK */
1744 #undef _DOMSG
1747 #ifdef HAVE_SOCKETS
1748 FL int
1749 sclose(struct sock *sp)
1751 int i;
1752 NYD_ENTER;
1754 i = sp->s_fd;
1755 sp->s_fd = -1;
1756 /* TODO NOTE: we MUST NOT close the descriptor 0 here...
1757 * TODO of course this should be handled in a VMAILFS->open() .s_fd=-1,
1758 * TODO but unfortunately it isn't yet */
1759 if (i <= 0)
1760 i = 0;
1761 else {
1762 if (sp->s_onclose != NULL)
1763 (*sp->s_onclose)();
1764 if (sp->s_wbuf != NULL)
1765 free(sp->s_wbuf);
1766 # ifdef HAVE_OPENSSL
1767 if (sp->s_use_ssl) {
1768 void *s_ssl = sp->s_ssl;
1770 sp->s_ssl = NULL;
1771 sp->s_use_ssl = 0;
1772 while (!SSL_shutdown(s_ssl)) /* XXX proper error handling;signals! */
1774 SSL_free(s_ssl);
1776 # endif
1777 i = close(i);
1779 NYD_LEAVE;
1780 return i;
1783 FL enum okay
1784 swrite(struct sock *sp, char const *data)
1786 enum okay rv;
1787 NYD2_ENTER;
1789 rv = swrite1(sp, data, strlen(data), 0);
1790 NYD2_LEAVE;
1791 return rv;
1794 FL enum okay
1795 swrite1(struct sock *sp, char const *data, int sz, int use_buffer)
1797 enum okay rv = STOP;
1798 int x;
1799 NYD2_ENTER;
1801 if (use_buffer > 0) {
1802 int di;
1804 if (sp->s_wbuf == NULL) {
1805 sp->s_wbufsize = 4096;
1806 sp->s_wbuf = smalloc(sp->s_wbufsize);
1807 sp->s_wbufpos = 0;
1809 while (sp->s_wbufpos + sz > sp->s_wbufsize) {
1810 di = sp->s_wbufsize - sp->s_wbufpos;
1811 sz -= di;
1812 if (sp->s_wbufpos > 0) {
1813 memcpy(sp->s_wbuf + sp->s_wbufpos, data, di);
1814 rv = swrite1(sp, sp->s_wbuf, sp->s_wbufsize, -1);
1815 } else
1816 rv = swrite1(sp, data, sp->s_wbufsize, -1);
1817 if (rv != OKAY)
1818 goto jleave;
1819 data += di;
1820 sp->s_wbufpos = 0;
1822 if (sz == sp->s_wbufsize) {
1823 rv = swrite1(sp, data, sp->s_wbufsize, -1);
1824 if (rv != OKAY)
1825 goto jleave;
1826 } else if (sz) {
1827 memcpy(sp->s_wbuf+ sp->s_wbufpos, data, sz);
1828 sp->s_wbufpos += sz;
1830 rv = OKAY;
1831 goto jleave;
1832 } else if (use_buffer == 0 && sp->s_wbuf != NULL && sp->s_wbufpos > 0) {
1833 x = sp->s_wbufpos;
1834 sp->s_wbufpos = 0;
1835 if ((rv = swrite1(sp, sp->s_wbuf, x, -1)) != OKAY)
1836 goto jleave;
1838 if (sz == 0) {
1839 rv = OKAY;
1840 goto jleave;
1843 # ifdef HAVE_OPENSSL
1844 if (sp->s_use_ssl) {
1845 jssl_retry:
1846 x = SSL_write(sp->s_ssl, data, sz);
1847 if (x < 0) {
1848 switch (SSL_get_error(sp->s_ssl, x)) {
1849 case SSL_ERROR_WANT_READ:
1850 case SSL_ERROR_WANT_WRITE:
1851 goto jssl_retry;
1854 } else
1855 # endif
1857 x = xwrite(sp->s_fd, data, sz);
1859 if (x != sz) {
1860 char o[512];
1861 snprintf(o, sizeof o, "%s write error",
1862 (sp->s_desc ? sp->s_desc : "socket"));
1863 # ifdef HAVE_OPENSSL
1864 if (sp->s_use_ssl)
1865 ssl_gen_err("%s", o);
1866 else
1867 # endif
1868 n_perr(o, 0);
1869 if (x < 0)
1870 sclose(sp);
1871 rv = STOP;
1872 goto jleave;
1874 rv = OKAY;
1875 jleave:
1876 NYD2_LEAVE;
1877 return rv;
1880 static sigjmp_buf __sopen_actjmp; /* TODO someday, we won't need it no more */
1881 static int __sopen_sig; /* TODO someday, we won't need it no more */
1882 static void
1883 __sopen_onsig(int sig) /* TODO someday, we won't need it no more */
1885 NYD_X; /* Signal handler */
1886 if (__sopen_sig < 0) {
1887 /* Of course the following doesn't belong into a signal handler XXX */
1888 int i, j;
1890 if (__sopen_sig == -1) {
1891 fprintf(stderr,
1892 _("\nInterrupting it could turn the (GNU/Linux+) DNS resolver "
1893 "unusable.\n"
1894 " Wait until it's done, or do terminate the program\n"));
1895 __sopen_sig = -2;
1896 } else if ((i = j = ABS(__sopen_sig)) + 15 < scrnwidth) {
1897 putc('\r', stderr);
1898 for (; j > 0; --j)
1899 putc(' ', stderr);
1900 fputs("___( o)", stderr);
1901 putc((i & 1) ? '=' : '>', stderr);
1902 putc(' ', stderr);
1903 putc(' ', stderr);
1904 ++i;
1905 __sopen_sig = -i;
1907 } else {
1908 __sopen_sig = sig;
1909 siglongjmp(__sopen_actjmp, 1);
1913 FL bool_t
1914 sopen(struct sock *sp, struct url *urlp) /* TODO sighandling; refactor */
1916 # ifdef HAVE_SO_SNDTIMEO
1917 struct timeval tv;
1918 # endif
1919 # ifdef HAVE_SO_LINGER
1920 struct linger li;
1921 # endif
1922 # ifdef HAVE_GETADDRINFO
1923 char hbuf[NI_MAXHOST];
1924 struct addrinfo hints, *res0 = NULL, *res;
1925 # else
1926 struct sockaddr_in servaddr;
1927 struct in_addr **pptr;
1928 struct hostent *hp;
1929 struct servent *ep;
1930 # endif
1931 sighandler_type volatile ohup, oint;
1932 char const * volatile serv;
1933 int volatile sofd = -1, errval;
1934 NYD_ENTER;
1936 UNINIT(errval, 0);
1938 /* Connect timeouts after 30 seconds XXX configurable */
1939 # ifdef HAVE_SO_SNDTIMEO
1940 tv.tv_sec = 30;
1941 tv.tv_usec = 0;
1942 # endif
1943 serv = (urlp->url_port != NULL) ? urlp->url_port : urlp->url_proto;
1945 if (options & OPT_VERB)
1946 n_err(_("Resolving host \"%s:%s\" ... "),
1947 urlp->url_host.s, serv);
1949 /* Signal handling (in respect to __sopen_sig dealing) is heavy, but no
1950 * healing until v15.0 and i want to end up with that functionality */
1951 hold_sigs();
1952 __sopen_sig = 0;
1953 ohup = safe_signal(SIGHUP, &__sopen_onsig);
1954 oint = safe_signal(SIGINT, &__sopen_onsig);
1955 if (sigsetjmp(__sopen_actjmp, 0)) {
1956 jpseudo_jump:
1957 n_err("%s\n",
1958 (__sopen_sig == SIGHUP ? _("Hangup") : _("Interrupted")));
1959 if (sofd >= 0) {
1960 close(sofd);
1961 sofd = -1;
1963 goto jjumped;
1965 rele_sigs();
1967 # ifdef HAVE_GETADDRINFO
1968 for (;;) {
1969 memset(&hints, 0, sizeof hints);
1970 hints.ai_socktype = SOCK_STREAM;
1971 __sopen_sig = -1;
1972 errval = getaddrinfo(urlp->url_host.s, serv, &hints, &res0);
1973 if (__sopen_sig != -1) {
1974 __sopen_sig = SIGINT;
1975 goto jpseudo_jump;
1977 __sopen_sig = 0;
1978 if (errval == 0)
1979 break;
1981 if (options & OPT_VERB)
1982 n_err(_("failed\n"));
1983 n_err(_("Lookup of \"%s:%s\" failed: %s\n"),
1984 urlp->url_host.s, serv, gai_strerror(errval));
1986 /* Error seems to depend on how "smart" the /etc/service code is: is it
1987 * "able" to state wether the service as such is NONAME or does it only
1988 * check for the given ai_socktype.. */
1989 if (errval == EAI_NONAME || errval == EAI_SERVICE) {
1990 if (serv == urlp->url_proto &&
1991 (serv = url_servbyname(urlp, NULL)) != NULL) {
1992 n_err(_(" Trying standard protocol port \"%s\"\n"
1993 " If that succeeds consider including the port in the URL!\n"),
1994 serv);
1995 continue;
1997 if (serv != urlp->url_port)
1998 n_err(_(" Including a port number in the URL may "
1999 "circumvent this problem\n"));
2001 assert(sofd == -1);
2002 errval = 0;
2003 goto jjumped;
2005 if (options & OPT_VERB)
2006 n_err(_("done\n"));
2008 for (res = res0; res != NULL && sofd < 0; res = res->ai_next) {
2009 if (options & OPT_VERB) {
2010 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof hbuf,
2011 NULL, 0, NI_NUMERICHOST))
2012 memcpy(hbuf, "unknown host", sizeof("unknown host"));
2013 n_err(_("%sConnecting to \"%s:%s\" ..."),
2014 (res == res0 ? "" : "\n"), hbuf, serv);
2017 sofd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2018 if (sofd >= 0) {
2019 # ifdef HAVE_SO_SNDTIMEO
2020 (void)setsockopt(sofd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
2021 # endif
2022 if (connect(sofd, res->ai_addr, res->ai_addrlen)) {
2023 errval = errno;
2024 close(sofd);
2025 sofd = -1;
2030 jjumped:
2031 if (res0 != NULL) {
2032 freeaddrinfo(res0);
2033 res0 = NULL;
2036 # else /* HAVE_GETADDRINFO */
2037 if (serv == urlp->url_proto) {
2038 if ((ep = getservbyname(UNCONST(serv), "tcp")) != NULL)
2039 urlp->url_portno = ep->s_port;
2040 else {
2041 if (options & OPT_VERB)
2042 n_err(_("failed\n"));
2043 if ((serv = url_servbyname(urlp, &urlp->url_portno)) != NULL)
2044 n_err(_(" Unknown service: \"%s\"\n"
2045 " Trying standard protocol port \"%s\"\n"
2046 " If that succeeds consider including the port in the URL!\n"),
2047 urlp->url_proto, serv);
2048 else {
2049 n_err(_(" Unknown service: \"%s\"\n"
2050 " Including a port in the URL may circumvent this problem\n"),
2051 urlp->url_proto);
2052 assert(sofd == -1 && errval == 0);
2053 goto jjumped;
2058 __sopen_sig = -1;
2059 hp = gethostbyname(urlp->url_host.s);
2060 if (__sopen_sig != -1) {
2061 __sopen_sig = SIGINT;
2062 goto jpseudo_jump;
2064 __sopen_sig = 0;
2066 if (hp == NULL) {
2067 char const *emsg;
2069 if (options & OPT_VERB)
2070 n_err(_("failed\n"));
2071 switch (h_errno) {
2072 case HOST_NOT_FOUND: emsg = N_("host not found"); break;
2073 default:
2074 case TRY_AGAIN: emsg = N_("(maybe) try again later"); break;
2075 case NO_RECOVERY: emsg = N_("non-recoverable server error"); break;
2076 case NO_DATA: emsg = N_("valid name without IP address"); break;
2078 n_err(_("Lookup of \"%s:%s\" failed: %s\n"),
2079 urlp->url_host.s, serv, V_(emsg));
2080 goto jjumped;
2081 } else if (options & OPT_VERB)
2082 n_err(_("done\n"));
2084 pptr = (struct in_addr**)hp->h_addr_list;
2085 if ((sofd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
2086 n_perr(_("could not create socket"), 0);
2087 assert(sofd == -1 && errval == 0);
2088 goto jjumped;
2091 memset(&servaddr, 0, sizeof servaddr);
2092 servaddr.sin_family = AF_INET;
2093 servaddr.sin_port = htons(urlp->url_portno);
2094 memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr));
2095 if (options & OPT_VERB)
2096 n_err(_("%sConnecting to \"%s:%d\" ... "),
2097 "", inet_ntoa(**pptr), (int)urlp->url_portno);
2098 # ifdef HAVE_SO_SNDTIMEO
2099 (void)setsockopt(sofd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
2100 # endif
2101 if (connect(sofd, (struct sockaddr*)&servaddr, sizeof servaddr)) {
2102 errval = errno;
2103 close(sofd);
2104 sofd = -1;
2106 jjumped:
2107 # endif /* !HAVE_GETADDRINFO */
2109 hold_sigs();
2110 safe_signal(SIGINT, oint);
2111 safe_signal(SIGHUP, ohup);
2112 rele_sigs();
2114 if (sofd < 0) {
2115 if (errval != 0) {
2116 errno = errval;
2117 n_perr(_("Could not connect"), 0);
2119 goto jleave;
2122 if (options & OPT_VERB)
2123 n_err(_("connected.\n"));
2125 /* And the regular timeouts XXX configurable */
2126 # ifdef HAVE_SO_SNDTIMEO
2127 tv.tv_sec = 42;
2128 tv.tv_usec = 0;
2129 (void)setsockopt(sofd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
2130 (void)setsockopt(sofd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
2131 # endif
2132 # ifdef HAVE_SO_LINGER
2133 li.l_onoff = 1;
2134 li.l_linger = 42;
2135 (void)setsockopt(sofd, SOL_SOCKET, SO_LINGER, &li, sizeof li);
2136 # endif
2138 memset(sp, 0, sizeof *sp);
2139 sp->s_fd = sofd;
2141 /* SSL/TLS upgrade? */
2142 # ifdef HAVE_SSL
2143 if (urlp->url_needs_tls) {
2144 hold_sigs();
2145 ohup = safe_signal(SIGHUP, &__sopen_onsig);
2146 oint = safe_signal(SIGINT, &__sopen_onsig);
2147 if (sigsetjmp(__sopen_actjmp, 0)) {
2148 n_err(_("%s during SSL/TLS handshake\n"),
2149 (__sopen_sig == SIGHUP ? _("Hangup") : _("Interrupted")));
2150 goto jsclose;
2152 rele_sigs();
2154 if (ssl_open(urlp, sp) != OKAY) {
2155 jsclose:
2156 sclose(sp);
2157 sofd = -1;
2160 hold_sigs();
2161 safe_signal(SIGINT, oint);
2162 safe_signal(SIGHUP, ohup);
2163 rele_sigs();
2165 # endif /* HAVE_SSL */
2167 jleave:
2168 /* May need to bounce the signal to the lex.c trampoline (or wherever) */
2169 if (__sopen_sig != 0) {
2170 sigset_t cset;
2171 sigemptyset(&cset);
2172 sigaddset(&cset, __sopen_sig);
2173 sigprocmask(SIG_UNBLOCK, &cset, NULL);
2174 n_raise(__sopen_sig);
2176 NYD_LEAVE;
2177 return (sofd >= 0);
2180 FL int
2181 (sgetline)(char **line, size_t *linesize, size_t *linelen, struct sock *sp
2182 SMALLOC_DEBUG_ARGS)
2184 int rv;
2185 size_t lsize;
2186 char *lp_base, *lp;
2187 NYD2_ENTER;
2189 lsize = *linesize;
2190 lp_base = *line;
2191 lp = lp_base;
2193 if (sp->s_rsz < 0) {
2194 sclose(sp);
2195 rv = sp->s_rsz;
2196 goto jleave;
2199 do {
2200 if (lp_base == NULL || PTRCMP(lp, >, lp_base + lsize - 128)) {
2201 size_t diff = PTR2SIZE(lp - lp_base);
2202 *linesize = (lsize += 256); /* XXX magic */
2203 *line = lp_base = (srealloc)(lp_base, lsize SMALLOC_DEBUG_ARGSCALL);
2204 lp = lp_base + diff;
2207 if (sp->s_rbufptr == NULL ||
2208 PTRCMP(sp->s_rbufptr, >=, sp->s_rbuf + sp->s_rsz)) {
2209 # ifdef HAVE_OPENSSL
2210 if (sp->s_use_ssl) {
2211 jssl_retry:
2212 sp->s_rsz = SSL_read(sp->s_ssl, sp->s_rbuf, sizeof sp->s_rbuf);
2213 if (sp->s_rsz <= 0) {
2214 if (sp->s_rsz < 0) {
2215 char o[512];
2216 switch(SSL_get_error(sp->s_ssl, sp->s_rsz)) {
2217 case SSL_ERROR_WANT_READ:
2218 case SSL_ERROR_WANT_WRITE:
2219 goto jssl_retry;
2221 snprintf(o, sizeof o, "%s",
2222 (sp->s_desc ? sp->s_desc : "socket"));
2223 ssl_gen_err("%s", o);
2225 break;
2227 } else
2228 # endif
2230 jagain:
2231 sp->s_rsz = read(sp->s_fd, sp->s_rbuf, sizeof sp->s_rbuf);
2232 if (sp->s_rsz <= 0) {
2233 if (sp->s_rsz < 0) {
2234 char o[512];
2235 if (errno == EINTR)
2236 goto jagain;
2237 snprintf(o, sizeof o, "%s",
2238 (sp->s_desc ? sp->s_desc : "socket"));
2239 n_perr(o, 0);
2241 break;
2244 sp->s_rbufptr = sp->s_rbuf;
2246 } while ((*lp++ = *sp->s_rbufptr++) != '\n');
2247 *lp = '\0';
2248 lsize = PTR2SIZE(lp - lp_base);
2250 if (linelen)
2251 *linelen = lsize;
2252 rv = (int)lsize;
2253 jleave:
2254 NYD2_LEAVE;
2255 return rv;
2257 #endif /* HAVE_SOCKETS */
2259 FL void
2260 load(char const *name)
2262 struct str n;
2263 void *cond;
2264 FILE *in, *oldin;
2265 NYD_ENTER;
2267 if (name == NULL || *name == '\0' || (in = Fopen(name, "r")) == NULL)
2268 goto jleave;
2270 oldin = _fio_input;
2271 _fio_input = in;
2272 pstate |= PS_IN_LOAD;
2273 /* commands() may sreset(), copy over file name */
2274 n.l = strlen(name);
2275 n.s = ac_alloc(n.l +1);
2276 memcpy(n.s, name, n.l +1);
2278 cond = condstack_release();
2279 if (!commands())
2280 n_err(_("Stopped loading \"%s\" due to errors "
2281 "(enable *debug* for trace)\n"), n.s);
2282 condstack_take(cond);
2284 ac_free(n.s);
2285 pstate &= ~PS_IN_LOAD;
2286 _fio_input = oldin;
2287 Fclose(in);
2288 jleave:
2289 NYD_LEAVE;
2292 FL int
2293 c_source(void *v)
2295 int rv;
2296 NYD_ENTER;
2298 rv = _source_file(*(char**)v, FAL0) ? 0 : 1;
2299 NYD_LEAVE;
2300 return rv;
2303 FL int
2304 c_source_if(void *v) /* XXX obsolete?, support file tests in `if' etc.! */
2306 int rv;
2307 NYD_ENTER;
2309 rv = _source_file(*(char**)v, TRU1) ? 0 : 1;
2310 rv = 0;
2311 NYD_LEAVE;
2312 return rv;
2315 FL int
2316 unstack(void)
2318 int rv = 1;
2319 NYD_ENTER;
2321 if (_fio_stack_size == 0) {
2322 n_err(_("`source' stack over-pop\n"));
2323 pstate &= ~PS_SOURCING;
2324 goto jleave;
2327 Fclose(_fio_input);
2329 --_fio_stack_size;
2330 if (!condstack_take(_fio_stack[_fio_stack_size].s_cond))
2331 n_err(_("Unmatched \"if\"\n"));
2332 if (_fio_stack[_fio_stack_size].s_loading)
2333 pstate |= PS_LOADING;
2334 else
2335 pstate &= ~PS_LOADING;
2336 _fio_input = _fio_stack[_fio_stack_size].s_file;
2337 if (_fio_stack_size == 0) {
2338 if (pstate & PS_LOADING)
2339 pstate |= PS_SOURCING;
2340 else
2341 pstate &= ~PS_SOURCING;
2343 rv = 0;
2344 jleave:
2345 NYD_LEAVE;
2346 return rv;
2349 /* s-it-mode */