1 /* $OpenBSD: cut.c,v 1.11 2009/10/27 23:59:47 deraadt Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 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 <ctype.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "common.h"
27
28 static void cb_rotate(SCR *);
29
30 /*
31 * cut --
32 * Put a range of lines/columns into a TEXT buffer.
33 *
34 * There are two buffer areas, both found in the global structure. The first
35 * is the linked list of all the buffers the user has named, the second is the
36 * unnamed buffer storage. There is a pointer, too, which is the current
37 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
38 * depending on into what buffer the last text was cut. Logically, in both
39 * delete and yank operations, if the user names a buffer, the text is cut
40 * into it. If it's a delete of information on more than a single line, the
41 * contents of the numbered buffers are rotated up one, the contents of the
42 * buffer named '9' are discarded, and the text is cut into the buffer named
43 * '1'. The text is always cut into the unnamed buffer.
44 *
45 * In all cases, upper-case buffer names are the same as lower-case names,
46 * with the exception that they cause the buffer to be appended to instead
47 * of replaced. Note, however, that if text is appended to a buffer, the
48 * default buffer only contains the appended text, not the entire contents
49 * of the buffer.
50 *
51 * !!!
52 * The contents of the default buffer would disappear after most operations
53 * in historic vi. It's unclear that this is useful, so we don't bother.
54 *
55 * When users explicitly cut text into the numeric buffers, historic vi became
56 * genuinely strange. I've never been able to figure out what was supposed to
57 * happen. It behaved differently if you deleted text than if you yanked text,
58 * and, in the latter case, the text was appended to the buffer instead of
59 * replacing the contents. Hopefully it's not worth getting right, and here
60 * we just treat the numeric buffers like any other named buffer.
61 *
62 * PUBLIC: int cut(SCR *, CHAR_T *, MARK *, MARK *, int);
63 */
64 int
cut(sp,namep,fm,tm,flags)65 cut(sp, namep, fm, tm, flags)
66 SCR *sp;
67 CHAR_T *namep;
68 MARK *fm, *tm;
69 int flags;
70 {
71 CB *cbp;
72 CHAR_T name = '1'; /* default numeric buffer */
73 recno_t lno;
74 int append, copy_one, copy_def;
75
76 /*
77 * If the user specified a buffer, put it there. (This may require
78 * a copy into the numeric buffers. We do the copy so that we don't
79 * have to reference count and so we don't have to deal with things
80 * like appends to buffers that are used multiple times.)
81 *
82 * Otherwise, if it's supposed to be put in a numeric buffer (usually
83 * a delete) put it there. The rules for putting things in numeric
84 * buffers were historically a little strange. There were three cases.
85 *
86 * 1: Some motions are always line mode motions, which means
87 * that the cut always goes into the numeric buffers.
88 * 2: Some motions aren't line mode motions, e.g. d10w, but
89 * can cross line boundaries. For these commands, if the
90 * cut crosses a line boundary, it goes into the numeric
91 * buffers. This includes most of the commands.
92 * 3: Some motions aren't line mode motions, e.g. d`<char>,
93 * but always go into the numeric buffers, regardless. This
94 * was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
95 *
96 * Otherwise, put it in the unnamed buffer.
97 */
98 append = copy_one = copy_def = 0;
99 if (namep != NULL) {
100 name = *namep;
101 if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
102 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
103 copy_one = 1;
104 cb_rotate(sp);
105 }
106 if ((append = isupper(name)) == 1) {
107 if (!copy_one)
108 copy_def = 1;
109 name = tolower(name);
110 }
111 namecb: CBNAME(sp, cbp, name);
112 } else if (LF_ISSET(CUT_NUMREQ) || (LF_ISSET(CUT_NUMOPT) &&
113 (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno))) {
114 /* Copy into numeric buffer 1. */
115 cb_rotate(sp);
116 goto namecb;
117 } else
118 cbp = &sp->gp->dcb_store;
119
120 copyloop:
121 /*
122 * If this is a new buffer, create it and add it into the list.
123 * Otherwise, if it's not an append, free its current contents.
124 */
125 if (cbp == NULL) {
126 CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
127 cbp->name = name;
128 CIRCLEQ_INIT(&cbp->textq);
129 LIST_INSERT_HEAD(&sp->gp->cutq, cbp, q);
130 } else if (!append) {
131 text_lfree(&cbp->textq);
132 cbp->len = 0;
133 cbp->flags = 0;
134 }
135
136
137 /* In line mode, it's pretty easy, just cut the lines. */
138 if (LF_ISSET(CUT_LINEMODE)) {
139 cbp->flags |= CB_LMODE;
140 for (lno = fm->lno; lno <= tm->lno; ++lno)
141 if (cut_line(sp, lno, 0, CUT_LINE_TO_EOL, cbp))
142 goto cut_line_err;
143 } else {
144 /*
145 * Get the first line. A length of CUT_LINE_TO_EOL causes
146 * cut_line() to cut from the MARK to the end of the line.
147 */
148 if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
149 CUT_LINE_TO_EOL : (tm->cno - fm->cno) + 1, cbp))
150 goto cut_line_err;
151
152 /* Get the intermediate lines. */
153 for (lno = fm->lno; ++lno < tm->lno;)
154 if (cut_line(sp, lno, 0, CUT_LINE_TO_EOL, cbp))
155 goto cut_line_err;
156
157 /* Get the last line. */
158 if (tm->lno != fm->lno &&
159 cut_line(sp, lno, 0, tm->cno + 1, cbp))
160 goto cut_line_err;
161 }
162
163 append = 0; /* Only append to the named buffer. */
164 sp->gp->dcbp = cbp; /* Repoint the default buffer on each pass. */
165
166 if (copy_one) { /* Copy into numeric buffer 1. */
167 CBNAME(sp, cbp, name);
168 copy_one = 0;
169 goto copyloop;
170 }
171 if (copy_def) { /* Copy into the default buffer. */
172 cbp = &sp->gp->dcb_store;
173 copy_def = 0;
174 goto copyloop;
175 }
176 return (0);
177
178 cut_line_err:
179 text_lfree(&cbp->textq);
180 cbp->len = 0;
181 cbp->flags = 0;
182 return (1);
183 }
184
185 /*
186 * cb_rotate --
187 * Rotate the numbered buffers up one.
188 */
189 static void
cb_rotate(sp)190 cb_rotate(sp)
191 SCR *sp;
192 {
193 CB *cbp, *del_cbp;
194
195 del_cbp = NULL;
196 LIST_FOREACH(cbp, &sp->gp->cutq, q)
197 switch(cbp->name) {
198 case '1':
199 cbp->name = '2';
200 break;
201 case '2':
202 cbp->name = '3';
203 break;
204 case '3':
205 cbp->name = '4';
206 break;
207 case '4':
208 cbp->name = '5';
209 break;
210 case '5':
211 cbp->name = '6';
212 break;
213 case '6':
214 cbp->name = '7';
215 break;
216 case '7':
217 cbp->name = '8';
218 break;
219 case '8':
220 cbp->name = '9';
221 break;
222 case '9':
223 del_cbp = cbp;
224 break;
225 }
226 if (del_cbp != NULL) {
227 LIST_REMOVE(del_cbp, q);
228 text_lfree(&del_cbp->textq);
229 free(del_cbp);
230 }
231 }
232
233 /*
234 * cut_line --
235 * Cut a portion of a single line.
236 *
237 * PUBLIC: int cut_line(SCR *, recno_t, size_t, size_t, CB *);
238 */
239 int
cut_line(sp,lno,fcno,clen,cbp)240 cut_line(sp, lno, fcno, clen, cbp)
241 SCR *sp;
242 recno_t lno;
243 size_t fcno, clen;
244 CB *cbp;
245 {
246 TEXT *tp;
247 size_t len;
248 char *p;
249
250 /* Get the line. */
251 if (db_get(sp, lno, DBG_FATAL, &p, &len))
252 return (1);
253
254 /* Create a TEXT structure that can hold the entire line. */
255 if ((tp = text_init(sp, NULL, 0, len)) == NULL)
256 return (1);
257
258 /*
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.
261 */
262 if (len != 0) {
263 if (clen == CUT_LINE_TO_EOL)
264 clen = len - fcno;
265 memcpy(tp->lb, p + fcno, clen);
266 tp->len = clen;
267 }
268
269 /* Append to the end of the cut buffer. */
270 CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
271 cbp->len += tp->len;
272
273 return (0);
274 }
275
276 /*
277 * cut_close --
278 * Discard all cut buffers.
279 *
280 * PUBLIC: void cut_close(GS *);
281 */
282 void
cut_close(gp)283 cut_close(gp)
284 GS *gp;
285 {
286 CB *cbp;
287
288 /* Free cut buffer list. */
289 while ((cbp = LIST_FIRST(&gp->cutq)) != NULL) {
290 if (CIRCLEQ_FIRST(&cbp->textq) != CIRCLEQ_END(&cbp->textq))
291 text_lfree(&cbp->textq);
292 LIST_REMOVE(cbp, q);
293 free(cbp);
294 }
295
296 /* Free default cut storage. */
297 cbp = &gp->dcb_store;
298 if (CIRCLEQ_FIRST(&cbp->textq) != CIRCLEQ_END(&cbp->textq))
299 text_lfree(&cbp->textq);
300 }
301
302 /*
303 * text_init --
304 * Allocate a new TEXT structure.
305 *
306 * PUBLIC: TEXT *text_init(SCR *, const char *, size_t, size_t);
307 */
308 TEXT *
text_init(sp,p,len,total_len)309 text_init(sp, p, len, total_len)
310 SCR *sp;
311 const char *p;
312 size_t len, total_len;
313 {
314 TEXT *tp;
315
316 CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
317 if (tp == NULL)
318 return (NULL);
319 /* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
320 if ((tp->lb_len = total_len) != 0) {
321 MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len);
322 if (tp->lb == NULL) {
323 free(tp);
324 return (NULL);
325 }
326 if (p != NULL && len != 0)
327 memcpy(tp->lb, p, len);
328 }
329 tp->len = len;
330 return (tp);
331 }
332
333 /*
334 * text_lfree --
335 * Free a chain of text structures.
336 *
337 * PUBLIC: void text_lfree(TEXTH *);
338 */
339 void
text_lfree(headp)340 text_lfree(headp)
341 TEXTH *headp;
342 {
343 TEXT *tp;
344
345 while ((tp = CIRCLEQ_FIRST(headp)) != CIRCLEQ_END(headp)) {
346 CIRCLEQ_REMOVE(headp, tp, q);
347 text_free(tp);
348 }
349 }
350
351 /*
352 * text_free --
353 * Free a text structure.
354 *
355 * PUBLIC: void text_free(TEXT *);
356 */
357 void
text_free(tp)358 text_free(tp)
359 TEXT *tp;
360 {
361 if (tp->lb != NULL)
362 free(tp->lb);
363 free(tp);
364 }
365