Home | History | Annotate | Line # | Download | only in ed
buf.c revision 1.2
      1 /* buf.c: This file contains the scratch-file buffer rountines for the
      2    ed line editor. */
      3 /*-
      4  * Copyright (c) 1992 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rodney Ruddock of the University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 static char sccsid[] = "@(#)buf.c	5.5 (Berkeley) 3/28/93";
     41 #endif /* not lint */
     42 
     43 #include <signal.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <sys/file.h>
     48 #include <unistd.h>
     49 
     50 #include "ed.h"
     51 
     52 extern char errmsg[];
     53 extern line_t line0;
     54 
     55 FILE *sfp;				/* scratch file pointer */
     56 off_t sfseek;				/* scratch file position */
     57 int seek_write;				/* seek before writing */
     58 
     59 /* gettxt: get a line of text from the scratch file; return pointer
     60    to the text */
     61 char *
     62 gettxt(lp)
     63 	line_t *lp;
     64 {
     65 	static char txtbuf[MAXLINE];
     66 	int len, ct;
     67 
     68 	if (lp == &line0)
     69 		return NULL;
     70 	seek_write = 1;				/* force seek on write */
     71 	/* out of position */
     72 	if (sfseek != lp->seek) {
     73 		sfseek = lp->seek;
     74 		if (fseek(sfp, sfseek, SEEK_SET) < 0) {
     75 			sprintf(errmsg, "cannot seek temp file");
     76 			return (char *) ERR;
     77 		}
     78 	}
     79 	len = lp->len & ~ACTV;
     80 	if ((ct = fread(txtbuf, sizeof(char), len, sfp)) <  0 || ct != len) {
     81 		sprintf(errmsg, "cannot read temp file");
     82 		return (char *) ERR;
     83 	}
     84 	sfseek += len;				/* update file position */
     85 	txtbuf[len] = '\0';
     86 	return txtbuf;
     87 }
     88 
     89 
     90 extern long curln;
     91 extern long lastln;
     92 
     93 /* puttxt: write a line of text to the scratch file and add a line node
     94    to the editor buffer;  return a pointer to the end of the text */
     95 char *
     96 puttxt(cs)
     97 	char *cs;
     98 {
     99 	line_t *lp;
    100 	int len, ct;
    101 	char *s;
    102 
    103 	if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
    104 		sprintf(errmsg, "out of memory");
    105 		return (char *) ERR;
    106 	}
    107 	/* assert: cs is '\n' terminated */
    108 	for (s = cs; *s != '\n'; s++)
    109 		;
    110 	len = (s - cs) & ~ACTV;
    111 	/* out of position */
    112 	if (seek_write) {
    113 		if (fseek(sfp, 0L, SEEK_END) < 0) {
    114 			sprintf(errmsg, "cannot seek temp file");
    115 			return (char *) ERR;
    116 		}
    117 		sfseek = ftell(sfp);
    118 		seek_write = 0;
    119 	}
    120 	/* assert: spl1() */
    121 	if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
    122 		sfseek = -1;
    123 		sprintf(errmsg, "cannot write temp file");
    124 		return (char *) ERR;
    125 	}
    126 	lp->len = len;
    127 	lp->seek  = sfseek;
    128 	lpqueue(lp);
    129 	sfseek += len;			/* update file position */
    130 	return (*++s == '\0') ? NULL : s;
    131 }
    132 
    133 
    134 /* lpqueue: add a line node in the editor buffer after the current line */
    135 void
    136 lpqueue(lp)
    137 	line_t *lp;
    138 {
    139 	line_t *cp;
    140 
    141 	cp = getptr(curln);				/* this getptr last! */
    142 	insqueue(lp, cp);
    143 	lastln++;
    144 	curln++;
    145 }
    146 
    147 
    148 extern int mutex;
    149 extern int sigflags;
    150 
    151 /* getptr: return pointer to a line node in the editor buffer */
    152 line_t *
    153 getptr(n)
    154 	long n;
    155 {
    156 	static line_t *lp = &line0;
    157 	static long on = 0;
    158 
    159 	spl1();
    160 	if (n > on)
    161 		if (n <= (on + lastln) >> 1)
    162 			for (; on < n; on++)
    163 				lp = lp->next;
    164 		else {
    165 			lp = line0.prev;
    166 			for (on = lastln; on > n; on--)
    167 				lp = lp->prev;
    168 		}
    169 	else
    170 		if (n >= on >> 1)
    171 			for (; on > n; on--)
    172 				lp = lp->prev;
    173 		else {
    174 			lp = &line0;
    175 			for (on = 0; on < n; on++)
    176 				lp = lp->next;
    177 		}
    178 	spl0();
    179 	return lp;
    180 }
    181 
    182 
    183 char sfn[15] = "";				/* scratch file name */
    184 
    185 /* sbopen: open scratch file */
    186 sbopen()
    187 {
    188 	strcpy(sfn, "/tmp/ed.XXXXXX");
    189 	if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
    190 		sprintf(errmsg, "cannot open temp file");
    191 		return ERR;
    192 	}
    193 	return 0;
    194 }
    195 
    196 
    197 /* sbclose: close scratch file */
    198 sbclose()
    199 {
    200 	if (sfp) {
    201 		fclose(sfp);
    202 		sfp = NULL;
    203 		unlink(sfn);
    204 	}
    205 	sfseek = seek_write = 0;
    206 }
    207 
    208 
    209 /* quit: remove scratch file and exit */
    210 void
    211 quit(n)
    212 	int n;
    213 {
    214 	if (sfp) {
    215 		unlink(sfn);
    216 		fclose(sfp);
    217 	}
    218 	exit(n);
    219 }
    220