1/*	$OpenBSD: v_util.c,v 1.8 2016/01/06 22:28:52 millert Exp $	*/
2
3/*-
4 * Copyright (c) 1992, 1993, 1994
5 *	The Regents of the University of California.  All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 *	Keith Bostic.  All rights reserved.
8 *
9 * See the LICENSE file for redistribution information.
10 */
11
12#include "config.h"
13
14#include <sys/types.h>
15#include <sys/queue.h>
16#include <sys/time.h>
17
18#include <bitstring.h>
19#include <ctype.h>
20#include <limits.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#include "../common/common.h"
27#include "vi.h"
28
29/*
30 * v_eof --
31 *	Vi end-of-file error.
32 *
33 * PUBLIC: void v_eof(SCR *, MARK *);
34 */
35void
36v_eof(SCR *sp, MARK *mp)
37{
38	recno_t lno;
39
40	if (mp == NULL)
41		v_emsg(sp, NULL, VIM_EOF);
42	else {
43		if (db_last(sp, &lno))
44			return;
45		if (mp->lno >= lno)
46			v_emsg(sp, NULL, VIM_EOF);
47		else
48			msgq(sp, M_BERR, "Movement past the end-of-file");
49	}
50}
51
52/*
53 * v_eol --
54 *	Vi end-of-line error.
55 *
56 * PUBLIC: void v_eol(SCR *, MARK *);
57 */
58void
59v_eol(SCR *sp, MARK *mp)
60{
61	size_t len;
62
63	if (mp == NULL)
64		v_emsg(sp, NULL, VIM_EOL);
65	else {
66		if (db_get(sp, mp->lno, DBG_FATAL, NULL, &len))
67			return;
68		if (mp->cno == len - 1)
69			v_emsg(sp, NULL, VIM_EOL);
70		else
71			msgq(sp, M_BERR, "Movement past the end-of-line");
72	}
73}
74
75/*
76 * v_nomove --
77 *	Vi no cursor movement error.
78 *
79 * PUBLIC: void v_nomove(SCR *);
80 */
81void
82v_nomove(SCR *sp)
83{
84	msgq(sp, M_BERR, "No cursor movement made");
85}
86
87/*
88 * v_sof --
89 *	Vi start-of-file error.
90 *
91 * PUBLIC: void v_sof(SCR *, MARK *);
92 */
93void
94v_sof(SCR *sp, MARK *mp)
95{
96	if (mp == NULL || mp->lno == 1)
97		msgq(sp, M_BERR, "Already at the beginning of the file");
98	else
99		msgq(sp, M_BERR, "Movement past the beginning of the file");
100}
101
102/*
103 * v_sol --
104 *	Vi start-of-line error.
105 *
106 * PUBLIC: void v_sol(SCR *);
107 */
108void
109v_sol(SCR *sp)
110{
111	msgq(sp, M_BERR, "Already in the first column");
112}
113
114/*
115 * v_isempty --
116 *	Return if the line contains nothing but white-space characters.
117 *
118 * PUBLIC: int v_isempty(char *, size_t);
119 */
120int
121v_isempty(char *p, size_t len)
122{
123	for (; len--; ++p)
124		if (!isblank(*p))
125			return (0);
126	return (1);
127}
128
129/*
130 * v_emsg --
131 *	Display a few common vi messages.
132 *
133 * PUBLIC: void v_emsg(SCR *, char *, vim_t);
134 */
135void
136v_emsg(SCR *sp, char *p, vim_t which)
137{
138	switch (which) {
139	case VIM_COMBUF:
140		msgq(sp, M_ERR,
141		    "Buffers should be specified before the command");
142		break;
143	case VIM_EMPTY:
144		msgq(sp, M_BERR, "The file is empty");
145		break;
146	case VIM_EOF:
147		msgq(sp, M_BERR, "Already at end-of-file");
148		break;
149	case VIM_EOL:
150		msgq(sp, M_BERR, "Already at end-of-line");
151		break;
152	case VIM_NOCOM:
153	case VIM_NOCOM_B:
154		msgq(sp,
155		    which == VIM_NOCOM_B ? M_BERR : M_ERR,
156		    "%s isn't a vi command", p);
157		break;
158	case VIM_WRESIZE:
159		msgq(sp, M_ERR, "Window resize interrupted text input mode");
160		break;
161	case VIM_USAGE:
162		msgq(sp, M_ERR, "Usage: %s", p);
163		break;
164	}
165}
166