reshuffling of dlls
[reactos.git] / reactos / dll / win32 / setupapi / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "setupapi_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
31
32 #define MAX_HANDLES 16384
33 #define FIRST_HANDLE 32
34
35 static HINF handles[MAX_HANDLES];
36
37
38 static RETERR16 alloc_hinf16( HINF hinf, HINF16 *hinf16 )
39 {
40 int i;
41 for (i = 0; i < MAX_HANDLES; i++)
42 {
43 if (!handles[i])
44 {
45 handles[i] = hinf;
46 *hinf16 = i + FIRST_HANDLE;
47 return OK;
48 }
49 }
50 return ERR_IP_OUT_OF_HANDLES;
51 }
52
53 static HINF get_hinf( HINF16 hinf16 )
54 {
55 int idx = hinf16 - FIRST_HANDLE;
56 if (idx < 0 || idx >= MAX_HANDLES) return 0;
57 return handles[idx];
58 }
59
60
61 static HINF free_hinf16( HINF16 hinf16 )
62 {
63 HINF ret;
64 int idx = hinf16 - FIRST_HANDLE;
65
66 if (idx < 0 || idx >= MAX_HANDLES) return 0;
67 ret = handles[idx];
68 handles[idx] = 0;
69 return ret;
70 }
71
72 /* convert last error code to a RETERR16 value */
73 static RETERR16 get_last_error(void)
74 {
75 switch(GetLastError())
76 {
77 case ERROR_EXPECTED_SECTION_NAME:
78 case ERROR_BAD_SECTION_NAME_LINE:
79 case ERROR_SECTION_NAME_TOO_LONG: return ERR_IP_INVALID_SECT_NAME;
80 case ERROR_SECTION_NOT_FOUND: return ERR_IP_SECT_NOT_FOUND;
81 case ERROR_LINE_NOT_FOUND: return ERR_IP_LINE_NOT_FOUND;
82 default: return IP_ERROR; /* FIXME */
83 }
84 }
85
86
87 /***********************************************************************
88 * IpOpen (SETUPX.2)
89 *
90 */
91 RETERR16 WINAPI IpOpen16( LPCSTR filename, HINF16 *hinf16 )
92 {
93 HINF hinf = SetupOpenInfFileA( filename, NULL, INF_STYLE_WIN4, NULL );
94 if (hinf == (HINF)INVALID_HANDLE_VALUE) return get_last_error();
95 return alloc_hinf16( hinf, hinf16 );
96 }
97
98
99 /***********************************************************************
100 * IpClose (SETUPX.4)
101 */
102 RETERR16 WINAPI IpClose16( HINF16 hinf16 )
103 {
104 HINF hinf = free_hinf16( hinf16 );
105 if (!hinf) return ERR_IP_INVALID_HINF;
106 SetupCloseInfFile( hinf );
107 return OK;
108 }
109
110
111 /***********************************************************************
112 * IpGetProfileString (SETUPX.210)
113 */
114 RETERR16 WINAPI IpGetProfileString16( HINF16 hinf16, LPCSTR section, LPCSTR entry,
115 LPSTR buffer, WORD buflen )
116 {
117 DWORD required_size;
118 HINF hinf = get_hinf( hinf16 );
119
120 if (!hinf) return ERR_IP_INVALID_HINF;
121 if (!SetupGetLineTextA( NULL, hinf, section, entry, buffer, buflen, &required_size ))
122 return get_last_error();
123 TRACE("%p: section %s entry %s ret %s\n",
124 hinf, debugstr_a(section), debugstr_a(entry), debugstr_a(buffer) );
125 return OK;
126 }
127
128
129 /***********************************************************************
130 * GenFormStrWithoutPlaceHolders (SETUPX.103)
131 *
132 * ought to be pretty much implemented, I guess...
133 */
134 void WINAPI GenFormStrWithoutPlaceHolders16( LPSTR dst, LPCSTR src, HINF16 hinf16 )
135 {
136 UNICODE_STRING srcW;
137 HINF hinf = get_hinf( hinf16 );
138
139 if (!hinf) return;
140
141 if (!RtlCreateUnicodeStringFromAsciiz( &srcW, src )) return;
142 PARSER_string_substA( hinf, srcW.Buffer, dst, MAX_INF_STRING_LENGTH );
143 RtlFreeUnicodeString( &srcW );
144 TRACE( "%s -> %s\n", debugstr_a(src), debugstr_a(dst) );
145 }
146
147 /***********************************************************************
148 * GenInstall (SETUPX.101)
149 *
150 * generic installer function for .INF file sections
151 *
152 * This is not perfect - patch whenever you can !
153 *
154 * wFlags == GENINSTALL_DO_xxx
155 * e.g. NetMeeting:
156 * first call GENINSTALL_DO_REGSRCPATH | GENINSTALL_DO_FILES,
157 * second call GENINSTALL_DO_LOGCONFIG | CFGAUTO | INI2REG | REG | INI
158 */
159 RETERR16 WINAPI GenInstall16( HINF16 hinf16, LPCSTR section, WORD genflags )
160 {
161 UINT flags = 0;
162 HINF hinf = get_hinf( hinf16 );
163 RETERR16 ret = OK;
164 void *context;
165
166 if (!hinf) return ERR_IP_INVALID_HINF;
167
168 if (genflags & GENINSTALL_DO_FILES) flags |= SPINST_FILES;
169 if (genflags & GENINSTALL_DO_INI) flags |= SPINST_INIFILES;
170 if (genflags & GENINSTALL_DO_REG) flags |= SPINST_REGISTRY;
171 if (genflags & GENINSTALL_DO_INI2REG) flags |= SPINST_INI2REG;
172 if (genflags & GENINSTALL_DO_LOGCONFIG) flags |= SPINST_LOGCONFIG;
173 if (genflags & GENINSTALL_DO_REGSRCPATH) FIXME( "unsupported flag: GENINSTALL_DO_REGSRCPATH\n" );
174 if (genflags & GENINSTALL_DO_CFGAUTO) FIXME( "unsupported flag: GENINSTALL_DO_CFGAUTO\n" );
175 if (genflags & GENINSTALL_DO_PERUSER) FIXME( "unsupported flag: GENINSTALL_DO_PERUSER\n" );
176
177 context = SetupInitDefaultQueueCallback( 0 );
178 if (!SetupInstallFromInfSectionA( 0, hinf, section, flags, 0, NULL,
179 SP_COPY_NEWER_OR_SAME, SetupDefaultQueueCallbackA,
180 context, 0, 0 ))
181 ret = get_last_error();
182
183 SetupTermDefaultQueueCallback( context );
184 return ret;
185 }