Home | History | Annotate | Line # | Download | only in ed
buf.c revision 1.5
      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.5  alm 	static char buf[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.3  alm 			fprintf(stderr, "%s\n", strerror(errno));
     76  1.2  cgd 			sprintf(errmsg, "cannot seek temp file");
     77  1.1  cgd 			return (char *) ERR;
     78  1.1  cgd 		}
     79  1.1  cgd 	}
     80  1.1  cgd 	len = lp->len & ~ACTV;
     81  1.5  alm 	if ((ct = fread(buf, sizeof(char), len, sfp)) <  0 || ct != len) {
     82  1.3  alm 		fprintf(stderr, "%s\n", strerror(errno));
     83  1.2  cgd 		sprintf(errmsg, "cannot read temp file");
     84  1.1  cgd 		return (char *) ERR;
     85  1.1  cgd 	}
     86  1.1  cgd 	sfseek += len;				/* update file position */
     87  1.5  alm 	buf[len] = '\0';
     88  1.5  alm 	return buf;
     89  1.1  cgd }
     90  1.1  cgd 
     91  1.1  cgd 
     92  1.1  cgd extern long curln;
     93  1.1  cgd extern long lastln;
     94  1.1  cgd 
     95  1.1  cgd /* puttxt: write a line of text to the scratch file and add a line node
     96  1.1  cgd    to the editor buffer;  return a pointer to the end of the text */
     97  1.1  cgd char *
     98  1.1  cgd puttxt(cs)
     99  1.1  cgd 	char *cs;
    100  1.1  cgd {
    101  1.1  cgd 	line_t *lp;
    102  1.1  cgd 	int len, ct;
    103  1.1  cgd 	char *s;
    104  1.1  cgd 
    105  1.1  cgd 	if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
    106  1.3  alm 		fprintf(stderr, "%s\n", strerror(errno));
    107  1.2  cgd 		sprintf(errmsg, "out of memory");
    108  1.1  cgd 		return (char *) ERR;
    109  1.1  cgd 	}
    110  1.1  cgd 	/* assert: cs is '\n' terminated */
    111  1.1  cgd 	for (s = cs; *s != '\n'; s++)
    112  1.1  cgd 		;
    113  1.1  cgd 	len = (s - cs) & ~ACTV;
    114  1.1  cgd 	/* out of position */
    115  1.1  cgd 	if (seek_write) {
    116  1.1  cgd 		if (fseek(sfp, 0L, SEEK_END) < 0) {
    117  1.3  alm 			fprintf(stderr, "%s\n", strerror(errno));
    118  1.2  cgd 			sprintf(errmsg, "cannot seek temp file");
    119  1.1  cgd 			return (char *) ERR;
    120  1.1  cgd 		}
    121  1.1  cgd 		sfseek = ftell(sfp);
    122  1.1  cgd 		seek_write = 0;
    123  1.1  cgd 	}
    124  1.1  cgd 	/* assert: spl1() */
    125  1.1  cgd 	if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
    126  1.1  cgd 		sfseek = -1;
    127  1.3  alm 		fprintf(stderr, "%s\n", strerror(errno));
    128  1.2  cgd 		sprintf(errmsg, "cannot write temp file");
    129  1.1  cgd 		return (char *) ERR;
    130  1.1  cgd 	}
    131  1.1  cgd 	lp->len = len;
    132  1.1  cgd 	lp->seek  = sfseek;
    133  1.1  cgd 	lpqueue(lp);
    134  1.1  cgd 	sfseek += len;			/* update file position */
    135  1.1  cgd 	return (*++s == '\0') ? NULL : s;
    136  1.1  cgd }
    137  1.1  cgd 
    138  1.1  cgd 
    139  1.1  cgd /* lpqueue: add a line node in the editor buffer after the current line */
    140  1.1  cgd void
    141  1.1  cgd lpqueue(lp)
    142  1.1  cgd 	line_t *lp;
    143  1.1  cgd {
    144  1.1  cgd 	line_t *cp;
    145  1.1  cgd 
    146  1.1  cgd 	cp = getptr(curln);				/* this getptr last! */
    147  1.1  cgd 	insqueue(lp, cp);
    148  1.1  cgd 	lastln++;
    149  1.1  cgd 	curln++;
    150  1.1  cgd }
    151  1.1  cgd 
    152  1.1  cgd 
    153  1.3  alm /* getaddr: return line number of pointer */
    154  1.3  alm long
    155  1.3  alm getaddr(lp)
    156  1.3  alm 	line_t *lp;
    157  1.3  alm {
    158  1.3  alm 	line_t *cp = &line0;
    159  1.3  alm 	long n = 0;
    160  1.3  alm 
    161  1.3  alm 	while (cp != lp && (cp = cp->next) != &line0)
    162  1.3  alm 		n++;
    163  1.3  alm 	return (cp != &line0) ? n : 0;
    164  1.3  alm }
    165  1.3  alm 
    166  1.3  alm 
    167  1.1  cgd extern int mutex;
    168  1.1  cgd extern int sigflags;
    169  1.1  cgd 
    170  1.1  cgd /* getptr: return pointer to a line node in the editor buffer */
    171  1.1  cgd line_t *
    172  1.1  cgd getptr(n)
    173  1.1  cgd 	long n;
    174  1.1  cgd {
    175  1.1  cgd 	static line_t *lp = &line0;
    176  1.1  cgd 	static long on = 0;
    177  1.1  cgd 
    178  1.1  cgd 	spl1();
    179  1.1  cgd 	if (n > on)
    180  1.2  cgd 		if (n <= (on + lastln) >> 1)
    181  1.1  cgd 			for (; on < n; on++)
    182  1.1  cgd 				lp = lp->next;
    183  1.2  cgd 		else {
    184  1.1  cgd 			lp = line0.prev;
    185  1.1  cgd 			for (on = lastln; on > n; on--)
    186  1.1  cgd 				lp = lp->prev;
    187  1.1  cgd 		}
    188  1.1  cgd 	else
    189  1.2  cgd 		if (n >= on >> 1)
    190  1.1  cgd 			for (; on > n; on--)
    191  1.1  cgd 				lp = lp->prev;
    192  1.2  cgd 		else {
    193  1.1  cgd 			lp = &line0;
    194  1.1  cgd 			for (on = 0; on < n; on++)
    195  1.1  cgd 				lp = lp->next;
    196  1.1  cgd 		}
    197  1.1  cgd 	spl0();
    198  1.1  cgd 	return lp;
    199  1.1  cgd }
    200  1.1  cgd 
    201  1.1  cgd 
    202  1.1  cgd char sfn[15] = "";				/* scratch file name */
    203  1.1  cgd 
    204  1.1  cgd /* sbopen: open scratch file */
    205  1.1  cgd sbopen()
    206  1.1  cgd {
    207  1.1  cgd 	strcpy(sfn, "/tmp/ed.XXXXXX");
    208  1.1  cgd 	if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
    209  1.3  alm 		fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
    210  1.2  cgd 		sprintf(errmsg, "cannot open temp file");
    211  1.1  cgd 		return ERR;
    212  1.1  cgd 	}
    213  1.1  cgd 	return 0;
    214  1.1  cgd }
    215  1.1  cgd 
    216  1.2  cgd 
    217  1.1  cgd /* sbclose: close scratch file */
    218  1.1  cgd sbclose()
    219  1.1  cgd {
    220  1.1  cgd 	if (sfp) {
    221  1.3  alm 		if (fclose(sfp) < 0) {
    222  1.3  alm 			fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
    223  1.3  alm 			sprintf(errmsg, "cannot close temp file");
    224  1.3  alm 			return ERR;
    225  1.3  alm 		}
    226  1.1  cgd 		sfp = NULL;
    227  1.2  cgd 		unlink(sfn);
    228  1.1  cgd 	}
    229  1.1  cgd 	sfseek = seek_write = 0;
    230  1.4  alm 	return 0;
    231  1.1  cgd }
    232  1.1  cgd 
    233  1.1  cgd 
    234  1.1  cgd /* quit: remove scratch file and exit */
    235  1.1  cgd void
    236  1.1  cgd quit(n)
    237  1.1  cgd 	int n;
    238  1.1  cgd {
    239  1.1  cgd 	if (sfp) {
    240  1.1  cgd 		unlink(sfn);
    241  1.1  cgd 		fclose(sfp);
    242  1.1  cgd 	}
    243  1.1  cgd 	exit(n);
    244  1.1  cgd }
    245