1 /*	$Id: main.c,v 1.14 2001/08/10 15:18:08 sandro Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2001 Sandro Sigala.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "config.h"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <err.h>
34 
35 #include "slisp.h"
36 #include "extern.h"
37 #include "version.h"
38 
39 /* Be verbose. */
40 int opt_verbose = 0;
41 /* Show warnings. */
42 int opt_lint = 0;
43 
process_input(void)44 static void process_input(void)
45 {
46 	objectp p, p1;
47 	int onatty = input_file == stdin && isatty(STDIN_FILENO);
48 
49 	init_lex();
50 
51 	for (;;) {
52 		if (onatty) {
53 			printf("%d/%d >> ", used_objs, free_objs);
54 			fflush(stdout);
55 		}
56 
57 		if ((p = parse_object(0)) == NULL)
58 			break;
59 
60 		if (opt_verbose) {
61 			printf("<<= ");
62 			princ_object(stdout, p);
63 			printf("\n");
64 		}
65 
66 		p1 = eval(p);
67 
68 		if (onatty || opt_verbose) {
69 			printf("==> ");
70 			princ_object(stdout, p1);
71 			printf("\n");
72 		}
73 
74 #ifdef DEBUG
75 		warnx(":: objects before collection");
76 		print_obj_lists();
77 #endif
78 		garbage_collect();
79 #ifdef DEBUG
80 		warnx(":: objects after collection");
81 		print_obj_lists();
82 #endif
83 	}
84 
85 	done_lex();
86 }
87 
process_file(char * filename)88 static void process_file(char *filename)
89 {
90 	if (filename != NULL && strcmp(filename, "-") != 0) {
91 		if ((input_file = fopen(filename, "r")) == NULL)
92 			err(1, "%s", filename);
93 	} else
94 		input_file = stdin;
95 
96 	process_input();
97 
98 	if (input_file != stdin)
99 		fclose(input_file);
100 }
101 
usage(void)102 static void usage(void)
103 {
104 	fprintf(stderr, "usage: slisp [-vVW] [file...]\n");
105 	exit(1);
106 }
107 
108 /*
109  * Used by the err() functions.
110  */
111 char *progname;
112 
main(int argc,char ** argv)113 int main(int argc, char **argv)
114 {
115 	int c;
116 
117 	progname = argv[0];
118 
119 	while ((c = getopt(argc, argv, "vVW")) != -1)
120 		switch (c) {
121 		case 'v':
122 			opt_verbose = 1;
123 			break;
124 		case 'V':
125 			fprintf(stderr, "%s\n", SLISP_VERSION);
126 			exit(0);
127 		case 'W':
128 			opt_lint = 1;
129 			break;
130 		case '?':
131 		default:
132 			usage();
133 			/* NOTREACHED */
134 		}
135 	argc -= optind;
136 	argv += optind;
137 
138 	init_objects();
139 
140 	if (argc < 1)
141 		process_file(NULL);
142 	else
143 		while (*argv)
144 			process_file(*argv++);
145 
146 	return 0;
147 }
148