netcalc.bac

'
' Network calculator - a program to calculate the IPv4 network segment
'   based on a netmask
'
' June 2010, PvE - GPL.
' May 2015 - revisions (PvE).
'
' This HTML page created by using enscript:
'  # enscript --color --style=emacs --language=html --highlight=bacon netcalc.bac -p - | expand -t 4 > netcalc.bac.html
'
'--------------------------------------------------------------------------------

INCLUDE "hug.bac"

SUB Calculate_Network

    LOCAL host1, host2, host3, host4, x

    host1 = 256
    IF VAL(GRAB$(n4)) <> 0 THEN
        DECR host1, VAL(GRAB$(n4))
    ELSE
        host2 = 256
        IF VAL(GRAB$(n3)) <> 0 THEN
            DECR host2, VAL(GRAB$(n3))
        ELSE
            host3 = 256
            IF VAL(GRAB$(n2)) <> 0 THEN
                DECR host3, VAL(GRAB$(n2))
            ELSE
                host4 = 256
                IF VAL(GRAB$(n1)) <> 0 THEN DECR host4, VAL(GRAB$(n1))
            END IF
        END IF
    END IF

    IF host1 < 256 THEN
        FOR x = 0 TO 256 STEP host1
            IF VAL(GRAB$(m4)) < x THEN
                TEXT(result1, GRAB$(m1) & "." & GRAB$(m2) & "." & GRAB$(m3) & "." & STR$(x-host1))
                TEXT(result2, GRAB$(m1) & "." & GRAB$(m2) & "." & GRAB$(m3) & "." & STR$(x-1))
                BREAK
            END IF
        NEXT
    ELIF host2 < 256 THEN
        FOR x = 0 TO 256 STEP host2
            IF VAL(GRAB$(m3)) < x THEN
                TEXT(result1, GRAB$(m1) & "." & GRAB$(m2) & "." & STR$(x-host2) & ".0")
                TEXT(result2, GRAB$(m1) & "." & GRAB$(m2) & "." & STR$(x-1) & ".255")
                BREAK
            END IF
        NEXT
    ELIF host3 < 256 THEN
        FOR x = 0 TO 256 STEP host3
            IF VAL(GRAB$(m2)) < x THEN
                TEXT(result1, GRAB$(m1) & "." & STR$(x-host3) & ".0.0")
                TEXT(result2, GRAB$(m1) & "." & STR$(x-1) & ".255.255")
                BREAK
            END IF
        NEXT
    ELIF host4 < 256 THEN
        FOR x = 0 TO 256 STEP host4
            IF VAL(GRAB$(m1)) < x THEN
                TEXT(result1, STR$(x-host4) & ".0.0.0")
                TEXT(result2, STR$(x-1) & ".255.255.255")
                BREAK
            END IF
        NEXT
    END IF

END SUB

'--------------------------------------------------------------------------------

SUB Sort_All(NUMBER widget)

    SELECT widget
        CASE n1
            TEXT(n2, "0")
            TEXT(n3, "0")
            TEXT(n4, "0")
        CASE n2
            TEXT(n1, "255")
            TEXT(n3, "0")
            TEXT(n4, "0")
        CASE n3
            TEXT(n1, "255")
            TEXT(n2, "255")
            TEXT(n4, "0")
        CASE n4
            TEXT(n1, "255")
            TEXT(n2, "255")
            TEXT(n3, "255")
    END SELECT

END SUB

'--------------------------------------------------------------------------------

mainwin = WINDOW("Calculating network", 320, 200)

frame = FRAME(310, 190)
ATTACH (mainwin, frame, 5, 5)

label1 = MARK("Address:", 70, 30)
ATTACH(mainwin, label1, 10, 15)

m1 = ENTRY("192", 50, 30)
ATTACH(mainwin, m1, 85, 15)
m2 = ENTRY("168", 50, 30)
ATTACH(mainwin, m2, 140, 15)
m3 = ENTRY("100", 50, 30)
ATTACH(mainwin, m3, 195, 15)
m4 = ENTRY("145", 50, 30)
ATTACH(mainwin, m4, 250, 15)

label2 = MARK("Netmask:", 70, 30)
ATTACH(mainwin, label2, 10, 55)

n1 = ENTRY("255", 50, 30)
ATTACH(mainwin, n1, 85, 55)
n2 = ENTRY("255", 50, 30)
ATTACH(mainwin, n2, 140, 55)
n3 = ENTRY("255", 50, 30)
ATTACH(mainwin, n3, 195, 55)
n4 = ENTRY("255", 50, 30)
ATTACH(mainwin, n4, 250, 55)

sep = HSEPARATOR(290)
ATTACH(mainwin, sep, 15, 100)

label3 = MARK("Network:", 70, 30)
ATTACH(mainwin, label3, 10, 110)
result1 = MARK("0.0.0.0", 130, 30)
SET(result1, 1)
ATTACH(mainwin, result1, 75, 110)

label4 = MARK("Broadcast:", 70, 30)
ATTACH(mainwin, label4, 10, 150)
result2 = MARK("0.0.0.0", 130, 30)
SET(result2, 1)
ATTACH(mainwin, result2, 75, 150)

calc_but = STOCK("gtk-network", 90, 30)
ATTACH(mainwin, calc_but, 210, 110)
exit_but = STOCK("gtk-quit", 90, 30)
ATTACH(mainwin, exit_but, 210, 150)

CALLBACK(n1, Sort_All)
CALLBACK(n2, Sort_All)
CALLBACK(n3, Sort_All)
CALLBACK(n4, Sort_All)
CALLBACK(calc_but, Calculate_Network)
CALLBACK(exit_but, QUIT)

FOCUS(n4)

DISPLAY

Generated by GNU Enscript 1.6.5.90.