/* $Id: processor.c,v 1.35 2003/07/24 04:48:09 reinelt Exp $ * * main data processing * * Copyright 1999, 2000 by Michael Reinelt (reinelt@eunet.at) * * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. * * * $Log: processor.c,v $ * Revision 1.35 2003/07/24 04:48:09 reinelt * 'soft clear' needed for virtual rows * * Revision 1.34 2003/07/21 06:34:14 reinelt * bars on virtual rows fixed * * Revision 1.33 2003/07/21 06:10:11 reinelt * removed maxlen parameter from process_row() * * Revision 1.32 2003/06/21 05:46:18 reinelt * DVB client integrated * * Revision 1.31 2003/06/13 06:35:56 reinelt * added scrolling capability * * Revision 1.30 2003/02/22 07:53:10 reinelt * cfg_get(key,defval) * * Revision 1.29 2003/02/05 04:31:38 reinelt * * T_EXEC: remove trailing CR/LF * T_EXEC: deactivated maxlen calculation (for I don't understand what it is for :-) * * Revision 1.28 2002/12/05 19:23:01 reinelt * fixed undefined operations found by gcc3 * * Revision 1.27 2002/08/19 04:41:20 reinelt * introduced bar.c, moved bar stuff from display.h to bar.h * * Revision 1.26 2001/05/06 10:01:27 reinelt * * fixed a bug which prevented extendet tokens to be used for GPO's * many thanks to Carsten Nau! * * Revision 1.25 2001/03/17 11:11:31 ltoetsch * bugfix: max for BAR_T * * Revision 1.24 2001/03/16 16:40:17 ltoetsch * implemented time bar * * Revision 1.23 2001/03/16 09:28:08 ltoetsch * bugfixes * * Revision 1.22 2001/03/15 15:49:23 ltoetsch * fixed compile HD44780.c, cosmetics * * Revision 1.21 2001/03/15 09:47:13 reinelt * * some fixes to ppdef * off-by-one bug in processor.c fixed * * Revision 1.20 2001/03/14 13:19:29 ltoetsch * Added pop3/imap4 mail support * * Revision 1.19 2001/03/13 08:34:15 reinelt * * corrected a off-by-one bug with sensors * * Revision 1.18 2001/03/08 15:25:38 ltoetsch * improved exec * * Revision 1.17 2001/03/08 08:39:55 reinelt * * fixed two typos * * Revision 1.16 2001/03/07 18:10:21 ltoetsch * added e(x)ec commands * * Revision 1.15 2001/03/02 10:18:03 ltoetsch * added /proc/apm battery stat * * Revision 1.14 2001/02/19 00:15:46 reinelt * * integrated mail and seti client * major rewrite of parser and tokenizer to support double-byte tokens * * Revision 1.13 2001/02/16 14:15:11 reinelt * * fixed type in processor.c * GPO documentation update from Carsten * * Revision 1.12 2001/02/16 08:23:09 reinelt * * new token 'ic' (ISDN connected) by Carsten Nau * * Revision 1.11 2001/02/14 07:40:16 reinelt * * first (incomplete) GPO implementation * * Revision 1.10 2001/02/13 09:00:13 reinelt * * prepared framework for GPO's (general purpose outputs) * * Revision 1.9 2001/02/11 23:34:07 reinelt * * fixed a small bug where the throughput of an offline ISDN connection * is displayed as '----', but the online value is 5 chars long. * corrected to ' ----'. Thanks to Carsten Nau * * Revision 1.8 2000/08/10 09:44:09 reinelt * * new debugging scheme: error(), info(), debug() * uses syslog if in daemon mode * * Revision 1.7 2000/07/31 10:43:44 reinelt * * some changes to support kernel-2.4 (different layout of various files in /proc) * * Revision 1.6 2000/05/21 06:20:35 reinelt * * added ppp throughput * token is '%t[iomt]' at the moment, but this will change in the near future * * Revision 1.5 2000/04/15 11:56:35 reinelt * * more debug messages * * Revision 1.4 2000/04/15 11:13:54 reinelt * * added '-d' (debugging) switch * added several debugging messages * removed config entry 'Delay' for HD44780 driver * delay loop for HD44780 will be calibrated automatically * * Revision 1.3 2000/04/01 16:22:38 reinelt * * bug that caused a segfault in processor.c fixed (thanks to herp) * * Revision 1.2 2000/03/23 07:24:48 reinelt * * PPM driver up and running (but slow!) * * Revision 1.1 2000/03/22 07:33:50 reinelt * * FAQ added * new modules 'processor.c' contains all data processing * */ /* * exported functions: * * void process_init (void); * does all necessary initializations * * void process (int smooth); * processes a whole screen * bars will always be processed * texts only if smooth=0 * */ #include #include #include #include #include #include "debug.h" #include "cfg.h" #include "system.h" #include "isdn.h" #include "parser.h" #include "display.h" #include "bar.h" #include "processor.h" #include "mail.h" #include "battery.h" #include "dvb.h" #include "seti.h" #include "exec.h" #define ROWS 64 #define GPOS 16 static char *row[ROWS+1]; static int gpo[GPOS+1]; static int rows, cols, xres, yres, supported_bars, gpos; static int lines, scroll, turn; static int token_usage[256]={0,}; static struct { int total, used, free, shared, buffer, cache, avail; } ram; static struct { double load1, load2, load3, overload; } load; static struct { double user, nice, system, idle; } busy; static struct { int read, write, total, max, peak; } disk; static struct { int rx, tx, total, max, peak, bytes; } net; static struct { int usage, in, out, total, max, peak; } isdn; static struct { int rx, tx, total, max, peak; } ppp; static struct { int perc, stat; double dur; } batt; static struct { double perc, cput; } seti; static struct { int num, unseen;} mail[MAILBOXES+1]; static struct { double val, min, max; } sensor[SENSORS+1]; static struct { double strength, snr; } dvb; static double query (int token) { switch (token&255) { case T_MEM_TOTAL: return ram.total; case T_MEM_USED: return ram.used; case T_MEM_FREE: return ram.free; case T_MEM_SHARED: return ram.shared; case T_MEM_BUFFER: return ram.buffer; case T_MEM_CACHE: return ram.cache; case T_MEM_AVAIL: return ram.avail; case T_LOAD_1: return load.load1; case T_LOAD_2: return load.load2; case T_LOAD_3: return load.load3; case T_CPU_USER: return busy.user; case T_CPU_NICE: return busy.nice; case T_CPU_SYSTEM: return busy.system; case T_CPU_BUSY: return 1.0-busy.idle; case T_CPU_IDLE: return busy.idle; case T_DISK_READ: return disk.read; case T_DISK_WRITE: return disk.write; case T_DISK_TOTAL: return disk.total; case T_DISK_MAX: return disk.max; case T_ETH_RX: return net.rx; case T_ETH_TX: return net.tx; case T_ETH_TOTAL: return net.total; case T_ETH_MAX: return net.max; case T_ISDN_IN: return isdn.in; case T_ISDN_OUT: return isdn.out; case T_ISDN_TOTAL: return isdn.total; case T_ISDN_MAX: return isdn.max; case T_ISDN_USED: return isdn.usage; case T_PPP_RX: return ppp.rx; case T_PPP_TX: return ppp.tx; case T_PPP_TOTAL: return ppp.total; case T_PPP_MAX: return ppp.max; case T_SETI_PRC: return seti.perc; case T_SETI_CPU: return seti.cput; case T_BATT_PERC: return batt.perc; case T_BATT_STAT: return batt.stat; case T_BATT_DUR: return batt.dur; case T_DVB_STRENGTH: return dvb.strength; case T_DVB_SNR: return dvb.snr; case T_MAIL: return mail[(token>>8)-'0'].num; case T_MAIL_UNSEEN: return mail[(token>>8)-'0'].unseen; case T_SENSOR: return sensor[(token>>8)-'0'].val; case T_EXEC: return exec[(token>>8)-'0'].val; } return 0.0; } /* return a value 0..1 */ static double query_bar (int token) { int i; double value=query(token); switch (token & 255) { case T_MEM_TOTAL: case T_MEM_USED: case T_MEM_FREE: case T_MEM_SHARED: case T_MEM_BUFFER: case T_MEM_CACHE: case T_MEM_AVAIL: return value/ram.total; case T_LOAD_1: case T_LOAD_2: case T_LOAD_3: return value/load.overload; case T_DISK_READ: case T_DISK_WRITE: case T_DISK_MAX: return value/disk.peak; case T_DISK_TOTAL: return value/disk.peak/2.0; case T_ETH_RX: case T_ETH_TX: case T_ETH_MAX: return value/net.peak; case T_ETH_TOTAL: return value/net.peak/2.0; case T_ISDN_IN: case T_ISDN_OUT: case T_ISDN_MAX: return value/isdn.peak; case T_ISDN_TOTAL: return value/isdn.peak/2.0; case T_PPP_RX: case T_PPP_TX: case T_PPP_MAX: return value/ppp.peak; case T_PPP_TOTAL: return value/ppp.peak/2.0; case T_SETI_PRC: return value; case T_BATT_PERC: { static int alarm; alarm=((alarm+1) % 3); if(value < atoi(cfg_get("battwarning","10")) && !alarm) /* flash bar */ value = 0; return value/100; } case T_SENSOR: i=(token>>8)-'0'; return (value-sensor[i].min)/(sensor[i].max-sensor[i].min); } return value; } static void print_token (int token, char **p, char *start) { double val; int i; switch (token & 255) { case T_PERCENT: *(*p)++='%'; break; case T_DOLLAR: *(*p)++='$'; break; case T_OS: *p+=sprintf (*p, "%s", System()); break; case T_RELEASE: *p+=sprintf (*p, "%s", Release()); break; case T_CPU: *p+=sprintf (*p, "%s", Processor()); break; case T_RAM: *p+=sprintf (*p, "%d", Memory()); break; case T_OVERLOAD: *(*p)++=load.load1>load.overload?'!':' '; break; case T_MEM_TOTAL: case T_MEM_USED: case T_MEM_FREE: case T_MEM_SHARED: case T_MEM_BUFFER: case T_MEM_CACHE: case T_MEM_AVAIL: *p+=sprintf (*p, "%6.0f", query(token)); break; case T_LOAD_1: case T_LOAD_2: case T_LOAD_3: case T_SENSOR: val=query(token); if (val<10.0) *p+=sprintf (*p, "%5.2f", val); else if (val<100.0) *p+=sprintf (*p, "%5.1f", val); else *p+=sprintf (*p, "%5.0f", val); break; case T_CPU_USER: case T_CPU_NICE: case T_CPU_SYSTEM: case T_CPU_BUSY: case T_CPU_IDLE: *p+=sprintf (*p, "%3.0f", 100.0*query(token)); break; case T_ETH_RX: case T_ETH_TX: case T_ETH_MAX: case T_ETH_TOTAL: val=query(token); if (net.bytes) val/=1024.0; *p+=sprintf (*p, "%5.0f", val); break; case T_ISDN_IN: case T_ISDN_OUT: case T_ISDN_MAX: case T_ISDN_TOTAL: if (isdn.usage) *p+=sprintf (*p, "%5.0f", query(token)); else *p+=sprintf (*p, " ----"); break; case T_ISDN_USED: if (isdn.usage) *p+=sprintf (*p, "*"); else *p+=sprintf (*p, " "); break; case T_SETI_PRC: val=100.0*query(token); if (val<100.0) *p+=sprintf (*p, "%4.1f", val); else *p+=sprintf (*p, " 100"); break; case T_SETI_CPU: val=query(token); *p+=sprintf (*p, "%2d.%2.2d:%2.2d", (int)val/86400, (int)((int)val%86400)/3600, (int)(((int)val%86400)%3600)/60 ); break; case T_BATT_PERC: *p+=sprintf(*p, "%3.0f", query(token)); break; case T_BATT_STAT: { int ival = (int) query(token); switch (ival) { case 0: **p = '='; break; case 1: **p = '+'; break; case 2: **p = '-'; break; default: **p = '?'; break; } } (*p)++; break; case T_BATT_DUR: { char eh = 's'; val = query(token); if (val > 99) { val /= 60; eh = 'm'; } if (val > 99) { val /= 60; eh = 'h'; } if (val > 99) { val /= 24; eh = 'd'; } *p+=sprintf(*p, "%2.0f%c", val, eh); } break; case T_DVB_STRENGTH: case T_DVB_SNR: *p+=sprintf (*p, "%5.1f", 100.0*query(token)); break; case T_EXEC: i = (token>>8)-'0'; *p+=sprintf (*p, "%.*s",cols-(*p-start), exec[i].s); break; default: *p+=sprintf (*p, "%5.0f", query(token)); } } static void collect_data (void) { int i; if (token_usage[C_MEM]) { Ram (&ram.total, &ram.free, &ram.shared, &ram.buffer, &ram.cache); ram.used=ram.total-ram.free; ram.avail=ram.free+ram.buffer+ram.cache; } if (token_usage[C_LOAD]) { Load (&load.load1, &load.load2, &load.load3); } if (token_usage[C_CPU]) { Busy (&busy.user, &busy.nice, &busy.system, &busy.idle); } if (token_usage[C_DISK]) { Disk (&disk.read, &disk.write); disk.total=disk.read+disk.write; disk.max=disk.read>disk.write?disk.read:disk.write; if (disk.max>disk.peak) disk.peak=disk.max; } if (token_usage[C_ETH]) { Net (&net.rx, &net.tx, &net.bytes); net.total=net.rx+net.tx; net.max=net.rx>net.tx?net.rx:net.tx; if (net.max>net.peak) net.peak=net.max; } if (token_usage[C_ISDN]) { Isdn (&isdn.in, &isdn.out, &isdn.usage); isdn.total=isdn.in+isdn.out; isdn.max=isdn.in>isdn.out?isdn.in:isdn.out; if (isdn.max>isdn.peak) isdn.peak=isdn.max; } if (token_usage[C_PPP]) { PPP (0, &ppp.rx, &ppp.tx); ppp.total=ppp.rx+ppp.tx; ppp.max=ppp.rx>ppp.tx?ppp.rx:ppp.tx; if (ppp.max>ppp.peak) ppp.peak=ppp.max; } if (token_usage[C_SETI]) { Seti (&seti.perc, &seti.cput); } if (token_usage[C_BATT]) { Battery (&batt.perc, &batt.stat, &batt.dur); } if (token_usage[C_DVB]) { DVB (&dvb.strength, &dvb.snr); } for (i=0; i<=MAILBOXES; i++) { if (token_usage[T_MAIL]&(1<T_EXTENDED) token += (*(unsigned char*)++s)<<8; print_token (token, &p, buffer); } else if (*s=='$') { double val1, val2; int i, type, len; type=*++s; len=*++s; token = *(unsigned char*)++s; if (token>T_EXTENDED) token += (*(unsigned char*)++s)<<8; val1=query_bar(token); val2=val1; if (type & (BAR_H2 | BAR_V2)) { token = *(unsigned char*)++s; if (token>T_EXTENDED) token += (*(unsigned char*)++s)<<8; val2=query_bar(token); } else if (type & BAR_T) val2 = *(unsigned char*)++s; /* width */ if (type & BAR_H) lcd_bar (type, row, p-buffer+1, len*xres, val1*len*xres, val2*len*xres); else if (type & BAR_T) lcd_bar (type, row, p-buffer+1, len*yres, val1*len*yres, val2*xres); else lcd_bar (type, row, p-buffer+1, len*yres, val1*len*yres, val2*len*yres); if (type & BAR_H) { for (i=0; i 0.0); } static int Turn (void) { static struct timeval old = {tv_sec:0, tv_usec:0}; static struct timeval new = {tv_sec:0, tv_usec:0}; struct timeval now; int initialized; if (turn<=0) return 0; gettimeofday (&now, NULL); // first time invocation? initialized=(new.tv_sec>0); if (now.tv_sec==new.tv_sec ? now.tv_usec>new.tv_usec : now.tv_sec>new.tv_sec) { old=now; new.tv_sec =old.tv_sec; new.tv_usec=old.tv_usec+turn*1000; while (new.tv_usec>=1000000) { new.tv_usec-=1000000; new.tv_sec++; } return initialized; } return 0; } void process_init (void) { int i; load.overload=atof(cfg_get("overload","2.0")); lcd_query (&rows, &cols, &xres, &yres, &supported_bars, &gpos); if (rows>ROWS) { error ("%d rows exceeds limit, reducing to %d rows", rows, ROWS); rows=ROWS; } if (gpos>GPOS) { error ("%d gpos exceeds limit, reducing to %d gpos", gpos, GPOS); gpos=GPOS; } debug ("Display: %d rows, %d columns, %dx%d pixels, %d GPOs", rows, cols, xres, yres, gpos); lines=atoi(cfg_get("Rows","1")); if (lines<1) { error ("bad 'Rows' entry in %s, ignoring.", cfg_file()); lines=1; } if (lines>ROWS) { error ("%d virtual rows exceeds limit, reducing to %d rows", lines, ROWS); lines=ROWS; } if (lines>rows) { scroll=atoi(cfg_get("Scroll","1")); if (scroll<1) { error ("bad 'Scroll' entry in %s, ignoring and using '1'", cfg_file()); scroll=1; } if (scroll>rows) { error ("'Scroll' entry in %s is %d, > %d display rows.", cfg_file(), scroll, rows); error ("This may lead to unexpected results!"); } turn=atoi(cfg_get("Turn","1000")); if (turn<1) { error ("bad 'Turn' entry in %s, ignoring and using '1000'", cfg_file()); turn=1; } debug ("Virtual: %d rows, scroll %d lines every %d msec", lines, scroll, turn); } else { lines=rows; scroll=0; turn=0; } for (i=1; i<=lines; i++) { char buffer[8], *p; snprintf (buffer, sizeof(buffer), "Row%d", i); p=cfg_get(buffer,""); debug ("%s: %s", buffer, p); row[i]=strdup(parse_row(p, supported_bars, token_usage)); } for (i=1; i<=gpos; i++) { char buffer[8], *p; snprintf (buffer, sizeof(buffer), "GPO%d", i); p=cfg_get(buffer,""); debug ("%s: %s", buffer, p); gpo[i]=parse_gpo(p, token_usage); } } void process (int smooth) { int i, j, val; char *txt; static int offset=0; collect_data(); if (smooth==0 && Turn()) { offset+=scroll; while (offset>=lines) { offset-=lines; } lcd_clear(0); // soft clear } for (i=1; i<=rows; i++) { j=i+offset; while (j>lines) { j-=lines; } txt=process_row (row[j], i, cols); if (smooth==0) lcd_put (i, 1, txt); } for (i=1; i<=gpos; i++) { val=process_gpo (i); lcd_gpo (i, val); } lcd_flush(); } ' href='#n476'>476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
#!/bin/sh
# install - install a program, script, or datafile

scriptversion=2006-12-25.00

# This originates from X11R5 (mit/util/scripts/install.sh), which was
# later released in X11R6 (xc/config/util/install.sh) with the
# following copyright and license.
#
# Copyright (C) 1994 X Consortium
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name of the X Consortium shall not
# be used in advertising or otherwise to promote the sale, use or other deal-
# ings in this Software without prior written authorization from the X Consor-
# tium.
#
#
# FSF changes to this file are in the public domain.
#
# Calling this script install-sh is preferred over install.sh, to prevent
# `make' implicit rules from creating a file called install from it
# when there is no Makefile.
#
# This script is compatible with the BSD install script, but was written
# from scratch.

nl='
'
IFS=" ""	$nl"

# set DOITPROG to echo to test this script

# Don't use :- since 4.3BSD and earlier shells don't like it.
doit=${DOITPROG-}
if test -z "$doit"; then
  doit_exec=exec
else
  doit_exec=$doit
fi

# Put in absolute file names if you don't have them in your path;
# or use environment vars.

chgrpprog=${CHGRPPROG-chgrp}
chmodprog=${CHMODPROG-chmod}
chownprog=${CHOWNPROG-chown}
cmpprog=${CMPPROG-cmp}
cpprog=${CPPROG-cp}
mkdirprog=${MKDIRPROG-mkdir}
mvprog=${MVPROG-mv}
rmprog=${RMPROG-rm}
stripprog=${STRIPPROG-strip}

posix_glob='?'
initialize_posix_glob='
  test "$posix_glob" != "?" || {
    if (set -f) 2>/dev/null; then
      posix_glob=
    else
      posix_glob=:
    fi
  }
'

posix_mkdir=

# Desired mode of installed file.
mode=0755

chgrpcmd=
chmodcmd=$chmodprog
chowncmd=
mvcmd=$mvprog
rmcmd="$rmprog -f"
stripcmd=

src=
dst=
dir_arg=
dst_arg=

copy_on_change=false
no_target_directory=

usage="\
Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
   or: $0 [OPTION]... SRCFILES... DIRECTORY
   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
   or: $0 [OPTION]... -d DIRECTORIES...

In the 1st form, copy SRCFILE to DSTFILE.
In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
In the 4th, create DIRECTORIES.

Options:
     --help     display this help and exit.
     --version  display version info and exit.

  -c            (ignored)
  -C            install only if different (preserve the last data modification time)
  -d            create directories instead of installing files.
  -g GROUP      $chgrpprog installed files to GROUP.
  -m MODE       $chmodprog installed files to MODE.
  -o USER       $chownprog installed files to USER.
  -s            $stripprog installed files.
  -t DIRECTORY  install into DIRECTORY.
  -T            report an error if DSTFILE is a directory.

Environment variables override the default commands:
  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
  RMPROG STRIPPROG
"

while test $# -ne 0; do
  case $1 in
    -c) ;;

    -C) copy_on_change=true;;

    -d) dir_arg=true;;

    -g) chgrpcmd="$chgrpprog $2"
	shift;;

    --help) echo "$usage"; exit $?;;

    -m) mode=$2
	case $mode in
	  *' '* | *'	'* | *'
'*	  | *'*'* | *'?'* | *'['*)
	    echo "$0: invalid mode: $mode" >&2
	    exit 1;;
	esac
	shift;;

    -o) chowncmd="$chownprog $2"
	shift;;

    -s) stripcmd=$stripprog;;

    -t) dst_arg=$2
	shift;;

    -T) no_target_directory=true;;

    --version) echo "$0 $scriptversion"; exit $?;;

    --)	shift
	break;;

    -*)	echo "$0: invalid option: $1" >&2
	exit 1;;

    *)  break;;
  esac
  shift
done

if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
  # When -d is used, all remaining arguments are directories to create.
  # When -t is used, the destination is already specified.
  # Otherwise, the last argument is the destination.  Remove it from $@.
  for arg
  do
    if test -n "$dst_arg"; then
      # $@ is not empty: it contains at least $arg.
      set fnord "$@" "$dst_arg"
      shift # fnord
    fi
    shift # arg
    dst_arg=$arg
  done
fi

if test $# -eq 0; then
  if test -z "$dir_arg"; then
    echo "$0: no input file specified." >&2
    exit 1
  fi
  # It's OK to call `install-sh -d' without argument.
  # This can happen when creating conditional directories.
  exit 0
fi

if test -z "$dir_arg"; then
  trap '(exit $?); exit' 1 2 13 15

  # Set umask so as not to create temps with too-generous modes.
  # However, 'strip' requires both read and write access to temps.
  case $mode in
    # Optimize common cases.
    *644) cp_umask=133;;
    *755) cp_umask=22;;

    *[0-7])
      if test -z "$stripcmd"; then
	u_plus_rw=
      else
	u_plus_rw='% 200'
      fi
      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
    *)
      if test -z "$stripcmd"; then
	u_plus_rw=
      else
	u_plus_rw=,u+rw
      fi
      cp_umask=$mode$u_plus_rw;;
  esac
fi

for src
do
  # Protect names starting with `-'.
  case $src in
    -*) src=./$src;;
  esac

  if test -n "$dir_arg"; then
    dst=$src
    dstdir=$dst
    test -d "$dstdir"
    dstdir_status=$?
  else

    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
    # might cause directories to be created, which would be especially bad
    # if $src (and thus $dsttmp) contains '*'.
    if test ! -f "$src" && test ! -d "$src"; then
      echo "$0: $src does not exist." >&2
      exit 1
    fi

    if test -z "$dst_arg"; then
      echo "$0: no destination specified." >&2
      exit 1
    fi

    dst=$dst_arg
    # Protect names starting with `-'.
    case $dst in
      -*) dst=./$dst;;
    esac

    # If destination is a directory, append the input filename; won't work
    # if double slashes aren't ignored.
    if test -d "$dst"; then
      if test -n "$no_target_directory"; then
	echo "$0: $dst_arg: Is a directory" >&2
	exit 1
      fi
      dstdir=$dst
      dst=$dstdir/`basename "$src"`
      dstdir_status=0
    else
      # Prefer dirname, but fall back on a substitute if dirname fails.
      dstdir=`
	(dirname "$dst") 2>/dev/null ||
	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
	     X"$dst" : 'X\(//\)[^/]' \| \
	     X"$dst" : 'X\(//\)$' \| \
	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
	echo X"$dst" |
	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
		   s//\1/
		   q
		 }
		 /^X\(\/\/\)[^/].*/{
		   s//\1/
		   q
		 }
		 /^X\(\/\/\)$/{
		   s//\1/
		   q
		 }
		 /^X\(\/\).*/{
		   s//\1/
		   q
		 }
		 s/.*/./; q'
      `

      test -d "$dstdir"
      dstdir_status=$?
    fi
  fi

  obsolete_mkdir_used=false

  if test $dstdir_status != 0; then
    case $posix_mkdir in
      '')
	# Create intermediate dirs using mode 755 as modified by the umask.
	# This is like FreeBSD 'install' as of 1997-10-28.
	umask=`umask`
	case $stripcmd.$umask in
	  # Optimize common cases.
	  *[2367][2367]) mkdir_umask=$umask;;
	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;

	  *[0-7])
	    mkdir_umask=`expr $umask + 22 \
	      - $umask % 100 % 40 + $umask % 20 \
	      - $umask % 10 % 4 + $umask % 2
	    `;;
	  *) mkdir_umask=$umask,go-w;;
	esac

	# With -d, create the new directory with the user-specified mode.
	# Otherwise, rely on $mkdir_umask.
	if test -n "$dir_arg"; then
	  mkdir_mode=-m$mode
	else
	  mkdir_mode=
	fi

	posix_mkdir=false
	case $umask in
	  *[123567][0-7][0-7])
	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
	    ;;
	  *)
	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0

	    if (umask $mkdir_umask &&
		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
	    then
	      if test -z "$dir_arg" || {
		   # Check for POSIX incompatibilities with -m.
		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
		   # other-writeable bit of parent directory when it shouldn't.
		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
		   case $ls_ld_tmpdir in
		     d????-?r-*) different_mode=700;;
		     d????-?--*) different_mode=755;;
		     *) false;;
		   esac &&
		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
		   }
		 }
	      then posix_mkdir=:
	      fi
	      rmdir "$tmpdir/d" "$tmpdir"
	    else
	      # Remove any dirs left behind by ancient mkdir implementations.
	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
	    fi
	    trap '' 0;;
	esac;;
    esac

    if
      $posix_mkdir && (
	umask $mkdir_umask &&
	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
      )
    then :
    else

      # The umask is ridiculous, or mkdir does not conform to POSIX,
      # or it failed possibly due to a race condition.  Create the
      # directory the slow way, step by step, checking for races as we go.

      case $dstdir in
	/*) prefix='/';;
	-*) prefix='./';;
	*)  prefix='';;
      esac

      eval "$initialize_posix_glob"

      oIFS=$IFS
      IFS=/
      $posix_glob set -f
      set fnord $dstdir
      shift
      $posix_glob set +f
      IFS=$oIFS

      prefixes=

      for d
      do
	test -z "$d" && continue

	prefix=$prefix$d
	if test -d "$prefix"; then
	  prefixes=
	else
	  if $posix_mkdir; then
	    (umask=$mkdir_umask &&
	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
	    # Don't fail if two instances are running concurrently.
	    test -d "$prefix" || exit 1
	  else
	    case $prefix in
	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
	      *) qprefix=$prefix;;
	    esac
	    prefixes="$prefixes '$qprefix'"
	  fi
	fi
	prefix=$prefix/
      done

      if test -n "$prefixes"; then
	# Don't fail if two instances are running concurrently.
	(umask $mkdir_umask &&
	 eval "\$doit_exec \$mkdirprog $prefixes") ||
	  test -d "$dstdir" || exit 1
	obsolete_mkdir_used=true
      fi
    fi
  fi

  if test -n "$dir_arg"; then
    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
  else

    # Make a couple of temp file names in the proper directory.
    dsttmp=$dstdir/_inst.$$_
    rmtmp=$dstdir/_rm.$$_

    # Trap to clean up those temp files at exit.
    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0

    # Copy the file name to the temp name.
    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&

    # and set any options; do chmod last to preserve setuid bits.
    #
    # If any of these fail, we abort the whole thing.  If we want to
    # ignore errors from any of these, just make sure not to ignore
    # errors from the above "$doit $cpprog $src $dsttmp" command.
    #
    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&

    # If -C, don't bother to copy if it wouldn't change the file.
    if $copy_on_change &&
       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&

       eval "$initialize_posix_glob" &&
       $posix_glob set -f &&
       set X $old && old=:$2:$4:$5:$6 &&
       set X $new && new=:$2:$4:$5:$6 &&
       $posix_glob set +f &&

       test "$old" = "$new" &&
       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
    then
      rm -f "$dsttmp"
    else
      # Rename the file to the real destination.
      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||

      # The rename failed, perhaps because mv can't rename something else
      # to itself, or perhaps because mv is so ancient that it does not
      # support -f.
      {
	# Now remove or move aside any old file at destination location.
	# We try this two ways since rm can't unlink itself on some
	# systems and the destination file might be busy for other
	# reasons.  In this case, the final cleanup might fail but the new
	# file should still install successfully.
	{
	  test ! -f "$dst" ||
	  $doit $rmcmd -f "$dst" 2>/dev/null ||
	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
	  } ||
	  { echo "$0: cannot unlink or rename $dst" >&2
	    (exit 1); exit 1
	  }
	} &&

	# Now rename the file to the real destination.
	$doit $mvcmd "$dsttmp" "$dst"
      }
    fi || exit 1

    trap '' 0
  fi
done

# Local variables:
# eval: (add-hook 'write-file-hooks 'time-stamp)
# time-stamp-start: "scriptversion="
# time-stamp-format: "%:y-%02m-%02d.%02H"
# time-stamp-end: "$"
# End: