runbut 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env guile
  2. !#
  3. ;;; runbut.scm --- Run program if it's not one of the undesired ones
  4. ;; Copyright © 2016 Alex Kost
  5. ;; Author: Alex Kost <alezost@gmail.com>
  6. ;; Created: 4 Mar 2016
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; If you have several programs with the same name in $PATH, you can use
  19. ;; this script to avoid running particular ones.
  20. ;; For example, if there is "/bin/foo" program and you want to make a
  21. ;; wrapper shell script "~/bin/foo" (which has a priority in your
  22. ;; $PATH), this script may look like this:
  23. ;;
  24. ;; #!/bin/bash
  25. ;;
  26. ;; runbut foo "${BASH_SOURCE[0]}" -- "$@" &>> /dev/null
  27. ;;
  28. ;; So when you run "foo" in the command-line, "~/bin/foo" is started and
  29. ;; with a help of "runbut" it runs another available "foo" (which is
  30. ;; "/bin/foo").
  31. ;;; Code:
  32. (use-modules
  33. (ice-9 match)
  34. (srfi srfi-11)
  35. (al files)
  36. (al messages)
  37. (al utils))
  38. (define (show-help)
  39. (format #t "Usage: ~a PROGRAM [FILES ...] [-- ARGS ...]
  40. Find PROGRAM in $PATH if it is not one of FILES and run it with ARGS.
  41. "
  42. (car (command-line))))
  43. (define (main args)
  44. (match (cdr args)
  45. (((or "-h" "--help" "help") _ ...)
  46. (show-help))
  47. ((program rest ...)
  48. (let-values (((files args) (split rest "--")))
  49. (let ((prog (apply which-but program files)))
  50. (if prog
  51. (apply system* prog args)
  52. (leave "Program not found: ~a" program)))))
  53. (_ (show-help))))
  54. (when (batch-mode?)
  55. (main (command-line)))
  56. ;;; runbut.scm ends here