xref: /csrg-svn/contrib/ed/input_lines.c (revision 58355)
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*58355Sbostic static char sccsid[] = "@(#)input_lines.c	5.6 (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 
23*58355Sbostic #ifdef DBI
24*58355Sbostic #include <db.h>
25*58355Sbostic #endif
26*58355Sbostic 
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;
4558351Sbostic 	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 
5458315Sbostic 	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 	}
6458315Sbostic 	sigspecial--;
6558315Sbostic 	if (sigint_flag && (!sigspecial))
6658315Sbostic 		SIGINT_ACTION;
6757689Sbostic 
6858315Sbostic 	sigspecial++;
6958351Sbostic         if (l_jmp_flag = setjmp(ctrl_position3))
7058351Sbostic                 goto point;
7158315Sbostic 
7257710Sbostic 	for (;;) {
7358315Sbostic 		if (sigint_flag && (!sigspecial))
7458315Sbostic 			goto point;
7558351Sbostic         	sigspecial3 = 1;
7658315Sbostic 		l_ss = getc(fp);
7758351Sbostic         	sigspecial3 = 0;
7858315Sbostic 		if (l_ss == EOF) {
7958315Sbostic 			if (add_flag) {
8058315Sbostic 				l_text[l_nn++] = '\0';
8158315Sbostic 				goto eof_mk;
8258315Sbostic 			}
8357710Sbostic 			break;
8458315Sbostic 		}
8558351Sbostic 		if (!l_ss) /* 8-bit okay, but NULL not */
8657710Sbostic 			continue;
8758315Sbostic 		l_text[l_nn++] = (char)l_ss;
8857710Sbostic 		if (l_ss == '\n') {
8958315Sbostic 			if (sigint_flag && (!sigspecial))
9058315Sbostic 				goto point;
9157710Sbostic 			l_text[l_nn - 1] = '\0';
9257710Sbostic 			if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
9357710Sbostic 				break;
9458315Sbostic eof_mk:
9558351Sbostic 			nn_max_end = (LINE *)malloc(sizeof(LINE));
9657710Sbostic 			if (nn_max_end == NULL) {
9757710Sbostic 				*errnum = -1;
9857710Sbostic 				strcpy(help_msg, "out of memory error");
9957710Sbostic 				return (0L);
10057710Sbostic 			}
10157710Sbostic 			(nn_max_end->len) = l_nn - 1;
10257710Sbostic 			(nn_max_end->handle) = add_line(l_text, l_nn - 1);
10357710Sbostic 			(nn_max_end->above) = l_temp_line;
10457710Sbostic 			(nn_max_end->below) = NULL;
10557710Sbostic 			if (l_temp_line)
10657710Sbostic 				(l_temp_line->below) = nn_max_end;
10757710Sbostic 			else
10857710Sbostic 				top = nn_max_end;
10957710Sbostic 			l_temp_line = nn_max_end;
11057689Sbostic 
11157710Sbostic 			l_ttl += l_nn;
11257710Sbostic 			l_nn = 0;
11358315Sbostic 			if (l_ss == EOF)
11458315Sbostic 				break;
11557710Sbostic 		} else
11657710Sbostic 			if (l_nn > l_nn_max) {
11757710Sbostic 				l_nn_max += 512;
11857710Sbostic 				nn_max = l_nn_max;
11957710Sbostic 				l_text2 = l_text;
12057710Sbostic 				l_text = text =
12157710Sbostic 				    calloc(l_nn_max + 2, sizeof(char));
12257710Sbostic 				if (text == NULL) {
12357710Sbostic 					*errnum = -1;
12457710Sbostic 					strcpy(help_msg, "out of memory error");
12557710Sbostic 					return (0L);
12657710Sbostic 				}
12757710Sbostic 				bcopy(l_text2, l_text, l_nn);
12857710Sbostic 				free(l_text2);
12957710Sbostic 			}
13057710Sbostic 	}
13157689Sbostic 
13257710Sbostic point:	current = nn_max_end;
13357710Sbostic 	if (current == NULL)
13457710Sbostic 		current = top;
13557710Sbostic 	if (l_temp1 == NULL)
13657710Sbostic 		bottom = nn_max_end;
13757710Sbostic 	else
13857710Sbostic 		if (nn_max_end != NULL) {
13957710Sbostic 			(nn_max_end->below) = l_temp1;
14057710Sbostic 			u_add_stk(&(l_temp1->above));
14157710Sbostic 			(l_temp1->above) = nn_max_end;
14257710Sbostic 		}
14357710Sbostic 	change_flag = 1;
14458315Sbostic 	sigspecial--;
14558351Sbostic 	sigspecial3 = 0;
14658315Sbostic 	if (sigint_flag && (!sigspecial))
14758315Sbostic 		SIGINT_ACTION;
14857710Sbostic 	*errnum = 1;
14957710Sbostic 	ss = l_ss;
15057710Sbostic 	return (l_ttl);
15157710Sbostic }
152