xref: /csrg-svn/contrib/ed/input_lines.c (revision 58351)
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*58351Sbostic static char sccsid[] = "@(#)input_lines.c	5.5 (Berkeley) 03/01/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 
2357689Sbostic #include "ed.h"
2457710Sbostic #include "extern.h"
2557689Sbostic 
2657689Sbostic /*
2757689Sbostic  * This central function gets text from some file, which can (and most
2857689Sbostic  * oft is) stdin. This flexability allows any text inputing function
2957689Sbostic  * to use it.
3057689Sbostic  */
3157689Sbostic long
3257689Sbostic input_lines(fp, errnum)
3357710Sbostic 	FILE *fp;
3457710Sbostic 	int *errnum;
3557689Sbostic {
3657710Sbostic 	register long l_nn = 0;
3757710Sbostic 	register int l_ss = ss;
3857710Sbostic 	register char *l_text = text;
3957710Sbostic 	LINE *l_temp_line, *l_temp1;
4057710Sbostic 	long l_ttl = 0;
41*58351Sbostic 	int l_nn_max = nn_max, l_jmp_flag;
4257710Sbostic 	char *l_text2;
4357689Sbostic 
4457710Sbostic 	if (End_default)
4557710Sbostic 		start = current;
4657710Sbostic 	else
4757710Sbostic 		start = End;
4857710Sbostic 	start_default = End_default = 0;
4957689Sbostic 
5058315Sbostic 	sigspecial++;
5157710Sbostic 	/* start == NULL means line 0 which is legal for this function only. */
5257710Sbostic 	nn_max_end = l_temp_line = start;
5357710Sbostic 	if (start == NULL) {
5457710Sbostic 		l_temp1 = top;
5557710Sbostic 		u_add_stk(&(top->above));
5657710Sbostic 	} else {
5757710Sbostic 		u_add_stk(&(start->below));
5857710Sbostic 		l_temp1 = start->below;
5957710Sbostic 	}
6058315Sbostic 	sigspecial--;
6158315Sbostic 	if (sigint_flag && (!sigspecial))
6258315Sbostic 		SIGINT_ACTION;
6357689Sbostic 
6458315Sbostic 	sigspecial++;
65*58351Sbostic         if (l_jmp_flag = setjmp(ctrl_position3))
66*58351Sbostic                 goto point;
6758315Sbostic 
6857710Sbostic 	for (;;) {
6958315Sbostic 		if (sigint_flag && (!sigspecial))
7058315Sbostic 			goto point;
71*58351Sbostic         	sigspecial3 = 1;
7258315Sbostic 		l_ss = getc(fp);
73*58351Sbostic         	sigspecial3 = 0;
7458315Sbostic 		if (l_ss == EOF) {
7558315Sbostic 			if (add_flag) {
7658315Sbostic 				l_text[l_nn++] = '\0';
7758315Sbostic 				goto eof_mk;
7858315Sbostic 			}
7957710Sbostic 			break;
8058315Sbostic 		}
81*58351Sbostic 		if (!l_ss) /* 8-bit okay, but NULL not */
8257710Sbostic 			continue;
8358315Sbostic 		l_text[l_nn++] = (char)l_ss;
8457710Sbostic 		if (l_ss == '\n') {
8558315Sbostic 			if (sigint_flag && (!sigspecial))
8658315Sbostic 				goto point;
8757710Sbostic 			l_text[l_nn - 1] = '\0';
8857710Sbostic 			if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
8957710Sbostic 				break;
9058315Sbostic eof_mk:
91*58351Sbostic 			nn_max_end = (LINE *)malloc(sizeof(LINE));
9257710Sbostic 			if (nn_max_end == NULL) {
9357710Sbostic 				*errnum = -1;
9457710Sbostic 				strcpy(help_msg, "out of memory error");
9557710Sbostic 				return (0L);
9657710Sbostic 			}
9757710Sbostic 			(nn_max_end->len) = l_nn - 1;
9857710Sbostic 			(nn_max_end->handle) = add_line(l_text, l_nn - 1);
9957710Sbostic 			(nn_max_end->above) = l_temp_line;
10057710Sbostic 			(nn_max_end->below) = NULL;
10157710Sbostic 			if (l_temp_line)
10257710Sbostic 				(l_temp_line->below) = nn_max_end;
10357710Sbostic 			else
10457710Sbostic 				top = nn_max_end;
10557710Sbostic 			l_temp_line = nn_max_end;
10657689Sbostic 
10757710Sbostic 			l_ttl += l_nn;
10857710Sbostic 			l_nn = 0;
10958315Sbostic 			if (l_ss == EOF)
11058315Sbostic 				break;
11157710Sbostic 		} else
11257710Sbostic 			if (l_nn > l_nn_max) {
11357710Sbostic 				l_nn_max += 512;
11457710Sbostic 				nn_max = l_nn_max;
11557710Sbostic 				l_text2 = l_text;
11657710Sbostic 				l_text = text =
11757710Sbostic 				    calloc(l_nn_max + 2, sizeof(char));
11857710Sbostic 				if (text == NULL) {
11957710Sbostic 					*errnum = -1;
12057710Sbostic 					strcpy(help_msg, "out of memory error");
12157710Sbostic 					return (0L);
12257710Sbostic 				}
12357710Sbostic 				bcopy(l_text2, l_text, l_nn);
12457710Sbostic 				free(l_text2);
12557710Sbostic 			}
12657710Sbostic 	}
12757689Sbostic 
12857710Sbostic point:	current = nn_max_end;
12957710Sbostic 	if (current == NULL)
13057710Sbostic 		current = top;
13157710Sbostic 	if (l_temp1 == NULL)
13257710Sbostic 		bottom = nn_max_end;
13357710Sbostic 	else
13457710Sbostic 		if (nn_max_end != NULL) {
13557710Sbostic 			(nn_max_end->below) = l_temp1;
13657710Sbostic 			u_add_stk(&(l_temp1->above));
13757710Sbostic 			(l_temp1->above) = nn_max_end;
13857710Sbostic 		}
13957710Sbostic 	change_flag = 1;
14058315Sbostic 	sigspecial--;
141*58351Sbostic 	sigspecial3 = 0;
14258315Sbostic 	if (sigint_flag && (!sigspecial))
14358315Sbostic 		SIGINT_ACTION;
14457710Sbostic 	*errnum = 1;
14557710Sbostic 	ss = l_ss;
14657710Sbostic 	return (l_ttl);
14757710Sbostic }
148