-1

I want to learn to create a wrapper around a program in linux. How does one do this? A tutorial reference web-page/link or example will do. To clarify what I want to learn, I will explain with an example.

I use vim for editing text files. And use rcs as my simple revision control system. rcs allows you to check-in and checkout-files. I would like to create a warpper program named vir which when I type in the shell as:

$ vir temp.txt

will load the file temp.txt into rcs with ci -u temp.txt and then allows me to edit the file using vim.

When I get out and go back in, It will need to check out the file first, using ci -u temp.txt and allow me to edit the file as one normally does with vim, and then when I save and exit, it should check-in the file using co -u temp.txt and as part of that I should be able to add a version control comment.

Basically, all I want to be doing on the command line is:

$ vir temp.txt

as one would with vim. And the wrapper should take care of the version control for me.

  • This is generally what aliases, functions, scripts, or compiled programs are for. Any way you can write a program, you can probably do this. – D. Ben Knoble Oct 27 '19 at 15:13

2 Answers2

0

Take a look at rcsvers.vim, a vim plugin for automatically saving versions in RCS; you could modify that. There are also other RCS plugins for vim at vim.org

rpilkey
  • 945
  • 1
  • 10
  • 16
  • Thank you. I will look into the plugins as that will provide better functionality than I am likely to create for myself. I still want to learn how to create a wrapper myself though, so I would like to learn that as well. – Kris Matrix Oct 28 '19 at 15:26
0

I have a wrapper to enhance the ping command (using zsh) it could, maybe help you:

# ping command wrapper - Last Change: out 27 2019 18:47
# source: https://www.cyberciti.biz/tips/unix-linux-bash-shell-script-wrapper-examples.html
ping(){
    # Name: ping() wrapper
    # Arg: (url|domain|ip)
    # Purpose: Send ping request to domain by removing urls, protocol, username:pass using system /usr/bin/ping
    local array=( $@ )          # get all args in an array
    local host=${array[-1]}     # get the last arg
    local args=${array[1,-2]}   # get all args before last arg in $@
    #local _ping="/usr/bin/ping"
    local _ping="/bin/ping"
    local c=$(_getdomainnameonly "$host")
    [ "$host" != "$c" ] && echo "Sending ICMP ECHO_REQUEST to \"$c\"..."
    # pass args and host
    # $_ping $args $c
    # default args for ping
    $_ping -n -c 2 -i 1 -W1 $c
}

_getdomainnameonly(){
    # Name: _getdomainnameonly
    # Arg: Url/domain/ip
    # Returns: Only domain name
    # Purpose: Get domain name and remove protocol part, username:password and other parts from url
    # get url
    local h="$1"
    # upper to lowercase
    local f="${h:l}"
    # remove protocol part of hostname
    f="${f#http://}"
    f="${f#https://}"
    f="${f#ftp://}"
    f="${f#scp://}"
    f="${f#scp://}"
    f="${f#sftp://}"
    # Remove username and/or username:password part of hostname
    f="${f#*:*@}"
    f="${f#*@}"
    # remove all /foo/xyz.html*
    f=${f%%/*}
    # show domain name only
    echo "$f"
}

What it hides the local ping using a function called "ping", so if your script has precedence on your path it will find at first the function ping. Then inside the script I define an internal variable called ping that points out to the real ping command:

 local _ping="/bin/ping"

You can also notice that the args are stored in one array.

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
  • I think this is what I want. Is this C code or is it bash? It looks like it is bash. I need to an explanation/elaboration on what these lines of code are doing: `local _ping="/bin/ping" #it seems like $_ping gets replaced by /bin/ping. Is this true in c?` `local c=$(_getdomainnameonly "$host") #looks like you are doing an eval of sorts. where you are calling the function getdomainnameonly() and passing variable $host` `$_ping -n -c 2 -i 1 -W1 $c #assume -n, -c 2, -i 1, -W1 are standard ping flags?` – Kris Matrix Oct 28 '19 at 15:36
  • I have never tested this code in "C" but yes, $_ping replaces `/bin/ping`. The getdomainnameonly gets rid of everything but the domain of the target site. As long as I have written this code a long time ago, but these are to make the ping faster. – SergioAraujo Oct 31 '19 at 18:41