only clean out gs when last window goes
[nvi.git] / ex / ex_init.c
blob0fbd9e8da179d5c7dc6bb015a19977fc6f045f8f
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_init.c,v 10.28 2000/06/25 17:34:39 skimo Exp $ (Berkeley) $Date: 2000/06/25 17:34:39 $";
14 #endif /* not lint */
16 #include <sys/param.h>
17 #include <sys/types.h> /* XXX: param.h may not have included types.h */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <bitstring.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "tag.h"
31 #include "pathnames.h"
33 enum rc { NOEXIST, NOPERM, RCOK };
34 static enum rc exrc_isok __P((SCR *, struct stat *, char *, int, int));
36 static int ex_run_file __P((SCR *, char *));
39 * ex_screen_copy --
40 * Copy ex screen.
42 * PUBLIC: int ex_screen_copy __P((SCR *, SCR *));
44 int
45 ex_screen_copy(orig, sp)
46 SCR *orig, *sp;
48 EX_PRIVATE *oexp, *nexp;
50 /* Create the private ex structure. */
51 CALLOC_RET(orig, nexp, EX_PRIVATE *, 1, sizeof(EX_PRIVATE));
52 sp->ex_private = nexp;
54 /* Initialize queues. */
55 CIRCLEQ_INIT(&nexp->tq);
56 TAILQ_INIT(&nexp->tagfq);
57 LIST_INIT(&nexp->cscq);
59 if (orig == NULL) {
60 } else {
61 oexp = EXP(orig);
63 if (oexp->lastbcomm != NULL &&
64 (nexp->lastbcomm = strdup(oexp->lastbcomm)) == NULL) {
65 msgq(sp, M_SYSERR, NULL);
66 return(1);
68 if (ex_tag_copy(orig, sp))
69 return (1);
71 return (0);
75 * ex_screen_end --
76 * End a vi screen.
78 * PUBLIC: int ex_screen_end __P((SCR *));
80 int
81 ex_screen_end(sp)
82 SCR *sp;
84 EX_PRIVATE *exp;
85 int rval;
87 if ((exp = EXP(sp)) == NULL)
88 return (0);
90 rval = 0;
92 /* Close down script connections. */
93 if (F_ISSET(sp, SC_SCRIPT) && sscr_end(sp))
94 rval = 1;
96 if (argv_free(sp))
97 rval = 1;
99 if (exp->ibp != NULL)
100 free(exp->ibp);
102 if (exp->lastbcomm != NULL)
103 free(exp->lastbcomm);
105 if (ex_tag_free(sp))
106 rval = 1;
108 /* Free private memory. */
109 free(exp);
110 sp->ex_private = NULL;
112 return (rval);
116 * ex_optchange --
117 * Handle change of options for ex.
119 * PUBLIC: int ex_optchange __P((SCR *, int, char *, u_long *));
122 ex_optchange(sp, offset, str, valp)
123 SCR *sp;
124 int offset;
125 char *str;
126 u_long *valp;
128 switch (offset) {
129 case O_TAGS:
130 return (ex_tagf_alloc(sp, str));
132 return (0);
136 * ex_exrc --
137 * Read the EXINIT environment variable and the startup exrc files,
138 * and execute their commands.
140 * PUBLIC: int ex_exrc __P((SCR *));
143 ex_exrc(sp)
144 SCR *sp;
146 struct stat hsb, lsb;
147 char *p, path[MAXPATHLEN];
150 * Source the system, environment, $HOME and local .exrc values.
151 * Vi historically didn't check $HOME/.exrc if the environment
152 * variable EXINIT was set. This is all done before the file is
153 * read in, because things in the .exrc information can set, for
154 * example, the recovery directory.
156 * !!!
157 * While nvi can handle any of the options settings of historic vi,
158 * the converse is not true. Since users are going to have to have
159 * files and environmental variables that work with both, we use nvi
160 * versions of both the $HOME and local startup files if they exist,
161 * otherwise the historic ones.
163 * !!!
164 * For a discussion of permissions and when what .exrc files are
165 * read, see the comment above the exrc_isok() function below.
167 * !!!
168 * If the user started the historic of vi in $HOME, vi read the user's
169 * .exrc file twice, as $HOME/.exrc and as ./.exrc. We avoid this, as
170 * it's going to make some commands behave oddly, and I can't imagine
171 * anyone depending on it.
173 switch (exrc_isok(sp, &hsb, _PATH_SYSEXRC, 1, 0)) {
174 case NOEXIST:
175 case NOPERM:
176 break;
177 case RCOK:
178 if (ex_run_file(sp, _PATH_SYSEXRC))
179 return (1);
180 break;
183 /* Run the commands. */
184 if (EXCMD_RUNNING(sp->wp))
185 (void)ex_cmd(sp);
186 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
187 return (0);
189 if ((p = getenv("NEXINIT")) != NULL) {
190 if (ex_run_str(sp, "NEXINIT", p, strlen(p), 1, 0))
191 return (1);
192 } else if ((p = getenv("EXINIT")) != NULL) {
193 if (ex_run_str(sp, "EXINIT", p, strlen(p), 1, 0))
194 return (1);
195 } else if ((p = getenv("HOME")) != NULL && *p) {
196 (void)snprintf(path, sizeof(path), "%s/%s", p, _PATH_NEXRC);
197 switch (exrc_isok(sp, &hsb, path, 0, 1)) {
198 case NOEXIST:
199 (void)snprintf(path,
200 sizeof(path), "%s/%s", p, _PATH_EXRC);
201 if (exrc_isok(sp,
202 &hsb, path, 0, 1) == RCOK && ex_run_file(sp, path))
203 return (1);
204 break;
205 case NOPERM:
206 break;
207 case RCOK:
208 if (ex_run_file(sp, path))
209 return (1);
210 break;
214 /* Run the commands. */
215 if (EXCMD_RUNNING(sp->wp))
216 (void)ex_cmd(sp);
217 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
218 return (0);
220 /* Previous commands may have set the exrc option. */
221 if (O_ISSET(sp, O_EXRC)) {
222 switch (exrc_isok(sp, &lsb, _PATH_NEXRC, 0, 0)) {
223 case NOEXIST:
224 if (exrc_isok(sp, &lsb, _PATH_EXRC, 0, 0) == RCOK &&
225 (lsb.st_dev != hsb.st_dev ||
226 lsb.st_ino != hsb.st_ino) &&
227 ex_run_file(sp, _PATH_EXRC))
228 return (1);
229 break;
230 case NOPERM:
231 break;
232 case RCOK:
233 if ((lsb.st_dev != hsb.st_dev ||
234 lsb.st_ino != hsb.st_ino) &&
235 ex_run_file(sp, _PATH_NEXRC))
236 return (1);
237 break;
239 /* Run the commands. */
240 if (EXCMD_RUNNING(sp->wp))
241 (void)ex_cmd(sp);
242 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
243 return (0);
246 return (0);
250 * ex_run_file --
251 * Set up a file of ex commands to run.
253 static int
254 ex_run_file(sp, name)
255 SCR *sp;
256 char *name;
258 EXCMD cmd;
260 ex_cinit(sp, &cmd, C_SOURCE, 0, OOBLNO, OOBLNO, 0);
261 argv_exp0(sp, &cmd, name, strlen(name));
262 return (ex_source(sp, &cmd));
266 * ex_run_str --
267 * Set up a string of ex commands to run.
269 * PUBLIC: int ex_run_str __P((SCR *, char *, char *, size_t, int, int));
272 ex_run_str(sp, name, str, len, ex_flags, nocopy)
273 SCR *sp;
274 char *name, *str;
275 size_t len;
276 int ex_flags, nocopy;
278 WIN *wp;
279 EXCMD *ecp;
281 wp = sp->wp;
282 if (EXCMD_RUNNING(wp)) {
283 CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
284 LIST_INSERT_HEAD(&wp->ecq, ecp, q);
285 } else
286 ecp = &wp->excmd;
288 F_INIT(ecp,
289 ex_flags ? E_BLIGNORE | E_NOAUTO | E_NOPRDEF | E_VLITONLY : 0);
291 if (nocopy)
292 ecp->cp = str;
293 else
294 if ((ecp->cp = v_strdup(sp, str, len)) == NULL)
295 return (1);
296 ecp->clen = len;
298 if (name == NULL)
299 ecp->if_name = NULL;
300 else {
301 if ((ecp->if_name = v_strdup(sp, name, strlen(name))) == NULL)
302 return (1);
303 ecp->if_lno = 1;
304 F_SET(ecp, E_NAMEDISCARD);
307 return (0);
311 * exrc_isok --
312 * Check a .exrc file for source-ability.
314 * !!!
315 * Historically, vi read the $HOME and local .exrc files if they were owned
316 * by the user's real ID, or the "sourceany" option was set, regardless of
317 * any other considerations. We no longer support the sourceany option as
318 * it's a security problem of mammoth proportions. We require the system
319 * .exrc file to be owned by root, the $HOME .exrc file to be owned by the
320 * user's effective ID (or that the user's effective ID be root) and the
321 * local .exrc files to be owned by the user's effective ID. In all cases,
322 * the file cannot be writeable by anyone other than its owner.
324 * In O'Reilly ("Learning the VI Editor", Fifth Ed., May 1992, page 106),
325 * it notes that System V release 3.2 and later has an option "[no]exrc".
326 * The behavior is that local .exrc files are read only if the exrc option
327 * is set. The default for the exrc option was off, so, by default, local
328 * .exrc files were not read. The problem this was intended to solve was
329 * that System V permitted users to give away files, so there's no possible
330 * ownership or writeability test to ensure that the file is safe.
332 * POSIX 1003.2-1992 standardized exrc as an option. It required the exrc
333 * option to be off by default, thus local .exrc files are not to be read
334 * by default. The Rationale noted (incorrectly) that this was a change
335 * to historic practice, but correctly noted that a default of off improves
336 * system security. POSIX also required that vi check the effective user
337 * ID instead of the real user ID, which is why we've switched from historic
338 * practice.
340 * We initialize the exrc variable to off. If it's turned on by the system
341 * or $HOME .exrc files, and the local .exrc file passes the ownership and
342 * writeability tests, then we read it. This breaks historic 4BSD practice,
343 * but it gives us a measure of security on systems where users can give away
344 * files.
346 static enum rc
347 exrc_isok(sp, sbp, path, rootown, rootid)
348 SCR *sp;
349 struct stat *sbp;
350 char *path;
351 int rootown, rootid;
353 enum { ROOTOWN, OWN, WRITER } etype;
354 uid_t euid;
355 int nf1, nf2;
356 char *a, *b, buf[MAXPATHLEN];
358 /* Check for the file's existence. */
359 if (stat(path, sbp))
360 return (NOEXIST);
362 /* Check ownership permissions. */
363 euid = geteuid();
364 if (!(rootown && sbp->st_uid == 0) &&
365 !(rootid && euid == 0) && sbp->st_uid != euid) {
366 etype = rootown ? ROOTOWN : OWN;
367 goto denied;
370 /* Check writeability. */
371 if (sbp->st_mode & (S_IWGRP | S_IWOTH)) {
372 etype = WRITER;
373 goto denied;
375 return (RCOK);
377 denied: a = msg_print(sp, path, &nf1);
378 if (strchr(path, '/') == NULL && getcwd(buf, sizeof(buf)) != NULL) {
379 b = msg_print(sp, buf, &nf2);
380 switch (etype) {
381 case ROOTOWN:
382 msgq(sp, M_ERR,
383 "125|%s/%s: not sourced: not owned by you or root",
384 b, a);
385 break;
386 case OWN:
387 msgq(sp, M_ERR,
388 "126|%s/%s: not sourced: not owned by you", b, a);
389 break;
390 case WRITER:
391 msgq(sp, M_ERR,
392 "127|%s/%s: not sourced: writeable by a user other than the owner", b, a);
393 break;
395 if (nf2)
396 FREE_SPACE(sp, b, 0);
397 } else
398 switch (etype) {
399 case ROOTOWN:
400 msgq(sp, M_ERR,
401 "128|%s: not sourced: not owned by you or root", a);
402 break;
403 case OWN:
404 msgq(sp, M_ERR,
405 "129|%s: not sourced: not owned by you", a);
406 break;
407 case WRITER:
408 msgq(sp, M_ERR,
409 "130|%s: not sourced: writeable by a user other than the owner", a);
410 break;
413 if (nf1)
414 FREE_SPACE(sp, a, 0);
415 return (NOPERM);