xref: /csrg-svn/contrib/ed/get_line.c (revision 60663)
157687Sbostic /*-
2*60663Sbostic  * Copyright (c) 1992, 1993
3*60663Sbostic  *	The Regents of the University of California.  All rights reserved.
457687Sbostic  *
557687Sbostic  * This code is derived from software contributed to Berkeley by
657687Sbostic  * Rodney Ruddock of the University of Guelph.
757687Sbostic  *
857687Sbostic  * %sccs.include.redist.c%
957687Sbostic  */
1057687Sbostic 
1157687Sbostic #ifndef lint
12*60663Sbostic static char sccsid[] = "@(#)get_line.c	8.1 (Berkeley) 05/31/93";
1357687Sbostic #endif /* not lint */
1457687Sbostic 
1557710Sbostic #include <sys/types.h>
1657710Sbostic 
1757710Sbostic #include <regex.h>
1857710Sbostic #include <setjmp.h>
1957710Sbostic #include <stdio.h>
2057710Sbostic #include <string.h>
2157710Sbostic 
2258315Sbostic #ifdef DBI
2358315Sbostic #include <db.h>
2458315Sbostic #endif
2558315Sbostic 
2657687Sbostic #include "ed.h"
2757710Sbostic #include "extern.h"
2857687Sbostic 
2957687Sbostic /*
3057687Sbostic  * Get the specified line from the buffer. Note that in the buffer
3157687Sbostic  * we stored lengths of text, not strings (NULL terminated, except
3257687Sbostic  * under MEMORY). So we want to put a terminating NULL in for
3357687Sbostic  * whatever command is going to be handling the line.
3457687Sbostic  */
3557687Sbostic 
3657687Sbostic /* these variables are here (instead of main and ed.h) because they
3757687Sbostic  * are only used with the buffer when run under STDIO. STDIO is a
3857687Sbostic  * resource pig with most of the OS's I've tested this with. The
3957687Sbostic  * use of these variables improves performance up to 100% in several
4057687Sbostic  * cases. The piggyness is thus: fseek causes the current STDIO buf
4157687Sbostic  * to be flushed out and a new one read in...even when it is not necessary!
4257687Sbostic  * Read 512 (or 1024) when you don't have to for almost every access
4357687Sbostic  * and you'll slow down too. So these variable are used to control unneeded
4457687Sbostic  * fseeks.
4557687Sbostic  * I've been told the newer BSD STDIO has fixed this, but don't
4657687Sbostic  * currently have a copy.
4757687Sbostic  */
4857687Sbostic int file_loc=0;
4957687Sbostic 
5057687Sbostic /* Get a particular line of length len from ed's buffer and place it in
5157687Sbostic  * 'text', the standard repository for the "current" line.
5257687Sbostic  */
5357687Sbostic void
5457687Sbostic get_line(loc, len)
5558315Sbostic #ifdef STDIO
5658315Sbostic 	long loc;
5758315Sbostic #endif
5858315Sbostic #ifdef DBI
5957710Sbostic 	recno_t loc;
6058315Sbostic #endif
6158315Sbostic #ifdef MEMORY
6258315Sbostic 	char *loc;
6358315Sbostic #endif
6457710Sbostic 	int len;
6557687Sbostic {
6658315Sbostic #ifdef DBI
6757710Sbostic 	DBT db_key, db_data;
6858315Sbostic #endif
6958315Sbostic 	int l_jmp_flag;
7057687Sbostic 
7158315Sbostic 	if (l_jmp_flag = setjmp(ctrl_position2)) {
7258315Sbostic 		text[0] = '\0';
7358315Sbostic 		return;
7458315Sbostic 	}
7558315Sbostic 
7658315Sbostic 	sigspecial2 = 1;
7758315Sbostic #ifdef STDIO
7858315Sbostic 
7958315Sbostic 	if (loc == 0L) {
8058315Sbostic 		text[0] = '\0';
8158315Sbostic 		return;
8258315Sbostic 	}
8358315Sbostic 	if (file_loc != loc)
8458315Sbostic 		fseek(fhtmp, loc, 0);
8558315Sbostic 	file_seek = 1;
8658315Sbostic 	file_loc = loc + fread(text, sizeof(char), len, fhtmp);
8758315Sbostic #endif
8858315Sbostic 
8958315Sbostic #ifdef DBI
9058315Sbostic 	if (loc == (recno_t)0) {
9158315Sbostic 		text[0] = '\0';
9258315Sbostic 		return;
9358315Sbostic 	}
9457710Sbostic 	(db_key.data) = &loc;
9557710Sbostic 	(db_key.size) = sizeof(recno_t);
9657710Sbostic 	(dbhtmp->get) (dbhtmp, &db_key, &db_data, (u_int) 0);
9757710Sbostic 	strcpy(text, db_data.data);
9858315Sbostic #endif
9958315Sbostic 
10058315Sbostic #ifdef MEMORY
10158315Sbostic         if (loc == (char *)NULL) {
10258315Sbostic                 text[0] = '\0';
10358315Sbostic                 return;
10458315Sbostic         }
10559482Sbostic 	memmove(text, loc, len);
10658315Sbostic #endif
10757710Sbostic 	text[len] = '\0';
10858315Sbostic 	sigspecial2 = 0;
10957710Sbostic }
110