1*54239Sbostic /*- 2*54239Sbostic * Copyright (c) 1992 The Regents of the University of California. 3*54239Sbostic * All rights reserved. 4*54239Sbostic * 5*54239Sbostic * This code is derived from software contributed to Berkeley by 6*54239Sbostic * Christos Zoulas of Cornell University. 7*54239Sbostic * 8*54239Sbostic * %sccs.include.redist.c% 9*54239Sbostic */ 10*54239Sbostic 11*54239Sbostic #ifndef lint 12*54239Sbostic char copyright[] = 13*54239Sbostic "@(#) Copyright (c) 1992 The Regents of the University of California.\n\ 14*54239Sbostic All rights reserved.\n"; 15*54239Sbostic #endif /* not lint */ 16*54239Sbostic 17*54239Sbostic #ifndef lint 18*54239Sbostic static char sccsid[] = "@(#)test.c 5.1 (Berkeley) 06/22/92"; 19*54239Sbostic #endif /* not lint */ 20*54239Sbostic 21*54239Sbostic /* 22*54239Sbostic * el.test.c: A little test program 23*54239Sbostic */ 24*54239Sbostic #include "sys.h" 25*54239Sbostic #include <stdio.h> 26*54239Sbostic #include <string.h> 27*54239Sbostic #include <signal.h> 28*54239Sbostic #include <sys/wait.h> 29*54239Sbostic #include <ctype.h> 30*54239Sbostic #include <stdlib.h> 31*54239Sbostic #include <unistd.h> 32*54239Sbostic #include <dirent.h> 33*54239Sbostic 34*54239Sbostic #include "editline.h" 35*54239Sbostic #include "tokenizer.h" 36*54239Sbostic #include "history.h" 37*54239Sbostic 38*54239Sbostic static int continuation = 0; 39*54239Sbostic static EditLine *el = NULL; 40*54239Sbostic 41*54239Sbostic static char * 42*54239Sbostic /*ARGSUSED*/ 43*54239Sbostic prompt(el) 44*54239Sbostic EditLine *el; 45*54239Sbostic { 46*54239Sbostic static char a[] = "Edit$"; 47*54239Sbostic static char b[] = "Edit>"; 48*54239Sbostic return continuation ? b : a; 49*54239Sbostic } 50*54239Sbostic 51*54239Sbostic static void 52*54239Sbostic sig(i) 53*54239Sbostic int i; 54*54239Sbostic { 55*54239Sbostic (void) fprintf(stderr, "Got signal %d.\n", i); 56*54239Sbostic el_reset(el); 57*54239Sbostic } 58*54239Sbostic 59*54239Sbostic static unsigned char 60*54239Sbostic /*ARGSUSED*/ 61*54239Sbostic complete(el, ch) 62*54239Sbostic EditLine *el; 63*54239Sbostic int ch; 64*54239Sbostic { 65*54239Sbostic DIR *dd = opendir("."); 66*54239Sbostic struct dirent *dp; 67*54239Sbostic const char* ptr; 68*54239Sbostic const LineInfo *lf = el_line(el); 69*54239Sbostic int len; 70*54239Sbostic 71*54239Sbostic /* 72*54239Sbostic * Find the last word 73*54239Sbostic */ 74*54239Sbostic for (ptr = lf->cursor - 1; !isspace(*ptr) && ptr > lf->buffer; ptr--) 75*54239Sbostic continue; 76*54239Sbostic len = lf->cursor - ++ptr; 77*54239Sbostic 78*54239Sbostic for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) { 79*54239Sbostic if (len > strlen(dp->d_name)) 80*54239Sbostic continue; 81*54239Sbostic if (strncmp(dp->d_name, ptr, len) == 0) { 82*54239Sbostic closedir(dd); 83*54239Sbostic if (el_insertstr(el, &dp->d_name[len]) == -1) 84*54239Sbostic return CC_ERROR; 85*54239Sbostic else 86*54239Sbostic return CC_REFRESH; 87*54239Sbostic } 88*54239Sbostic } 89*54239Sbostic 90*54239Sbostic closedir(dd); 91*54239Sbostic return CC_ERROR; 92*54239Sbostic } 93*54239Sbostic 94*54239Sbostic int 95*54239Sbostic /*ARGSUSED*/ 96*54239Sbostic main(argc, argv) 97*54239Sbostic int argc; 98*54239Sbostic char *argv[]; 99*54239Sbostic { 100*54239Sbostic int num; 101*54239Sbostic const char *buf; 102*54239Sbostic Tokenizer *tok; 103*54239Sbostic History *hist; 104*54239Sbostic 105*54239Sbostic (void) signal(SIGINT, sig); 106*54239Sbostic (void) signal(SIGQUIT, sig); 107*54239Sbostic (void) signal(SIGHUP, sig); 108*54239Sbostic (void) signal(SIGTERM, sig); 109*54239Sbostic 110*54239Sbostic hist = history_init(); /* Init the builtin history */ 111*54239Sbostic history(hist, H_EVENT, 100); /* Remember 100 events */ 112*54239Sbostic 113*54239Sbostic tok = tok_init(NULL); /* Initialize the tokenizer */ 114*54239Sbostic 115*54239Sbostic el = el_init(*argv, stdin, stdout); /* Initialize editline */ 116*54239Sbostic 117*54239Sbostic el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */ 118*54239Sbostic el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */ 119*54239Sbostic el_set(el, EL_PROMPT, prompt); /* Set the prompt function */ 120*54239Sbostic 121*54239Sbostic /* Tell editline to use this history interface */ 122*54239Sbostic el_set(el, EL_HIST, history, hist); 123*54239Sbostic 124*54239Sbostic /* Add a user-defined function */ 125*54239Sbostic el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete); 126*54239Sbostic 127*54239Sbostic el_set(el, EL_BIND, "^I", "ed-complete", NULL);/* Bind tab to it */ 128*54239Sbostic /* 129*54239Sbostic * Bind j, k in vi command mode to previous and next line, instead 130*54239Sbostic * of previous and next history. 131*54239Sbostic */ 132*54239Sbostic el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL); 133*54239Sbostic el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL); 134*54239Sbostic 135*54239Sbostic while ((buf = el_gets(el, &num)) != NULL && num != 0) { 136*54239Sbostic int ac; 137*54239Sbostic char **av; 138*54239Sbostic #ifdef DEBUG 139*54239Sbostic (void) fprintf(stderr, "got %d %s", num, buf); 140*54239Sbostic #endif 141*54239Sbostic if (!continuation && num == 1) 142*54239Sbostic continue; 143*54239Sbostic if (tok_line(tok, buf, &ac, &av) > 0) { 144*54239Sbostic history(hist, continuation ? H_ADD : H_ENTER, buf); 145*54239Sbostic continuation = 1; 146*54239Sbostic continue; 147*54239Sbostic } 148*54239Sbostic history(hist, continuation ? H_ADD : H_ENTER, buf); 149*54239Sbostic 150*54239Sbostic continuation = 0; 151*54239Sbostic if (el_parse(el, ac, av) != -1) { 152*54239Sbostic tok_reset(tok); 153*54239Sbostic continue; 154*54239Sbostic } 155*54239Sbostic 156*54239Sbostic switch (fork()) { 157*54239Sbostic case 0: 158*54239Sbostic execvp(av[0], av); 159*54239Sbostic perror(av[0]); 160*54239Sbostic _exit(1); 161*54239Sbostic /*NOTREACHED*/ 162*54239Sbostic break; 163*54239Sbostic 164*54239Sbostic case -1: 165*54239Sbostic perror("fork"); 166*54239Sbostic break; 167*54239Sbostic 168*54239Sbostic default: 169*54239Sbostic if (wait(&num) == -1) 170*54239Sbostic perror("wait"); 171*54239Sbostic (void) fprintf(stderr, "Exit %x\n", num); 172*54239Sbostic break; 173*54239Sbostic } 174*54239Sbostic tok_reset(tok); 175*54239Sbostic } 176*54239Sbostic 177*54239Sbostic el_end(el); 178*54239Sbostic tok_end(tok); 179*54239Sbostic history_end(hist); 180*54239Sbostic 181*54239Sbostic return 0; 182*54239Sbostic } 183