GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mg/line.c Lines: 0 274 0.0 %
Date: 2017-11-07 Branches: 0 192 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: line.c,v 1.59 2017/09/09 13:10:28 florian Exp $	*/
2
3
/* This file is in the public domain. */
4
5
/*
6
 *		Text line handling.
7
 *
8
 * The functions in this file are a general set of line management
9
 * utilities. They are the only routines that touch the text. They
10
 * also touch the buffer and window structures to make sure that the
11
 * necessary updating gets done.
12
 *
13
 * Note that this code only updates the dot and mark values in the window
14
 * list.  Since all the code acts on the current window, the buffer that
15
 * we are editing must be displayed, which means that "b_nwnd" is non-zero,
16
 * which means that the dot and mark values in the buffer headers are
17
 * nonsense.
18
 */
19
20
#include <sys/queue.h>
21
#include <limits.h>
22
#include <signal.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include "def.h"
28
29
/*
30
 * Allocate a new line of size `used'.  lrealloc() can be called if the line
31
 * ever needs to grow beyond that.
32
 */
33
struct line *
34
lalloc(int used)
35
{
36
	struct line *lp;
37
38
	if ((lp = malloc(sizeof(*lp))) == NULL)
39
		return (NULL);
40
	lp->l_text = NULL;
41
	lp->l_size = 0;
42
	lp->l_used = used;	/* XXX */
43
	if (lrealloc(lp, used) == FALSE) {
44
		free(lp);
45
		return (NULL);
46
	}
47
	return (lp);
48
}
49
50
int
51
lrealloc(struct line *lp, int newsize)
52
{
53
	char *tmp;
54
55
	if (lp->l_size < newsize) {
56
		if ((tmp = realloc(lp->l_text, newsize)) == NULL)
57
			return (FALSE);
58
		lp->l_text = tmp;
59
		lp->l_size = newsize;
60
	}
61
	return (TRUE);
62
}
63
64
/*
65
 * Delete line "lp".  Fix all of the links that might point to it (they are
66
 * moved to offset 0 of the next line.  Unlink the line from whatever buffer
67
 * it might be in, and release the memory.  The buffers are updated too; the
68
 * magic conditions described in the above comments don't hold here.
69
 */
70
void
71
lfree(struct line *lp)
72
{
73
	struct buffer	*bp;
74
	struct mgwin	*wp;
75
76
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
77
		if (wp->w_linep == lp)
78
			wp->w_linep = lp->l_fp;
79
		if (wp->w_dotp == lp) {
80
			wp->w_dotp = lp->l_fp;
81
			wp->w_doto = 0;
82
		}
83
		if (wp->w_markp == lp) {
84
			wp->w_markp = lp->l_fp;
85
			wp->w_marko = 0;
86
		}
87
	}
88
	for (bp = bheadp; bp != NULL; bp = bp->b_bufp) {
89
		if (bp->b_nwnd == 0) {
90
			if (bp->b_dotp == lp) {
91
				bp->b_dotp = lp->l_fp;
92
				bp->b_doto = 0;
93
			}
94
			if (bp->b_markp == lp) {
95
				bp->b_markp = lp->l_fp;
96
				bp->b_marko = 0;
97
			}
98
		}
99
	}
100
	lp->l_bp->l_fp = lp->l_fp;
101
	lp->l_fp->l_bp = lp->l_bp;
102
	free(lp->l_text);
103
	free(lp);
104
}
105
106
/*
107
 * This routine is called when a character changes in place in the current
108
 * buffer. It updates all of the required flags in the buffer and window
109
 * system. The flag used is passed as an argument; if the buffer is being
110
 * displayed in more than 1 window we change EDIT to HARD. Set MODE if the
111
 * mode line needs to be updated (the "*" has to be set).
112
 */
113
void
114
lchange(int flag)
115
{
116
	struct mgwin	*wp;
117
118
	/* update mode lines if this is the first change. */
119
	if ((curbp->b_flag & BFCHG) == 0) {
120
		flag |= WFMODE;
121
		curbp->b_flag |= BFCHG;
122
	}
123
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
124
		if (wp->w_bufp == curbp) {
125
			wp->w_rflag |= flag;
126
			if (wp != curwp)
127
				wp->w_rflag |= WFFULL;
128
		}
129
	}
130
}
131
132
/*
133
 * Insert "n" copies of the character "c" at the current location of dot.
134
 * In the easy case all that happens is the text is stored in the line.
135
 * In the hard case, the line has to be reallocated.  When the window list
136
 * is updated, take special care; I screwed it up once.  You always update
137
 * dot in the current window.  You update mark and a dot in another window
138
 * if it is greater than the place where you did the insert. Return TRUE
139
 * if all is well, and FALSE on errors.
140
 */
141
int
142
linsert(int n, int c)
143
{
144
	struct line	*lp1;
145
	struct mgwin	*wp;
146
	RSIZE	 i;
147
	int	 doto;
148
	int s;
149
150
	if (!n)
151
		return (TRUE);
152
153
	if ((s = checkdirty(curbp)) != TRUE)
154
		return (s);
155
156
	if (curbp->b_flag & BFREADONLY) {
157
		dobeep();
158
		ewprintf("Buffer is read only");
159
		return (FALSE);
160
	}
161
162
	lchange(WFEDIT);
163
164
	/* current line */
165
	lp1 = curwp->w_dotp;
166
167
	/* special case for the end */
168
	if (lp1 == curbp->b_headp) {
169
		struct line *lp2, *lp3;
170
171
		/* now should only happen in empty buffer */
172
		if (curwp->w_doto != 0) {
173
			dobeep();
174
			ewprintf("bug: linsert");
175
			return (FALSE);
176
		}
177
		/* allocate a new line */
178
		if ((lp2 = lalloc(n)) == NULL)
179
			return (FALSE);
180
		/* previous line */
181
		lp3 = lp1->l_bp;
182
		/* link in */
183
		lp3->l_fp = lp2;
184
		lp2->l_fp = lp1;
185
		lp1->l_bp = lp2;
186
		lp2->l_bp = lp3;
187
		for (i = 0; i < n; ++i)
188
			lp2->l_text[i] = c;
189
		for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
190
			if (wp->w_linep == lp1)
191
				wp->w_linep = lp2;
192
			if (wp->w_dotp == lp1)
193
				wp->w_dotp = lp2;
194
			if (wp->w_markp == lp1)
195
				wp->w_markp = lp2;
196
		}
197
		undo_add_insert(lp2, 0, n);
198
		curwp->w_doto = n;
199
		return (TRUE);
200
	}
201
	/* save for later */
202
	doto = curwp->w_doto;
203
204
	if ((lp1->l_used + n) > lp1->l_size) {
205
		if (lrealloc(lp1, lp1->l_used + n) == FALSE)
206
			return (FALSE);
207
	}
208
	lp1->l_used += n;
209
	if (lp1->l_used != n)
210
		memmove(&lp1->l_text[doto + n], &lp1->l_text[doto],
211
		    lp1->l_used - n - doto);
212
213
	/* Add the characters */
214
	for (i = 0; i < n; ++i)
215
		lp1->l_text[doto + i] = c;
216
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
217
		if (wp->w_dotp == lp1) {
218
			if (wp == curwp || wp->w_doto > doto)
219
				wp->w_doto += n;
220
		}
221
		if (wp->w_markp == lp1) {
222
			if (wp->w_marko > doto)
223
				wp->w_marko += n;
224
		}
225
	}
226
	undo_add_insert(curwp->w_dotp, doto, n);
227
	return (TRUE);
228
}
229
230
/*
231
 * Do the work of inserting a newline at the given line/offset.
232
 * If mark is on the current line, we may have to move the markline
233
 * to keep line numbers in sync.
234
 * lnewline_at assumes the current buffer is writable. Checking for
235
 * this fact should be done by the caller.
236
 */
237
int
238
lnewline_at(struct line *lp1, int doto)
239
{
240
	struct line	*lp2;
241
	struct mgwin	*wp;
242
	int	 	 nlen, tcurwpdotline;
243
244
	lchange(WFFULL);
245
246
	curwp->w_bufp->b_lines++;
247
	/* Check if mark is past dot (even on current line) */
248
	if (curwp->w_markline > curwp->w_dotline  ||
249
	   (curwp->w_dotline == curwp->w_markline &&
250
	    curwp->w_marko >= doto))
251
		curwp->w_markline++;
252
253
	tcurwpdotline = curwp->w_dotline;
254
255
	/* If start of line, allocate a new line instead of copying */
256
	if (doto == 0) {
257
		/* new first part */
258
		if ((lp2 = lalloc(0)) == NULL)
259
			return (FALSE);
260
		lp2->l_bp = lp1->l_bp;
261
		lp1->l_bp->l_fp = lp2;
262
		lp2->l_fp = lp1;
263
		lp1->l_bp = lp2;
264
		for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
265
			if (wp->w_linep == lp1)
266
				wp->w_linep = lp2;
267
			if (wp->w_dotline >= tcurwpdotline &&
268
			    wp->w_bufp == curwp->w_bufp)
269
				wp->w_dotline++;
270
		}
271
		undo_add_boundary(FFRAND, 1);
272
		undo_add_insert(lp2, 0, 1);
273
		undo_add_boundary(FFRAND, 1);
274
		return (TRUE);
275
	}
276
277
	/* length of new part */
278
	nlen = llength(lp1) - doto;
279
280
	/* new second half line */
281
	if ((lp2 = lalloc(nlen)) == NULL)
282
		return (FALSE);
283
	if (nlen != 0)
284
		bcopy(&lp1->l_text[doto], &lp2->l_text[0], nlen);
285
	lp1->l_used = doto;
286
	lp2->l_bp = lp1;
287
	lp2->l_fp = lp1->l_fp;
288
	lp1->l_fp = lp2;
289
	lp2->l_fp->l_bp = lp2;
290
	/* Windows */
291
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
292
		if (wp->w_dotp == lp1 && wp->w_doto >= doto) {
293
			wp->w_dotp = lp2;
294
			wp->w_doto -= doto;
295
			wp->w_dotline++;
296
		} else if (wp->w_dotline > tcurwpdotline &&
297
		    wp->w_bufp == curwp->w_bufp)
298
			wp->w_dotline++;
299
		if (wp->w_markp == lp1 && wp->w_marko >= doto) {
300
			wp->w_markp = lp2;
301
			wp->w_marko -= doto;
302
		}
303
	}
304
	undo_add_boundary(FFRAND, 1);
305
	undo_add_insert(lp1, llength(lp1), 1);
306
	undo_add_boundary(FFRAND, 1);
307
	return (TRUE);
308
}
309
310
/*
311
 * Insert a newline into the buffer at the current location of dot in the
312
 * current window.
313
 */
314
int
315
lnewline(void)
316
{
317
	int s;
318
319
	if ((s = checkdirty(curbp)) != TRUE)
320
		return (s);
321
	if (curbp->b_flag & BFREADONLY) {
322
		dobeep();
323
		ewprintf("Buffer is read only");
324
		return (FALSE);
325
	}
326
	return (lnewline_at(curwp->w_dotp, curwp->w_doto));
327
}
328
329
/*
330
 * This function deletes "n" bytes, starting at dot. (actually, n+1, as the
331
 * newline is included) It understands how to deal with end of lines, etc.
332
 * It returns TRUE if all of the characters were deleted, and FALSE if
333
 * they were not (because dot ran into the end of the buffer).
334
 * The "kflag" indicates either no insertion, or direction  of insertion
335
 * into the kill buffer.
336
 */
337
int
338
ldelete(RSIZE n, int kflag)
339
{
340
	struct line	*dotp;
341
	RSIZE		 chunk;
342
	struct mgwin	*wp;
343
	int		 doto;
344
	char		*cp1, *cp2;
345
	size_t		 len;
346
	char		*sv = NULL;
347
	int		 end;
348
	int		 s;
349
	int		 rval = FALSE;
350
351
	if ((s = checkdirty(curbp)) != TRUE)
352
		return (s);
353
	if (curbp->b_flag & BFREADONLY) {
354
		dobeep();
355
		ewprintf("Buffer is read only");
356
		goto out;
357
	}
358
	len = n;
359
	if ((sv = calloc(1, len + 1)) == NULL)
360
		goto out;
361
	end = 0;
362
363
	undo_add_delete(curwp->w_dotp, curwp->w_doto, n, (kflag & KREG));
364
365
	while (n != 0) {
366
		dotp = curwp->w_dotp;
367
		doto = curwp->w_doto;
368
		/* Hit the end of the buffer */
369
		if (dotp == curbp->b_headp)
370
			goto out;
371
		/* Size of the chunk */
372
		chunk = dotp->l_used - doto;
373
374
		if (chunk > n)
375
			chunk = n;
376
		/* End of line, merge */
377
		if (chunk == 0) {
378
			if (dotp == blastlp(curbp))
379
				goto out;
380
			lchange(WFFULL);
381
			if (ldelnewline() == FALSE)
382
				goto out;
383
			end = strlcat(sv, "\n", len + 1);
384
			--n;
385
			continue;
386
		}
387
		lchange(WFEDIT);
388
		/* Scrunch text */
389
		cp1 = &dotp->l_text[doto];
390
		memcpy(&sv[end], cp1, chunk);
391
		end += chunk;
392
		sv[end] = '\0';
393
		for (cp2 = cp1 + chunk; cp2 < &dotp->l_text[dotp->l_used];
394
		    cp2++)
395
			*cp1++ = *cp2;
396
		dotp->l_used -= (int)chunk;
397
		for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
398
			if (wp->w_dotp == dotp && wp->w_doto >= doto) {
399
				wp->w_doto -= chunk;
400
				if (wp->w_doto < doto)
401
					wp->w_doto = doto;
402
			}
403
			if (wp->w_markp == dotp && wp->w_marko >= doto) {
404
				wp->w_marko -= chunk;
405
				if (wp->w_marko < doto)
406
					wp->w_marko = doto;
407
			}
408
		}
409
		n -= chunk;
410
	}
411
	if (kchunk(sv, (RSIZE)len, kflag) != TRUE)
412
		goto out;
413
	rval = TRUE;
414
out:
415
	free(sv);
416
	return (rval);
417
}
418
419
/*
420
 * Delete a newline and join the current line with the next line. If the next
421
 * line is the magic header line always return TRUE; merging the last line
422
 * with the header line can be thought of as always being a successful
423
 * operation.  Even if nothing is done, this makes the kill buffer work
424
 * "right". If the mark is past the dot (actually, markline > dotline),
425
 * decrease the markline accordingly to keep line numbers in sync.
426
 * Easy cases can be done by shuffling data around.  Hard cases
427
 * require that lines be moved about in memory.  Return FALSE on error and
428
 * TRUE if all looks ok. We do not update w_dotline here, as deletes are done
429
 * after moves.
430
 */
431
int
432
ldelnewline(void)
433
{
434
	struct line	*lp1, *lp2, *lp3;
435
	struct mgwin	*wp;
436
	int s;
437
438
	if ((s = checkdirty(curbp)) != TRUE)
439
		return (s);
440
	if (curbp->b_flag & BFREADONLY) {
441
		dobeep();
442
		ewprintf("Buffer is read only");
443
		return (FALSE);
444
	}
445
446
	lp1 = curwp->w_dotp;
447
	lp2 = lp1->l_fp;
448
	/* at the end of the buffer */
449
	if (lp2 == curbp->b_headp)
450
		return (TRUE);
451
	/* Keep line counts in sync */
452
	curwp->w_bufp->b_lines--;
453
	if (curwp->w_markline > curwp->w_dotline)
454
		curwp->w_markline--;
455
	if (lp2->l_used <= lp1->l_size - lp1->l_used) {
456
		bcopy(&lp2->l_text[0], &lp1->l_text[lp1->l_used], lp2->l_used);
457
		for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
458
			if (wp->w_linep == lp2)
459
				wp->w_linep = lp1;
460
			if (wp->w_dotp == lp2) {
461
				wp->w_dotp = lp1;
462
				wp->w_doto += lp1->l_used;
463
			}
464
			if (wp->w_markp == lp2) {
465
				wp->w_markp = lp1;
466
				wp->w_marko += lp1->l_used;
467
			}
468
		}
469
		lp1->l_used += lp2->l_used;
470
		lp1->l_fp = lp2->l_fp;
471
		lp2->l_fp->l_bp = lp1;
472
		free(lp2);
473
		return (TRUE);
474
	}
475
	if ((lp3 = lalloc(lp1->l_used + lp2->l_used)) == NULL)
476
		return (FALSE);
477
	bcopy(&lp1->l_text[0], &lp3->l_text[0], lp1->l_used);
478
	bcopy(&lp2->l_text[0], &lp3->l_text[lp1->l_used], lp2->l_used);
479
	lp1->l_bp->l_fp = lp3;
480
	lp3->l_fp = lp2->l_fp;
481
	lp2->l_fp->l_bp = lp3;
482
	lp3->l_bp = lp1->l_bp;
483
	for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
484
		if (wp->w_linep == lp1 || wp->w_linep == lp2)
485
			wp->w_linep = lp3;
486
		if (wp->w_dotp == lp1)
487
			wp->w_dotp = lp3;
488
		else if (wp->w_dotp == lp2) {
489
			wp->w_dotp = lp3;
490
			wp->w_doto += lp1->l_used;
491
		}
492
		if (wp->w_markp == lp1)
493
			wp->w_markp = lp3;
494
		else if (wp->w_markp == lp2) {
495
			wp->w_markp = lp3;
496
			wp->w_marko += lp1->l_used;
497
		}
498
	}
499
	free(lp1);
500
	free(lp2);
501
	return (TRUE);
502
}
503
504
/*
505
 * Replace plen characters before dot with argument string.  Control-J
506
 * characters in st are interpreted as newlines.  There is a casehack
507
 * disable flag (normally it likes to match case of replacement to what
508
 * was there).
509
 */
510
int
511
lreplace(RSIZE plen, char *st)
512
{
513
	RSIZE	rlen;	/* replacement length		 */
514
	int s;
515
516
	if ((s = checkdirty(curbp)) != TRUE)
517
		return (s);
518
	if (curbp->b_flag & BFREADONLY) {
519
		dobeep();
520
		ewprintf("Buffer is read only");
521
		return (FALSE);
522
	}
523
	undo_boundary_enable(FFRAND, 0);
524
525
	(void)backchar(FFARG | FFRAND, (int)plen);
526
	(void)ldelete(plen, KNONE);
527
528
	rlen = strlen(st);
529
	region_put_data(st, rlen);
530
	lchange(WFFULL);
531
532
	undo_boundary_enable(FFRAND, 1);
533
	return (TRUE);
534
}
535
536
/*
537
 * Allocate and return the supplied line as a C string
538
 */
539
char *
540
linetostr(const struct line *ln)
541
{
542
	int	 len;
543
	char	*line;
544
545
	len = llength(ln);
546
	if (len == INT_MAX)  /* (len + 1) overflow */
547
		return (NULL);
548
549
	if ((line = malloc(len + 1)) == NULL)
550
		return (NULL);
551
552
	(void)memcpy(line, ltext(ln), len);
553
	line[len] = '\0';
554
555
	return (line);
556
}