This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.bin / vi / ex / ex_util.c
CommitLineData
395c4480
AM
1/*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)ex_util.c 8.6 (Berkeley) 3/23/94";
36#endif /* not lint */
37
38#include <sys/types.h>
39#include <queue.h>
40#include <sys/stat.h>
41#include <sys/time.h>
42
43#include <bitstring.h>
44#include <errno.h>
45#include <limits.h>
46#include <signal.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <termios.h>
51#include <unistd.h>
52
53#include <db.h>
54#include <regex.h>
55
56#include "vi.h"
57#include "excmd.h"
58
59/*
60 * ex_getline --
61 * Return a line from the terminal.
62 */
63int
64ex_getline(sp, fp, lenp)
65 SCR *sp;
66 FILE *fp;
67 size_t *lenp;
68{
69 EX_PRIVATE *exp;
70 size_t off;
71 int ch;
72 char *p;
73
74 exp = EXP(sp);
75 for (off = 0, p = exp->ibp;; ++off) {
76 ch = getc(fp);
77 if (off >= exp->ibp_len) {
78 BINC_RET(sp, exp->ibp, exp->ibp_len, off + 1);
79 p = exp->ibp + off;
80 }
81 if (ch == EOF || ch == '\n') {
82 if (ch == EOF && !off)
83 return (1);
84 *lenp = off;
85 return (0);
86 }
87 *p++ = ch;
88 }
89 /* NOTREACHED */
90}
91
92/*
93 * ex_sleave --
94 * Save the terminal/signal state, not screen modification time.
95 * Specific to ex/filter.c and ex/ex_shell.c.
96 */
97int
98ex_sleave(sp)
99 SCR *sp;
100{
101 struct sigaction act;
102 struct stat sb;
103 EX_PRIVATE *exp;
104
105 /* Ignore sessions not using tty's. */
106 if (!F_ISSET(sp->gp, G_STDIN_TTY))
107 return (1);
108
109 /*
110 * The old terminal values almost certainly turn on VINTR, VQUIT and
111 * VSUSP. We don't want to interrupt the parent(s), so we ignore
112 * VINTR. VQUIT is ignored by main() because nvi never wants to catch
113 * it. A VSUSP handler have been installed by the screen code.
114 */
115 act.sa_handler = SIG_IGN;
116 sigemptyset(&act.sa_mask);
117 act.sa_flags = 0;
118
119 exp = EXP(sp);
120 if (sigaction(SIGINT, &act, &exp->leave_act)) {
121 msgq(sp, M_SYSERR, "sigaction");
122 return (1);
123 }
124 if (tcgetattr(STDIN_FILENO, &exp->leave_term)) {
125 msgq(sp, M_SYSERR, "tcgetattr");
126 goto err;
127 }
128 if (tcsetattr(STDIN_FILENO,
129 TCSANOW | TCSASOFT, &sp->gp->original_termios)) {
130 msgq(sp, M_SYSERR, "tcsetattr");
131 /*
132 * If an error occurs, back out the changes and run
133 * without interrupts.
134 */
135err: (void)sigaction(SIGINT, &exp->leave_act, NULL);
136 return (1);
137 }
138 /*
139 * The process may write to the terminal. Save the access time
140 * (read) and modification time (write) of the tty; if they have
141 * changed when we restore the modes, will have to refresh the
142 * screen.
143 */
144 if (fstat(STDIN_FILENO, &sb)) {
145 msgq(sp, M_SYSERR, "stat: stdin");
146 exp->leave_atime = exp->leave_mtime = 0;
147 } else {
148 exp->leave_atime = sb.st_atime;
149 exp->leave_mtime = sb.st_mtime;
150 }
151 return (0);
152}
153
154/*
155 * ex_rleave --
156 * Return the terminal/signal state, not screen modification time.
157 * Specific to ex/filter.c and ex/ex_shell.c.
158 */
159void
160ex_rleave(sp)
161 SCR *sp;
162{
163 EX_PRIVATE *exp;
164 struct stat sb;
165
166 exp = EXP(sp);
167
168 /* Turn off interrupts. */
169 if (tcsetattr(STDIN_FILENO, TCSANOW | TCSASOFT, &exp->leave_term))
170 msgq(sp, M_SYSERR, "tcsetattr");
171
172 /* Reset the signal state. */
173 if (sigaction(SIGINT, &exp->leave_act, NULL))
174 msgq(sp, M_SYSERR, "sigaction");
175
176 /* If the terminal was used, refresh the screen. */
177 if (fstat(STDIN_FILENO, &sb) || exp->leave_atime == 0 ||
178 exp->leave_atime != sb.st_atime || exp->leave_mtime != sb.st_mtime)
179 F_SET(sp, S_REFRESH);
180}