GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/vi/build/../vi/v_ulcase.c Lines: 0 51 0.0 %
Date: 2017-11-13 Branches: 0 66 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: v_ulcase.c,v 1.10 2016/05/27 09:18:12 martijn 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
#include <sys/time.h>
17
18
#include <bitstring.h>
19
#include <ctype.h>
20
#include <errno.h>
21
#include <limits.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "../common/common.h"
27
#include "vi.h"
28
29
static int ulcase(SCR *, recno_t, CHAR_T *, size_t, size_t, size_t);
30
31
/*
32
 * v_ulcase -- [count]~
33
 *	Toggle upper & lower case letters.
34
 *
35
 * !!!
36
 * Historic vi didn't permit ~ to cross newline boundaries.  I can
37
 * think of no reason why it shouldn't, which at least lets the user
38
 * auto-repeat through a paragraph.
39
 *
40
 * !!!
41
 * In historic vi, the count was ignored.  It would have been better
42
 * if there had been an associated motion, but it's too late to make
43
 * that the default now.
44
 *
45
 * PUBLIC: int v_ulcase(SCR *, VICMD *);
46
 */
47
int
48
v_ulcase(SCR *sp, VICMD *vp)
49
{
50
	recno_t lno;
51
	size_t cno, lcnt, len;
52
	u_long cnt;
53
	char *p;
54
55
	lno = vp->m_start.lno;
56
	cno = vp->m_start.cno;
57
58
	for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt > 0; cno = 0) {
59
		/* SOF is an error, EOF is an infinite count sink. */
60
		if (db_get(sp, lno, 0, &p, &len)) {
61
			if (lno == 1) {
62
				v_emsg(sp, NULL, VIM_EMPTY);
63
				return (1);
64
			}
65
			--lno;
66
			break;
67
		}
68
69
		/* Empty lines decrement the count by one. */
70
		if (len == 0) {
71
			--cnt;
72
			vp->m_final.cno = 0;
73
			continue;
74
		}
75
76
		if (cno + cnt >= len) {
77
			lcnt = len - 1;
78
			cnt -= len - cno;
79
80
			vp->m_final.cno = len - 1;
81
		} else {
82
			lcnt = cno + cnt - 1;
83
			cnt = 0;
84
85
			vp->m_final.cno = lcnt + 1;
86
		}
87
88
		if (ulcase(sp, lno, p, len, cno, lcnt))
89
			return (1);
90
91
		if (cnt > 0)
92
			++lno;
93
	}
94
95
	vp->m_final.lno = lno;
96
	return (0);
97
}
98
99
/*
100
 * v_mulcase -- [count]~[count]motion
101
 *	Toggle upper & lower case letters over a range.
102
 *
103
 * PUBLIC: int v_mulcase(SCR *, VICMD *);
104
 */
105
int
106
v_mulcase(SCR *sp, VICMD *vp)
107
{
108
	CHAR_T *p;
109
	size_t len;
110
	recno_t lno;
111
112
	for (lno = vp->m_start.lno;;) {
113
		if (db_get(sp, lno, DBG_FATAL, (char **) &p, &len))
114
			return (1);
115
		if (len != 0 && ulcase(sp, lno, p, len,
116
		    lno == vp->m_start.lno ? vp->m_start.cno : 0,
117
		    !F_ISSET(vp, VM_LMODE) &&
118
		    lno == vp->m_stop.lno ? vp->m_stop.cno : len))
119
			return (1);
120
121
		if (++lno > vp->m_stop.lno)
122
			break;
123
	}
124
125
	/*
126
	 * XXX
127
	 * I didn't create a new motion command when I added motion semantics
128
	 * for ~.  While that's the correct way to do it, that choice would
129
	 * have required changes all over the vi directory for little gain.
130
	 * Instead, we pretend it's a yank command.  Note, this means that we
131
	 * follow the cursor motion rules for yank commands, but that seems
132
	 * reasonable to me.
133
	 */
134
	return (0);
135
}
136
137
/*
138
 * ulcase --
139
 *	Change part of a line's case.
140
 */
141
static int
142
ulcase(SCR *sp, recno_t lno, CHAR_T *lp, size_t len, size_t scno, size_t ecno)
143
{
144
	size_t blen;
145
	int change, rval;
146
	CHAR_T ch, *p, *t;
147
	char *bp;
148
149
	GET_SPACE_RET(sp, bp, blen, len);
150
	memmove(bp, lp, len);
151
152
	change = rval = 0;
153
	for (p = bp + scno, t = bp + ecno + 1; p < t; ++p) {
154
		ch = *(u_char *)p;
155
		if (islower(ch)) {
156
			*p = toupper(ch);
157
			change = 1;
158
		} else if (isupper(ch)) {
159
			*p = tolower(ch);
160
			change = 1;
161
		}
162
	}
163
164
	if (change && db_set(sp, lno, bp, len))
165
		rval = 1;
166
167
	FREE_SPACE(sp, bp, blen);
168
	return (rval);
169
}