mshtml: Added IHTMLScriptElement::put_text implementation.
[wine] / dlls / setupx.dll16 / infparse.c
1 /*
2  * SetupX .inf file parsing functions
3  *
4  * Copyright 2000 Andreas Mohr for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * FIXME:
21  * - return values ???
22  * - this should be reimplemented at some point to have its own
23  *   file parsing instead of using profile functions,
24  *   as some SETUPX exports probably demand that
25  *   (IpSaveRestorePosition, IpFindNextMatchLine, ...).
26  */
27
28 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winnls.h"
39 #include "setupapi.h"
40 #include "setupx16.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
44
45 #define MAX_HANDLES 16384
46 #define FIRST_HANDLE 32
47
48 static HINF handles[MAX_HANDLES];
49
50
51 static RETERR16 alloc_hinf16( HINF hinf, HINF16 *hinf16 )
52 {
53     int i;
54     for (i = 0; i < MAX_HANDLES; i++)
55     {
56         if (!handles[i])
57         {
58             handles[i] = hinf;
59             *hinf16 = i + FIRST_HANDLE;
60             return OK;
61         }
62     }
63     return ERR_IP_OUT_OF_HANDLES;
64 }
65
66 static HINF get_hinf( HINF16 hinf16 )
67 {
68     int idx = hinf16 - FIRST_HANDLE;
69     if (idx < 0 || idx >= MAX_HANDLES) return 0;
70     return handles[idx];
71 }
72
73
74 static HINF free_hinf16( HINF16 hinf16 )
75 {
76     HINF ret;
77     int idx = hinf16 - FIRST_HANDLE;
78
79     if (idx < 0 || idx >= MAX_HANDLES) return 0;
80     ret = handles[idx];
81     handles[idx] = 0;
82     return ret;
83 }
84
85 /* convert last error code to a RETERR16 value */
86 static RETERR16 get_last_error(void)
87 {
88     switch(GetLastError())
89     {
90     case ERROR_EXPECTED_SECTION_NAME:
91     case ERROR_BAD_SECTION_NAME_LINE:
92     case ERROR_SECTION_NAME_TOO_LONG: return ERR_IP_INVALID_SECT_NAME;
93     case ERROR_SECTION_NOT_FOUND: return ERR_IP_SECT_NOT_FOUND;
94     case ERROR_LINE_NOT_FOUND: return ERR_IP_LINE_NOT_FOUND;
95     default: return IP_ERROR;  /* FIXME */
96     }
97 }
98
99 /* string substitution support, duplicated from setupapi/parser.c */
100
101 static const char *get_string_subst( HINF hinf, const char *str, unsigned int *len,
102                                      char subst[MAX_INF_STRING_LENGTH], BOOL no_trailing_slash )
103 {
104     int dirid;
105     char *end;
106     INFCONTEXT context;
107     char buffer[MAX_INF_STRING_LENGTH];
108
109     if (!*len)  /* empty string (%%) is replaced by single percent */
110     {
111         *len = 1;
112         return "%";
113     }
114     memcpy( buffer, str, *len );
115     buffer[*len] = 0;
116
117     if (SetupFindFirstLineA( hinf, "Strings", buffer, &context ) &&
118         SetupGetStringFieldA( &context, 1, subst, MAX_INF_STRING_LENGTH, NULL ))
119     {
120         *len = strlen( subst );
121         return subst;
122     }
123
124     /* check for integer id */
125     dirid = strtoul( buffer, &end, 10 );
126     if (!*end && !CtlGetLddPath16( dirid, subst ))
127     {
128         *len = strlen( subst );
129         if (no_trailing_slash && *len && subst[*len - 1] == '\\') *len -= 1;
130         return subst;
131     }
132     return NULL;
133 }
134
135 static unsigned int string_subst( HINF hinf, const char *text, char *buffer )
136 {
137     const char *start, *subst, *p;
138     unsigned int len, total = 0;
139     int inside = 0;
140     unsigned int size = MAX_INF_STRING_LENGTH;
141     char tmp[MAX_INF_STRING_LENGTH];
142
143     for (p = start = text; *p; p++)
144     {
145         if (*p != '%') continue;
146         inside = !inside;
147         if (inside)  /* start of a %xx% string */
148         {
149             len = p - start;
150             if (len > size - 1) len = size - 1;
151             if (buffer) memcpy( buffer + total, start, len );
152             total += len;
153             size -= len;
154             start = p;
155         }
156         else /* end of the %xx% string, find substitution */
157         {
158             len = p - start - 1;
159             subst = get_string_subst( hinf, start + 1, &len, tmp, p[1] == '\\' );
160             if (!subst)
161             {
162                 subst = start;
163                 len = p - start + 1;
164             }
165             if (len > size - 1) len = size - 1;
166             if (buffer) memcpy( buffer + total, subst, len );
167             total += len;
168             size -= len;
169             start = p + 1;
170         }
171     }
172
173     if (start != p) /* unfinished string, copy it */
174     {
175         len = p - start;
176         if (len > size - 1) len = size - 1;
177         if (buffer) memcpy( buffer + total, start, len );
178         total += len;
179     }
180     if (buffer && size) buffer[total] = 0;
181     return total;
182 }
183
184
185 /***********************************************************************
186  *              IpOpen (SETUPX.2)
187  *
188  */
189 RETERR16 WINAPI IpOpen16( LPCSTR filename, HINF16 *hinf16 )
190 {
191     HINF hinf = SetupOpenInfFileA( filename, NULL, INF_STYLE_WIN4, NULL );
192     if (hinf == INVALID_HANDLE_VALUE) return get_last_error();
193     return alloc_hinf16( hinf, hinf16 );
194 }
195
196
197 /***********************************************************************
198  *              IpClose (SETUPX.4)
199  */
200 RETERR16 WINAPI IpClose16( HINF16 hinf16 )
201 {
202     HINF hinf = free_hinf16( hinf16 );
203     if (!hinf) return ERR_IP_INVALID_HINF;
204     SetupCloseInfFile( hinf );
205     return OK;
206 }
207
208
209 /***********************************************************************
210  *              IpGetProfileString (SETUPX.210)
211  */
212 RETERR16 WINAPI IpGetProfileString16( HINF16 hinf16, LPCSTR section, LPCSTR entry,
213                                       LPSTR buffer, WORD buflen )
214 {
215     DWORD required_size;
216     HINF hinf = get_hinf( hinf16 );
217
218     if (!hinf) return ERR_IP_INVALID_HINF;
219     if (!SetupGetLineTextA( NULL, hinf, section, entry, buffer, buflen, &required_size ))
220         return get_last_error();
221     TRACE("%p: section %s entry %s ret %s\n",
222           hinf, debugstr_a(section), debugstr_a(entry), debugstr_a(buffer) );
223     return OK;
224 }
225
226
227 /***********************************************************************
228  *              GenFormStrWithoutPlaceHolders (SETUPX.103)
229  *
230  * ought to be pretty much implemented, I guess...
231  */
232 void WINAPI GenFormStrWithoutPlaceHolders16( LPSTR dst, LPCSTR src, HINF16 hinf16 )
233 {
234     HINF hinf = get_hinf( hinf16 );
235
236     if (!hinf) return;
237
238     string_subst( hinf, src, dst );
239     TRACE( "%s -> %s\n", debugstr_a(src), debugstr_a(dst) );
240 }
241
242 /***********************************************************************
243  *              GenInstall (SETUPX.101)
244  *
245  * generic installer function for .INF file sections
246  *
247  * This is not perfect - patch whenever you can !
248  *
249  * wFlags == GENINSTALL_DO_xxx
250  * e.g. NetMeeting:
251  * first call GENINSTALL_DO_REGSRCPATH | GENINSTALL_DO_FILES,
252  * second call GENINSTALL_DO_LOGCONFIG | CFGAUTO | INI2REG | REG | INI
253  */
254 RETERR16 WINAPI GenInstall16( HINF16 hinf16, LPCSTR section, WORD genflags )
255 {
256     UINT flags = 0;
257     HINF hinf = get_hinf( hinf16 );
258     RETERR16 ret = OK;
259     void *context;
260
261     if (!hinf) return ERR_IP_INVALID_HINF;
262
263     if (genflags & GENINSTALL_DO_FILES) flags |= SPINST_FILES;
264     if (genflags & GENINSTALL_DO_INI) flags |= SPINST_INIFILES;
265     if (genflags & GENINSTALL_DO_REG) flags |= SPINST_REGISTRY;
266     if (genflags & GENINSTALL_DO_INI2REG) flags |= SPINST_INI2REG;
267     if (genflags & GENINSTALL_DO_LOGCONFIG) flags |= SPINST_LOGCONFIG;
268     if (genflags & GENINSTALL_DO_REGSRCPATH) FIXME( "unsupported flag: GENINSTALL_DO_REGSRCPATH\n" );
269     if (genflags & GENINSTALL_DO_CFGAUTO) FIXME( "unsupported flag: GENINSTALL_DO_CFGAUTO\n" );
270     if (genflags & GENINSTALL_DO_PERUSER) FIXME( "unsupported flag: GENINSTALL_DO_PERUSER\n" );
271
272     context = SetupInitDefaultQueueCallback( 0 );
273     if (!SetupInstallFromInfSectionA( 0, hinf, section, flags, 0, NULL,
274                                       SP_COPY_NEWER_OR_SAME, SetupDefaultQueueCallbackA,
275                                       context, 0, 0 ))
276         ret = get_last_error();
277
278     SetupTermDefaultQueueCallback( context );
279     return ret;
280 }