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