11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Example trivial client program that uses the sparse library
31f5207b7SJohn Levon  * to tokenize, preprocess and parse a C file, and prints out
41f5207b7SJohn Levon  * the results.
51f5207b7SJohn Levon  *
61f5207b7SJohn Levon  * Copyright (C) 2003 Transmeta Corp.
71f5207b7SJohn Levon  *               2003 Linus Torvalds
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
101f5207b7SJohn Levon  * of this software and associated documentation files (the "Software"), to deal
111f5207b7SJohn Levon  * in the Software without restriction, including without limitation the rights
121f5207b7SJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
131f5207b7SJohn Levon  * copies of the Software, and to permit persons to whom the Software is
141f5207b7SJohn Levon  * furnished to do so, subject to the following conditions:
151f5207b7SJohn Levon  *
161f5207b7SJohn Levon  * The above copyright notice and this permission notice shall be included in
171f5207b7SJohn Levon  * all copies or substantial portions of the Software.
181f5207b7SJohn Levon  *
191f5207b7SJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
201f5207b7SJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
211f5207b7SJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
221f5207b7SJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
231f5207b7SJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
241f5207b7SJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
251f5207b7SJohn Levon  * THE SOFTWARE.
261f5207b7SJohn Levon  */
271f5207b7SJohn Levon #include <stdarg.h>
281f5207b7SJohn Levon #include <stdlib.h>
291f5207b7SJohn Levon #include <stdio.h>
301f5207b7SJohn Levon #include <string.h>
311f5207b7SJohn Levon #include <ctype.h>
321f5207b7SJohn Levon #include <unistd.h>
331f5207b7SJohn Levon #include <fcntl.h>
341f5207b7SJohn Levon 
351f5207b7SJohn Levon #include "lib.h"
361f5207b7SJohn Levon #include "allocate.h"
371f5207b7SJohn Levon #include "token.h"
381f5207b7SJohn Levon #include "parse.h"
391f5207b7SJohn Levon #include "symbol.h"
401f5207b7SJohn Levon #include "expression.h"
411f5207b7SJohn Levon 
clean_up_symbols(struct symbol_list * list)421f5207b7SJohn Levon static void clean_up_symbols(struct symbol_list *list)
431f5207b7SJohn Levon {
441f5207b7SJohn Levon 	struct symbol *sym;
451f5207b7SJohn Levon 
461f5207b7SJohn Levon 	FOR_EACH_PTR(list, sym) {
471f5207b7SJohn Levon 		expand_symbol(sym);
481f5207b7SJohn Levon 	} END_FOR_EACH_PTR(sym);
491f5207b7SJohn Levon }
501f5207b7SJohn Levon 
main(int argc,char ** argv)511f5207b7SJohn Levon int main(int argc, char **argv)
521f5207b7SJohn Levon {
531f5207b7SJohn Levon 	struct symbol_list * list;
541f5207b7SJohn Levon 	struct string_list * filelist = NULL;
551f5207b7SJohn Levon 	char *file;
561f5207b7SJohn Levon 
571f5207b7SJohn Levon 	list = sparse_initialize(argc, argv, &filelist);
581f5207b7SJohn Levon 
591f5207b7SJohn Levon 	// Simplification
601f5207b7SJohn Levon 	clean_up_symbols(list);
611f5207b7SJohn Levon 
621f5207b7SJohn Levon #if 1
631f5207b7SJohn Levon 	show_symbol_list(list, "\n\n");
641f5207b7SJohn Levon 	printf("\n\n");
651f5207b7SJohn Levon #endif
661f5207b7SJohn Levon 
67*c85f09ccSJohn Levon 	FOR_EACH_PTR(filelist, file) {
681f5207b7SJohn Levon 		list = sparse(file);
691f5207b7SJohn Levon 
701f5207b7SJohn Levon 		// Simplification
711f5207b7SJohn Levon 		clean_up_symbols(list);
721f5207b7SJohn Levon 
731f5207b7SJohn Levon #if 1
741f5207b7SJohn Levon 		// Show the end result.
751f5207b7SJohn Levon 		show_symbol_list(list, "\n\n");
761f5207b7SJohn Levon 		printf("\n\n");
771f5207b7SJohn Levon #endif
78*c85f09ccSJohn Levon 	} END_FOR_EACH_PTR(file);
791f5207b7SJohn Levon 
801f5207b7SJohn Levon #if 0
811f5207b7SJohn Levon 	// And show the allocation statistics
821f5207b7SJohn Levon 	show_ident_alloc();
831f5207b7SJohn Levon 	show_token_alloc();
841f5207b7SJohn Levon 	show_symbol_alloc();
851f5207b7SJohn Levon 	show_expression_alloc();
861f5207b7SJohn Levon 	show_statement_alloc();
871f5207b7SJohn Levon 	show_string_alloc();
881f5207b7SJohn Levon 	show_bytes_alloc();
891f5207b7SJohn Levon #endif
901f5207b7SJohn Levon 	return 0;
911f5207b7SJohn Levon }
92