xref: /original-bsd/contrib/ed/input_lines.c (revision 909c03fb)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rodney Ruddock of the University of Guelph.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)input_lines.c	5.10 (Berkeley) 04/28/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <regex.h>
18 #include <setjmp.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #ifdef DBI
24 #include <db.h>
25 #endif
26 
27 #include "ed.h"
28 #include "extern.h"
29 
30 /*
31  * This central function gets text from some file, which can (and most
32  * oft is) stdin. This flexability allows any text inputing function
33  * to use it.
34  */
35 long
36 input_lines(fp, errnum)
37 	FILE *fp;
38 	int *errnum;
39 {
40 	register long l_nn = 0;
41 	register int l_ss = ss;
42 	register char *l_text = text;
43 	LINE *l_temp_line, *l_temp1;
44 	long l_ttl = 0;
45 	int l_nn_max = nn_max, l_jmp_flag;
46 	char *l_text2;
47 
48 	if (End_default)
49 		Start = current;
50 	else
51 		Start = End;
52 	Start_default = End_default = 0;
53 
54 	sigspecial++;
55 	/* Start == NULL means line 0 which is legal for this function only. */
56 	nn_max_end = l_temp_line = Start;
57 	if (Start == NULL) {
58 		l_temp1 = top;
59 		u_add_stk(&(top->above));
60 	} else {
61 		u_add_stk(&(Start->below));
62 		l_temp1 = Start->below;
63 	}
64 	sigspecial--;
65 	if (sigint_flag && (!sigspecial))
66 		SIGINT_ACTION;
67 
68 	sigspecial++;
69         if (l_jmp_flag = setjmp(ctrl_position3))
70                 goto point;
71 
72 	for (;;) {
73 		if (sigint_flag)
74 			goto point;
75         	sigspecial3 = 1;
76 		l_ss = getc(fp);
77         	sigspecial3 = 0;
78 		if (l_ss == EOF) {
79 				clearerr(fp);
80 				if (l_nn) {
81 					printf("<newline> added at end of line\n");
82 					l_nn++;
83 					goto eof_mk;
84 				}
85 			break;
86 		}
87 		l_text[l_nn++] = (char)l_ss;
88 		if (l_ss == '\n') {
89 			if (sigint_flag)
90 				goto point;
91 eof_mk:
92 			l_text[l_nn - 1] = '\0';
93 			if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
94 				break;
95 			nn_max_end = (LINE *)malloc(sizeof(LINE));
96 			if (nn_max_end == NULL) {
97 				*errnum = -1;
98 				strcpy(help_msg, "out of memory error");
99 				return (0L);
100 			}
101 			(nn_max_end->len) = l_nn - 1;
102 			(nn_max_end->handle) = add_line(l_text, l_nn - 1);
103 			(nn_max_end->above) = l_temp_line;
104 			(nn_max_end->below) = NULL;
105 			if (l_temp_line)
106 				(l_temp_line->below) = nn_max_end;
107 			else
108 				top = nn_max_end;
109 			l_temp_line = nn_max_end;
110 
111 			l_ttl += l_nn;
112 			l_nn = 0;
113 			if (l_ss == EOF)
114 				break;
115 		} else
116 			if (l_nn > l_nn_max) {
117 				l_nn_max += 512;
118 				nn_max = l_nn_max;
119 				l_text2 = l_text;
120 				l_text = text =
121 				    calloc(l_nn_max + 2, sizeof(char));
122 				if (text == NULL) {
123 					*errnum = -1;
124 					strcpy(help_msg, "out of memory error");
125 					return (0L);
126 				}
127 				memmove(l_text, l_text2, l_nn);
128 				free(l_text2);
129 			}
130 	}
131 
132 point:	current = nn_max_end;
133 	if (current == NULL)
134 		current = top;
135 	if (l_temp1 == NULL)
136 		bottom = nn_max_end;
137 	else
138 		if (nn_max_end != NULL) {
139 			(nn_max_end->below) = l_temp1;
140 			u_add_stk(&(l_temp1->above));
141 			(l_temp1->above) = nn_max_end;
142 		}
143 	change_flag = 1;
144 	sigspecial--;
145 	sigspecial3 = 0;
146 	if (sigint_flag && (!sigspecial))
147 		SIGINT_ACTION;
148 	*errnum = 1;
149 	ss = l_ss;
150 	return (l_ttl);
151 }
152