OpenVMS Source Code Demos

Logical Name demo in python

Introduction:
  1. UNIX and Linux platforms usually only have one kind of environment variable and these come from the calling shell (e.g. BASH variables)
  2. VMS and OpenVMS systems employ two kinds of environment variables (e.g. DCL variables and Logical Names)
    • In DEC-C for OpenVMS, library routine getenv() will first look for a named shell variable then, if not found, will look for a named logical name.
    • In Python for OpenVMS, library routine os.environ.get() will only look for a named shell variable. If your Python program requires access to a logical name then you will need to do something like this demo.
Menu:
  1. File-1 (just hacking)
  2. File-2 (importable code)
  3. File-3 (how to call)
  4. Links

File-1 (just hacking here)

'''
===========================================================
title   : logical_name_demo_100.py
platform: python-3.10.0 on VSI OpenVMS Version 8.4-1H1 I64
author  : Neil Rieck
created : 2023-07-13
edit    : 2023-07-14
notes   :
1) demos how to read OpenVMS logical names
2) also shows how to reference STARLET definitions
3) this was a quick hack employing a few shortcuts
usage   : $ python logical_name_demo_100.py
===========================================================
'''
from vms import dscdef as DSC       # dscdef is from STARLET
from vms import lnmdef as LNM       # lnmdef is from STARLET
from vms import ssdef as SS         # ssdef  is from STARLET
from vms import ile3 as ILE3        # item list definitions
from vms import sys as SYS          # system calls via python wrapper
import _sys as VMS                  # native system calls (natural parameter order)
from vms.sys import trnlnm          # this is a third way
from vms.lib import get_hostname    # something else

def breakout(title, obj):
    print(title)
    for x in obj:
        print("x: ", x)

def main():
    hn = get_hostname()
    print("hostname: ", hn[1])
    print("debug? (0/1, default=0) ", end="")
    try:
        debug = input()
        if type(debug) != int:
            debug = int(debug)
    except Exception:
        debug = 0
    log_table = "LNM$SYSTEM_TABLE"
    log_name = "SYS$DISK"
    log_value = ""
    print(f"TEST> table: {log_table} logical name: {log_name}")
    #
    for test in range(1, 3):
        print(40*"=", f" test #{test}")
        itmlst = ILE3.ile3list()
        itmlst.append(LNM.LNM__STRING, DSC.DSC_K_DTYPE_T, 255)          # should use 255 here
        if 1 == 0:                                                      # enable for 4 more items
            itmlst.append(LNM.LNM__LENGTH, DSC.DSC_K_DTYPE_LU)          # bytes returned
            itmlst.append(LNM.LNM__INDEX, DSC.DSC_K_DTYPE_LU)           # index
            itmlst.append(LNM.LNM__ATTRIBUTES, DSC.DSC_K_DTYPE_LU)      # created attributes
            itmlst.append(LNM.LNM__TABLE, DSC.DSC_K_DTYPE_T, 32)        # table name
        if debug > 0:
            breakout("itmlist before the call", itmlst)
        if test == 1:
            rc = SYS.trnlnm(None, log_table, log_name, None, itmlst)    # STARLET parameter order
        else:
            # 
            # https://wiki.vmssoftware.com/VMS-Specific_Python_Modules
            # trnlnm(attr:int, tabnam:str, lognam:str, acmode:int, il:object)->int
            #        
            rc = VMS.trnlnm(log_name, log_table, itmlst)                # wrapper parameter order 
        print("rc  :", rc)
        if debug > 0:
            breakout("itmlist after the call", itmlst)
        if rc == SS.SS__NORMAL:
            print("-i-the call to trnlnm was successful")
            log_value = itmlst[0]
            print("log_value: ", log_value)
        else:
            print("-w-the call to trnlnm failed")
    # --------------------------------------------

if __name__ == "__main__":
        main()

# this is the end

File-2 (importable code)

Note: it might make more sense to rename this file "vms_extras.py" then add more vms specific functions to it.

'''
===========================================================
title   : logical_name.py
platform: python-3.10.0 on VSI OpenVMS Version 8.4-1H1 I64
author  : Neil Rieck
created : 2024-02-01 (from logical_name_demo_100.py)
edit    : 2024-02-01
notes   :
1) demos how to read OpenVMS logical names
2) also shows how to reference STARLET definitions
===========================================================
'''
from vms import dscdef as DSC       # dscdef is from STARLET
from vms import lnmdef as LNM       # lnmdef is from STARLET
from vms import ile3 as ILE3        # item list definitions
from vms import sys as SYS          # system calls via python wrapper

def logical_name(log_name="", log_table=""):
    log_value = ""
    rc = 0                              # VMS-w-
    try:
        itmlst = ILE3.ile3list()
        itmlst.append(LNM.LNM__STRING, DSC.DSC_K_DTYPE_T, 255)
        rc = SYS.trnlnm(None, log_table, log_name, None, itmlst)    # STARLET parameter order
        if (rc & 7) == 1:
            log_value = itmlst[0]
    except Exception:
        rc = 2                          # VMS-e-
    return rc, log_value

# this is the end

File-3 (how to call)

'''
title  : logical_name_how_to_call_100.py
author : Neil Rieck
created: 2024-02-01
notes  : shows how to call the hack library
'''

import logical_name as ovms
import sys

def main():
    pgm = sys.argv[0]
    print(f"\n-i-script: {pgm}")
    rc = 1
    value = ""
    table = "LNM$SYSTEM_TABLE"
    name = "SYS$DISK"
    print(f"-i-data-name: {name} table: {table}")
    rc, value = ovms.logical_name(name, table)
    print(f"rc: {rc} value: {value}")
    rc, value = ovms.logical_name("MARIA_PASSWORD", "LNM$SYSTEM_TABLE" )
    print(f"rc: {rc} value: {value}")
    print("-i-normal exit")

if __name__ == "__main__":
    main()

# this is the end

Links