common/log.c: minor whitespace change
[nvi.git] / ex / ex_args.c
blob32d7aac5976d2514008e1e86761b80f7184f2611
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 #ifndef lint
13 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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "../common/common.h"
28 #include "../vi/vi.h"
30 static int ex_N_next __P((SCR *, EXCMD *));
33 * ex_next -- :next [+cmd] [files]
34 * Edit the next file, optionally setting the list of files.
36 * !!!
37 * The :next command behaved differently from the :rewind command in
38 * historic vi. See nvi/docs/autowrite for details, but the basic
39 * idea was that it ignored the force flag if the autowrite flag was
40 * set. This implementation handles them all identically.
42 * PUBLIC: int ex_next __P((SCR *, EXCMD *));
44 int
45 ex_next(SCR *sp, EXCMD *cmdp)
47 ARGS **argv;
48 FREF *frp;
49 int noargs;
50 char **ap;
51 CHAR_T *wp;
52 size_t wlen;
53 char *np;
54 size_t nlen;
56 /* Check for file to move to. */
57 if (cmdp->argc == 0 && (sp->cargv == NULL || sp->cargv[1] == NULL)) {
58 msgq(sp, M_ERR, "111|No more files to edit");
59 return (1);
62 if (F_ISSET(cmdp, E_NEWSCREEN)) {
63 /* By default, edit the next file in the old argument list. */
64 if (cmdp->argc == 0) {
65 CHAR2INT(sp, sp->cargv[1], strlen(sp->cargv[1]) + 1,
66 wp, wlen);
67 if (argv_exp0(sp, cmdp, wp, wlen - 1))
68 return (1);
69 return (ex_edit(sp, cmdp));
71 return (ex_N_next(sp, cmdp));
74 /* Check modification. */
75 if (file_m1(sp,
76 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
77 return (1);
79 /* Any arguments are a replacement file list. */
80 if (cmdp->argc) {
81 /* Free the current list. */
82 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
83 for (ap = sp->argv; *ap != NULL; ++ap)
84 free(*ap);
85 free(sp->argv);
87 F_CLR(sp, SC_ARGNOFREE | SC_ARGRECOVER);
88 sp->cargv = NULL;
90 /* Create a new list. */
91 CALLOC_RET(sp,
92 sp->argv, char **, cmdp->argc + 1, sizeof(char *));
93 for (ap = sp->argv,
94 argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
95 INT2CHAR(sp, argv[0]->bp, argv[0]->len, np, nlen);
96 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
97 return (1);
99 *ap = NULL;
101 /* Switch to the first file. */
102 sp->cargv = sp->argv;
103 if ((frp = file_add(sp, *sp->cargv)) == NULL)
104 return (1);
105 noargs = 0;
107 /* Display a file count with the welcome message. */
108 F_SET(sp, SC_STATUS_CNT);
109 } else {
110 if ((frp = file_add(sp, sp->cargv[1])) == NULL)
111 return (1);
112 if (F_ISSET(sp, SC_ARGRECOVER))
113 F_SET(frp, FR_RECOVER);
114 noargs = 1;
117 if (file_init(sp, frp, NULL, FS_SETALT |
118 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
119 return (1);
120 if (noargs)
121 ++sp->cargv;
123 F_SET(sp, SC_FSWITCH);
124 return (0);
128 * ex_N_next --
129 * New screen version of ex_next.
131 static int
132 ex_N_next(SCR *sp, EXCMD *cmdp)
134 SCR *new;
135 FREF *frp;
136 char *np;
137 size_t nlen;
139 /* Get a new screen. */
140 if (screen_init(sp->gp, sp, &new))
141 return (1);
142 if (vs_split(sp, new, 0)) {
143 (void)screen_end(new);
144 return (1);
147 /* Get a backing file. */
148 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len + 1, np, nlen);
149 if ((frp = file_add(new, np)) == NULL ||
150 file_init(new, frp, NULL,
151 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0))) {
152 (void)vs_discard(new, NULL);
153 (void)screen_end(new);
154 return (1);
157 /* The arguments are a replacement file list. */
158 new->cargv = new->argv = ex_buildargv(sp, cmdp, NULL);
160 /* Display a file count with the welcome message. */
161 F_SET(new, SC_STATUS_CNT);
163 /* Set up the switch. */
164 sp->nextdisp = new;
165 F_SET(sp, SC_SSWITCH);
167 return (0);
171 * ex_prev -- :prev
172 * Edit the previous file.
174 * PUBLIC: int ex_prev __P((SCR *, EXCMD *));
177 ex_prev(SCR *sp, EXCMD *cmdp)
179 FREF *frp;
180 size_t wlen;
181 CHAR_T *wp;
183 if (sp->cargv == sp->argv) {
184 msgq(sp, M_ERR, "112|No previous files to edit");
185 return (1);
188 if (F_ISSET(cmdp, E_NEWSCREEN)) {
189 CHAR2INT(sp, sp->cargv[-1], strlen(sp->cargv[-1]) + 1,
190 wp, wlen);
191 if (argv_exp0(sp, cmdp, wp, wlen - 1))
192 return (1);
193 return (ex_edit(sp, cmdp));
196 if (file_m1(sp,
197 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
198 return (1);
200 if ((frp = file_add(sp, sp->cargv[-1])) == NULL)
201 return (1);
203 if (file_init(sp, frp, NULL, FS_SETALT |
204 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
205 return (1);
206 --sp->cargv;
208 F_SET(sp, SC_FSWITCH);
209 return (0);
213 * ex_rew -- :rew
214 * Re-edit the list of files.
216 * !!!
217 * Historic practice was that all files would start editing at the beginning
218 * of the file. We don't get this right because we may have multiple screens
219 * and we can't clear the FR_CURSORSET bit for a single screen. I don't see
220 * anyone noticing, but if they do, we'll have to put information into the SCR
221 * structure so we can keep track of it.
223 * PUBLIC: int ex_rew __P((SCR *, EXCMD *));
226 ex_rew(SCR *sp, EXCMD *cmdp)
228 FREF *frp;
231 * !!!
232 * Historic practice -- you can rewind to the current file.
234 if (sp->argv == NULL) {
235 msgq(sp, M_ERR, "113|No previous files to rewind");
236 return (1);
239 if (file_m1(sp,
240 FL_ISSET(cmdp->iflags, E_C_FORCE), FS_ALL | FS_POSSIBLE))
241 return (1);
243 /* Switch to the first one. */
244 sp->cargv = sp->argv;
245 if ((frp = file_add(sp, *sp->cargv)) == NULL)
246 return (1);
247 if (file_init(sp, frp, NULL, FS_SETALT |
248 (FL_ISSET(cmdp->iflags, E_C_FORCE) ? FS_FORCE : 0)))
249 return (1);
251 /* Switch and display a file count with the welcome message. */
252 F_SET(sp, SC_FSWITCH | SC_STATUS_CNT);
254 return (0);
258 * ex_args -- :args
259 * Display the list of files.
261 * PUBLIC: int ex_args __P((SCR *, EXCMD *));
264 ex_args(SCR *sp, EXCMD *cmdp)
266 GS *gp;
267 int cnt, col, len, sep;
268 char **ap;
270 if (sp->argv == NULL) {
271 (void)msgq(sp, M_ERR, "114|No file list to display");
272 return (0);
275 gp = sp->gp;
276 col = len = sep = 0;
277 for (cnt = 1, ap = sp->argv; *ap != NULL; ++ap) {
278 col += len = strlen(*ap) + sep + (ap == sp->cargv ? 2 : 0);
279 if (col >= sp->cols - 1) {
280 col = len;
281 sep = 0;
282 (void)ex_puts(sp, "\n");
283 } else if (cnt != 1) {
284 sep = 1;
285 (void)ex_puts(sp, " ");
287 ++cnt;
289 (void)ex_printf(sp, "%s%s%s", ap == sp->cargv ? "[" : "",
290 *ap, ap == sp->cargv ? "]" : "");
291 if (INTERRUPTED(sp))
292 break;
294 (void)ex_puts(sp, "\n");
295 return (0);
299 * ex_buildargv --
300 * Build a new file argument list.
302 * PUBLIC: char **ex_buildargv __P((SCR *, EXCMD *, char *));
304 char **
305 ex_buildargv(SCR *sp, EXCMD *cmdp, char *name)
307 ARGS **argv;
308 int argc;
309 char **ap, **s_argv;
310 char *np;
311 size_t nlen;
313 argc = cmdp == NULL ? 1 : cmdp->argc;
314 CALLOC(sp, s_argv, char **, argc + 1, sizeof(char *));
315 if ((ap = s_argv) == NULL)
316 return (NULL);
318 if (cmdp == NULL) {
319 if ((*ap = v_strdup(sp, name, strlen(name))) == NULL)
320 return (NULL);
321 ++ap;
322 } else
323 for (argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv) {
324 INT2CHAR(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len,
325 np, nlen);
326 if ((*ap = v_strdup(sp, np, nlen)) == NULL)
327 return (NULL);
329 *ap = NULL;
330 return (s_argv);