| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: osinit.pl,v 2.8 2005/06/21 22:10:53 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood mhonarc@mhonarc.org | ||
| 6 | ## Description: | ||
| 7 | ## A library for setting up a script based upon the OS the script | ||
| 8 | ## is running under. The main routine defined is OSinit. See | ||
| 9 | ## the routine for specific information. | ||
| 10 | ##---------------------------------------------------------------------------## | ||
| 11 | ## MHonArc -- Internet mail-to-HTML converter | ||
| 12 | ## Copyright (C) 1995-1999 Earl Hood, mhonarc@mhonarc.org | ||
| 13 | ## | ||
| 14 | ## This program is free software; you can redistribute it and/or modify | ||
| 15 | ## it under the terms of the GNU General Public License as published by | ||
| 16 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 17 | ## (at your option) any later version. | ||
| 18 | ## | ||
| 19 | ## This program is distributed in the hope that it will be useful, | ||
| 20 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 21 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 22 | ## GNU General Public License for more details. | ||
| 23 | ## | ||
| 24 | ## You should have received a copy of the GNU General Public License | ||
| 25 | ## along with this program; if not, write to the Free Software | ||
| 26 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
| 27 | ## 02111-1307, USA | ||
| 28 | ##---------------------------------------------------------------------------## | ||
| 29 | |||
| 30 | use File::Basename; | ||
| 31 | |||
| 32 | package mhonarc; | ||
| 33 | |||
| 34 | ##---------------------------------------------------------------------------## | ||
| 35 | ## OSinit() checks what operating system we are running on set | ||
| 36 | ## some global variables that can be used by the calling routine. | ||
| 37 | ## All global variables are exported to package main. | ||
| 38 | ## | ||
| 39 | ## Variables set: | ||
| 40 | ## | ||
| 41 | ## $MSDOS => Set to 1 if running under MS-DOS/Windows | ||
| 42 | ## $MACOS => Set to 1 if running under Mac | ||
| 43 | ## $UNIX => Set to 1 if running under Unix | ||
| 44 | ## $VMS => Set to 1 if running under VMS | ||
| 45 | ## $DIRSEP => Directory separator character | ||
| 46 | ## $DIRSEPREX => Directory separator character for use in | ||
| 47 | ## regular expressions. | ||
| 48 | ## $PATHSEP => Recommend path list separator | ||
| 49 | ## $CURDIR => Current working directory | ||
| 50 | ## $PROG => Program name with leading pathname component | ||
| 51 | ## stripped off. | ||
| 52 | ## | ||
| 53 | ## If running under a Mac and the script is a droplet, command-line | ||
| 54 | ## options will be prompted for unless $noOptions argument is | ||
| 55 | ## set to true. | ||
| 56 | ## | ||
| 57 | sub OSinit { | ||
| 58 | my ($noOptions) = shift; | ||
| 59 | |||
| 60 | ## Check what system we are executing under | ||
| 61 | my ($tmp); | ||
| 62 | if ($^O =~ /vms/i) { | ||
| 63 | $MSDOS = 0; | ||
| 64 | $MACOS = 0; | ||
| 65 | $UNIX = 0; | ||
| 66 | $VMS = 1; | ||
| 67 | $DIRSEP = '/'; | ||
| 68 | $CURDIR = '.'; | ||
| 69 | $PATHSEP = ':'; | ||
| 70 | fileparse_set_fstype('VMS'); | ||
| 71 | |||
| 72 | } elsif ( | ||
| 73 | ($^O !~ /cygwin/i) | ||
| 74 | && ( ($^O =~ /mswin/i) | ||
| 75 | || ($^O =~ /\bdos\b/i) | ||
| 76 | || ($^O =~ /\bos2\b/i) | ||
| 77 | || ( ($tmp = $ENV{'COMSPEC'}) | ||
| 78 | && ($tmp =~ /^[a-zA-Z]:\\/) | ||
| 79 | && (-e $tmp)) | ||
| 80 | ) | ||
| 81 | ) { | ||
| 82 | $MSDOS = 1; | ||
| 83 | $MACOS = 0; | ||
| 84 | $UNIX = 0; | ||
| 85 | $VMS = 0; | ||
| 86 | $DIRSEP = '\\'; | ||
| 87 | $CURDIR = '.'; | ||
| 88 | $PATHSEP = ';'; | ||
| 89 | fileparse_set_fstype(($^O =~ /mswin/i) ? 'MSWin32' : 'MSDOS'); | ||
| 90 | |||
| 91 | } elsif (defined($MacPerl::Version)) { | ||
| 92 | $MSDOS = 0; | ||
| 93 | $MACOS = 1; | ||
| 94 | $UNIX = 0; | ||
| 95 | $VMS = 0; | ||
| 96 | $DIRSEP = ':'; | ||
| 97 | $CURDIR = ':'; | ||
| 98 | $PATHSEP = "\n"; | ||
| 99 | fileparse_set_fstype('MacOS'); | ||
| 100 | |||
| 101 | } else { | ||
| 102 | $MSDOS = 0; | ||
| 103 | $MACOS = 0; | ||
| 104 | $UNIX = 1; | ||
| 105 | $VMS = 0; | ||
| 106 | $DIRSEP = '/'; | ||
| 107 | $CURDIR = '.'; | ||
| 108 | $PATHSEP = ':'; | ||
| 109 | fileparse_set_fstype('UNIX'); | ||
| 110 | } | ||
| 111 | |||
| 112 | ## Store name of program | ||
| 113 | if ($MSDOS) { | ||
| 114 | $DIRSEPREX = "\\\\\\/"; | ||
| 115 | } else { | ||
| 116 | ($DIRSEPREX = $DIRSEP) =~ s/(\W)/\\$1/g; | ||
| 117 | } | ||
| 118 | ($PROG = $0) =~ s%.*[$DIRSEPREX]%%o; | ||
| 119 | |||
| 120 | ## Ask for command-line options if script is a Mac droplet | ||
| 121 | ## Code taken from the MacPerl FAQ | ||
| 122 | if ( !$noOptions | ||
| 123 | && defined($MacPerl::Version) | ||
| 124 | && ($MacPerl::Version =~ /Application$/)) { | ||
| 125 | |||
| 126 | # we're running from the app | ||
| 127 | local ($cmdLine, @args); | ||
| 128 | $cmdLine = &MacPerl::Ask("Enter command line options:"); | ||
| 129 | require "shellwords.pl"; | ||
| 130 | @args = &shellwords($cmdLine); | ||
| 131 | unshift(@ARGV, @args); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | ##---------------------------------------------------------------------------## | ||
| 136 | ## OSis_absolute_path() returns true if a string is an absolute path | ||
| 137 | ## | ||
| 138 | sub OSis_absolute_path { | ||
| 139 | |||
| 140 | if ($MSDOS) { | ||
| 141 | return $_[0] =~ /^([a-z]:)?[\\\/]/i; | ||
| 142 | } | ||
| 143 | if ($MACOS) { ## Not sure about Mac | ||
| 144 | return $_[0] =~ /^:/o; | ||
| 145 | } | ||
| 146 | $_[0] =~ m|^/|o; ## Unix (fallback) | ||
| 147 | } | ||
| 148 | |||
| 149 | ##---------------------------------------------------------------------------## | ||
| 150 | 1; |