OpenPrinter should fail if pPrinterName is "".
[wine] / dlls / setupapi / setupx_main.c
1 /*
2  *      SETUPX library
3  *
4  *      Copyright 1998,2000  Andreas Mohr
5  *
6  * FIXME: Rather non-functional functions for now.
7  *
8  * See:
9  * http://www.geocities.com/SiliconValley/Network/5317/drivers.html
10  * http://willemer.de/informatik/windows/inf_info.htm (German)
11  * http://www.microsoft.com/ddk/ddkdocs/win98ddk/devinst_12uw.htm
12  * DDK: setupx.h
13  * http://mmatrix.tripod.com/customsystemfolder/infsysntaxfull.html
14  * http://www.rdrop.com/~cary/html/inf_faq.html
15  * http://support.microsoft.com/support/kb/articles/q194/6/40.asp
16  *
17  * Stuff tested with:
18  * - rs405deu.exe (German Acroread 4.05 setup)
19  * - ie5setup.exe
20  * - Netmeeting
21  *
22  * FIXME:
23  * - string handling is... weird ;) (buflen etc.)
24  * - memory leaks ?
25  * - separate that mess (but probably only when it's done completely)
26  *
27  * SETUPX consists of several parts with the following acronyms/prefixes:
28  * Di   device installer (devinst.c ?)
29  * Gen  generic installer (geninst.c ?)
30  * Ip   .INF parsing (infparse.c)
31  * LDD  logical device descriptor (ldd.c ?)
32  * LDID logical device ID 
33  * SU   setup (setup.c ?)
34  * Tp   text processing (textproc.c ?)
35  * Vcp  virtual copy module (vcp.c ?)
36  * ...
37  *
38  * The SETUPX DLL is NOT thread-safe. That's why many installers urge you to
39  * "close all open applications".
40  * All in all the design of it seems to be a bit weak.
41  * Not sure whether my implementation of it is better, though ;-)
42  */
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include "winreg.h"
48 #include "wine/winuser16.h"
49 #include "setupx16.h"
50 #include "setupapi_private.h"
51 #include "winerror.h"
52 #include "debugtools.h"
53
54 DEFAULT_DEBUG_CHANNEL(setupapi);
55
56 /***********************************************************************
57  *              SURegOpenKey (SETUPX.47)
58  */
59 DWORD WINAPI SURegOpenKey( HKEY hkey, LPCSTR lpszSubKey, LPHKEY retkey )
60 {
61     FIXME("(%x,%s,%p), semi-stub.\n",hkey,debugstr_a(lpszSubKey),retkey);
62     return RegOpenKeyA( hkey, lpszSubKey, retkey );
63 }
64
65 /***********************************************************************
66  *              SURegQueryValueEx (SETUPX.50)
67  */
68 DWORD WINAPI SURegQueryValueEx( HKEY hkey, LPSTR lpszValueName,
69                                 LPDWORD lpdwReserved, LPDWORD lpdwType,
70                                 LPBYTE lpbData, LPDWORD lpcbData )
71 {
72     FIXME("(%x,%s,%p,%p,%p,%ld), semi-stub.\n",hkey,debugstr_a(lpszValueName),
73           lpdwReserved,lpdwType,lpbData,lpcbData?*lpcbData:0);
74     return RegQueryValueExA( hkey, lpszValueName, lpdwReserved, lpdwType,
75                                lpbData, lpcbData );
76 }
77
78 /*
79  * Returns pointer to a string list with the first entry being number
80  * of strings.
81  *
82  * Hmm. Should this be InitSubstrData(), GetFirstSubstr() and GetNextSubstr()
83  * instead?
84  */
85 static LPSTR *SETUPX_GetSubStrings(LPSTR start, char delimiter)
86 {
87     LPSTR p, q;
88     LPSTR *res = NULL;
89     DWORD count = 0;
90     int len;
91
92     p = start;
93
94     while (1)
95     {
96         /* find beginning of real substring */
97         while ( (*p == ' ') || (*p == '\t') || (*p == '"') ) p++;
98
99         /* find end of real substring */
100         q = p;
101         while ( (*q)
102              && (*q != ' ') && (*q != '\t') && (*q != '"')
103              && (*q != ';') && (*q != delimiter) ) q++;
104         if (q == p)
105             break;
106         len = (int)q - (int)p;
107
108         /* alloc entry for new substring in steps of 32 units and copy over */
109         if (count % 32 == 0)
110         { /* 1 for count field + current count + 32 */
111             res = HeapReAlloc(GetProcessHeap(), 0, res, (1+count+32)*sizeof(LPSTR));
112         }
113         *(res+1+count) = HeapAlloc(GetProcessHeap(), 0, len+1);
114         strncpy(*(res+1+count), p, len);
115         (*(res+1+count))[len] = '\0';
116         count++;
117
118         /* we are still within last substring (before delimiter),
119          * so get out of it */
120         while ((*q) && (*q != ';') && (*q != delimiter)) q++;
121         if ((!*q) || (*q == ';'))
122             break;
123         p = q+1;
124     }
125     
126     /* put number of entries at beginning of list */
127     *(DWORD *)res = count;
128     return res;
129 }
130
131 static void SETUPX_FreeSubStrings(LPSTR *substr)
132 {
133     DWORD count = *(DWORD *)substr;
134     LPSTR *pStrings = substr+1;
135     DWORD n;
136
137     for (n=0; n < count; n++)
138         HeapFree(GetProcessHeap(), 0, *pStrings++);
139
140     HeapFree(GetProcessHeap(), 0, substr);
141 }
142
143 static void SETUPX_IsolateSubString(LPSTR *begin, LPSTR *end)
144 {
145     LPSTR p, q;
146
147     p = *begin;
148     q = *end;
149
150     while ((p < q) && ((*p == ' ') || (*p == '\t'))) p++;
151     while ((p < q) && (*p == '"')) p++;
152
153     while ((q-1 >= p) && ((*(q-1) == ' ') || (*(q-1) == '\t'))) q--;
154     while ((q-1 >= p) && (*(q-1) == '"')) q--;
155
156     *begin = p;
157     *end = q;
158 }
159
160 /*
161  * Example: HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"C:\"
162  * FIXME: use SETUPX_GetSubStrings() instead.
163  *        Hmm, but on the other hand SETUPX_GetSubStrings() will probably
164  *        soon be replaced by InitSubstrData() etc. anyway.
165  * 
166  */
167 static BOOL SETUPX_LookupRegistryString(LPSTR regstr, LPSTR buffer, DWORD buflen)
168 {
169     HANDLE heap = GetProcessHeap();
170     LPSTR items[5];
171     LPSTR p, q, next;
172     int len, n;
173     HKEY hkey, hsubkey;
174     DWORD dwType;
175
176     TRACE("retrieving '%s'\n", regstr);
177
178     p = regstr;
179
180     /* isolate root key, subkey, value, flag, defval */
181     for (n=0; n < 5; n++)
182     {
183         q = strchr(p, ',');
184         if (!q)
185         {
186             if (n == 4)
187                 q = p+strlen(p);
188             else
189                 return FALSE;
190         }
191         next = q+1;
192         if (q < regstr)
193             return FALSE;
194         SETUPX_IsolateSubString(&p, &q);
195         len = (int)q - (int)p;
196         items[n] = HeapAlloc(heap, 0, len+1);
197         strncpy(items[n], p, len);
198         items[n][len] = '\0';
199         p = next;
200     }
201     TRACE("got '%s','%s','%s','%s','%s'\n",
202                         items[0], items[1], items[2], items[3], items[4]);
203     
204     /* check root key */
205     if (!strcasecmp(items[0], "HKCR"))
206         hkey = HKEY_CLASSES_ROOT;
207     else
208     if (!strcasecmp(items[0], "HKCU"))
209         hkey = HKEY_CURRENT_USER;
210     else
211     if (!strcasecmp(items[0], "HKLM"))
212         hkey = HKEY_LOCAL_MACHINE;
213     else
214     if (!strcasecmp(items[0], "HKU"))
215         hkey = HKEY_USERS;
216     else
217     { /* HKR ? -> relative to key passed to GenInstallEx */
218         FIXME("unsupported regkey '%s'\n", items[0]);
219         goto regfailed;
220     }
221
222     if (RegOpenKeyA(hkey, items[1], &hsubkey) != ERROR_SUCCESS)
223         goto regfailed;
224
225     if (RegQueryValueExA(hsubkey, items[2], NULL, &dwType, buffer, &buflen)
226                 != ERROR_SUCCESS)
227         goto regfailed;
228     goto done;
229
230 regfailed:
231     if (buffer) strcpy(buffer, items[4]); /* I don't care about buflen */
232 done:
233     for (n=0; n < 5; n++)
234         HeapFree(heap, 0, items[n]);
235     if (buffer)
236         TRACE("return '%s'\n", buffer);
237     return TRUE;
238 }
239
240 static LPSTR SETUPX_GetSections(LPCSTR filename)
241 {
242     LPSTR buf = NULL;
243     DWORD len = 1024, res;
244
245     do {
246         buf = HeapReAlloc(GetProcessHeap(), 0, buf, len);
247         res = GetPrivateProfileStringA(NULL, NULL, NULL, buf, len, filename);
248         len *= 2;
249     } while ((!res) && (len < 1048576));
250     if (!res)
251     {
252         HeapFree(GetProcessHeap(), 0, buf);
253         return NULL;
254     }
255     return buf;
256 }
257
258 static LPSTR SETUPX_GetSectionEntries(LPCSTR filename, LPCSTR section)
259 {
260     LPSTR buf = NULL;
261     DWORD len = 1024, res;
262
263     do {
264         buf = HeapReAlloc(GetProcessHeap(), 0, buf, len);
265         res = GetPrivateProfileSectionA(section, buf, len, filename);
266         len *= 2;
267     } while ((!res) && (len < 1048576));
268     if (!res)
269     {
270         HeapFree(GetProcessHeap(), 0, buf);
271         return NULL;
272     }
273     return buf;
274 }
275
276
277 /***********************************************************************
278  *              InstallHinfSection (SETUPX.527)
279  *
280  * hwnd = parent window
281  * hinst = instance of SETUPX.DLL
282  * lpszCmdLine = e.g. "DefaultInstall 132 C:\MYINSTALL\MYDEV.INF"
283  * Here "DefaultInstall" is the .inf file section to be installed (optional).
284  * The 132 value is made of the HOW_xxx flags and sometimes 128 (-> setupx16.h).
285  * 
286  * nCmdShow = nCmdShow of CreateProcess
287  */
288 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
289 RETERR16 WINAPI InstallHinfSection16( HWND16 hwnd, HINSTANCE16 hinst, LPCSTR lpszCmdLine, INT16 nCmdShow)
290 {
291     LPSTR *pSub;
292     DWORD count;
293     HINF16 hInf = 0;
294     RETERR16 res = OK, tmp;
295     WORD wFlags;
296     BOOL reboot = FALSE;
297     HMODULE hMod;
298     MSGBOX_PROC pMessageBoxA;
299     
300     TRACE("(%04x, %04x, %s, %d);\n", hwnd, hinst, lpszCmdLine, nCmdShow);
301     
302     pSub = SETUPX_GetSubStrings((LPSTR)lpszCmdLine, ' ');
303
304     count = *(DWORD *)pSub;
305     if (count < 2) /* invalid number of arguments ? */
306         goto end;
307     if (IpOpen16(*(pSub+count), &hInf) != OK)
308     {
309         res = ERROR_FILE_NOT_FOUND; /* yes, correct */
310         goto end;
311     }
312     if (VcpOpen16(NULL, 0))
313         goto end;
314     if (GenInstall16(hInf, *(pSub+count-2), GENINSTALL_DO_ALL) != OK)
315         goto end;
316     wFlags = atoi(*(pSub+count-1)) & ~128;
317     switch (wFlags)
318     {
319         case HOW_ALWAYS_SILENT_REBOOT:
320         case HOW_SILENT_REBOOT:
321             reboot = TRUE;
322             break;
323         case HOW_ALWAYS_PROMPT_REBOOT:
324         case HOW_PROMPT_REBOOT:
325             if ((hMod = GetModuleHandleA("user32.dll")))
326             {
327               if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( hMod, "MessageBoxA" )))
328               {
329                  
330                 if (pMessageBoxA(hwnd, "You must restart Wine before the new settings will take effect.\n\nDo you want to exit Wine now ?", "Systems Settings Change", MB_YESNO|MB_ICONQUESTION) == IDYES)
331                   reboot = TRUE;
332               }
333             }
334             break;
335         default:
336             ERR("invalid flags %d !\n", wFlags);
337             goto end;
338     }
339     
340     res = OK;
341 end:
342     tmp = VcpClose16(VCPFL_ALL, NULL);
343     if (tmp != OK)
344         res = tmp;
345     tmp = IpClose16(hInf);
346     if (tmp != OK)
347         res = tmp;
348     SETUPX_FreeSubStrings(pSub);
349     if (reboot)
350     {
351         /* FIXME: we should have a means of terminating all wine + wineserver */
352         MESSAGE("Program or user told me to restart. Exiting Wine...\n");
353         ExitProcess(1);
354     }
355
356     return res;
357 }
358
359 typedef struct
360 {
361     LPCSTR RegValName;
362     LPCSTR StdString; /* fallback string; sub dir of windows directory */
363 } LDID_DATA;
364
365 static const LDID_DATA LDID_Data[34] =
366 {
367     { /* 0 (LDID_NULL) -- not defined */
368         NULL,
369         NULL
370     },
371     { /* 1 (LDID_SRCPATH) = source of installation. hmm, what to do here ? */
372         "SourcePath", /* hmm, does SETUPX have to care about updating it ?? */
373         NULL
374     },
375     { /* 2 (LDID_SETUPTEMP) = setup temp dir */
376         "SetupTempDir",
377         NULL
378     },
379     { /* 3 (LDID_UNINSTALL) = uninstall backup dir */
380         "UninstallDir",
381         NULL
382     },
383     { /* 4 (LDID_BACKUP) = backup dir */
384         "BackupDir",
385         NULL
386     },
387     { /* 5 (LDID_SETUPSCRATCH) = setup scratch dir */
388         "SetupScratchDir",
389         NULL
390     },
391     { /* 6 -- not defined */
392         NULL,
393         NULL
394     },
395     { /* 7 -- not defined */
396         NULL,
397         NULL
398     },
399     { /* 8 -- not defined */
400         NULL,
401         NULL
402     },
403     { /* 9 -- not defined */
404         NULL,
405         NULL
406     },
407     { /* 10 (LDID_WIN) = windows dir */
408         "WinDir",
409         ""
410     },
411     { /* 11 (LDID_SYS) = system dir */
412         "SysDir",
413         NULL /* call GetSystemDirectory() instead */
414     },
415     { /* 12 (LDID_IOS) = IOSubSys dir */
416         NULL, /* FIXME: registry string ? */
417         "SYSTEM\\IOSUBSYS"
418     },
419     { /* 13 (LDID_CMD) = COMMAND dir */
420         NULL, /* FIXME: registry string ? */
421         "COMMAND"
422     },
423     { /* 14 (LDID_CPL) = control panel dir */
424         NULL,
425         ""
426     },
427     { /* 15 (LDID_PRINT) = windows printer dir */
428         NULL,
429         "SYSTEM" /* correct ?? */
430     },
431     { /* 16 (LDID_MAIL) = destination mail dir */
432         NULL,
433         ""
434     },
435     { /* 17 (LDID_INF) = INF dir */
436         "SetupScratchDir", /* correct ? */
437         "INF"
438     },
439     { /* 18 (LDID_HELP) = HELP dir */
440         NULL, /* ??? */
441         "HELP"
442     },
443     { /* 19 (LDID_WINADMIN) = Admin dir */
444         "WinAdminDir",
445         ""
446     },
447     { /* 20 (LDID_FONTS) = Fonts dir */
448         NULL, /* ??? */
449         "FONTS"
450     },
451     { /* 21 (LDID_VIEWERS) = Viewers */
452         NULL, /* ??? */
453         "SYSTEM\\VIEWERS"
454     },
455     { /* 22 (LDID_VMM32) = VMM32 dir */
456         NULL, /* ??? */
457         "SYSTEM\\VMM32"
458     },
459     { /* 23 (LDID_COLOR) = ICM dir */
460         "ICMPath",
461         "SYSTEM\\COLOR"
462     },
463     { /* 24 (LDID_APPS) = root of boot drive ? */
464         "AppsDir",
465         "C:\\"
466     },
467     { /* 25 (LDID_SHARED) = shared dir */
468         "SharedDir",
469         ""
470     },
471     { /* 26 (LDID_WINBOOT) = Windows boot dir */
472         "WinBootDir",
473         ""
474     },
475     { /* 27 (LDID_MACHINE) = machine specific files */
476         "MachineDir",
477         NULL
478     },
479     { /* 28 (LDID_HOST_WINBOOT) = Host Windows boot dir */
480         "HostWinBootDir",
481         NULL
482     },
483     { /* 29 -- not defined */
484         NULL,
485         NULL
486     },
487     { /* 30 (LDID_BOOT) = Root of boot drive */
488         "BootDir",
489         NULL
490     },
491     { /* 31 (LDID_BOOT_HOST) = Root of boot drive host */
492         "BootHost",
493         NULL
494     },
495     { /* 32 (LDID_OLD_WINBOOT) = subdir of root */
496         "OldWinBootDir",
497         NULL
498     },
499     { /* 33 (LDID_OLD_WIN) = old win dir */
500         "OldWinDir",
501         NULL
502     }
503     /* the rest (34-38) isn't too interesting, so I'll forget about it */
504 };
505
506 /* 
507  * LDD  == Logical Device Descriptor
508  * LDID == Logical Device ID
509  *
510  * The whole LDD/LDID business might go into a separate file named
511  * ldd.c.
512  * At the moment I don't know what the hell these functions are really doing.
513  * That's why I added reporting stubs.
514  * The only thing I do know is that I need them for the LDD/LDID infrastructure.
515  * That's why I implemented them in a way that's suitable for my purpose.
516  */
517 static LDD_LIST *pFirstLDD = NULL;
518
519 static BOOL std_LDDs_done = FALSE;
520
521 void SETUPX_CreateStandardLDDs(void)
522 {
523     HKEY hKey = 0;
524     WORD n;
525     DWORD type, len;
526     LOGDISKDESC_S ldd;
527     char buffer[MAX_PATH];
528
529     /* has to be here, otherwise loop */
530     std_LDDs_done = TRUE;
531
532     RegOpenKeyA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup", &hKey);
533
534     for (n=0; n < sizeof(LDID_Data)/sizeof(LDID_DATA); n++)
535     {
536         buffer[0] = '\0';
537
538         len = MAX_PATH;
539         if ( (hKey) && (LDID_Data[n].RegValName)
540         &&   (RegQueryValueExA(hKey, LDID_Data[n].RegValName,
541                 NULL, &type, buffer, &len) == ERROR_SUCCESS)
542         &&   (type == REG_SZ) )
543         {
544             TRACE("found value '%s' for LDID %d\n", buffer, n);
545         }
546         else
547         switch(n)
548         {
549             case LDID_SRCPATH:
550                 FIXME("LDID_SRCPATH: what exactly do we have to do here ?\n");
551                 strcpy(buffer, "X:\\FIXME");
552                 break;
553             case LDID_SYS:
554                 GetSystemDirectoryA(buffer, MAX_PATH);
555                 break;
556             case LDID_APPS:
557             case LDID_MACHINE:
558             case LDID_HOST_WINBOOT:
559             case LDID_BOOT:
560             case LDID_BOOT_HOST:
561                 strcpy(buffer, "C:\\");
562                 break;
563             default:
564                 if (LDID_Data[n].StdString)
565                 {
566                     DWORD len = GetWindowsDirectoryA(buffer, MAX_PATH);
567                     LPSTR p;
568                     p = buffer + len;
569                     *p++ = '\\';
570                     strcpy(p, LDID_Data[n].StdString);
571                 }
572                 break;
573         }
574         if (buffer[0])
575         {
576             INIT_LDD(ldd, n);
577             ldd.pszPath = buffer;
578             TRACE("LDID %d -> '%s'\n", ldd.ldid, ldd.pszPath);
579             CtlSetLdd16(&ldd);
580         }
581     }
582     if (hKey) RegCloseKey(hKey);
583 }
584         
585 /***********************************************************************
586  * CtlDelLdd            (SETUPX.37)
587  *
588  * RETURN
589  *   ERR_VCP_LDDINVALID if ldid < LDID_ASSIGN_START.
590  */
591 RETERR16 SETUPX_DelLdd(LOGDISKID16 ldid)
592 {
593     LDD_LIST *pCurr, *pPrev = NULL;
594
595     TRACE("(%d)\n", ldid);
596
597     if (!std_LDDs_done)
598         SETUPX_CreateStandardLDDs();
599
600     if (ldid < LDID_ASSIGN_START)
601         return ERR_VCP_LDDINVALID;
602
603     pCurr = pFirstLDD;
604     /* search until we find the appropriate LDD or hit the end */
605     while ((pCurr != NULL) && (ldid > pCurr->pldd->ldid))
606     {
607          pPrev = pCurr;
608          pCurr = pCurr->next;
609     }
610     if ( (pCurr == NULL) /* hit end of list */
611       || (ldid != pCurr->pldd->ldid) )
612         return ERR_VCP_LDDFIND; /* correct ? */
613
614     /* ok, found our victim: eliminate it */
615
616     if (pPrev)
617         pPrev->next = pCurr->next;
618
619     if (pCurr == pFirstLDD)
620         pFirstLDD = NULL;
621     HeapFree(GetProcessHeap(), 0, pCurr);
622     
623     return OK;
624 }
625
626 /***********************************************************************
627  *              CtlDelLdd (SETUPX.37)
628  */
629 RETERR16 WINAPI CtlDelLdd16(LOGDISKID16 ldid)
630 {
631     FIXME("(%d); - please report to a.mohr@mailto.de !!!\n", ldid);
632     return SETUPX_DelLdd(ldid);
633 }
634
635 /***********************************************************************
636  * CtlFindLdd           (SETUPX.35)
637  *
638  * doesn't check pldd ptr validity: crash (W98SE)
639  *
640  * RETURN
641  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
642  *   1 in all other cases ??
643  * 
644  */
645 RETERR16 WINAPI CtlFindLdd16(LPLOGDISKDESC pldd)
646 {
647     LDD_LIST *pCurr, *pPrev = NULL;
648
649     TRACE("(%p)\n", pldd);
650    
651     if (!std_LDDs_done)
652         SETUPX_CreateStandardLDDs();
653
654     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
655         return ERR_VCP_LDDINVALID;
656
657     pCurr = pFirstLDD;
658     /* search until we find the appropriate LDD or hit the end */
659     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
660     {
661         pPrev = pCurr;
662         pCurr = pCurr->next;
663     }
664     if ( (pCurr == NULL) /* hit end of list */
665       || (pldd->ldid != pCurr->pldd->ldid) )
666         return ERR_VCP_LDDFIND; /* correct ? */
667
668     memcpy(pldd, pCurr->pldd, pldd->cbSize);
669     /* hmm, we probably ought to strcpy() the string ptrs here */
670     
671     return 1; /* what is this ?? */
672 }
673
674 /***********************************************************************
675  * CtlSetLdd                    (SETUPX.33)
676  *
677  * Set an LDD entry.
678  *
679  * RETURN
680  *   ERR_VCP_LDDINVALID if pldd.cbSize != sizeof(LOGDISKDESC_S)
681  *
682  */
683 RETERR16 WINAPI CtlSetLdd16(LPLOGDISKDESC pldd)
684 {
685     LDD_LIST *pCurr, *pPrev = NULL;
686     LPLOGDISKDESC pCurrLDD;
687     HANDLE heap;
688     BOOL is_new = FALSE;
689
690     TRACE("(%p)\n", pldd);
691
692     if (!std_LDDs_done)
693         SETUPX_CreateStandardLDDs();
694
695     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
696         return ERR_VCP_LDDINVALID;
697
698     heap = GetProcessHeap();
699     pCurr = pFirstLDD;
700     /* search until we find the appropriate LDD or hit the end */
701     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
702     {
703          pPrev = pCurr;
704          pCurr = pCurr->next;
705     }
706     if (pCurr == NULL) /* hit end of list */
707     {
708         is_new = TRUE;
709         pCurr = HeapAlloc(heap, 0, sizeof(LDD_LIST));
710         pCurr->pldd = HeapAlloc(heap, 0, sizeof(LOGDISKDESC_S));
711         pCurr->next = NULL;
712         pCurrLDD = pCurr->pldd;
713     }
714     else
715     {
716         pCurrLDD = pCurr->pldd;
717         if (pCurrLDD->pszPath)          HeapFree(heap, 0, pCurrLDD->pszPath);
718         if (pCurrLDD->pszVolLabel)      HeapFree(heap, 0, pCurrLDD->pszVolLabel);
719         if (pCurrLDD->pszDiskName)      HeapFree(heap, 0, pCurrLDD->pszDiskName);
720     }
721
722     memcpy(pCurrLDD, pldd, sizeof(LOGDISKDESC_S));
723
724     if (pldd->pszPath)
725     {
726         pCurrLDD->pszPath = HeapAlloc( heap, 0, strlen(pldd->pszPath)+1 );
727         strcpy( pCurrLDD->pszPath, pldd->pszPath );
728     }
729     if (pldd->pszVolLabel)
730     {
731         pCurrLDD->pszVolLabel = HeapAlloc( heap, 0, strlen(pldd->pszVolLabel)+1 );
732         strcpy( pCurrLDD->pszVolLabel, pldd->pszVolLabel );
733     }
734     if (pldd->pszDiskName)
735     {
736         pCurrLDD->pszDiskName = HeapAlloc( heap, 0, strlen(pldd->pszDiskName)+1 );
737         strcpy( pCurrLDD->pszDiskName, pldd->pszDiskName );
738     }
739
740     if (is_new) /* link into list */
741     {
742         if (pPrev)
743         {
744             pCurr->next = pPrev->next;
745             pPrev->next = pCurr;
746         }
747         if (!pFirstLDD)
748             pFirstLDD = pCurr;
749     }
750     
751     return OK;
752 }
753
754
755 /***********************************************************************
756  * CtlAddLdd            (SETUPX.36)
757  *
758  * doesn't check pldd ptr validity: crash (W98SE)
759  *
760  */
761 static LOGDISKID16 ldid_to_add = LDID_ASSIGN_START;
762 RETERR16 WINAPI CtlAddLdd16(LPLOGDISKDESC pldd)
763 {
764     pldd->ldid = ldid_to_add++;
765     return CtlSetLdd16(pldd);
766 }
767
768 /***********************************************************************
769  * CtlGetLdd            (SETUPX.34)
770  *
771  * doesn't check pldd ptr validity: crash (W98SE)
772  * What the !@#$%&*( is the difference between CtlFindLdd() and CtlGetLdd() ??
773  *
774  * RETURN
775  *   ERR_VCP_LDDINVALID if pldd->cbSize != structsize
776  * 
777  */
778 static RETERR16 SETUPX_GetLdd(LPLOGDISKDESC pldd)
779 {
780     LDD_LIST *pCurr, *pPrev = NULL;
781
782     if (!std_LDDs_done)
783         SETUPX_CreateStandardLDDs();
784
785     if (pldd->cbSize != sizeof(LOGDISKDESC_S))
786         return ERR_VCP_LDDINVALID;
787
788     pCurr = pFirstLDD;
789     /* search until we find the appropriate LDD or hit the end */
790     while ((pCurr != NULL) && (pldd->ldid > pCurr->pldd->ldid))
791     {
792          pPrev = pCurr;
793          pCurr = pCurr->next;
794     }
795     if (pCurr == NULL) /* hit end of list */
796         return ERR_VCP_LDDFIND; /* correct ? */
797
798     memcpy(pldd, pCurr->pldd, pldd->cbSize);
799     /* hmm, we probably ought to strcpy() the string ptrs here */
800
801     return OK;
802 }
803
804 /**********************************************************************/
805
806 RETERR16 WINAPI CtlGetLdd16(LPLOGDISKDESC pldd)
807 {
808     FIXME("(%p); - please report to a.mohr@mailto.de !!!\n", pldd);
809     return SETUPX_GetLdd(pldd);
810 }
811
812 /***********************************************************************
813  *              CtlGetLddPath           (SETUPX.38)
814  *
815  * Gets the path of an LDD.
816  * No crash if szPath == NULL.
817  * szPath has to be at least MAX_PATH_LEN bytes long.
818  * RETURN
819  *   ERR_VCP_LDDUNINIT if LDD for LDID not found.
820  */
821 RETERR16 WINAPI CtlGetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
822 {
823     TRACE("(%d, %p);\n", ldid, szPath);
824
825     if (szPath)
826     {
827         LOGDISKDESC_S ldd;
828         INIT_LDD(ldd, ldid);
829         if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
830             return ERR_VCP_LDDUNINIT;
831         SETUPX_GetLdd(&ldd);
832         strcpy(szPath, ldd.pszPath);
833         TRACE("ret '%s' for LDID %d\n", szPath, ldid);
834     }
835     return OK;
836 }
837
838 /***********************************************************************
839  *              CtlSetLddPath           (SETUPX.508)
840  *
841  * Sets the path of an LDD.
842  * Creates LDD for LDID if not existing yet.
843  */
844 RETERR16 WINAPI CtlSetLddPath16(LOGDISKID16 ldid, LPSTR szPath)
845 {
846     LOGDISKDESC_S ldd;
847     TRACE("(%d, '%s');\n", ldid, szPath);
848     
849     INIT_LDD(ldd, ldid);
850     ldd.pszPath = szPath;
851     return CtlSetLdd16(&ldd);
852 }
853
854 /*
855  * Find the value of a custom LDID in a .inf file
856  * e.g. for 49301:
857  * 49300,49301=ProgramFilesDir,5
858  * -- profile section lookup -->
859  * [ProgramFilesDir]
860  * HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"%24%"
861  * -- GenFormStrWithoutPlaceHolders16 -->
862  * HKLM,"Software\Microsoft\Windows\CurrentVersion","ProgramFilesDir",,"C:\"
863  * -- registry lookup -->
864  * C:\Program Files (or C:\ if not found in registry)
865  * 
866  * FIXME:
867  * - maybe we ought to add a caching array for speed ? - I don't care :)
868  * - not sure whether the processing is correct - sometimes there are equal
869  *   LDIDs for both install and removal sections.
870  * - probably the whole function can be removed as installers add that on their
871  *   own 
872  */
873 static BOOL SETUPX_AddCustomLDID(int ldid, INT16 hInf)
874 {
875     char ldidstr[6];
876     LPSTR sectionbuf = NULL, entrybuf = NULL, regsectionbuf = NULL;
877     LPCSTR filename;
878     LPSTR pSec, pEnt, pEqual, p, *pSub = NULL;
879     BOOL ret = FALSE;
880     char buffer[MAX_PATH];
881     LOGDISKDESC_S ldd;
882
883     sprintf(ldidstr, "%d", ldid);
884     filename = IP_GetFileName(hInf);
885     if (!(sectionbuf = SETUPX_GetSections(filename)))
886     {
887         ERR("couldn't get sections !\n");
888         return FALSE;
889     }
890     for (pSec=sectionbuf; *pSec; pSec += strlen(pSec)+1)
891     {
892         if (!(entrybuf = SETUPX_GetSectionEntries(filename, pSec)))
893         {
894             ERR("couldn't get section entries !\n");
895             goto end;
896         }
897         for (pEnt=entrybuf; *pEnt; pEnt += strlen(pEnt)+1)
898         {
899             if (strstr(pEnt, ldidstr))
900             {
901                 pEqual = strchr(pEnt, '=');
902                 if (!pEqual) /* crippled entry ?? */
903                     continue;
904
905                 /* make sure we found the LDID on left side of the equation */
906                 if (pEnt+strlen(ldidstr) <= pEqual)
907                 { /* found */
908
909                     /* but we don't want entries in the strings section */
910                     if (!strcasecmp(pSec, "Strings"))
911                         goto next_section;
912                     p = pEqual+1;
913                     goto found;
914                 }
915             }
916         }
917 next_section:
918     }
919     goto end;
920 found:
921     TRACE("found entry '%s'\n", p);
922     pSub = SETUPX_GetSubStrings(p, ',');
923     if (*(DWORD *)pSub > 2)
924     {
925         ERR("malformed entry '%s' ?\n", p);
926         goto end;
927     }
928     TRACE("found section '%s'\n", *(pSub+1));
929     /* FIXME: what are the optional flags at the end of an entry used for ?? */
930
931     /* get the location of the registry key from that section */
932     if (!(regsectionbuf = SETUPX_GetSectionEntries(filename, *(pSub+1))))
933     {
934         ERR("couldn't get registry section entries !\n");
935         goto end;
936     }
937     /* sectionbuf is > 1024 bytes anyway, so use it */
938     GenFormStrWithoutPlaceHolders16(sectionbuf, regsectionbuf, hInf);
939     ret = SETUPX_LookupRegistryString(sectionbuf, buffer, MAX_PATH);
940     TRACE("return '%s'\n", buffer);
941     INIT_LDD(ldd, ldid);
942     ldd.pszPath = buffer;
943     CtlSetLdd16(&ldd);
944 end:
945     SETUPX_FreeSubStrings(pSub);
946     if (sectionbuf)     HeapFree(GetProcessHeap(), 0, sectionbuf);
947     if (entrybuf)       HeapFree(GetProcessHeap(), 0, entrybuf);
948     if (regsectionbuf)  HeapFree(GetProcessHeap(), 0, regsectionbuf);
949     return ret;
950 }
951
952 /*
953  * Translate a logical disk identifier (LDID) into its string representation
954  * I'm afraid this can be totally replaced by CtlGetLddPath().
955  */
956 static BOOL SETUPX_IP_TranslateLDID(int ldid, LPSTR *p, HINF16 hInf)
957 {
958     BOOL handled = FALSE;
959     LOGDISKDESC_S ldd;
960
961     ldd.cbSize = sizeof(LOGDISKDESC_S);
962     ldd.ldid = ldid;
963     if (CtlFindLdd16(&ldd) == ERR_VCP_LDDFIND)
964     {
965         /* hmm, it seems the installers already do the work for us
966          * (by calling CtlSetLddPath) that SETUPX_AddCustomLDID
967          * is supposed to do. Grmbl ;-)
968          * Well, I'll leave it here anyway, but print error... */
969         ERR("hmm, LDID %d not registered yet !?\n", ldid);
970         handled = SETUPX_AddCustomLDID(ldid, hInf);
971     }
972     else
973         handled = TRUE;
974     
975     SETUPX_GetLdd(&ldd);
976     
977     if (!handled)
978     {
979         FIXME("What is LDID %d ??\n", ldid);
980         *p = "LDID_FIXME";
981     }
982     else
983         *p = ldd.pszPath;
984
985     return handled;
986 }
987
988 /***********************************************************************
989  *              GenFormStrWithoutPlaceHolders (SETUPX.103)
990  *
991  * ought to be pretty much implemented, I guess...
992  */
993 void WINAPI GenFormStrWithoutPlaceHolders16( LPSTR szDst, LPCSTR szSrc, HINF16 hInf)
994 {
995     LPCSTR pSrc = szSrc, pSrcEnd = szSrc + strlen(szSrc);
996     LPSTR pDst = szDst, p, pPHBegin;
997     int count;
998     
999     TRACE("(%p, '%s', %04x);\n", szDst, szSrc, hInf);
1000     while (pSrc < pSrcEnd)
1001     {
1002         p = strchr(pSrc, '%');
1003         if (p)
1004         {
1005             count = (int)p - (int)pSrc;
1006             strncpy(pDst, pSrc, count);
1007             pSrc += count;
1008             pDst += count;
1009             pPHBegin = p+1;
1010             p = strchr(pPHBegin, '%');
1011             if (p)
1012             {
1013                 char placeholder[80]; /* that really ought to be enough ;) */
1014                 int ldid;
1015                 BOOL done = TRUE;
1016                 count = (int)p - (int)pPHBegin;
1017                 strncpy(placeholder, pPHBegin, count);
1018                 placeholder[count] = '\0';
1019                 ldid = atoi(placeholder);
1020                 if (ldid)
1021                 {
1022                     LPSTR p;
1023                     done = SETUPX_IP_TranslateLDID(ldid, &p, hInf);
1024                     strcpy(pDst, p);
1025                     if (done)
1026                         pDst += strlen(pDst);
1027                 }
1028                 else
1029                 { /* hmm, string placeholder. Need to look up
1030                      in the [strings] section of the hInf */
1031                     DWORD ret;
1032                     char buf[256]; /* long enough ? */
1033                     
1034                     ret = GetPrivateProfileStringA("strings", placeholder, "",
1035                                         buf, 256, IP_GetFileName(hInf));
1036                     if (ret)
1037                     {
1038                         strcpy(pDst, buf);
1039                         pDst += strlen(buf);
1040                     }
1041                     else
1042                     {
1043                         ERR("placeholder string '%s' not found !\n", placeholder);
1044                         done = FALSE;
1045                     }
1046                 }
1047                 if (!done)
1048                 { /* copy raw placeholder string over */
1049                     count = (int)p - (int)pPHBegin + 2;
1050                     strncpy(pDst, pPHBegin-1, count);
1051                     pDst += count;
1052                     
1053                 }
1054                 pSrc = p+1;
1055                 continue;
1056             }
1057         }
1058
1059         /* copy the remaining source string over */
1060         strncpy(pDst, pSrc, (int)pSrcEnd - (int)pSrc + 1);
1061         break;
1062     }
1063     TRACE("ret '%s'\n", szDst);
1064 }
1065
1066 /*
1067  * Copy all items in a CopyFiles entry over to the destination
1068  *
1069  * - VNLP_xxx is what is given as flags for a .INF CopyFiles section
1070  */
1071 static BOOL SETUPX_CopyFiles(LPSTR *pSub, HINF16 hInf)
1072 {
1073     BOOL bSingle = FALSE;
1074     unsigned int n;
1075     LPCSTR filename = IP_GetFileName(hInf);
1076     LPSTR pCopyEntry;
1077     char pDstStr[MAX_PATH];
1078     LPSTR pSrcDir, pDstDir;
1079     LPSTR pFileEntries, p;
1080     WORD ldid;
1081     LOGDISKDESC_S ldd;
1082     LPSTR *pSubFile;
1083     LPSTR pSrcFile, pDstFile;
1084     WORD flag;
1085
1086     for (n=0; n < *(DWORD *)pSub; n++)
1087     {
1088         pCopyEntry = *(pSub+1+n);
1089         if (*pCopyEntry == '@')
1090         {
1091             pCopyEntry++;
1092             bSingle = TRUE;
1093         }
1094         else
1095             bSingle = FALSE;
1096
1097         /* get source directory for that entry */
1098         INIT_LDD(ldd, LDID_SRCPATH);
1099         SETUPX_GetLdd(&ldd);
1100         pSrcDir = ldd.pszPath;
1101         
1102         /* get destination directory for that entry */
1103         if (!(GetPrivateProfileStringA("DestinationDirs", pCopyEntry, "",
1104                                         pDstStr, sizeof(pDstStr), filename)))
1105         {
1106             /* hmm, not found; try the default entry */
1107             if (!(GetPrivateProfileStringA("DestinationDirs", "DefaultDestDir", "", pDstStr, sizeof(pDstStr), filename)))
1108             {
1109                 WARN("DefaultDestDir not found.\n");
1110                 continue;
1111             }
1112         }
1113
1114         /* translate destination dir if given as LDID */
1115         ldid = atoi(pDstStr);
1116         if (ldid)
1117         {
1118             if (!(SETUPX_IP_TranslateLDID(ldid, &pDstDir, hInf)))
1119                 continue;
1120         }
1121         else
1122             pDstDir = pDstStr;
1123         
1124         /* now that we have the destination dir, register file copying */
1125
1126         if (bSingle)
1127         {
1128             VcpQueueCopy16(pCopyEntry, pCopyEntry, pSrcDir, pDstDir, LDID_SRCPATH, ldid ? ldid : 0xffff, 0, VFNL_COPY, 0);
1129             return TRUE;
1130         }
1131
1132         /* entry wasn't a single file, so let's iterate over section */
1133         pFileEntries = SETUPX_GetSectionEntries(filename, pCopyEntry);
1134         for (p=pFileEntries; *p; p +=strlen(p)+1)
1135         {
1136             pSubFile = SETUPX_GetSubStrings(p, ',');
1137             pSrcFile = *(pSubFile+1);
1138             pDstFile = (*(DWORD *)pSubFile > 1) ? *(pSubFile+2) : pSrcFile;
1139             TRACE("copying file '%s\\%s' to '%s\\%s'\n", pSrcDir, pSrcFile, pDstDir, pDstFile);
1140             flag = 0;
1141             if (*(DWORD *)pSubFile > 2)
1142             {
1143                 if ((flag = atoi(*(pSubFile+3)))) /* ah, flag */
1144                 {
1145                     if (flag & 0x2c)
1146                     FIXME("VNLP_xxx flag %d not handled yet.\n", flag);
1147                 }
1148                 else
1149                 {
1150                     FIXME("temp file name '%s' given. Need to register in wininit.ini !\n", *(pSubFile+3));
1151                     /* we probably need to set VIRTNODE.vhstrDstFinalName to
1152                      * the final destination name, and the temp name is merely
1153                      * the copy destination */
1154                 }
1155             }
1156             VcpQueueCopy16(pSrcFile, pDstFile, pSrcDir, pDstDir, LDID_SRCPATH, ldid ? ldid : 0xffff, 0, VFNL_COPY|flag, 0);
1157             SETUPX_FreeSubStrings(pSubFile);
1158         }
1159     }
1160             
1161     return TRUE;
1162 }
1163
1164 /***********************************************************************
1165  *              GenInstall (SETUPX.101)
1166  *
1167  * generic installer function for .INF file sections
1168  *
1169  * This is not perfect - patch whenever you can !
1170  * 
1171  * wFlags == GENINSTALL_DO_xxx
1172  * e.g. NetMeeting:
1173  * first call GENINSTALL_DO_REGSRCPATH | GENINSTALL_DO_FILES,
1174  * second call GENINSTALL_DO_LOGCONFIG | CFGAUTO | INI2REG | REG | INI
1175  */
1176 RETERR16 WINAPI GenInstall16(HINF16 hInfFile, LPCSTR szInstallSection, WORD wFlags)
1177 {
1178     LPCSTR filename = IP_GetFileName(hInfFile);
1179     LPSTR pEntries, p, pEnd;
1180     DWORD len;
1181     LPSTR *pSub;
1182
1183     FIXME("(%04x, '%s', %04x), semi-stub. Please implement additional operations here !\n", hInfFile, szInstallSection, wFlags);
1184     pEntries = SETUPX_GetSectionEntries(filename, szInstallSection);
1185     if (!pEntries)
1186     {
1187         ERR("couldn't find entries for section '%s' !\n", szInstallSection);
1188         return ERR_IP_SECT_NOT_FOUND;
1189     }
1190     for (p=pEntries; *p; p +=strlen(p)+1)
1191     {
1192         pEnd = strchr(p, '=');
1193         if (!pEnd) continue;
1194         pSub = SETUPX_GetSubStrings(pEnd+1, ','); /* split entries after the '=' */
1195         SETUPX_IsolateSubString(&p, &pEnd);
1196         len = (int)pEnd - (int)p;
1197
1198         if (wFlags & GENINSTALL_DO_FILES)
1199         {
1200             if (!strncasecmp(p, "CopyFiles", len))
1201             {
1202                 SETUPX_CopyFiles(pSub, hInfFile);
1203                 continue;
1204             }
1205 #if IMPLEMENT_THAT
1206             else
1207             if (!strncasecmp(p, "DelFiles", len))
1208             {
1209                 SETUPX_DelFiles(filename, szInstallSection, pSub);
1210                 continue;
1211             }
1212 #endif
1213         }
1214         if (wFlags & GENINSTALL_DO_INI)
1215         {
1216 #if IMPLEMENT_THAT
1217             if (!strncasecmp(p, "UpdateInis", len))
1218             {
1219                 SETUPX_UpdateInis(filename, szInstallSection, pSub);
1220                 continue;
1221             }
1222 #endif
1223         }
1224         if (wFlags & GENINSTALL_DO_REG)
1225         {
1226 #if IMPLEMENT_THAT
1227             /* probably use SUReg*() functions here */
1228             if (!strncasecmp(p, "AddReg", len))
1229             {
1230                 SETUPX_AddReg(filename, szInstallSection, pSub);
1231                 continue;
1232             }
1233             else
1234             if (!strncasecmp(p, "DelReg", len))
1235             {
1236                 SETUPX_DelReg(filename, szInstallSection, pSub);
1237                 continue;
1238             }
1239 #endif
1240         }
1241         
1242         SETUPX_FreeSubStrings(pSub);
1243     }
1244     HeapFree(GetProcessHeap(), 0, pEntries);
1245     return OK;
1246 }