Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Proposed changes to _bsd_pkg -- request for comments



Hello users,

I am looking for some feedback from *BSD users/admins who find
themselves using zsh's _bsd_pkg completion for pkg_add, pkg_create,
pkg_delete and pkg_info. I am working with OpenBSD but would really
appreciate some feedback from someone using _bsd_pkg under FreeBSD.

Arising from some light discussion on -workers, I have attached a
revised version of _bsd_pkg which takes into account OpenBSD specifics.
However, I have some queries about this file as well as looking for
people who can confirm its behaviour under FreeBSD and NetBSD.

Part I: _bsd_pkg_pkgfiles(), _bsd_pkg_makepaths(), pkg_add()

Includes a patch for _bsd_pkg_pkgfiles(), which was matching paths
against an undefined variable $PKG_PATH. Is this defined under FreeBSD,
perhaps? I found the completion worked erroneously under OpenBSD.
My attached version fixes that, and also allows this sort of completion:

% ls /usr/ports/packages/blah/
All databases
% ls /usr/ports/packages/blah/All
foo.tgz bar.tgz
% ls /usr/ports/packages/blah/databases
db1.tgz db2.tgz
% pwd
/my/home/directory
% ls -dF * */*
dir1/ dir1/file3.tgz dir2/ file1.tgz file2.txt
% pkg_add <Tab>
file1.tgz foo.tgz bar.tgz
% pkg_add dir1/<Tab>file3.tgz
% pkg_add databases/<Tab>
db1.tgz db2.tgz
% pkg_add foo<Tab>
pkg_add /usr/ports/packages/blah/All/foo.tgz
% pkg_add -v foo.tgz
pkg_add -v /usr/ports/packages/blah/All/foo.tgz

The previous behaviour (for me) was:

% pkg_add <Tab>
/usr/ports/packages/blah/All/foo.tgz
/usr/ports/packages/blah/All/bar.tgz
file1.tgz
dir1
dir2
/bin
/dev
/etc

I like the "new" behaviour. Basically, the selection list is much more
readable and can understand packages subdirectories (to help jog one's
memory). So far, the latter is in a 'case' construct for OpenBSD since
I don't know how other BSDs lay out their /usr/ports/packages/*.

Now this is getting bloat-like, but the previous behaviour seems to
leave plenty of room to squeeze out some day-to-date value. The
patch I post to zsh-workers won't include all of this for now,
but I am interested in users' opinions. Maybe you would like a
function that you could bind to a keystroke to bring up package
lists.

Part II: Flags for pkg_info

Under OpenBSD, the -a option can only be used on its own (no other flags
or arguments are appropriate). I have implemented this inside a 'case'
for openbsd*. If the other BSDs are the same, then we can reduce some
of the 'case' constructs.

Part III: Other patches

Other differences between HEAD and my attachment are (copied from a
patch file that I will pass on to -workers):

# Replaced single quotes with double quotes
# (for readability/neatness when there are nested quote marks).
# Detabbed.
# OpenBSD compatability for pkg_delete, pkg_info, pkg_create options.
# _bsd_pkg_pkgs allows symlinks to directories, not just directories
# (rarely useful, but helped once when I was in a pinch).

-- 
James

#compdef pkg_add pkg_create pkg_delete pkg_info

# James Devenish <j-devenish@xxxxxxxxxxxxxxxxxxxxx>
# Fri, 3 Jan 2003 16:35:32 +0800
# Posted to <zsh-users@xxxxxxxxxx> mailing list for opinions.

# Replaced single quotes with double quotes.
# (for readability/neatness when there are nested quote marks).
# Detabbed.
# OpenBSD compatability for pkg_delete, pkg_info, pkg_create options.
# OpenBSD compatability for pkg_add packages list (requires $MACHTYPE, allows .tar).
# _bsd_pkg_pkgs allow symlinks to directories in addition to directories
# (rarely useful, but helped once when I was in a pinch).
#  - from James Devenish <j-devenish@xxxxxxxxxxxxxxxxxxxxx>
# _bsd_pkg_pkgfiles no longer includes matches from a lone empty path element
# (fixes directories matching /* from appearing when cwd is not /)
#  - from Oliver Kiddle <okiddle@xxxxxxxxxxx>, arising out of a query from JD
# includes abbreviated completion for pkg_add plus pkg_list
#  - some portions taken from Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>

local portsdir pkgsdir paths

(( $+functions[_bsd_pkg_makepaths] )) ||
_bsd_pkg_makepaths() {
  case $OSTYPE in
  netbsd*)
    portsdir=${PORTSDIR:-/usr/pkgsrc}
    pkgsdir=${PACKAGES:-$portsdir/packages}/All
    paths=( . $pkgsdir ${(s.:.)PKG_PATH} )
  ;;
  openbsd*)
    portsdir=${PORTSDIR:-/usr/ports}
    pkgsdir=${PACKAGES:-$portsdir/packages}/$(arch -s)
    paths=( . $pkgsdir/All $pkgsdir ${(s.:.)PKG_PATH} )
  ;;
  *)
    portsdir=${PORTSDIR:-/usr/ports}
    pkgsdir=${PACKAGES:-$portsdir/packages}/All
    paths=( . $pkgsdir ${(s.:.)PKG_PATH} )
  ;;
  esac
}

# files that could be package files:
(( $+functions[_bsd_pkg_pkgfiles] )) ||
_bsd_pkg_pkgfiles() {
  local ret=1 portsdir pkgsdir paths
  
  _bsd_pkg_makepaths

  case $OSTYPE in
  openbsd*)
    _files "$@" -W paths -g '*.(t[bg]z|tar)'
    ;;
  *)
    _files "$@" -W paths -g '*.t[bg]z'
    ;;
  esac
  
  return ret
}

# names of packages that are currently installed:
(( $+functions[_bsd_pkg_pkgs] )) ||
_bsd_pkg_pkgs() {
  compadd "$@" - ${PKG_DBDIR:-/var/db/pkg}/*(-/:t)
}

(( $+functions[_bsd_pkg_pkgs_and_files] )) ||
_bsd_pkg_pkgs_and_files() {
  local ret=1

  if (( $words[(I)-*F*] )); then
    _files "$@" && ret=0
  else
    _bsd_pkg_pkgs "$@" && ret=0
  fi

  return ret
}

_bsd_pkg() {
  local flags

  case "$service" in
  pkg_add)
    flags=(
      "-f[force installation]"
      "-I[don't execute installation scripts]"
      "-M[run in master mode]"
      "-n[don't really install packages]"
      "-p[specify prefix]:prefix directory:_files -/"
      "-R[don't record]"
      "-S[run in slave mode]"
      "-t[specify mktemp template]:mktemp template:_files -/"
      "-v[be verbose]"
    )

    case "$OSTYPE" in
    freebsd*)
      flags=(
        $flags[@]
        "-r[fetch from remote site]"
      )
      ;;
    netbsd*)
      flags=(
        $flags[@]
        "-u[update]"
        "-V[show version and exit]"
      )
      ;;
    esac

    _arguments -s \
      $flags[@] \
      "*:package to install:_bsd_pkg_pkgfiles"
    ;;

  pkg_create)
    flags=(
      "-f[specify plist file]:plist file:_files"
      "(-b)-c[specify comment file]:comment file:_files"
      "(-b)-d[specify descr file]:descr file:_files"
      "-Y[assume YES for any questions asked]"
      "-N[assume NO for any questions asked]"
      "(-b)-O[packing list only mode]"
      "-v[be verbose]"
      "-h[force tar to follow symlinks]"
      "(-b)-i[specify pre-install script]:pre-install script:_files"
      "(-b)-P[specify initial dependencies]:dependencies:_bsd_pkg_pkgs"
      "(-b)-p[specify prefix]:prefix directory:_files -/"
      "(-b)-k[specify deinstall script]:deinstall script:_files"
      "(-b)-r[specify req script]:req script:_files"
      "(-b)-t[specify mktemp template]:mktemp template:_files"
      "(-b)-X[specify exclude file]:exclude file for tar:_files"
      "(-b)-D[specify message file]:message file:_files"
      "(-b)-m[specify mtree file]:mtree file:_files"
    )
    case "$OSTYPE" in
    netbsd*)
      # NetBSD users, improve me!
      flags=(
        $flags[@]
        "*:package name:_bsd_pkg_pkgs"
      )
      ;;
    openbsd*)
        # TODO check -b
      flags=(
        $flags[@]
        "-C[specify conflict list]:conflicts:_bsd_pkg_pkgs"
        "-s[fake prefix]:fake destination directory:_files -/"
        "*:package file name:_files"
      )
      ;;
    freebsd*)
      flags=(
        $flags[@]
        "(-b)-I[specify post-install script]:post-install script:_files"
        "(-b)-s[specify source directory]:source directory:_files -/"
        "(-b)-K[specify post-deinstall script]:post-deinstall script:_files"
        "(-b)-o[specify origin]:origin:_files -W ${PORTSDIR\:-/usr/ports} -/"
        "-j[use bzip2]"
        "-z[use gzip]"
        "(-c -d -O -i -I -P -p -k -K -r -s -t -X -D -m -o)-b[specify pkgname]:pkgname:_bsd_pkg_pkgs"
        "*:package file name:_files"
      )
      ;;
    esac

    _arguments -s \
      $flags[@]
      ;;

  pkg_delete)
    flags=(
      "-D[don't execute deinstallation scripts]"
      "-d[remove empty directories]"
      "-f[force deinstallation]"
      "-n[don't really deinstall packages]"
      "-p[specify prefix]:prefix directory:_files -/"
      "-v[be verbose]"
    )

    case "$OSTYPE" in
    freebsd*)
      flags=(
        $flags[@]
        "(:)-a[delete all installed packages]"
        "-G[do not expand glob patterns]"
        "-i[be interactive]"
        "-r[delete recursively]"
        "-x[use regular expression]"
      )
      ;;
    netbsd*)
      flags=(
        $flags[@]
        "(:)-a[delete all installed packages]"
        "-F[specify each package by an installed file]"
        "-i[be interactive]"
        "-O[only delete the package""s entries]"
        "-R[delete upward recursively]"
        "-r[delete recursively]"
        "-V[show version and exit]"
      )
      ;;
    esac

    case "$OSTYPE" in
    openbsd*)
      _arguments -s \
        $flags[@] \
        "*:installed package name:_bsd_pkg_pkgs"
      ;;
    *)
      _arguments -s \
        $flags[@] \
        "(-a)*:package name:_bsd_pkg_pkgs_and_files"
      ;;
    esac
    ;;

  pkg_info)
    flags=(
      "-c[show comment fields]"
      "-D[show install-message files]"
      "-d[show long descriptions]"
      "-e[test if package is installed]:package name:_bsd_pkg_pkgs"
      "-f[show packing list instructions]"
      "-I[show index lines]"
      "-i[show install scripts]"
      "-k[show deinstall scripts]"
      "-L[show full pathnames of files]"
      "-l[specify prefix string]:prefix string:"
      "-m[show mtree files]"
      "-p[show installation prefixes]"
      "-q[be quiet]"
      "-R[show list list of installed requiring packages]"
      "-r[show requirements scripts]"
      "-v[be verbose]"
    )

    case "$OSTYPE" in
    freebsd*)
      flags=(
        $flags[@]
        "-G[do not expand glob patterns]"
        "-g[show files that are modified]"
        "-o[show origin]"
        "-s[show total size occupied by each package]"
        "-t[specify mktemp template]:mktemp template:_files -/"
        "*-W[show which package the file belongs to]:file:_files"
        "-x[use regular expression]"
      )
      ;;
    netbsd*)
      flags=(
        $flags[@]
        "-B[show build information]"
        "-b[show RCS Id strings]"
        "-F[specify each package by an installed file]"
        "-S[show total size occupied by each package and its dependents]"
        "-s[show total size occupied by each package]"
        "-V[show version and exit]"
      )
      ;;
    esac

    case "$OSTYPE" in
    openbsd*)
      _arguments -s \
        "(* -)-a[show all installed packages]" \
        $flags[@] \
        "*:installed package name:_bsd_pkg_pkgs"
      ;;
    *)
      _arguments -s \
        "(:)-a[show all installed packages]" \
        $flags[@] \
        "(-a)*:package name:_bsd_pkg_pkgs_and_files"
      ;;
    esac
    ;;
  esac
}

# convert abbreviated package paths to full package paths
pkg_add() {
  setopt localoptions noksharrays noshwordsplit
  integer i=$ARGC
  if ((i)); then
    _bsd_pkg_makepaths
    while ((i))
    do
      for candidate in $paths
      do
        if [[ -f $candidate/$argv[i] ]]; then
          argv[i]=$candidate/$argv[i]
          continue
        fi
      done
      ((i--))
    done
    echo pkg_add $*
  fi
  command pkg_add $*
}

_bsd_pkg "$@"



Messages sorted by: Reverse Date, Date, Thread, Author