Turned off debugging messages
[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 the characters in p2 past the last matching characters in
111     p1.  
112 */
113 char *
114 getStringDifference (char *pinto, const char *p1, const char *p2)
115 {
116   char *p = pinto;
117
118   while (*p1 != '\0' && *p2 != '\0')
119     {
120       if (*p1++ != *p2++)
121         {
122           break;
123         }
124     }
125   while (*p2)
126     {
127       *p++ = *p2++;
128     }
129   *p = '\0';
130
131   return pinto;
132 }
133
134 /** Given a file with path information in the binary files directory,
135     returns what PREFIX must be to get this path.  Used for discovery
136     of where SDCC is installed.  Returns NULL if the path is
137     impossible.
138 */
139 char *
140 getPrefixFromBinPath (const char *prel)
141 {
142   strcpy(scratchFileName, prel);
143   /* Strip off the /sdcc at the end */
144   *strrchr(scratchFileName, DIR_SEPARATOR_CHAR) = '\0';
145   /* Compute what the difference between the prefix and the bin dir
146      should be. */
147   getStringDifference (buffer, PREFIX, BINDIR);
148
149   /* Verify that the path in has the expected suffix */
150   if (strlen(buffer) > strlen(scratchFileName))
151     {
152       /* Not long enough */
153       return NULL;
154     }
155   if (strcmp(buffer, scratchFileName + strlen(scratchFileName) - strlen(buffer)) != 0)
156     {
157       /* Doesn't match */
158       return NULL;
159     }
160
161   scratchFileName[strlen(scratchFileName) - strlen(buffer)] = '\0';
162
163   return gc_strdup (scratchFileName);
164 }
165
166 /** Returns true if the given path exists.
167  */
168 bool
169 pathExists (const char *ppath)
170 {
171   struct stat s;
172
173   return stat (ppath, &s) == 0;
174 }
175
176 static hTab *_mainValues;
177
178 void
179 setMainValue (const char *pname, const char *pvalue)
180 {
181   shash_add (&_mainValues, pname, pvalue);
182 }
183
184 void
185 buildCmdLine2 (char *pbuffer, const char *pcmd)
186 {
187   char *poutcmd = msprintf(_mainValues, pcmd);
188   strcpy(pbuffer, poutcmd);
189 }
190
191 void
192 populateMainValues (const char **ppin)
193 {
194   _mainValues = populateStringHash(ppin);
195 }
196
197 char *
198 gc_strdup (const char *s)
199 {
200   char *ret;
201   ret = Safe_calloc (1, strlen (s) + 1);
202   strcpy (ret, s);
203   return ret;
204 }
205
206 /** Returns true if sz starts with the string given in key.
207  */
208 bool
209 startsWith (const char *sz, const char *key)
210 {
211   return !strncmp (sz, key, strlen (key));
212 }
213
214 /** Removes any newline characters from the string.  Not strictly the
215     same as perl's chomp.
216 */
217 void
218 chomp (char *sz)
219 {
220   char *nl;
221   while ((nl = strrchr (sz, '\n')))
222     *nl = '\0';
223 }
224