xref: /csrg-svn/contrib/ed/input_lines.c (revision 58315)
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.3 (Berkeley) 02/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 
70 	for (;;) {
71 		if (sigint_flag && (!sigspecial))
72 			goto point;
73 		/* If NULL or bit-8 high not text; chuck. */
74 		l_ss = getc(fp);
75 		if (l_ss == EOF) {
76 			if (add_flag) {
77 				l_text[l_nn++] = '\0';
78 				goto eof_mk;
79 			}
80 			break;
81 		}
82 		if ((!l_ss) || (l_ss > 127))
83 			continue;
84 		l_text[l_nn++] = (char)l_ss;
85 		if (l_ss == '\n') {
86 			if (sigint_flag && (!sigspecial))
87 				goto point;
88 			l_text[l_nn - 1] = '\0';
89 			if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
90 				break;
91 eof_mk:
92 			nn_max_end = malloc(sizeof(LINE));
93 			if (nn_max_end == NULL) {
94 				*errnum = -1;
95 				strcpy(help_msg, "out of memory error");
96 				return (0L);
97 			}
98 			(nn_max_end->len) = l_nn - 1;
99 			(nn_max_end->handle) = add_line(l_text, l_nn - 1);
100 			(nn_max_end->above) = l_temp_line;
101 			(nn_max_end->below) = NULL;
102 			if (l_temp_line)
103 				(l_temp_line->below) = nn_max_end;
104 			else
105 				top = nn_max_end;
106 			l_temp_line = nn_max_end;
107 
108 			l_ttl += l_nn;
109 			l_nn = 0;
110 			if (l_ss == EOF)
111 				break;
112 		} else
113 			if (l_nn > l_nn_max) {
114 				l_nn_max += 512;
115 				nn_max = l_nn_max;
116 				l_text2 = l_text;
117 				l_text = text =
118 				    calloc(l_nn_max + 2, sizeof(char));
119 				if (text == NULL) {
120 					*errnum = -1;
121 					strcpy(help_msg, "out of memory error");
122 					return (0L);
123 				}
124 				bcopy(l_text2, l_text, l_nn);
125 				free(l_text2);
126 			}
127 	}
128 
129 point:	current = nn_max_end;
130 	if (current == NULL)
131 		current = top;
132 	if (l_temp1 == NULL)
133 		bottom = nn_max_end;
134 	else
135 		if (nn_max_end != NULL) {
136 			(nn_max_end->below) = l_temp1;
137 			u_add_stk(&(l_temp1->above));
138 			(l_temp1->above) = nn_max_end;
139 		}
140 	change_flag = 1;
141 	sigspecial--;
142 	if (sigint_flag && (!sigspecial))
143 		SIGINT_ACTION;
144 	*errnum = 1;
145 	ss = l_ss;
146 	sigspecial = 0;
147 	return (l_ttl);
148 }
149