1 /*-------------------------------------------------------------------------
2 SDCCutil.c - Small utility functions.
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
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
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.
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.
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 -------------------------------------------------------------------------*/
26 #include "SDCCmacro.h"
31 /** Given an array of name, value string pairs creates a new hash
32 containing all of the pairs.
35 populateStringHash(const char **pin)
41 shash_add (&pret, pin[0], pin[1]);
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.
53 addToList (const char **list, const char *str)
55 /* This is the bad way to do things :) */
61 werror (E_OUT_OF_MEM, __FILE__, 0);
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.
72 join(const char **pplist)
79 strcpy(pinto, *pplist);
80 pinto += strlen(*pplist);
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.
93 joinn(char **pplist, int n)
100 strcpy(pinto, *pplist);
101 pinto += strlen(*pplist);
110 /** Returns TRUE if for the host the two path characters are
114 pathCharsEquivalent(char c1, char c2)
117 /* win32 is case insensitive */
118 if (tolower(c1) == tolower(c2))
122 /* And / is equivalent to \ */
123 else if (c1 == '/' && c2 == '\\')
127 else if (c1 == '\\' && c2 == '/')
136 /* Assume a Unix host where they must match exactly. */
142 pathEquivalent(const char *p1, const char *p2)
144 while (*p1 != '\0' && *p2 != '\0')
146 if (pathCharsEquivalent (*p1, *p2) == FALSE)
158 pathCharTransform(char c)
163 return DIR_SEPARATOR_CHAR;
174 /** Fixes up a potentially mixed path to the proper representation for
175 the host. Fixes up in place.
184 *p = pathCharTransform(*p);
192 /** Returns the characters in p2 past the last matching characters in
196 getPathDifference (char *pinto, const char *p1, const char *p2)
201 /* win32 can have a path at the start. */
204 p2 = strchr(p2, ':')+1;
208 while (*p1 != '\0' && *p2 != '\0')
210 if (pathCharsEquivalent(*p1, *p2) == FALSE)
223 return fixupPath(pinto);
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
232 getPrefixFromBinPath (const char *prel)
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
239 getPathDifference (buffer, PREFIX, BINDIR);
241 /* Verify that the path in has the expected suffix */
242 if (strlen(buffer) > strlen(scratchFileName))
244 /* Not long enough */
248 if (pathEquivalent (buffer, scratchFileName + strlen(scratchFileName) - strlen(buffer)) == FALSE)
254 scratchFileName[strlen(scratchFileName) - strlen(buffer)] = '\0';
256 return gc_strdup (scratchFileName);
259 /** Returns true if the given path exists.
262 pathExists (const char *ppath)
266 return stat (ppath, &s) == 0;
269 static hTab *_mainValues;
272 setMainValue (const char *pname, const char *pvalue)
274 shash_add (&_mainValues, pname, pvalue);
278 buildCmdLine2 (char *pbuffer, const char *pcmd)
280 char *poutcmd = msprintf(_mainValues, pcmd);
281 strcpy(pbuffer, poutcmd);
285 populateMainValues (const char **ppin)
287 _mainValues = populateStringHash(ppin);
291 gc_strdup (const char *s)
294 ret = Safe_calloc (1, strlen (s) + 1);
299 /** Returns true if sz starts with the string given in key.
302 startsWith (const char *sz, const char *key)
304 return !strncmp (sz, key, strlen (key));
307 /** Removes any newline characters from the string. Not strictly the
308 same as perl's chomp.
314 while ((nl = strrchr (sz, '\n')))