improve exit status handling for program map code
[unix-history] / usr / src / contrib / ed / input_lines.c
CommitLineData
9a4ab09a 1/*-
ba5e8546
KB
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
9a4ab09a
KB
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
ba5e8546 12static char sccsid[] = "@(#)input_lines.c 8.1 (Berkeley) %G%";
9a4ab09a
KB
13#endif /* not lint */
14
ecbf4ad0
KB
15#include <sys/types.h>
16
ecbf4ad0
KB
17#include <regex.h>
18#include <setjmp.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
9a4ab09a 22
31e0ca30
KB
23#ifdef DBI
24#include <db.h>
25#endif
26
ecbf4ad0
KB
27#include "ed.h"
28#include "extern.h"
9a4ab09a
KB
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 */
9a4ab09a
KB
35long
36input_lines(fp, errnum)
ecbf4ad0
KB
37 FILE *fp;
38 int *errnum;
9a4ab09a 39{
ecbf4ad0
KB
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;
c842a051 45 int l_nn_max = nn_max, l_jmp_flag;
ecbf4ad0 46 char *l_text2;
9a4ab09a 47
ecbf4ad0 48 if (End_default)
035e94f8 49 Start = current;
ecbf4ad0 50 else
035e94f8
RC
51 Start = End;
52 Start_default = End_default = 0;
9a4ab09a 53
e692f66f 54 sigspecial++;
035e94f8
RC
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) {
ecbf4ad0
KB
58 l_temp1 = top;
59 u_add_stk(&(top->above));
60 } else {
035e94f8
RC
61 u_add_stk(&(Start->below));
62 l_temp1 = Start->below;
ecbf4ad0 63 }
e692f66f
KB
64 sigspecial--;
65 if (sigint_flag && (!sigspecial))
66 SIGINT_ACTION;
67
68 sigspecial++;
c842a051
KB
69 if (l_jmp_flag = setjmp(ctrl_position3))
70 goto point;
9a4ab09a 71
ecbf4ad0 72 for (;;) {
672cc1c0 73 if (sigint_flag)
e692f66f 74 goto point;
c842a051 75 sigspecial3 = 1;
e692f66f 76 l_ss = getc(fp);
c842a051 77 sigspecial3 = 0;
e692f66f 78 if (l_ss == EOF) {
afdef93a
KB
79 clearerr(fp);
80 if (l_nn) {
81 printf("<newline> added at end of line\n");
82 l_nn++;
83 goto eof_mk;
84 }
ecbf4ad0 85 break;
e692f66f 86 }
e692f66f 87 l_text[l_nn++] = (char)l_ss;
ecbf4ad0 88 if (l_ss == '\n') {
672cc1c0 89 if (sigint_flag)
e692f66f 90 goto point;
afdef93a 91eof_mk:
ecbf4ad0
KB
92 l_text[l_nn - 1] = '\0';
93 if ((l_nn == 2) && (l_text[0] == '.') && add_flag)
94 break;
c842a051 95 nn_max_end = (LINE *)malloc(sizeof(LINE));
ecbf4ad0
KB
96 if (nn_max_end == NULL) {
97 *errnum = -1;
98 strcpy(help_msg, "out of memory error");
99 return (0L);
100 }
101 (nn_max_end->len) = l_nn - 1;
102 (nn_max_end->handle) = add_line(l_text, l_nn - 1);
103 (nn_max_end->above) = l_temp_line;
104 (nn_max_end->below) = NULL;
105 if (l_temp_line)
106 (l_temp_line->below) = nn_max_end;
107 else
108 top = nn_max_end;
109 l_temp_line = nn_max_end;
9a4ab09a 110
ecbf4ad0
KB
111 l_ttl += l_nn;
112 l_nn = 0;
e692f66f
KB
113 if (l_ss == EOF)
114 break;
ecbf4ad0
KB
115 } else
116 if (l_nn > l_nn_max) {
117 l_nn_max += 512;
118 nn_max = l_nn_max;
119 l_text2 = l_text;
120 l_text = text =
121 calloc(l_nn_max + 2, sizeof(char));
122 if (text == NULL) {
123 *errnum = -1;
124 strcpy(help_msg, "out of memory error");
125 return (0L);
126 }
080766e7 127 memmove(l_text, l_text2, l_nn);
ecbf4ad0
KB
128 free(l_text2);
129 }
130 }
9a4ab09a 131
ecbf4ad0
KB
132point: current = nn_max_end;
133 if (current == NULL)
134 current = top;
135 if (l_temp1 == NULL)
136 bottom = nn_max_end;
137 else
138 if (nn_max_end != NULL) {
139 (nn_max_end->below) = l_temp1;
140 u_add_stk(&(l_temp1->above));
141 (l_temp1->above) = nn_max_end;
142 }
143 change_flag = 1;
e692f66f 144 sigspecial--;
c842a051 145 sigspecial3 = 0;
e692f66f
KB
146 if (sigint_flag && (!sigspecial))
147 SIGINT_ACTION;
ecbf4ad0
KB
148 *errnum = 1;
149 ss = l_ss;
150 return (l_ttl);
151}