1 /*
2  * Copyright (c) 2003, 2004, 2006 Tama Communications Corporation
3  *
4  * This file is part of GNU GLOBAL.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 #include <ctype.h>
23 #ifdef STDC_HEADERS
24 #include <stdlib.h>
25 #endif
26 #include "global.h"
27 #include "incop.h"
28 
29 #if defined(_WIN32) || defined(__DJGPP__)
30 static const char *
strtolower(const char * s)31 strtolower(const char *s)
32 {
33 	static char lower[MAXPATHLEN];
34 	char *t = lower, *end = lower + sizeof(lower);
35 
36 	do {
37 		if (t == end)
38 			die("name is too long.");
39 		/*
40 		 * In some systems, tolower() are #define macros,
41 		 * where the argument gets evaluated more than once.
42 		 */
43 		*t = tolower((unsigned char)*s);
44 		s++;
45 	} while (*t++ != '\0');
46 
47 	return lower;
48 }
49 #define HASH_KEY(name)	strtolower(name)
50 #else
51 #define HASH_KEY(name)	(name)
52 #endif
53 /*----------------------------------------------------------------------*/
54 /* Include path list							*/
55 /*----------------------------------------------------------------------*/
56 static STRHASH *head_inc;
57 
58 /**
59  * init_inc: initialize include file list.
60  */
61 void
init_inc(void)62 init_inc(void)
63 {
64 	head_inc = strhash_open(1024);
65 }
66 /**
67  * put_inc: put include file.
68  *
69  *	@param[in]	file	file name (the last component of the path)
70  *	@param[in]	path	path name or command line.
71  *	@param[in]	id	path id
72  */
73 void
put_inc(const char * file,const char * path,int id)74 put_inc(const char *file, const char *path, int id)
75 {
76 	struct sh_entry *entry;
77 	struct data *data;
78 
79 	entry = strhash_assign(head_inc, HASH_KEY(file), 1);
80 	data = entry->value;
81 	if (data == NULL) {
82 		data = (struct data *)check_malloc(sizeof(struct data));
83 #if defined(_WIN32) || defined(__DJGPP__)
84 		strlimcpy(data->name, file, sizeof(data->name));
85 #else
86 		data->name = entry->name;
87 #endif
88 		data->id = id;
89 		data->contents = strbuf_open(0);
90 		data->ref_contents = NULL;
91 		data->count = 0;
92 		data->ref_count = 0;
93 		entry->value = data;
94 	}
95 	strbuf_puts0(data->contents, path);
96 	data->count++;
97 }
98 /**
99  * get_inc: get include file.
100  *
101  *	@param[in]	name	path name or command line.
102  *	@return		descriptor
103  */
104 struct data *
get_inc(const char * name)105 get_inc(const char *name)
106 {
107 	struct sh_entry *entry = strhash_assign(head_inc, HASH_KEY(name), 0);
108 
109 	return entry ? entry->value : NULL;
110 }
111 /**
112  * first_inc: get the first include file.
113  *
114  *	@return		descriptor
115  */
116 struct data *
first_inc(void)117 first_inc(void)
118 {
119 	struct sh_entry *entry = strhash_first(head_inc);
120 
121 	return entry ? entry->value : NULL;
122 }
123 /**
124  * next_inc: get the next include file.
125  *
126  *	@return		descriptor
127  */
128 struct data *
next_inc(void)129 next_inc(void)
130 {
131 	struct sh_entry *entry = strhash_next(head_inc);
132 
133 	return entry ? entry->value : NULL;
134 }
135 
136 
137 /**
138  * put_included: put include file reference.
139  *
140  *	@param[in]	data	inc structure
141  *	@param[in]	path	path name or command line.
142  */
143 void
put_included(struct data * data,const char * path)144 put_included(struct data *data, const char *path)
145 {
146 	if (data->ref_contents == NULL)
147 		data->ref_contents = strbuf_open(0);
148 	strbuf_puts0(data->ref_contents, path);
149 	data->ref_count++;
150 }
151 /**
152  * get_included: get included file.
153  *
154  *	@param[in]	name	path name or command line.
155  *	@return		descriptor
156  */
157 struct data *
get_included(const char * name)158 get_included(const char *name)
159 {
160 	struct data *data = get_inc(name);
161 
162 	return (data && data->ref_count) ? data : NULL;
163 }
164 /*
165  * Terminate function is not needed.
166  */
167