xref: /illumos-gate/usr/src/common/ficl/main.c (revision a1d41cf940fc4cda50098ad61e6a78b19c7483cd)
1 /*
2  * stub main for testing Ficl
3  * $Id: main.c,v 1.2 2010/09/10 09:01:28 asau Exp $
4  */
5 /*
6  * Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
7  * All rights reserved.
8  *
9  * Get the latest Ficl release at http://ficl.sourceforge.net
10  *
11  * I am interested in hearing from anyone who uses Ficl. If you have
12  * a problem, a success story, a defect, an enhancement request, or
13  * if you would like to contribute to the Ficl release, please
14  * contact me by email at the address above.
15  *
16  * L I C E N S E  and  D I S C L A I M E R
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <termios.h>
44 #include <sys/errno.h>
45 #include <err.h>
46 
47 #include <ficl.h>
48 #include <ficlplatform/emu.h>
49 #include <libtecla.h>
50 
51 #define	LINELEN	1024
52 #define	HISTORY	2048
53 
54 static char *
55 prompt(void)
56 {
57 	static char prompt[20]; /* probably too large, but well... */
58 	char *pr, *p, *cp, *ev;
59 	int n = 0;
60 
61 	if ((cp = getenv("prompt")) == NULL)
62 		cp = ">";
63 	pr = p = strdup(cp);
64 
65 	while (*p != 0) {
66 		if ((*p == '$') && (*(p+1) == '{')) {
67 			for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
68 				;
69 			*cp = 0;
70 			ev = getenv(p + 2);
71 
72 			if (ev != NULL)
73 				n = sprintf(prompt+n, "%s", ev);
74 			p = cp + 1;
75 			continue;
76 		}
77 		prompt[n++] = *p;
78 		p++;
79 	}
80 	if (prompt[n - 1] != ' ')
81 		prompt[n++] = ' ';
82 	prompt[n] = '\0';
83 	free(pr);
84 	return (prompt);
85 }
86 
87 int
88 main(int argc, char **argv)
89 {
90 	int returnValue = 0;
91 	char *buffer;
92 	GetLine *gl;
93 	ficlVm *vm;
94 	struct winsize ws;
95 	int cols = 80, rows = 24;
96 
97 	if (ioctl(1, TIOCGWINSZ, &ws) != -1) {
98 		if (ws.ws_col)
99 			cols = ws.ws_col;
100 		if (ws.ws_row)
101 			rows = ws.ws_row;
102 	}
103 
104 	(void) clearenv();
105 	if (asprintf(&buffer, "%d", cols) < 0)
106 		err(EXIT_FAILURE, NULL);
107 	(void) setenv("screen-#cols", buffer, 1);
108 	free(buffer);
109 	if (asprintf(&buffer, "%d", rows) < 0)
110 		err(EXIT_FAILURE, NULL);
111 	(void) setenv("screen-#rows", buffer, 1);
112 	free(buffer);
113 
114 	if (getenv("prompt") == NULL)
115 		(void) setenv("prompt", "${interpret}", 1);
116 	if (getenv("interpret") == NULL)
117 		(void) setenv("interpret", "ok", 1);
118 
119 	if ((vm = bf_init("", NULL)) == NULL)
120 		err(EXIT_FAILURE, NULL);
121 	returnValue = ficlVmEvaluate(vm, ".ver cr quit");
122 
123 	/*
124 	 * load files specified on command-line
125 	 */
126 	if (argc  > 1) {
127 		if (asprintf(&buffer, ".( loading %s ) cr include %s\n cr",
128 		    argv[1], argv[1]) < 0)
129 			err(EXIT_FAILURE, NULL);
130 		returnValue = ficlVmEvaluate(vm, buffer);
131 		free(buffer);
132 	}
133 
134 	if ((gl = new_GetLine(LINELEN, HISTORY)) == NULL) {
135 		bf_fini();
136 		err(EXIT_FAILURE, NULL);
137 	}
138 
139 	while (returnValue != FICL_VM_STATUS_USER_EXIT) {
140 		if ((buffer = gl_get_line(gl, prompt(), NULL, -1)) == NULL)
141 			break;
142 		returnValue = bf_run(buffer);
143 	}
144 
145 	gl = del_GetLine(gl);
146 	bf_fini();
147 	if (returnValue != FICL_VM_STATUS_USER_EXIT)
148 		return (EXIT_FAILURE);
149 	return (EXIT_SUCCESS);
150 }
151