shell script for Mozilla remote process

"Alex'a X Spirit" <[email protected]>
Newsgroups gmane.comp.mozilla.devel.unix
Organization Ora pro nobis
Message-ID <[email protected]>
Written & tested on FreeBSD 4.9 for Mozilla 1.7.3.

-------------------------------------------------------------------------
#!/bin/sh

# Time-stamp: <Fri Jan  7 18:10:01 EET 2005>
#
# lacerta - shell script for Mozilla remote process
# Copyright (c) 2005 Alexander Gromnizki <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

prog_name=lacerta
prog_ver=0.1a

url_encode()
# taken from Heiner Steven's urlencode-1.2
# modified to ignore '/' char, removed staff with EOL
# and special space handling
{
	echo "$1" | awk '
		BEGIN {
		# We assume an awk implementation that is just plain dumb.
		# We will convert an character to its ASCII value with the
		# table ord[], and produce two-digit hexadecimal output
		# without the printf("%02X") feature.

			split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
			hextab [0] = 0
			for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
		}
		{
			encoded = ""
			for ( i=1; i<=length ($0); ++i ) {
				c = substr ($0, i, 1)
				if ( c ~ /[a-zA-Z0-9.-\/]/ ) {
					encoded = encoded c		# safe character
				} else {
					# unsafe character, encode it as a two-digit hex-number
					lo = ord [c] % 16
					hi = int (ord [c] / 16);
					encoded = encoded "%" hextab [hi] hextab [lo]
			}
		}

		print encoded
		}'
}

usage()
{
	printf "$prog_name $prog_ver - shell script for Mozilla remote process
Written by Alexander Gromnizki <[email protected]> for Mozilla 1.7.3

Usage: $prog_name [-vhFE] [-t|w] files...
    -F    don't add full path
    -E    don't encode argv (usefull for English speaking users)
    -t    load in new tab
    -W    load in already running window
    -v    be verbose (for debug only)
    -h    this help

Examples:
  * Load 2 files in 2 tabs:
        $prog_name -t index.html foo.html
  * Load all html files from current directory in tabs
        $prog_name -t *html
  * Load file in new window:
        $prog_name bar.html
  * FileRunner configure sample for config(view,extensions) variable:
        { {$prog_name -F {%s}}
          {*.html *.htm *.shtml }}
  * Simply load Mozilla web page
        $prog_name http://www.mozilla.org
  * Reading list of the links from stdin:
        cat links.txt | $prog_name
    In this example, file links.txt consists of several lines, where each
    link is located on own line, i.e.: one line - one link
" 1>&2

	exit 1
}

# check for browser
[ `which mozilla` ] || {
	echo Cannot find Mozilla in PATH. Without it this script is unusable 1>&2
	exit 2
}

is_add_full_path=true
is_encode_url=true
open_url_in=new-window

while getopts hvWtFE opt
do
	case $opt in
	F)
		# FileRunner needs this
		is_add_full_path=""
		;;
	E)
		is_encode_url=""
		;;
	v)
		verbose=true
		;;
	t)
		open_url_in=new-tab
		;;
	W)
		# load in already opened window
		open_url_in=""
		;;
	h|*)
		usage
	esac
done
shift `expr $OPTIND - 1`

# for empty argc try to read stdin
if [ $# -eq 0 ] ; then
	while read line
	do
		# one URL by line
		url_list=`printf '%s\n%s' "$url_list" "$line"`
	done
	[ "$url_list" ] || {
		printf '\nNothing to load\n' 1>&2
		exit 1
	}
fi

# parse argv
[ ! "$url_list" ] && {
	[ "$verbose" ] && echo Getting argv from command line...
	for i in "$@"
	do
		url_list=`printf '%s\n%s' "$url_list" "$i"`
	done
}

IFS='
'
for i in $url_list
do
	[ "$verbose" ] && echo "*> $i <*"

	# try to guess is the arg is a local file or real URL
	echo "$i" | egrep '^(http|ftp)://[^ А-Яа-я\t]+$'
	if [ $? -ne 0 ] ; then
		is_cur_arg_file=true
	else
		is_add_full_path=""
		is_encode_url=""
	fi

	if [ "$is_add_full_path" ] ; then
		grep '/' "$i" > /dev/null
		if [ $? -eq 0 ] ; then
			cd `dirname "$i"`
			cur_arg="`pwd`/`basename "$i"`"
		else
			cur_arg="`pwd`/$i"
		fi
	else
		cur_arg="$i"
	fi

	# encode URL
	[ "$is_encode_url" ] && cur_arg=`url_encode "$cur_arg"`

#	echo "$cur_arg" | egrep '^(http|ftp|file)://'
#	[ $? -ne 0 ] && cur_arg="file://$cur_arg"
	[ "$is_cur_arg_file" ] && cur_arg="file://$cur_arg"

	# in this point we have final URL/file arg for browser
	[ "$verbose" ] && echo "cur_arg: $cur_arg"

	# last action: executing Mozilla
	mozilla -remote ping\(\)
	if [ $? -eq 0 ] ; then
		[ "$verbose" ] && echo Connecting to Mozilla...
		if [ "$open_url_in" ] ; then
			mozilla -remote 'openFile('"$cur_arg"','$open_url_in')'
		else
			mozilla -remote 'openFile('"$cur_arg"')'
		fi
	else
		[ "$verbose" ] && echo Starting mozilla...
		mozilla "$cur_arg"
	fi

done
-------------------------------------------------------------------------

-- 
Alex'a X Spirit
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.