Remove building with NOCRYPTO option
[minix.git] / external / bsd / nvi / dist / ex / ex_join.c
blob55602bcf8fe24dcd752ea5c751c2a5522b193eec
1 /* $NetBSD: ex_join.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 static const char sccsid[] = "Id: ex_join.c,v 10.17 2004/03/16 14:14:04 skimo Exp (Berkeley) Date: 2004/03/16 14:14:04 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_join.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20 #endif
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
26 #include <ctype.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "../common/common.h"
35 * ex_join -- :[line [,line]] j[oin][!] [count] [flags]
36 * Join lines.
38 * PUBLIC: int ex_join __P((SCR *, EXCMD *));
40 int
41 ex_join(SCR *sp, EXCMD *cmdp)
43 db_recno_t from, to;
44 size_t blen, clen, len, tlen;
45 int extra, first;
46 ARG_CHAR_T echar = 0;
47 CHAR_T *p, *bp, *tbp = NULL;
49 NEEDFILE(sp, cmdp);
51 from = cmdp->addr1.lno;
52 to = cmdp->addr2.lno;
54 /* Check for no lines to join. */
55 if (!db_exist(sp, from + 1)) {
56 msgq(sp, M_ERR, "131|No following lines to join");
57 return (1);
60 GET_SPACE_RETW(sp, bp, blen, 256);
63 * The count for the join command was off-by-one,
64 * historically, to other counts for other commands.
66 if (F_ISSET(cmdp, E_ADDR_DEF) || cmdp->addrcnt == 1)
67 ++cmdp->addr2.lno;
69 clen = tlen = 0;
70 for (first = 1,
71 from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
73 * Get next line. Historic versions of vi allowed "10J" while
74 * less than 10 lines from the end-of-file, so we do too.
76 if (db_get(sp, from, 0, &p, &len)) {
77 cmdp->addr2.lno = from - 1;
78 break;
81 /* Empty lines just go away. */
82 if (len == 0)
83 continue;
86 * Get more space if necessary. Note, tlen isn't the length
87 * of the new line, it's roughly the amount of space needed.
88 * tbp - bp is the length of the new line.
90 tlen += len + 2;
91 ADD_SPACE_RETW(sp, bp, blen, tlen);
92 tbp = bp + clen;
95 * Historic practice:
97 * If force specified, join without modification.
98 * If the current line ends with whitespace, strip leading
99 * whitespace from the joined line.
100 * If the next line starts with a ), do nothing.
101 * If the current line ends with ., insert two spaces.
102 * Else, insert one space.
104 * One change -- add ? and ! to the list of characters for
105 * which we insert two spaces. I expect that POSIX 1003.2
106 * will require this as well.
108 * Echar is the last character in the last line joined.
110 extra = 0;
111 if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) {
112 if (ISBLANK(echar))
113 for (; len && ISBLANK((UCHAR_T)*p); --len, ++p);
114 else if (p[0] != ')') {
115 if (STRCHR(L(".?!"), echar)) {
116 *tbp++ = ' ';
117 ++clen;
118 extra = 1;
120 *tbp++ = ' ';
121 ++clen;
122 for (; len && ISBLANK((UCHAR_T)*p); --len, ++p);
126 if (len != 0) {
127 MEMCPYW(tbp, p, len);
128 tbp += len;
129 clen += len;
130 echar = p[len - 1];
131 } else
132 echar = ' ';
135 * Historic practice for vi was to put the cursor at the first
136 * inserted whitespace character, if there was one, or the
137 * first character of the joined line, if there wasn't, or the
138 * last character of the line if joined to an empty line. If
139 * a count was specified, the cursor was moved as described
140 * for the first line joined, ignoring subsequent lines. If
141 * the join was a ':' command, the cursor was placed at the
142 * first non-blank character of the line unless the cursor was
143 * "attracted" to the end of line when the command was executed
144 * in which case it moved to the new end of line. There are
145 * probably several more special cases, but frankly, my dear,
146 * I don't give a damn. This implementation puts the cursor
147 * on the first inserted whitespace character, the first
148 * character of the joined line, or the last character of the
149 * line regardless. Note, if the cursor isn't on the joined
150 * line (possible with : commands), it is reset to the starting
151 * line.
153 if (first) {
154 sp->cno = (tbp - bp) - (1 + extra);
155 first = 0;
156 } else
157 sp->cno = (tbp - bp) - len - (1 + extra);
159 sp->lno = cmdp->addr1.lno;
161 /* Delete the joined lines. */
162 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
163 if (db_delete(sp, to))
164 goto err;
166 /* If the original line changed, reset it. */
167 if (!first && db_set(sp, from, bp, tbp - bp)) {
168 err: FREE_SPACEW(sp, bp, blen);
169 return (1);
171 FREE_SPACEW(sp, bp, blen);
173 sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
174 return (0);