1b8ba871bSPeter Wemm /*-
2b8ba871bSPeter Wemm * Copyright (c) 1992, 1993, 1994
3b8ba871bSPeter Wemm * The Regents of the University of California. All rights reserved.
4b8ba871bSPeter Wemm * Copyright (c) 1992, 1993, 1994, 1995, 1996
5b8ba871bSPeter Wemm * Keith Bostic. All rights reserved.
6b8ba871bSPeter Wemm *
7b8ba871bSPeter Wemm * See the LICENSE file for redistribution information.
8b8ba871bSPeter Wemm */
9b8ba871bSPeter Wemm
10b8ba871bSPeter Wemm #include "config.h"
11b8ba871bSPeter Wemm
12b8ba871bSPeter Wemm #include <sys/types.h>
13b8ba871bSPeter Wemm #include <sys/queue.h>
14b8ba871bSPeter Wemm #include <sys/time.h>
15b8ba871bSPeter Wemm
16b8ba871bSPeter Wemm #include <bitstring.h>
17b8ba871bSPeter Wemm #include <ctype.h>
18b8ba871bSPeter Wemm #include <errno.h>
19b8ba871bSPeter Wemm #include <limits.h>
20b8ba871bSPeter Wemm #include <stdio.h>
21b8ba871bSPeter Wemm #include <stdlib.h>
22b8ba871bSPeter Wemm #include <string.h>
23b8ba871bSPeter Wemm
24b8ba871bSPeter Wemm #include "../common/common.h"
25b8ba871bSPeter Wemm #include "vi.h"
26b8ba871bSPeter Wemm
27f0957ccaSPeter Wemm static CHAR_T * const fmt[] = {
28b8ba871bSPeter Wemm #define DEC 0
29f0957ccaSPeter Wemm L("%ld"),
30b8ba871bSPeter Wemm #define SDEC 1
31f0957ccaSPeter Wemm L("%+ld"),
32b8ba871bSPeter Wemm #define HEXC 2
33f0957ccaSPeter Wemm L("0X%0*lX"),
34b8ba871bSPeter Wemm #define HEXL 3
35f0957ccaSPeter Wemm L("0x%0*lx"),
36b8ba871bSPeter Wemm #define OCTAL 4
37f0957ccaSPeter Wemm L("%#0*lo"),
38b8ba871bSPeter Wemm };
39b8ba871bSPeter Wemm
40c271fa92SBaptiste Daroussin static void inc_err(SCR *, enum nresult);
41b8ba871bSPeter Wemm
42b8ba871bSPeter Wemm /*
43b8ba871bSPeter Wemm * v_increment -- [count]#[#+-]
44b8ba871bSPeter Wemm * Increment/decrement a keyword number.
45b8ba871bSPeter Wemm *
46c271fa92SBaptiste Daroussin * PUBLIC: int v_increment(SCR *, VICMD *);
47b8ba871bSPeter Wemm */
48b8ba871bSPeter Wemm int
v_increment(SCR * sp,VICMD * vp)49f0957ccaSPeter Wemm v_increment(SCR *sp, VICMD *vp)
50b8ba871bSPeter Wemm {
51b8ba871bSPeter Wemm enum nresult nret;
52b8ba871bSPeter Wemm u_long ulval;
53b8ba871bSPeter Wemm long change, ltmp, lval;
54b8ba871bSPeter Wemm size_t beg, blen, end, len, nlen, wlen;
55b8ba871bSPeter Wemm int base, isempty, rval;
56f0957ccaSPeter Wemm CHAR_T *ntype, nbuf[100];
57f0957ccaSPeter Wemm CHAR_T *bp, *p, *t;
58b8ba871bSPeter Wemm
59b8ba871bSPeter Wemm /* Validate the operator. */
60b8ba871bSPeter Wemm if (vp->character == '#')
61b8ba871bSPeter Wemm vp->character = '+';
62b8ba871bSPeter Wemm if (vp->character != '+' && vp->character != '-') {
63b8ba871bSPeter Wemm v_emsg(sp, vp->kp->usage, VIM_USAGE);
64b8ba871bSPeter Wemm return (1);
65b8ba871bSPeter Wemm }
66b8ba871bSPeter Wemm
67b8ba871bSPeter Wemm /* If new value set, save it off, but it has to fit in a long. */
68b8ba871bSPeter Wemm if (F_ISSET(vp, VC_C1SET)) {
69b8ba871bSPeter Wemm if (vp->count > LONG_MAX) {
70b8ba871bSPeter Wemm inc_err(sp, NUM_OVER);
71b8ba871bSPeter Wemm return (1);
72b8ba871bSPeter Wemm }
73b8ba871bSPeter Wemm change = vp->count;
74b8ba871bSPeter Wemm } else
75b8ba871bSPeter Wemm change = 1;
76b8ba871bSPeter Wemm
77b8ba871bSPeter Wemm /* Get the line. */
78b8ba871bSPeter Wemm if (db_eget(sp, vp->m_start.lno, &p, &len, &isempty)) {
79b8ba871bSPeter Wemm if (isempty)
80b8ba871bSPeter Wemm goto nonum;
81b8ba871bSPeter Wemm return (1);
82b8ba871bSPeter Wemm }
83b8ba871bSPeter Wemm
84b8ba871bSPeter Wemm /*
85b8ba871bSPeter Wemm * Skip any leading space before the number. Getting a cursor word
86b8ba871bSPeter Wemm * implies moving the cursor to its beginning, if we moved, refresh
87b8ba871bSPeter Wemm * now.
88b8ba871bSPeter Wemm */
89f0957ccaSPeter Wemm for (beg = vp->m_start.cno; beg < len && ISSPACE(p[beg]); ++beg);
90b8ba871bSPeter Wemm if (beg >= len)
91b8ba871bSPeter Wemm goto nonum;
92b8ba871bSPeter Wemm if (beg != vp->m_start.cno) {
93b8ba871bSPeter Wemm sp->cno = beg;
94b8ba871bSPeter Wemm (void)vs_refresh(sp, 0);
95b8ba871bSPeter Wemm }
96b8ba871bSPeter Wemm
97b8ba871bSPeter Wemm #undef ishex
98f0957ccaSPeter Wemm #define ishex(c) (ISXDIGIT(c))
99b8ba871bSPeter Wemm #undef isoctal
100f0957ccaSPeter Wemm #define isoctal(c) ((c) >= '0' && (c) <= '7')
101b8ba871bSPeter Wemm
102b8ba871bSPeter Wemm /*
103b8ba871bSPeter Wemm * Look for 0[Xx], or leading + or - signs, guess at the base.
104b8ba871bSPeter Wemm * The character after that must be a number. Wlen is set to
105b8ba871bSPeter Wemm * the remaining characters in the line that could be part of
106b8ba871bSPeter Wemm * the number.
107b8ba871bSPeter Wemm */
108b8ba871bSPeter Wemm wlen = len - beg;
109b8ba871bSPeter Wemm if (p[beg] == '0' && wlen > 2 &&
110b8ba871bSPeter Wemm (p[beg + 1] == 'X' || p[beg + 1] == 'x')) {
111b8ba871bSPeter Wemm base = 16;
112b8ba871bSPeter Wemm end = beg + 2;
113b8ba871bSPeter Wemm if (!ishex(p[end]))
114b8ba871bSPeter Wemm goto decimal;
115b8ba871bSPeter Wemm ntype = p[beg + 1] == 'X' ? fmt[HEXC] : fmt[HEXL];
116b8ba871bSPeter Wemm } else if (p[beg] == '0' && wlen > 1) {
117b8ba871bSPeter Wemm base = 8;
118b8ba871bSPeter Wemm end = beg + 1;
119b8ba871bSPeter Wemm if (!isoctal(p[end]))
120b8ba871bSPeter Wemm goto decimal;
121b8ba871bSPeter Wemm ntype = fmt[OCTAL];
122b8ba871bSPeter Wemm } else if (wlen >= 1 && (p[beg] == '+' || p[beg] == '-')) {
123b8ba871bSPeter Wemm base = 10;
124b8ba871bSPeter Wemm end = beg + 1;
125b8ba871bSPeter Wemm ntype = fmt[SDEC];
126b8ba871bSPeter Wemm if (!isdigit(p[end]))
127b8ba871bSPeter Wemm goto nonum;
128b8ba871bSPeter Wemm } else {
129b8ba871bSPeter Wemm decimal: base = 10;
130b8ba871bSPeter Wemm end = beg;
131b8ba871bSPeter Wemm ntype = fmt[DEC];
132b8ba871bSPeter Wemm if (!isdigit(p[end])) {
133b8ba871bSPeter Wemm nonum: msgq(sp, M_ERR, "181|Cursor not in a number");
134b8ba871bSPeter Wemm return (1);
135b8ba871bSPeter Wemm }
136b8ba871bSPeter Wemm }
137b8ba871bSPeter Wemm
138b8ba871bSPeter Wemm /* Find the end of the word, possibly correcting the base. */
139b8ba871bSPeter Wemm while (++end < len) {
140b8ba871bSPeter Wemm switch (base) {
141b8ba871bSPeter Wemm case 8:
142b8ba871bSPeter Wemm if (isoctal(p[end]))
143b8ba871bSPeter Wemm continue;
144b8ba871bSPeter Wemm if (p[end] == '8' || p[end] == '9') {
145b8ba871bSPeter Wemm base = 10;
146b8ba871bSPeter Wemm ntype = fmt[DEC];
147b8ba871bSPeter Wemm continue;
148b8ba871bSPeter Wemm }
149b8ba871bSPeter Wemm break;
150b8ba871bSPeter Wemm case 10:
151b8ba871bSPeter Wemm if (isdigit(p[end]))
152b8ba871bSPeter Wemm continue;
153b8ba871bSPeter Wemm break;
154b8ba871bSPeter Wemm case 16:
155b8ba871bSPeter Wemm if (ishex(p[end]))
156b8ba871bSPeter Wemm continue;
157b8ba871bSPeter Wemm break;
158b8ba871bSPeter Wemm default:
159b8ba871bSPeter Wemm abort();
160b8ba871bSPeter Wemm /* NOTREACHED */
161b8ba871bSPeter Wemm }
162b8ba871bSPeter Wemm break;
163b8ba871bSPeter Wemm }
164b8ba871bSPeter Wemm wlen = (end - beg);
165b8ba871bSPeter Wemm
166b8ba871bSPeter Wemm /*
167b8ba871bSPeter Wemm * XXX
168b8ba871bSPeter Wemm * If the line was at the end of the buffer, we have to copy it
169b8ba871bSPeter Wemm * so we can guarantee that it's NULL-terminated. We make the
170b8ba871bSPeter Wemm * buffer big enough to fit the line changes as well, and only
171b8ba871bSPeter Wemm * allocate once.
172b8ba871bSPeter Wemm */
173f0957ccaSPeter Wemm GET_SPACE_RETW(sp, bp, blen, len + 50);
174b8ba871bSPeter Wemm if (end == len) {
175f0957ccaSPeter Wemm MEMMOVE(bp, &p[beg], wlen);
176b8ba871bSPeter Wemm bp[wlen] = '\0';
177b8ba871bSPeter Wemm t = bp;
178b8ba871bSPeter Wemm } else
179b8ba871bSPeter Wemm t = &p[beg];
180b8ba871bSPeter Wemm
181b8ba871bSPeter Wemm /*
182b8ba871bSPeter Wemm * Octal or hex deal in unsigned longs, everything else is done
183b8ba871bSPeter Wemm * in signed longs.
184b8ba871bSPeter Wemm */
185b8ba871bSPeter Wemm if (base == 10) {
186b8ba871bSPeter Wemm if ((nret = nget_slong(&lval, t, NULL, 10)) != NUM_OK)
187b8ba871bSPeter Wemm goto err;
188b8ba871bSPeter Wemm ltmp = vp->character == '-' ? -change : change;
189b8ba871bSPeter Wemm if (lval > 0 && ltmp > 0 && !NPFITS(LONG_MAX, lval, ltmp)) {
190b8ba871bSPeter Wemm nret = NUM_OVER;
191b8ba871bSPeter Wemm goto err;
192b8ba871bSPeter Wemm }
193b8ba871bSPeter Wemm if (lval < 0 && ltmp < 0 && !NNFITS(LONG_MIN, lval, ltmp)) {
194b8ba871bSPeter Wemm nret = NUM_UNDER;
195b8ba871bSPeter Wemm goto err;
196b8ba871bSPeter Wemm }
197b8ba871bSPeter Wemm lval += ltmp;
198b8ba871bSPeter Wemm /* If we cross 0, signed numbers lose their sign. */
199b8ba871bSPeter Wemm if (lval == 0 && ntype == fmt[SDEC])
200b8ba871bSPeter Wemm ntype = fmt[DEC];
201*6680e5a5SBaptiste Daroussin nlen = SPRINTF(nbuf, SIZE(nbuf), ntype, lval);
202b8ba871bSPeter Wemm } else {
203b8ba871bSPeter Wemm if ((nret = nget_uslong(&ulval, t, NULL, base)) != NUM_OK)
204b8ba871bSPeter Wemm goto err;
205b8ba871bSPeter Wemm if (vp->character == '+') {
206b8ba871bSPeter Wemm if (!NPFITS(ULONG_MAX, ulval, change)) {
207b8ba871bSPeter Wemm nret = NUM_OVER;
208b8ba871bSPeter Wemm goto err;
209b8ba871bSPeter Wemm }
210b8ba871bSPeter Wemm ulval += change;
211b8ba871bSPeter Wemm } else {
212b8ba871bSPeter Wemm if (ulval < change) {
213b8ba871bSPeter Wemm nret = NUM_UNDER;
214b8ba871bSPeter Wemm goto err;
215b8ba871bSPeter Wemm }
216b8ba871bSPeter Wemm ulval -= change;
217b8ba871bSPeter Wemm }
218b8ba871bSPeter Wemm
219b8ba871bSPeter Wemm /* Correct for literal "0[Xx]" in format. */
220b8ba871bSPeter Wemm if (base == 16)
221b8ba871bSPeter Wemm wlen -= 2;
222b8ba871bSPeter Wemm
223*6680e5a5SBaptiste Daroussin nlen = SPRINTF(nbuf, SIZE(nbuf), ntype, wlen, ulval);
224b8ba871bSPeter Wemm }
225b8ba871bSPeter Wemm
226b8ba871bSPeter Wemm /* Build the new line. */
227f0957ccaSPeter Wemm MEMMOVE(bp, p, beg);
228f0957ccaSPeter Wemm MEMMOVE(bp + beg, nbuf, nlen);
229f0957ccaSPeter Wemm MEMMOVE(bp + beg + nlen, p + end, len - beg - (end - beg));
230b8ba871bSPeter Wemm len = beg + nlen + (len - beg - (end - beg));
231b8ba871bSPeter Wemm
232b8ba871bSPeter Wemm nret = NUM_OK;
233b8ba871bSPeter Wemm rval = db_set(sp, vp->m_start.lno, bp, len);
234b8ba871bSPeter Wemm
235b8ba871bSPeter Wemm if (0) {
236b8ba871bSPeter Wemm err: rval = 1;
237b8ba871bSPeter Wemm inc_err(sp, nret);
238b8ba871bSPeter Wemm }
239b8ba871bSPeter Wemm if (bp != NULL)
240f0957ccaSPeter Wemm FREE_SPACEW(sp, bp, blen);
241b8ba871bSPeter Wemm return (rval);
242b8ba871bSPeter Wemm }
243b8ba871bSPeter Wemm
244b8ba871bSPeter Wemm static void
inc_err(SCR * sp,enum nresult nret)245f0957ccaSPeter Wemm inc_err(SCR *sp, enum nresult nret)
246b8ba871bSPeter Wemm {
247b8ba871bSPeter Wemm switch (nret) {
248b8ba871bSPeter Wemm case NUM_ERR:
249b8ba871bSPeter Wemm break;
250b8ba871bSPeter Wemm case NUM_OK:
251b8ba871bSPeter Wemm abort();
252b8ba871bSPeter Wemm /* NOREACHED */
253b8ba871bSPeter Wemm case NUM_OVER:
254b8ba871bSPeter Wemm msgq(sp, M_ERR, "182|Resulting number too large");
255b8ba871bSPeter Wemm break;
256b8ba871bSPeter Wemm case NUM_UNDER:
257b8ba871bSPeter Wemm msgq(sp, M_ERR, "183|Resulting number too small");
258b8ba871bSPeter Wemm break;
259b8ba871bSPeter Wemm }
260b8ba871bSPeter Wemm }
261