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