Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_args.c
blobbae85d9dd21b78b9da2212ebf671a374b2bd31d7
1 /* $NetBSD: ex_args.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1991, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "../common/common.h"
30 #include "../vi/vi.h"
32 static int ex_N_next __P((SCR *, EXCMD *));
35 * ex_next -- :next [+cmd] [files]
36 * Edit the next file, optionally setting the list of files.
38 * !!!
39 * The :next command behaved differently from the :rewind command in
40 * historic vi. See nvi/docs/autowrite for details, but the basic
41 * idea was that it ignored the force flag if the autowrite flag was
42 * set. This implementation handles them all identically.
44 * PUBLIC: int ex_next __P((SCR *, EXCMD *));
46 int
47 ex_next(SCR *sp, EXCMD *cmdp)
49 ARGS **argv;
50 FREF *frp;
51 int noargs;
52 char **ap;
53 const CHAR_T *wp;
54 size_t wlen;
55 const char *np;
56 size_t nlen;
58 /* Check for file to move to. */
59 if (cmdp->argc == 0 && (sp->cargv == NULL || sp->cargv[1] == NULL)) {
60 msgq(sp, M_ERR, "111|No more files to edit");
61 return (1);
64 if (F_ISSET(cmdp, E_NEWSCREEN)) {
65 /* By default, edit the next file in the old argument list. */
66 if (cmdp->argc == 0) {
67 CHAR2INT(sp, sp->cargv[1], strlen(sp->cargv[1]) + 1,
68 wp, wlen);
69 if (argv_exp0(sp, cmdp, wp, wlen - 1))
70 return (1);
71 return (ex_edit(sp, cmdp));
73 return (ex_N_next(sp, cmdp));
76 /* Check modification. */
77 if (file_m1(sp,
78 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
79 return (1);
81 /* Any arguments are a replacement file list. */
82 if (cmdp->argc) {
83 /* Free the current list. */
84 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
85 for (ap = sp->argv; *ap != NULL; ++ap)
86 free(*ap);
87 free(sp->argv);
89 F_CLR(sp, SC_ARGNOFREE | SC_ARGRECOVER);
90 sp->cargv = NULL;
92 /* Create a new list. */
93 CALLOC_RET(sp,
94 sp->argv, char **, cmdp->argc + 1, sizeof(char *));
95 for (ap = sp->argv,
96 argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
97 INT2CHAR(sp, argv[0]->bp, argv[0]->len, np, nlen);
98 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
99 return (1);
101 *ap = NULL;
103 /* Switch to the first file. */
104 sp->cargv = sp->argv;
105 if ((frp = file_add(sp, *sp->cargv)) == NULL)
106 return (1);
107 noargs = 0;
109 /* Display a file count with the welcome message. */
110 F_SET(sp, SC_STATUS_CNT);
111 } else {
112 if ((frp = file_add(sp, sp->cargv[1])) == NULL)
113 return (1);
114 if (F_ISSET(sp, SC_ARGRECOVER))
115 F_SET(frp, FR_RECOVER);
116 noargs = 1;
119 if (file_init(sp, frp, NULL, FS_SETALT |
120 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
121 return (1);
122 if (noargs)
123 ++sp->cargv;
125 F_SET(sp, SC_FSWITCH);
126 return (0);
130 * ex_N_next --
131 * New screen version of ex_next.
133 static int
134 ex_N_next(SCR *sp, EXCMD *cmdp)
136 SCR *new;
137 FREF *frp;
138 const char *np;
139 size_t nlen;
141 /* Get a new screen. */
142 if (screen_init(sp->gp, sp, &new))
143 return (1);
144 if (vs_split(sp, new, 0)) {
145 (void)screen_end(new);
146 return (1);
149 /* Get a backing file. */
150 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, np, nlen);
151 if ((frp = file_add(new, np)) == NULL ||
152 file_init(new, frp, NULL,
153 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
154 (void)vs_discard(new, NULL);
155 (void)screen_end(new);
156 return (1);
159 /* The arguments are a replacement file list. */
160 new->cargv = new->argv = ex_buildargv(sp, cmdp, NULL);
162 /* Display a file count with the welcome message. */
163 F_SET(new, SC_STATUS_CNT);
165 /* Set up the switch. */
166 sp->nextdisp = new;
167 F_SET(sp, SC_SSWITCH);
169 return (0);
173 * ex_prev -- :prev
174 * Edit the previous file.
176 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
179 ex_prev(SCR *sp, EXCMD *cmdp)
181 FREF *frp;
182 size_t wlen;
183 const CHAR_T *wp;
185 if (sp->cargv == sp->argv) {
186 msgq(sp, M_ERR, "112|No previous files to edit");
187 return (1);
190 if (F_ISSET(cmdp, E_NEWSCREEN)) {
191 CHAR2INT(sp, sp->cargv[-1], strlen(sp->cargv[-1]) + 1,
192 wp, wlen);
193 if (argv_exp0(sp, cmdp, wp, wlen - 1))
194 return (1);
195 return (ex_edit(sp, cmdp));
198 if (file_m1(sp,
199 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
200 return (1);
202 if ((frp = file_add(sp, sp->cargv[-1])) == NULL)
203 return (1);
205 if (file_init(sp, frp, NULL, FS_SETALT |
206 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
207 return (1);
208 --sp->cargv;
210 F_SET(sp, SC_FSWITCH);
211 return (0);
215 * ex_rew -- :rew
216 * Re-edit the list of files.
218 * !!!
219 * Historic practice was that all files would start editing at the beginning
220 * of the file. We don't get this right because we may have multiple screens
221 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
222 * anyone noticing, but if they do, we'll have to put information into the SCR
223 * structure so we can keep track of it.
225 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
228 ex_rew(SCR *sp, EXCMD *cmdp)
230 FREF *frp;
233 * !!!
234 * Historic practice -- you can rewind to the current file.
236 if (sp->argv == NULL) {
237 msgq(sp, M_ERR, "113|No previous files to rewind");
238 return (1);
241 if (file_m1(sp,
242 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
243 return (1);
245 /* Switch to the first one. */
246 sp->cargv = sp->argv;
247 if ((frp = file_add(sp, *sp->cargv)) == NULL)
248 return (1);
249 if (file_init(sp, frp, NULL, FS_SETALT |
250 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
251 return (1);
253 /* Switch and display a file count with the welcome message. */
254 F_SET(sp, SC_FSWITCH | SC_STATUS_CNT);
256 return (0);
260 * ex_args -- :args
261 * Display the list of files.
263 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
266 ex_args(SCR *sp, EXCMD *cmdp)
268 GS *gp;
269 int cnt, sep;
270 size_t col, len;
271 char **ap;
273 if (sp->argv == NULL) {
274 (void)msgq(sp, M_ERR, "114|No file list to display");
275 return (0);
278 gp = sp->gp;
279 col = len = sep = 0;
280 for (cnt = 1, ap = sp->argv; *ap != NULL; ++ap) {
281 col += len = strlen(*ap) + sep + (ap == sp->cargv ? 2 : 0);
282 if (col >= sp->cols - 1) {
283 col = len;
284 sep = 0;
285 (void)ex_puts(sp, "\n");
286 } else if (cnt != 1) {
287 sep = 1;
288 (void)ex_puts(sp, " ");
290 ++cnt;
292 (void)ex_printf(sp, "%s%s%s", ap == sp->cargv ? "[" : "",
293 *ap, ap == sp->cargv ? "]" : "");
294 if (INTERRUPTED(sp))
295 break;
297 (void)ex_puts(sp, "\n");
298 return (0);
302 * ex_buildargv --
303 * Build a new file argument list.
305 * PUBLIC: char **ex_buildargv __P((SCR *, EXCMD *, char *));
307 char **
308 ex_buildargv(SCR *sp, EXCMD *cmdp, char *name)
310 ARGS **argv;
311 int argc;
312 char **ap, **s_argv;
313 const char *np;
314 size_t nlen;
316 argc = cmdp == NULL ? 1 : cmdp->argc;
317 CALLOC(sp, s_argv, char **, argc + 1, sizeof(char *));
318 if ((ap = s_argv) == NULL)
319 return (NULL);
321 if (cmdp == NULL) {
322 if ((*ap = v_strdup(sp, name, strlen(name))) == NULL)
323 return (NULL);
324 ++ap;
325 } else
326 for (argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
327 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len,
328 np, nlen);
329 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
330 return (NULL);
332 *ap = NULL;
333 return (s_argv);