WIN32 version of getBinPath() calls GetModuleFileName() to determine
[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 #ifdef _WIN32
26 #include <windows.h>
27 #endif
28 #include <sys/stat.h>
29 #include "SDCCglobl.h"
30 #include "SDCCmacro.h"
31 #include "SDCCutil.h"
32 #include "newalloc.h"
33
34 /** Given an array of name, value string pairs creates a new hash
35     containing all of the pairs.
36 */
37 hTab *
38 populateStringHash(const char **pin)
39 {
40   hTab *pret = NULL;
41
42   while (*pin)
43     {
44       shash_add (&pret, pin[0], pin[1]);
45       pin += 2;
46     }
47
48   return pret;
49 }
50
51 /** Given an array of string pointers and another string, adds the
52     string to the end of the list.  The end of the list is assumed to
53     be the first NULL pointer.
54 */
55 void
56 addToList (const char **list, const char *str)
57 {
58   /* This is the bad way to do things :) */
59   while (*list)
60     list++;
61   *list = Safe_strdup (str);
62   if (!*list)
63     {
64       werror (E_OUT_OF_MEM, __FILE__, 0);
65       exit (1);
66     }
67   *(++list) = NULL;
68 }
69
70 /** Given an array of string pointers returns a string containing all
71     of the strings seperated by spaces.  The returned string is on the
72     heap.  The join stops when a NULL pointer is hit.
73 */
74 char *
75 join(const char **pplist)
76 {
77     buffer[0] = 0;  
78     
79     while (*pplist)
80     {
81         strncatz(buffer, *pplist, PATH_MAX);
82         strncatz(buffer, " ", PATH_MAX);
83         pplist++;
84     }
85
86     return buffer;
87 }
88
89 /** Given an array of string pointers, returns a string containing all
90     of the strings seperated by spaces.  The returned string is on the
91     heap.  n is the number of strings in the list.
92 */
93 char *
94 joinn(char **pplist, int n)
95 {
96     buffer[0] = 0;  
97     
98     while (n--)
99     {
100         strncatz(buffer, *pplist, PATH_MAX);
101         strncatz(buffer, " ", PATH_MAX);
102         pplist++;
103     }
104
105     return buffer;
106 }
107
108 /** Returns TRUE if for the host the two path characters are
109     equivalent.
110 */
111 static bool
112 pathCharsEquivalent(char c1, char c2)
113 {
114 #if NATIVE_WIN32
115   /* win32 is case insensitive */
116   if (tolower(c1) == tolower(c2))
117     {
118       return TRUE;
119     }
120   /* And / is equivalent to \ */
121   else if (c1 == '/' && c2 == '\\')
122     {
123       return TRUE;
124     }
125   else if (c1 == '\\' && c2 == '/')
126     {
127       return TRUE;
128     }
129   else
130     {
131       return FALSE;
132     }
133 #else
134   /* Assume a Unix host where they must match exactly. */
135   return c1 == c2;
136 #endif
137 }
138
139 static char
140 pathCharTransform(char c)
141 {
142 #if NATIVE_WIN32
143   if (c == '/')
144     {
145       return DIR_SEPARATOR_CHAR;
146     }
147   else
148     {
149       return c;
150     }
151 #else
152   return c;
153 #endif
154 }
155
156 /** Fixes up a potentially mixed path to the proper representation for
157     the host.  Fixes up in place.
158 */
159 static char *
160 fixupPath(char *pin)
161 {
162   char *p = pin;
163
164   while (*p)
165     {
166       *p = pathCharTransform(*p);
167       p++;
168     }
169   *p = '\0';
170
171   return pin;
172 }
173
174 /** Returns the characters in p2 past the last matching characters in
175     p1.  
176 */
177 char *
178 getPathDifference (char *pinto, const char *p1, const char *p2)
179 {
180   char *p = pinto;
181
182 #if NATIVE_WIN32
183   /* win32 can have a path at the start. */
184   if (strchr(p2, ':'))
185     {
186       p2 = strchr(p2, ':')+1;
187     }
188 #endif  
189
190   while (*p1 != '\0' && *p2 != '\0')
191     {
192       if (pathCharsEquivalent(*p1, *p2) == FALSE)
193         {
194           break;
195         }
196       p1++;
197       p2++;
198     }
199   while (*p2)
200     {
201       *p++ = *p2++;
202     }
203   *p = '\0';
204
205   return fixupPath(pinto);
206 }
207
208
209 /** Given a file with path information in the binary files directory,
210     returns the directory component. Used for discovery of bin
211     directory of SDCC installation. Returns NULL if the path is
212     impossible.
213 */
214 #ifdef _WIN32
215 char *
216 getBinPath(const char *prel)
217 {
218   char *p;
219   size_t len;
220   static char path[PATH_MAX];
221     
222   /* try DOS and *nix dir separator on WIN32 */
223   if (NULL != (p = strrchr(prel, DIR_SEPARATOR_CHAR)) ||
224     NULL != (p = strrchr(prel, UNIX_DIR_SEPARATOR_CHAR))) {
225     len = min((sizeof path) - 1, p - prel);
226     strncpy(path, prel, len);
227     path[len] = '\0';
228     return path;
229   }
230   /* not enough info in prel; do it with module name */
231   else if (0 != GetModuleFileName(NULL, path, sizeof path) != 0 &&
232     NULL != (p = strrchr(path, DIR_SEPARATOR_CHAR))) {
233     *p = '\0';
234     return path;
235   }
236   else
237     return NULL;
238 }
239 #else
240 char *
241 getBinPath(const char *prel)
242 {
243   char *p;
244   size_t len;
245   static char path[PATH_MAX];
246     
247   if ((p = strrchr(prel, DIR_SEPARATOR_CHAR)) == NULL)
248     return NULL;
249
250   len = min((sizeof path) - 1, p - prel);
251   strncpy(path, prel, len);
252   path[len] = '\0';
253
254   return path;
255 }
256 #endif
257
258 /** Returns true if the given path exists.
259  */
260 bool
261 pathExists (const char *ppath)
262 {
263   struct stat s;
264
265   return stat (ppath, &s) == 0;
266 }
267
268 static hTab *_mainValues;
269
270 void
271 setMainValue (const char *pname, const char *pvalue)
272 {
273   assert(pname);
274
275   shash_add (&_mainValues, pname, pvalue);
276 }
277
278 void
279 buildCmdLine2 (char *pbuffer, const char *pcmd, size_t len)
280 {
281   char *poutcmd;
282   assert(pbuffer && pcmd);
283   assert(_mainValues);
284
285   poutcmd = msprintf(_mainValues, pcmd);
286   strncpyz(pbuffer, poutcmd, len);
287 }
288
289 void
290 populateMainValues (const char **ppin)
291 {
292   _mainValues = populateStringHash(ppin);
293 }
294
295 /** Returns true if sz starts with the string given in key.
296  */
297 bool
298 startsWith (const char *sz, const char *key)
299 {
300   return !strncmp (sz, key, strlen (key));
301 }
302
303 /** Removes any newline characters from the string.  Not strictly the
304     same as perl's chomp.
305 */
306 void
307 chomp (char *sz)
308 {
309   char *nl;
310   while ((nl = strrchr (sz, '\n')))
311     *nl = '\0';
312 }
313
314 hTab *
315 getRuntimeVariables(void)
316 {
317   return _mainValues;
318 }
319
320
321 /* strncpy() with guaranteed NULL termination. */
322 char *strncpyz(char *dest, const char *src, size_t n)
323 {
324     assert(n > 0);
325
326     --n;
327     // paranoia...
328     if (strlen(src) > n)
329     {
330         fprintf(stderr, "strncpyz prevented buffer overrun!\n");
331     }
332     
333     strncpy(dest, src, n);
334     dest[n] = 0;
335     return dest;
336 }
337
338 /* like strncat() with guaranteed NULL termination
339  * The passed size should be the size of the dest buffer, not the number of 
340  * bytes to copy.
341  */
342 char *strncatz(char *dest, const char *src, size_t n)
343 {
344     size_t maxToCopy;
345     size_t destLen = strlen(dest);
346     
347     assert(n > 0);
348     assert(n > destLen);
349     
350     maxToCopy = n - destLen - 1;
351     
352     // paranoia...
353     if (strlen(src) + destLen >= n)
354     {
355         fprintf(stderr, "strncatz prevented buffer overrun!\n");
356     }
357     
358     strncat(dest, src, maxToCopy);
359     dest[n - 1] = 0;
360     return dest;
361 }
362
363
364 #if defined(HAVE_VSNPRINTF) || defined(HAVE_VSPRINTF)
365 size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
366 {
367   va_list args;
368   int len;
369
370   va_start(args, fmt);
371
372 # if defined(HAVE_VSNPRINTF)
373   len = vsnprintf(dst, n, fmt, args);
374 # else
375   vsprintf(dst, fmt, args);
376   len = strlen(dst) + 1;
377 # endif
378
379   va_end(args);
380
381   /* on some gnu systems, vsnprintf returns -1 if output is truncated.
382    * In the C99 spec, vsnprintf returns the number of characters that 
383    * would have been written, were space available.
384    */
385   if ((len < 0) || (size_t) len >= n) {
386     fprintf(stderr, "internal error: sprintf truncated.\n");
387   }
388
389   return len;
390 }
391
392 #endif