xref: /illumos-gate/usr/src/common/ficl/main.c (revision 9890ff83)
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 
46 #include <ficl.h>
47 #include <ficlplatform/emu.h>
48 #include <libtecla.h>
49 
50 #define	LINELEN	1024
51 #define	HISTORY	2048
52 
53 static char *
54 prompt(void)
55 {
56 	static char prompt[20]; /* probably too large, but well... */
57 	char *pr, *p, *cp, *ev;
58 	int n = 0;
59 
60 	if ((cp = getenv("prompt")) == NULL)
61 		cp = ">";
62 	pr = p = strdup(cp);
63 
64 	while (*p != 0) {
65 		if ((*p == '$') && (*(p+1) == '{')) {
66 			for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
67 				;
68 			*cp = 0;
69 			ev = getenv(p + 2);
70 
71 			if (ev != NULL)
72 				n = sprintf(prompt+n, "%s", ev);
73 			p = cp + 1;
74 			continue;
75 		}
76 		prompt[n++] = *p;
77 		p++;
78 	}
79 	if (prompt[n - 1] != ' ')
80 		prompt[n++] = ' ';
81 	prompt[n] = '\0';
82 	free(pr);
83 	return (prompt);
84 }
85 
86 int
87 main(int argc, char **argv)
88 {
89 	int returnValue = 0;
90 	char *buffer;
91 	GetLine *gl;
92 	ficlVm *vm;
93 	struct winsize ws;
94 	int cols = 80, rows = 24;
95 
96 	if (ioctl(1, TIOCGWINSZ, &ws) != -1) {
97 		if (ws.ws_col)
98 			cols = ws.ws_col;
99 		if (ws.ws_row)
100 			rows = ws.ws_row;
101 	}
102 
103 	clearenv();
104 	asprintf(&buffer, "%d", cols);
105 	setenv("screen-#cols", buffer, 1);
106 	free(buffer);
107 	asprintf(&buffer, "%d", rows);
108 	setenv("screen-#rows", buffer, 1);
109 	free(buffer);
110 
111 	if (getenv("prompt") == NULL)
112 		setenv("prompt", "${interpret}", 1);
113 	if (getenv("interpret") == NULL)
114 		setenv("interpret", "ok", 1);
115 
116 	if ((vm = bf_init("", NULL)) == NULL)
117 		return (ENOMEM);
118 	returnValue = ficlVmEvaluate(vm, ".ver cr quit");
119 
120 	/*
121 	 * load files specified on command-line
122 	 */
123 	if (argc  > 1) {
124 		asprintf(&buffer, ".( loading %s ) cr include %s\n cr",
125 		    argv[1], argv[1]);
126 		returnValue = ficlVmEvaluate(vm, buffer);
127 		free(buffer);
128 	}
129 
130 	if ((gl = new_GetLine(LINELEN, HISTORY)) == NULL) {
131 		bf_fini();
132 		return (ENOMEM);
133 	}
134 
135 	while (returnValue != FICL_VM_STATUS_USER_EXIT) {
136 		if ((buffer = gl_get_line(gl, prompt(), NULL, -1)) == NULL)
137 			break;
138 		returnValue = bf_run(buffer);
139 	}
140 
141 	gl = del_GetLine(gl);
142 	bf_fini();
143 	return (returnValue);
144 }
145