MODULE ram$append_string_to_file;

*copyc amp$get_next
*copyc amp$put_next
*copyc clp$evaluate_parameters
*copyc clp$trimmed_string_size
*copyc fsp$close_file
*copyc fsp$open_file

  PROGRAM rap$append_string_to_file
    (    parameter_list: clt$parameter_list;
     VAR status: ost$status);


{ PROCEDURE (ram$appstf) append_pdt (
{   append_string, as: string = $required
{   input, i: file = $required
{   output, o: file = $required
{   status)

?? PUSH (LISTEXT := ON) ??

  VAR
    pdt: [STATIC, READ, cls$declaration_section] record
      header: clt$pdt_header,
      names: array [1 .. 7] of clt$pdt_parameter_name,
      parameters: array [1 .. 4] of clt$pdt_parameter,
      type1: record
        header: clt$type_specification_header,
        qualifier: clt$string_type_qualifier,
      recend,
      type2: record
        header: clt$type_specification_header,
      recend,
      type3: record
        header: clt$type_specification_header,
      recend,
      type4: record
        header: clt$type_specification_header,
      recend,
    recend := [
    [1,
    [88, 5, 19, 18, 36, 15, 201],
    clc$command, 7, 4, 3, 0, 0, 0, 4, 'RAM$APPSTF'], [
    ['APPEND_STRING                  ',clc$nominal_entry, 1],
    ['AS                             ',clc$abbreviation_entry, 1],
    ['I                              ',clc$abbreviation_entry, 2],
    ['INPUT                          ',clc$nominal_entry, 2],
    ['O                              ',clc$abbreviation_entry, 3],
    ['OUTPUT                         ',clc$nominal_entry, 3],
    ['STATUS                         ',clc$nominal_entry, 4]],
    [
{ PARAMETER 1
    [1, clc$normal_usage_entry, clc$non_secure_parameter,
    $clt$parameter_spec_methods[clc$specify_by_name, clc$specify_positionally],
    clc$pass_by_value, clc$immediate_evaluation,
  clc$standard_parameter_checking, 8, clc$required_parameter, 0, 0],
{ PARAMETER 2
    [4, clc$normal_usage_entry, clc$non_secure_parameter,
    $clt$parameter_spec_methods[clc$specify_by_name, clc$specify_positionally],
    clc$pass_by_value, clc$immediate_evaluation,
  clc$standard_parameter_checking, 3, clc$required_parameter, 0, 0],
{ PARAMETER 3
    [6, clc$normal_usage_entry, clc$non_secure_parameter,
    $clt$parameter_spec_methods[clc$specify_by_name, clc$specify_positionally],
    clc$pass_by_value, clc$immediate_evaluation,
  clc$standard_parameter_checking, 3, clc$required_parameter, 0, 0],
{ PARAMETER 4
    [7, clc$normal_usage_entry, clc$non_secure_parameter,
    $clt$parameter_spec_methods[clc$specify_by_name],
    clc$pass_by_reference, clc$immediate_evaluation,
  clc$standard_parameter_checking, 3, clc$optional_parameter, 0, 0]],
{ PARAMETER 1
    [[1, 0, clc$string_type], [0, clc$max_string_size, FALSE]],
{ PARAMETER 2
    [[1, 0, clc$file_type]],
{ PARAMETER 3
    [[1, 0, clc$file_type]],
{ PARAMETER 4
    [[1, 0, clc$status_type]]];

?? POP ??

  CONST
    p$append_string = 1,
    p$input = 2,
    p$output = 3,
    p$status = 4;

  VAR
    pvt: array [1 .. 4] of clt$parameter_value;

    TYPE
      work_files = (input_file, output_file);

    VAR
      append_string: ost$string,
      file: work_files,
      file_identifier: array [work_files] of amt$file_identifier,
      ignore_byte_address: amt$file_byte_address,
      input_line: ost$string,
      local_status: ost$status,
      output_line: ost$string,
      position_of_file: amt$file_position,
      transfer_count: amt$transfer_count;

    PROCEDURE open_files
      (VAR local_status: ost$status);

      VAR
        file_attachment: array [1 .. 3] of fst$attachment_option;


      file_attachment [1].selector := fsc$access_and_share_modes;
      file_attachment [1].access_modes.selector := fsc$specific_access_modes;
      file_attachment [1].access_modes.value :=
            $fst$file_access_options [fsc$read];
      file_attachment [1].share_modes.selector := fsc$specific_share_modes;
      file_attachment [1].share_modes.value :=
            $fst$file_access_options [fsc$read, fsc$execute];
      file_attachment [2].selector := fsc$open_share_modes;
      file_attachment [2].open_share_modes :=
            $fst$file_access_options [fsc$read, fsc$execute];
      file_attachment [3].selector := fsc$create_file;
      file_attachment [3].create_file := FALSE;

      fsp$open_file (pvt [p$input].value^.file_value^, amc$record,
            ^file_attachment, NIL, NIL, NIL, NIL, file_identifier [input_file],
            local_status);
      IF NOT local_status.normal THEN
        RETURN;
      IFEND;

      file_attachment [1].selector := fsc$access_and_share_modes;
      file_attachment [1].access_modes.selector := fsc$specific_access_modes;
      file_attachment [1].access_modes.value :=
            $fst$file_access_options [fsc$append, fsc$shorten];
      file_attachment [1].share_modes.selector := fsc$specific_share_modes;
      file_attachment [1].share_modes.value := $fst$file_access_options [];
      file_attachment [2].selector := fsc$access_and_share_modes;
      file_attachment [2].access_modes.selector := fsc$specific_access_modes;
      file_attachment [2].access_modes.value :=
            $fst$file_access_options [fsc$append];
      file_attachment [2].share_modes.selector := fsc$specific_share_modes;
      file_attachment [2].share_modes.value := $fst$file_access_options [];
      file_attachment [3].selector := fsc$open_share_modes;
      file_attachment [3].open_share_modes := -$fst$file_access_options [];

      fsp$open_file (pvt [p$output].value^.file_value^, amc$record,
            ^file_attachment, NIL, NIL, NIL, NIL,
            file_identifier [output_file], local_status);
      IF NOT local_status.normal THEN
        fsp$close_file (file_identifier [input_file], local_status);
        RETURN;
      IFEND;
    PROCEND open_files;

    clp$evaluate_parameters (parameter_list, #SEQ (pdt), NIL, ^pvt, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    append_string.value := pvt [p$append_string].value^.string_value^;
    append_string.size := clp$trimmed_string_size (append_string.value);

    open_files (local_status);
    IF NOT local_status.normal THEN
      status := local_status;
      RETURN;
    IFEND;

  /process_input_file/
    WHILE TRUE DO

      amp$get_next (file_identifier [input_file], ^input_line.value,
            osc$max_string_size, transfer_count, ignore_byte_address,
            position_of_file, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;
      IF position_of_file = amc$eoi THEN
        EXIT /process_input_file/;
      IFEND;
      input_line.size := transfer_count;
      output_line.value (1, input_line.size) :=
            input_line.value (1, input_line.size);
      output_line.value (input_line.size + 1,
            append_string.size) := append_string.value;
      output_line.size := input_line.size + append_string.size;
      amp$put_next (file_identifier [output_file],
            ^output_line.value (1, output_line.size), output_line.size,
            ignore_byte_address, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;

    WHILEND /process_input_file/;

    FOR file := LOWERVALUE (file) TO UPPERVALUE (file) DO
      fsp$close_file (file_identifier [file], local_status);
      IF (NOT local_status.normal) AND status.normal THEN
        status := local_status;
      IFEND;
    FOREND;
  PROCEND rap$append_string_to_file;
MODEND ram$append_string_to_file;
