kernel - cleanup vfs_cache debugging
[dragonfly.git] / contrib / nvi2 / vi / v_ulcase.c
blob9e306caebb60f1d3d1200db7dfd60ddc7c4f24bf
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_ulcase.c,v 10.12 2011/12/02 19:58:32 zy Exp $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "../common/common.h"
29 #include "vi.h"
31 static int ulcase(SCR *, recno_t, CHAR_T *, size_t, size_t, size_t);
34 * v_ulcase -- [count]~
35 * Toggle upper & lower case letters.
37 * !!!
38 * Historic vi didn't permit ~ to cross newline boundaries. I can
39 * think of no reason why it shouldn't, which at least lets the user
40 * auto-repeat through a paragraph.
42 * !!!
43 * In historic vi, the count was ignored. It would have been better
44 * if there had been an associated motion, but it's too late to make
45 * that the default now.
47 * PUBLIC: int v_ulcase(SCR *, VICMD *);
49 int
50 v_ulcase(SCR *sp, VICMD *vp)
52 recno_t lno;
53 size_t cno, lcnt, len;
54 u_long cnt;
55 CHAR_T *p;
57 lno = vp->m_start.lno;
58 cno = vp->m_start.cno;
60 for (cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1; cnt > 0; cno = 0) {
61 /* SOF is an error, EOF is an infinite count sink. */
62 if (db_get(sp, lno, 0, &p, &len)) {
63 if (lno == 1) {
64 v_emsg(sp, NULL, VIM_EMPTY);
65 return (1);
67 --lno;
68 break;
71 /* Empty lines decrement the count by one. */
72 if (len == 0) {
73 --cnt;
74 vp->m_final.cno = 0;
75 continue;
78 if (cno + cnt >= len) {
79 lcnt = len - 1;
80 cnt -= len - cno;
82 vp->m_final.cno = len - 1;
83 } else {
84 lcnt = cno + cnt - 1;
85 cnt = 0;
87 vp->m_final.cno = lcnt + 1;
90 if (ulcase(sp, lno, p, len, cno, lcnt))
91 return (1);
93 if (cnt > 0)
94 ++lno;
97 vp->m_final.lno = lno;
98 return (0);
102 * v_mulcase -- [count]~[count]motion
103 * Toggle upper & lower case letters over a range.
105 * PUBLIC: int v_mulcase(SCR *, VICMD *);
108 v_mulcase(SCR *sp, VICMD *vp)
110 CHAR_T *p;
111 size_t len;
112 recno_t lno;
114 for (lno = vp->m_start.lno;;) {
115 if (db_get(sp, lno, DBG_FATAL, &p, &len))
116 return (1);
117 if (len != 0 && ulcase(sp, lno, p, len,
118 lno == vp->m_start.lno ? vp->m_start.cno : 0,
119 !F_ISSET(vp, VM_LMODE) &&
120 lno == vp->m_stop.lno ? vp->m_stop.cno : len))
121 return (1);
123 if (++lno > vp->m_stop.lno)
124 break;
128 * XXX
129 * I didn't create a new motion command when I added motion semantics
130 * for ~. While that's the correct way to do it, that choice would
131 * have required changes all over the vi directory for little gain.
132 * Instead, we pretend it's a yank command. Note, this means that we
133 * follow the cursor motion rules for yank commands, but that seems
134 * reasonable to me.
136 return (0);
140 * ulcase --
141 * Change part of a line's case.
143 static int
144 ulcase(SCR *sp, recno_t lno, CHAR_T *lp, size_t len, size_t scno, size_t ecno)
146 size_t blen;
147 int change, rval;
148 ARG_CHAR_T ch;
149 CHAR_T *p, *t, *bp;
151 GET_SPACE_RETW(sp, bp, blen, len);
152 MEMMOVE(bp, lp, len);
154 change = rval = 0;
155 for (p = bp + scno, t = bp + ecno + 1; p < t; ++p) {
156 ch = (UCHAR_T)*p;
157 if (ISLOWER(ch)) {
158 *p = TOUPPER(ch);
159 change = 1;
160 } else if (ISUPPER(ch)) {
161 *p = TOLOWER(ch);
162 change = 1;
166 if (change && db_set(sp, lno, bp, len))
167 rval = 1;
169 FREE_SPACEW(sp, bp, blen);
170 return (rval);