Only adjust item rect by horizontal scrollbar offset in report view.
[wine/multimedia.git] / tools / wineconf
blob755eb7e86be7900a3b87b1b624aa934a4ff081bf
1 #!/usr/bin/perl -w
3 # This program generates wine.conf files on STDOUT.
4 # (C) 1996 Stephen Simmons
5 # Redistributable under Wine License
7 $RCS_ID = '$Id$ ';
9 # This program examines the contents of the DOS filesystems and
10 # attempts to generate a sensible wine.conf file. This is output
11 # to STDOUT.
12 # It reads /etc/FSTAB to find mounting locations of the hard disk drives
13 # It uses the correct algorithm for ordering DOS drives, with the
14 # exception of the case of multiple drive controller types, where I don't
15 # know what DOS's algorithm is.
16 # It uses find to find all of the win.ini files on any DOS partition
17 # and sorts them by age to guess which is part of the active Windows
18 # installation.
19 # It reads the autoexec.bat file (if found) and records all variable
20 # settings. There are some inaccuracies in its determination.
21 # First, while variables are interpolated properly, no control
22 # structures are supported so calls and execs to other batch files are
23 # ignored, and all variable settings take effect regardless of whether
24 # they would in DOS (i,e., both if and else clauses are read).
25 # This is used to determine the path and temp directories. Duplicate
26 # path directories and path directories that don't exist are thrown
27 # out.
28 # On failing to find C:\AUTOEXEC.BAT, wineconf finds all executables
29 # in the windows directory and subdirectories, and generates an
30 # optimized path statement encompassing all the executables.
31 # Then it also looks for \TEMP and \TMP on all drives taking the first
32 # one it finds.
33 # wineconf doesn't support floppy drives, network drives, printers,
34 # and serial device configuration is hardcoded and not configured for
35 # the machine it runs on. Similarly, spy parameters are hard coded.
37 # It would make sense to incorporate much of the hueristic code in
38 # this program into a library to be shared with a dosemu configuration
39 # program, because it seems that at least some of the same stuff will
40 # be wanted. The program needs to be cleaned up still. A better tmp
41 # search algorithm could be written. A fast option is planned. Less
42 # Linux-dependence is desired. Should look for devices independent
43 # of /etc/fstab; then sanity checks on /etc/fstab can be performed.
45 use Getopt::Long;
46 use File::Basename;
47 use strict;
48 use Carp;
50 GetOptions('windir=s', 'sysdir=s', 'thorough', 'debug:s', 'inifile=s') || &Usage;
52 &ReadFSTAB();
53 &FindWindowsDir();
54 &ReadAutoexecBat();
55 &StandardStuff();
57 sub Usage {
58 print "Usage: $0 <options>\n";
59 # print "-fstab <filename> Location of alternate fstab file\n";
60 print "-windir <filename> Location of windows dir in DOS space\n";
61 print "-thorough Do careful analysis (default)\n";
62 print "-sysdir <filename> Location of systems dir in DOS space\n";
63 print "-inifile <filename> Path to the wine.ini file (by default './wine.ini')\n";
64 # print "-tmpdir <filename> Location of tmp directory\n";
65 print "Generates (to STDOUT) a wine configuration file based on\n";
66 print "/etc/fstab and searching around in DOS directories\n";
67 print "The options above can override certain values\n";
68 print "This should be considered ALPHA code\n";
69 exit(0);
72 sub ReadFSTAB {
73 $::opt_f = $::opt_f ? $::opt_f : '/etc/fstab';
74 open(FSTAB, $::opt_f) || die "Cannot read $::opt_f\n";
75 while(<FSTAB>) {
76 next if /^\s*\#/;
77 next if /^\s*$/;
79 my ($device, $mntpoint, $type, @rest) = split(' ', $_);
80 if ($device !~ m"^/dev/fd") {
81 if ($type eq "msdos" || $type eq "vfat") {
82 push(@::FatDrives, [$device, $mntpoint, $type]);
84 elsif ($type eq "iso9660" ||
85 $mntpoint eq "/cdrom" ||
86 ($device eq '/dev/cdrom' && $type eq 'auto') ) {
87 push(@::CdromDrives, [$device, $mntpoint, 'win95']);
91 if (!@::FatDrives) {
92 warn "ERROR ($0): Cannot find any MSDOS drives.\n";
93 warn "This does not mean you cannot run Wine, but $0\n";
94 warn "cannot help you (yet)\n";
95 exit(1);
97 push(@::UnixDrives, ['', '/tmp', 'hd']);
98 push(@::UnixDrives, ['', '${HOME}', 'network']);
99 my $MagicDrive = 'C';
100 @::FatDrives = sort byDriveOrder @::FatDrives;
101 @::CdromDrives = sort byCdOrder @::CdromDrives;
102 foreach my $FatDrive (@::FatDrives) {
103 print "[Drive $MagicDrive]\n";
104 my $MntPoint = $FatDrive->[1];
105 my $FileSys = $FatDrive->[2];
106 print "Path=$MntPoint\n";
107 print "Type=hd\n";
108 print "Filesystem=$FileSys\n";
109 print "\n";
110 &RegisterDrive($MagicDrive, $FatDrive);
111 if(!&IsMounted($FatDrive->[0])) {
112 warn "WARNING: DOS Drive $MagicDrive (" . $FatDrive->[0] .
113 ") is not mounted\n";
115 $MagicDrive++;
117 foreach my $CdromDrive (@::CdromDrives) {
118 print "[Drive $MagicDrive]\n";
119 my $Device = $CdromDrive->[0];
120 my $MntPoint = $CdromDrive->[1];
121 my $FileSys = $CdromDrive->[2];
122 print "Path=$MntPoint\n";
123 print "Type=cdrom\n";
124 print "Device=$Device\n";
125 print "Filesystem=$FileSys\n";
126 print "\n";
127 &RegisterDrive($MagicDrive, $CdromDrive);
128 $MagicDrive++;
130 foreach my $UnixDrive (@::UnixDrives) {
131 print "[Drive $MagicDrive]\n";
132 my $MntPoint = $UnixDrive->[1];
133 my $Type = $UnixDrive->[2];
134 print "Path=$MntPoint\n";
135 print "Type=$Type\n";
136 print "Filesystem=win95\n";
137 print "\n";
138 $MagicDrive++;
142 sub FindWindowsDir {
143 my($MagicDrive) = 'C';
144 my(@FATD)=@::FatDrives;
145 my(@wininis) = ();
146 my ($winini);
148 if (!$::opt_windir && !$::opt_fast && !$::opt_thorough) {
149 $::opt_thorough++;
151 if ($::opt_windir) {
152 $winini = &ToUnix($::opt_windir);
153 if (!-e $winini) {
154 die "ERROR: Specified winini file does not exist\n";
157 elsif ($::opt_fast) {
158 die "-fast code can be implemented\n";
160 elsif ($::opt_thorough) {
161 if ($::opt_debug) { print STDERR "DEBUG: Num FATD = ", $#FATD+1, "\n"; }
162 foreach(@FATD) {
163 my $ThisDrive = shift(@FATD);
164 my $MntPoint = $ThisDrive->[1];
165 push(@wininis, `find $MntPoint -name win.ini -print`);
167 foreach $winini (@wininis) {
168 chomp $winini;
170 my ($winini_cnt) = $#wininis+1;
171 if ($::opt_debug) {
172 print STDERR "DEBUG: Num wininis found: $winini_cnt\n";}
173 if ($winini_cnt > 1) {
174 warn "$winini_cnt win.ini files found:\n";
175 @wininis = sort byFileAge @wininis;
176 warn join("\n", @wininis), "\n";
177 $winini = $wininis[0];
178 warn "Using most recent one: $winini\n";
180 elsif ($winini_cnt == 0) {
181 die "ERROR: No win.ini found in DOS partitions\n";
183 else {
184 $winini = $wininis[0];
187 else {
188 die "ERROR: None of -windir, -fast, or -thorough set\n";
190 $::windir = &ToDos(dirname($winini));
191 print "[wine]\n";
192 print "windows=$::windir\n";
193 if ($::opt_sysdir) {
194 print "system=$::opt_sysdir\n";
196 else {
197 print "system=$::windir\\SYSTEM\n";
201 # Returns 1 if the device is mounted; -1 if mount check failed; 0 if not
202 # mounted.
203 # This code is Linux specific, and needs to be broadened.
204 sub IsMounted {
205 my($Device) = @_;
206 if (-d "/proc") {
207 if (-e "/proc/mounts") {
208 open(MOUNTS, "/proc/mounts") ||
209 (warn "Cannot open /proc/mounts, although it exists\n" &&
210 return -1);
211 while(<MOUNTS>) {
212 if (/^$Device/) {
213 return 1; # Tested 1.4
216 return 0; # Tested 1.4
219 return -1;
222 sub RegisterDrive {
223 my($DOSdrive, $Drive) = @_;
224 $::DOS2Unix{$DOSdrive} = $Drive;
225 $::Device2DOS{$Drive->[0]} = $DOSdrive;
226 $::MntPoint2DOS{$Drive->[1]} = $DOSdrive;
227 $::DOS2MntPoint{$DOSdrive} = $Drive->[1];
228 $::DOS2Device{$DOSdrive} = $Drive->[0];
231 sub ReadAutoexecBat {
232 if (!%::DOS2Unix) { &ReadFSTAB; }
233 my($DriveC) = $::DOS2MntPoint{"C"};
234 $DriveC =~ s%/$%%;
235 my($path);
236 if ($::opt_debug) {
237 print STDERR "DEBUG: Looking for $DriveC/autoexec.bat\n"; }
238 if (-e "$DriveC/autoexec.bat") {
239 # Tested 1.4
240 open(AUTOEXEC, "$DriveC/autoexec.bat") ||
241 die "Cannot read autoexec.bat\n";
242 while(<AUTOEXEC>) {
243 s/\015//;
244 if (/^\s*(set\s+)?(\w+)\s*[\s\=]\s*(.*)$/i) {
245 my($varname) = $2;
246 my($varvalue) = $3;
247 chomp($varvalue);
248 $varname =~ tr/A-Z/a-z/;
249 while ($varvalue =~ /%(\w+)%/) {
250 my $matchname = $1;
251 my $subname = $1;
252 $subname =~ tr/A-Z/a-z/;
253 if ($::opt_debug =~ /path/i) {
254 print STDERR "DEBUG: Found $matchname as $subname\n";
255 print STDERR "DEBUG: Old varvalue:\n$varvalue\n";
256 print STDERR "DEBUG: Old subname value:\n" .
257 $::DOSenv{$subname} . "\n";
259 if ($::DOSenv{$subname}) {
260 $varvalue =~ s/\%$matchname\%/$::DOSenv{$subname}/;
262 else {
263 warn "DOS environment variable $subname not\n";
264 warn "defined in autoexec.bat. (Reading config.sys\n";
265 warn "is not implemented.) Using null value\n";
266 $varvalue =~ s/%$matchname%//;
268 if ($::opt_debug =~ /path/i) {
269 print STDERR "DEBUG: New varvalue:\n$varvalue\n";
272 if ($::opt_debug) {
273 print STDERR "DEBUG: $varname = $varvalue\n";
275 $::DOSenv{$varname} = $varvalue;
278 close(AUTOEXEC);
280 else {
281 # Tested 1.4
282 warn "WARNING: C:\\AUTOEXEC.BAT was not found.\n";
285 if ($::DOSenv{"path"}) {
286 my @pathdirs = split(/\s*;\s*/, $::DOSenv{"path"});
287 if ($::opt_debug =~ /path/i) {
288 print STDERR "DEBUG (path): @pathdirs\n";
290 foreach my $pathdir (@pathdirs) {
291 if (-d &ToUnix($pathdir)) {
292 if ($::DOSpathdir{$pathdir}++) {
293 warn "Ignoring duplicate DOS path entry $pathdir\n";
295 else {
296 if ($::opt_debug =~ /path/i) {
297 print STDERR "DEBUG (path): Found $pathdir\n";
299 push(@::DOSpathlist, $pathdir);
302 else {
303 warn "Ignoring DOS path directory $pathdir, as it does not\n";
304 warn "exist\n";
307 print "path=" . join(";", @::DOSpathlist) . "\n";
309 else {
310 # Code status: tested 1.4
311 warn "WARNING: Making assumptions for PATH\n";
312 warn "Will scan windows directory for executables and generate\n";
313 warn "path from that\n";
314 my $shellcmd = 'find ' . &ToUnix($::windir) . " -iregex '" .
315 '.*\.\(exe\|bat\|com\|dll\)' . "' -print";
316 if ($::opt_debug) {
317 print STDERR "DEBUG: autoexec.bat search command:\n $shellcmd\n";
319 push(@::DOScommand, `$shellcmd`);
320 if ($::opt_debug && $::opt_debug =~ /autoexec/i) {
321 print STDERR "DEBUG: autoexec.bat search results:\n\@DOS::command\n";
323 foreach my $command (@::DOScommand) {
324 $command =~ s%[^/]+$%%;
325 $::DOSexecdir{&ToDos($command)}++;
327 print "path=" .
328 join(";",
329 grep(s%\\$%%,
330 sort {$::DOSexecdir{$b} <=> $::DOSexecdir{$a}}
331 (keys %::DOSexecdir))) . "\n";
334 if ($::DOSenv{"temp"} && -d &ToUnix($::DOSenv{"temp"})) {
335 print "temp=" . $::DOSenv{"temp"} . "\n";
337 else {
338 my $TheTemp;
340 warn "WARNING: Making assumptions for TEMP\n";
341 warn "Looking for \\TEMP and then \\TMP on every drive\n";
342 # Watch out .. might pick CDROM drive :-)
343 foreach my $DOSdrive (keys %::DOS2Unix) {
344 my $tmp = &ToUnix("$DOSdrive:\\temp");
345 if (-d $tmp) { $TheTemp = "$DOSdrive:\\temp"; last; }
346 $tmp = &ToUnix("$DOSdrive:\\tmp");
347 if (-d $tmp) { $TheTemp = "$DOSdrive:\\tmp"; last; }
349 $TheTemp = '/tmp' if (!$TheTemp && -d '/tmp');
350 if ($TheTemp) {
351 warn "Using $TheTemp\n";
352 print "temp=$TheTemp\n";
354 else {
355 warn "Using C:\\\n";
356 print "temp=C:\\\n";
359 print "\n";
362 # FNunix = &ToUnix(FNdos);
363 # Converts DOS filenames to Unix filenames, leaving Unix filenames
364 # untouched.
365 sub ToUnix {
366 my($FNdos) = @_;
367 my($FNunix);
369 # Initialize tables if necessary.
370 if (!%::DOS2Unix) { &ReadFSTAB; }
372 # Determine which type of conversion is necessary
373 if ($FNdos =~ /^([A-Z])\:(.*)$/) { # DOS drive specified
374 $FNunix = $::DOS2MntPoint{$1} . "/$2";
376 elsif ($FNdos =~ m%\\%) { # DOS drive not specified, C: is default
377 $FNunix = $::DOS2MntPoint{"C"} . "/$FNdos";
379 else { # Unix filename
380 $FNunix = $FNdos;
382 1 while ($FNunix =~ s%\\%/%); # Convert \ to /
383 $FNunix =~ tr/A-Z/a-z/; # Translate to lower case
384 1 while ($FNunix =~ s%//%/%); # Translate double / to /
385 return $FNunix;
388 # FNdos = &ToDOS(FNunix)
389 # Converts Unix filenames to DOS filenames
390 sub ToDos {
391 my($FNunix) = @_;
392 my(@MntList) = keys %::MntPoint2DOS;
393 my ($TheMntPt, $FNdos);
395 foreach my $MntPt (@MntList) { # Scan mount point list to see if path matches
396 if ($FNunix =~ /^$MntPt/) {
397 $TheMntPt = $MntPt;
398 last;
401 if (!$TheMntPt) {
402 Carp("ERROR: $FNunix not found in DOS directories\n");
403 exit(1);
405 $FNdos = $FNunix;
406 $FNdos =~ s/^$TheMntPt//;
407 $FNdos = $::MntPoint2DOS{$TheMntPt} . ":" . $FNdos;
408 1 while($FNdos =~ s%/%\\%);
409 return $FNdos;
412 sub InsertDefaultFile {
413 my ($fileName, $tag) = @_;
414 my $state = 0;
416 if (open(DEFFILE, "$fileName")) {
417 while (<DEFFILE>) {
418 $state = 0 if ($state == 1 && $_ =~ /^[ \t]*\#/o && index($_, "</$tag>") >= 0);
419 print $_ if ($state == 1);
420 $state = 1 if ($state == 0 && $_ =~ /^[ \t]*\#/o && index($_, "<$tag>" ) >= 0);
422 close(DEFFILE);
423 } else {
424 print STDERR "Cannot read $fileName\n";
428 sub StandardStuff {
429 if (!$::opt_inifile) {
430 &InsertDefaultFile("./wine.ini", "wineconf");
431 } else {
432 &InsertDefaultFile($::opt_inifile, "wineconf");
436 sub byFileAge {
437 -M $a <=> -M $b;
440 sub byDriveOrder {
441 my($DeviceA) = $a->[0];
442 my($DeviceB) = $b->[0];
444 # Primary drives come first, logical drives last
445 # DOS User's Guide (version 6) p. 70, IBM version.
446 # If both drives are the same type, sort alphabetically
447 # This makes drive a come before b, etc.
448 # It also makes SCSI drives come before IDE drives;
449 # this may or may not be right :-(
450 my($Alogical, $Blogical);
451 if (substr($DeviceA, 3, 1) >= 5) { $Alogical++; }
452 if (substr($DeviceB, 3, 1) >= 5) { $Blogical++; }
453 if ($Alogical && !$Blogical) { return -1; }
454 elsif ($Blogical && !$Alogical) { return 1; }
455 else { return ($DeviceA cmp $DeviceB); }
458 sub byCdOrder {
459 my($DeviceA) = $a->[0];
460 my($DeviceB) = $b->[0];
461 $DeviceA cmp $DeviceB;