xref: /freebsd/contrib/nvi/vi/v_search.c (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
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 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "../common/common.h"
25 #include "vi.h"
26 
27 static int v_exaddr(SCR *, VICMD *, dir_t);
28 static int v_search(SCR *, VICMD *, CHAR_T *, size_t, u_int, dir_t);
29 
30 /*
31  * v_srch -- [count]?RE[? offset]
32  *	Ex address search backward.
33  *
34  * PUBLIC: int v_searchb(SCR *, VICMD *);
35  */
36 int
37 v_searchb(SCR *sp, VICMD *vp)
38 {
39 	return (v_exaddr(sp, vp, BACKWARD));
40 }
41 
42 /*
43  * v_searchf -- [count]/RE[/ offset]
44  *	Ex address search forward.
45  *
46  * PUBLIC: int v_searchf(SCR *, VICMD *);
47  */
48 int
49 v_searchf(SCR *sp, VICMD *vp)
50 {
51 	return (v_exaddr(sp, vp, FORWARD));
52 }
53 
54 /*
55  * v_exaddr --
56  *	Do a vi search (which is really an ex address).
57  */
58 static int
59 v_exaddr(SCR *sp, VICMD *vp, dir_t dir)
60 {
61 	static EXCMDLIST fake = { L("search") };
62 	EXCMD *cmdp;
63 	GS *gp;
64 	TEXT *tp;
65 	recno_t s_lno;
66 	size_t len, s_cno, tlen;
67 	int err, nb, type;
68 	char buf[20];
69 	CHAR_T *cmd, *t;
70 	CHAR_T *w;
71 	size_t wlen;
72 
73 	/*
74 	 * !!!
75 	 * If using the search command as a motion, any addressing components
76 	 * are lost, i.e. y/ptrn/+2, when repeated, is the same as y/ptrn/.
77 	 */
78 	if (F_ISSET(vp, VC_ISDOT))
79 		return (v_search(sp, vp,
80 		    NULL, 0, SEARCH_PARSE | SEARCH_MSG | SEARCH_SET, dir));
81 
82 	/* Get the search pattern. */
83 	if (v_tcmd(sp, vp, dir == BACKWARD ? CH_BSEARCH : CH_FSEARCH,
84 	    TXT_BS | TXT_CR | TXT_ESCAPE | TXT_PROMPT |
85 	    (O_ISSET(sp, O_SEARCHINCR) ? TXT_SEARCHINCR : 0)))
86 		return (1);
87 
88 	tp = TAILQ_FIRST(sp->tiq);
89 
90 	/* If the user backspaced over the prompt, do nothing. */
91 	if (tp->term == TERM_BS)
92 		return (1);
93 
94 	/*
95 	 * If the user was doing an incremental search, then we've already
96 	 * updated the cursor and moved to the right location.  Return the
97 	 * correct values, we're done.
98 	 */
99 	if (tp->term == TERM_SEARCH) {
100 		vp->m_stop.lno = sp->lno;
101 		vp->m_stop.cno = sp->cno;
102 		if (ISMOTION(vp))
103 			return (v_correct(sp, vp, 0));
104 		vp->m_final = vp->m_stop;
105 		return (0);
106 	}
107 
108 	/*
109 	 * If the user entered <escape> or <carriage-return>, the length is
110 	 * 1 and the right thing will happen, i.e. the prompt will be used
111 	 * as a command character.
112 	 *
113 	 * Build a fake ex command structure.
114 	 */
115 	gp = sp->gp;
116 	gp->excmd.cp = tp->lb;
117 	gp->excmd.clen = tp->len;
118 	F_INIT(&gp->excmd, E_VISEARCH);
119 
120 	/*
121 	 * XXX
122 	 * Warn if the search wraps.  This is a pretty special case, but it's
123 	 * nice feature that wasn't in the original implementations of ex/vi.
124 	 * (It was added at some point to System V's version.)  This message
125 	 * is only displayed if there are no keys in the queue. The problem is
126 	 * the command is going to succeed, and the message is informational,
127 	 * not an error.  If a macro displays it repeatedly, e.g., the pattern
128 	 * only occurs once in the file and wrapscan is set, you lose big.  For
129 	 * example, if the macro does something like:
130 	 *
131 	 *	:map K /pattern/^MjK
132 	 *
133 	 * Each search will display the message, but the following "/pattern/"
134 	 * will immediately overwrite it, with strange results.  The System V
135 	 * vi displays the "wrapped" message multiple times, but because it's
136 	 * overwritten each time, it's not as noticeable.  As we don't discard
137 	 * messages, it's a real problem for us.
138 	 */
139 	if (!KEYS_WAITING(sp))
140 		F_SET(&gp->excmd, E_SEARCH_WMSG);
141 
142 	/* Save the current line/column. */
143 	s_lno = sp->lno;
144 	s_cno = sp->cno;
145 
146 	/*
147 	 * !!!
148 	 * Historically, vi / and ? commands were full-blown ex addresses,
149 	 * including ';' delimiters, trailing <blank>'s, multiple search
150 	 * strings (separated by semi-colons) and, finally, full-blown z
151 	 * commands after the / and ? search strings.  (If the search was
152 	 * being used as a motion, the trailing z command was ignored.
153 	 * Also, we do some argument checking on the z command, to be sure
154 	 * that it's not some other random command.) For multiple search
155 	 * strings, leading <blank>'s at the second and subsequent strings
156 	 * were eaten as well.  This has some (unintended?) side-effects:
157 	 * the command /ptrn/;3 is legal and results in moving to line 3.
158 	 * I suppose you could use it to optionally move to line 3...
159 	 *
160 	 * !!!
161 	 * Historically, if any part of the search command failed, the cursor
162 	 * remained unmodified (even if ; was used).  We have to play games
163 	 * because the underlying ex parser thinks we're modifying the cursor
164 	 * as we go, but I think we're compatible with historic practice.
165 	 *
166 	 * !!!
167 	 * Historically, the command "/STRING/;   " failed, apparently it
168 	 * confused the parser.  We're not that compatible.
169 	 */
170 	cmdp = &gp->excmd;
171 	if (ex_range(sp, cmdp, &err))
172 		return (1);
173 
174 	/*
175 	 * Remember where any remaining command information is, and clean
176 	 * up the fake ex command.
177 	 */
178 	cmd = cmdp->cp;
179 	len = cmdp->clen;
180 	gp->excmd.clen = 0;
181 
182 	if (err)
183 		goto err2;
184 
185 	/* Copy out the new cursor position and make sure it's okay. */
186 	switch (cmdp->addrcnt) {
187 	case 1:
188 		vp->m_stop = cmdp->addr1;
189 		break;
190 	case 2:
191 		vp->m_stop = cmdp->addr2;
192 		break;
193 	}
194 	if (!db_exist(sp, vp->m_stop.lno)) {
195 		ex_badaddr(sp, &fake,
196 		    vp->m_stop.lno == 0 ? A_ZERO : A_EOF, NUM_OK);
197 		goto err2;
198 	}
199 
200 	/*
201 	 * !!!
202 	 * Historic practice is that a trailing 'z' was ignored if it was a
203 	 * motion command.  Should probably be an error, but not worth the
204 	 * effort.
205 	 */
206 	if (ISMOTION(vp))
207 		return (v_correct(sp, vp, F_ISSET(cmdp, E_DELTA)));
208 
209 	/*
210 	 * !!!
211 	 * Historically, if it wasn't a motion command, a delta in the search
212 	 * pattern turns it into a first nonblank movement.
213 	 */
214 	nb = F_ISSET(cmdp, E_DELTA);
215 
216 	/* Check for the 'z' command. */
217 	if (len != 0) {
218 		if (*cmd != 'z')
219 			goto err1;
220 
221 		/* No blanks, just like the z command. */
222 		for (t = cmd + 1, tlen = len - 1; tlen > 0; ++t, --tlen)
223 			if (!isdigit(*t))
224 				break;
225 		if (tlen &&
226 		    (*t == '-' || *t == '.' || *t == '+' || *t == '^')) {
227 			++t;
228 			--tlen;
229 			type = 1;
230 		} else
231 			type = 0;
232 		if (tlen)
233 			goto err1;
234 
235 		/* The z command will do the nonblank for us. */
236 		nb = 0;
237 
238 		/* Default to z+. */
239 		if (!type &&
240 		    v_event_push(sp, NULL, L("+"), 1, CH_NOMAP | CH_QUOTED))
241 			return (1);
242 
243 		/* Push the user's command. */
244 		if (v_event_push(sp, NULL, cmd, len, CH_NOMAP | CH_QUOTED))
245 			return (1);
246 
247 		/* Push line number so get correct z display. */
248 		tlen = snprintf(buf,
249 		    sizeof(buf), "%lu", (u_long)vp->m_stop.lno);
250 		CHAR2INT(sp, buf, tlen, w, wlen);
251 		if (v_event_push(sp, NULL, w, wlen, CH_NOMAP | CH_QUOTED))
252 			return (1);
253 
254 		/* Don't refresh until after 'z' happens. */
255 		F_SET(VIP(sp), VIP_S_REFRESH);
256 	}
257 
258 	/* Non-motion commands move to the end of the range. */
259 	vp->m_final = vp->m_stop;
260 	if (nb) {
261 		F_CLR(vp, VM_RCM_MASK);
262 		F_SET(vp, VM_RCM_SETFNB);
263 	}
264 	return (0);
265 
266 err1:	msgq(sp, M_ERR,
267 	    "188|Characters after search string, line offset and/or z command");
268 err2:	vp->m_final.lno = s_lno;
269 	vp->m_final.cno = s_cno;
270 	return (1);
271 }
272 
273 /*
274  * v_searchN -- N
275  *	Reverse last search.
276  *
277  * PUBLIC: int v_searchN(SCR *, VICMD *);
278  */
279 int
280 v_searchN(SCR *sp, VICMD *vp)
281 {
282 	dir_t dir;
283 
284 	switch (sp->searchdir) {
285 	case BACKWARD:
286 		dir = FORWARD;
287 		break;
288 	case FORWARD:
289 		dir = BACKWARD;
290 		break;
291 	default:
292 		dir = sp->searchdir;
293 		break;
294 	}
295 	return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, dir));
296 }
297 
298 /*
299  * v_searchn -- n
300  *	Repeat last search.
301  *
302  * PUBLIC: int v_searchn(SCR *, VICMD *);
303  */
304 int
305 v_searchn(SCR *sp, VICMD *vp)
306 {
307 	return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, sp->searchdir));
308 }
309 
310 /*
311  * is_special --
312  *	Test if the character is special in a basic RE.
313  */
314 static int
315 is_special(CHAR_T c)
316 {
317 	/*
318 	 * !!!
319 	 * `*' and `$' are ordinary when appear at the beginning of a RE,
320 	 * but it's safe to distinguish them from the ordinary characters.
321 	 * The tilde is vi-specific, of course.
322 	 */
323 	return (STRCHR(L(".[*\\^$~"), c) && c);
324 }
325 
326 /*
327  * Rear delimiter for word search when the keyword ends in
328  * (i.e., consists of) a non-word character.  See v_searchw below.
329  */
330 #define RE_NWSTOP	L("([^[:alnum:]_]|$)")
331 #define RE_NWSTOP_LEN	(SIZE(RE_NWSTOP) - 1)
332 
333 /*
334  * v_searchw -- [count]^A
335  *	Search for the word under the cursor.
336  *
337  * PUBLIC: int v_searchw(SCR *, VICMD *);
338  */
339 int
340 v_searchw(SCR *sp, VICMD *vp)
341 {
342 	size_t blen, len;
343 	int rval;
344 	CHAR_T *bp, *p;
345 
346 	/* An upper bound for the SIZE of the RE under construction. */
347 	len = VIP(sp)->klen + MAX(RE_WSTART_LEN, 1)
348 	    + MAX(RE_WSTOP_LEN, RE_NWSTOP_LEN);
349 	GET_SPACE_RETW(sp, bp, blen, len);
350 	p = bp;
351 
352 	/* Only the first character can be non-word, see v_curword. */
353 	if (inword(VIP(sp)->keyw[0])) {
354 		MEMCPY(p, RE_WSTART, RE_WSTART_LEN);
355 		p += RE_WSTART_LEN;
356 	} else if (is_special(VIP(sp)->keyw[0])) {
357 		MEMCPY(p, L("\\"), 1);
358 		p += 1;
359 	}
360 
361 	MEMCPY(p, VIP(sp)->keyw, VIP(sp)->klen);
362 	p += VIP(sp)->klen;
363 
364 	if (inword(p[-1])) {
365 		MEMCPY(p, RE_WSTOP, RE_WSTOP_LEN);
366 		p += RE_WSTOP_LEN;
367 	} else {
368 		/*
369 		 * The keyword is a single non-word character.
370 		 * We want it to stay the same when typing ^A several times
371 		 * in a row, just the way the other cases behave.
372 		 */
373 		MEMCPY(p, RE_NWSTOP, RE_NWSTOP_LEN);
374 		p += RE_NWSTOP_LEN;
375 	}
376 
377 	len = p - bp;
378 	rval = v_search(sp, vp, bp, len, SEARCH_SET, FORWARD);
379 
380 	FREE_SPACEW(sp, bp, blen);
381 	return (rval);
382 }
383 
384 /*
385  * v_search --
386  *	The search commands.
387  */
388 static int
389 v_search(SCR *sp, VICMD *vp, CHAR_T *ptrn, size_t plen, u_int flags, dir_t dir)
390 {
391 	/* Display messages. */
392 	LF_SET(SEARCH_MSG);
393 
394 	/* If it's a motion search, offset past end-of-line is okay. */
395 	if (ISMOTION(vp))
396 		LF_SET(SEARCH_EOL);
397 
398 	/*
399 	 * XXX
400 	 * Warn if the search wraps.  See the comment above, in v_exaddr().
401 	 */
402 	if (!KEYS_WAITING(sp))
403 		LF_SET(SEARCH_WMSG);
404 
405 	switch (dir) {
406 	case BACKWARD:
407 		if (b_search(sp,
408 		    &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
409 			return (1);
410 		break;
411 	case FORWARD:
412 		if (f_search(sp,
413 		    &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
414 			return (1);
415 		break;
416 	case NOTSET:
417 		msgq(sp, M_ERR, "189|No previous search pattern");
418 		return (1);
419 	default:
420 		abort();
421 	}
422 
423 	/* Correct motion commands, otherwise, simply move to the location. */
424 	if (ISMOTION(vp)) {
425 		if (v_correct(sp, vp, 0))
426 			return(1);
427 	} else
428 		vp->m_final = vp->m_stop;
429 	return (0);
430 }
431 
432 /*
433  * v_correct --
434  *	Handle command with a search as the motion.
435  *
436  * !!!
437  * Historically, commands didn't affect the line searched to/from if the
438  * motion command was a search and the final position was the start/end
439  * of the line.  There were some special cases and vi was not consistent;
440  * it was fairly easy to confuse it.  For example, given the two lines:
441  *
442  *	abcdefghi
443  *	ABCDEFGHI
444  *
445  * placing the cursor on the 'A' and doing y?$ would so confuse it that 'h'
446  * 'k' and put would no longer work correctly.  In any case, we try to do
447  * the right thing, but it's not going to exactly match historic practice.
448  *
449  * PUBLIC: int v_correct(SCR *, VICMD *, int);
450  */
451 int
452 v_correct(SCR *sp, VICMD *vp, int isdelta)
453 {
454 	dir_t dir;
455 	MARK m;
456 	size_t len;
457 
458 	/*
459 	 * !!!
460 	 * We may have wrapped if wrapscan was set, and we may have returned
461 	 * to the position where the cursor started.  Historic vi didn't cope
462 	 * with this well.  Yank wouldn't beep, but the first put after the
463 	 * yank would move the cursor right one column (without adding any
464 	 * text) and the second would put a copy of the current line.  The
465 	 * change and delete commands would beep, but would leave the cursor
466 	 * on the colon command line.  I believe that there are macros that
467 	 * depend on delete, at least, failing.  For now, commands that use
468 	 * search as a motion component fail when the search returns to the
469 	 * original cursor position.
470 	 */
471 	if (vp->m_start.lno == vp->m_stop.lno &&
472 	    vp->m_start.cno == vp->m_stop.cno) {
473 		msgq(sp, M_BERR, "190|Search wrapped to original position");
474 		return (1);
475 	}
476 
477 	/*
478 	 * !!!
479 	 * Searches become line mode operations if there was a delta specified
480 	 * to the search pattern.
481 	 */
482 	if (isdelta)
483 		F_SET(vp, VM_LMODE);
484 
485 	/*
486 	 * If the motion is in the reverse direction, switch the start and
487 	 * stop MARK's so that it's in a forward direction.  (There's no
488 	 * reason for this other than to make the tests below easier.  The
489 	 * code in vi.c:vi() would have done the switch.)  Both forward
490 	 * and backward motions can happen for any kind of search command
491 	 * because of the wrapscan option.
492 	 */
493 	if (vp->m_start.lno > vp->m_stop.lno ||
494 	    (vp->m_start.lno == vp->m_stop.lno &&
495 	    vp->m_start.cno > vp->m_stop.cno)) {
496 		m = vp->m_start;
497 		vp->m_start = vp->m_stop;
498 		vp->m_stop = m;
499 		dir = BACKWARD;
500 	} else
501 		dir = FORWARD;
502 
503 	/*
504 	 * BACKWARD:
505 	 *	Delete and yank commands move to the end of the range.
506 	 *	Ignore others.
507 	 *
508 	 * FORWARD:
509 	 *	Delete and yank commands don't move.  Ignore others.
510 	 */
511 	vp->m_final = vp->m_start;
512 
513 	/*
514 	 * !!!
515 	 * Delta'd searches don't correct based on column positions.
516 	 */
517 	if (isdelta)
518 		return (0);
519 
520 	/*
521 	 * !!!
522 	 * Backward searches starting at column 0, and forward searches ending
523 	 * at column 0 are corrected to the last column of the previous line.
524 	 * Otherwise, adjust the starting/ending point to the character before
525 	 * the current one (this is safe because we know the search had to move
526 	 * to succeed).
527 	 *
528 	 * Searches become line mode operations if they start at the first
529 	 * nonblank and end at column 0 of another line.
530 	 */
531 	if (vp->m_start.lno < vp->m_stop.lno && vp->m_stop.cno == 0) {
532 		if (db_get(sp, --vp->m_stop.lno, DBG_FATAL, NULL, &len))
533 			return (1);
534 		vp->m_stop.cno = len ? len - 1 : 0;
535 		len = 0;
536 		if (nonblank(sp, vp->m_start.lno, &len))
537 			return (1);
538 		if (vp->m_start.cno <= len)
539 			F_SET(vp, VM_LMODE);
540 	} else
541 		--vp->m_stop.cno;
542 
543 	return (0);
544 }
545