1 /* $OpenBSD: el.c,v 1.11 2003/06/02 20:18:40 millert Exp $ */ 2 /* $NetBSD: el.c,v 1.6 1997/04/24 18:54:16 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Christos Zoulas of Cornell University. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #if !defined(lint) && !defined(SCCSID) 37 #if 0 38 static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94"; 39 #else 40 static const char rcsid[] = "$OpenBSD: el.c,v 1.11 2003/06/02 20:18:40 millert Exp $"; 41 #endif 42 #endif /* not lint && not SCCSID */ 43 44 /* 45 * el.c: EditLine interface functions 46 */ 47 #include "sys.h" 48 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <string.h> 52 #include <stdlib.h> 53 #include <stdarg.h> 54 #include <unistd.h> 55 #include "el.h" 56 57 /* el_init(): 58 * Initialize editline and set default parameters. 59 */ 60 public EditLine * 61 el_init(prog, fin, fout) 62 const char *prog; 63 FILE *fin, *fout; 64 { 65 EditLine *el = (EditLine *) el_malloc(sizeof(EditLine)); 66 #ifdef DEBUG 67 char *tty; 68 #endif 69 70 if (el == NULL) 71 return NULL; 72 73 memset(el, 0, sizeof(EditLine)); 74 75 el->el_infd = fileno(fin); 76 el->el_outfile = fout; 77 el->el_prog = strdup(prog); 78 79 #ifdef DEBUG 80 if (issetugid() == 0 && (tty = getenv("DEBUGTTY")) != NULL) { 81 el->el_errfile = fopen(tty, "w"); 82 if (el->el_errfile == NULL) { 83 extern errno; 84 (void)fprintf(stderr, "Cannot open %s (%s).\n", 85 tty, strerror(errno)); 86 return NULL; 87 } 88 } 89 else 90 #endif 91 el->el_errfile = stderr; 92 93 /* 94 * Initialize all the modules. Order is important!!! 95 */ 96 (void)term_init(el); 97 (void)tty_init(el); 98 (void)key_init(el); 99 (void)map_init(el); 100 (void)ch_init(el); 101 (void)search_init(el); 102 (void)hist_init(el); 103 (void)prompt_init(el); 104 (void)sig_init(el); 105 el->el_flags = 0; 106 107 return el; 108 } /* end el_init */ 109 110 111 /* el_end(): 112 * Clean up. 113 */ 114 public void 115 el_end(el) 116 EditLine *el; 117 { 118 if (el == NULL) 119 return; 120 121 el_reset(el); 122 123 term_end(el); 124 tty_end(el); 125 key_end(el); 126 map_end(el); 127 ch_end(el); 128 search_end(el); 129 hist_end(el); 130 prompt_end(el); 131 sig_end(el); 132 133 el_free((ptr_t) el->el_prog); 134 el_free((ptr_t) el); 135 } /* end el_end */ 136 137 138 /* el_reset(): 139 * Reset the tty and the parser 140 */ 141 public void 142 el_reset(el) 143 EditLine *el; 144 { 145 tty_cookedmode(el); 146 ch_reset(el); /* XXX: Do we want that? */ 147 } 148 149 150 /* el_set(): 151 * set the editline parameters 152 */ 153 public int 154 el_set(EditLine *el, int op, ...) 155 { 156 va_list va; 157 int rv; 158 159 va_start(va, op); 160 161 switch (op) { 162 case EL_PROMPT: 163 rv = prompt_set(el, va_arg(va, el_pfunc_t)); 164 break; 165 166 case EL_TERMINAL: 167 rv = term_set(el, va_arg(va, char *)); 168 break; 169 170 case EL_EDITOR: 171 rv = map_set_editor(el, va_arg(va, char *)); 172 break; 173 174 case EL_SIGNAL: 175 if (va_arg(va, int)) 176 el->el_flags |= HANDLE_SIGNALS; 177 else 178 el->el_flags &= ~HANDLE_SIGNALS; 179 rv = 0; 180 break; 181 182 case EL_BIND: 183 case EL_TELLTC: 184 case EL_SETTC: 185 case EL_ECHOTC: 186 case EL_SETTY: 187 { 188 char *argv[20]; 189 int i; 190 for (i = 1; i < 20; i++) 191 if ((argv[i] = va_arg(va, char *)) == NULL) 192 break; 193 194 switch (op) { 195 case EL_BIND: 196 argv[0] = "bind"; 197 rv = map_bind(el, i, argv); 198 break; 199 200 case EL_TELLTC: 201 argv[0] = "telltc"; 202 rv = term_telltc(el, i, argv); 203 break; 204 205 case EL_SETTC: 206 argv[0] = "settc"; 207 rv = term_settc(el, i, argv); 208 break; 209 210 case EL_ECHOTC: 211 argv[0] = "echotc"; 212 rv = term_echotc(el, i, argv); 213 break; 214 215 case EL_SETTY: 216 argv[0] = "setty"; 217 rv = tty_stty(el, i, argv); 218 break; 219 220 default: 221 rv = -1; 222 abort(); 223 break; 224 } 225 } 226 break; 227 228 case EL_ADDFN: 229 { 230 char *name = va_arg(va, char *); 231 char *help = va_arg(va, char *); 232 el_func_t func = va_arg(va, el_func_t); 233 rv = map_addfunc(el, name, help, func); 234 } 235 break; 236 237 case EL_HIST: 238 { 239 hist_fun_t func = va_arg(va, hist_fun_t); 240 ptr_t ptr = va_arg(va, char *); 241 rv = hist_set(el, func, ptr); 242 } 243 break; 244 245 default: 246 rv = -1; 247 } 248 249 va_end(va); 250 return rv; 251 } /* end el_set */ 252 253 254 /* el_line(): 255 * Return editing info 256 */ 257 public const LineInfo * 258 el_line(el) 259 EditLine *el; 260 { 261 return (const LineInfo *) &el->el_line; 262 } 263 264 static const char elpath[] = "/.editrc"; 265 266 /* el_source(): 267 * Source a file 268 */ 269 public int 270 el_source(el, fname) 271 EditLine *el; 272 const char *fname; 273 { 274 FILE *fp; 275 size_t len; 276 char *ptr, path[MAXPATHLEN]; 277 278 if (fname == NULL) { 279 ptr = getenv("HOME"); 280 if (issetugid() != 0 || ptr == NULL || *ptr == '\0') 281 return -1; 282 (void) snprintf(path, sizeof(path), "%s%s", ptr, elpath); 283 fname = path; 284 } 285 286 if ((fp = fopen(fname, "r")) == NULL) 287 return -1; 288 289 while ((ptr = fgetln(fp, &len)) != NULL) { 290 if (ptr[len - 1] == '\n') 291 --len; 292 ptr[len] = '\0'; 293 if (parse_line(el, ptr) == -1) { 294 (void)fclose(fp); 295 return -1; 296 } 297 } 298 299 (void)fclose(fp); 300 return 0; 301 } 302 303 304 /* el_resize(): 305 * Called from program when terminal is resized 306 */ 307 public void 308 el_resize(el) 309 EditLine *el; 310 { 311 int lins, cols; 312 sigset_t oset, nset; 313 (void)sigemptyset(&nset); 314 (void)sigaddset(&nset, SIGWINCH); 315 (void)sigprocmask(SIG_BLOCK, &nset, &oset); 316 317 /* get the correct window size */ 318 if (term_get_size(el, &lins, &cols)) 319 term_change_size(el, lins, cols); 320 321 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 322 } 323