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