xref: /freebsd/contrib/nvi/vi/v_util.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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 <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 
24 #include "../common/common.h"
25 #include "vi.h"
26 
27 /*
28  * v_eof --
29  *	Vi end-of-file error.
30  *
31  * PUBLIC: void v_eof(SCR *, MARK *);
32  */
33 void
34 v_eof(SCR *sp, MARK *mp)
35 {
36 	recno_t lno;
37 
38 	if (mp == NULL)
39 		v_emsg(sp, NULL, VIM_EOF);
40 	else {
41 		if (db_last(sp, &lno))
42 			return;
43 		if (mp->lno >= lno)
44 			v_emsg(sp, NULL, VIM_EOF);
45 		else
46 			msgq(sp, M_BERR, "195|Movement past the end-of-file");
47 	}
48 }
49 
50 /*
51  * v_eol --
52  *	Vi end-of-line error.
53  *
54  * PUBLIC: void v_eol(SCR *, MARK *);
55  */
56 void
57 v_eol(SCR *sp, MARK *mp)
58 {
59 	size_t len;
60 
61 	if (mp == NULL)
62 		v_emsg(sp, NULL, VIM_EOL);
63 	else {
64 		if (db_get(sp, mp->lno, DBG_FATAL, NULL, &len))
65 			return;
66 		if (mp->cno == len - 1)
67 			v_emsg(sp, NULL, VIM_EOL);
68 		else
69 			msgq(sp, M_BERR, "196|Movement past the end-of-line");
70 	}
71 }
72 
73 /*
74  * v_nomove --
75  *	Vi no cursor movement error.
76  *
77  * PUBLIC: void v_nomove(SCR *);
78  */
79 void
80 v_nomove(SCR *sp)
81 {
82 	msgq(sp, M_BERR, "197|No cursor movement made");
83 }
84 
85 /*
86  * v_sof --
87  *	Vi start-of-file error.
88  *
89  * PUBLIC: void v_sof(SCR *, MARK *);
90  */
91 void
92 v_sof(SCR *sp, MARK *mp)
93 {
94 	if (mp == NULL || mp->lno == 1)
95 		msgq(sp, M_BERR, "198|Already at the beginning of the file");
96 	else
97 		msgq(sp, M_BERR, "199|Movement past the beginning of the file");
98 }
99 
100 /*
101  * v_sol --
102  *	Vi start-of-line error.
103  *
104  * PUBLIC: void v_sol(SCR *);
105  */
106 void
107 v_sol(SCR *sp)
108 {
109 	msgq(sp, M_BERR, "200|Already in the first column");
110 }
111 
112 /*
113  * v_isempty --
114  *	Return if the line contains nothing but white-space characters.
115  *
116  * PUBLIC: int v_isempty(CHAR_T *, size_t);
117  */
118 int
119 v_isempty(CHAR_T *p, size_t len)
120 {
121 	for (; len--; ++p)
122 		if (!isblank(*p))
123 			return (0);
124 	return (1);
125 }
126 
127 /*
128  * v_emsg --
129  *	Display a few common vi messages.
130  *
131  * PUBLIC: void v_emsg(SCR *, char *, vim_t);
132  */
133 void
134 v_emsg(SCR *sp, char *p, vim_t which)
135 {
136 	switch (which) {
137 	case VIM_COMBUF:
138 		msgq(sp, M_ERR,
139 		    "201|Buffers should be specified before the command");
140 		break;
141 	case VIM_EMPTY:
142 		msgq(sp, M_BERR, "209|The file is empty");
143 		break;
144 	case VIM_EOF:
145 		msgq(sp, M_BERR, "202|Already at end-of-file");
146 		break;
147 	case VIM_EOL:
148 		msgq(sp, M_BERR, "203|Already at end-of-line");
149 		break;
150 	case VIM_NOCOM:
151 	case VIM_NOCOM_B:
152 		msgq(sp,
153 		    which == VIM_NOCOM_B ? M_BERR : M_ERR,
154 		    "204|%s isn't a vi command", p);
155 		break;
156 	case VIM_WRESIZE:
157 		msgq(sp, M_ERR, "Window resize interrupted text input mode");
158 		break;
159 	case VIM_USAGE:
160 		msgq(sp, M_ERR, "205|Usage: %s", p);
161 		break;
162 	}
163 }
164