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 static char rcsid[] = "$Id: buf.c,v 1.10 1993/11/23 04:41:48 alm Exp $"; 31 #endif /* not lint */ 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <sys/file.h> 37 #include <unistd.h> 38 39 #include "ed.h" 40 41 extern char errmsg[]; 42 43 FILE *sfp; /* scratch file pointer */ 44 char *sfbuf = NULL; /* scratch file input buffer */ 45 int sfbufsz = 0; /* scratch file input buffer size */ 46 off_t sfseek; /* scratch file position */ 47 int seek_write; /* seek before writing */ 48 line_t line0; /* initial node of line queue */ 49 50 /* get_sbuf_line: get a line of text from the scratch file; return pointer 51 to the text */ 52 char * 53 get_sbuf_line(lp) 54 line_t *lp; 55 { 56 int len, ct; 57 58 if (lp == &line0) 59 return NULL; 60 seek_write = 1; /* force seek on write */ 61 /* out of position */ 62 if (sfseek != lp->seek) { 63 sfseek = lp->seek; 64 if (fseek(sfp, sfseek, SEEK_SET) < 0) { 65 fprintf(stderr, "%s\n", strerror(errno)); 66 sprintf(errmsg, "cannot seek temp file"); 67 return NULL; 68 } 69 } 70 len = lp->len; 71 CKBUF(sfbuf, sfbufsz, len + 1, NULL); 72 if ((ct = fread(sfbuf, sizeof(char), len, sfp)) < 0 || ct != len) { 73 fprintf(stderr, "%s\n", strerror(errno)); 74 sprintf(errmsg, "cannot read temp file"); 75 return NULL; 76 } 77 sfseek += len; /* update file position */ 78 sfbuf[len] = '\0'; 79 return sfbuf; 80 } 81 82 83 extern long current_addr; 84 extern long addr_last; 85 86 /* put_sbuf_line: write a line of text to the scratch file and add a line node 87 to the editor buffer; return a pointer to the end of the text */ 88 char * 89 put_sbuf_line(cs) 90 char *cs; 91 { 92 line_t *lp; 93 int len, ct; 94 char *s; 95 96 if ((lp = (line_t *) malloc(sizeof(line_t))) == NULL) { 97 fprintf(stderr, "%s\n", strerror(errno)); 98 sprintf(errmsg, "out of memory"); 99 return NULL; 100 } 101 /* assert: cs is '\n' terminated */ 102 for (s = cs; *s != '\n'; s++) 103 ; 104 if (s - cs >= LINECHARS) { 105 sprintf(errmsg, "line too long"); 106 return NULL; 107 } 108 len = s - cs; 109 /* out of position */ 110 if (seek_write) { 111 if (fseek(sfp, 0L, SEEK_END) < 0) { 112 fprintf(stderr, "%s\n", strerror(errno)); 113 sprintf(errmsg, "cannot seek temp file"); 114 return NULL; 115 } 116 sfseek = ftell(sfp); 117 seek_write = 0; 118 } 119 /* assert: SPL1() */ 120 if ((ct = fwrite(cs, sizeof(char), len, sfp)) < 0 || ct != len) { 121 sfseek = -1; 122 fprintf(stderr, "%s\n", strerror(errno)); 123 sprintf(errmsg, "cannot write temp file"); 124 return NULL; 125 } 126 lp->len = len; 127 lp->seek = sfseek; 128 add_line_node(lp); 129 sfseek += len; /* update file position */ 130 return ++s; 131 } 132 133 134 /* add_line_node: add a line node in the editor buffer after the current line */ 135 void 136 add_line_node(lp) 137 line_t *lp; 138 { 139 line_t *cp; 140 141 cp = get_addressed_line_node(current_addr); /* this get_addressed_line_node last! */ 142 insqueue(lp, cp); 143 addr_last++; 144 current_addr++; 145 } 146 147 148 /* get_line_node_addr: return line number of pointer */ 149 long 150 get_line_node_addr(lp) 151 line_t *lp; 152 { 153 line_t *cp = &line0; 154 long n = 0; 155 156 while (cp != lp && (cp = cp->next) != &line0) 157 n++; 158 if (n && cp == &line0) { 159 sprintf(errmsg, "invalid address"); 160 return ERR; 161 } 162 return n; 163 } 164 165 166 /* get_addressed_line_node: return pointer to a line node in the editor buffer */ 167 line_t * 168 get_addressed_line_node(n) 169 long n; 170 { 171 static line_t *lp = &line0; 172 static long on = 0; 173 174 SPL1(); 175 if (n > on) 176 if (n <= (on + addr_last) >> 1) 177 for (; on < n; on++) 178 lp = lp->next; 179 else { 180 lp = line0.prev; 181 for (on = addr_last; on > n; on--) 182 lp = lp->prev; 183 } 184 else 185 if (n >= on >> 1) 186 for (; on > n; on--) 187 lp = lp->prev; 188 else { 189 lp = &line0; 190 for (on = 0; on < n; on++) 191 lp = lp->next; 192 } 193 SPL0(); 194 return lp; 195 } 196 197 198 char sfn[15] = ""; /* scratch file name */ 199 200 /* open_sbuf: open scratch file */ 201 int 202 open_sbuf() 203 { 204 strcpy(sfn, "/tmp/ed.XXXXXX"); 205 if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) { 206 fprintf(stderr, "%s: %s\n", sfn, strerror(errno)); 207 sprintf(errmsg, "cannot open temp file"); 208 return ERR; 209 } 210 return 0; 211 } 212 213 214 /* close_sbuf: close scratch file */ 215 int 216 close_sbuf() 217 { 218 if (sfp) { 219 if (fclose(sfp) < 0) { 220 fprintf(stderr, "%s: %s\n", sfn, strerror(errno)); 221 sprintf(errmsg, "cannot close temp file"); 222 return ERR; 223 } 224 sfp = NULL; 225 unlink(sfn); 226 } 227 sfseek = seek_write = 0; 228 return 0; 229 } 230 231 232 /* quit: remove_lines scratch file and exit */ 233 void 234 quit(n) 235 int n; 236 { 237 if (sfp) { 238 fclose(sfp); 239 unlink(sfn); 240 } 241 exit(n); 242 } 243 244 245 unsigned char ctab[256]; /* character translation table */ 246 247 /* init_buffers: open scratch buffer; initialize line queue */ 248 void 249 init_buffers() 250 { 251 int i = 0; 252 253 if (open_sbuf() < 0) 254 quit(2); 255 requeue(&line0, &line0); 256 for (i = 0; i < 256; i++) 257 ctab[i] = i; 258 } 259 260 261 /* translit_text: translate characters in a string */ 262 char * 263 translit_text(s, len, from, to) 264 char *s; 265 int len; 266 int from; 267 int to; 268 { 269 static int i = 0; 270 271 unsigned char *us; 272 273 ctab[i] = i; /* restore table to initial state */ 274 ctab[i = from] = to; 275 for (us = (unsigned char *) s; len-- > 0; us++) 276 *us = ctab[*us]; 277 return s; 278 } 279