*copyc OSD$DEFAULT_PRAGMATS
MODULE iim$st_init_open_desc_atributes;

{ PURPOSE:  This module initializes the attributes field of an open
{           file descriptor.  Its procedure is called only by
{           IIP$ST_OPEN.
{
{  DESIGN:  The connection attribute values used to initialize the
{           open file descriptor are obtained from REQUEST_TERMINAL,
{           CHANGE_TERM_CONN_ATTRIBUTES, CHANGE_TERM_CONN_DEFAULT
{           command interface requests, and RMP$REQUEST_TERMINAL,
{           IFP$CHANGE_TERM_CONN_ATTRIBUTES, IFP$CHANGE_TERM_CONN_
{           DEFAULTS program interface requests, upline attribute
{           changes (i.e., attributes changed by TIP commands), and
{           the default connection attributes established at LOGIN.
{           An attribute hierarchy is established here:  LOGIN values,
{           CHATCD/IFP$CHATCD, REQT/RMP$REQT/CHATCA/IFP$CHATCA, and,
{           finally, upline attribute changes, in order of increasing
{           priority.
{
?? TITLE := 'MODULE iim$st_init_open_desc_atributes' ??

?? PUSH (LISTEXT := ON) ??
*copyc fmp$get_terminal_attributes
*copyc clp$get_ultimate_connection
*copyc iip$clear_lock
*copyc iip$search_connection_desc
*copyc iip$set_lock
*copyc iip$vt_get_attr_ch_indications
*copyc iip$vt_to_terminal_attributes
*copyc iip$xlate_local_file_to_session
*copyc iiv$connection_desc_ptr
*copyc iiv$interactive_terminated
*copyc oss$task_private
*copyc ost$status
?? POP ??

?? NEWTITLE := 'PROCEDURE iip$st_init_open_desc_atributes', EJECT ??

  PROCEDURE [XDCL] iip$st_init_open_desc_atributes (file_name: ost$name;
        open_file_desc_pointer: ^iit$st_open_file_description;
    VAR status: ost$status);

    VAR
      connection_desc_ptr: ^iit$connection_description,
      dummy: iit$vt_attribute_kind,
      i: integer,
      if_attributes: ^ift$terminal_attributes,
      j: ift$connection_attribute_keys,
      lnt_attributes: array [1 .. (ORD (ifc$max_connection_key) + 1)] OF
        ift$get_connection_attribute,
      number_of_attributes: 0 .. iic$vt_max_number_of_attributes,
      session_file: amt$local_file_name,
      ultimate_name: amt$local_file_name,
      valid_name: boolean,
      vt_attributes: ^iit$vt_attributes,
      vt_indications: ^iit$vt_attributes,
      validated_name: ost$name;

    status.normal := TRUE;
    lnt_attributes := iiv$all_get_term_attributes;

    clp$get_ultimate_connection (file_name, ultimate_name, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;
    iip$xlate_local_file_to_session (ultimate_name, session_file, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;
    iip$search_connection_desc (session_file, connection_desc_ptr);
    IF connection_desc_ptr = NIL THEN
      RETURN;
    IFEND;

  { Initialize the open file descriptor's attributes to the connection's defaults.

    open_file_desc_pointer^.attributes := iiv$deflt_connection_attributes;

  { Get the connection attributes from the Local Name Table entry for the file
  { and use them to update the open file descriptor attributes.

    fmp$get_terminal_attributes (ultimate_name, lnt_attributes, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;
    FOR i := 1 TO (ORD (ifc$max_connection_key) + 1) DO
      CASE lnt_attributes [i].key OF
      = ifc$attention_character_action =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.attention_character_action.value :=
                lnt_attributes [i].attention_character_action;
          open_file_desc_pointer^.attributes.attention_character_action.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$break_key_action =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.break_key_action.value :=
                lnt_attributes [i].break_key_action;
          open_file_desc_pointer^.attributes.break_key_action.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$end_of_information =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.end_of_information.value :=
                lnt_attributes [i].end_of_information;
          open_file_desc_pointer^.attributes.end_of_information.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$input_block_size =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.input_block_size.value :=
                lnt_attributes [i].input_block_size;
          open_file_desc_pointer^.attributes.input_block_size.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$input_editing_mode =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.input_editing_mode.value :=
                lnt_attributes [i].input_editing_mode;
          open_file_desc_pointer^.attributes.input_editing_mode.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$input_output_mode =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.input_output_mode.value :=
                lnt_attributes [i].input_output_mode;
          open_file_desc_pointer^.attributes.input_output_mode.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$input_timeout =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.input_timeout.value :=
                lnt_attributes [i].input_timeout;
          open_file_desc_pointer^.attributes.input_timeout.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$input_timeout_length =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.input_timeout_length.value :=
                lnt_attributes [i].input_timeout_length;
          open_file_desc_pointer^.attributes.input_timeout_length.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$input_timeout_purge =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.input_timeout_purge.value :=
                lnt_attributes [i].input_timeout_purge;
          open_file_desc_pointer^.attributes.input_timeout_purge.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$partial_char_forwarding =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.partial_char_forwarding.value :=
                lnt_attributes [i].partial_character_forwarding;
          open_file_desc_pointer^.attributes.partial_char_forwarding.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$prompt_file =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.prompt_file.value :=
                lnt_attributes [i].prompt_file;
          open_file_desc_pointer^.attributes.prompt_file.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$prompt_file_identifier =
        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.prompt_file_identifier.value :=
                lnt_attributes [i].prompt_file_identifier;
          open_file_desc_pointer^.attributes.prompt_file_identifier.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$prompt_string =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.prompt_string.value :=
                lnt_attributes [i].prompt_string;
          open_file_desc_pointer^.attributes.prompt_string.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$store_backspace_character =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.store_backspace_character.value :=
                lnt_attributes [i].store_backspace_character;
          open_file_desc_pointer^.attributes.store_backspace_character.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$store_nuls_dels =
        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.store_nuls_dels.value :=
                lnt_attributes [i].store_nuls_dels;
          open_file_desc_pointer^.attributes.store_nuls_dels.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$trans_character_mode =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.trans_character_mode.value :=
                lnt_attributes [i].trans_character_mode;
          open_file_desc_pointer^.attributes.trans_character_mode.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$trans_forward_character =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.trans_forward_character.value :=
                lnt_attributes [i].trans_forward_character;
          open_file_desc_pointer^.attributes.trans_forward_character.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$trans_length_mode =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.trans_length_mode.value :=
                lnt_attributes [i].trans_length_mode;
          open_file_desc_pointer^.attributes.trans_length_mode.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$trans_timeout_mode =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.trans_timeout_mode.value :=
                lnt_attributes [i].trans_timeout_mode;
          open_file_desc_pointer^.attributes.trans_timeout_mode.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$trans_message_length =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.trans_message_length.value :=
                lnt_attributes [i].trans_message_length;
          open_file_desc_pointer^.attributes.trans_message_length.source :=
                lnt_attributes [i].source;
        IFEND;

      = ifc$trans_terminate_character =

        IF lnt_attributes [i].source <> ifc$undefined_attribute THEN
          open_file_desc_pointer^.attributes.trans_terminate_character.value :=
                lnt_attributes [i].trans_terminate_character;
          open_file_desc_pointer^.attributes.trans_terminate_character.source :=
                lnt_attributes [i].source;
        IFEND;

      ELSE
      CASEND;
    FOREND;

  { Update BAM's page width and page length attributes for the file if they have been
  { changed from the terminal end with a TIP command.

    PUSH vt_attributes: [1 .. (ORD (UPPERVALUE (dummy))-
          ORD (LOWERVALUE (dummy))+ 1)];
    iip$vt_get_attr_ch_indications (connection_desc_ptr^.vtp_connection_id,
          vt_attributes^, number_of_attributes, status);
    IF status.normal AND (number_of_attributes <> 0) THEN
      PUSH vt_indications: [1 .. number_of_attributes];
      PUSH if_attributes: [1 .. number_of_attributes];
      FOR i := 1 TO number_of_attributes DO
        vt_indications^ [i] := vt_attributes^ [i];
      FOREND;
      iip$vt_to_terminal_attributes (vt_indications^, if_attributes^);
      iip$set_lock (connection_desc_ptr^.terminal_attributes_lock, osc$wait, status);
      IF status.normal THEN
        FOR i := 1 TO number_of_attributes DO
          CASE if_attributes^ [i].key OF
          = ifc$page_length =
            connection_desc_ptr^.page_length := if_attributes^ [i].page_length;
          = ifc$page_width =
            connection_desc_ptr^.page_width := if_attributes^ [i].page_width;
          = ifc$terminal_model =
            connection_desc_ptr^.terminal_model := if_attributes^ [i].terminal_model;
          ELSE
          CASEND;
        FOREND;
        iip$clear_lock (connection_desc_ptr^.terminal_attributes_lock, status);

      IFEND; { status.normal from iip$set_lock }
    IFEND; { status.normal from iip$vt_get_attr_ch_indications AND (number_of_attributes <> 0) }

  PROCEND iip$st_init_open_desc_atributes;
MODEND iim$st_init_open_desc_atributes;
