Remove building with NOCRYPTO option
[minix3.git] / external / bsd / nvi / dist / ex / ex_args.c
blob6d3b2c4a7e4e861c4e4caf36ed54fd3bdec4c728
1 /* $NetBSD: ex_args.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3 * Copyright (c) 1991, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1991, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #include <sys/cdefs.h>
14 #if 0
15 #ifndef lint
16 static const char sccsid[] = "Id: ex_args.c,v 10.18 2001/06/25 15:19:14 skimo Exp (Berkeley) Date: 2001/06/25 15:19:14 ";
17 #endif /* not lint */
18 #else
19 __RCSID("$NetBSD: ex_args.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
20 #endif
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 #include <sys/time.h>
26 #include <bitstring.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include "../common/common.h"
34 #include "../vi/vi.h"
36 static int ex_N_next __P((SCR *, EXCMD *));
39 * ex_next -- :next [+cmd] [files]
40 * Edit the next file, optionally setting the list of files.
42 * !!!
43 * The :next command behaved differently from the :rewind command in
44 * historic vi. See nvi/docs/autowrite for details, but the basic
45 * idea was that it ignored the force flag if the autowrite flag was
46 * set. This implementation handles them all identically.
48 * PUBLIC: int ex_next __P((SCR *, EXCMD *));
50 int
51 ex_next(SCR *sp, EXCMD *cmdp)
53 ARGS **argv;
54 FREF *frp;
55 int noargs;
56 char **ap;
57 const CHAR_T *wp;
58 size_t wlen;
59 const char *np;
60 size_t nlen;
62 /* Check for file to move to. */
63 if (cmdp->argc == 0 && (sp->cargv == NULL || sp->cargv[1] == NULL)) {
64 msgq(sp, M_ERR, "111|No more files to edit");
65 return (1);
68 if (F_ISSET(cmdp, E_NEWSCREEN)) {
69 /* By default, edit the next file in the old argument list. */
70 if (cmdp->argc == 0) {
71 CHAR2INT(sp, sp->cargv[1], strlen(sp->cargv[1]) + 1,
72 wp, wlen);
73 if (argv_exp0(sp, cmdp, wp, wlen - 1))
74 return (1);
75 return (ex_edit(sp, cmdp));
77 return (ex_N_next(sp, cmdp));
80 /* Check modification. */
81 if (file_m1(sp,
82 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
83 return (1);
85 /* Any arguments are a replacement file list. */
86 if (cmdp->argc) {
87 /* Free the current list. */
88 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
89 for (ap = sp->argv; *ap != NULL; ++ap)
90 free(*ap);
91 free(sp->argv);
93 F_CLR(sp, SC_ARGNOFREE | SC_ARGRECOVER);
94 sp->cargv = NULL;
96 /* Create a new list. */
97 CALLOC_RET(sp,
98 sp->argv, char **, cmdp->argc + 1, sizeof(char *));
99 for (ap = sp->argv,
100 argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
101 INT2CHAR(sp, argv[0]->bp, argv[0]->len, np, nlen);
102 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
103 return (1);
105 *ap = NULL;
107 /* Switch to the first file. */
108 sp->cargv = sp->argv;
109 if ((frp = file_add(sp, *sp->cargv)) == NULL)
110 return (1);
111 noargs = 0;
113 /* Display a file count with the welcome message. */
114 F_SET(sp, SC_STATUS_CNT);
115 } else {
116 if ((frp = file_add(sp, sp->cargv[1])) == NULL)
117 return (1);
118 if (F_ISSET(sp, SC_ARGRECOVER))
119 F_SET(frp, FR_RECOVER);
120 noargs = 1;
123 if (file_init(sp, frp, NULL, FS_SETALT |
124 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
125 return (1);
126 if (noargs)
127 ++sp->cargv;
129 F_SET(sp, SC_FSWITCH);
130 return (0);
134 * ex_N_next --
135 * New screen version of ex_next.
137 static int
138 ex_N_next(SCR *sp, EXCMD *cmdp)
140 SCR *new;
141 FREF *frp;
142 const char *np;
143 size_t nlen;
145 /* Get a new screen. */
146 if (screen_init(sp->gp, sp, &new))
147 return (1);
148 if (vs_split(sp, new, 0)) {
149 (void)screen_fini(new);
150 return (1);
153 /* Get a backing file. */
154 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, np, nlen);
155 if ((frp = file_add(new, np)) == NULL ||
156 file_init(new, frp, NULL,
157 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
158 (void)vs_discard(new, NULL);
159 (void)screen_end(new);
160 return (1);
163 /* The arguments are a replacement file list. */
164 new->cargv = new->argv = ex_buildargv(sp, cmdp, NULL);
166 /* Display a file count with the welcome message. */
167 F_SET(new, SC_STATUS_CNT);
169 /* Set up the switch. */
170 sp->nextdisp = new;
171 F_SET(sp, SC_SSWITCH);
173 return (0);
177 * ex_prev -- :prev
178 * Edit the previous file.
180 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
183 ex_prev(SCR *sp, EXCMD *cmdp)
185 FREF *frp;
186 size_t wlen;
187 const CHAR_T *wp;
189 if (sp->cargv == sp->argv) {
190 msgq(sp, M_ERR, "112|No previous files to edit");
191 return (1);
194 if (F_ISSET(cmdp, E_NEWSCREEN)) {
195 CHAR2INT(sp, sp->cargv[-1], strlen(sp->cargv[-1]) + 1,
196 wp, wlen);
197 if (argv_exp0(sp, cmdp, wp, wlen - 1))
198 return (1);
199 return (ex_edit(sp, cmdp));
202 if (file_m1(sp,
203 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
204 return (1);
206 if ((frp = file_add(sp, sp->cargv[-1])) == NULL)
207 return (1);
209 if (file_init(sp, frp, NULL, FS_SETALT |
210 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
211 return (1);
212 --sp->cargv;
214 F_SET(sp, SC_FSWITCH);
215 return (0);
219 * ex_rew -- :rew
220 * Re-edit the list of files.
222 * !!!
223 * Historic practice was that all files would start editing at the beginning
224 * of the file. We don't get this right because we may have multiple screens
225 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
226 * anyone noticing, but if they do, we'll have to put information into the SCR
227 * structure so we can keep track of it.
229 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
232 ex_rew(SCR *sp, EXCMD *cmdp)
234 FREF *frp;
237 * !!!
238 * Historic practice -- you can rewind to the current file.
240 if (sp->argv == NULL) {
241 msgq(sp, M_ERR, "113|No previous files to rewind");
242 return (1);
245 if (file_m1(sp,
246 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
247 return (1);
249 /* Switch to the first one. */
250 sp->cargv = sp->argv;
251 if ((frp = file_add(sp, *sp->cargv)) == NULL)
252 return (1);
253 if (file_init(sp, frp, NULL, FS_SETALT |
254 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
255 return (1);
257 /* Switch and display a file count with the welcome message. */
258 F_SET(sp, SC_FSWITCH | SC_STATUS_CNT);
260 return (0);
264 * ex_args -- :args
265 * Display the list of files.
267 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
270 ex_args(SCR *sp, EXCMD *cmdp)
272 int cnt, sep;
273 size_t col, len;
274 char **ap;
276 if (sp->argv == NULL) {
277 (void)msgq(sp, M_ERR, "114|No file list to display");
278 return (0);
281 col = len = sep = 0;
282 for (cnt = 1, ap = sp->argv; *ap != NULL; ++ap) {
283 col += len = strlen(*ap) + sep + (ap == sp->cargv ? 2 : 0);
284 if (col >= sp->cols - 1) {
285 col = len;
286 sep = 0;
287 (void)ex_puts(sp, "\n");
288 } else if (cnt != 1) {
289 sep = 1;
290 (void)ex_puts(sp, " ");
292 ++cnt;
294 (void)ex_printf(sp, "%s%s%s", ap == sp->cargv ? "[" : "",
295 *ap, ap == sp->cargv ? "]" : "");
296 if (INTERRUPTED(sp))
297 break;
299 (void)ex_puts(sp, "\n");
300 return (0);
304 * ex_buildargv --
305 * Build a new file argument list.
307 * PUBLIC: char **ex_buildargv __P((SCR *, EXCMD *, char *));
309 char **
310 ex_buildargv(SCR *sp, EXCMD *cmdp, char *name)
312 ARGS **argv;
313 int argc;
314 char **ap, **s_argv;
315 const char *np;
316 size_t nlen;
318 argc = cmdp == NULL ? 1 : cmdp->argc;
319 CALLOC(sp, s_argv, char **, argc + 1, sizeof(char *));
320 if ((ap = s_argv) == NULL)
321 return (NULL);
323 if (cmdp == NULL) {
324 if ((*ap = v_strdup(sp, name, strlen(name))) == NULL)
325 return (NULL);
326 ++ap;
327 } else
328 for (argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
329 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len,
330 np, nlen);
331 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
332 return (NULL);
334 *ap = NULL;
335 return (s_argv);