PROCEDURE control_access, cona (
  system_file_name, sfn: name = $required
  device_environment_variable, dev: name = $required
  authorization_file, af: file = $required
  statistics_file, sf: file = $required
  file_disposition, fd: (VAR) key
      (hold, h)
      (print_and_hold, pah)
      (print_and_terminate, pat)
      (terminate, t)
    keyend = $required
  status)

" CONTROL_ACCESS Batch Output Filter.
"
"   This filter validates the LOGIN_FAMILY/LOGIN_USER that created the queue
"   file for access to the STATION or STATION,DEVICE.
"
"   Access is granted to users who appear in the AUTHORIZATION_FILE under a
"   specific STATION or STATION,DEVICE entry, or if the specific STATION or
"   STATION,DEVICE does not appear in the AUTHORIZATION_FILE.
"   Access is also granted automatically if the AUTHORIZATION_FILE is empty
"   or does not exist.
"
"   Access is denied (the file is put on HOLD) when the STATION or
"   STATION,DEVICE entry appears in the authorization file and the user
"   does not appear below this entry.
"
"   AUTHORIZATION_FILE Format:
"
"     station_name{,device_name}
"     :family_name.user_name_1
"     :family_name.user_name_n
"                                    <--- Blank line terminates user list
"     station_name{,device_name}
"     :family_name.user_name_1
"     :family_name.user_name_n

  VAR
    device: string
    ignore: status
    local_status: status
    station: string
    station_line: integer
    user: string
  VAREND

  IF ($file(authorization_file, size) > 0) AND (file_disposition <> hold) AND ..
        (file_disposition <> terminate) THEN

    device = $string($device_attributes(dev, device_name))
    station = $string($device_attributes(dev, station))
    user = ':'//$job_output(sfn, login_family)//'.'//$job_output(sfn, login_user)

    EDIT_FILE authorization_file p=$null o=$null

" Delete Comment Lines (double quote in column 1) and remove embedded blanks.

      set_search_margins 1..1
      delete_text '"' l=a n=a status=ignore
      set_search_margins
      replace_text ' ' '' l=a n=a status=ignore

" Insert blank line (terminator) after last entry.

      position_cursor l=last
      insert_empty_line

" Search for STATION,DEVICE entry.  If not found, try STATION.

      locate_text station//','//device l=a n=1 uc=true w=true ..
            status=local_status
      IF NOT local_status.normal THEN
        locate_text station l=a n=1 uc=true w=true status=local_status
      IFEND

" If entry found, look for user before the next blank line.

      IF local_status.normal THEN
        station_line = $current_line
        locate_empty_line
        locate_text user l=station_line+1..current n=1 uc=true w=true ..
              status=local_status

" If user not found, HOLD file and issue statistic.

        IF NOT local_status.normal THEN
          file_disposition = hold
          put_line 'BF10000 ''ACCESS TO PRINTER DENIED: SFN='//sfn//' U='//..
user//' S='//station//' D='//device//'''' o=statistics_file.$eoi
        IFEND
      IFEND

    QUIT false " Changes are temporary - do not modify file.

  IFEND

PROCEND control_access
