
PROCEDURE {[INLINE]} bai$get_ansi_scw;

  VAR
    binary_length: clt$integer,
    os_status: ost$error;

  CONST
    ebcdic_zero = $CHAR(0F0(16));

/find_header/
  WHILE TRUE DO

    manually_advance_to_next_block := FALSE;

    start_new_block := (block_info^.block_position <> bac$middle_of_block) OR
            (rhl > block_info^.residual_block_length);

    get_data (file_identifier, operation, #LOC(rh), rhl,
      allow_direct_io_transfer, start_new_block, {convert_if_ebcdic =} FALSE, status);

{ If the record header was recorded in EBCDIC and character conversion is requested,
{ convert the record header to ASCII.  This code assumes that any value greater
{ than or equal to EBCDIC zero indicates an EBCDIC representation for the control word.

    IF (state_info^.character_set = amc$ebcdic) AND state_info^.character_conversion
          AND (rh.header_type >= ebcdic_zero) THEN
      osp$translate_bytes (#LOC(rh), rhl, #LOC(rh), rhl, ^osv$ebcdic_to_ascii, os_status);
    IFEND;

    start_new_block := FALSE;

    IF NOT status.normal THEN
      manually_advance_to_next_block := TRUE;
      exit_situation := TRUE;
      EXIT /find_header/;
    IFEND;

    IF gfi^.positioning_info.record_info.transfer_count < rhl THEN
      residual_data_length := 0;
      IF (tape_descriptor^.volume_position = amc$eov) OR
         (tape_descriptor^.volume_position = amc$after_tapemark) THEN
        no_header_read := TRUE;
        exit_situation := TRUE;
      ELSE
      { ! ERROR IN BLOCK
        manually_advance_to_next_block := TRUE;
        exit_situation := TRUE;
        amp$set_file_instance_abnormal (file_identifier, ame$improper_record_header,
          call_block.operation, ' ', status);
      IFEND;
      EXIT /find_header/;
    IFEND;

    IF (rh.header_type <= bac$end_segment) AND (rh.header_type >= bac$full_segment) THEN

      clp$convert_string_to_integer (rh.length, binary_length, status);
      IF NOT status.normal THEN
        manually_advance_to_next_block := TRUE;
        exit_situation := TRUE;
        amp$set_file_instance_abnormal (file_identifier, ame$improper_record_header,
          call_block.operation, ' ', status);
        EXIT /find_header/;
      IFEND;

      IF (binary_length.value <= UPPERVALUE (bat$scw_length_value_range)) AND
         (binary_length.value >= LOWERVALUE (bat$scw_length_value_range)) AND
         (binary_length.value - rhl <= block_info^.residual_block_length) THEN

        last_record_header_p := ^tape_descriptor^.get_tape_block_buffer^
          [block_info^.current_block_byte_address +1 -rhl ];
        residual_data_length := binary_length.value - rhl;

        IF binary_length.value = rhl THEN
          IF rh.header_type = bac$full_segment THEN
            zero_length_record := TRUE;
            exit_situation := TRUE;
          IFEND;
        IFEND;

        EXIT /find_header/;

      ELSE
        manually_advance_to_next_block := TRUE;
        exit_situation := TRUE;
        amp$set_file_instance_abnormal (file_identifier, ame$improper_record_header,
          call_block.operation, ' ', status);
        EXIT /find_header/;
      IFEND;

    ELSEIF rh.header_type = bac$ansi_block_padding_char THEN
      residual_data_length := 0;
      block_info^.current_block_byte_address :=
        block_info^.current_block_byte_address + block_info^.residual_block_length;
      block_info^.residual_block_length :=
        block_info^.residual_block_length - block_info^.residual_block_length;
      CYCLE /find_header/;

    ELSE
      manually_advance_to_next_block := TRUE;
      exit_situation := TRUE;
      amp$set_file_instance_abnormal (file_identifier, ame$improper_record_header,
        call_block.operation, ' ', status);
      EXIT /find_header/;
    IFEND;

  WHILEND /find_header/;

  gfi^.positioning_info.record_info.transfer_count := 0;

  IF manually_advance_to_next_block THEN
    residual_data_length := 0;
    block_info^.current_block_byte_address :=
      block_info^.current_block_byte_address + block_info^.residual_block_length;
    block_info^.residual_block_length :=
      block_info^.residual_block_length - block_info^.residual_block_length;
    gfi^.positioning_info.record_info.file_position := amc$mid_record;
  IFEND;

PROCEND bai$get_ansi_scw;

