Remove building with NOCRYPTO option
[minix.git] / external / bsd / nvi / dist / common / cut.c
blob43a7ed984199a4f14cf2947a9f984121c5e2781a
1 /* $NetBSD: cut.c,v 1.4 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: cut.c,v 10.18 2001/06/25 15:19:09 skimo Exp (Berkeley) Date: 2001/06/25 15:19:09 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: cut.c,v 1.4 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 <errno.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "common.h"
36 static void cb_rotate __P((SCR *));
39 * cut --
40 * Put a range of lines/columns into a TEXT buffer.
42 * There are two buffer areas, both found in the global structure. The first
43 * is the linked list of all the buffers the user has named, the second is the
44 * unnamed buffer storage. There is a pointer, too, which is the current
45 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
46 * depending on into what buffer the last text was cut. Logically, in both
47 * delete and yank operations, if the user names a buffer, the text is cut
48 * into it. If it's a delete of information on more than a single line, the
49 * contents of the numbered buffers are rotated up one, the contents of the
50 * buffer named '9' are discarded, and the text is cut into the buffer named
51 * '1'. The text is always cut into the unnamed buffer.
53 * In all cases, upper-case buffer names are the same as lower-case names,
54 * with the exception that they cause the buffer to be appended to instead
55 * of replaced. Note, however, that if text is appended to a buffer, the
56 * default buffer only contains the appended text, not the entire contents
57 * of the buffer.
59 * !!!
60 * The contents of the default buffer would disappear after most operations
61 * in historic vi. It's unclear that this is useful, so we don't bother.
63 * When users explicitly cut text into the numeric buffers, historic vi became
64 * genuinely strange. I've never been able to figure out what was supposed to
65 * happen. It behaved differently if you deleted text than if you yanked text,
66 * and, in the latter case, the text was appended to the buffer instead of
67 * replacing the contents. Hopefully it's not worth getting right, and here
68 * we just treat the numeric buffers like any other named buffer.
70 * PUBLIC: int cut __P((SCR *, ARG_CHAR_T *, MARK *, MARK *, int));
72 int
73 cut(SCR *sp, ARG_CHAR_T *namep, MARK *fm, MARK *tm, int flags)
75 CB *cbp;
76 ARG_CHAR_T name = '\0';
77 db_recno_t lno;
78 int append, copy_one, copy_def;
81 * If the user specified a buffer, put it there. (This may require
82 * a copy into the numeric buffers. We do the copy so that we don't
83 * have to reference count and so we don't have to deal with things
84 * like appends to buffers that are used multiple times.)
86 * Otherwise, if it's supposed to be put in a numeric buffer (usually
87 * a delete) put it there. The rules for putting things in numeric
88 * buffers were historically a little strange. There were three cases.
90 * 1: Some motions are always line mode motions, which means
91 * that the cut always goes into the numeric buffers.
92 * 2: Some motions aren't line mode motions, e.g. d10w, but
93 * can cross line boundaries. For these commands, if the
94 * cut crosses a line boundary, it goes into the numeric
95 * buffers. This includes most of the commands.
96 * 3: Some motions aren't line mode motions, e.g. d`<char>,
97 * but always go into the numeric buffers, regardless. This
98 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
100 * Otherwise, put it in the unnamed buffer.
102 append = copy_one = copy_def = 0;
103 if (namep != NULL) {
104 name = *namep;
105 if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
106 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
107 copy_one = 1;
108 cb_rotate(sp);
110 if ((append = ISUPPER(name))) {
111 if (!copy_one)
112 copy_def = 1;
113 name = TOLOWER(name);
115 namecb: CBNAME(sp, cbp, name);
116 } else if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
117 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
118 name = L('1');
119 cb_rotate(sp);
120 goto namecb;
121 } else
122 cbp = &sp->wp->dcb_store;
124 copyloop:
126 * If this is a new buffer, create it and add it into the list.
127 * Otherwise, if it's not an append, free its current contents.
129 if (cbp == NULL) {
130 CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
131 cbp->name = name;
132 TAILQ_INIT(&cbp->textq);
133 LIST_INSERT_HEAD(&sp->wp->cutq, cbp, q);
134 } else if (!append) {
135 text_lfree(&cbp->textq);
136 cbp->len = 0;
137 cbp->flags = 0;
141 /* In line mode, it's pretty easy, just cut the lines. */
142 if (LF_ISSET(CUT_LINEMODE)) {
143 cbp->flags |= CB_LMODE;
144 for (lno = fm->lno; lno <= tm->lno; ++lno)
145 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
146 goto cut_line_err;
147 } else {
149 * Get the first line. A length of ENTIRE_LINE causes cut_line
150 * to cut from the MARK to the end of the line.
152 if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
153 ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
154 goto cut_line_err;
156 /* Get the intermediate lines. */
157 for (lno = fm->lno; ++lno < tm->lno;)
158 if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
159 goto cut_line_err;
161 /* Get the last line. */
162 if (tm->lno != fm->lno &&
163 cut_line(sp, lno, 0, tm->cno + 1, cbp))
164 goto cut_line_err;
167 append = 0; /* Only append to the named buffer. */
168 sp->wp->dcbp = cbp; /* Repoint the default buffer on each pass. */
170 if (copy_one) { /* Copy into numeric buffer 1. */
171 name = L('1');
172 CBNAME(sp, cbp, name);
173 copy_one = 0;
174 goto copyloop;
176 if (copy_def) { /* Copy into the default buffer. */
177 cbp = &sp->wp->dcb_store;
178 copy_def = 0;
179 goto copyloop;
181 return (0);
183 cut_line_err:
184 text_lfree(&cbp->textq);
185 cbp->len = 0;
186 cbp->flags = 0;
187 return (1);
191 * cb_rotate --
192 * Rotate the numbered buffers up one.
194 static void
195 cb_rotate(SCR *sp)
197 CB *cbp, *del_cbp;
199 del_cbp = NULL;
200 LIST_FOREACH(cbp, &sp->wp->cutq, q)
201 switch(cbp->name) {
202 case L('1'):
203 cbp->name = L('2');
204 break;
205 case L('2'):
206 cbp->name = L('3');
207 break;
208 case L('3'):
209 cbp->name = L('4');
210 break;
211 case L('4'):
212 cbp->name = L('5');
213 break;
214 case L('5'):
215 cbp->name = L('6');
216 break;
217 case L('6'):
218 cbp->name = L('7');
219 break;
220 case L('7'):
221 cbp->name = L('8');
222 break;
223 case L('8'):
224 cbp->name = L('9');
225 break;
226 case L('9'):
227 del_cbp = cbp;
228 break;
230 if (del_cbp != NULL) {
231 LIST_REMOVE(del_cbp, q);
232 text_lfree(&del_cbp->textq);
233 free(del_cbp);
238 * cut_line --
239 * Cut a portion of a single line.
241 * PUBLIC: int cut_line __P((SCR *, db_recno_t, size_t, size_t, CB *));
244 cut_line(SCR *sp, db_recno_t lno, size_t fcno, size_t clen, CB *cbp)
246 TEXT *tp;
247 size_t len;
248 CHAR_T *p;
250 /* Get the line. */
251 if (db_get(sp, lno, DBG_FATAL, &p, &len))
252 return (1);
254 /* Create a TEXT structure that can hold the entire line. */
255 if ((tp = text_init(sp, NULL, 0, len)) == NULL)
256 return (1);
259 * If the line isn't empty and it's not the entire line,
260 * copy the portion we want, and reset the TEXT length.
262 if (len != 0) {
263 if (clen == ENTIRE_LINE)
264 clen = len - fcno;
265 MEMCPYW(tp->lb, p + fcno, clen);
266 tp->len = clen;
269 /* Append to the end of the cut buffer. */
270 TAILQ_INSERT_TAIL(&cbp->textq, tp, q);
271 cbp->len += tp->len;
273 return (0);
277 * cut_close --
278 * Discard all cut buffers.
280 * PUBLIC: void cut_close __P((WIN *));
282 void
283 cut_close(WIN *wp)
285 CB *cbp;
287 /* Free cut buffer list. */
288 while ((cbp = LIST_FIRST(&wp->cutq)) != NULL) {
289 if (!TAILQ_EMPTY(&cbp->textq))
290 text_lfree(&cbp->textq);
291 LIST_REMOVE(cbp, q);
292 free(cbp);
295 /* Free default cut storage. */
296 cbp = &wp->dcb_store;
297 if (!TAILQ_EMPTY(&cbp->textq))
298 text_lfree(&cbp->textq);
302 * text_init --
303 * Allocate a new TEXT structure.
305 * PUBLIC: TEXT *text_init __P((SCR *, const CHAR_T *, size_t, size_t));
307 TEXT *
308 text_init(SCR *sp, const CHAR_T *p, size_t len, size_t total_len)
310 TEXT *tp;
312 CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
313 if (tp == NULL)
314 return (NULL);
315 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
316 if ((tp->lb_len = total_len * sizeof(CHAR_T)) != 0) {
317 MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len * sizeof(CHAR_T));
318 if (tp->lb == NULL) {
319 free(tp);
320 return (NULL);
322 if (p != NULL && len != 0)
323 MEMCPYW(tp->lb, p, len);
325 tp->len = len;
326 return (tp);
330 * text_lfree --
331 * Free a chain of text structures.
333 * PUBLIC: void text_lfree __P((TEXTH *));
335 void
336 text_lfree(TEXTH *headp)
338 TEXT *tp;
340 while ((tp = TAILQ_FIRST(headp)) != NULL) {
341 TAILQ_REMOVE(headp, tp, q);
342 text_free(tp);
347 * text_free --
348 * Free a text structure.
350 * PUBLIC: void text_free __P((TEXT *));
352 void
353 text_free(TEXT *tp)
355 if (tp->lb != NULL)
356 free(tp->lb);
357 free(tp);