#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL history_count AS LONG
    LOCAL history_pos AS LONG
    LOCAL max_history AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL temp_str AS STRING
    LOCAL search_result AS LONG
    LOCAL search_idx AS LONG
    LOCAL found_idx AS LONG
    LOCAL search_hit AS LONG

    max_history = 100
    history_count = 10

    PRINT "=== Command History Demo ==="
    temp_str = STR$(history_count)
    PRINT "History size: " + temp_str + " entries"
    PRINT ""

    ' Simulate entering commands
    i = 0
    WHILE i < 8
        IF i = 0 THEN PRINT ": HELP"
        IF i = 1 THEN PRINT ": DATE"
        IF i = 2 THEN PRINT ": TIME"
        IF i = 3 THEN PRINT ": VER"
        IF i = 4 THEN PRINT ": VOL"
        IF i = 5 THEN PRINT ": LABEL"
        IF i = 6 THEN PRINT ": MORE"
        IF i = 7 THEN PRINT ": EXIT"
        history_count = history_count + 1
        i = i + 1
    WEND

    PRINT ""
    PRINT "=== History Recall Demo ==="
    PRINT ""

    temp_str = STR$(history_count)
    PRINT "Full command history (" + temp_str + " entries):"
    PRINT "------------------------------"
    i = 0
    WHILE i < history_count
        temp_str = STR$(i + 1)
        IF i < 9 THEN
            temp_str = " " + temp_str
        END IF
        IF i < 99 THEN
            temp_str = " " + temp_str
        END IF

        IF i = 0 THEN PRINT temp_str + ": DIR /W"
        IF i = 1 THEN PRINT temp_str + ": CD Documents"
        IF i = 2 THEN PRINT temp_str + ": TYPE README.TXT"
        IF i = 3 THEN PRINT temp_str + ": CLS"
        IF i = 4 THEN PRINT temp_str + ": COPY FILE1.TXT FILE2.TXT"
        IF i = 5 THEN PRINT temp_str + ": DEL TEMP.TMP"
        IF i = 6 THEN PRINT temp_str + ": REN OLD.TXT NEW.TXT"
        IF i = 7 THEN PRINT temp_str + ": MKDIR BACKUP"
        IF i = 8 THEN PRINT temp_str + ": CD BACKUP"
        IF i = 9 THEN PRINT temp_str + ": DIR *.BAK"
        IF i = 10 THEN PRINT temp_str + ": HELP"
        IF i = 11 THEN PRINT temp_str + ": DATE"
        IF i = 12 THEN PRINT temp_str + ": TIME"
        IF i = 13 THEN PRINT temp_str + ": VER"
        IF i = 14 THEN PRINT temp_str + ": VOL"
        IF i = 15 THEN PRINT temp_str + ": LABEL"
        IF i = 16 THEN PRINT temp_str + ": MORE"
        IF i = 17 THEN PRINT temp_str + ": EXIT"
        i = i + 1
    WEND

    PRINT ""
    PRINT "Up-Arrow recall simulation:"
    PRINT "------------------------------"
    history_pos = history_count - 1
    i = 0
    WHILE i < 5
        IF history_pos >= 0 THEN
            temp_str = STR$(i + 1)
            IF history_pos = 0 THEN PRINT "Recall " + temp_str + ": DIR /W"
            IF history_pos = 1 THEN PRINT "Recall " + temp_str + ": CD Documents"
            IF history_pos = 2 THEN PRINT "Recall " + temp_str + ": TYPE README.TXT"
            IF history_pos = 3 THEN PRINT "Recall " + temp_str + ": CLS"
            IF history_pos = 4 THEN PRINT "Recall " + temp_str + ": COPY FILE1.TXT FILE2.TXT"
            IF history_pos = 5 THEN PRINT "Recall " + temp_str + ": DEL TEMP.TMP"
            IF history_pos = 6 THEN PRINT "Recall " + temp_str + ": REN OLD.TXT NEW.TXT"
            IF history_pos = 7 THEN PRINT "Recall " + temp_str + ": MKDIR BACKUP"
            IF history_pos = 8 THEN PRINT "Recall " + temp_str + ": CD BACKUP"
            IF history_pos = 9 THEN PRINT "Recall " + temp_str + ": DIR *.BAK"
            IF history_pos = 10 THEN PRINT "Recall " + temp_str + ": HELP"
            IF history_pos = 11 THEN PRINT "Recall " + temp_str + ": DATE"
            IF history_pos = 12 THEN PRINT "Recall " + temp_str + ": TIME"
            IF history_pos = 13 THEN PRINT "Recall " + temp_str + ": VER"
            IF history_pos = 14 THEN PRINT "Recall " + temp_str + ": VOL"
            IF history_pos = 15 THEN PRINT "Recall " + temp_str + ": LABEL"
            IF history_pos = 16 THEN PRINT "Recall " + temp_str + ": MORE"
            IF history_pos = 17 THEN PRINT "Recall " + temp_str + ": EXIT"
            history_pos = history_pos - 1
            IF history_pos < 0 THEN
                history_pos = history_count - 1
            END IF
        END IF
        i = i + 1
    WEND

    PRINT ""
    PRINT "History search for 'DIR':"
    PRINT "------------------------------"
    search_result = -1
    i = history_count - 1
    WHILE i >= 0
        temp_str = ""
        IF i = 0 THEN temp_str = "DIR /W"
        IF i = 1 THEN temp_str = "CD Documents"
        IF i = 2 THEN temp_str = "TYPE README.TXT"
        IF i = 3 THEN temp_str = "CLS"
        IF i = 4 THEN temp_str = "COPY FILE1.TXT FILE2.TXT"
        IF i = 5 THEN temp_str = "DEL TEMP.TMP"
        IF i = 6 THEN temp_str = "REN OLD.TXT NEW.TXT"
        IF i = 7 THEN temp_str = "MKDIR BACKUP"
        IF i = 8 THEN temp_str = "CD BACKUP"
        IF i = 9 THEN temp_str = "DIR *.BAK"
        IF i = 10 THEN temp_str = "HELP"
        IF i = 11 THEN temp_str = "DATE"
        IF i = 12 THEN temp_str = "TIME"
        IF i = 13 THEN temp_str = "VER"
        IF i = 14 THEN temp_str = "VOL"
        IF i = 15 THEN temp_str = "LABEL"
        IF i = 16 THEN temp_str = "MORE"
        IF i = 17 THEN temp_str = "EXIT"

        IF INSTR(1, temp_str, "DIR") > 0 THEN
            search_result = i
            PRINT "Found at position " + STR$(i + 1) + ": " + temp_str
        END IF
        i = i - 1
    WEND
    IF search_result < 0 THEN
        PRINT "Not found."
    END IF

    PRINT ""
    PRINT "Reverse-i-search for 'C':"
    PRINT "------------------------------"
    search_idx = history_count - 1
    i = 0
    WHILE i < 3
        found_idx = -1
        j = search_idx
        WHILE j >= 0
            temp_str = ""
            IF j = 0 THEN temp_str = "DIR /W"
            IF j = 1 THEN temp_str = "CD Documents"
            IF j = 2 THEN temp_str = "TYPE README.TXT"
            IF j = 3 THEN temp_str = "CLS"
            IF j = 4 THEN temp_str = "COPY FILE1.TXT FILE2.TXT"
            IF j = 5 THEN temp_str = "DEL TEMP.TMP"
            IF j = 6 THEN temp_str = "REN OLD.TXT NEW.TXT"
            IF j = 7 THEN temp_str = "MKDIR BACKUP"
            IF j = 8 THEN temp_str = "CD BACKUP"
            IF j = 9 THEN temp_str = "DIR *.BAK"
            IF j = 10 THEN temp_str = "HELP"
            IF j = 11 THEN temp_str = "DATE"
            IF j = 12 THEN temp_str = "TIME"
            IF j = 13 THEN temp_str = "VER"
            IF j = 14 THEN temp_str = "VOL"
            IF j = 15 THEN temp_str = "LABEL"
            IF j = 16 THEN temp_str = "MORE"
            IF j = 17 THEN temp_str = "EXIT"

            IF INSTR(1, temp_str, "C") > 0 THEN
                found_idx = j
                j = -1
            END IF
            j = j - 1
        WEND
        IF found_idx >= 0 THEN
            temp_str = ""
            IF found_idx = 0 THEN temp_str = "DIR /W"
            IF found_idx = 1 THEN temp_str = "CD Documents"
            IF found_idx = 2 THEN temp_str = "TYPE README.TXT"
            IF found_idx = 3 THEN temp_str = "CLS"
            IF found_idx = 4 THEN temp_str = "COPY FILE1.TXT FILE2.TXT"
            IF found_idx = 5 THEN temp_str = "DEL TEMP.TMP"
            IF found_idx = 6 THEN temp_str = "REN OLD.TXT NEW.TXT"
            IF found_idx = 7 THEN temp_str = "MKDIR BACKUP"
            IF found_idx = 8 THEN temp_str = "CD BACKUP"
            IF found_idx = 9 THEN temp_str = "DIR *.BAK"
            IF found_idx = 10 THEN temp_str = "HELP"
            IF found_idx = 11 THEN temp_str = "DATE"
            IF found_idx = 12 THEN temp_str = "TIME"
            IF found_idx = 13 THEN temp_str = "VER"
            IF found_idx = 14 THEN temp_str = "VOL"
            IF found_idx = 15 THEN temp_str = "LABEL"
            IF found_idx = 16 THEN temp_str = "MORE"
            IF found_idx = 17 THEN temp_str = "EXIT"
            PRINT "(reverse-i-search): " + temp_str
            search_idx = found_idx - 1
        ELSE
            search_idx = history_count - 1
        END IF
        IF search_idx < 0 THEN
            search_idx = history_count - 1
        END IF
        i = i + 1
    WEND

    PRINT ""
    temp_str = STR$(history_count)
    PRINT "History entries: " + temp_str
    PRINT "Command history demo complete."

    FUNCTION = 0
END FUNCTION
