Move path related code to new path.c
[s-mailx.git] / fio.c
blobc72d31f06d33834b95ed887b47665c7f022013fa
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 ui32_t s_pstate; /* Copy of ::pstate */
78 /* Slots in ::message */
79 static size_t _message_space;
81 /* XXX Our Popen() main() takes void, temporary global data store */
82 #ifdef HAVE_DOTLOCK
83 static enum file_lock_type _dotlock_flt;
84 static int _dotlock_fd;
85 struct dotlock_info * _dotlock_dip;
86 #endif
88 /* */
89 static struct fio_stack _fio_stack[FIO_STACK_SIZE];
90 static size_t _fio_stack_size;
91 static FILE * _fio_input;
93 /* Locate the user's mailbox file (where new, unread mail is queued) */
94 static void _findmail(char *buf, size_t bufsize, char const *user,
95 bool_t force);
97 /* Perform shell meta character expansion TODO obsolete (INSECURE!) */
98 static char * _globname(char const *name, enum fexp_mode fexpm);
100 /* line is a buffer with the result of fgets(). Returns the first newline or
101 * the last character read */
102 static size_t _length_of_line(char const *line, size_t linesize);
104 /* Read a line, one character at a time */
105 static char * _fgetline_byone(char **line, size_t *linesize, size_t *llen,
106 FILE *fp, int appendnl, size_t n SMALLOC_DEBUG_ARGS);
108 /* Take the data out of the passed ghost file and toss it into a dynamically
109 * allocated message structure */
110 static void makemessage(void);
112 static enum okay get_header(struct message *mp);
114 /* Workhorse */
115 static bool_t _file_lock(int fd, enum file_lock_type ft,
116 off_t off, off_t len);
118 /* main() of fork(2)ed dot file locker */
119 #ifdef HAVE_DOTLOCK
120 static int _dotlock_main(void);
121 #endif
123 /* Write to socket fd, restarting on EINTR, unless anything is written */
124 #ifdef HAVE_SOCKETS
125 static long xwrite(int fd, char const *data, size_t sz);
126 #endif
128 /* `source' and `source_if' (if silent_error: no pipes allowed, then) */
129 static bool_t _source_file(char const *file, bool_t silent_error);
131 static void
132 _findmail(char *buf, size_t bufsize, char const *user, bool_t force)
134 char *cp;
135 NYD_ENTER;
137 if (!strcmp(user, myname) && !force && (cp = ok_vlook(folder)) != NULL) {
141 if (force || (cp = ok_vlook(MAIL)) == NULL)
142 snprintf(buf, bufsize, "%s/%s", MAILSPOOL, user); /* TODO */
143 else
144 n_strscpy(buf, cp, bufsize);
145 NYD_LEAVE;
148 static char *
149 _globname(char const *name, enum fexp_mode fexpm)
151 #ifdef HAVE_WORDEXP
152 wordexp_t we;
153 char *cp = NULL;
154 sigset_t nset;
155 int i;
156 NYD_ENTER;
158 /* Mac OS X Snow Leopard and Linux don't init fields on error, causing
159 * SIGSEGV in wordfree(3); so let's just always zero it ourselfs */
160 memset(&we, 0, sizeof we);
162 /* Some systems (notably Open UNIX 8.0.0) fork a shell for wordexp()
163 * and wait, which will fail if our SIGCHLD handler is active */
164 sigemptyset(&nset);
165 sigaddset(&nset, SIGCHLD);
166 sigprocmask(SIG_BLOCK, &nset, NULL);
167 # ifndef WRDE_NOCMD
168 # define WRDE_NOCMD 0
169 # endif
170 i = wordexp(name, &we, WRDE_NOCMD);
171 sigprocmask(SIG_UNBLOCK, &nset, NULL);
173 switch (i) {
174 case 0:
175 break;
176 #ifdef WRDE_CMDSUB
177 case WRDE_CMDSUB:
178 if (!(fexpm & FEXP_SILENT))
179 n_err(_("\"%s\": Command substitution not allowed\n"), name);
180 goto jleave;
181 #endif
182 case WRDE_NOSPACE:
183 if (!(fexpm & FEXP_SILENT))
184 n_err(_("\"%s\": Expansion buffer overflow\n"), name);
185 goto jleave;
186 case WRDE_BADCHAR:
187 case WRDE_SYNTAX:
188 default:
189 if (!(fexpm & FEXP_SILENT))
190 n_err(_("Syntax error in \"%s\"\n"), name);
191 goto jleave;
194 switch (we.we_wordc) {
195 case 1:
196 cp = savestr(we.we_wordv[0]);
197 break;
198 case 0:
199 if (!(fexpm & FEXP_SILENT))
200 n_err(_("\"%s\": No match\n"), name);
201 break;
202 default:
203 if (fexpm & FEXP_MULTIOK) {
204 size_t j, l;
206 for (l = 0, j = 0; j < we.we_wordc; ++j)
207 l += strlen(we.we_wordv[j]) + 1;
208 ++l;
209 cp = salloc(l);
210 for (l = 0, j = 0; j < we.we_wordc; ++j) {
211 size_t x = strlen(we.we_wordv[j]);
212 memcpy(cp + l, we.we_wordv[j], x);
213 l += x;
214 cp[l++] = ' ';
216 cp[l] = '\0';
217 } else if (!(fexpm & FEXP_SILENT))
218 n_err(_("\"%s\": Ambiguous\n"), name);
219 break;
221 jleave:
222 wordfree(&we);
223 NYD_LEAVE;
224 return cp;
226 #else /* HAVE_WORDEXP */
227 struct stat sbuf;
228 char xname[PATH_MAX +1], cmdbuf[PATH_MAX +1], /* also used for files */
229 *shellp, *cp = NULL;
230 int pivec[2], pid, l, waits;
231 NYD_ENTER;
233 if (pipe(pivec) < 0) {
234 n_perr(_("pipe"), 0);
235 goto jleave;
237 snprintf(cmdbuf, sizeof cmdbuf, "echo %s", name);
238 if ((shellp = ok_vlook(SHELL)) == NULL)
239 shellp = UNCONST(XSHELL);
240 pid = start_command(shellp, NULL, COMMAND_FD_NULL, pivec[1],
241 "-c", cmdbuf, NULL, NULL);
242 if (pid < 0) {
243 close(pivec[0]);
244 close(pivec[1]);
245 goto jleave;
247 close(pivec[1]);
249 jagain:
250 l = read(pivec[0], xname, sizeof xname);
251 if (l < 0) {
252 if (errno == EINTR)
253 goto jagain;
254 n_perr(_("read"), 0);
255 close(pivec[0]);
256 goto jleave;
258 close(pivec[0]);
259 if (!wait_child(pid, &waits) && WTERMSIG(waits) != SIGPIPE) {
260 if (!(fexpm & FEXP_SILENT))
261 n_err(_("\"%s\": Expansion failed\n"), name);
262 goto jleave;
264 if (l == 0) {
265 if (!(fexpm & FEXP_SILENT))
266 n_err(_("\"%s\": No match\n"), name);
267 goto jleave;
269 if (l == sizeof xname) {
270 if (!(fexpm & FEXP_SILENT))
271 n_err(_("\"%s\": Expansion buffer overflow\n"), name);
272 goto jleave;
274 xname[l] = 0;
275 for (cp = xname + l - 1; *cp == '\n' && cp > xname; --cp)
277 cp[1] = '\0';
278 if (!(fexpm & FEXP_MULTIOK) && strchr(xname, ' ') != NULL &&
279 stat(xname, &sbuf) < 0) {
280 if (!(fexpm & FEXP_SILENT))
281 n_err(_("\"%s\": Ambiguous\n"), name);
282 cp = NULL;
283 goto jleave;
285 cp = savestr(xname);
286 jleave:
287 NYD_LEAVE;
288 return cp;
289 #endif /* !HAVE_WORDEXP */
292 static size_t
293 _length_of_line(char const *line, size_t linesize)
295 size_t i;
296 NYD2_ENTER;
298 /* Last character is always '\0' and was added by fgets() */
299 for (--linesize, i = 0; i < linesize; i++)
300 if (line[i] == '\n')
301 break;
302 i = (i < linesize) ? i + 1 : linesize;
303 NYD2_LEAVE;
304 return i;
307 static char *
308 _fgetline_byone(char **line, size_t *linesize, size_t *llen, FILE *fp,
309 int appendnl, size_t n SMALLOC_DEBUG_ARGS)
311 char *rv;
312 int c;
313 NYD2_ENTER;
315 assert(*linesize == 0 || *line != NULL);
316 for (rv = *line;;) {
317 if (*linesize <= LINESIZE || n >= *linesize - 128) {
318 *linesize += ((rv == NULL) ? LINESIZE + n + 1 : 256);
319 *line = rv = (srealloc)(rv, *linesize SMALLOC_DEBUG_ARGSCALL);
321 c = getc(fp);
322 if (c != EOF) {
323 rv[n++] = c;
324 rv[n] = '\0';
325 if (c == '\n')
326 break;
327 } else {
328 if (n > 0) {
329 if (appendnl) {
330 rv[n++] = '\n';
331 rv[n] = '\0';
333 break;
334 } else {
335 rv = NULL;
336 goto jleave;
340 if (llen)
341 *llen = n;
342 jleave:
343 NYD2_LEAVE;
344 return rv;
347 static void
348 makemessage(void)
350 NYD_ENTER;
351 if (msgCount == 0)
352 message_append(NULL);
353 setdot(message);
354 message[msgCount].m_size = 0;
355 message[msgCount].m_lines = 0;
356 NYD_LEAVE;
359 static enum okay
360 get_header(struct message *mp)
362 enum okay rv;
363 NYD_ENTER;
364 UNUSED(mp);
366 switch (mb.mb_type) {
367 case MB_FILE:
368 case MB_MAILDIR:
369 rv = OKAY;
370 break;
371 #ifdef HAVE_POP3
372 case MB_POP3:
373 rv = pop3_header(mp);
374 break;
375 #endif
376 case MB_VOID:
377 default:
378 rv = STOP;
379 break;
381 NYD_LEAVE;
382 return rv;
385 static bool_t
386 _file_lock(int fd, enum file_lock_type flt, off_t off, off_t len)
388 struct flock flp;
389 bool_t rv;
390 NYD2_ENTER;
392 memset(&flp, 0, sizeof flp);
394 switch (flt) {
395 default:
396 case FLT_READ: rv = F_RDLCK; break;
397 case FLT_WRITE: rv = F_WRLCK; break;
399 flp.l_type = rv;
400 flp.l_start = off;
401 flp.l_whence = SEEK_SET;
402 flp.l_len = len;
404 rv = (fcntl(fd, F_SETLK, &flp) != -1);
405 NYD2_LEAVE;
406 return rv;
409 #ifdef HAVE_DOTLOCK
410 static int
411 _dotlock_main(void)
413 /* Use PATH_MAX not NAME_MAX to catch those "we proclaim the minimum value"
414 * problems (SunOS), since the pathconf(3) value comes too late! */
415 char name[PATH_MAX +1];
416 struct dotlock_info di;
417 struct stat stb, fdstb;
418 enum dotlock_state dls;
419 char const *cp;
420 int fd;
421 enum file_lock_type flt;
422 NYD_ENTER;
424 /* Ignore SIGPIPE, we'll see EPIPE and "fall through" */
425 safe_signal(SIGPIPE, SIG_IGN);
427 /* Get the arguments "passed to us" */
428 flt = _dotlock_flt;
429 fd = _dotlock_fd;
430 UNUSED(fd);
431 di = *_dotlock_dip;
433 /* chdir(2)? */
434 jislink:
435 dls = DLS_CANT_CHDIR | DLS_ABANDON;
437 if ((cp = strrchr(di.di_file_name, '/')) != NULL) {
438 char const *fname = cp + 1;
440 while (PTRCMP(cp - 1, >, di.di_file_name) && cp[-1] == '/')
441 --cp;
442 cp = savestrbuf(di.di_file_name, PTR2SIZE(cp - di.di_file_name));
443 if (chdir(cp))
444 goto jmsg;
446 di.di_file_name = fname;
449 /* So we're here, but then again the file can be a symbolic link!
450 * This is however only true if we do not have realpath(3) available since
451 * that'll have resolved the path already otherwise; nonetheless, let
452 * readlink(2) be a precondition for dotlocking and keep this code */
453 if (lstat(cp = di.di_file_name, &stb) == -1)
454 goto jmsg;
455 if (S_ISLNK(stb.st_mode)) {
456 /* Use salloc() and hope we stay in builtin buffer.. */
457 char *x;
458 size_t i;
459 ssize_t sr;
461 for (x = NULL, i = PATH_MAX;; i += PATH_MAX) {
462 x = salloc(i +1);
463 sr = readlink(cp, x, i);
464 if (sr <= 0) {
465 dls = DLS_FISHY | DLS_ABANDON;
466 goto jmsg;
468 if (UICMP(z, sr, <, i)) {
469 x[sr] = '\0';
470 i = (size_t)sr;
471 break;
474 di.di_file_name = x;
475 goto jislink;
478 dls = DLS_FISHY | DLS_ABANDON;
480 /* Bail out if the file has changed its identity in the meanwhile */
481 if (fstat(fd, &fdstb) == -1 ||
482 fdstb.st_dev != stb.st_dev || fdstb.st_ino != stb.st_ino ||
483 fdstb.st_uid != stb.st_uid || fdstb.st_gid != stb.st_gid ||
484 fdstb.st_mode != stb.st_mode)
485 goto jmsg;
487 /* Be aware, even if the error is false! Note the shared code in dotlock.h
488 * *requires* that it is possible to create a filename at least one byte
489 * longer than di_lock_name! */
490 do/* while(0) breaker */{
491 # ifdef HAVE_PATHCONF
492 long pc;
493 # endif
494 int i = snprintf(name, sizeof name, "%s.lock", di.di_file_name);
496 /* fd is a file, not portable to use for _PC_NAME_MAX */
497 if(i < 0){
498 jenametool:
499 dls = DLS_NAMETOOLONG | DLS_ABANDON;
500 goto jmsg;
502 # ifdef HAVE_PATHCONF
503 errno = 0;
504 if((pc = pathconf(".", _PC_NAME_MAX)) == -1){
505 /* errno unchanged: no limit */
506 if(errno == 0)
507 break;
508 }else if(pc - 1 >= (long)i)
509 break;
510 else
511 goto jenametool;
512 # endif
513 if(UICMP(z, NAME_MAX - 1, <, (uiz_t)i))
514 goto jenametool;
515 }while(0);
517 di.di_lock_name = name;
519 /* We are in the directory of the mailbox for which we have to create
520 * a dotlock file for. We don't know wether we have realpath(3) available,
521 * and manually resolving the path is due especially given that S-nail
522 * supports the special "%:" syntax to warp any file into a "system
523 * mailbox"; there may also be multiple system mailbox directories...
524 * So what we do is that we fstat(2) the mailbox and check its UID and
525 * GID against that of our own process: if any of those mismatch we must
526 * either assume a directory we are not allowed to write in, or that we run
527 * via -u/$USER/%USER as someone else, in which case we favour our
528 * privilege-separated dotlock process */
529 assert(cp != NULL); /* Ugly: avoid a useless var and reuse that one */
530 if (access(".", W_OK)) {
531 /* This may however also indicate a read-only filesystem, which is not
532 * really an error from our point of view since the mailbox will degrade
533 * to a readonly one for which no dotlock is needed, then, and errors
534 * may arise only due to actions which require box modifications */
535 if (errno == EROFS) {
536 dls = DLS_ROFS | DLS_ABANDON;
537 goto jmsg;
539 cp = NULL;
541 if (cp == NULL || stb.st_uid != user_id || stb.st_gid != group_id) {
542 char itoabuf[64];
543 char const *args[13];
545 snprintf(itoabuf, sizeof itoabuf, "%" PRIuZ, di.di_pollmsecs);
546 args[ 0] = PRIVSEP;
547 args[ 1] = (flt == FLT_READ ? "rdotlock" : "wdotlock");
548 args[ 2] = "mailbox"; args[ 3] = di.di_file_name;
549 args[ 4] = "name"; args[ 5] = di.di_lock_name;
550 args[ 6] = "hostname"; args[ 7] = di.di_hostname;
551 args[ 8] = "randstr"; args[ 9] = di.di_randstr;
552 args[10] = "pollmsecs"; args[11] = itoabuf;
553 args[12] = NULL;
554 execv(LIBEXECDIR "/" UAGENT "-privsep", UNCONST(args));
556 dls = DLS_NOEXEC;
557 write(STDOUT_FILENO, &dls, sizeof dls);
558 /* But fall through and try it with normal privileges! */
561 /* So let's try and call it ourselfs! Note that we don't block signals just
562 * like our privsep child does, the user will anyway be able to remove his
563 * file again, and if we're in -u/$USER mode then we are allowed to access
564 * the user's box: shall we leave behind a stale dotlock then at least we
565 * start a friendly human conversation. Since we cannot handle SIGKILL and
566 * SIGSTOP malicious things could happen whatever we do */
567 safe_signal(SIGHUP, SIG_IGN);
568 safe_signal(SIGINT, SIG_IGN);
569 safe_signal(SIGQUIT, SIG_IGN);
570 safe_signal(SIGTERM, SIG_IGN);
572 NYD;
573 dls = _dotlock_create(&di);
574 NYD;
576 /* Finally: notify our parent about the actual lock state.. */
577 jmsg:
578 write(STDOUT_FILENO, &dls, sizeof dls);
579 close(STDOUT_FILENO);
581 /* ..then eventually wait until we shall remove the lock again, which will
582 * be notified via the read returning */
583 if (dls == DLS_NONE) {
584 read(STDIN_FILENO, &dls, sizeof dls);
586 unlink(name);
588 NYD_LEAVE;
589 return EXIT_OK;
591 #endif /* HAVE_DOTLOCK */
593 #ifdef HAVE_SOCKETS
594 static long
595 xwrite(int fd, char const *data, size_t sz)
597 long rv = -1, wo;
598 size_t wt = 0;
599 NYD_ENTER;
601 do {
602 if ((wo = write(fd, data + wt, sz - wt)) < 0) {
603 if (errno == EINTR)
604 continue;
605 else
606 goto jleave;
608 wt += wo;
609 } while (wt < sz);
610 rv = (long)sz;
611 jleave:
612 NYD_LEAVE;
613 return rv;
615 #endif /* HAVE_SOCKETS */
617 static bool_t
618 _source_file(char const *file, bool_t silent_error)
620 char *cp;
621 bool_t ispipe;
622 FILE *fi;
623 NYD_ENTER;
625 fi = NULL;
627 if ((ispipe = !silent_error)) {
628 size_t i = strlen(file);
630 while (i > 0 && spacechar(file[i - 1]))
631 --i;
632 if (i > 0 && file[i - 1] == '|')
633 cp = savestrbuf(file, --i);
634 else
635 ispipe = FAL0;
638 if (ispipe) {
639 char const *sh;
641 if ((sh = ok_vlook(SHELL)) == NULL)
642 sh = XSHELL;
643 if ((fi = Popen(cp, "r", sh, NULL, COMMAND_FD_NULL)) == NULL) {
644 n_perr(cp, 0);
645 goto jleave;
647 } else if ((cp = fexpand(file, FEXP_LOCAL)) == NULL)
648 goto jleave;
649 else if ((fi = Fopen(cp, "r")) == NULL) {
650 if (!silent_error || (options & OPT_D_V))
651 n_perr(cp, 0);
652 goto jleave;
655 if (temporary_localopts_store != NULL) {
656 n_err(_("Before v15 you cannot `source' from within macros, sorry\n"));
657 goto jeclose;
659 if (_fio_stack_size >= NELEM(_fio_stack)) {
660 n_err(_("Too many `source' recursions\n"));
661 jeclose:
662 if (ispipe)
663 Pclose(fi, TRU1);
664 else
665 Fclose(fi);
666 fi = NULL;
667 goto jleave;
670 _fio_stack[_fio_stack_size].s_file = _fio_input;
671 _fio_stack[_fio_stack_size].s_cond = condstack_release();
672 _fio_stack[_fio_stack_size].s_pstate = pstate;
673 ++_fio_stack_size;
674 pstate &= ~(PS_LOADING | PS_PIPING);
675 pstate |= PS_SOURCING | (ispipe ? PS_PIPING : PS_NONE);
676 _fio_input = fi;
677 jleave:
678 NYD_LEAVE;
679 return (fi != NULL);
682 FL char *
683 (fgetline)(char **line, size_t *linesize, size_t *cnt, size_t *llen, FILE *fp,
684 int appendnl SMALLOC_DEBUG_ARGS)
686 size_t i_llen, sz;
687 char *rv;
688 NYD2_ENTER;
690 if (cnt == NULL) {
691 /* Without count, we can't determine where the chars returned by fgets()
692 * end if there's no newline. We have to read one character by one */
693 rv = _fgetline_byone(line, linesize, llen, fp, appendnl, 0
694 SMALLOC_DEBUG_ARGSCALL);
695 goto jleave;
698 if ((rv = *line) == NULL || *linesize < LINESIZE)
699 *line = rv = (srealloc)(rv, *linesize = LINESIZE SMALLOC_DEBUG_ARGSCALL);
700 sz = (*linesize <= *cnt) ? *linesize : *cnt + 1;
701 if (sz <= 1 || fgets(rv, sz, fp) == NULL) {
702 /* Leave llen untouched; it is used to determine whether the last line
703 * was \n-terminated in some callers */
704 rv = NULL;
705 goto jleave;
708 i_llen = _length_of_line(rv, sz);
709 *cnt -= i_llen;
710 while (rv[i_llen - 1] != '\n') {
711 *line = rv = (srealloc)(rv, *linesize += 256 SMALLOC_DEBUG_ARGSCALL);
712 sz = *linesize - i_llen;
713 sz = (sz <= *cnt) ? sz : *cnt + 1;
714 if (sz <= 1 || fgets(rv + i_llen, sz, fp) == NULL) {
715 if (appendnl) {
716 rv[i_llen++] = '\n';
717 rv[i_llen] = '\0';
719 break;
721 sz = _length_of_line(rv + i_llen, sz);
722 i_llen += sz;
723 *cnt -= sz;
725 if (llen)
726 *llen = i_llen;
727 jleave:
728 NYD2_LEAVE;
729 return rv;
732 FL int
733 (readline_restart)(FILE *ibuf, char **linebuf, size_t *linesize, size_t n
734 SMALLOC_DEBUG_ARGS)
736 /* TODO readline_restart(): always *appends* LF just to strip it again;
737 * TODO should be configurable just as for fgetline(); ..or whatever.. */
738 int rv = -1;
739 long sz;
740 NYD2_ENTER;
742 clearerr(ibuf);
744 /* Interrupts will cause trouble if we are inside a stdio call. As this is
745 * only relevant if input is from tty, bypass it by read(), then */
746 if (fileno(ibuf) == 0 && (options & OPT_TTYIN)) {
747 assert(*linesize == 0 || *linebuf != NULL);
748 for (;;) {
749 if (*linesize <= LINESIZE || n >= *linesize - 128) {
750 *linesize += ((*linebuf == NULL) ? LINESIZE + n + 1 : 256);
751 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
753 jagain:
754 sz = read(0, *linebuf + n, *linesize - n - 1);
755 if (sz > 0) {
756 n += sz;
757 (*linebuf)[n] = '\0';
758 if (n > 0 && (*linebuf)[n - 1] == '\n')
759 break;
760 } else {
761 if (sz < 0 && errno == EINTR)
762 goto jagain;
763 if (n > 0) {
764 if ((*linebuf)[n - 1] != '\n') {
765 (*linebuf)[n++] = '\n';
766 (*linebuf)[n] = '\0';
768 break;
769 } else
770 goto jleave;
773 } else {
774 /* Not reading from standard input or standard input not a terminal. We
775 * read one char at a time as it is the only way to get lines with
776 * embedded NUL characters in standard stdio */
777 if (_fgetline_byone(linebuf, linesize, &n, ibuf, 1, n
778 SMALLOC_DEBUG_ARGSCALL) == NULL)
779 goto jleave;
781 if (n > 0 && (*linebuf)[n - 1] == '\n')
782 (*linebuf)[--n] = '\0';
783 rv = (int)n;
784 jleave:
785 NYD2_LEAVE;
786 return rv;
789 FL int
790 (readline_input)(char const *prompt, bool_t nl_escape, char **linebuf,
791 size_t *linesize, char const *string SMALLOC_DEBUG_ARGS)
793 /* TODO readline: linebuf pool! */
794 FILE *ifile = (_fio_input != NULL) ? _fio_input : stdin;
795 bool_t doprompt, dotty;
796 int n, nold;
797 NYD2_ENTER;
799 doprompt = (!(pstate & PS_SOURCING) && (options & OPT_INTERACTIVE));
800 dotty = (doprompt && !ok_blook(line_editor_disable));
801 if (!doprompt)
802 prompt = NULL;
803 else if (prompt == NULL)
804 prompt = getprompt();
806 /* Ensure stdout is flushed first anyway */
807 if (!dotty && prompt == NULL)
808 fflush(stdout);
810 for (nold = n = 0;;) {
811 if (dotty) {
812 assert(ifile == stdin);
813 if (string != NULL && (n = (int)strlen(string)) > 0) {
814 if (*linesize > 0)
815 *linesize += n +1;
816 else
817 *linesize = (size_t)n + LINESIZE +1;
818 *linebuf = (srealloc)(*linebuf, *linesize SMALLOC_DEBUG_ARGSCALL);
819 memcpy(*linebuf, string, (size_t)n +1);
821 string = NULL;
822 /* TODO if nold>0, don't redisplay the entire line!
823 * TODO needs complete redesign ... */
824 n = (n_tty_readline)(prompt, linebuf, linesize, n
825 SMALLOC_DEBUG_ARGSCALL);
826 } else {
827 if (prompt != NULL) {
828 if (*prompt != '\0')
829 fputs(prompt, stdout);
830 fflush(stdout);
832 n = (readline_restart)(ifile, linebuf, linesize, n
833 SMALLOC_DEBUG_ARGSCALL);
835 if (n > 0 && nold > 0) {
836 int i = 0;
837 char const *cp = *linebuf + nold;
839 while (blankspacechar(*cp) && nold + i < n)
840 ++cp, ++i;
841 if (i > 0) {
842 memmove(*linebuf + nold, cp, n - nold - i);
843 n -= i;
844 (*linebuf)[n] = '\0';
848 if (n <= 0)
849 break;
851 /* POSIX says:
852 * An unquoted <backslash> at the end of a command line shall
853 * be discarded and the next line shall continue the command */
854 if (!nl_escape || n == 0 || (*linebuf)[n - 1] != '\\')
855 break;
856 (*linebuf)[nold = --n] = '\0';
857 if (prompt != NULL && *prompt != '\0')
858 prompt = ".. "; /* XXX PS2 .. */
861 if (n >= 0 && (options & OPT_D_VV))
862 n_err(_("%s %d bytes <%.*s>\n"),
863 ((pstate & PS_LOADING) ? "LOAD"
864 : (pstate & PS_SOURCING) ? "SOURCE" : "READ"),
865 n, n, *linebuf);
866 NYD2_LEAVE;
867 return n;
870 FL char *
871 n_input_cp_addhist(char const *prompt, char const *string, bool_t isgabby)
873 /* FIXME n_input_cp_addhist(): leaks on sigjmp without linepool */
874 size_t linesize = 0;
875 char *linebuf = NULL, *rv = NULL;
876 int n;
877 NYD2_ENTER;
879 n = readline_input(prompt, FAL0, &linebuf, &linesize, string);
880 if (n > 0 && *(rv = savestrbuf(linebuf, (size_t)n)) != '\0' &&
881 (options & OPT_INTERACTIVE))
882 n_tty_addhist(rv, isgabby);
884 if (linebuf != NULL)
885 free(linebuf);
886 NYD2_LEAVE;
887 return rv;
890 FL void
891 setptr(FILE *ibuf, off_t offset)
893 struct message self;
894 char *cp, *linebuf = NULL;
895 char const *cp2;
896 int c, maybe = 1, inhead = 0, selfcnt = 0;
897 size_t linesize = 0, filesize, cnt;
898 NYD_ENTER;
900 memset(&self, 0, sizeof self);
901 self.m_flag = MUSED | MNEW | MNEWEST;
902 filesize = mailsize - offset;
903 offset = ftell(mb.mb_otf);
905 for (;;) {
906 if (fgetline(&linebuf, &linesize, &filesize, &cnt, ibuf, 0) == NULL) {
907 self.m_xsize = self.m_size;
908 self.m_xlines = self.m_lines;
909 self.m_have = HAVE_HEADER | HAVE_BODY;
910 if (selfcnt > 0)
911 message_append(&self);
912 makemessage();
913 if (linebuf)
914 free(linebuf);
915 break;
918 #ifdef notdef
919 if (linebuf[0] == '\0')
920 linebuf[0] = '.';
921 #endif
922 /* XXX Convert CRLF to LF; this should be rethought in that
923 * XXX CRLF input should possibly end as CRLF output? */
924 if (cnt >= 2 && linebuf[cnt - 1] == '\n' && linebuf[cnt - 2] == '\r')
925 linebuf[--cnt - 1] = '\n';
926 fwrite(linebuf, sizeof *linebuf, cnt, mb.mb_otf);
927 if (ferror(mb.mb_otf)) {
928 n_perr(_("/tmp"), 0);
929 exit(EXIT_ERR);
931 if (linebuf[cnt - 1] == '\n')
932 linebuf[cnt - 1] = '\0';
933 if (maybe && linebuf[0] == 'F' && is_head(linebuf, cnt, FAL0)) {
934 /* TODO char date[FROM_DATEBUF];
935 * TODO extract_date_from_from_(linebuf, cnt, date);
936 * TODO self.m_time = 10000; */
937 self.m_xsize = self.m_size;
938 self.m_xlines = self.m_lines;
939 self.m_have = HAVE_HEADER | HAVE_BODY;
940 if (selfcnt++ > 0)
941 message_append(&self);
942 msgCount++;
943 self.m_flag = MUSED | MNEW | MNEWEST;
944 self.m_size = 0;
945 self.m_lines = 0;
946 self.m_block = mailx_blockof(offset);
947 self.m_offset = mailx_offsetof(offset);
948 inhead = 1;
949 } else if (linebuf[0] == 0) {
950 inhead = 0;
951 } else if (inhead) {
952 for (cp = linebuf, cp2 = "status";; ++cp) {
953 if ((c = *cp2++) == 0) {
954 while (c = *cp++, whitechar(c))
956 if (cp[-1] != ':')
957 break;
958 while ((c = *cp++) != '\0')
959 if (c == 'R')
960 self.m_flag |= MREAD;
961 else if (c == 'O')
962 self.m_flag &= ~MNEW;
963 break;
965 if (*cp != c && *cp != upperconv(c))
966 break;
968 for (cp = linebuf, cp2 = "x-status";; ++cp) {
969 if ((c = *cp2++) == 0) {
970 while ((c = *cp++, whitechar(c)))
972 if (cp[-1] != ':')
973 break;
974 while ((c = *cp++) != '\0')
975 if (c == 'F')
976 self.m_flag |= MFLAGGED;
977 else if (c == 'A')
978 self.m_flag |= MANSWERED;
979 else if (c == 'T')
980 self.m_flag |= MDRAFTED;
981 break;
983 if (*cp != c && *cp != upperconv(c))
984 break;
987 offset += cnt;
988 self.m_size += cnt;
989 ++self.m_lines;
990 maybe = linebuf[0] == 0;
992 NYD_LEAVE;
995 FL int
996 putline(FILE *obuf, char *linebuf, size_t cnt)
998 int rv = -1;
999 NYD_ENTER;
1001 fwrite(linebuf, sizeof *linebuf, cnt, obuf);
1002 putc('\n', obuf);
1003 if (!ferror(obuf))
1004 rv = (int)(cnt + 1);
1005 NYD_LEAVE;
1006 return rv;
1009 FL FILE *
1010 setinput(struct mailbox *mp, struct message *m, enum needspec need)
1012 FILE *rv = NULL;
1013 enum okay ok = STOP;
1014 NYD_ENTER;
1016 switch (need) {
1017 case NEED_HEADER:
1018 ok = (m->m_have & HAVE_HEADER) ? OKAY : get_header(m);
1019 break;
1020 case NEED_BODY:
1021 ok = (m->m_have & HAVE_BODY) ? OKAY : get_body(m);
1022 break;
1023 case NEED_UNSPEC:
1024 ok = OKAY;
1025 break;
1027 if (ok != OKAY)
1028 goto jleave;
1030 fflush(mp->mb_otf);
1031 if (fseek(mp->mb_itf, (long)mailx_positionof(m->m_block, m->m_offset),
1032 SEEK_SET) == -1) {
1033 n_perr(_("fseek"), 0);
1034 n_panic(_("temporary file seek"));
1036 rv = mp->mb_itf;
1037 jleave:
1038 NYD_LEAVE;
1039 return rv;
1042 FL void
1043 message_reset(void)
1045 NYD_ENTER;
1046 if (message != NULL) {
1047 free(message);
1048 message = NULL;
1050 msgCount = 0;
1051 _message_space = 0;
1052 NYD_LEAVE;
1055 FL void
1056 message_append(struct message *mp)
1058 NYD_ENTER;
1059 if (UICMP(z, msgCount + 1, >=, _message_space)) {
1060 /* XXX remove _message_space magics (or use s_Vector) */
1061 _message_space = (_message_space >= 128 && _message_space <= 1000000)
1062 ? _message_space << 1 : _message_space + 64;
1063 message = srealloc(message, _message_space * sizeof *message);
1065 if (msgCount > 0) {
1066 if (mp != NULL)
1067 message[msgCount - 1] = *mp;
1068 else
1069 memset(message + msgCount - 1, 0, sizeof *message);
1071 NYD_LEAVE;
1074 FL bool_t
1075 message_match(struct message *mp, struct search_expr const *sep,
1076 bool_t with_headers)
1078 char **line;
1079 size_t *linesize, cnt;
1080 FILE *fp;
1081 bool_t rv = FAL0;
1082 NYD_ENTER;
1084 if ((fp = Ftmp(NULL, "mpmatch", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL)
1085 goto j_leave;
1087 if (sendmp(mp, fp, NULL, NULL, SEND_TOSRCH, NULL) < 0)
1088 goto jleave;
1089 fflush_rewind(fp);
1091 cnt = fsize(fp);
1092 line = &termios_state.ts_linebuf; /* XXX line pool */
1093 linesize = &termios_state.ts_linesize; /* XXX line pool */
1095 if (!with_headers)
1096 while (fgetline(line, linesize, &cnt, NULL, fp, 0))
1097 if (**line == '\n')
1098 break;
1100 while (fgetline(line, linesize, &cnt, NULL, fp, 0)) {
1101 #ifdef HAVE_REGEX
1102 if (sep->ss_sexpr == NULL) {
1103 if (regexec(&sep->ss_regex, *line, 0,NULL, 0) == REG_NOMATCH)
1104 continue;
1105 } else
1106 #endif
1107 if (!substr(*line, sep->ss_sexpr))
1108 continue;
1109 rv = TRU1;
1110 break;
1113 jleave:
1114 Fclose(fp);
1115 j_leave:
1116 NYD_LEAVE;
1117 return rv;
1120 FL struct message *
1121 setdot(struct message *mp)
1123 NYD_ENTER;
1124 if (dot != mp) {
1125 prevdot = dot;
1126 pstate &= ~PS_DID_PRINT_DOT;
1128 dot = mp;
1129 uncollapse1(dot, 0);
1130 NYD_LEAVE;
1131 return dot;
1134 FL off_t
1135 fsize(FILE *iob)
1137 struct stat sbuf;
1138 off_t rv;
1139 NYD_ENTER;
1141 rv = (fstat(fileno(iob), &sbuf) == -1) ? 0 : sbuf.st_size;
1142 NYD_LEAVE;
1143 return rv;
1146 FL char *
1147 fexpand(char const *name, enum fexp_mode fexpm)
1149 char cbuf[PATH_MAX +1];
1150 char const *res;
1151 struct str s;
1152 bool_t dyn;
1153 NYD_ENTER;
1155 /* The order of evaluation is "%" and "#" expand into constants.
1156 * "&" can expand into "+". "+" can expand into shell meta characters.
1157 * Shell meta characters expand into constants.
1158 * This way, we make no recursive expansion */
1159 if ((fexpm & FEXP_NSHORTCUT) || (res = shortcut_expand(name)) == NULL)
1160 res = UNCONST(name);
1162 if (fexpm & FEXP_SHELL) {
1163 dyn = FAL0;
1164 goto jshell;
1166 jnext:
1167 dyn = FAL0;
1168 switch (*res) {
1169 case '%':
1170 if (res[1] == ':' && res[2] != '\0') {
1171 res = &res[2];
1172 goto jnext;
1174 _findmail(cbuf, sizeof cbuf, (res[1] != '\0' ? res + 1 : myname),
1175 (res[1] != '\0' || (options & OPT_u_FLAG)));
1176 res = cbuf;
1177 goto jislocal;
1178 case '#':
1179 if (res[1] != '\0')
1180 break;
1181 if (prevfile[0] == '\0') {
1182 n_err(_("No previous file\n"));
1183 res = NULL;
1184 goto jleave;
1186 res = prevfile;
1187 goto jislocal;
1188 case '&':
1189 if (res[1] == '\0') {
1190 if ((res = ok_vlook(MBOX)) == NULL)
1191 res = UNCONST("~/mbox"); /* XXX no magics (POSIX though) */
1192 else if (res[0] != '&' || res[1] != '\0')
1193 goto jnext;
1195 break;
1198 if (res[0] == '+' && getfold(cbuf, sizeof cbuf)) {
1199 size_t i = strlen(cbuf);
1201 res = str_concat_csvl(&s, cbuf,
1202 ((i > 0 && cbuf[i - 1] == '/') ? "" : "/"), res + 1, NULL)->s;
1203 dyn = TRU1;
1205 if (res[0] == '%' && res[1] == ':') {
1206 res += 2;
1207 goto jnext;
1211 /* Catch the most common shell meta character */
1212 jshell:
1213 if (res[0] == '~') {
1214 res = n_shell_expand_tilde(res, NULL);
1215 dyn = TRU1;
1217 if (anyof(res, "|&;<>{}()[]*?$`'\"\\"))
1218 switch (which_protocol(res)) {
1219 case PROTO_FILE:
1220 case PROTO_MAILDIR:
1221 res = (fexpm & FEXP_NSHELL) ? n_shell_expand_var(res, TRU1, NULL)
1222 : _globname(res, fexpm);
1223 dyn = TRU1;
1224 goto jleave;
1225 default:
1226 break;
1228 jislocal:
1229 if (fexpm & FEXP_LOCAL)
1230 switch (which_protocol(res)) {
1231 case PROTO_FILE:
1232 case PROTO_MAILDIR:
1233 break;
1234 default:
1235 n_err(_("Not a local file or directory: \"%s\"\n"), name);
1236 res = NULL;
1237 break;
1239 jleave:
1240 if (res && !dyn)
1241 res = savestr(res);
1242 NYD_LEAVE;
1243 return UNCONST(res);
1246 FL char *
1247 fexpand_nshell_quote(char const *name)
1249 size_t i, j;
1250 char *rv, c;
1251 NYD_ENTER;
1253 for (i = j = 0; (c = name[i]) != '\0'; ++i)
1254 if (c == '\\')
1255 ++j;
1257 if (j == 0)
1258 rv = savestrbuf(name, i);
1259 else {
1260 rv = salloc(i + j +1);
1261 for (i = j = 0; (c = name[i]) != '\0'; ++i) {
1262 rv[j++] = c;
1263 if (c == '\\')
1264 rv[j++] = c;
1266 rv[j] = '\0';
1268 NYD_LEAVE;
1269 return rv;
1272 FL bool_t
1273 var_folder_updated(char const *name, char **store)
1275 char rv = TRU1;
1276 char *folder, *unres = NULL, *res = NULL;
1277 NYD_ENTER;
1279 if ((folder = UNCONST(name)) == NULL)
1280 goto jleave;
1282 /* Expand the *folder*; skip %: prefix for simplicity of use */
1283 /* XXX This *only* works because we do NOT
1284 * XXX update environment variables via the "set" mechanism */
1285 if (folder[0] == '%' && folder[1] == ':')
1286 folder += 2;
1287 if ((folder = fexpand(folder, FEXP_FULL)) == NULL) /* XXX error? */
1288 goto jleave;
1290 switch (which_protocol(folder)) {
1291 case PROTO_POP3:
1292 n_err(_("*folder* cannot be set to a flat, readonly POP3 account\n"));
1293 rv = FAL0;
1294 goto jleave;
1295 default:
1296 /* Further expansion desired */
1297 break;
1300 /* All non-absolute paths are relative to our home directory */
1301 if (*folder != '/') {
1302 size_t l1 = strlen(homedir), l2 = strlen(folder);
1303 unres = ac_alloc(l1 + l2 + 1 +1);
1304 memcpy(unres, homedir, l1);
1305 unres[l1] = '/';
1306 memcpy(unres + l1 + 1, folder, l2);
1307 unres[l1 + 1 + l2] = '\0';
1308 folder = unres;
1311 /* Since lex.c:_update_mailname() uses realpath(3) if available to
1312 * avoid that we loose track of our currently open folder in case we
1313 * chdir away, but still checks the leading path portion against
1314 * getfold() to be able to abbreviate to the +FOLDER syntax if
1315 * possible, we need to realpath(3) the folder, too */
1316 #ifdef HAVE_REALPATH
1317 res = ac_alloc(PATH_MAX +1);
1318 if (realpath(folder, res) == NULL)
1319 n_err(_("Can't canonicalize \"%s\"\n"), folder);
1320 else
1321 folder = res;
1322 #endif
1324 *store = sstrdup(folder);
1326 if (res != NULL)
1327 ac_free(res);
1328 if (unres != NULL)
1329 ac_free(unres);
1330 jleave:
1331 NYD_LEAVE;
1332 return rv;
1335 FL char const *
1336 getdeadletter(void) /* XXX should that be in auxlily.c? */
1338 char const *cp;
1339 NYD_ENTER;
1341 if ((cp = ok_vlook(DEAD)) == NULL || (cp = fexpand(cp, FEXP_LOCAL)) == NULL)
1342 cp = fexpand("~/dead.letter", FEXP_LOCAL | FEXP_SHELL);
1343 else if (*cp != '/') {
1344 size_t sz = strlen(cp) + 2 +1;
1345 char *buf = ac_alloc(sz);
1347 snprintf(buf, sz, "~/%s", cp);
1348 cp = fexpand(buf, FEXP_LOCAL | FEXP_SHELL);
1349 ac_free(buf);
1352 if (cp == NULL)
1353 cp = "dead.letter"; /* XXX magic -> nail.h (POSIX thing though) */
1354 NYD_LEAVE;
1355 return cp;
1358 FL enum okay
1359 get_body(struct message *mp)
1361 enum okay rv;
1362 NYD_ENTER;
1363 UNUSED(mp);
1365 switch (mb.mb_type) {
1366 case MB_FILE:
1367 case MB_MAILDIR:
1368 rv = OKAY;
1369 break;
1370 #ifdef HAVE_POP3
1371 case MB_POP3:
1372 rv = pop3_body(mp);
1373 break;
1374 #endif
1375 case MB_VOID:
1376 default:
1377 rv = STOP;
1378 break;
1380 NYD_LEAVE;
1381 return rv;
1384 FL bool_t
1385 file_lock(int fd, enum file_lock_type flt, off_t off, off_t len,
1386 size_t pollmsecs)
1388 size_t tries;
1389 bool_t rv;
1390 NYD_ENTER;
1392 for (tries = 0; tries <= FILE_LOCK_TRIES; ++tries)
1393 if ((rv = _file_lock(fd, flt, off, len)) || pollmsecs == 0)
1394 break;
1395 else
1396 n_msleep(pollmsecs, FAL0);
1397 NYD_LEAVE;
1398 return rv;
1401 FL FILE *
1402 dot_lock(char const *fname, int fd, enum file_lock_type flt,
1403 off_t off, off_t len, size_t pollmsecs)
1405 #undef _DOMSG
1406 #ifdef HAVE_DOTLOCK
1407 # define _DOMSG() n_err(_("Creating dotlock for \"%s\" "), fname)
1408 #else
1409 # define _DOMSG() n_err(_("Trying to lock file \"%s\" "), fname)
1410 #endif
1412 #ifdef HAVE_DOTLOCK
1413 int cpipe[2];
1414 struct dotlock_info di;
1415 enum dotlock_state dls;
1416 char const *emsg = NULL;
1417 #endif
1418 int UNINIT(serrno, 0);
1419 union {size_t tries; int (*ptf)(void); char const *sh; ssize_t r;} u;
1420 bool_t flocked, didmsg = FAL0;
1421 FILE *rv = NULL;
1422 NYD_ENTER;
1424 if (options & OPT_D_VV) {
1425 _DOMSG();
1426 didmsg = TRUM1;
1429 flocked = FAL0;
1430 for (u.tries = 0; !_file_lock(fd, flt, off, len);)
1431 switch ((serrno = errno)) {
1432 case EACCES:
1433 case EAGAIN:
1434 case ENOLCK:
1435 if (pollmsecs > 0 && ++u.tries < FILE_LOCK_TRIES) {
1436 if (!didmsg)
1437 _DOMSG();
1438 n_err(".");
1439 didmsg = TRUM1;
1440 n_msleep(pollmsecs, FAL0);
1441 continue;
1443 /* FALLTHRU */
1444 default:
1445 goto jleave;
1447 flocked = TRU1;
1449 #ifndef HAVE_DOTLOCK
1450 jleave:
1451 if (didmsg == TRUM1)
1452 n_err("\n");
1453 if (flocked)
1454 rv = (FILE*)-1;
1455 else
1456 errno = serrno;
1457 NYD_LEAVE;
1458 return rv;
1460 #else
1461 /* Create control-pipe for our dot file locker process, which will remove
1462 * the lock and terminate once the pipe is closed, for whatever reason */
1463 if (pipe_cloexec(cpipe) == -1) {
1464 serrno = errno;
1465 emsg = N_(" Can't create file lock control pipe\n");
1466 goto jemsg;
1469 /* And the locker process itself; it'll be a (rather cheap) thread only
1470 * unless the lock has to be placed in the system spool and we have our
1471 * privilege-separated dotlock program available, in which case that will be
1472 * executed and do "it" with changed group-id */
1473 di.di_file_name = fname;
1474 di.di_pollmsecs = pollmsecs;
1475 /* Initialize some more stuff; query the two strings in the parent in order
1476 * to cache the result of the former and anyway minimalize child page-ins.
1477 * Especially uname(3) may hang for multiple seconds when it is called the
1478 * first time! */
1479 di.di_hostname = nodename(FAL0);
1480 di.di_randstr = getrandstring(16);
1481 _dotlock_flt = flt;
1482 _dotlock_fd = fd;
1483 _dotlock_dip = &di;
1485 u.ptf = &_dotlock_main;
1486 rv = Popen((char*)-1, "W", u.sh, NULL, cpipe[1]);
1487 serrno = errno;
1489 close(cpipe[1]);
1490 if (rv == NULL) {
1491 close(cpipe[0]);
1492 emsg = N_(" Can't create file lock process\n");
1493 goto jemsg;
1496 /* Let's check wether we were able to create the dotlock file */
1497 for (;;) {
1498 u.r = read(cpipe[0], &dls, sizeof dls);
1499 if (UICMP(z, u.r, !=, sizeof dls)) {
1500 serrno = (u.r != -1) ? EAGAIN : errno;
1501 dls = DLS_DUNNO | DLS_ABANDON;
1502 } else
1503 serrno = 0;
1505 if (dls == DLS_NONE || (dls & DLS_ABANDON))
1506 close(cpipe[0]);
1508 switch (dls & ~DLS_ABANDON) {
1509 case DLS_NONE:
1510 goto jleave;
1511 case DLS_CANT_CHDIR:
1512 if (options & OPT_D_V)
1513 emsg = N_(" Can't change directory! Please check permissions\n");
1514 serrno = EACCES;
1515 break;
1516 case DLS_NAMETOOLONG:
1517 emsg = N_("Resulting dotlock filename would be too long\n");
1518 serrno = EACCES;
1519 break;
1520 case DLS_ROFS:
1521 assert(dls & DLS_ABANDON);
1522 if (options & OPT_D_V)
1523 emsg = N_(" Read-only filesystem, not creating lock file\n");
1524 serrno = EROFS;
1525 break;
1526 case DLS_NOPERM:
1527 if (options & OPT_D_V)
1528 emsg = N_(" Can't create a lock file! Please check permissions\n"
1529 " (Maybe setting *dotlock-ignore-error* variable helps.)\n");
1530 serrno = EACCES;
1531 break;
1532 case DLS_NOEXEC:
1533 if (options & OPT_D_V)
1534 emsg = N_(" Can't find privilege-separated file lock program\n");
1535 serrno = ENOENT;
1536 break;
1537 case DLS_PRIVFAILED:
1538 emsg = N_(" Privilege-separated file lock program can't change "
1539 "privileges\n");
1540 serrno = EPERM;
1541 break;
1542 case DLS_EXIST:
1543 emsg = N_(" It seems there is a stale dotlock file?\n"
1544 " Please remove the lock file manually, then retry\n");
1545 serrno = EEXIST;
1546 break;
1547 case DLS_FISHY:
1548 emsg = N_(" Fishy! Is someone trying to \"steal\" foreign files?\n"
1549 " Please check the mailbox file etc. manually, then retry\n");
1550 serrno = EAGAIN; /* ? Hack to ignore *dotlock-ignore-error* xxx */
1551 break;
1552 default:
1553 case DLS_DUNNO:
1554 emsg = N_(" Unspecified dotlock file control process error.\n"
1555 " Like broken I/O pipe; this one is unlikely to happen\n");
1556 if (serrno != EAGAIN)
1557 serrno = EINVAL;
1558 break;
1559 case DLS_PING:
1560 if (!didmsg)
1561 _DOMSG();
1562 n_err(".");
1563 didmsg = TRUM1;
1564 continue;
1567 if (emsg != NULL) {
1568 if (!didmsg) {
1569 _DOMSG();
1570 didmsg = TRUM1;
1572 if (didmsg == TRUM1)
1573 n_err("\n");
1574 didmsg = TRU1;
1575 n_err(V_(emsg));
1576 emsg = NULL;
1579 if (dls & DLS_ABANDON) {
1580 Pclose(rv, FAL0);
1581 rv = NULL;
1582 break;
1586 jleave:
1587 if (didmsg == TRUM1)
1588 n_err("\n");
1589 if (rv == NULL) {
1590 if (flocked && (serrno == EROFS ||
1591 (serrno != EAGAIN && serrno != EEXIST &&
1592 ok_blook(dotlock_ignore_error))))
1593 rv = (FILE*)-1;
1594 else
1595 errno = serrno;
1597 NYD_LEAVE;
1598 return rv;
1599 jemsg:
1600 if (!didmsg)
1601 _DOMSG();
1602 n_err("\n");
1603 didmsg = TRU1;
1604 n_err(V_(emsg));
1605 goto jleave;
1606 #endif /* HAVE_DOTLOCK */
1607 #undef _DOMSG
1610 #ifdef HAVE_SOCKETS
1611 FL int
1612 sclose(struct sock *sp)
1614 int i;
1615 NYD_ENTER;
1617 i = sp->s_fd;
1618 sp->s_fd = -1;
1619 /* TODO NOTE: we MUST NOT close the descriptor 0 here...
1620 * TODO of course this should be handled in a VMAILFS->open() .s_fd=-1,
1621 * TODO but unfortunately it isn't yet */
1622 if (i <= 0)
1623 i = 0;
1624 else {
1625 if (sp->s_onclose != NULL)
1626 (*sp->s_onclose)();
1627 if (sp->s_wbuf != NULL)
1628 free(sp->s_wbuf);
1629 # ifdef HAVE_OPENSSL
1630 if (sp->s_use_ssl) {
1631 void *s_ssl = sp->s_ssl;
1633 sp->s_ssl = NULL;
1634 sp->s_use_ssl = 0;
1635 while (!SSL_shutdown(s_ssl)) /* XXX proper error handling;signals! */
1637 SSL_free(s_ssl);
1639 # endif
1640 i = close(i);
1642 NYD_LEAVE;
1643 return i;
1646 FL enum okay
1647 swrite(struct sock *sp, char const *data)
1649 enum okay rv;
1650 NYD2_ENTER;
1652 rv = swrite1(sp, data, strlen(data), 0);
1653 NYD2_LEAVE;
1654 return rv;
1657 FL enum okay
1658 swrite1(struct sock *sp, char const *data, int sz, int use_buffer)
1660 enum okay rv = STOP;
1661 int x;
1662 NYD2_ENTER;
1664 if (use_buffer > 0) {
1665 int di;
1667 if (sp->s_wbuf == NULL) {
1668 sp->s_wbufsize = 4096;
1669 sp->s_wbuf = smalloc(sp->s_wbufsize);
1670 sp->s_wbufpos = 0;
1672 while (sp->s_wbufpos + sz > sp->s_wbufsize) {
1673 di = sp->s_wbufsize - sp->s_wbufpos;
1674 sz -= di;
1675 if (sp->s_wbufpos > 0) {
1676 memcpy(sp->s_wbuf + sp->s_wbufpos, data, di);
1677 rv = swrite1(sp, sp->s_wbuf, sp->s_wbufsize, -1);
1678 } else
1679 rv = swrite1(sp, data, sp->s_wbufsize, -1);
1680 if (rv != OKAY)
1681 goto jleave;
1682 data += di;
1683 sp->s_wbufpos = 0;
1685 if (sz == sp->s_wbufsize) {
1686 rv = swrite1(sp, data, sp->s_wbufsize, -1);
1687 if (rv != OKAY)
1688 goto jleave;
1689 } else if (sz) {
1690 memcpy(sp->s_wbuf+ sp->s_wbufpos, data, sz);
1691 sp->s_wbufpos += sz;
1693 rv = OKAY;
1694 goto jleave;
1695 } else if (use_buffer == 0 && sp->s_wbuf != NULL && sp->s_wbufpos > 0) {
1696 x = sp->s_wbufpos;
1697 sp->s_wbufpos = 0;
1698 if ((rv = swrite1(sp, sp->s_wbuf, x, -1)) != OKAY)
1699 goto jleave;
1701 if (sz == 0) {
1702 rv = OKAY;
1703 goto jleave;
1706 # ifdef HAVE_OPENSSL
1707 if (sp->s_use_ssl) {
1708 jssl_retry:
1709 x = SSL_write(sp->s_ssl, data, sz);
1710 if (x < 0) {
1711 switch (SSL_get_error(sp->s_ssl, x)) {
1712 case SSL_ERROR_WANT_READ:
1713 case SSL_ERROR_WANT_WRITE:
1714 goto jssl_retry;
1717 } else
1718 # endif
1720 x = xwrite(sp->s_fd, data, sz);
1722 if (x != sz) {
1723 char o[512];
1724 snprintf(o, sizeof o, "%s write error",
1725 (sp->s_desc ? sp->s_desc : "socket"));
1726 # ifdef HAVE_OPENSSL
1727 if (sp->s_use_ssl)
1728 ssl_gen_err("%s", o);
1729 else
1730 # endif
1731 n_perr(o, 0);
1732 if (x < 0)
1733 sclose(sp);
1734 rv = STOP;
1735 goto jleave;
1737 rv = OKAY;
1738 jleave:
1739 NYD2_LEAVE;
1740 return rv;
1743 static sigjmp_buf __sopen_actjmp; /* TODO someday, we won't need it no more */
1744 static int __sopen_sig; /* TODO someday, we won't need it no more */
1745 static void
1746 __sopen_onsig(int sig) /* TODO someday, we won't need it no more */
1748 NYD_X; /* Signal handler */
1749 if (__sopen_sig < 0) {
1750 /* Of course the following doesn't belong into a signal handler XXX */
1751 int i, j;
1753 if (__sopen_sig == -1) {
1754 fprintf(stderr,
1755 _("\nInterrupting it could turn the (GNU/Linux+) DNS resolver "
1756 "unusable.\n"
1757 " Wait until it's done, or do terminate the program\n"));
1758 __sopen_sig = -2;
1759 } else if ((i = j = ABS(__sopen_sig)) + 15 < scrnwidth) {
1760 putc('\r', stderr);
1761 for (; j > 0; --j)
1762 putc(' ', stderr);
1763 fputs("___( o)", stderr);
1764 putc((i & 1) ? '=' : '>', stderr);
1765 putc(' ', stderr);
1766 putc(' ', stderr);
1767 ++i;
1768 __sopen_sig = -i;
1770 } else {
1771 __sopen_sig = sig;
1772 siglongjmp(__sopen_actjmp, 1);
1776 FL bool_t
1777 sopen(struct sock *sp, struct url *urlp) /* TODO sighandling; refactor */
1779 # ifdef HAVE_SO_SNDTIMEO
1780 struct timeval tv;
1781 # endif
1782 # ifdef HAVE_SO_LINGER
1783 struct linger li;
1784 # endif
1785 # ifdef HAVE_GETADDRINFO
1786 char hbuf[NI_MAXHOST];
1787 struct addrinfo hints, *res0 = NULL, *res;
1788 # else
1789 struct sockaddr_in servaddr;
1790 struct in_addr **pptr;
1791 struct hostent *hp;
1792 struct servent *ep;
1793 # endif
1794 sighandler_type volatile ohup, oint;
1795 char const * volatile serv;
1796 int volatile sofd = -1, errval;
1797 NYD_ENTER;
1799 UNINIT(errval, 0);
1801 /* Connect timeouts after 30 seconds XXX configurable */
1802 # ifdef HAVE_SO_SNDTIMEO
1803 tv.tv_sec = 30;
1804 tv.tv_usec = 0;
1805 # endif
1806 serv = (urlp->url_port != NULL) ? urlp->url_port : urlp->url_proto;
1808 if (options & OPT_VERB)
1809 n_err(_("Resolving host \"%s:%s\" ... "),
1810 urlp->url_host.s, serv);
1812 /* Signal handling (in respect to __sopen_sig dealing) is heavy, but no
1813 * healing until v15.0 and i want to end up with that functionality */
1814 hold_sigs();
1815 __sopen_sig = 0;
1816 ohup = safe_signal(SIGHUP, &__sopen_onsig);
1817 oint = safe_signal(SIGINT, &__sopen_onsig);
1818 if (sigsetjmp(__sopen_actjmp, 0)) {
1819 jpseudo_jump:
1820 n_err("%s\n",
1821 (__sopen_sig == SIGHUP ? _("Hangup") : _("Interrupted")));
1822 if (sofd >= 0) {
1823 close(sofd);
1824 sofd = -1;
1826 goto jjumped;
1828 rele_sigs();
1830 # ifdef HAVE_GETADDRINFO
1831 for (;;) {
1832 memset(&hints, 0, sizeof hints);
1833 hints.ai_socktype = SOCK_STREAM;
1834 __sopen_sig = -1;
1835 errval = getaddrinfo(urlp->url_host.s, serv, &hints, &res0);
1836 if (__sopen_sig != -1) {
1837 __sopen_sig = SIGINT;
1838 goto jpseudo_jump;
1840 __sopen_sig = 0;
1841 if (errval == 0)
1842 break;
1844 if (options & OPT_VERB)
1845 n_err(_("failed\n"));
1846 n_err(_("Lookup of \"%s:%s\" failed: %s\n"),
1847 urlp->url_host.s, serv, gai_strerror(errval));
1849 /* Error seems to depend on how "smart" the /etc/service code is: is it
1850 * "able" to state wether the service as such is NONAME or does it only
1851 * check for the given ai_socktype.. */
1852 if (errval == EAI_NONAME || errval == EAI_SERVICE) {
1853 if (serv == urlp->url_proto &&
1854 (serv = url_servbyname(urlp, NULL)) != NULL) {
1855 n_err(_(" Trying standard protocol port \"%s\"\n"
1856 " If that succeeds consider including the port in the URL!\n"),
1857 serv);
1858 continue;
1860 if (serv != urlp->url_port)
1861 n_err(_(" Including a port number in the URL may "
1862 "circumvent this problem\n"));
1864 assert(sofd == -1);
1865 errval = 0;
1866 goto jjumped;
1868 if (options & OPT_VERB)
1869 n_err(_("done\n"));
1871 for (res = res0; res != NULL && sofd < 0; res = res->ai_next) {
1872 if (options & OPT_VERB) {
1873 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof hbuf,
1874 NULL, 0, NI_NUMERICHOST))
1875 memcpy(hbuf, "unknown host", sizeof("unknown host"));
1876 n_err(_("%sConnecting to \"%s:%s\" ..."),
1877 (res == res0 ? "" : "\n"), hbuf, serv);
1880 sofd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1881 if (sofd >= 0) {
1882 # ifdef HAVE_SO_SNDTIMEO
1883 (void)setsockopt(sofd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
1884 # endif
1885 if (connect(sofd, res->ai_addr, res->ai_addrlen)) {
1886 errval = errno;
1887 close(sofd);
1888 sofd = -1;
1893 jjumped:
1894 if (res0 != NULL) {
1895 freeaddrinfo(res0);
1896 res0 = NULL;
1899 # else /* HAVE_GETADDRINFO */
1900 if (serv == urlp->url_proto) {
1901 if ((ep = getservbyname(UNCONST(serv), "tcp")) != NULL)
1902 urlp->url_portno = ntohs(ep->s_port);
1903 else {
1904 if (options & OPT_VERB)
1905 n_err(_("failed\n"));
1906 if ((serv = url_servbyname(urlp, &urlp->url_portno)) != NULL)
1907 n_err(_(" Unknown service: \"%s\"\n"
1908 " Trying standard protocol port \"%s\"\n"
1909 " If that succeeds consider including the port in the URL!\n"),
1910 urlp->url_proto, serv);
1911 else {
1912 n_err(_(" Unknown service: \"%s\"\n"
1913 " Including a port in the URL may circumvent this problem\n"),
1914 urlp->url_proto);
1915 assert(sofd == -1 && errval == 0);
1916 goto jjumped;
1921 __sopen_sig = -1;
1922 hp = gethostbyname(urlp->url_host.s);
1923 if (__sopen_sig != -1) {
1924 __sopen_sig = SIGINT;
1925 goto jpseudo_jump;
1927 __sopen_sig = 0;
1929 if (hp == NULL) {
1930 char const *emsg;
1932 if (options & OPT_VERB)
1933 n_err(_("failed\n"));
1934 switch (h_errno) {
1935 case HOST_NOT_FOUND: emsg = N_("host not found"); break;
1936 default:
1937 case TRY_AGAIN: emsg = N_("(maybe) try again later"); break;
1938 case NO_RECOVERY: emsg = N_("non-recoverable server error"); break;
1939 case NO_DATA: emsg = N_("valid name without IP address"); break;
1941 n_err(_("Lookup of \"%s:%s\" failed: %s\n"),
1942 urlp->url_host.s, serv, V_(emsg));
1943 goto jjumped;
1944 } else if (options & OPT_VERB)
1945 n_err(_("done\n"));
1947 pptr = (struct in_addr**)hp->h_addr_list;
1948 if ((sofd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
1949 n_perr(_("could not create socket"), 0);
1950 assert(sofd == -1 && errval == 0);
1951 goto jjumped;
1954 memset(&servaddr, 0, sizeof servaddr);
1955 servaddr.sin_family = AF_INET;
1956 servaddr.sin_port = htons(urlp->url_portno);
1957 memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr));
1958 if (options & OPT_VERB)
1959 n_err(_("%sConnecting to \"%s:%d\" ... "),
1960 "", inet_ntoa(**pptr), (int)urlp->url_portno);
1961 # ifdef HAVE_SO_SNDTIMEO
1962 (void)setsockopt(sofd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
1963 # endif
1964 if (connect(sofd, (struct sockaddr*)&servaddr, sizeof servaddr)) {
1965 errval = errno;
1966 close(sofd);
1967 sofd = -1;
1969 jjumped:
1970 # endif /* !HAVE_GETADDRINFO */
1972 hold_sigs();
1973 safe_signal(SIGINT, oint);
1974 safe_signal(SIGHUP, ohup);
1975 rele_sigs();
1977 if (sofd < 0) {
1978 if (errval != 0) {
1979 errno = errval;
1980 n_perr(_("Could not connect"), 0);
1982 goto jleave;
1985 if (options & OPT_VERB)
1986 n_err(_("connected.\n"));
1988 /* And the regular timeouts XXX configurable */
1989 # ifdef HAVE_SO_SNDTIMEO
1990 tv.tv_sec = 42;
1991 tv.tv_usec = 0;
1992 (void)setsockopt(sofd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
1993 (void)setsockopt(sofd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
1994 # endif
1995 # ifdef HAVE_SO_LINGER
1996 li.l_onoff = 1;
1997 li.l_linger = 42;
1998 (void)setsockopt(sofd, SOL_SOCKET, SO_LINGER, &li, sizeof li);
1999 # endif
2001 memset(sp, 0, sizeof *sp);
2002 sp->s_fd = sofd;
2004 /* SSL/TLS upgrade? */
2005 # ifdef HAVE_SSL
2006 if (urlp->url_needs_tls) {
2007 hold_sigs();
2008 ohup = safe_signal(SIGHUP, &__sopen_onsig);
2009 oint = safe_signal(SIGINT, &__sopen_onsig);
2010 if (sigsetjmp(__sopen_actjmp, 0)) {
2011 n_err(_("%s during SSL/TLS handshake\n"),
2012 (__sopen_sig == SIGHUP ? _("Hangup") : _("Interrupted")));
2013 goto jsclose;
2015 rele_sigs();
2017 if (ssl_open(urlp, sp) != OKAY) {
2018 jsclose:
2019 sclose(sp);
2020 sofd = -1;
2023 hold_sigs();
2024 safe_signal(SIGINT, oint);
2025 safe_signal(SIGHUP, ohup);
2026 rele_sigs();
2028 # endif /* HAVE_SSL */
2030 jleave:
2031 /* May need to bounce the signal to the lex.c trampoline (or wherever) */
2032 if (__sopen_sig != 0) {
2033 sigset_t cset;
2034 sigemptyset(&cset);
2035 sigaddset(&cset, __sopen_sig);
2036 sigprocmask(SIG_UNBLOCK, &cset, NULL);
2037 n_raise(__sopen_sig);
2039 NYD_LEAVE;
2040 return (sofd >= 0);
2043 FL int
2044 (sgetline)(char **line, size_t *linesize, size_t *linelen, struct sock *sp
2045 SMALLOC_DEBUG_ARGS)
2047 int rv;
2048 size_t lsize;
2049 char *lp_base, *lp;
2050 NYD2_ENTER;
2052 lsize = *linesize;
2053 lp_base = *line;
2054 lp = lp_base;
2056 if (sp->s_rsz < 0) {
2057 sclose(sp);
2058 rv = sp->s_rsz;
2059 goto jleave;
2062 do {
2063 if (lp_base == NULL || PTRCMP(lp, >, lp_base + lsize - 128)) {
2064 size_t diff = PTR2SIZE(lp - lp_base);
2065 *linesize = (lsize += 256); /* XXX magic */
2066 *line = lp_base = (srealloc)(lp_base, lsize SMALLOC_DEBUG_ARGSCALL);
2067 lp = lp_base + diff;
2070 if (sp->s_rbufptr == NULL ||
2071 PTRCMP(sp->s_rbufptr, >=, sp->s_rbuf + sp->s_rsz)) {
2072 # ifdef HAVE_OPENSSL
2073 if (sp->s_use_ssl) {
2074 jssl_retry:
2075 sp->s_rsz = SSL_read(sp->s_ssl, sp->s_rbuf, sizeof sp->s_rbuf);
2076 if (sp->s_rsz <= 0) {
2077 if (sp->s_rsz < 0) {
2078 char o[512];
2079 switch(SSL_get_error(sp->s_ssl, sp->s_rsz)) {
2080 case SSL_ERROR_WANT_READ:
2081 case SSL_ERROR_WANT_WRITE:
2082 goto jssl_retry;
2084 snprintf(o, sizeof o, "%s",
2085 (sp->s_desc ? sp->s_desc : "socket"));
2086 ssl_gen_err("%s", o);
2088 break;
2090 } else
2091 # endif
2093 jagain:
2094 sp->s_rsz = read(sp->s_fd, sp->s_rbuf, sizeof sp->s_rbuf);
2095 if (sp->s_rsz <= 0) {
2096 if (sp->s_rsz < 0) {
2097 char o[512];
2098 if (errno == EINTR)
2099 goto jagain;
2100 snprintf(o, sizeof o, "%s",
2101 (sp->s_desc ? sp->s_desc : "socket"));
2102 n_perr(o, 0);
2104 break;
2107 sp->s_rbufptr = sp->s_rbuf;
2109 } while ((*lp++ = *sp->s_rbufptr++) != '\n');
2110 *lp = '\0';
2111 lsize = PTR2SIZE(lp - lp_base);
2113 if (linelen)
2114 *linelen = lsize;
2115 rv = (int)lsize;
2116 jleave:
2117 NYD2_LEAVE;
2118 return rv;
2120 #endif /* HAVE_SOCKETS */
2122 FL void
2123 load(char const *name)
2125 struct str n;
2126 void *cond;
2127 FILE *in, *oldin;
2128 NYD_ENTER;
2130 if (name == NULL || *name == '\0' || (in = Fopen(name, "r")) == NULL)
2131 goto jleave;
2133 oldin = _fio_input;
2134 _fio_input = in;
2135 pstate |= PS_IN_LOAD;
2136 /* commands() may sreset(), copy over file name */
2137 n.l = strlen(name);
2138 n.s = ac_alloc(n.l +1);
2139 memcpy(n.s, name, n.l +1);
2141 cond = condstack_release();
2142 if (!commands())
2143 n_err(_("Stopped loading \"%s\" due to errors "
2144 "(enable *debug* for trace)\n"), n.s);
2145 condstack_take(cond);
2147 ac_free(n.s);
2148 pstate &= ~PS_IN_LOAD;
2149 _fio_input = oldin;
2150 Fclose(in);
2151 jleave:
2152 NYD_LEAVE;
2155 FL int
2156 c_source(void *v)
2158 int rv;
2159 NYD_ENTER;
2161 rv = _source_file(*(char**)v, FAL0) ? 0 : 1;
2162 NYD_LEAVE;
2163 return rv;
2166 FL int
2167 c_source_if(void *v) /* XXX obsolete?, support file tests in `if' etc.! */
2169 int rv;
2170 NYD_ENTER;
2172 rv = _source_file(*(char**)v, TRU1) ? 0 : 1;
2173 rv = 0;
2174 NYD_LEAVE;
2175 return rv;
2178 FL int
2179 unstack(void)
2181 int rv = 1;
2182 NYD_ENTER;
2184 if (_fio_stack_size == 0) {
2185 n_err(_("`source' stack over-pop\n"));
2186 pstate &= ~PS_SOURCING;
2187 goto jleave;
2190 if (pstate & PS_PIPING)
2191 Pclose(_fio_input, TRU1);
2192 else
2193 Fclose(_fio_input);
2195 --_fio_stack_size;
2196 if (!condstack_take(_fio_stack[_fio_stack_size].s_cond))
2197 n_err(_("Unmatched \"if\"\n"));
2198 pstate &= ~(PS_LOADING | PS_PIPING);
2199 pstate |= _fio_stack[_fio_stack_size].s_pstate & (PS_LOADING | PS_PIPING);
2200 _fio_input = _fio_stack[_fio_stack_size].s_file;
2201 if (_fio_stack_size == 0) {
2202 if (pstate & PS_LOADING)
2203 pstate |= PS_SOURCING;
2204 else
2205 pstate &= ~PS_SOURCING;
2207 rv = 0;
2208 jleave:
2209 NYD_LEAVE;
2210 return rv;
2213 /* s-it-mode */