xref: /minix3/external/bsd/nvi/dist/ex/ex_join.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_join.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: ex_join.c,v 10.17 2004/03/16 14:14:04 skimo Exp  (Berkeley) Date: 2004/03/16 14:14:04 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_join.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <ctype.h>
2784d9c625SLionel Sambuc #include <limits.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <stdlib.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc #include "../common/common.h"
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * ex_join -- :[line [,line]] j[oin][!] [count] [flags]
3684d9c625SLionel Sambuc  *	Join lines.
3784d9c625SLionel Sambuc  *
3884d9c625SLionel Sambuc  * PUBLIC: int ex_join __P((SCR *, EXCMD *));
3984d9c625SLionel Sambuc  */
4084d9c625SLionel Sambuc int
ex_join(SCR * sp,EXCMD * cmdp)4184d9c625SLionel Sambuc ex_join(SCR *sp, EXCMD *cmdp)
4284d9c625SLionel Sambuc {
4384d9c625SLionel Sambuc 	db_recno_t from, to;
4484d9c625SLionel Sambuc 	size_t blen, clen, len, tlen;
4584d9c625SLionel Sambuc 	int extra, first;
4684d9c625SLionel Sambuc 	ARG_CHAR_T echar = 0;
4784d9c625SLionel Sambuc 	CHAR_T *p, *bp, *tbp = NULL;
4884d9c625SLionel Sambuc 
4984d9c625SLionel Sambuc 	NEEDFILE(sp, cmdp);
5084d9c625SLionel Sambuc 
5184d9c625SLionel Sambuc 	from = cmdp->addr1.lno;
5284d9c625SLionel Sambuc 	to = cmdp->addr2.lno;
5384d9c625SLionel Sambuc 
5484d9c625SLionel Sambuc 	/* Check for no lines to join. */
5584d9c625SLionel Sambuc 	if (!db_exist(sp, from + 1)) {
5684d9c625SLionel Sambuc 		msgq(sp, M_ERR, "131|No following lines to join");
5784d9c625SLionel Sambuc 		return (1);
5884d9c625SLionel Sambuc 	}
5984d9c625SLionel Sambuc 
6084d9c625SLionel Sambuc 	GET_SPACE_RETW(sp, bp, blen, 256);
6184d9c625SLionel Sambuc 
6284d9c625SLionel Sambuc 	/*
6384d9c625SLionel Sambuc 	 * The count for the join command was off-by-one,
6484d9c625SLionel Sambuc 	 * historically, to other counts for other commands.
6584d9c625SLionel Sambuc 	 */
6684d9c625SLionel Sambuc 	if (F_ISSET(cmdp, E_ADDR_DEF) || cmdp->addrcnt == 1)
6784d9c625SLionel Sambuc 		++cmdp->addr2.lno;
6884d9c625SLionel Sambuc 
6984d9c625SLionel Sambuc 	clen = tlen = 0;
7084d9c625SLionel Sambuc         for (first = 1,
7184d9c625SLionel Sambuc 	    from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) {
7284d9c625SLionel Sambuc 		/*
7384d9c625SLionel Sambuc 		 * Get next line.  Historic versions of vi allowed "10J" while
7484d9c625SLionel Sambuc 		 * less than 10 lines from the end-of-file, so we do too.
7584d9c625SLionel Sambuc 		 */
7684d9c625SLionel Sambuc 		if (db_get(sp, from, 0, &p, &len)) {
7784d9c625SLionel Sambuc 			cmdp->addr2.lno = from - 1;
7884d9c625SLionel Sambuc 			break;
7984d9c625SLionel Sambuc 		}
8084d9c625SLionel Sambuc 
8184d9c625SLionel Sambuc 		/* Empty lines just go away. */
8284d9c625SLionel Sambuc 		if (len == 0)
8384d9c625SLionel Sambuc 			continue;
8484d9c625SLionel Sambuc 
8584d9c625SLionel Sambuc 		/*
8684d9c625SLionel Sambuc 		 * Get more space if necessary.  Note, tlen isn't the length
8784d9c625SLionel Sambuc 		 * of the new line, it's roughly the amount of space needed.
8884d9c625SLionel Sambuc 		 * tbp - bp is the length of the new line.
8984d9c625SLionel Sambuc 		 */
9084d9c625SLionel Sambuc 		tlen += len + 2;
9184d9c625SLionel Sambuc 		ADD_SPACE_RETW(sp, bp, blen, tlen);
9284d9c625SLionel Sambuc 		tbp = bp + clen;
9384d9c625SLionel Sambuc 
9484d9c625SLionel Sambuc 		/*
9584d9c625SLionel Sambuc 		 * Historic practice:
9684d9c625SLionel Sambuc 		 *
9784d9c625SLionel Sambuc 		 * If force specified, join without modification.
9884d9c625SLionel Sambuc 		 * If the current line ends with whitespace, strip leading
9984d9c625SLionel Sambuc 		 *    whitespace from the joined line.
10084d9c625SLionel Sambuc 		 * If the next line starts with a ), do nothing.
10184d9c625SLionel Sambuc 		 * If the current line ends with ., insert two spaces.
10284d9c625SLionel Sambuc 		 * Else, insert one space.
10384d9c625SLionel Sambuc 		 *
10484d9c625SLionel Sambuc 		 * One change -- add ? and ! to the list of characters for
10584d9c625SLionel Sambuc 		 * which we insert two spaces.  I expect that POSIX 1003.2
10684d9c625SLionel Sambuc 		 * will require this as well.
10784d9c625SLionel Sambuc 		 *
10884d9c625SLionel Sambuc 		 * Echar is the last character in the last line joined.
10984d9c625SLionel Sambuc 		 */
11084d9c625SLionel Sambuc 		extra = 0;
11184d9c625SLionel Sambuc 		if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) {
11284d9c625SLionel Sambuc 			if (ISBLANK(echar))
11384d9c625SLionel Sambuc 				for (; len && ISBLANK((UCHAR_T)*p); --len, ++p);
11484d9c625SLionel Sambuc 			else if (p[0] != ')') {
11584d9c625SLionel Sambuc 				if (STRCHR(L(".?!"), echar)) {
11684d9c625SLionel Sambuc 					*tbp++ = ' ';
11784d9c625SLionel Sambuc 					++clen;
11884d9c625SLionel Sambuc 					extra = 1;
11984d9c625SLionel Sambuc 				}
12084d9c625SLionel Sambuc 				*tbp++ = ' ';
12184d9c625SLionel Sambuc 				++clen;
12284d9c625SLionel Sambuc 				for (; len && ISBLANK((UCHAR_T)*p); --len, ++p);
12384d9c625SLionel Sambuc 			}
12484d9c625SLionel Sambuc 		}
12584d9c625SLionel Sambuc 
12684d9c625SLionel Sambuc 		if (len != 0) {
12784d9c625SLionel Sambuc 			MEMCPYW(tbp, p, len);
12884d9c625SLionel Sambuc 			tbp += len;
12984d9c625SLionel Sambuc 			clen += len;
13084d9c625SLionel Sambuc 			echar = p[len - 1];
13184d9c625SLionel Sambuc 		} else
13284d9c625SLionel Sambuc 			echar = ' ';
13384d9c625SLionel Sambuc 
13484d9c625SLionel Sambuc 		/*
13584d9c625SLionel Sambuc 		 * Historic practice for vi was to put the cursor at the first
13684d9c625SLionel Sambuc 		 * inserted whitespace character, if there was one, or the
13784d9c625SLionel Sambuc 		 * first character of the joined line, if there wasn't, or the
13884d9c625SLionel Sambuc 		 * last character of the line if joined to an empty line.  If
13984d9c625SLionel Sambuc 		 * a count was specified, the cursor was moved as described
14084d9c625SLionel Sambuc 		 * for the first line joined, ignoring subsequent lines.  If
14184d9c625SLionel Sambuc 		 * the join was a ':' command, the cursor was placed at the
14284d9c625SLionel Sambuc 		 * first non-blank character of the line unless the cursor was
14384d9c625SLionel Sambuc 		 * "attracted" to the end of line when the command was executed
14484d9c625SLionel Sambuc 		 * in which case it moved to the new end of line.  There are
14584d9c625SLionel Sambuc 		 * probably several more special cases, but frankly, my dear,
14684d9c625SLionel Sambuc 		 * I don't give a damn.  This implementation puts the cursor
14784d9c625SLionel Sambuc 		 * on the first inserted whitespace character, the first
14884d9c625SLionel Sambuc 		 * character of the joined line, or the last character of the
14984d9c625SLionel Sambuc 		 * line regardless.  Note, if the cursor isn't on the joined
15084d9c625SLionel Sambuc 		 * line (possible with : commands), it is reset to the starting
15184d9c625SLionel Sambuc 		 * line.
15284d9c625SLionel Sambuc 		 */
15384d9c625SLionel Sambuc 		if (first) {
15484d9c625SLionel Sambuc 			sp->cno = (tbp - bp) - (1 + extra);
15584d9c625SLionel Sambuc 			first = 0;
15684d9c625SLionel Sambuc 		} else
15784d9c625SLionel Sambuc 			sp->cno = (tbp - bp) - len - (1 + extra);
15884d9c625SLionel Sambuc 	}
15984d9c625SLionel Sambuc 	sp->lno = cmdp->addr1.lno;
16084d9c625SLionel Sambuc 
16184d9c625SLionel Sambuc 	/* Delete the joined lines. */
16284d9c625SLionel Sambuc         for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to)
16384d9c625SLionel Sambuc 		if (db_delete(sp, to))
16484d9c625SLionel Sambuc 			goto err;
16584d9c625SLionel Sambuc 
16684d9c625SLionel Sambuc 	/* If the original line changed, reset it. */
16784d9c625SLionel Sambuc 	if (!first && db_set(sp, from, bp, tbp - bp)) {
16884d9c625SLionel Sambuc err:		FREE_SPACEW(sp, bp, blen);
16984d9c625SLionel Sambuc 		return (1);
17084d9c625SLionel Sambuc 	}
17184d9c625SLionel Sambuc 	FREE_SPACEW(sp, bp, blen);
17284d9c625SLionel Sambuc 
17384d9c625SLionel Sambuc 	sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1;
17484d9c625SLionel Sambuc 	return (0);
17584d9c625SLionel Sambuc }
176