xref: /original-bsd/contrib/ed/get_line.c (revision f5b1856a)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  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[] = "@(#)get_line.c	8.1 (Berkeley) 05/31/93";
13 #endif /* not lint */
14 
15 #include <sys/types.h>
16 
17 #include <regex.h>
18 #include <setjmp.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #ifdef DBI
23 #include <db.h>
24 #endif
25 
26 #include "ed.h"
27 #include "extern.h"
28 
29 /*
30  * Get the specified line from the buffer. Note that in the buffer
31  * we stored lengths of text, not strings (NULL terminated, except
32  * under MEMORY). So we want to put a terminating NULL in for
33  * whatever command is going to be handling the line.
34  */
35 
36 /* these variables are here (instead of main and ed.h) because they
37  * are only used with the buffer when run under STDIO. STDIO is a
38  * resource pig with most of the OS's I've tested this with. The
39  * use of these variables improves performance up to 100% in several
40  * cases. The piggyness is thus: fseek causes the current STDIO buf
41  * to be flushed out and a new one read in...even when it is not necessary!
42  * Read 512 (or 1024) when you don't have to for almost every access
43  * and you'll slow down too. So these variable are used to control unneeded
44  * fseeks.
45  * I've been told the newer BSD STDIO has fixed this, but don't
46  * currently have a copy.
47  */
48 int file_loc=0;
49 
50 /* Get a particular line of length len from ed's buffer and place it in
51  * 'text', the standard repository for the "current" line.
52  */
53 void
54 get_line(loc, len)
55 #ifdef STDIO
56 	long loc;
57 #endif
58 #ifdef DBI
59 	recno_t loc;
60 #endif
61 #ifdef MEMORY
62 	char *loc;
63 #endif
64 	int len;
65 {
66 #ifdef DBI
67 	DBT db_key, db_data;
68 #endif
69 	int l_jmp_flag;
70 
71 	if (l_jmp_flag = setjmp(ctrl_position2)) {
72 		text[0] = '\0';
73 		return;
74 	}
75 
76 	sigspecial2 = 1;
77 #ifdef STDIO
78 
79 	if (loc == 0L) {
80 		text[0] = '\0';
81 		return;
82 	}
83 	if (file_loc != loc)
84 		fseek(fhtmp, loc, 0);
85 	file_seek = 1;
86 	file_loc = loc + fread(text, sizeof(char), len, fhtmp);
87 #endif
88 
89 #ifdef DBI
90 	if (loc == (recno_t)0) {
91 		text[0] = '\0';
92 		return;
93 	}
94 	(db_key.data) = &loc;
95 	(db_key.size) = sizeof(recno_t);
96 	(dbhtmp->get) (dbhtmp, &db_key, &db_data, (u_int) 0);
97 	strcpy(text, db_data.data);
98 #endif
99 
100 #ifdef MEMORY
101         if (loc == (char *)NULL) {
102                 text[0] = '\0';
103                 return;
104         }
105 	memmove(text, loc, len);
106 #endif
107 	text[len] = '\0';
108 	sigspecial2 = 0;
109 }
110