1 /*	$OpenBSD: ex_z.c,v 1.6 2009/10/27 23:59:47 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 
17 #include <bitstring.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "../common/common.h"
24 
25 /*
26  * ex_z -- :[line] z [^-.+=] [count] [flags]
27  *	Adjust window.
28  *
29  * PUBLIC: int ex_z(SCR *, EXCMD *);
30  */
31 int
ex_z(sp,cmdp)32 ex_z(sp, cmdp)
33 	SCR *sp;
34 	EXCMD *cmdp;
35 {
36 	MARK mark_abs;
37 	recno_t cnt, equals, lno;
38 	int eofcheck;
39 
40 	NEEDFILE(sp, cmdp);
41 
42 	/*
43 	 * !!!
44 	 * If no count specified, use either two times the size of the
45 	 * scrolling region, or the size of the window option.  POSIX
46 	 * 1003.2 claims that the latter is correct, but historic ex/vi
47 	 * documentation and practice appear to use the scrolling region.
48 	 * I'm using the window size as it means that the entire screen
49 	 * is used instead of losing a line to roundoff.  Note, we drop
50 	 * a line from the cnt if using the window size to leave room for
51 	 * the next ex prompt.
52 	 */
53 	if (FL_ISSET(cmdp->iflags, E_C_COUNT))
54 		cnt = cmdp->count;
55 	else
56 #ifdef HISTORIC_PRACTICE
57 		cnt = O_VAL(sp, O_SCROLL) * 2;
58 #else
59 		cnt = O_VAL(sp, O_WINDOW) - 1;
60 #endif
61 
62 	equals = 0;
63 	eofcheck = 0;
64 	lno = cmdp->addr1.lno;
65 
66 	switch (FL_ISSET(cmdp->iflags,
67 	    E_C_CARAT | E_C_DASH | E_C_DOT | E_C_EQUAL | E_C_PLUS)) {
68 	case E_C_CARAT:		/* Display cnt * 2 before the line. */
69 		eofcheck = 1;
70 		if (lno > cnt * 2)
71 			cmdp->addr1.lno = (lno - cnt * 2) + 1;
72 		else
73 			cmdp->addr1.lno = 1;
74 		cmdp->addr2.lno = (cmdp->addr1.lno + cnt) - 1;
75 		break;
76 	case E_C_DASH:		/* Line goes at the bottom of the screen. */
77 		cmdp->addr1.lno = lno > cnt ? (lno - cnt) + 1 : 1;
78 		cmdp->addr2.lno = lno;
79 		break;
80 	case E_C_DOT:		/* Line goes in the middle of the screen. */
81 		/*
82 		 * !!!
83 		 * Historically, the "middleness" of the line overrode the
84 		 * count, so that "3z.19" or "3z.20" would display the first
85 		 * 12 lines of the file, i.e. (N - 1) / 2 lines before and
86 		 * after the specified line.
87 		 */
88 		eofcheck = 1;
89 		cnt = (cnt - 1) / 2;
90 		cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
91 		cmdp->addr2.lno = lno + cnt;
92 
93 		/*
94 		 * !!!
95 		 * Historically, z. set the absolute cursor mark.
96 		 */
97 		mark_abs.lno = sp->lno;
98 		mark_abs.cno = sp->cno;
99 		(void)mark_set(sp, ABSMARK1, &mark_abs, 1);
100 		break;
101 	case E_C_EQUAL:		/* Center with hyphens. */
102 		/*
103 		 * !!!
104 		 * Strangeness.  The '=' flag is like the '.' flag (see the
105 		 * above comment, it applies here as well) but with a special
106 		 * little hack.  Print out lines of hyphens before and after
107 		 * the specified line.  Additionally, the cursor remains set
108 		 * on that line.
109 		 */
110 		eofcheck = 1;
111 		cnt = (cnt - 1) / 2;
112 		cmdp->addr1.lno = lno > cnt ? lno - cnt : 1;
113 		cmdp->addr2.lno = lno - 1;
114 		if (ex_pr(sp, cmdp))
115 			return (1);
116 		(void)ex_puts(sp, "----------------------------------------\n");
117 		cmdp->addr2.lno = cmdp->addr1.lno = equals = lno;
118 		if (ex_pr(sp, cmdp))
119 			return (1);
120 		(void)ex_puts(sp, "----------------------------------------\n");
121 		cmdp->addr1.lno = lno + 1;
122 		cmdp->addr2.lno = (lno + cnt) - 1;
123 		break;
124 	default:
125 		/* If no line specified, move to the next one. */
126 		if (F_ISSET(cmdp, E_ADDR_DEF))
127 			++lno;
128 		/* FALLTHROUGH */
129 	case E_C_PLUS:		/* Line goes at the top of the screen. */
130 		eofcheck = 1;
131 		cmdp->addr1.lno = lno;
132 		cmdp->addr2.lno = (lno + cnt) - 1;
133 		break;
134 	}
135 
136 	if (eofcheck) {
137 		if (db_last(sp, &lno))
138 			return (1);
139 		if (cmdp->addr2.lno > lno)
140 			cmdp->addr2.lno = lno;
141 	}
142 
143 	if (ex_pr(sp, cmdp))
144 		return (1);
145 	if (equals)
146 		sp->lno = equals;
147 	return (0);
148 }
149