154239Sbostic /*-
2*61276Sbostic * Copyright (c) 1992, 1993
3*61276Sbostic * The Regents of the University of California. 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
12*61276Sbostic static char copyright[] =
13*61276Sbostic "@(#) Copyright (c) 1992, 1993\n\
14*61276Sbostic The Regents of the University of California. All rights reserved.\n";
1554239Sbostic #endif /* not lint */
1654239Sbostic
1754624Schristos #if !defined(lint) && !defined(SCCSID)
18*61276Sbostic static char sccsid[] = "@(#)test.c 8.1 (Berkeley) 06/04/93";
1954624Schristos #endif /* not lint && not SCCSID */
2054239Sbostic
2154239Sbostic /*
2254624Schristos * 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
3454247Smarc #include "histedit.h"
3554239Sbostic #include "tokenizer.h"
3654239Sbostic
3754239Sbostic static int continuation = 0;
3854239Sbostic static EditLine *el = NULL;
3954239Sbostic
4054239Sbostic static char *
4154239Sbostic /*ARGSUSED*/
prompt(el)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
sig(i)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*/
complete(el,ch)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*/
main(argc,argv)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 */
12754624Schristos
12854239Sbostic /*
12954239Sbostic * Bind j, k in vi command mode to previous and next line, instead
13054239Sbostic * of previous and next history.
13154239Sbostic */
13254239Sbostic el_set(el, EL_BIND, "-a", "k", "ed-prev-line", NULL);
13354239Sbostic el_set(el, EL_BIND, "-a", "j", "ed-next-line", NULL);
13454239Sbostic
13554624Schristos /*
13654624Schristos * Source the user's defaults file.
13754624Schristos */
13854624Schristos el_source(el, NULL);
13954624Schristos
14054239Sbostic while ((buf = el_gets(el, &num)) != NULL && num != 0) {
14154239Sbostic int ac;
14254239Sbostic char **av;
14354239Sbostic #ifdef DEBUG
14454239Sbostic (void) fprintf(stderr, "got %d %s", num, buf);
14554239Sbostic #endif
14654239Sbostic if (!continuation && num == 1)
14754239Sbostic continue;
14854239Sbostic if (tok_line(tok, buf, &ac, &av) > 0) {
14954239Sbostic history(hist, continuation ? H_ADD : H_ENTER, buf);
15054239Sbostic continuation = 1;
15154239Sbostic continue;
15254239Sbostic }
15354239Sbostic history(hist, continuation ? H_ADD : H_ENTER, buf);
15454239Sbostic
15554239Sbostic continuation = 0;
15654239Sbostic if (el_parse(el, ac, av) != -1) {
15754239Sbostic tok_reset(tok);
15854239Sbostic continue;
15954239Sbostic }
16054239Sbostic
16154239Sbostic switch (fork()) {
16254239Sbostic case 0:
16354239Sbostic execvp(av[0], av);
16454239Sbostic perror(av[0]);
16554239Sbostic _exit(1);
16654239Sbostic /*NOTREACHED*/
16754239Sbostic break;
16854239Sbostic
16954239Sbostic case -1:
17054239Sbostic perror("fork");
17154239Sbostic break;
17254239Sbostic
17354239Sbostic default:
17454239Sbostic if (wait(&num) == -1)
17554239Sbostic perror("wait");
17654239Sbostic (void) fprintf(stderr, "Exit %x\n", num);
17754239Sbostic break;
17854239Sbostic }
17954239Sbostic tok_reset(tok);
18054239Sbostic }
18154239Sbostic
18254239Sbostic el_end(el);
18354239Sbostic tok_end(tok);
18454239Sbostic history_end(hist);
18554239Sbostic
18654239Sbostic return 0;
18754239Sbostic }
188