1 /* buf.c: This file contains the scratch-file buffer routines for the
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 static char * const rcsid = "@(#)buf.c,v 1.4 1994/02/01 00:34:35 alm Exp";
33 static char * const rcsid =
34 "$FreeBSD: src/bin/ed/buf.c,v 1.17 1999/08/27 23:14:12 peter Exp $";
44 FILE *sfp; /* scratch file pointer */
45 off_t sfseek; /* scratch file position */
46 int seek_write; /* seek before writing */
47 line_t buffer_head; /* incore buffer */
49 /* get_sbuf_line: get a line of text from the scratch file; return pointer
55 static char *sfbuf = NULL; /* buffer */
56 static int sfbufsz = 0; /* buffer size */
60 if (lp == &buffer_head)
62 seek_write = 1; /* force seek on write */
64 if (sfseek != lp->seek) {
66 if (fseek(sfp, sfseek, SEEK_SET) < 0) {
67 fprintf(stderr, "%s\n", strerror(errno));
68 sprintf(errmsg, "cannot seek temp file");
73 REALLOC(sfbuf, sfbufsz, len + 1, NULL);
74 if ((ct = fread(sfbuf, sizeof(char), len, sfp)) < 0 || ct != len) {
75 fprintf(stderr, "%s\n", strerror(errno));
76 sprintf(errmsg, "cannot read temp file");
79 sfseek += len; /* update file position */
85 /* put_sbuf_line: 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 */
95 if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) {
96 fprintf(stderr, "%s\n", strerror(errno));
97 sprintf(errmsg, "out of memory");
100 /* assert: cs is '\n' terminated */
101 for (s = cs; *s != '\n'; s++)
103 if (s - cs >= LINECHARS) {
104 sprintf(errmsg, "line too long");
108 /* out of position */
110 if (fseek(sfp, 0L, SEEK_END) < 0) {
111 fprintf(stderr, "%s\n", strerror(errno));
112 sprintf(errmsg, "cannot seek temp file");
119 if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) {
121 fprintf(stderr, "%s\n", strerror(errno));
122 sprintf(errmsg, "cannot write temp file");
128 sfseek += len; /* update file position */
133 /* add_line_node: add a line node in the editor buffer after the current line */
140 cp = get_addressed_line_node(current_addr); /* this get_addressed_line_node last! */
147 /* get_line_node_addr: return line number of pointer */
149 get_line_node_addr(lp)
152 line_t *cp = &buffer_head;
155 while (cp != lp && (cp = cp->q_forw) != &buffer_head)
157 if (n && cp == &buffer_head) {
158 sprintf(errmsg, "invalid address");
165 /* get_addressed_line_node: return pointer to a line node in the editor buffer */
167 get_addressed_line_node(n)
170 static line_t *lp = &buffer_head;
175 if (n <= (on + addr_last) >> 1)
179 lp = buffer_head.q_back;
180 for (on = addr_last; on > n; on--)
189 for (on = 0; on < n; on++)
197 extern int newline_added;
199 char sfn[15] = ""; /* scratch file name */
201 /* open_sbuf: open scratch file */
208 isbinary = newline_added = 0;
210 strcpy(sfn, "/tmp/ed.XXXXXX");
211 if ((fd = mkstemp(sfn)) == -1 ||
212 (sfp = fdopen(fd, "w+")) == NULL) {
216 strcpy(errmsg, "cannot open temp file");
225 /* close_sbuf: close scratch file */
230 if (fclose(sfp) < 0) {
231 fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
232 sprintf(errmsg, "cannot close temp file");
238 sfseek = seek_write = 0;
243 /* quit: remove_lines scratch file and exit */
256 unsigned char ctab[256]; /* character translation table */
258 /* init_buffers: open scratch buffer; initialize line queue */
264 /* Read stdin one character at a time to avoid i/o contention
265 with shell escapes invoked by nonterminal input, e.g.,
270 setbuffer(stdin, stdinbuf, 1);
272 /* Ensure stdout is line buffered. This avoids bogus delays
273 of output if stdout is piped through utilities to a terminal. */
274 setvbuf(stdout, NULL, _IOLBF, 0);
277 REQUE(&buffer_head, &buffer_head);
278 for (i = 0; i < 256; i++)
283 /* translit_text: translate characters in a string */
285 translit_text(s, len, from, to)
295 ctab[i] = i; /* restore table to initial state */
297 for (us = (unsigned char *) s; len-- > 0; us++)