?? RIGHT := 110 ??
*copyc osd$default_pragmats
?? NEWTITLE := 'NOS/VE PTF Application : Remote File Access' ??

MODULE nfm$remote_file_access;

{
{ PURPOSE:
{   This module contains procedures to perform accesses to remote
{   permanent files.  Remote access is made implicitly by use of
{   certain SCL commands with references to files on remote families.
{   These procedures interface with the command processors.
{

?? NEWTITLE := 'Global Declarations', EJECT ??
?? PUSH (LISTEXT := ON) ??
*copyc clc$compiling_for_test_harness
*copyc clt$unbundled_pdt
*copyc clt$parameter_substitutions
*copyc clt$parameter_value_table
*copyc fsc$local
*copyc fst$file_reference
*copyc nfc$library_definitions
*copyc nfe$ptf_condition_codes
*copyc nft$mode_of_access
*copyc nft$number_implicit_commands
*copyc ost$name_reference
*copyc ost$user_identification
*copyc ost$status
?? POP ??
*copyc amp$get_file_attributes
*copyc avp$get_capability
*copyc clp$convert_string_to_file
*copyc clp$internal_convert_to_string
*copyc clp$trimmed_string_size
*copyc i#current_sequence_position
*copyc jmp$system_job
*copyc nfp$verify_family
*copyc osp$set_status_abnormal
*copyc pmp$execute

?? TITLE := 'nfp$check_implicit_access', EJECT ??

  PROCEDURE [XDCL] nfp$check_implicit_access
    (    family_name: ost$family_name;
     VAR remote_access: boolean;
     VAR status: ost$status);

    VAR
      family_is_local: boolean,
      ignore_attributes: ^amt$get_attributes,
      ignore_contains_data: boolean,
      ignore_previously_opened: boolean,
      implicit_remote_file: boolean,
      ptf_library_exists: boolean;

  /check_implicit_access/
    BEGIN
      ptf_library_exists := FALSE;
      remote_access := FALSE;
      status.normal := TRUE;

      IF family_name = fsc$local THEN
        EXIT /check_implicit_access/;
      IFEND;

      nfp$verify_family (family_name, family_is_local, status);
      IF (NOT status.normal) OR family_is_local THEN
        EXIT /check_implicit_access/;
      IFEND;

      IF NOT jmp$system_job () THEN
        avp$get_capability (avc$implicit_remote_file, avc$user, implicit_remote_file, status);
        IF NOT status.normal THEN
          EXIT /check_implicit_access/;
        IFEND;
        IF NOT implicit_remote_file THEN

{ User not validated for implicit remote file access, treat as local access - family not found.

          EXIT /check_implicit_access/;
        IFEND;
      IFEND;

      PUSH ignore_attributes: [1..1];
      ignore_attributes^ [1].key := amc$null_attribute;
      amp$get_file_attributes (nfc$system_library_name, ignore_attributes^, ptf_library_exists,
            ignore_previously_opened, ignore_contains_data, status);
      IF NOT (ptf_library_exists AND status.normal) THEN
        EXIT /check_implicit_access/;
      IFEND;

      remote_access := TRUE;

    END /check_implicit_access/;

  PROCEND nfp$check_implicit_access;
?? TITLE := 'nfp$perform_implicit_access', EJECT ??

  PROCEDURE [XDCL] nfp$perform_implicit_access
    (    location: ost$name;
         local_file_path: fst$file_reference;
         remote_file_path: fst$file_reference;
         access_mode: nft$mode_of_access;
         command_name: ost$name_reference;
         pdt: ^clt$unbundled_pdt;
         pvt: ^clt$parameter_value_table;
         parameter_substitutions: ^clt$parameter_substitutions;
     VAR work_area {input, output} : ^clt$work_area;
     VAR status: ost$status);

    VAR
      access_mode_ptr: ^nft$mode_of_access,
      command_name_ptr: ^ost$name,
      file_name: clt$file,
      implicit_commands: ^clt$data_representation,
      local_file_ptr: ^amt$local_file_name,
      location_ptr: ^ost$name,
      object_lib_string: amt$local_file_name,
      object_library: ^amt$local_file_name,
      parameters_length: integer,
      program_attributes: ^pmt$program_attributes,
      program_description: ^pmt$program_description,
      program_parameters: ^pmt$program_parameters,
      remote_file_ptr: ^ost$string,
      request: clt$convert_to_string_request,
      task_id: pmt$task_id,
      task_status: pmt$task_status;

?? NEWTITLE := 'put_implicit_commands', EJECT ??

    PROCEDURE [INLINE] put_implicit_commands;

      VAR
        implicit_command: ^ost$string,
        implicit_command_count: ^nft$number_implicit_commands,
        representation: ^clt$data_representation,
        representation_line: ^clt$string_value,
        representation_line_count: ^clt$data_representation_count,
        representation_line_index: clt$data_representation_count,
        representation_line_size: ^clt$string_size;


      NEXT implicit_command_count IN work_area;

      IF implicit_commands = NIL THEN
        implicit_command_count^ := 0;

      ELSE
        representation := implicit_commands;
        RESET representation;
        NEXT representation_line_count IN representation;
        implicit_command_count^ := representation_line_count^;

        FOR representation_line_index := 1 TO representation_line_count^ DO
          NEXT representation_line_size IN representation;
          NEXT representation_line: [representation_line_size^] IN representation;

          NEXT implicit_command IN work_area;
          implicit_command^.size := representation_line_size^;
          implicit_command^.value := representation_line^;
        FOREND;
      IFEND;

    PROCEND put_implicit_commands;
?? OLDTITLE, EJECT ??

    status.normal := TRUE;

    IF command_name = '' THEN
      implicit_commands := NIL;
    ELSE
      request.initial_indentation := 0;
      request.continuation_indentation := 0;
      request.max_string := osc$max_string_size;
      request.include_advanced_items := TRUE;
      request.include_hidden_items := TRUE;
      request.kind := clc$convert_parameters;
      request.initial_text := ^command_name;
      request.include_secure_parameters := TRUE;
      request.evaluated_pdt := pdt;
      request.evaluated_pvt := pvt;
      request.parameter_substitutions := parameter_substitutions;
      clp$internal_convert_to_string (request, work_area, implicit_commands, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;
    IFEND;


  /execute_ptf_task/
    BEGIN
      PUSH program_description: [[REP (#SIZE (pmt$program_attributes) +
            (nfc$number_ptfi_libraries * #SIZE (object_lib_string))) OF cell]];
      RESET program_description;
      NEXT program_attributes IN program_description;

{
{     Build program description
{

      program_attributes^.contents := $pmt$prog_description_contents
            [pmc$library_list_specified, pmc$starting_proc_specified, pmc$term_error_level_specified,
            pmc$debug_mode_specified];
      program_attributes^.starting_procedure := nfc$ptfi_sp;
      program_attributes^.termination_error_level := pmc$fatal_load_errors;
      program_attributes^.debug_mode := pmc$debug_mode_off;
      program_attributes^.number_of_libraries := nfc$number_ptfi_libraries;
      NEXT object_library IN program_description;
      clp$convert_string_to_file (nfc$ptfi_library_name, file_name, status);
      IF NOT status.normal THEN
        EXIT /execute_ptf_task/;
      IFEND;
      object_library^ := file_name.local_file_name;

{
{     build parameters for task
{

      NEXT location_ptr IN work_area;
      location_ptr^ := location;
      NEXT local_file_ptr IN work_area;
      clp$convert_string_to_file (local_file_path, file_name, status);
      IF NOT status.normal THEN
        EXIT /execute_ptf_task/;
      IFEND;
      local_file_ptr^ := file_name.local_file_name;
      NEXT remote_file_ptr IN work_area;
      remote_file_ptr^.size := clp$trimmed_string_size (remote_file_path);
      IF remote_file_ptr^.size > osc$max_string_size THEN
        osp$set_status_abnormal ('CL', cle$file_reference_too_long, remote_file_path (1, osc$max_string_size),
              status);
        EXIT /execute_ptf_task/;
      IFEND;
      remote_file_ptr^.value := remote_file_path;
      NEXT access_mode_ptr IN work_area;
      access_mode_ptr^ := access_mode;
      NEXT command_name_ptr IN work_area;
      command_name_ptr^ := command_name;
      put_implicit_commands;

      parameters_length := i#current_sequence_position (work_area);
      RESET work_area TO location_ptr;
      parameters_length := parameters_length - i#current_sequence_position (work_area);
      NEXT program_parameters: [[REP parameters_length OF cell]] IN work_area;
      RESET program_parameters;

{
{     execute the task
{

      ?IF clc$compiling_for_test_harness THEN
        osp$set_status_abnormal ('CL', 999999, 'Remote file access not support in SCL test harness.', status);
      ?ELSE
        pmp$execute (program_description^, program_parameters^, osc$wait, task_id, task_status, status);
        IF status.normal AND (NOT task_status.status.normal) THEN
          status := task_status.status;
        IFEND;
      ?IFEND
    END /execute_ptf_task/;

  PROCEND nfp$perform_implicit_access;

MODEND nfm$remote_file_access;
