1*bbb81c22Smaya /* $NetBSD: glbl.c,v 1.10 2019/01/04 19:13:58 maya Exp $ */
249f0ad86Scgd
31357f155Salm /* glob.c: This file contains the global command routines for the ed line
41357f155Salm editor */
51357f155Salm /*-
61357f155Salm * Copyright (c) 1993 Andrew Moore, Talke Studio.
71357f155Salm * All rights reserved.
81357f155Salm *
91357f155Salm * Redistribution and use in source and binary forms, with or without
101357f155Salm * modification, are permitted provided that the following conditions
111357f155Salm * are met:
121357f155Salm * 1. Redistributions of source code must retain the above copyright
131357f155Salm * notice, this list of conditions and the following disclaimer.
141357f155Salm * 2. Redistributions in binary form must reproduce the above copyright
151357f155Salm * notice, this list of conditions and the following disclaimer in the
161357f155Salm * documentation and/or other materials provided with the distribution.
171357f155Salm *
181357f155Salm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
191357f155Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201357f155Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211357f155Salm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
221357f155Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231357f155Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241357f155Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251357f155Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261357f155Salm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271357f155Salm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281357f155Salm * SUCH DAMAGE.
291357f155Salm */
301357f155Salm
318b7ade1aSthorpej #include <sys/cdefs.h>
321357f155Salm #ifndef lint
3349f0ad86Scgd #if 0
341357f155Salm static char *rcsid = "@(#)glob.c,v 1.1 1994/02/01 00:34:40 alm Exp";
3549f0ad86Scgd #else
36*bbb81c22Smaya __RCSID("$NetBSD: glbl.c,v 1.10 2019/01/04 19:13:58 maya Exp $");
3749f0ad86Scgd #endif
381357f155Salm #endif /* not lint */
391357f155Salm
401357f155Salm #include <sys/ioctl.h>
411357f155Salm #include <sys/wait.h>
421357f155Salm
431357f155Salm #include "ed.h"
441357f155Salm
451357f155Salm
461357f155Salm /* build_active_list: add line matching a pattern to the global-active list */
471357f155Salm int
build_active_list(int isgcmd)48d33a7206Sxtraeme build_active_list(int isgcmd)
491357f155Salm {
501357f155Salm pattern_t *pat;
511357f155Salm line_t *lp;
521357f155Salm long n;
531357f155Salm char *s;
541357f155Salm char delimiter;
551357f155Salm
561357f155Salm if ((delimiter = *ibufp) == ' ' || delimiter == '\n') {
577cc84c81Sdholland seterrmsg("invalid pattern delimiter");
581357f155Salm return ERR;
591357f155Salm } else if ((pat = get_compiled_pattern()) == NULL)
601357f155Salm return ERR;
611357f155Salm else if (*ibufp == delimiter)
621357f155Salm ibufp++;
631357f155Salm clear_active_list();
641357f155Salm lp = get_addressed_line_node(first_addr);
651357f155Salm for (n = first_addr; n <= second_addr; n++, lp = lp->q_forw) {
661357f155Salm if ((s = get_sbuf_line(lp)) == NULL)
671357f155Salm return ERR;
681357f155Salm if (isbinary)
691357f155Salm NUL_TO_NEWLINE(s, lp->len);
70858639cdSjoerg if ((!regexec(pat, s, 0, NULL, 0)) == isgcmd &&
711357f155Salm set_active_node(lp) < 0)
721357f155Salm return ERR;
731357f155Salm }
741357f155Salm return 0;
751357f155Salm }
761357f155Salm
771357f155Salm
781357f155Salm /* exec_global: apply command list in the command buffer to the active
791357f155Salm lines in a range; return command status */
801357f155Salm long
exec_global(int interact,int gflag)81d33a7206Sxtraeme exec_global(int interact, int gflag)
821357f155Salm {
831357f155Salm static char *ocmd = NULL;
841357f155Salm static int ocmdsz = 0;
851357f155Salm
861357f155Salm line_t *lp = NULL;
871357f155Salm int status;
881357f155Salm int n;
891357f155Salm char *cmd = NULL;
901357f155Salm #ifdef BACKWARDS
916310b596Schristos char cmdp[] = "p\n";
926310b596Schristos
93341bd18bSthorpej if (!interact) {
941357f155Salm if (!strcmp(ibufp, "\n"))
956310b596Schristos cmd = cmdp; /* null cmd-list == `p' */
961357f155Salm else if ((cmd = get_extended_line(&n, 0)) == NULL)
971357f155Salm return ERR;
98341bd18bSthorpej }
991357f155Salm #else
1001357f155Salm if (!interact && (cmd = get_extended_line(&n, 0)) == NULL)
1011357f155Salm return ERR;
1021357f155Salm #endif
1031357f155Salm clear_undo_stack();
1041357f155Salm while ((lp = next_active_node()) != NULL) {
1051357f155Salm if ((current_addr = get_line_node_addr(lp)) < 0)
1061357f155Salm return ERR;
1071357f155Salm if (interact) {
1081357f155Salm /* print current_addr; get a command in global syntax */
1091357f155Salm if (display_lines(current_addr, current_addr, gflag) < 0)
1101357f155Salm return ERR;
1111357f155Salm while ((n = get_tty_line()) > 0 &&
1121357f155Salm ibuf[n - 1] != '\n')
1131357f155Salm clearerr(stdin);
1141357f155Salm if (n < 0)
1151357f155Salm return ERR;
1161357f155Salm else if (n == 0) {
1177cc84c81Sdholland seterrmsg("unexpected end-of-file");
1181357f155Salm return ERR;
1191357f155Salm } else if (n == 1 && !strcmp(ibuf, "\n"))
1201357f155Salm continue;
1211357f155Salm else if (n == 2 && !strcmp(ibuf, "&\n")) {
1221357f155Salm if (cmd == NULL) {
1237cc84c81Sdholland seterrmsg("no previous command");
1241357f155Salm return ERR;
1251357f155Salm } else cmd = ocmd;
1261357f155Salm } else if ((cmd = get_extended_line(&n, 0)) == NULL)
1271357f155Salm return ERR;
1281357f155Salm else {
1291357f155Salm REALLOC(ocmd, ocmdsz, n + 1, ERR);
1301357f155Salm memcpy(ocmd, cmd, n + 1);
1311357f155Salm cmd = ocmd;
1321357f155Salm }
1331357f155Salm
1341357f155Salm }
1351357f155Salm ibufp = cmd;
1361357f155Salm for (; *ibufp;)
1371357f155Salm if ((status = extract_addr_range()) < 0 ||
1381357f155Salm (status = exec_command()) < 0 ||
1398b7ade1aSthorpej (status > 0 && (status = display_lines(
1407ae8af9aSchristos current_addr, current_addr, status)) < 0))
1411357f155Salm return status;
1421357f155Salm }
1431357f155Salm return 0;
1441357f155Salm }
1451357f155Salm
1461357f155Salm
1471357f155Salm line_t **active_list; /* list of lines active in a global command */
1481357f155Salm long active_last; /* index of last active line in active_list */
1491357f155Salm long active_size; /* size of active_list */
1501357f155Salm long active_ptr; /* active_list index (non-decreasing) */
1511357f155Salm long active_ndx; /* active_list index (modulo active_last) */
1521357f155Salm
1531357f155Salm /* set_active_node: add a line node to the global-active list */
1541357f155Salm int
set_active_node(line_t * lp)155d33a7206Sxtraeme set_active_node(line_t *lp)
1561357f155Salm {
1571357f155Salm if (active_last + 1 > active_size) {
1581357f155Salm int ti = active_size;
1591357f155Salm line_t **ts;
1601357f155Salm SPL1();
1611357f155Salm if ((ts = (line_t **) realloc(active_list,
1621357f155Salm (ti += MINBUFSZ) * sizeof(line_t **))) == NULL) {
1631357f155Salm fprintf(stderr, "%s\n", strerror(errno));
1647cc84c81Sdholland seterrmsg("out of memory");
1651357f155Salm SPL0();
1661357f155Salm return ERR;
1671357f155Salm }
1681357f155Salm active_size = ti;
1691357f155Salm active_list = ts;
1701357f155Salm SPL0();
1711357f155Salm }
1721357f155Salm active_list[active_last++] = lp;
1731357f155Salm return 0;
1741357f155Salm }
1751357f155Salm
1761357f155Salm
1771357f155Salm /* unset_active_nodes: remove a range of lines from the global-active list */
1781357f155Salm void
unset_active_nodes(line_t * np,line_t * mp)179d33a7206Sxtraeme unset_active_nodes(line_t *np, line_t *mp)
1801357f155Salm {
1811357f155Salm line_t *lp;
1821357f155Salm long i;
1831357f155Salm
1841357f155Salm for (lp = np; lp != mp; lp = lp->q_forw)
1851357f155Salm for (i = 0; i < active_last; i++)
1861357f155Salm if (active_list[active_ndx] == lp) {
1871357f155Salm active_list[active_ndx] = NULL;
1881357f155Salm active_ndx = INC_MOD(active_ndx, active_last - 1);
1891357f155Salm break;
1901357f155Salm } else active_ndx = INC_MOD(active_ndx, active_last - 1);
1911357f155Salm }
1921357f155Salm
1931357f155Salm
1941357f155Salm /* next_active_node: return the next global-active line node */
1951357f155Salm line_t *
next_active_node(void)196d33a7206Sxtraeme next_active_node(void)
1971357f155Salm {
1981357f155Salm while (active_ptr < active_last && active_list[active_ptr] == NULL)
1991357f155Salm active_ptr++;
2001357f155Salm return (active_ptr < active_last) ? active_list[active_ptr++] : NULL;
2011357f155Salm }
2021357f155Salm
2031357f155Salm
2041357f155Salm /* clear_active_list: clear the global-active list */
2051357f155Salm void
clear_active_list(void)206d33a7206Sxtraeme clear_active_list(void)
2071357f155Salm {
2081357f155Salm SPL1();
2091357f155Salm active_size = active_last = active_ptr = active_ndx = 0;
2101357f155Salm free(active_list);
2111357f155Salm active_list = NULL;
2121357f155Salm SPL0();
2131357f155Salm }
214