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