154239Sbostic /*- 254239Sbostic * Copyright (c) 1992 The Regents of the University of California. 354239Sbostic * All rights reserved. 454239Sbostic * 554239Sbostic * This code is derived from software contributed to Berkeley by 654239Sbostic * Christos Zoulas of Cornell University. 754239Sbostic * 854239Sbostic * %sccs.include.redist.c% 954239Sbostic */ 1054239Sbostic 1154239Sbostic #ifndef lint 1254239Sbostic char copyright[] = 1354239Sbostic "@(#) Copyright (c) 1992 The Regents of the University of California.\n\ 1454239Sbostic All rights reserved.\n"; 1554239Sbostic #endif /* not lint */ 1654239Sbostic 1754239Sbostic #ifndef lint 18*54247Smarc static char sccsid[] = "@(#)test.c 5.2 (Berkeley) 06/22/92"; 1954239Sbostic #endif /* not lint */ 2054239Sbostic 2154239Sbostic /* 2254239Sbostic * el.test.c: A little test program 2354239Sbostic */ 2454239Sbostic #include "sys.h" 2554239Sbostic #include <stdio.h> 2654239Sbostic #include <string.h> 2754239Sbostic #include <signal.h> 2854239Sbostic #include <sys/wait.h> 2954239Sbostic #include <ctype.h> 3054239Sbostic #include <stdlib.h> 3154239Sbostic #include <unistd.h> 3254239Sbostic #include <dirent.h> 3354239Sbostic 34*54247Smarc #include "histedit.h" 3554239Sbostic #include "tokenizer.h" 3654239Sbostic 3754239Sbostic static int continuation = 0; 3854239Sbostic static EditLine *el = NULL; 3954239Sbostic 4054239Sbostic static char * 4154239Sbostic /*ARGSUSED*/ 4254239Sbostic prompt(el) 4354239Sbostic EditLine *el; 4454239Sbostic { 4554239Sbostic static char a[] = "Edit$"; 4654239Sbostic static char b[] = "Edit>"; 4754239Sbostic return continuation ? b : a; 4854239Sbostic } 4954239Sbostic 5054239Sbostic static void 5154239Sbostic sig(i) 5254239Sbostic int i; 5354239Sbostic { 5454239Sbostic (void) fprintf(stderr, "Got signal %d.\n", i); 5554239Sbostic el_reset(el); 5654239Sbostic } 5754239Sbostic 5854239Sbostic static unsigned char 5954239Sbostic /*ARGSUSED*/ 6054239Sbostic complete(el, ch) 6154239Sbostic EditLine *el; 6254239Sbostic int ch; 6354239Sbostic { 6454239Sbostic DIR *dd = opendir("."); 6554239Sbostic struct dirent *dp; 6654239Sbostic const char* ptr; 6754239Sbostic const LineInfo *lf = el_line(el); 6854239Sbostic int len; 6954239Sbostic 7054239Sbostic /* 7154239Sbostic * Find the last word 7254239Sbostic */ 7354239Sbostic for (ptr = lf->cursor - 1; !isspace(*ptr) && ptr > lf->buffer; ptr--) 7454239Sbostic continue; 7554239Sbostic len = lf->cursor - ++ptr; 7654239Sbostic 7754239Sbostic for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) { 7854239Sbostic if (len > strlen(dp->d_name)) 7954239Sbostic continue; 8054239Sbostic if (strncmp(dp->d_name, ptr, len) == 0) { 8154239Sbostic closedir(dd); 8254239Sbostic if (el_insertstr(el, &dp->d_name[len]) == -1) 8354239Sbostic return CC_ERROR; 8454239Sbostic else 8554239Sbostic return CC_REFRESH; 8654239Sbostic } 8754239Sbostic } 8854239Sbostic 8954239Sbostic closedir(dd); 9054239Sbostic return CC_ERROR; 9154239Sbostic } 9254239Sbostic 9354239Sbostic int 9454239Sbostic /*ARGSUSED*/ 9554239Sbostic main(argc, argv) 9654239Sbostic int argc; 9754239Sbostic char *argv[]; 9854239Sbostic { 9954239Sbostic int num; 10054239Sbostic const char *buf; 10154239Sbostic Tokenizer *tok; 10254239Sbostic History *hist; 10354239Sbostic 10454239Sbostic (void) signal(SIGINT, sig); 10554239Sbostic (void) signal(SIGQUIT, sig); 10654239Sbostic (void) signal(SIGHUP, sig); 10754239Sbostic (void) signal(SIGTERM, sig); 10854239Sbostic 10954239Sbostic hist = history_init(); /* Init the builtin history */ 11054239Sbostic history(hist, H_EVENT, 100); /* Remember 100 events */ 11154239Sbostic 11254239Sbostic tok = tok_init(NULL); /* Initialize the tokenizer */ 11354239Sbostic 11454239Sbostic el = el_init(*argv, stdin, stdout); /* Initialize editline */ 11554239Sbostic 11654239Sbostic el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */ 11754239Sbostic el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */ 11854239Sbostic el_set(el, EL_PROMPT, prompt); /* Set the prompt function */ 11954239Sbostic 12054239Sbostic /* Tell editline to use this history interface */ 12154239Sbostic el_set(el, EL_HIST, history, hist); 12254239Sbostic 12354239Sbostic /* Add a user-defined function */ 12454239Sbostic el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete); 12554239Sbostic 12654239Sbostic el_set(el, EL_BIND, "^I", "ed-complete", NULL);/* Bind tab to it */ 12754239Sbostic /* 12854239Sbostic * Bind j, k in vi command mode to previous and next line, instead 12954239Sbostic * of previous and next history. 13054239Sbostic */ 13154239Sbostic el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL); 13254239Sbostic el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL); 13354239Sbostic 13454239Sbostic while ((buf = el_gets(el, &num)) != NULL && num != 0) { 13554239Sbostic int ac; 13654239Sbostic char **av; 13754239Sbostic #ifdef DEBUG 13854239Sbostic (void) fprintf(stderr, "got %d %s", num, buf); 13954239Sbostic #endif 14054239Sbostic if (!continuation && num == 1) 14154239Sbostic continue; 14254239Sbostic if (tok_line(tok, buf, &ac, &av) > 0) { 14354239Sbostic history(hist, continuation ? H_ADD : H_ENTER, buf); 14454239Sbostic continuation = 1; 14554239Sbostic continue; 14654239Sbostic } 14754239Sbostic history(hist, continuation ? H_ADD : H_ENTER, buf); 14854239Sbostic 14954239Sbostic continuation = 0; 15054239Sbostic if (el_parse(el, ac, av) != -1) { 15154239Sbostic tok_reset(tok); 15254239Sbostic continue; 15354239Sbostic } 15454239Sbostic 15554239Sbostic switch (fork()) { 15654239Sbostic case 0: 15754239Sbostic execvp(av[0], av); 15854239Sbostic perror(av[0]); 15954239Sbostic _exit(1); 16054239Sbostic /*NOTREACHED*/ 16154239Sbostic break; 16254239Sbostic 16354239Sbostic case -1: 16454239Sbostic perror("fork"); 16554239Sbostic break; 16654239Sbostic 16754239Sbostic default: 16854239Sbostic if (wait(&num) == -1) 16954239Sbostic perror("wait"); 17054239Sbostic (void) fprintf(stderr, "Exit %x\n", num); 17154239Sbostic break; 17254239Sbostic } 17354239Sbostic tok_reset(tok); 17454239Sbostic } 17554239Sbostic 17654239Sbostic el_end(el); 17754239Sbostic tok_end(tok); 17854239Sbostic history_end(hist); 17954239Sbostic 18054239Sbostic return 0; 18154239Sbostic } 182