* configure.in: Fixed up so that ucsim is only configured once.
[fw/sdcc] / src / SDCCutil.c
1 /*-------------------------------------------------------------------------
2   SDCCutil.c - Small utility functions.
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    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, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #include "common.h"
26 #include "SDCCmacro.h"
27 #include "SDCCutil.h"
28 #include "newalloc.h"
29 #include <sys/stat.h>
30
31 /** Given an array of name, value string pairs creates a new hash
32     containing all of the pairs.
33 */
34 hTab *
35 populateStringHash(const char **pin)
36 {
37   hTab *pret = NULL;
38
39   while (*pin)
40     {
41       shash_add (&pret, pin[0], pin[1]);
42       pin += 2;
43     }
44
45   return pret;
46 }
47
48 /** Given an array of string pointers and another string, adds the
49     string to the end of the list.  The end of the list is assumed to
50     be the first NULL pointer.
51 */
52 void
53 addToList (const char **list, const char *str)
54 {
55   /* This is the bad way to do things :) */
56   while (*list)
57     list++;
58   *list = strdup (str);
59   if (!*list)
60     {
61       werror (E_OUT_OF_MEM, __FILE__, 0);
62       exit (1);
63     }
64   *(++list) = NULL;
65 }
66
67 /** Given an array of string pointers returns a string containing all
68     of the strings seperated by spaces.  The returned string is on the
69     heap.  The join stops when a NULL pointer is hit.
70 */
71 char *
72 join(const char **pplist)
73 {
74   char *pinto = buffer;
75   *pinto = '\0';
76
77   while (*pplist)
78     {
79       strcpy(pinto, *pplist);
80       pinto += strlen(*pplist);
81       *pinto++ = ' ';
82       pplist++;
83     }
84
85   return buffer;
86 }
87
88 /** Given an array of string pointers, returns a string containing all
89     of the strings seperated by spaces.  The returned string is on the
90     heap.  n is the number of strings in the list.
91 */
92 char *
93 joinn(char **pplist, int n)
94 {
95   char *pinto = buffer;
96   *pinto = '\0';
97
98   while (n--)
99     {
100       strcpy(pinto, *pplist);
101       pinto += strlen(*pplist);
102       *pinto++ = ' ';
103       pplist++;
104     }
105   *pinto = '\0';
106
107   return buffer;
108 }
109
110 /** Returns TRUE if for the host the two path characters are
111     equivalent.
112 */
113 static bool
114 pathCharsEquivalent(char c1, char c2)
115 {
116 #if NATIVE_WIN32
117   /* win32 is case insensitive */
118   if (tolower(c1) == tolower(c2))
119     {
120       return TRUE;
121     }
122   /* And / is equivalent to \ */
123   else if (c1 == '/' && c2 == '\\')
124     {
125       return TRUE;
126     }
127   else if (c1 == '\\' && c2 == '/')
128     {
129       return TRUE;
130     }
131   else
132     {
133       return FALSE;
134     }
135 #else
136   /* Assume a Unix host where they must match exactly. */
137   return c1 == c2;
138 #endif
139 }
140
141 static bool
142 pathEquivalent(const char *p1, const char *p2)
143 {
144   while (*p1 != '\0' && *p2 != '\0')
145     {
146       if (pathCharsEquivalent (*p1, *p2) == FALSE)
147         {
148           break;
149         }
150       p1++;
151       p2++;
152     }
153
154   return *p1 == *p2;
155 }
156
157 static char
158 pathCharTransform(char c)
159 {
160 #if NATIVE_WIN32
161   if (c == '/')
162     {
163       return DIR_SEPARATOR_CHAR;
164     }
165   else
166     {
167       return c;
168     }
169 #else
170   return c;
171 #endif
172 }
173
174 /** Fixes up a potentially mixed path to the proper representation for
175     the host.  Fixes up in place.
176 */
177 static char *
178 fixupPath(char *pin)
179 {
180   char *p = pin;
181
182   while (*p)
183     {
184       *p = pathCharTransform(*p);
185       p++;
186     }
187   *p = '\0';
188
189   return pin;
190 }
191
192 /** Returns the characters in p2 past the last matching characters in
193     p1.  
194 */
195 char *
196 getPathDifference (char *pinto, const char *p1, const char *p2)
197 {
198   char *p = pinto;
199
200 #if NATIVE_WIN32
201   /* win32 can have a path at the start. */
202   if (strchr(p2, ':'))
203     {
204       p2 = strchr(p2, ':')+1;
205     }
206 #endif  
207
208   while (*p1 != '\0' && *p2 != '\0')
209     {
210       if (pathCharsEquivalent(*p1, *p2) == FALSE)
211         {
212           break;
213         }
214       p1++;
215       p2++;
216     }
217   while (*p2)
218     {
219       *p++ = *p2++;
220     }
221   *p = '\0';
222
223   return fixupPath(pinto);
224 }
225
226 /** Given a file with path information in the binary files directory,
227     returns what PREFIX must be to get this path.  Used for discovery
228     of where SDCC is installed.  Returns NULL if the path is
229     impossible.
230 */
231 char *
232 getPrefixFromBinPath (const char *prel)
233 {
234   strcpy(scratchFileName, prel);
235   /* Strip off the /sdcc at the end */
236   *strrchr(scratchFileName, DIR_SEPARATOR_CHAR) = '\0';
237   /* Compute what the difference between the prefix and the bin dir
238      should be. */
239   getPathDifference (buffer, PREFIX, BINDIR);
240
241   /* Verify that the path in has the expected suffix */
242   if (strlen(buffer) > strlen(scratchFileName))
243     {
244       /* Not long enough */
245       return NULL;
246     }
247
248   if (pathEquivalent (buffer, scratchFileName + strlen(scratchFileName) - strlen(buffer)) == FALSE)
249     {
250       /* Doesn't match */
251       return NULL;
252     }
253
254   scratchFileName[strlen(scratchFileName) - strlen(buffer)] = '\0';
255
256   return gc_strdup (scratchFileName);
257 }
258
259 /** Returns true if the given path exists.
260  */
261 bool
262 pathExists (const char *ppath)
263 {
264   struct stat s;
265
266   return stat (ppath, &s) == 0;
267 }
268
269 static hTab *_mainValues;
270
271 void
272 setMainValue (const char *pname, const char *pvalue)
273 {
274   shash_add (&_mainValues, pname, pvalue);
275 }
276
277 void
278 buildCmdLine2 (char *pbuffer, const char *pcmd)
279 {
280   char *poutcmd = msprintf(_mainValues, pcmd);
281   strcpy(pbuffer, poutcmd);
282 }
283
284 void
285 populateMainValues (const char **ppin)
286 {
287   _mainValues = populateStringHash(ppin);
288 }
289
290 char *
291 gc_strdup (const char *s)
292 {
293   char *ret;
294   ret = Safe_calloc (1, strlen (s) + 1);
295   strcpy (ret, s);
296   return ret;
297 }
298
299 /** Returns true if sz starts with the string given in key.
300  */
301 bool
302 startsWith (const char *sz, const char *key)
303 {
304   return !strncmp (sz, key, strlen (key));
305 }
306
307 /** Removes any newline characters from the string.  Not strictly the
308     same as perl's chomp.
309 */
310 void
311 chomp (char *sz)
312 {
313   char *nl;
314   while ((nl = strrchr (sz, '\n')))
315     *nl = '\0';
316 }
317