xref: /csrg-svn/contrib/ed/input_lines.c (revision 58315)
157689Sbostic /*-
257689Sbostic  * Copyright (c) 1992 The Regents of the University of California.
357689Sbostic  * All rights reserved.
457689Sbostic  *
557689Sbostic  * This code is derived from software contributed to Berkeley by
657689Sbostic  * Rodney Ruddock of the University of Guelph.
757689Sbostic  *
857689Sbostic  * %sccs.include.redist.c%
957689Sbostic  */
1057689Sbostic 
1157689Sbostic #ifndef lint
12*58315Sbostic static char sccsid[] = "@(#)input_lines.c	5.3 (Berkeley) 02/28/93";
1357689Sbostic #endif /* not lint */
1457689Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic 
1757710Sbostic #include <regex.h>
1857710Sbostic #include <setjmp.h>
1957710Sbostic #include <stdio.h>
2057710Sbostic #include <stdlib.h>
2157710Sbostic #include <string.h>
2257710Sbostic 
23*58315Sbostic #ifdef DBI
24*58315Sbostic #include <db.h>
25*58315Sbostic #endif
26*58315Sbostic 
2757689Sbostic #include "ed.h"
2857710Sbostic #include "extern.h"
2957689Sbostic 
3057689Sbostic /*
3157689Sbostic  * This central function gets text from some file, which can (and most
3257689Sbostic  * oft is) stdin. This flexability allows any text inputing function
3357689Sbostic  * to use it.
3457689Sbostic  */
3557689Sbostic long
3657689Sbostic input_lines(fp, errnum)
3757710Sbostic 	FILE *fp;
3857710Sbostic 	int *errnum;
3957689Sbostic {
4057710Sbostic 	register long l_nn = 0;
4157710Sbostic 	register int l_ss = ss;
4257710Sbostic 	register char *l_text = text;
4357710Sbostic 	LINE *l_temp_line, *l_temp1;
4457710Sbostic 	long l_ttl = 0;
45*58315Sbostic 	int l_nn_max = nn_max, l_jmp_flag;
4657710Sbostic 	char *l_text2;
4757689Sbostic 
4857710Sbostic 	if (End_default)
4957710Sbostic 		start = current;
5057710Sbostic 	else
5157710Sbostic 		start = End;
5257710Sbostic 	start_default = End_default = 0;
5357689Sbostic 
54*58315Sbostic 	sigspecial++;
5557710Sbostic 	/* start == NULL means line 0 which is legal for this function only. */
5657710Sbostic 	nn_max_end = l_temp_line = start;
5757710Sbostic 	if (start == NULL) {
5857710Sbostic 		l_temp1 = top;
5957710Sbostic 		u_add_stk(&(top->above));
6057710Sbostic 	} else {
6157710Sbostic 		u_add_stk(&(start->below));
6257710Sbostic 		l_temp1 = start->below;
6357710Sbostic 	}
64*58315Sbostic 	sigspecial--;
65*58315Sbostic 	if (sigint_flag && (!sigspecial))
66*58315Sbostic 		SIGINT_ACTION;
6757689Sbostic 
68*58315Sbostic 	sigspecial++;
69*58315Sbostic 
7057710Sbostic 	for (;;) {
71*58315Sbostic 		if (sigint_flag && (!sigspecial))
72*58315Sbostic 			goto point;
7357710Sbostic 		/* If NULL or bit-8 high not text; chuck. */
74*58315Sbostic 		l_ss = getc(fp);
75*58315Sbostic 		if (l_ss == EOF) {
76*58315Sbostic 			if (add_flag) {
77*58315Sbostic 				l_text[l_nn++] = '\0';
78*58315Sbostic 				goto eof_mk;
79*58315Sbostic 			}
8057710Sbostic 			break;
81*58315Sbostic 		}
8257710Sbostic 		if ((!l_ss) || (l_ss > 127))
8357710Sbostic 			continue;
84*58315Sbostic 		l_text[l_nn++] = (char)l_ss;
8557710Sbostic 		if (l_ss == '\n') {
86*58315Sbostic 			if (sigint_flag && (!sigspecial))
87*58315Sbostic 				goto point;
8857710Sbostic 			l_text[l_nn - 1] = '\0';
8957710Sbostic 			if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
9057710Sbostic 				break;
91*58315Sbostic eof_mk:
9257710Sbostic 			nn_max_end = malloc(sizeof(LINE));
9357710Sbostic 			if (nn_max_end == NULL) {
9457710Sbostic 				*errnum = -1;
9557710Sbostic 				strcpy(help_msg, "out of memory error");
9657710Sbostic 				return (0L);
9757710Sbostic 			}
9857710Sbostic 			(nn_max_end->len) = l_nn - 1;
9957710Sbostic 			(nn_max_end->handle) = add_line(l_text, l_nn - 1);
10057710Sbostic 			(nn_max_end->above) = l_temp_line;
10157710Sbostic 			(nn_max_end->below) = NULL;
10257710Sbostic 			if (l_temp_line)
10357710Sbostic 				(l_temp_line->below) = nn_max_end;
10457710Sbostic 			else
10557710Sbostic 				top = nn_max_end;
10657710Sbostic 			l_temp_line = nn_max_end;
10757689Sbostic 
10857710Sbostic 			l_ttl += l_nn;
10957710Sbostic 			l_nn = 0;
110*58315Sbostic 			if (l_ss == EOF)
111*58315Sbostic 				break;
11257710Sbostic 		} else
11357710Sbostic 			if (l_nn > l_nn_max) {
11457710Sbostic 				l_nn_max += 512;
11557710Sbostic 				nn_max = l_nn_max;
11657710Sbostic 				l_text2 = l_text;
11757710Sbostic 				l_text = text =
11857710Sbostic 				    calloc(l_nn_max + 2, sizeof(char));
11957710Sbostic 				if (text == NULL) {
12057710Sbostic 					*errnum = -1;
12157710Sbostic 					strcpy(help_msg, "out of memory error");
12257710Sbostic 					return (0L);
12357710Sbostic 				}
12457710Sbostic 				bcopy(l_text2, l_text, l_nn);
12557710Sbostic 				free(l_text2);
12657710Sbostic 			}
12757710Sbostic 	}
12857689Sbostic 
12957710Sbostic point:	current = nn_max_end;
13057710Sbostic 	if (current == NULL)
13157710Sbostic 		current = top;
13257710Sbostic 	if (l_temp1 == NULL)
13357710Sbostic 		bottom = nn_max_end;
13457710Sbostic 	else
13557710Sbostic 		if (nn_max_end != NULL) {
13657710Sbostic 			(nn_max_end->below) = l_temp1;
13757710Sbostic 			u_add_stk(&(l_temp1->above));
13857710Sbostic 			(l_temp1->above) = nn_max_end;
13957710Sbostic 		}
14057710Sbostic 	change_flag = 1;
141*58315Sbostic 	sigspecial--;
142*58315Sbostic 	if (sigint_flag && (!sigspecial))
143*58315Sbostic 		SIGINT_ACTION;
14457710Sbostic 	*errnum = 1;
14557710Sbostic 	ss = l_ss;
146*58315Sbostic 	sigspecial = 0;
14757710Sbostic 	return (l_ttl);
14857710Sbostic }
149