Fix UTIME_OMIT handling
[dragonfly.git] / contrib / nvi2 / ex / ex_args.c
blob5e8f2d23f573f79f632db1b322342d5f3530a12d
1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
16 #include <bitstring.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include "../common/common.h"
24 #include "../vi/vi.h"
26 static int ex_N_next(SCR *, EXCMD *);
29 * ex_next -- :next [+cmd] [files]
30 * Edit the next file, optionally setting the list of files.
32 * !!!
33 * The :next command behaved differently from the :rewind command in
34 * historic vi. See nvi/docs/autowrite for details, but the basic
35 * idea was that it ignored the force flag if the autowrite flag was
36 * set. This implementation handles them all identically.
38 * PUBLIC: int ex_next(SCR *, EXCMD *);
40 int
41 ex_next(SCR *sp, EXCMD *cmdp)
43 ARGS **argv;
44 FREF *frp;
45 int noargs;
46 char **ap;
47 CHAR_T *wp;
48 size_t wlen;
49 char *np;
50 size_t nlen;
52 /* Check for file to move to. */
53 if (cmdp->argc == 0 && (sp->cargv == NULL || sp->cargv[1] == NULL)) {
54 msgq(sp, M_ERR, "111|No more files to edit");
55 return (1);
58 if (F_ISSET(cmdp, E_NEWSCREEN)) {
59 /* By default, edit the next file in the old argument list. */
60 if (cmdp->argc == 0) {
61 CHAR2INT(sp, sp->cargv[1], strlen(sp->cargv[1]) + 1,
62 wp, wlen);
63 if (argv_exp0(sp, cmdp, wp, wlen - 1))
64 return (1);
65 return (ex_edit(sp, cmdp));
67 return (ex_N_next(sp, cmdp));
70 /* Check modification. */
71 if (file_m1(sp,
72 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
73 return (1);
75 /* Any arguments are a replacement file list. */
76 if (cmdp->argc) {
77 /* Free the current list. */
78 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
79 for (ap = sp->argv; *ap != NULL; ++ap)
80 free(*ap);
81 free(sp->argv);
83 F_CLR(sp, SC_ARGNOFREE | SC_ARGRECOVER);
84 sp->cargv = NULL;
86 /* Create a new list. */
87 CALLOC_RET(sp, sp->argv, cmdp->argc + 1, sizeof(char *));
88 for (ap = sp->argv,
89 argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
90 INT2CHAR(sp, argv[0]->bp, argv[0]->len, np, nlen);
91 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
92 return (1);
94 *ap = NULL;
96 /* Switch to the first file. */
97 sp->cargv = sp->argv;
98 if ((frp = file_add(sp, *sp->cargv)) == NULL)
99 return (1);
100 noargs = 0;
102 /* Display a file count with the welcome message. */
103 F_SET(sp, SC_STATUS_CNT);
104 } else {
105 if ((frp = file_add(sp, sp->cargv[1])) == NULL)
106 return (1);
107 if (F_ISSET(sp, SC_ARGRECOVER))
108 F_SET(frp, FR_RECOVER);
109 noargs = 1;
112 if (file_init(sp, frp, NULL, FS_SETALT |
113 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
114 return (1);
115 if (noargs)
116 ++sp->cargv;
118 F_SET(sp, SC_FSWITCH);
119 return (0);
123 * ex_N_next --
124 * New screen version of ex_next.
126 static int
127 ex_N_next(SCR *sp, EXCMD *cmdp)
129 SCR *new;
130 FREF *frp;
131 char *np;
132 size_t nlen;
134 /* Get a new screen. */
135 if (screen_init(sp->gp, sp, &new))
136 return (1);
137 if (vs_split(sp, new, 0)) {
138 (void)screen_end(new);
139 return (1);
142 /* Get a backing file. */
143 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, np, nlen);
144 if ((frp = file_add(new, np)) == NULL ||
145 file_init(new, frp, NULL,
146 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
147 (void)vs_discard(new, NULL);
148 (void)screen_end(new);
149 return (1);
152 /* The arguments are a replacement file list. */
153 new->cargv = new->argv = ex_buildargv(sp, cmdp, NULL);
155 /* Display a file count with the welcome message. */
156 F_SET(new, SC_STATUS_CNT);
158 /* Set up the switch. */
159 sp->nextdisp = new;
160 F_SET(sp, SC_SSWITCH);
162 return (0);
166 * ex_prev -- :prev
167 * Edit the previous file.
169 * PUBLIC: int ex_prev(SCR *, EXCMD *);
172 ex_prev(SCR *sp, EXCMD *cmdp)
174 FREF *frp;
175 size_t wlen;
176 CHAR_T *wp;
178 if (sp->cargv == sp->argv) {
179 msgq(sp, M_ERR, "112|No previous files to edit");
180 return (1);
183 if (F_ISSET(cmdp, E_NEWSCREEN)) {
184 CHAR2INT(sp, sp->cargv[-1], strlen(sp->cargv[-1]) + 1,
185 wp, wlen);
186 if (argv_exp0(sp, cmdp, wp, wlen - 1))
187 return (1);
188 return (ex_edit(sp, cmdp));
191 if (file_m1(sp,
192 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
193 return (1);
195 if ((frp = file_add(sp, sp->cargv[-1])) == NULL)
196 return (1);
198 if (file_init(sp, frp, NULL, FS_SETALT |
199 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
200 return (1);
201 --sp->cargv;
203 F_SET(sp, SC_FSWITCH);
204 return (0);
208 * ex_rew -- :rew
209 * Re-edit the list of files.
211 * !!!
212 * Historic practice was that all files would start editing at the beginning
213 * of the file. We don't get this right because we may have multiple screens
214 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
215 * anyone noticing, but if they do, we'll have to put information into the SCR
216 * structure so we can keep track of it.
218 * PUBLIC: int ex_rew(SCR *, EXCMD *);
221 ex_rew(SCR *sp, EXCMD *cmdp)
223 FREF *frp;
226 * !!!
227 * Historic practice -- you can rewind to the current file.
229 if (sp->argv == NULL) {
230 msgq(sp, M_ERR, "113|No previous files to rewind");
231 return (1);
234 if (file_m1(sp,
235 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
236 return (1);
238 /* Switch to the first one. */
239 sp->cargv = sp->argv;
240 if ((frp = file_add(sp, *sp->cargv)) == NULL)
241 return (1);
242 if (file_init(sp, frp, NULL, FS_SETALT |
243 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
244 return (1);
246 /* Switch and display a file count with the welcome message. */
247 F_SET(sp, SC_FSWITCH | SC_STATUS_CNT);
249 return (0);
253 * ex_args -- :args
254 * Display the list of files.
256 * PUBLIC: int ex_args(SCR *, EXCMD *);
259 ex_args(SCR *sp, EXCMD *cmdp)
261 GS *gp;
262 int cnt, col, len, sep;
263 char **ap;
265 if (sp->argv == NULL) {
266 (void)msgq(sp, M_ERR, "114|No file list to display");
267 return (0);
270 gp = sp->gp;
271 col = len = sep = 0;
272 for (cnt = 1, ap = sp->argv; *ap != NULL; ++ap) {
273 col += len = strlen(*ap) + sep + (ap == sp->cargv ? 2 : 0);
274 if (col >= sp->cols - 1) {
275 col = len;
276 sep = 0;
277 (void)ex_puts(sp, "\n");
278 } else if (cnt != 1) {
279 sep = 1;
280 (void)ex_puts(sp, " ");
282 ++cnt;
284 (void)ex_printf(sp, "%s%s%s", ap == sp->cargv ? "[" : "",
285 *ap, ap == sp->cargv ? "]" : "");
286 if (INTERRUPTED(sp))
287 break;
289 (void)ex_puts(sp, "\n");
290 return (0);
294 * ex_buildargv --
295 * Build a new file argument list.
297 * PUBLIC: char **ex_buildargv(SCR *, EXCMD *, char *);
299 char **
300 ex_buildargv(SCR *sp, EXCMD *cmdp, char *name)
302 ARGS **argv;
303 int argc;
304 char **ap, **s_argv;
305 char *np;
306 size_t nlen;
308 argc = cmdp == NULL ? 1 : cmdp->argc;
309 CALLOC(sp, s_argv, argc + 1, sizeof(char *));
310 if ((ap = s_argv) == NULL)
311 return (NULL);
313 if (cmdp == NULL) {
314 if ((*ap = v_strdup(sp, name, strlen(name))) == NULL)
315 return (NULL);
316 ++ap;
317 } else
318 for (argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
319 INT2CHAR(sp, argv[0]->bp, argv[0]->len, np, nlen);
320 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
321 return (NULL);
323 *ap = NULL;
324 return (s_argv);