1*0a6a1f1dSLionel Sambuc /* $NetBSD: v_util.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: v_util.c,v 10.14 2001/06/25 15:19:36 skimo Exp (Berkeley) Date: 2001/06/25 15:19:36 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: v_util.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 #include <sys/time.h>
2584d9c625SLionel Sambuc
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <ctype.h>
2884d9c625SLionel Sambuc #include <limits.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <stdlib.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc #include "vi.h"
3684d9c625SLionel Sambuc
3784d9c625SLionel Sambuc /*
3884d9c625SLionel Sambuc * v_eof --
3984d9c625SLionel Sambuc * Vi end-of-file error.
4084d9c625SLionel Sambuc *
4184d9c625SLionel Sambuc * PUBLIC: void v_eof __P((SCR *, MARK *));
4284d9c625SLionel Sambuc */
4384d9c625SLionel Sambuc void
v_eof(SCR * sp,MARK * mp)4484d9c625SLionel Sambuc v_eof(SCR *sp, MARK *mp)
4584d9c625SLionel Sambuc {
4684d9c625SLionel Sambuc db_recno_t lno;
4784d9c625SLionel Sambuc
4884d9c625SLionel Sambuc if (mp == NULL)
4984d9c625SLionel Sambuc v_emsg(sp, NULL, VIM_EOF);
5084d9c625SLionel Sambuc else {
5184d9c625SLionel Sambuc if (db_last(sp, &lno))
5284d9c625SLionel Sambuc return;
5384d9c625SLionel Sambuc if (mp->lno >= lno)
5484d9c625SLionel Sambuc v_emsg(sp, NULL, VIM_EOF);
5584d9c625SLionel Sambuc else
5684d9c625SLionel Sambuc msgq(sp, M_BERR, "195|Movement past the end-of-file");
5784d9c625SLionel Sambuc }
5884d9c625SLionel Sambuc }
5984d9c625SLionel Sambuc
6084d9c625SLionel Sambuc /*
6184d9c625SLionel Sambuc * v_eol --
6284d9c625SLionel Sambuc * Vi end-of-line error.
6384d9c625SLionel Sambuc *
6484d9c625SLionel Sambuc * PUBLIC: void v_eol __P((SCR *, MARK *));
6584d9c625SLionel Sambuc */
6684d9c625SLionel Sambuc void
v_eol(SCR * sp,MARK * mp)6784d9c625SLionel Sambuc v_eol(SCR *sp, MARK *mp)
6884d9c625SLionel Sambuc {
6984d9c625SLionel Sambuc size_t len;
7084d9c625SLionel Sambuc
7184d9c625SLionel Sambuc if (mp == NULL)
7284d9c625SLionel Sambuc v_emsg(sp, NULL, VIM_EOL);
7384d9c625SLionel Sambuc else {
7484d9c625SLionel Sambuc if (db_get(sp, mp->lno, DBG_FATAL, NULL, &len))
7584d9c625SLionel Sambuc return;
7684d9c625SLionel Sambuc if (mp->cno == len - 1)
7784d9c625SLionel Sambuc v_emsg(sp, NULL, VIM_EOL);
7884d9c625SLionel Sambuc else
7984d9c625SLionel Sambuc msgq(sp, M_BERR, "196|Movement past the end-of-line");
8084d9c625SLionel Sambuc }
8184d9c625SLionel Sambuc }
8284d9c625SLionel Sambuc
8384d9c625SLionel Sambuc /*
8484d9c625SLionel Sambuc * v_nomove --
8584d9c625SLionel Sambuc * Vi no cursor movement error.
8684d9c625SLionel Sambuc *
8784d9c625SLionel Sambuc * PUBLIC: void v_nomove __P((SCR *));
8884d9c625SLionel Sambuc */
8984d9c625SLionel Sambuc void
v_nomove(SCR * sp)9084d9c625SLionel Sambuc v_nomove(SCR *sp)
9184d9c625SLionel Sambuc {
9284d9c625SLionel Sambuc msgq(sp, M_BERR, "197|No cursor movement made");
9384d9c625SLionel Sambuc }
9484d9c625SLionel Sambuc
9584d9c625SLionel Sambuc /*
9684d9c625SLionel Sambuc * v_sof --
9784d9c625SLionel Sambuc * Vi start-of-file error.
9884d9c625SLionel Sambuc *
9984d9c625SLionel Sambuc * PUBLIC: void v_sof __P((SCR *, MARK *));
10084d9c625SLionel Sambuc */
10184d9c625SLionel Sambuc void
v_sof(SCR * sp,MARK * mp)10284d9c625SLionel Sambuc v_sof(SCR *sp, MARK *mp)
10384d9c625SLionel Sambuc {
10484d9c625SLionel Sambuc if (mp == NULL || mp->lno == 1)
10584d9c625SLionel Sambuc msgq(sp, M_BERR, "198|Already at the beginning of the file");
10684d9c625SLionel Sambuc else
10784d9c625SLionel Sambuc msgq(sp, M_BERR, "199|Movement past the beginning of the file");
10884d9c625SLionel Sambuc }
10984d9c625SLionel Sambuc
11084d9c625SLionel Sambuc /*
11184d9c625SLionel Sambuc * v_sol --
11284d9c625SLionel Sambuc * Vi start-of-line error.
11384d9c625SLionel Sambuc *
11484d9c625SLionel Sambuc * PUBLIC: void v_sol __P((SCR *));
11584d9c625SLionel Sambuc */
11684d9c625SLionel Sambuc void
v_sol(SCR * sp)11784d9c625SLionel Sambuc v_sol(SCR *sp)
11884d9c625SLionel Sambuc {
11984d9c625SLionel Sambuc msgq(sp, M_BERR, "200|Already in the first column");
12084d9c625SLionel Sambuc }
12184d9c625SLionel Sambuc
12284d9c625SLionel Sambuc /*
12384d9c625SLionel Sambuc * v_isempty --
12484d9c625SLionel Sambuc * Return if the line contains nothing but white-space characters.
12584d9c625SLionel Sambuc *
12684d9c625SLionel Sambuc * PUBLIC: int v_isempty __P((CHAR_T *, size_t));
12784d9c625SLionel Sambuc */
12884d9c625SLionel Sambuc int
v_isempty(CHAR_T * p,size_t len)12984d9c625SLionel Sambuc v_isempty(CHAR_T *p, size_t len)
13084d9c625SLionel Sambuc {
13184d9c625SLionel Sambuc for (; len--; ++p)
13284d9c625SLionel Sambuc if (!ISBLANK((UCHAR_T)*p))
13384d9c625SLionel Sambuc return (0);
13484d9c625SLionel Sambuc return (1);
13584d9c625SLionel Sambuc }
13684d9c625SLionel Sambuc
13784d9c625SLionel Sambuc /*
13884d9c625SLionel Sambuc * v_emsg --
13984d9c625SLionel Sambuc * Display a few common vi messages.
14084d9c625SLionel Sambuc *
14184d9c625SLionel Sambuc * PUBLIC: void v_emsg __P((SCR *, const char *, vim_t));
14284d9c625SLionel Sambuc */
14384d9c625SLionel Sambuc void
v_emsg(SCR * sp,const char * p,vim_t which)14484d9c625SLionel Sambuc v_emsg(SCR *sp, const char *p, vim_t which)
14584d9c625SLionel Sambuc {
14684d9c625SLionel Sambuc switch (which) {
14784d9c625SLionel Sambuc case VIM_COMBUF:
14884d9c625SLionel Sambuc msgq(sp, M_ERR,
14984d9c625SLionel Sambuc "201|Buffers should be specified before the command");
15084d9c625SLionel Sambuc break;
15184d9c625SLionel Sambuc case VIM_EMPTY:
15284d9c625SLionel Sambuc msgq(sp, M_BERR, "209|The file is empty");
15384d9c625SLionel Sambuc break;
15484d9c625SLionel Sambuc case VIM_EOF:
15584d9c625SLionel Sambuc msgq(sp, M_BERR, "202|Already at end-of-file");
15684d9c625SLionel Sambuc break;
15784d9c625SLionel Sambuc case VIM_EOL:
15884d9c625SLionel Sambuc msgq(sp, M_BERR, "203|Already at end-of-line");
15984d9c625SLionel Sambuc break;
16084d9c625SLionel Sambuc case VIM_NOCOM:
16184d9c625SLionel Sambuc case VIM_NOCOM_B:
16284d9c625SLionel Sambuc msgq(sp,
16384d9c625SLionel Sambuc which == VIM_NOCOM_B ? M_BERR : M_ERR,
16484d9c625SLionel Sambuc "204|%s isn't a vi command", p);
16584d9c625SLionel Sambuc break;
16684d9c625SLionel Sambuc case VIM_WRESIZE:
16784d9c625SLionel Sambuc msgq(sp, M_ERR, "Window resize interrupted text input mode");
16884d9c625SLionel Sambuc break;
16984d9c625SLionel Sambuc case VIM_USAGE:
17084d9c625SLionel Sambuc msgq(sp, M_ERR, "205|Usage: %s", p);
17184d9c625SLionel Sambuc break;
17284d9c625SLionel Sambuc }
17384d9c625SLionel Sambuc }
174