xref: /csrg-svn/contrib/ed/input_lines.c (revision 60663)
157689Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  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*60663Sbostic static char sccsid[] = "@(#)input_lines.c	8.1 (Berkeley) 05/31/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 
2358355Sbostic #ifdef DBI
2458355Sbostic #include <db.h>
2558355Sbostic #endif
2658355Sbostic 
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
input_lines(fp,errnum)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)
4958564Sralph 		Start = current;
5057710Sbostic 	else
5158564Sralph 		Start = End;
5258564Sralph 	Start_default = End_default = 0;
5357689Sbostic 
5458315Sbostic 	sigspecial++;
5558564Sralph 	/* Start == NULL means line 0 which is legal for this function only. */
5658564Sralph 	nn_max_end = l_temp_line = Start;
5758564Sralph 	if (Start == NULL) {
5857710Sbostic 		l_temp1 = top;
5957710Sbostic 		u_add_stk(&(top->above));
6057710Sbostic 	} else {
6158564Sralph 		u_add_stk(&(Start->below));
6258564Sralph 		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 (;;) {
7358710Sbostic 		if (sigint_flag)
7458315Sbostic 			goto point;
7558351Sbostic         	sigspecial3 = 1;
7658315Sbostic 		l_ss = getc(fp);
7758351Sbostic         	sigspecial3 = 0;
7858315Sbostic 		if (l_ss == EOF) {
7959475Sbostic 				clearerr(fp);
8059475Sbostic 				if (l_nn) {
8159475Sbostic 					printf("<newline> added at end of line\n");
8259475Sbostic 					l_nn++;
8359475Sbostic 					goto eof_mk;
8459475Sbostic 				}
8557710Sbostic 			break;
8658315Sbostic 		}
8758315Sbostic 		l_text[l_nn++] = (char)l_ss;
8857710Sbostic 		if (l_ss == '\n') {
8958710Sbostic 			if (sigint_flag)
9058315Sbostic 				goto point;
9159475Sbostic eof_mk:
9257710Sbostic 			l_text[l_nn - 1] = '\0';
9357710Sbostic 			if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
9457710Sbostic 				break;
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 				}
12759483Sbostic 				memmove(l_text, l_text2, 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