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