1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Example trivial client program that uses the sparse library
3*1f5207b7SJohn Levon  * to tokenize, preprocess and parse a C file, and prints out
4*1f5207b7SJohn Levon  * the results.
5*1f5207b7SJohn Levon  *
6*1f5207b7SJohn Levon  * Copyright (C) 2003 Transmeta Corp.
7*1f5207b7SJohn Levon  *               2003 Linus Torvalds
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
10*1f5207b7SJohn Levon  * of this software and associated documentation files (the "Software"), to deal
11*1f5207b7SJohn Levon  * in the Software without restriction, including without limitation the rights
12*1f5207b7SJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13*1f5207b7SJohn Levon  * copies of the Software, and to permit persons to whom the Software is
14*1f5207b7SJohn Levon  * furnished to do so, subject to the following conditions:
15*1f5207b7SJohn Levon  *
16*1f5207b7SJohn Levon  * The above copyright notice and this permission notice shall be included in
17*1f5207b7SJohn Levon  * all copies or substantial portions of the Software.
18*1f5207b7SJohn Levon  *
19*1f5207b7SJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*1f5207b7SJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*1f5207b7SJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22*1f5207b7SJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23*1f5207b7SJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24*1f5207b7SJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25*1f5207b7SJohn Levon  * THE SOFTWARE.
26*1f5207b7SJohn Levon  */
27*1f5207b7SJohn Levon #include <stdarg.h>
28*1f5207b7SJohn Levon #include <stdlib.h>
29*1f5207b7SJohn Levon #include <stdio.h>
30*1f5207b7SJohn Levon #include <string.h>
31*1f5207b7SJohn Levon #include <ctype.h>
32*1f5207b7SJohn Levon #include <unistd.h>
33*1f5207b7SJohn Levon #include <fcntl.h>
34*1f5207b7SJohn Levon 
35*1f5207b7SJohn Levon #include "lib.h"
36*1f5207b7SJohn Levon #include "allocate.h"
37*1f5207b7SJohn Levon #include "token.h"
38*1f5207b7SJohn Levon #include "parse.h"
39*1f5207b7SJohn Levon #include "symbol.h"
40*1f5207b7SJohn Levon #include "expression.h"
41*1f5207b7SJohn Levon 
42*1f5207b7SJohn Levon static void clean_up_symbols(struct symbol_list *list)
43*1f5207b7SJohn Levon {
44*1f5207b7SJohn Levon 	struct symbol *sym;
45*1f5207b7SJohn Levon 
46*1f5207b7SJohn Levon 	FOR_EACH_PTR(list, sym) {
47*1f5207b7SJohn Levon 		expand_symbol(sym);
48*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR(sym);
49*1f5207b7SJohn Levon }
50*1f5207b7SJohn Levon 
51*1f5207b7SJohn Levon int main(int argc, char **argv)
52*1f5207b7SJohn Levon {
53*1f5207b7SJohn Levon 	struct symbol_list * list;
54*1f5207b7SJohn Levon 	struct string_list * filelist = NULL;
55*1f5207b7SJohn Levon 	char *file;
56*1f5207b7SJohn Levon 
57*1f5207b7SJohn Levon 	list = sparse_initialize(argc, argv, &filelist);
58*1f5207b7SJohn Levon 
59*1f5207b7SJohn Levon 	// Simplification
60*1f5207b7SJohn Levon 	clean_up_symbols(list);
61*1f5207b7SJohn Levon 
62*1f5207b7SJohn Levon #if 1
63*1f5207b7SJohn Levon 	show_symbol_list(list, "\n\n");
64*1f5207b7SJohn Levon 	printf("\n\n");
65*1f5207b7SJohn Levon #endif
66*1f5207b7SJohn Levon 
67*1f5207b7SJohn Levon 	FOR_EACH_PTR_NOTAG(filelist, file) {
68*1f5207b7SJohn Levon 		list = sparse(file);
69*1f5207b7SJohn Levon 
70*1f5207b7SJohn Levon 		// Simplification
71*1f5207b7SJohn Levon 		clean_up_symbols(list);
72*1f5207b7SJohn Levon 
73*1f5207b7SJohn Levon #if 1
74*1f5207b7SJohn Levon 		// Show the end result.
75*1f5207b7SJohn Levon 		show_symbol_list(list, "\n\n");
76*1f5207b7SJohn Levon 		printf("\n\n");
77*1f5207b7SJohn Levon #endif
78*1f5207b7SJohn Levon 	} END_FOR_EACH_PTR_NOTAG(file);
79*1f5207b7SJohn Levon 
80*1f5207b7SJohn Levon #if 0
81*1f5207b7SJohn Levon 	// And show the allocation statistics
82*1f5207b7SJohn Levon 	show_ident_alloc();
83*1f5207b7SJohn Levon 	show_token_alloc();
84*1f5207b7SJohn Levon 	show_symbol_alloc();
85*1f5207b7SJohn Levon 	show_expression_alloc();
86*1f5207b7SJohn Levon 	show_statement_alloc();
87*1f5207b7SJohn Levon 	show_string_alloc();
88*1f5207b7SJohn Levon 	show_bytes_alloc();
89*1f5207b7SJohn Levon #endif
90*1f5207b7SJohn Levon 	return 0;
91*1f5207b7SJohn Levon }
92