tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / vi / v_ulcase.c
blob278e49099ef693a9805d855cbe56282fa2242b43
1 /* $NetBSD: v_ulcase.c,v 1.2 2013/11/22 15:52:06 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 #ifndef lint
14 static const char sccsid[] = "Id: v_ulcase.c,v 10.11 2001/06/25 15:19:36 skimo Exp (Berkeley) Date: 2001/06/25 15:19:36 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "../common/common.h"
30 #include "vi.h"
32 static int ulcase __P((SCR *, db_recno_t, CHAR_T *, size_t, size_t, size_t));
35 * v_ulcase -- [count]~
36 * Toggle upper & lower case letters.
38 * !!!
39 * Historic vi didn't permit ~ to cross newline boundaries. I can
40 * think of no reason why it shouldn't, which at least lets the user
41 * auto-repeat through a paragraph.
43 * !!!
44 * In historic vi, the count was ignored. It would have been better
45 * if there had been an associated motion, but it's too late to make
46 * that the default now.
48 * PUBLIC: int v_ulcase __P((SCR *, VICMD *));
50 int
51 v_ulcase(SCR *sp, VICMD *vp)
53 db_recno_t lno;
54 size_t cno, lcnt, len;
55 u_long cnt;
56 CHAR_T *p;
58 lno = vp->m_start.lno;
59 cno = vp->m_start.cno;
61 for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt > 0; cno = 0) {
62 /* SOF is an error, EOF is an infinite count sink. */
63 if (db_get(sp, lno, 0, &p, &len)) {
64 if (lno == 1) {
65 v_emsg(sp, NULL, VIM_EMPTY);
66 return (1);
68 --lno;
69 break;
72 /* Empty lines decrement the count by one. */
73 if (len == 0) {
74 --cnt;
75 vp->m_final.cno = 0;
76 continue;
79 if (cno + cnt >= len) {
80 lcnt = len - 1;
81 cnt -= len - cno;
83 vp->m_final.cno = len - 1;
84 } else {
85 lcnt = cno + cnt - 1;
86 cnt = 0;
88 vp->m_final.cno = lcnt + 1;
91 if (ulcase(sp, lno, p, len, cno, lcnt))
92 return (1);
94 if (cnt > 0)
95 ++lno;
98 vp->m_final.lno = lno;
99 return (0);
103 * v_mulcase -- [count]~[count]motion
104 * Toggle upper & lower case letters over a range.
106 * PUBLIC: int v_mulcase __P((SCR *, VICMD *));
109 v_mulcase(SCR *sp, VICMD *vp)
111 CHAR_T *p;
112 size_t len;
113 db_recno_t lno;
115 for (lno = vp->m_start.lno;;) {
116 if (db_get(sp, lno, DBG_FATAL, &p, &len))
117 return (1);
118 if (len != 0 && ulcase(sp, lno, p, len,
119 lno == vp->m_start.lno ? vp->m_start.cno : 0,
120 !F_ISSET(vp, VM_LMODE) &&
121 lno == vp->m_stop.lno ? vp->m_stop.cno : len))
122 return (1);
124 if (++lno > vp->m_stop.lno)
125 break;
129 * XXX
130 * I didn't create a new motion command when I added motion semantics
131 * for ~. While that's the correct way to do it, that choice would
132 * have required changes all over the vi directory for little gain.
133 * Instead, we pretend it's a yank command. Note, this means that we
134 * follow the cursor motion rules for yank commands, but that seems
135 * reasonable to me.
137 return (0);
141 * ulcase --
142 * Change part of a line's case.
144 static int
145 ulcase(SCR *sp, db_recno_t lno, CHAR_T *lp, size_t len, size_t scno, size_t ecno)
147 size_t blen;
148 int change, rval;
149 ARG_CHAR_T ch;
150 CHAR_T *p, *t, *bp;
152 GET_SPACE_RETW(sp, bp, blen, len);
153 MEMMOVEW(bp, lp, len);
155 change = rval = 0;
156 for (p = bp + scno, t = bp + ecno + 1; p < t; ++p) {
157 ch = (UCHAR_T)*p;
158 if (ISLOWER(ch)) {
159 *p = TOUPPER(ch);
160 change = 1;
161 } else if (ISUPPER(ch)) {
162 *p = TOLOWER(ch);
163 change = 1;
167 if (change && db_set(sp, lno, bp, len))
168 rval = 1;
170 FREE_SPACEW(sp, bp, blen);
171 return (rval);