Home | History | Annotate | Line # | Download | only in ed
buf.c revision 1.9
      1 /* buf.c: This file contains the scratch-file buffer rountines for the
      2    ed line editor. */
      3 /*-
      4  * Copyright (c) 1993 Andrew Moore, Talke Studio.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 #ifndef lint
     29 static char sccsid[] = "@(#)buf.c	5.5 (Talke Studio) 3/28/93";
     30 #endif /* not lint */
     31 
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 #include <sys/file.h>
     36 #include <unistd.h>
     37 
     38 #include "ed.h"
     39 
     40 extern char errmsg[];
     41 
     42 FILE *sfp;				/* scratch file pointer */
     43 char *sfbuf = NULL;			/* scratch file input buffer */
     44 int sfbufsz = 0;			/* scratch file input buffer size */
     45 off_t sfseek;				/* scratch file position */
     46 int seek_write;				/* seek before writing */
     47 line_t line0;				/* initial node of line queue */
     48 
     49 /* gettxt: get a line of text from the scratch file; return pointer
     50    to the text */
     51 char *
     52 gettxt(lp)
     53 	line_t *lp;
     54 {
     55 	int len, ct;
     56 
     57 	if (lp == &line0)
     58 		return NULL;
     59 	seek_write = 1;				/* force seek on write */
     60 	/* out of position */
     61 	if (sfseek != lp->seek) {
     62 		sfseek = lp->seek;
     63 		if (fseek(sfp, sfseek, SEEK_SET) < 0) {
     64 			fprintf(stderr, "%s\n", strerror(errno));
     65 			sprintf(errmsg, "cannot seek temp file");
     66 			return NULL;
     67 		}
     68 	}
     69 	len = lp->len;
     70 	CKBUF(sfbuf, sfbufsz, len + 1, NULL);
     71 	if ((ct = fread(sfbuf, sizeof(char), len, sfp)) <  0 || ct != len) {
     72 		fprintf(stderr, "%s\n", strerror(errno));
     73 		sprintf(errmsg, "cannot read temp file");
     74 		return NULL;
     75 	}
     76 	sfseek += len;				/* update file position */
     77 	sfbuf[len] = '\0';
     78 	return sfbuf;
     79 }
     80 
     81 
     82 extern long curln;
     83 extern long lastln;
     84 
     85 /* puttxt: write a line of text to the scratch file and add a line node
     86    to the editor buffer;  return a pointer to the end of the text */
     87 char *
     88 puttxt(cs)
     89 	char *cs;
     90 {
     91 	line_t *lp;
     92 	int len, ct;
     93 	char *s;
     94 
     95 	if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
     96 		fprintf(stderr, "%s\n", strerror(errno));
     97 		sprintf(errmsg, "out of memory");
     98 		return NULL;
     99 	}
    100 	/* assert: cs is '\n' terminated */
    101 	for (s = cs; *s != '\n'; s++)
    102 		;
    103 	if (s - cs >= LINECHARS) {
    104 		sprintf(errmsg, "line too long");
    105 		return NULL;
    106 	}
    107 	len = (s - cs);
    108 	/* out of position */
    109 	if (seek_write) {
    110 		if (fseek(sfp, 0L, SEEK_END) < 0) {
    111 			fprintf(stderr, "%s\n", strerror(errno));
    112 			sprintf(errmsg, "cannot seek temp file");
    113 			return NULL;
    114 		}
    115 		sfseek = ftell(sfp);
    116 		seek_write = 0;
    117 	}
    118 	/* assert: spl1() */
    119 	if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
    120 		sfseek = -1;
    121 		fprintf(stderr, "%s\n", strerror(errno));
    122 		sprintf(errmsg, "cannot write temp file");
    123 		return NULL;
    124 	}
    125 	lp->len = len;
    126 	lp->seek  = sfseek;
    127 	lpqueue(lp);
    128 	sfseek += len;			/* update file position */
    129 	return ++s;
    130 }
    131 
    132 
    133 /* lpqueue: add a line node in the editor buffer after the current line */
    134 void
    135 lpqueue(lp)
    136 	line_t *lp;
    137 {
    138 	line_t *cp;
    139 
    140 	cp = getlp(curln);				/* this getlp last! */
    141 	insqueue(lp, cp);
    142 	lastln++;
    143 	curln++;
    144 }
    145 
    146 
    147 /* getaddr: return line number of pointer */
    148 long
    149 getaddr(lp)
    150 	line_t *lp;
    151 {
    152 	line_t *cp = &line0;
    153 	long n = 0;
    154 
    155 	while (cp != lp && (cp = cp->next) != &line0)
    156 		n++;
    157 	if (n && cp == &line0) {
    158 		sprintf(errmsg, "invalid address");
    159 		return ERR;
    160 	 }
    161 	 return n;
    162 }
    163 
    164 
    165 /* getlp: return pointer to a line node in the editor buffer */
    166 line_t *
    167 getlp(n)
    168 	long n;
    169 {
    170 	static line_t *lp = &line0;
    171 	static long on = 0;
    172 
    173 	spl1();
    174 	if (n > on)
    175 		if (n <= (on + lastln) >> 1)
    176 			for (; on < n; on++)
    177 				lp = lp->next;
    178 		else {
    179 			lp = line0.prev;
    180 			for (on = lastln; on > n; on--)
    181 				lp = lp->prev;
    182 		}
    183 	else
    184 		if (n >= on >> 1)
    185 			for (; on > n; on--)
    186 				lp = lp->prev;
    187 		else {
    188 			lp = &line0;
    189 			for (on = 0; on < n; on++)
    190 				lp = lp->next;
    191 		}
    192 	spl0();
    193 	return lp;
    194 }
    195 
    196 
    197 char sfn[15] = "";				/* scratch file name */
    198 
    199 /* sbopen: open scratch file */
    200 sbopen()
    201 {
    202 	strcpy(sfn, "/tmp/ed.XXXXXX");
    203 	if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
    204 		fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
    205 		sprintf(errmsg, "cannot open temp file");
    206 		return ERR;
    207 	}
    208 	return 0;
    209 }
    210 
    211 
    212 /* sbclose: close scratch file */
    213 sbclose()
    214 {
    215 	if (sfp) {
    216 		if (fclose(sfp) < 0) {
    217 			fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
    218 			sprintf(errmsg, "cannot close temp file");
    219 			return ERR;
    220 		}
    221 		sfp = NULL;
    222 		unlink(sfn);
    223 	}
    224 	sfseek = seek_write = 0;
    225 	return 0;
    226 }
    227 
    228 
    229 /* quit: remove scratch file and exit */
    230 void
    231 quit(n)
    232 	int n;
    233 {
    234 	if (sfp) {
    235 		fclose(sfp);
    236 		unlink(sfn);
    237 	}
    238 	exit(n);
    239 }
    240 
    241 
    242 unsigned char ctab[256];		/* character translation table */
    243 
    244 /* inited: open scratch buffer; initialize line queue */
    245 void
    246 inited()
    247 {
    248 	int i = 0;
    249 
    250 	if (sbopen() < 0)
    251 		quit(2);
    252 	requeue(&line0, &line0);
    253 	for (i = 0; i < 256; i++)
    254 		ctab[i] = i;
    255 }
    256 
    257 
    258 /* translit: translate characters in a string */
    259 char *
    260 translit(s, len, from, to)
    261 	char *s;
    262 	int len;
    263 	int from;
    264 	int to;
    265 {
    266 	static int i = 0;
    267 
    268 	unsigned char *us;
    269 
    270 	ctab[i] = i;			/* restore table to initial state */
    271 	ctab[i = from] = to;
    272 	for (us = (unsigned char *) s; len-- > 0; us++)
    273 		*us = ctab[*us];
    274 	return s;
    275 }
    276