PROCEDURE (ram$infu) inform_users, infu (
  message, m: string 0..132 = $optional
  service_name, sn: (BY_NAME, ADVANCED) list 1..15 of name = $optional
  system, systems, s: (BY_NAME, ADVANCED) list of name = $optional
  title_pattern, tp: (BY_NAME, ADVANCED) string = '*T*'
  status)

" PURPOSE:
"   Send messages to NAM/VE terminal users using the CDCNET write_terminal_message command.
" DESIGN:
"   Accept an infinite number of message lines from any properly validated network_operator, and dispatch
"   lines in blocks of 0..132 characters by 1..15 lines, or blocks smaller than maximum CDCNET command length.
" NOTES:
"   Restrict distribution to those terminals with an active connection to the service names provided by this
"   mainframe, and those Device Interface (DI) boxes having the title_pattern as part of their system name.
"   Specify service_name=all to send messages to all terminals with an active CDCNET connection.

  VAR
    build_and_send_message: file = $unique(:$local)
    cdcnet_maximum: integer = 256
    cdcnet_command: string 1..cdcnet_maximum = 'writm'
    ignore: status
    line: string = 'QUIT'
    lines_per_command: integer = 15
    message_acknowledged: integer = 33296
    message_id: string = $char(bel)//'OPERATOR: '//$job(user)//', '//$processor(model)//'/'//$processor(serial_number, 0)
    message_text: string
    services_list: file = $unique(:$local)
    service_names: list 0..$max_list of string
  VAREND

" Delete the file created by this procedure whenever the procedure exits.
  WHEN exit DO
    delete_file file=build_and_send_message
  WHENEND

" Determine which service name connections must be active for the terminal message to be delivered.
  IF $specified(service_name) THEN
    service_names=$apply(service_name, $string(x))
  ELSE
    set_file_attributes file=services_list page_format=continuous file_contents=legible
    MANAGE_NETWORK_APPLICATIONS " Display service names registered by this mainframe.
      display_server_attributes server=osa$timesharing display_option=titles output=services_list
    QUIT
    get_lines variable=service_names input=services_list
    delete_file file=services_list
    service_names=$apply($rest(service_names), x(35, all))
  IFEND

" Construct the write_terminal_message service_name parameter, provided ALL was not specified.
  IF $nil($select(service_names, x='ALL')) THEN
    cdcnet_command=cdcnet_command // ' sn=('
    FOR EACH service IN service_names DO
      cdcnet_command=cdcnet_command // service // ' '
    FOREND
    cdcnet_command=cdcnet_command // ')'
  IFEND

" Save common message generation code, so that it need not be duplicated.
COLLECT_TEXT output=build_and_send_message until='**'
  REPEAT
    message_text=$quote(message_id)
    IF $specified(message) THEN
      message_text=message_text // ' ' // $quote(message)
    ELSE
      FOR message_line = 2 TO lines_per_command DO " Accept message text from input.
        get_line variable=line input=input prompt='infu? '
        EXIT WHEN (line = '') OR (line = 'QUIT') OR (line = 'QUI') OR (line = 'quit') OR (line = 'qui') OR (line = '**')
        IF $size(cdcnet_command//' m=('//message_text//' '//$quote(line)//')')>= cdcnet_maximum THEN
          put_line line=' --ERROR-- Last line too long, please reenter.' output=$response
          EXIT " End message text acceptance for the current message block
        IFEND
        message_text=message_text // ' ' // $quote(line)
      FOREND
    IFEND
    IF message_text <> $quote(message_id) THEN " At least one message line was entered.
      IF NOT $specified(system) THEN " Locate all DI system names to process the message command.
        system=$apply($select($matching_names(title_pattern), x<>''), $name(x))
        EXIT_PROC WHEN $nil(system) with $status(false, 'NA', nae$unknown_title_pattern, $quote(title_pattern))
      IFEND
      display_values values=(cdcnet_command//' m=('//message_text//')', system) output=$job_log
      send_command command=cdcnet_command//' m=('//message_text//')' system=system output=$job_log status=ignore
      FOR EACH tdi IN system DO " Check whether message was delivered by each system sent the message command.
        IF $response_identifier(tdi)= message_acknowledged THEN
          put_line line=' Message received by '//tdi output=$response
        IFEND
      FOREND
    IFEND
  UNTIL (line = '') OR (line = 'QUIT') OR (line = 'QUI') OR (line = 'quit') OR (line = 'qui') OR (line = '**')
**

" Enter operator utility to send the message command if the utility is NOT currently active.
  IF $utility(name)= network_command_utility THEN
    include_file build_and_send_message
  ELSE
    NETWORK_OPERATOR_UTILITY prolog=$null
      include_file build_and_send_message
    QUIT
  IFEND

PROCEND inform_users
