#!/usr/local/bin/kermit +
#
# g e t l i n e  --  Get first free dialout line.
#
# Given a string of the form:
#
#  device,modem,speed:device,modem,speed:device,modem,speed...
# 
# ...this script cycles through the devices until it finds a free one
# that it can assign.  Upon failure it exits with a failure code.
# 
# If it succeeds it returns with the associated modem type and speed also
# set: if called as a "kerbang" script, it leaves you at the C-Kermit>
# prompt; if called from another script, returns to the calling script.
#
# The string can be provided as a command-line argument or (if no
# command-line argument is given) the value of the DIALOUTS environment
# variable (DIALOUTS symbol or logical name in VMS).
#
# Illustrates using \fsplit() to break up delimited strings into arrays.
#
# Author: F. da Cruz, Columbia University, April 1999
# Updated Oct 1999 for QNX port locking.
#
local \&a[] \&b[] \%i myname          ; Local variables
local list defmodem defspeed usage    ; More local variables

.myname := \fbasename(\%0)            ; Name of this script
.defmodem = usr                       ; Default modem type (*)
.defspeed = 57600                     ; Default speed (*)

; (*) Change as needed

define USAGE {
    echo {\m(myname): \%1}
    echo Please supply a device list as (the only) command-line argument or
    echo in the DIALOUTS environment variable.  A device list looks like:
    echo {"device,modem,speed:device,modem,speed:device,modem,speed..."}
    exit 1
}
if > \v(argc) 2 {                     ; Check for bad command line
    usage {Too many arguments.}
}
if def \%1 .list := \%1               ; Get command-line argument 
if not def list .list := \$(DIALOUTS) ; If none get DIALOUTS variable
if not def list {                     ; If none give message and fail
    usage {No device list given.}
}
set quiet on                          ; Suppress error messages
set qnx-port-lock on                  ; Significant in QNX - ignored elsewhere
for \%i 1 \fsplit(\m(list),&a,:) 1 {  ; Loop for each triplet
    dcl \&b[3]                        ; Re-create this array each time
    .\%m := \fsplit(\&a[\%i],&b,{,})  ; Split this triplet
    if not def \&b[1] continue        ; Watch out for empty ones
    echo Trying \&b[1]...
    if not def \&b[2] {               ; If no modem type included
        .\&b[2] = \m(defmodem)        ; supply default
    }
    if not def \&b[3] {               ; Ditto for speed
        .\&b[3] = \m(defspeed)
    }
    set modem type \&b[2]             ; Try to set modem type
    if fail continue                  ; Failed - go to next triplet
    set line \&b[1]                   ; Try to get the line
    if fail continue                  ; Failed - go to next triplet
    set speed \&b[3]                  ; Set the speed but ignore errors
    if !\v(local) continue            ; Make sure we're in local mode
    echo {  Line:  \v(line)}          ; Report what we got...
    echo {  Modem: \v(modem)}
    echo {  Speed: \v(speed)}
    end 0                             ; And return successfully
}
exit 1 No devices free                ; Didn't get one - fail
