PROCEDURE define_ftam_initiator_address (
  transport_selector, ts: integer 1..12335 = 1
  session_selector, ss: string 0..32 = $optional
  presentation_selector, ps: string 0..8 = $optional
  status)


" Define constants local to this definition procedure.
  "$FORMAT=OFF"
  VAR
    legal_hex_characters: (READ) string 22 = '0123456789abcdefABCDEF'
    current_initiator_configuration: (READ) file = $system.ftam.initiator_configuration
  VAREND
  "$FORMAT=ON"

" Declare variables local to this definition procedure.

  "$FORMAT=OFF"
  VAR
    default_presentation_selector: string = '01'
    default_session_selector: string = '01'
    editing_directives: file
    ignore_status: status
    local_status: status
    presentation_selector_text: string 1..80
    session_selector_text: string 1..80
    new_initiator_configuration: file
  VAREND
  "$FORMAT=ON"

" Initialize local variables.

  editing_directives = $unique($local)
  new_initiator_configuration = $unique($local)

" Verify the procedure has been called by a system operator job.

  IF NOT $job(system) THEN
    EXIT procedure WITH $status(false, 'RA', 0, ..
          'DEFINE_FTAM_INITIATOR_ADDRESS can only be executed by a system operator.')
  IFEND

" Verify the initiator configuration file is present.

  IF (NOT $first($file_attributes(current_initiator_configuration, registered)).registered) THEN
    EXIT procedure WITH $status(false, 'RA', 0, ..
          'The file '//$string(current_initiator_configuration)//' was not found.')
  IFEND

" Verify the value specified for TRANSPORT_SELECTOR is within the ranges allowed.

  IF (transport_selector >= 1000) AND (transport_selector <= 3000) THEN
    EXIT procedure WITH $status(false, 'RA', 0, ..
          'The TRANSPORT_SELECTOR value must not be in the range of 1000 through 3000.')
  IFEND

" Verify the values specified for PRESENTATION_SELECTOR and SESSION_SELECTOR contain an appropriate number
" of hexidecmial digits.

  IF $specified(presentation_selector) THEN
    IF ($scan_not_any(legal_hex_characters, presentation_selector) <> 0) THEN
      EXIT procedure WITH $status(false, 'RA', 0, ..
            'Illegal hexidecimal digit found in PRESENTATION_SELECTOR value.')
    ELSEIF ($mod($strlen(presentation_selector), 2) <> 0) THEN
      EXIT procedure WITH $status(false, 'RA', 0, ..
            'The PRESENTATION_SELECTOR value must contain an even number of digits.')
    IFEND

    IF $specified(session_selector) THEN
      IF ($scan_not_any(legal_hex_characters, session_selector) <> 0) THEN
        EXIT procedure WITH $status(false, 'RA', 0, ..
              'Illegal hexidecimal digit found in SESSION_SELECTOR value.')
      ELSEIF ($mod($strlen(session_selector), 2) <> 0) THEN
        EXIT procedure WITH $status(false, 'RA', 0, ..
              'The SESSION_SELECTOR value must contain an even number of digits.')
      ELSE

" The values for PRESENTATION_SELECTOR and SESSION_SELECTOR are syntactically correct.

        IF presentation_selector = '' THEN
          presentation_selector_text = 'define sap 30 16 C# '
        ELSE
          presentation_selector_text = 'define sap 30 16 H'//presentation_selector//' '
        IFEND

        IF session_selector = '' THEN
          session_selector_text = 'define sap 16 17 C# '
        ELSE
          session_selector_text = 'define sap 16 17 H'//session_selector//' '
        IFEND

" Generate the editing directives neccessary to customize presentation and session selector values in
" the FTAM initiator configuration file.

COLLECT_TEXT output=editing_directives until='*** END_EDITING_DIRECTIVES ***' substitution_mark='?' ..
              status=local_status

" Locate and change the presentation selector for the C code.

        locate_text text='define sap 30' number=1 lines=first..last
        start = $current_column

        locate_text text='70 70' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='?presentation_selector_text?' ..
              line=current columns=start..end

"  Locate and change the presentation selector for the Transport Interface code.

        locate_text text='osa$ftam_client' number=1 lines=first..last
        start = $current_column

        locate_text text=' ts=' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='osa$ftam_client ps=''''?presentation_selector?''''' ..
              line=current columns=start..end

" Locate and change the session selector for C code.

        locate_text text='define sap 16' number=1 lines=first..last
        start = $current_column

        locate_text text='70 50' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='?session_selector_text?' line=current columns=start..end

"  Locate and change the session selector for the Transport Interface code.

        locate_text text='osa$ftam_client' number=1 lines=first..last
        start = $current_column

        locate_text text=' ps=' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='osa$ftam_client ss=''''?session_selector?''''' ..
              line=current columns=start..end

*** END_EDITING_DIRECTIVES ***
        IF NOT local_status.normal THEN

" The editing directives file could not be generated. Map the abnormal status value AME$FILE_NOT_KNOWN to
" PFE$UNKNOWN_PERMANENT_FILE to provide the caller with a more appropriate error message.

          IF local_status.condition = ame$file_not_known THEN
            local_status = $status(false, 'PF', pfe$unknown_permanent_file, $string(editing_directives))
          IFEND
          EXIT procedure WITH local_status
        IFEND

      IFEND
    ELSE " No value was provided for the SESSION_SELECTOR parameter.
      EXIT procedure WITH $status(false, 'RA', 0, ..
            'A SESSION_SELECTOR value is required when PRESENTATION_SELECTOR is specified.')
    IFEND
  ELSE " No value was provided for the PRESENTATION_SELECTOR parameter.

    IF $specified(session_selector) THEN
      EXIT procedure WITH $status(false, 'RA', 0, ..
            'A PRESENTATION_SELECTOR value is required when SESSION_SELECTOR is specified.')
    IFEND

" Generate the editing directives neccessary to set presentation and session selector values in
" the FTAM initiator configuration file to their default values.

COLLECT_TEXT output=editing_directives until='*** END_EDITING_DIRECTIVES ***' substitution_mark='?' ..
          status=local_status

"  Locate and change the presentation selector for the C code to the default value.

        locate_text text='define sap 30' number=1 lines=first..last
        start = $current_column

        locate_text text='70 70' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='define sap 30 16 H?default_presentation_selector? ' ..
              line=current columns=start..end

"  Locate and delete the presentation selector for the Transport Interface code if it exists.

        locate_text text='osa$ftam_client' number=1 lines=first..last
        start = $current_column

        locate_text text=' ts=' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='osa$ftam_client ' line=current columns=start..end

"  Locate and change the session selector for C code to the default value.

        locate_text text='define sap 16' number=1 lines=first..last
        start = $current_column

        locate_text text='70 50' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='define sap 16 17 H?default_session_selector? ' ..
              line=current columns=start..end

*** END_EDITING_DIRECTIVES ***
    IF NOT local_status.normal THEN

" The editing directives file could not be generated.

      IF local_status.condition = ame$file_not_known THEN
        local_status = $status(false, 'PF', pfe$unknown_permanent_file, $string(editing_directives))
      IFEND
      EXIT procedure WITH local_status
    IFEND
  IFEND


" Make a local startup command file.

  copy_file input=current_initiator_configuration output=new_initiator_configuration

" Generate the editing directives neccessary to customize the transport selector value in the FTAM
" initiator configuration file.

COLLECT_TEXT output=editing_directives.$eoi until='*** END_EDITING_DIRECTIVES ***' substitution_mark='?' ..
        status=local_status

        locate_text text='ts=' number=1 lines=first..last
        start = $current_column

        locate_text text=' mc=' number=1 lines=first..last
        end = ($current_column - 1)

        replace_text text=$substring($line_text, start, (end - start + 1)) ..
              new_text='ts=?transport_selector?' line=current columns=start..end

*** END_EDITING_DIRECTIVES ***
  IF NOT local_status.normal THEN

" The editing directives file could not be generated.

    IF local_status.condition = ame$file_not_known THEN
      local_status = $status(false, 'PF', pfe$unknown_permanent_file, $string(editing_directives))
    IFEND
    EXIT procedure WITH local_status
  IFEND

  IF $first($file_attributes(editing_directives, registered)).registered AND ..
        $first($file_attributes(editing_directives, size)).size <> 0 THEN
COLLECT_TEXT output=editing_directives.$eoi until='*** END_EDITING_DIRECTIVES ***' status=local_status
        QUIT
*** END_EDITING_DIRECTIVES ***
    IF NOT local_status.normal THEN

" The editing directives file could not be generated.

      IF local_status.condition = ame$file_not_known THEN
        local_status = $status(false, 'PF', pfe$unknown_permanent_file, $string(editing_directives))
      IFEND
      EXIT procedure WITH local_status
    IFEND

" Customize the initiator configuration file using EDIT_FILE. A seperate task is created to bypass problems
" which could result if this procedure is called from within the EDIT_FILE utility. FORMAT_SCL_PROCEDURE will
" generate an error indicating that no end-of-utility statement was found. Inhibit formatting.

    TASK
      "$FORMAT=OFF"
      $system.EDIT_FILE file=new_initiator_configuration input=editing_directives output=$null prolog=$null
      "$FORMAT=ON"
    TASKEND

    copy_file input=new_initiator_configuration output=current_initiator_configuration.$next

    TASK ring=6
      change_file_attributes file=current_initiator_configuration ring_attributes=(6, 13, 13)
    TASKEND
  IFEND

  detach_file file=new_initiator_configuration status=ignore_status
  delete_file file=new_initiator_configuration status=ignore_status

  detach_file file=editing_directives status=ignore_status
  delete_file file=editing_directives status=ignore_status

PROCEND define_ftam_initiator_address
