BSD 4_4 release
[unix-history] / usr / src / contrib / ed / input_lines.c
CommitLineData
9a4ab09a 1/*-
ba5e8546
KB
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
9a4ab09a
KB
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rodney Ruddock of the University of Guelph.
7 *
ad787160
C
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
9a4ab09a
KB
35 */
36
37#ifndef lint
ad787160 38static char sccsid[] = "@(#)input_lines.c 8.1 (Berkeley) 5/31/93";
9a4ab09a
KB
39#endif /* not lint */
40
ecbf4ad0
KB
41#include <sys/types.h>
42
ecbf4ad0
KB
43#include <regex.h>
44#include <setjmp.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
9a4ab09a 48
31e0ca30
KB
49#ifdef DBI
50#include <db.h>
51#endif
52
ecbf4ad0
KB
53#include "ed.h"
54#include "extern.h"
9a4ab09a
KB
55
56/*
57 * This central function gets text from some file, which can (and most
58 * oft is) stdin. This flexability allows any text inputing function
59 * to use it.
60 */
9a4ab09a
KB
61long
62input_lines(fp, errnum)
ecbf4ad0
KB
63 FILE *fp;
64 int *errnum;
9a4ab09a 65{
ecbf4ad0
KB
66 register long l_nn = 0;
67 register int l_ss = ss;
68 register char *l_text = text;
69 LINE *l_temp_line, *l_temp1;
70 long l_ttl = 0;
c842a051 71 int l_nn_max = nn_max, l_jmp_flag;
ecbf4ad0 72 char *l_text2;
9a4ab09a 73
ecbf4ad0 74 if (End_default)
035e94f8 75 Start = current;
ecbf4ad0 76 else
035e94f8
RC
77 Start = End;
78 Start_default = End_default = 0;
9a4ab09a 79
e692f66f 80 sigspecial++;
035e94f8
RC
81 /* Start == NULL means line 0 which is legal for this function only. */
82 nn_max_end = l_temp_line = Start;
83 if (Start == NULL) {
ecbf4ad0
KB
84 l_temp1 = top;
85 u_add_stk(&(top->above));
86 } else {
035e94f8
RC
87 u_add_stk(&(Start->below));
88 l_temp1 = Start->below;
ecbf4ad0 89 }
e692f66f
KB
90 sigspecial--;
91 if (sigint_flag && (!sigspecial))
92 SIGINT_ACTION;
93
94 sigspecial++;
c842a051
KB
95 if (l_jmp_flag = setjmp(ctrl_position3))
96 goto point;
9a4ab09a 97
ecbf4ad0 98 for (;;) {
672cc1c0 99 if (sigint_flag)
e692f66f 100 goto point;
c842a051 101 sigspecial3 = 1;
e692f66f 102 l_ss = getc(fp);
c842a051 103 sigspecial3 = 0;
e692f66f 104 if (l_ss == EOF) {
afdef93a
KB
105 clearerr(fp);
106 if (l_nn) {
107 printf("<newline> added at end of line\n");
108 l_nn++;
109 goto eof_mk;
110 }
ecbf4ad0 111 break;
e692f66f 112 }
e692f66f 113 l_text[l_nn++] = (char)l_ss;
ecbf4ad0 114 if (l_ss == '\n') {
672cc1c0 115 if (sigint_flag)
e692f66f 116 goto point;
afdef93a 117eof_mk:
ecbf4ad0
KB
118 l_text[l_nn - 1] = '\0';
119 if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
120 break;
c842a051 121 nn_max_end = (LINE *)malloc(sizeof(LINE));
ecbf4ad0
KB
122 if (nn_max_end == NULL) {
123 *errnum = -1;
124 strcpy(help_msg, "out of memory error");
125 return (0L);
126 }
127 (nn_max_end->len) = l_nn - 1;
128 (nn_max_end->handle) = add_line(l_text, l_nn - 1);
129 (nn_max_end->above) = l_temp_line;
130 (nn_max_end->below) = NULL;
131 if (l_temp_line)
132 (l_temp_line->below) = nn_max_end;
133 else
134 top = nn_max_end;
135 l_temp_line = nn_max_end;
9a4ab09a 136
ecbf4ad0
KB
137 l_ttl += l_nn;
138 l_nn = 0;
e692f66f
KB
139 if (l_ss == EOF)
140 break;
ecbf4ad0
KB
141 } else
142 if (l_nn > l_nn_max) {
143 l_nn_max += 512;
144 nn_max = l_nn_max;
145 l_text2 = l_text;
146 l_text = text =
147 calloc(l_nn_max + 2, sizeof(char));
148 if (text == NULL) {
149 *errnum = -1;
150 strcpy(help_msg, "out of memory error");
151 return (0L);
152 }
080766e7 153 memmove(l_text, l_text2, l_nn);
ecbf4ad0
KB
154 free(l_text2);
155 }
156 }
9a4ab09a 157
ecbf4ad0
KB
158point: current = nn_max_end;
159 if (current == NULL)
160 current = top;
161 if (l_temp1 == NULL)
162 bottom = nn_max_end;
163 else
164 if (nn_max_end != NULL) {
165 (nn_max_end->below) = l_temp1;
166 u_add_stk(&(l_temp1->above));
167 (l_temp1->above) = nn_max_end;
168 }
169 change_flag = 1;
e692f66f 170 sigspecial--;
c842a051 171 sigspecial3 = 0;
e692f66f
KB
172 if (sigint_flag && (!sigspecial))
173 SIGINT_ACTION;
ecbf4ad0
KB
174 *errnum = 1;
175 ss = l_ss;
176 return (l_ttl);
177}