1 /* $NetBSD: tc1.c,v 1.2 2009/03/31 17:38:27 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Christos Zoulas of Cornell University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include "config.h" 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"); 39 #endif /* not lint */ 40 41 #if !defined(lint) && !defined(SCCSID) 42 #if 0 43 static char sccsid[] = "@(#)test.c 8.1 (Berkeley) 6/4/93"; 44 #else 45 __RCSID("$NetBSD: tc1.c,v 1.2 2009/03/31 17:38:27 christos Exp $"); 46 #endif 47 #endif /* not lint && not SCCSID */ 48 49 /* 50 * test.c: A little test program 51 */ 52 #include <stdio.h> 53 #include <string.h> 54 #include <signal.h> 55 #include <sys/wait.h> 56 #include <ctype.h> 57 #include <stdlib.h> 58 #include <unistd.h> 59 #include <dirent.h> 60 61 #include "histedit.h" 62 63 static int continuation = 0; 64 volatile sig_atomic_t gotsig = 0; 65 66 static unsigned char complete(EditLine *, int); 67 int main(int, char **); 68 static char *prompt(EditLine *); 69 static void sig(int); 70 71 static char * 72 prompt(EditLine *el) 73 { 74 static char a[] = "\1\e[7m\1Edit$\1\e[0m\1 "; 75 static char b[] = "Edit> "; 76 77 return (continuation ? b : a); 78 } 79 80 static void 81 sig(int i) 82 { 83 gotsig = i; 84 } 85 86 static unsigned char 87 complete(EditLine *el, int ch) 88 { 89 DIR *dd = opendir("."); 90 struct dirent *dp; 91 const char* ptr; 92 const LineInfo *lf = el_line(el); 93 int len; 94 95 /* 96 * Find the last word 97 */ 98 for (ptr = lf->cursor - 1; 99 !isspace((unsigned char)*ptr) && ptr > lf->buffer; ptr--) 100 continue; 101 len = lf->cursor - ++ptr; 102 103 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) { 104 if (len > strlen(dp->d_name)) 105 continue; 106 if (strncmp(dp->d_name, ptr, len) == 0) { 107 closedir(dd); 108 if (el_insertstr(el, &dp->d_name[len]) == -1) 109 return (CC_ERROR); 110 else 111 return (CC_REFRESH); 112 } 113 } 114 115 closedir(dd); 116 return (CC_ERROR); 117 } 118 119 int 120 main(int argc, char *argv[]) 121 { 122 EditLine *el = NULL; 123 int num; 124 const char *buf; 125 Tokenizer *tok; 126 #if 0 127 int lastevent = 0; 128 #endif 129 int ncontinuation; 130 History *hist; 131 HistEvent ev; 132 133 (void) signal(SIGINT, sig); 134 (void) signal(SIGQUIT, sig); 135 (void) signal(SIGHUP, sig); 136 (void) signal(SIGTERM, sig); 137 138 hist = history_init(); /* Init the builtin history */ 139 /* Remember 100 events */ 140 history(hist, &ev, H_SETSIZE, 100); 141 142 tok = tok_init(NULL); /* Initialize the tokenizer */ 143 144 /* Initialize editline */ 145 el = el_init(*argv, stdin, stdout, stderr); 146 147 el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */ 148 el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */ 149 el_set(el, EL_PROMPT, prompt, '\1');/* Set the prompt function */ 150 151 /* Tell editline to use this history interface */ 152 el_set(el, EL_HIST, history, hist); 153 154 /* Add a user-defined function */ 155 el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete); 156 157 /* Bind tab to it */ 158 el_set(el, EL_BIND, "^I", "ed-complete", NULL); 159 160 /* 161 * Bind j, k in vi command mode to previous and next line, instead 162 * of previous and next history. 163 */ 164 el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL); 165 el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL); 166 167 /* 168 * Source the user's defaults file. 169 */ 170 el_source(el, NULL); 171 172 while ((buf = el_gets(el, &num)) != NULL && num != 0) { 173 int ac, cc, co; 174 #ifdef DEBUG 175 int i; 176 #endif 177 const char **av; 178 const LineInfo *li; 179 li = el_line(el); 180 #ifdef DEBUG 181 (void) fprintf(stderr, "==> got %d %s", num, buf); 182 (void) fprintf(stderr, " > li `%.*s_%.*s'\n", 183 (li->cursor - li->buffer), li->buffer, 184 (li->lastchar - 1 - li->cursor), 185 (li->cursor >= li->lastchar) ? "" : li->cursor); 186 187 #endif 188 if (gotsig) { 189 (void) fprintf(stderr, "Got signal %d.\n", gotsig); 190 gotsig = 0; 191 el_reset(el); 192 } 193 194 if (!continuation && num == 1) 195 continue; 196 197 ac = cc = co = 0; 198 ncontinuation = tok_line(tok, li, &ac, &av, &cc, &co); 199 if (ncontinuation < 0) { 200 (void) fprintf(stderr, "Internal error\n"); 201 continuation = 0; 202 continue; 203 } 204 #ifdef DEBUG 205 (void) fprintf(stderr, " > nc %d ac %d cc %d co %d\n", 206 ncontinuation, ac, cc, co); 207 #endif 208 #if 0 209 if (continuation) { 210 /* 211 * Append to the right event in case the user 212 * moved around in history. 213 */ 214 if (history(hist, &ev, H_SET, lastevent) == -1) 215 err(1, "%d: %s", lastevent, ev.str); 216 history(hist, &ev, H_ADD , buf); 217 } else { 218 history(hist, &ev, H_ENTER, buf); 219 lastevent = ev.num; 220 } 221 #else 222 /* Simpler */ 223 history(hist, &ev, continuation ? H_APPEND : H_ENTER, buf); 224 #endif 225 226 continuation = ncontinuation; 227 ncontinuation = 0; 228 if (continuation) 229 continue; 230 #ifdef DEBUG 231 for (i = 0; i < ac; i++) { 232 (void) fprintf(stderr, " > arg# %2d ", i); 233 if (i != cc) 234 (void) fprintf(stderr, "`%s'\n", av[i]); 235 else 236 (void) fprintf(stderr, "`%.*s_%s'\n", 237 co, av[i], av[i] + co); 238 } 239 #endif 240 241 if (strcmp(av[0], "history") == 0) { 242 int rv; 243 244 switch (ac) { 245 case 1: 246 for (rv = history(hist, &ev, H_LAST); rv != -1; 247 rv = history(hist, &ev, H_PREV)) 248 (void) fprintf(stdout, "%4d %s", 249 ev.num, ev.str); 250 break; 251 252 case 2: 253 if (strcmp(av[1], "clear") == 0) 254 history(hist, &ev, H_CLEAR); 255 else 256 goto badhist; 257 break; 258 259 case 3: 260 if (strcmp(av[1], "load") == 0) 261 history(hist, &ev, H_LOAD, av[2]); 262 else if (strcmp(av[1], "save") == 0) 263 history(hist, &ev, H_SAVE, av[2]); 264 break; 265 266 badhist: 267 default: 268 (void) fprintf(stderr, 269 "Bad history arguments\n"); 270 break; 271 } 272 } else if (el_parse(el, ac, av) == -1) { 273 switch (fork()) { 274 case 0: 275 execvp(av[0], (char *const *)__UNCONST(av)); 276 perror(av[0]); 277 _exit(1); 278 /*NOTREACHED*/ 279 break; 280 281 case -1: 282 perror("fork"); 283 break; 284 285 default: 286 if (wait(&num) == -1) 287 perror("wait"); 288 (void) fprintf(stderr, "Exit %x\n", num); 289 break; 290 } 291 } 292 293 tok_reset(tok); 294 } 295 296 el_end(el); 297 tok_end(tok); 298 history_end(hist); 299 300 return (0); 301 } 302