1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * All rights reserved. 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 12 static char sccsid[] = "@(#)input_lines.c 5.2 (Berkeley) 01/23/93"; 13 #endif /* not lint */ 14 15 #include <sys/types.h> 16 17 #include <db.h> 18 #include <regex.h> 19 #include <setjmp.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 24 #include "ed.h" 25 #include "extern.h" 26 27 /* 28 * This central function gets text from some file, which can (and most 29 * oft is) stdin. This flexability allows any text inputing function 30 * to use it. 31 */ 32 long 33 input_lines(fp, errnum) 34 FILE *fp; 35 int *errnum; 36 { 37 register long l_nn = 0; 38 register int l_ss = ss; 39 register char *l_text = text; 40 LINE *l_temp_line, *l_temp1; 41 long l_ttl = 0; 42 int l_nn_max = nn_max; 43 char *l_text2; 44 45 if (End_default) 46 start = current; 47 else 48 start = End; 49 start_default = End_default = 0; 50 51 /* start == NULL means line 0 which is legal for this function only. */ 52 nn_max_end = l_temp_line = start; 53 if (start == NULL) { 54 l_temp1 = top; 55 u_add_stk(&(top->above)); 56 } else { 57 u_add_stk(&(start->below)); 58 l_temp1 = start->below; 59 } 60 61 for (;;) { 62 /* If NULL or bit-8 high not text; chuck. */ 63 if ((l_ss = getc(fp)) == EOF) 64 break; 65 if ((!l_ss) || (l_ss > 127)) 66 continue; 67 if (sigint_flag) 68 goto point; 69 l_text[l_nn++] = (char) l_ss; 70 if (l_ss == '\n') { 71 l_text[l_nn - 1] = '\0'; 72 if ((l_nn == 2) && (l_text[0] == '.') && add_flag) 73 break; 74 nn_max_end = malloc(sizeof(LINE)); 75 if (nn_max_end == NULL) { 76 *errnum = -1; 77 strcpy(help_msg, "out of memory error"); 78 return (0L); 79 } 80 (nn_max_end->len) = l_nn - 1; 81 (nn_max_end->handle) = add_line(l_text, l_nn - 1); 82 (nn_max_end->above) = l_temp_line; 83 (nn_max_end->below) = NULL; 84 if (l_temp_line) 85 (l_temp_line->below) = nn_max_end; 86 else 87 top = nn_max_end; 88 l_temp_line = nn_max_end; 89 90 l_ttl += l_nn; 91 l_nn = 0; 92 } else 93 if (l_nn > l_nn_max) { 94 l_nn_max += 512; 95 nn_max = l_nn_max; 96 l_text2 = l_text; 97 l_text = text = 98 calloc(l_nn_max + 2, sizeof(char)); 99 if (text == NULL) { 100 *errnum = -1; 101 strcpy(help_msg, "out of memory error"); 102 return (0L); 103 } 104 bcopy(l_text2, l_text, l_nn); 105 free(l_text2); 106 } 107 } 108 109 point: current = nn_max_end; 110 if (current == NULL) 111 current = top; 112 if (l_temp1 == NULL) 113 bottom = nn_max_end; 114 else 115 if (nn_max_end != NULL) { 116 (nn_max_end->below) = l_temp1; 117 u_add_stk(&(l_temp1->above)); 118 (l_temp1->above) = nn_max_end; 119 } 120 change_flag = 1; 121 *errnum = 1; 122 ss = l_ss; 123 return (l_ttl); 124 } 125