Home | History | Annotate | Line # | Download | only in ed
buf.c revision 1.1
      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.1  cgd extern line_t line0;
     53  1.1  cgd 
     54  1.1  cgd FILE *sfp;				/* scratch file pointer */
     55  1.1  cgd off_t sfseek;				/* scratch file position */
     56  1.1  cgd int seek_write;				/* seek before writing */
     57  1.1  cgd 
     58  1.1  cgd /* gettxt: get a line of text from the scratch file; return pointer
     59  1.1  cgd    to the text */
     60  1.1  cgd char *
     61  1.1  cgd gettxt(lp)
     62  1.1  cgd 	line_t *lp;
     63  1.1  cgd {
     64  1.1  cgd 	static char txtbuf[MAXLINE];
     65  1.1  cgd 	int len, ct;
     66  1.1  cgd 
     67  1.1  cgd 	if (lp == &line0)
     68  1.1  cgd 		return NULL;
     69  1.1  cgd 	seek_write = 1;				/* force seek on write */
     70  1.1  cgd 	/* out of position */
     71  1.1  cgd 	if (sfseek != lp->seek) {
     72  1.1  cgd 		sfseek = lp->seek;
     73  1.1  cgd 		if (fseek(sfp, sfseek, SEEK_SET) < 0) {
     74  1.1  cgd 			fprintf(stderr, "cannot seek temp file\n");
     75  1.1  cgd 			return (char *) ERR;
     76  1.1  cgd 		}
     77  1.1  cgd 	}
     78  1.1  cgd 	len = lp->len & ~ACTV;
     79  1.1  cgd 	if ((ct = fread(txtbuf, sizeof(char), len, sfp)) <  0 || ct != len) {
     80  1.1  cgd 		fprintf(stderr, "cannot read temp file\n");
     81  1.1  cgd 		return (char *) ERR;
     82  1.1  cgd 	}
     83  1.1  cgd 	sfseek += len;				/* update file position */
     84  1.1  cgd 	txtbuf[len] = '\0';
     85  1.1  cgd 	return txtbuf;
     86  1.1  cgd }
     87  1.1  cgd 
     88  1.1  cgd 
     89  1.1  cgd extern long curln;
     90  1.1  cgd extern long lastln;
     91  1.1  cgd 
     92  1.1  cgd /* puttxt: write a line of text to the scratch file and add a line node
     93  1.1  cgd    to the editor buffer;  return a pointer to the end of the text */
     94  1.1  cgd char *
     95  1.1  cgd puttxt(cs)
     96  1.1  cgd 	char *cs;
     97  1.1  cgd {
     98  1.1  cgd 	line_t *lp;
     99  1.1  cgd 	int len, ct;
    100  1.1  cgd 	char *s;
    101  1.1  cgd 
    102  1.1  cgd 	if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
    103  1.1  cgd 		fprintf(stderr, "out of memory\n");
    104  1.1  cgd 		return (char *) ERR;
    105  1.1  cgd 	}
    106  1.1  cgd 	/* assert: cs is '\n' terminated */
    107  1.1  cgd 	for (s = cs; *s != '\n'; s++)
    108  1.1  cgd 		;
    109  1.1  cgd 	len = (s - cs) & ~ACTV;
    110  1.1  cgd 	/* out of position */
    111  1.1  cgd 	if (seek_write) {
    112  1.1  cgd 		if (fseek(sfp, 0L, SEEK_END) < 0) {
    113  1.1  cgd 			fprintf(stderr, "cannot seek temp file\n");
    114  1.1  cgd 			return (char *) ERR;
    115  1.1  cgd 		}
    116  1.1  cgd 		sfseek = ftell(sfp);
    117  1.1  cgd 		seek_write = 0;
    118  1.1  cgd 	}
    119  1.1  cgd 	/* assert: spl1() */
    120  1.1  cgd 	if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
    121  1.1  cgd 		sfseek = -1;
    122  1.1  cgd 		fprintf(stderr, "cannot write temp file\n");
    123  1.1  cgd 		return (char *) ERR;
    124  1.1  cgd 	}
    125  1.1  cgd 	lp->len = len;
    126  1.1  cgd 	lp->seek  = sfseek;
    127  1.1  cgd 	lpqueue(lp);
    128  1.1  cgd 	sfseek += len;			/* update file position */
    129  1.1  cgd 	return (*++s == '\0') ? NULL : s;
    130  1.1  cgd }
    131  1.1  cgd 
    132  1.1  cgd 
    133  1.1  cgd /* lpqueue: add a line node in the editor buffer after the current line */
    134  1.1  cgd void
    135  1.1  cgd lpqueue(lp)
    136  1.1  cgd 	line_t *lp;
    137  1.1  cgd {
    138  1.1  cgd 	line_t *cp;
    139  1.1  cgd 
    140  1.1  cgd 	cp = getptr(curln);				/* this getptr last! */
    141  1.1  cgd 	insqueue(lp, cp);
    142  1.1  cgd 	lastln++;
    143  1.1  cgd 	curln++;
    144  1.1  cgd }
    145  1.1  cgd 
    146  1.1  cgd 
    147  1.1  cgd extern int mutex;
    148  1.1  cgd extern int sigflags;
    149  1.1  cgd 
    150  1.1  cgd /* getptr: return pointer to a line node in the editor buffer */
    151  1.1  cgd line_t *
    152  1.1  cgd getptr(n)
    153  1.1  cgd 	long n;
    154  1.1  cgd {
    155  1.1  cgd 	static line_t *lp = &line0;
    156  1.1  cgd 	static long on = 0;
    157  1.1  cgd 
    158  1.1  cgd 	spl1();
    159  1.1  cgd 	if (n > on)
    160  1.1  cgd 		if (n <= (on + lastln) >> 1) {
    161  1.1  cgd 			for (; on < n; on++)
    162  1.1  cgd 				lp = lp->next;
    163  1.1  cgd 		} else {
    164  1.1  cgd 			lp = line0.prev;
    165  1.1  cgd 			for (on = lastln; on > n; on--)
    166  1.1  cgd 				lp = lp->prev;
    167  1.1  cgd 		}
    168  1.1  cgd 	else
    169  1.1  cgd 		if (n >= on >> 1) {
    170  1.1  cgd 			for (; on > n; on--)
    171  1.1  cgd 				lp = lp->prev;
    172  1.1  cgd 		} else {
    173  1.1  cgd 			lp = &line0;
    174  1.1  cgd 			for (on = 0; on < n; on++)
    175  1.1  cgd 				lp = lp->next;
    176  1.1  cgd 		}
    177  1.1  cgd 	spl0();
    178  1.1  cgd 	return lp;
    179  1.1  cgd }
    180  1.1  cgd 
    181  1.1  cgd 
    182  1.1  cgd char sfn[15] = "";				/* scratch file name */
    183  1.1  cgd 
    184  1.1  cgd /* sbopen: open scratch file */
    185  1.1  cgd sbopen()
    186  1.1  cgd {
    187  1.1  cgd 	strcpy(sfn, "/tmp/ed.XXXXXX");
    188  1.1  cgd 	if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
    189  1.1  cgd 		fprintf(stderr, "cannot open temp file\n");
    190  1.1  cgd 		return ERR;
    191  1.1  cgd 	}
    192  1.1  cgd 	return 0;
    193  1.1  cgd }
    194  1.1  cgd 
    195  1.1  cgd /* sbclose: close scratch file */
    196  1.1  cgd sbclose()
    197  1.1  cgd {
    198  1.1  cgd 	if (sfp) {
    199  1.1  cgd 		unlink(sfn);
    200  1.1  cgd 		fclose(sfp);
    201  1.1  cgd 		sfp = NULL;
    202  1.1  cgd 	}
    203  1.1  cgd 	sfseek = seek_write = 0;
    204  1.1  cgd }
    205  1.1  cgd 
    206  1.1  cgd 
    207  1.1  cgd /* quit: remove scratch file and exit */
    208  1.1  cgd void
    209  1.1  cgd quit(n)
    210  1.1  cgd 	int n;
    211  1.1  cgd {
    212  1.1  cgd 	if (sfp) {
    213  1.1  cgd 		unlink(sfn);
    214  1.1  cgd 		fclose(sfp);
    215  1.1  cgd 	}
    216  1.1  cgd 	exit(n);
    217  1.1  cgd }
    218