xref: /haiku/src/bin/rc/rdef.cpp (revision e221c09e508ffc3c62738140c9b6fc4fa211662a)
1*e221c09eSPhilippe Houdoin /*
2*e221c09eSPhilippe Houdoin  * Copyright (c) 2003 Matthijs Hollemans
3*e221c09eSPhilippe Houdoin  *
4*e221c09eSPhilippe Houdoin  * Permission is hereby granted, free of charge, to any person obtaining a
5*e221c09eSPhilippe Houdoin  * copy of this software and associated documentation files (the "Software"),
6*e221c09eSPhilippe Houdoin  * to deal in the Software without restriction, including without limitation
7*e221c09eSPhilippe Houdoin  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*e221c09eSPhilippe Houdoin  * and/or sell copies of the Software, and to permit persons to whom the
9*e221c09eSPhilippe Houdoin  * Software is furnished to do so, subject to the following conditions:
10*e221c09eSPhilippe Houdoin  *
11*e221c09eSPhilippe Houdoin  * The above copyright notice and this permission notice shall be included in
12*e221c09eSPhilippe Houdoin  * all copies or substantial portions of the Software.
13*e221c09eSPhilippe Houdoin  *
14*e221c09eSPhilippe Houdoin  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*e221c09eSPhilippe Houdoin  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*e221c09eSPhilippe Houdoin  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17*e221c09eSPhilippe Houdoin  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18*e221c09eSPhilippe Houdoin  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19*e221c09eSPhilippe Houdoin  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20*e221c09eSPhilippe Houdoin  * DEALINGS IN THE SOFTWARE.
21*e221c09eSPhilippe Houdoin  */
22*e221c09eSPhilippe Houdoin 
23*e221c09eSPhilippe Houdoin #include <stdio.h>
24*e221c09eSPhilippe Houdoin #include <stdlib.h>
25*e221c09eSPhilippe Houdoin #include <string.h>
26*e221c09eSPhilippe Houdoin #include <unistd.h>
27*e221c09eSPhilippe Houdoin 
28*e221c09eSPhilippe Houdoin #include "rdef.h"
29*e221c09eSPhilippe Houdoin #include "private.h"
30*e221c09eSPhilippe Houdoin 
31*e221c09eSPhilippe Houdoin ptr_list_t include_dirs;
32*e221c09eSPhilippe Houdoin ptr_list_t input_files;
33*e221c09eSPhilippe Houdoin 
34*e221c09eSPhilippe Houdoin uint32 flags = 0;
35*e221c09eSPhilippe Houdoin 
36*e221c09eSPhilippe Houdoin status_t rdef_err;
37*e221c09eSPhilippe Houdoin int32 rdef_err_line;
38*e221c09eSPhilippe Houdoin char rdef_err_file[B_PATH_NAME_LENGTH];
39*e221c09eSPhilippe Houdoin char rdef_err_msg[1024];
40*e221c09eSPhilippe Houdoin 
41*e221c09eSPhilippe Houdoin 
42*e221c09eSPhilippe Houdoin void
free_ptr_list(ptr_list_t & list)43*e221c09eSPhilippe Houdoin free_ptr_list(ptr_list_t &list)
44*e221c09eSPhilippe Houdoin {
45*e221c09eSPhilippe Houdoin 	for (ptr_iter_t i = list.begin(); i != list.end(); ++i) {
46*e221c09eSPhilippe Houdoin 		free(*i);
47*e221c09eSPhilippe Houdoin 	}
48*e221c09eSPhilippe Houdoin 
49*e221c09eSPhilippe Houdoin 	list.clear();
50*e221c09eSPhilippe Houdoin }
51*e221c09eSPhilippe Houdoin 
52*e221c09eSPhilippe Houdoin 
53*e221c09eSPhilippe Houdoin int32
rdef_get_version()54*e221c09eSPhilippe Houdoin rdef_get_version()
55*e221c09eSPhilippe Houdoin {
56*e221c09eSPhilippe Houdoin 	return 2;
57*e221c09eSPhilippe Houdoin }
58*e221c09eSPhilippe Houdoin 
59*e221c09eSPhilippe Houdoin 
60*e221c09eSPhilippe Houdoin status_t
rdef_add_include_dir(const char * dir,bool toEndOfList)61*e221c09eSPhilippe Houdoin rdef_add_include_dir(const char *dir, bool toEndOfList)
62*e221c09eSPhilippe Houdoin {
63*e221c09eSPhilippe Houdoin 	clear_error();
64*e221c09eSPhilippe Houdoin 
65*e221c09eSPhilippe Houdoin 	char *path = (char *)malloc(strlen(dir) + 2);
66*e221c09eSPhilippe Houdoin 	if (path == NULL) {
67*e221c09eSPhilippe Houdoin 		rdef_err = B_NO_MEMORY;
68*e221c09eSPhilippe Houdoin 		return B_NO_MEMORY;
69*e221c09eSPhilippe Houdoin 	}
70*e221c09eSPhilippe Houdoin 
71*e221c09eSPhilippe Houdoin 	strcpy(path, dir);
72*e221c09eSPhilippe Houdoin 	strcat(path, "/");
73*e221c09eSPhilippe Houdoin 
74*e221c09eSPhilippe Houdoin 	if (toEndOfList)
75*e221c09eSPhilippe Houdoin 		include_dirs.push_back(path);
76*e221c09eSPhilippe Houdoin 	else
77*e221c09eSPhilippe Houdoin 		include_dirs.push_front(path);
78*e221c09eSPhilippe Houdoin 
79*e221c09eSPhilippe Houdoin 	return B_OK;
80*e221c09eSPhilippe Houdoin }
81*e221c09eSPhilippe Houdoin 
82*e221c09eSPhilippe Houdoin 
83*e221c09eSPhilippe Houdoin status_t
rdef_remove_include_dir(const char * dir)84*e221c09eSPhilippe Houdoin rdef_remove_include_dir(const char *dir)
85*e221c09eSPhilippe Houdoin {
86*e221c09eSPhilippe Houdoin 	size_t length = strlen(dir);
87*e221c09eSPhilippe Houdoin 	bool noSlash = false;
88*e221c09eSPhilippe Houdoin 	if (dir[length - 1] != '/')
89*e221c09eSPhilippe Houdoin 		noSlash = true;
90*e221c09eSPhilippe Houdoin 
91*e221c09eSPhilippe Houdoin 	for (ptr_iter_t i = include_dirs.begin(); i != include_dirs.end(); ++i) {
92*e221c09eSPhilippe Houdoin 		char *path = (char *)*i;
93*e221c09eSPhilippe Houdoin 
94*e221c09eSPhilippe Houdoin 		if (!strncmp(dir, path, length)
95*e221c09eSPhilippe Houdoin 			&& path[length + (noSlash ? 1 : 0)] == '\0') {
96*e221c09eSPhilippe Houdoin 			// we found the entry in the list, let's remove it
97*e221c09eSPhilippe Houdoin 			include_dirs.erase(i);
98*e221c09eSPhilippe Houdoin 			free(path);
99*e221c09eSPhilippe Houdoin 			return B_OK;
100*e221c09eSPhilippe Houdoin 		}
101*e221c09eSPhilippe Houdoin 	}
102*e221c09eSPhilippe Houdoin 
103*e221c09eSPhilippe Houdoin 	return B_ENTRY_NOT_FOUND;
104*e221c09eSPhilippe Houdoin }
105*e221c09eSPhilippe Houdoin 
106*e221c09eSPhilippe Houdoin 
107*e221c09eSPhilippe Houdoin void
rdef_free_include_dirs()108*e221c09eSPhilippe Houdoin rdef_free_include_dirs()
109*e221c09eSPhilippe Houdoin {
110*e221c09eSPhilippe Houdoin 	free_ptr_list(include_dirs);
111*e221c09eSPhilippe Houdoin }
112*e221c09eSPhilippe Houdoin 
113*e221c09eSPhilippe Houdoin 
114*e221c09eSPhilippe Houdoin status_t
rdef_add_input_file(const char * file)115*e221c09eSPhilippe Houdoin rdef_add_input_file(const char *file)
116*e221c09eSPhilippe Houdoin {
117*e221c09eSPhilippe Houdoin 	clear_error();
118*e221c09eSPhilippe Houdoin 
119*e221c09eSPhilippe Houdoin 	char *temp = strdup(file);
120*e221c09eSPhilippe Houdoin 	if (temp == NULL) {
121*e221c09eSPhilippe Houdoin 		rdef_err = B_NO_MEMORY;
122*e221c09eSPhilippe Houdoin 		return B_NO_MEMORY;
123*e221c09eSPhilippe Houdoin 	}
124*e221c09eSPhilippe Houdoin 
125*e221c09eSPhilippe Houdoin 	input_files.push_back(temp);
126*e221c09eSPhilippe Houdoin 	return B_OK;
127*e221c09eSPhilippe Houdoin }
128*e221c09eSPhilippe Houdoin 
129*e221c09eSPhilippe Houdoin 
130*e221c09eSPhilippe Houdoin void
rdef_free_input_files()131*e221c09eSPhilippe Houdoin rdef_free_input_files()
132*e221c09eSPhilippe Houdoin {
133*e221c09eSPhilippe Houdoin 	free_ptr_list(input_files);
134*e221c09eSPhilippe Houdoin }
135*e221c09eSPhilippe Houdoin 
136*e221c09eSPhilippe Houdoin 
137*e221c09eSPhilippe Houdoin void
rdef_set_flags(uint32 flags_)138*e221c09eSPhilippe Houdoin rdef_set_flags(uint32 flags_)
139*e221c09eSPhilippe Houdoin {
140*e221c09eSPhilippe Houdoin 	flags = flags_;
141*e221c09eSPhilippe Houdoin }
142*e221c09eSPhilippe Houdoin 
143*e221c09eSPhilippe Houdoin 
144*e221c09eSPhilippe Houdoin void
rdef_clear_flags()145*e221c09eSPhilippe Houdoin rdef_clear_flags()
146*e221c09eSPhilippe Houdoin {
147*e221c09eSPhilippe Houdoin 	flags = 0;
148*e221c09eSPhilippe Houdoin }
149*e221c09eSPhilippe Houdoin 
150*e221c09eSPhilippe Houdoin 
151*e221c09eSPhilippe Houdoin void
clear_error()152*e221c09eSPhilippe Houdoin clear_error()
153*e221c09eSPhilippe Houdoin {
154*e221c09eSPhilippe Houdoin 	rdef_err = B_OK;
155*e221c09eSPhilippe Houdoin 	rdef_err_line = 0;
156*e221c09eSPhilippe Houdoin 	rdef_err_file[0] = '\0';
157*e221c09eSPhilippe Houdoin 	rdef_err_msg[0] = '\0';
158*e221c09eSPhilippe Houdoin }
159*e221c09eSPhilippe Houdoin 
160*e221c09eSPhilippe Houdoin 
161*e221c09eSPhilippe Houdoin bool
open_file_from_include_dir(const char * filename,char * outname)162*e221c09eSPhilippe Houdoin open_file_from_include_dir(const char *filename, char *outname)
163*e221c09eSPhilippe Houdoin {
164*e221c09eSPhilippe Houdoin 	for (ptr_iter_t i = include_dirs.begin(); i != include_dirs.end(); ++i) {
165*e221c09eSPhilippe Houdoin 		char tmpname[B_PATH_NAME_LENGTH];
166*e221c09eSPhilippe Houdoin 		strcpy(tmpname, (char *)*i);
167*e221c09eSPhilippe Houdoin 		strcat(tmpname, filename);
168*e221c09eSPhilippe Houdoin 
169*e221c09eSPhilippe Houdoin 		if (access(tmpname, R_OK) == 0) {
170*e221c09eSPhilippe Houdoin 			strcpy(outname, tmpname);
171*e221c09eSPhilippe Houdoin 			return true;
172*e221c09eSPhilippe Houdoin 		}
173*e221c09eSPhilippe Houdoin 	}
174*e221c09eSPhilippe Houdoin 
175*e221c09eSPhilippe Houdoin 	return false;
176*e221c09eSPhilippe Houdoin }
177*e221c09eSPhilippe Houdoin 
178