# vim: set sts=2 sw=2 ts=8 et:1#2# Copyright (c) 2001-2004 Akinori MUSHA <[email protected]>3# Copyright (c) 2006-2008 Sergey Matveychuk <[email protected]>4# Copyright (c) 2009-2012 Stanislav Sedov <[email protected]>5#6# All rights reserved.7#8# Redistribution and use in source and binary forms, with or without9# modification, are permitted provided that the following conditions10# are met:11# 1. Redistributions of source code must retain the above copyright12# notice, this list of conditions and the following disclaimer.13# 2. Redistributions in binary form must reproduce the above copyright14# notice, this list of conditions and the following disclaimer in the15# documentation and/or other materials provided with the distribution.16#17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27# SUCH DAMAGE.28#2930class Array31def qindex(item)32lower = -133upper = size()34while lower + 1 != upper35mid = (lower + upper) / 23637cmp = self[mid] <=> item3839cmp.zero? and return mid4041if cmp < 042lower = mid43else44upper = mid45end46end4748nil49end5051alias qinclude? qindex52end5354def shellwords(line)55unless line.kind_of?(String)56raise ArgumentError, "Argument must be String class object."57end58line = line.sub(/\A\s+/, '')59words = []60while line != ''61field = ''62while true63if line.sub!(/\A"(([^"\\]|\\.)*)"/, '') then #"64snippet = $165snippet.gsub!(/\\(.)/, '\1')66elsif line =~ /\A"/ then #"67raise ArgumentError, "Unmatched double quote: #{line}"68elsif line.sub!(/\A'([^']*)'/, '') then #'69snippet = $170elsif line =~ /\A'/ then #'71raise ArgumentError, "Unmatched single quote: #{line}"72elsif line.sub!(/\A\\(.)/, '') then73snippet = $174elsif line.sub!(/\A([^\s\\'"]+)/, '') then #'75snippet = $176else77line.sub!(/\A\s+/, '')78break79end80field.concat(snippet)81end82words.push(field)83end84words85end8687def shelljoin(*args)88args.collect { |arg|89if /[*?{}\[\]<>()~&|\\$;\'\`\s]/ =~ arg90'"' + arg.gsub(/([$\\\"\`])/, "\\\\\\1") + '"'91else92arg93end94}.join(' ')95end9697def init_tmpdir98if ! $tmpdir.nil? && $tmpdir != "" then99return100end101maintmpdir = ENV['PKG_TMPDIR'] || ENV['TMPDIR'] || '/var/tmp'102if !FileTest.directory?(maintmpdir)103raise "Temporary directory #{maintmpdir} does not exist"104end105106cmdline = shelljoin("/usr/bin/mktemp", "-d", maintmpdir + "/portupgradeXXXXXXXX")107pipe = IO.popen(cmdline)108tmpdir = pipe.gets109pipe.close110if $? != 0 || tmpdir.nil? || tmpdir.length == 0111raise "Could not create temporary directory in #{maintmpdir}"112end113tmpdir.chomp!114115at_exit {116begin117xsystem("rm -r #{tmpdir}")118rescue119warning_message "Could not clean up temporary directory: " + $!120end121}122$tmpdir=tmpdir123end124125126