]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/nvi/vi/v_ulcase.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / nvi / vi / v_ulcase.c
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9
10 #include "config.h"
11
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 */
15
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19
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>
27
28 #include "../common/common.h"
29 #include "vi.h"
30
31 static int ulcase __P((SCR *, recno_t, CHAR_T *, size_t, size_t, size_t));
32
33 /*
34  * v_ulcase -- [count]~
35  *      Toggle upper & lower case letters.
36  *
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.
41  *
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.
46  *
47  * PUBLIC: int v_ulcase __P((SCR *, VICMD *));
48  */
49 int
50 v_ulcase(SCR *sp, VICMD *vp)
51 {
52         recno_t lno;
53         size_t cno, lcnt, len;
54         u_long cnt;
55         CHAR_T *p;
56
57         lno = vp->m_start.lno;
58         cno = vp->m_start.cno;
59
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);
66                         }
67                         --lno;
68                         break;
69                 }
70
71                 /* Empty lines decrement the count by one. */
72                 if (len == 0) {
73                         --cnt;
74                         vp->m_final.cno = 0;
75                         continue;
76                 }
77
78                 if (cno + cnt >= len) {
79                         lcnt = len - 1;
80                         cnt -= len - cno;
81
82                         vp->m_final.cno = len - 1;
83                 } else {
84                         lcnt = cno + cnt - 1;
85                         cnt = 0;
86
87                         vp->m_final.cno = lcnt + 1;
88                 }
89
90                 if (ulcase(sp, lno, p, len, cno, lcnt))
91                         return (1);
92
93                 if (cnt > 0)
94                         ++lno;
95         }
96
97         vp->m_final.lno = lno;
98         return (0);
99 }
100
101 /*
102  * v_mulcase -- [count]~[count]motion
103  *      Toggle upper & lower case letters over a range.
104  *
105  * PUBLIC: int v_mulcase __P((SCR *, VICMD *));
106  */
107 int
108 v_mulcase(SCR *sp, VICMD *vp)
109 {
110         CHAR_T *p;
111         size_t len;
112         recno_t lno;
113
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);
122
123                 if (++lno > vp->m_stop.lno)
124                         break;
125         }
126
127         /*
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.
135          */
136         return (0);
137 }
138
139 /*
140  * ulcase --
141  *      Change part of a line's case.
142  */
143 static int
144 ulcase(SCR *sp, recno_t lno, CHAR_T *lp, size_t len, size_t scno, size_t ecno)
145 {
146         size_t blen;
147         int change, rval;
148         ARG_CHAR_T ch;
149         CHAR_T *p, *t, *bp;
150
151         GET_SPACE_RETW(sp, bp, blen, len);
152         MEMMOVE(bp, lp, len);
153
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;
163                 }
164         }
165
166         if (change && db_set(sp, lno, bp, len))
167                 rval = 1;
168
169         FREE_SPACEW(sp, bp, blen);
170         return (rval);
171 }