' ***********************************************************
' PROGRAM:      bing.bac
' PURPOSE:      to check whether a network connection is alive
' AUTHOR:               Peter van Eerten (network.bac)
' MODDED:       vovchik (Puppy Linux forum)
'                       command line argument feature added
' COMMENTS:     http://www.basic-converter.org/network.bac.html                ;
' DEPENDS:      bash, bacon
' PLATFORM:     Puppy Linux (actually, any *nix)
' DATE:         30-05-2010
' VERSION:      0.1a
' ***********************************************************


' *****************
' DECLARATIONS
' *****************

OPTION SOCKET 1
GLOBAL site$, port$ TYPE STRING
CONST version$ = "0.1a"

' *****************
' END DECLARATIONS
' *****************


' *****************
' SUBROUTINES
' *****************

' ------------
SUB USAGE()
' ------------
        PRINT NL$,"bing - BaCon network check - ", version$,NL$
        PRINT "Usage: bing IP port", NL$
        PRINT "Where:", NL$
        PRINT "  IP   = a site such as 'www.google.com' or"
        PRINT "         an IP address such as '192.168.0.1'"
        PRINT "  port = a TCP/IP port number, such as '80' for http",NL$
        PRINT "Example: bing www.google.com 80", NL$
        END
END SUB

' ------------
SUB GET_ARGS()
' ------------
        SPLIT ARGUMENT$ BY " " TO array$ SIZE argcnt
        IF argcnt < 3 THEN
                USAGE
        ELSE
                IF VAL(array$[2]) > 0 THEN
                        site$ = array$[1]
                        port$ = array$[2]
                ELSE
                        PRINT NL$, "Input arguments contain bad values."
                        USAGE
                END IF
        END IF
END SUB

' ------------
SUB SHOW_RESULTS(NUMBER OK)
' ------------
        IF OK THEN
                PRINT NL$, "All OK. Site '", site$, "' is reachable.", NL$
        ELSE
                PRINT NL$, "Site '", site$, "' not reachable! ", ERR$(ERROR), NL$
                END
        END IF
END SUB

' ------------
SUB DNS_INTERRUPT()
' ------------
        PRINT NL$, "That DNS lookup took too long! Exiting...", NL$
        END
END SUB

' *****************
' END SUBROUTINES
' *****************


' *****************
' MAIN
' *****************

GET_ARGS
TRAP LOCAL
CATCH GOTO NETWORK_ERROR
ALARM DNS_INTERRUPT, 2000
OPEN CONCAT$(site$, ":", port$) FOR NETWORK AS mynet
CLOSE NETWORK mynet
ALARM DNS_INTERRUPT, 0
SHOW_RESULTS(TRUE)
END

' ------------
LABEL NETWORK_ERROR
' ------------
        SHOW_RESULTS(FALSE)

' *****************
' END MAIN
' *****************