?? RIGHT := 110 ??
?? NEWTITLE := 'NOS/VE SCL Interpreter : Output routines for System Files' ??
MODULE clm$output_to_system_files;

{
{ PURPOSE:
{   This module contains procedures that write a line to a "system file".
{   A "system file" is one that, for certain operations, is (implicitly) opened the first time it is used
{   in a task, has that instance of open used for subsequent accesses in that task, and is closed as part
{   of task termination.
{

?? NEWTITLE := 'Global Declarations', EJECT ??
?? PUSH (LISTEXT := ON) ??
*copyc clc$standard_file_names
*copyc clc$system_messages_module
*copyc clt$interpreter_modes
*copyc cyt$string_size
*copyc fst$file_reference
*copyc fst$path_element_size
*copyc oss$job_paged_literal
*copyc ost$name_reference
*copyc ost$status
?? POP ??
*copyc amp$put_next
*copyc clp$fetch_system_file_id
*copyc clp$store_system_file_id
*copyc clp$trimmed_string_size
*IF NOT $true(osv$unix)
*copyc clv$system_messages_module
*ELSE
*copyc clp$get_screen_mode
*copyc osp_screen_output
*copyc csp$output_window
*copyc clv$standard_files
*IFEND
*copyc fsp$open_file
*IF NOT $true(osv$unix)
*copyc osp$find_param_assist_prompt
*copyc osp$find_help_module
*copyc osp$format_help_message
*copyc osp$generate_log_message
*IFEND
*copyc osp$get_status_condition_name
*copyc osp$get_status_condition_string
*copyc osv$lower_to_upper
*IF NOT $true(osv$unix)
*copyc pmp$log
*IFEND
?? TITLE := 'clp$get_system_file_id', EJECT ??

  PROCEDURE [XDCL, #GATE] clp$get_system_file_id
    (    file_name: amt$local_file_name;
     VAR file_id: amt$file_identifier;
     VAR status: ost$status);

*IF NOT $true(osv$unix)
    CONST
      path_prefix = ':$LOCAL.',
      path_prefix_size = 8,
      path_cycle = '.1',
      path_cycle_size = 2,
      path_size = path_prefix_size + osc$max_name_size + path_cycle_size;
*IFEND

    VAR
      file_attachment: [STATIC, READ, oss$job_paged_literal] array [1 .. 4] of fst$attachment_option := [
            {} [fsc$create_file, TRUE],
            {} [fsc$open_position, amc$open_no_positioning],
            {} [fsc$access_and_share_modes, {} [fsc$specific_access_modes, [fsc$append, fsc$shorten]],
            {} [fsc$specific_share_modes, []]],
            {} [fsc$open_share_modes, [fsc$read, fsc$shorten, fsc$append, fsc$modify, fsc$execute]]],
      file_id_defined: boolean;

    VAR
*IF NOT $true(osv$unix)
      file: string (path_size),
*IFEND
      file_name_size: fst$path_element_size;


    status.normal := TRUE;
    clp$fetch_system_file_id (file_name, file_id, file_id_defined);
    IF NOT file_id_defined THEN
*IF NOT $true(osv$unix)
      file := ' ';
      file_name_size := clp$trimmed_string_size (file_name);
      file (1, path_prefix_size) := path_prefix;
      file (path_prefix_size + 1, file_name_size) := file_name;
      file (path_prefix_size + 1 + file_name_size, path_cycle_size) := path_cycle;
      fsp$open_file (file, amc$record, ^file_attachment, NIL, NIL, NIL, NIL, file_id, status);
*ELSE
      fsp$open_file (file_name, amc$record, ^file_attachment,
            file_id, status);
*IFEND
      IF NOT status.normal THEN
        RETURN;
      IFEND;
      clp$store_system_file_id (file_name, file_id);
    IFEND;

  PROCEND clp$get_system_file_id;
*IF NOT $true(osv$unix)
?? TITLE := 'clp$put_echoed_commands', EJECT ??

  PROCEDURE [INLINE] clp$put_echoed_commands
    (    text: ^string ( * );
     VAR status: ost$status);

    VAR
      file_id: amt$file_identifier,
      ignore_byte_address: amt$file_byte_address,
      ignore_status: ^ost$status;


    clp$get_system_file_id (clc$echoed_commands, file_id, status);
    IF status.normal THEN
      amp$put_next (file_id, text, clp$trimmed_string_size (text^), ignore_byte_address, status);
    IFEND;

    IF NOT status.normal THEN
      PUSH ignore_status;
      pmp$log (' --  Attempt to write the following line to $ECHO failed:', ignore_status^);
      pmp$log (text^, ignore_status^);
      pmp$log (' --  The reason for the failure was:', ignore_status^);
      osp$generate_log_message ($pmt$ascii_logset [pmc$job_log], status, ignore_status^);
      status.normal := TRUE;
    IFEND;

  PROCEND clp$put_echoed_commands;
?? TITLE := 'clp$echo_command', EJECT ??

  PROCEDURE [XDCL] clp$echo_command
    (    interpreter_mode: clt$interpreter_modes;
         command: string ( * );
     VAR status: ost$status);

    CONST
      echo_header_size = 4;

    VAR
      line: ^string ( * ),
      line_size: cyt$string_size;


    status.normal := TRUE;

    IF (echo_header_size + STRLENGTH (command)) > cyc$max_string_size THEN
      line_size := cyc$max_string_size;
    ELSE
      line_size := echo_header_size + STRLENGTH (command);
    IFEND;
    PUSH line: [line_size];
    IF interpreter_mode = clc$interpret_mode THEN
      line^ (1, echo_header_size) := ' CI ' {Command Interpret} ;
    ELSE
      line^ (1, echo_header_size) := ' CS ' {Command Skip} ;
    IFEND;
    line^ (echo_header_size + 1, * ) := command;

    clp$put_echoed_commands (line, status);

  PROCEND clp$echo_command;
?? TITLE := 'clp$echo_trace_information', EJECT ??

  PROCEDURE [XDCL] clp$echo_trace_information
    (    message_name: ost$name_reference;
         identifying_name: ^ost$name_reference;
         file_reference: ^fst$file_reference;
         message_status: ^ost$status;
     VAR status: ost$status);

    VAR
      ignore_natural_language: ost$natural_language,
      ignore_online_manual_name: ost$online_manual_name,
      message: ^ost$status_message,
      message_line_count: ^ost$status_message_line_count,
      message_parameters: array [1 .. 3] of ^ost$message_parameter,
      message_status_name: ost$status_condition_name,
      message_status_string: ^ost$string,
      message_template: ^ost$message_template,
      message_text: ^ost$status_message_line,
      message_text_size: ^ost$status_message_line_size,
      no_identifying_name: [STATIC, READ, oss$job_paged_literal] string (4) := 'none',
      normal_status: [STATIC, READ, oss$job_paged_literal] string (6) := 'normal',
      system_messages_module: ^ost$help_module,
      translated_message_name: ost$name;


    osp$find_help_module (clc$system_messages_module, system_messages_module, ignore_online_manual_name,
          ignore_natural_language, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    IF system_messages_module = NIL THEN
      IF clv$system_messages_module = NIL THEN
        RETURN;
      IFEND;
      system_messages_module := clv$system_messages_module;
    IFEND;

    #TRANSLATE (osv$lower_to_upper, message_name, translated_message_name);
    osp$find_param_assist_prompt (system_messages_module, translated_message_name, message_template, status);
    IF (NOT status.normal) OR (message_template = NIL) THEN
      RETURN;
    IFEND;

    IF (identifying_name = NIL) OR (identifying_name^ = '') THEN
      message_parameters [1] := ^no_identifying_name;
    ELSE
      message_parameters [1] := identifying_name;
    IFEND;
    message_parameters [2] := file_reference;
    IF (message_status = NIL) OR message_status^.normal THEN
      message_parameters [3] := ^normal_status;
    ELSE
      osp$get_status_condition_name (message_status^.condition, message_status_name, status);
      IF status.normal AND (message_status_name <> 'UNKNOWN_CONDITION') THEN
        message_parameters [3] := ^message_status_name;
      ELSE
        PUSH message_status_string;
        message_status_string^.size := 0;
        osp$get_status_condition_string (message_status^.condition, message_status_string^, status);
        status.normal := TRUE;
        message_parameters [3] := ^message_status_string^.value (1, message_status_string^.size);
      IFEND;
    IFEND;

      PUSH message;

    osp$format_help_message (message_template, ^message_parameters, osc$max_status_message_line, message^,
          status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    RESET message;
    NEXT message_line_count IN message;
    NEXT message_text_size IN message;
    NEXT message_text: [message_text_size^] IN message;

    clp$put_echoed_commands (message_text, status);

  PROCEND clp$echo_trace_information;
*IFEND
?? TITLE := 'clp$put_error_output', EJECT ??

  PROCEDURE [XDCL, #GATE] clp$put_error_output
    (    text: string ( * );
     VAR status: ost$status);

    VAR
      file_id: amt$file_identifier,
      ignore_byte_address: amt$file_byte_address;


    clp$get_system_file_id (clc$error_output, file_id, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    amp$put_next (file_id, ^text, STRLENGTH (text), ignore_byte_address, status);

  PROCEND clp$put_error_output;
?? TITLE := 'clp$put_job_output', EJECT ??
*copy clh$put_job_output

  PROCEDURE [XDCL, #GATE] clp$put_job_output
    (    text: string ( * );
     VAR status: ost$status);

    VAR
      file_id: amt$file_identifier,
*IF $true(osv$unix)
      attachment_option: array [1 .. 1] of fst$attachment_option,
      in_screen_mode: boolean,
      length: ost_c_integer,
      screen_output: ost_c_fixed_string,
*IFEND
      ignore_byte_address: amt$file_byte_address;


*IF $true(osv$unix)
    clp$get_screen_mode (in_screen_mode);
    IF in_screen_mode THEN
      attachment_option [1].selector := fsc$access_and_share_modes;
      attachment_option [1].access_modes.selector := fsc$specific_access_modes;
      attachment_option [1].access_modes.value := $fst$file_access_options [fsc$append, fsc$shorten];
      osp_screen_output (screen_output, length);
      IF length < 1 THEN
{???
        RETURN;
      IFEND;
      fsp$open_file (screen_output(1, length), amc$record, ^attachment_option, file_id, status);
    ELSE
*IFEND
    clp$get_system_file_id (clc$job_output, file_id, status);
*IF $true(osv$unix)
    IFEND;
*IFEND
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    amp$put_next (file_id, ^text, STRLENGTH (text), ignore_byte_address, status);
*IF $true(osv$unix)
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    IF in_screen_mode THEN
      csp_output_window;
    IFEND;
*IFEND
  PROCEND clp$put_job_output;
?? TITLE := 'clp$put_job_command_response', EJECT ??

  PROCEDURE [XDCL, #GATE] clp$put_job_command_response
    (    text: string ( * );
     VAR status: ost$status);

    VAR
      file_id: amt$file_identifier,
*IF $true(osv$unix)
      attachment_option: array [1 .. 1] of fst$attachment_option,
      in_screen_mode: boolean,
      length: ost_c_integer,
      screen_output: ost_c_fixed_string,
*IFEND
      ignore_byte_address: amt$file_byte_address;


*IF NOT $true(osv$unix)
    clp$get_system_file_id (clc$job_command_response, file_id, status);
*ELSE
    clp$get_screen_mode (in_screen_mode);
    IF in_screen_mode THEN
      attachment_option [1].selector := fsc$access_and_share_modes;
      attachment_option [1].access_modes.selector := fsc$specific_access_modes;
      attachment_option [1].access_modes.value := $fst$file_access_options [fsc$append, fsc$shorten];
      osp_screen_output (screen_output, length);
      IF length < 1 THEN
{???
        RETURN;
      IFEND;
      fsp$open_file (screen_output(1, length), amc$record, ^attachment_option, file_id, status);
    ELSE
    clp$get_system_file_id (clc$job_output, file_id, status);
    IFEND;
*IFEND
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    amp$put_next (file_id, ^text, STRLENGTH (text), ignore_byte_address, status);
*IF $true(osv$unix)
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    IF in_screen_mode THEN
      csp_output_window;
    IFEND;
*IFEND

  PROCEND clp$put_job_command_response;

MODEND clm$output_to_system_files;
