1 /* $NetBSD: v_increment.c,v 1.2 2013/11/22 15:52:06 christos Exp $ */
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.
14 static const char sccsid
[] = "Id: v_increment.c,v 10.16 2001/06/25 15:19:31 skimo Exp (Berkeley) Date: 2001/06/25 15:19:31 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
29 #include "../common/common.h"
32 static const CHAR_T
*fmt
[] = {
45 static void inc_err
__P((SCR
*, enum nresult
));
48 * v_increment -- [count]#[#+-]
49 * Increment/decrement a keyword number.
51 * PUBLIC: int v_increment __P((SCR *, VICMD *));
54 v_increment(SCR
*sp
, VICMD
*vp
)
59 size_t beg
, blen
, end
, len
, nlen
, wlen
;
60 int base
, isempty
, rval
;
61 CHAR_T
*bp
, *p
, *t
, nbuf
[100];
64 /* Validate the operator. */
65 if (vp
->character
== L('#'))
66 vp
->character
= L('+');
67 if (vp
->character
!= L('+') && vp
->character
!= L('-')) {
68 v_emsg(sp
, vp
->kp
->usage
, VIM_USAGE
);
72 /* If new value set, save it off, but it has to fit in a long. */
73 if (F_ISSET(vp
, VC_C1SET
)) {
74 if (vp
->count
> LONG_MAX
) {
75 inc_err(sp
, NUM_OVER
);
83 if (db_eget(sp
, vp
->m_start
.lno
, &p
, &len
, &isempty
)) {
90 * Skip any leading space before the number. Getting a cursor word
91 * implies moving the cursor to its beginning, if we moved, refresh
94 for (beg
= vp
->m_start
.cno
; beg
< len
&& ISSPACE((UCHAR_T
)p
[beg
]); ++beg
);
97 if (beg
!= vp
->m_start
.cno
) {
99 (void)vs_refresh(sp
, 0);
103 #define isoctal(c) (ISDIGIT(c) && (c) != L('8') && (c) != L('9'))
106 * Look for 0[Xx], or leading + or - signs, guess at the base.
107 * The character after that must be a number. Wlen is set to
108 * the remaining characters in the line that could be part of
112 if (p
[beg
] == L('0') && wlen
> 2 &&
113 (p
[beg
+ 1] == L('X') || p
[beg
+ 1] == L('x'))) {
116 if (!ISXDIGIT((UCHAR_T
)p
[end
]))
118 ntype
= p
[beg
+ 1] == L('X') ? fmt
[HEXC
] : fmt
[HEXL
];
119 } else if (p
[beg
] == L('0') && wlen
> 1) {
122 if (!isoctal((UCHAR_T
)p
[end
]))
125 } else if (wlen
>= 1 && (p
[beg
] == L('+') || p
[beg
] == L('-'))) {
129 if (!ISDIGIT((UCHAR_T
)p
[end
]))
135 if (!ISDIGIT(p
[end
])) {
136 nonum
: msgq(sp
, M_ERR
, "181|Cursor not in a number");
141 /* Find the end of the word, possibly correcting the base. */
142 while (++end
< len
) {
145 if (isoctal((UCHAR_T
)p
[end
]))
147 if (p
[end
] == L('8') || p
[end
] == L('9')) {
154 if (ISDIGIT((UCHAR_T
)p
[end
]))
158 if (ISXDIGIT((UCHAR_T
)p
[end
]))
171 * If the line was at the end of the buffer, we have to copy it
172 * so we can guarantee that it's NULL-terminated. We make the
173 * buffer big enough to fit the line changes as well, and only
176 GET_SPACE_RETW(sp
, bp
, blen
, len
+ 50);
178 MEMMOVEW(bp
, &p
[beg
], wlen
);
185 * Octal or hex deal in unsigned longs, everything else is done
189 if ((nret
= nget_slong(sp
, &lval
, t
, NULL
, 10)) != NUM_OK
)
191 ltmp
= vp
->character
== L('-') ? -change
: change
;
192 if (lval
> 0 && ltmp
> 0 &&
193 !NPFITS(LONG_MAX
, (unsigned long)lval
, (unsigned long)ltmp
)) {
197 if (lval
< 0 && ltmp
< 0 && !NNFITS(LONG_MIN
, lval
, ltmp
)) {
202 /* If we cross 0, signed numbers lose their sign. */
203 if (lval
== 0 && ntype
== fmt
[SDEC
])
205 nlen
= SPRINTF(nbuf
, SIZE(nbuf
), ntype
, lval
);
207 if ((nret
= nget_uslong(sp
, &ulval
, t
, NULL
, base
)) != NUM_OK
)
209 if (vp
->character
== L('+')) {
210 if (!NPFITS(ULONG_MAX
, ulval
, change
)) {
216 if (ulval
< change
) {
223 /* Correct for literal "0[Xx]" in format. */
227 nlen
= SPRINTF(nbuf
, SIZE(nbuf
), ntype
, wlen
, ulval
);
230 /* Build the new line. */
231 MEMMOVEW(bp
, p
, beg
);
232 MEMMOVEW(bp
+ beg
, nbuf
, nlen
);
233 MEMMOVEW(bp
+ beg
+ nlen
, p
+ end
, len
- beg
- (end
- beg
));
234 len
= beg
+ nlen
+ (len
- beg
- (end
- beg
));
237 rval
= db_set(sp
, vp
->m_start
.lno
, bp
, len
);
244 FREE_SPACEW(sp
, bp
, blen
);
249 inc_err(SCR
*sp
, enum nresult nret
)
258 msgq(sp
, M_ERR
, "182|Resulting number too large");
261 msgq(sp
, M_ERR
, "183|Resulting number too small");