?? RIGHT := 110 ??
?? NEWTITLE := 'NOS/VE Accounting and Validation: Dual State Prompting' ??
MODULE avm$dual_state_prompt;
*copyc avc$compile_test_code
?? PUSH (LISTEXT := ON) ??
?? NEWTITLE := 'Global Declarations referenced by this module', EJECT ??
*copyc ave$condition_codes
*copyc cle$ecc_command_processing
*copyc oss$job_paged_literal
*copyc oss$task_private
*copyc ost$caller_identifier
*copyc ost$user_identification
?? POP ??
*copyc amp$get_next
*copyc amp$put_next
*copyc amp$return
*copyc avp$prevalidate_job
*copyc clp$convert_string_to_name
*copyc clp$trimmed_string_size
*copyc fsp$close_file
*copyc fsp$open_file
*copyc osp$format_message
*copyc osp$set_status_abnormal
*copyc pmp$get_unique_name
*copyc rmp$request_terminal
*copyc avv$account_name
*copyc avv$project_name
?? OLDTITLE ??
?? NEWTITLE := '[XDCL] avp$dual_state_prompt', EJECT ??
  PROCEDURE [XDCL] avp$dual_state_prompt
    (    user_name: ost$user_name;
         family_name: ost$family_name;
     VAR status: ost$status);

    VAR
      account_name: avt$account_name,
      project_name: avt$project_name,
      ignore_status: ost$status,
      input_file_id: amt$file_identifier,
      input_file_name: amt$local_file_name,
      prompt_file_id: amt$file_identifier,
      prompt_file_name: amt$local_file_name,
      validation_attributes: ^avt$validation_items;
?? NEWTITLE := 'read_account_project', EJECT ??
    PROCEDURE read_account_project
      (VAR account_name: avt$account_name;
       VAR project_name: avt$project_name;
       VAR status: ost$status);

      VAR
        account_valid: boolean,
        in_message: ost$string,
        ignore_status: ost$status,
        project_valid: boolean,
        prompt_message: ost$status,
        valid_name: clt$name;
?? NEWTITLE := 'put_prompt_get_reply', EJECT ??
      PROCEDURE put_prompt_get_reply
        (    prompt_message_status: ost$status;
             input_file_id: amt$file_identifier;
         VAR line: ost$string;
         VAR status: ost$status);

        VAR
          temp_line: ost$string;
?? NEWTITLE := 'get_parameter_input', EJECT ??
        PROCEDURE get_parameter_input
          (    input_file_id: amt$file_identifier;
           VAR line: ost$string;
           VAR status: ost$status);

          VAR
            continuation_line: ost$string,
            file_position: amt$file_position,
            ignore_byte_address: amt$file_byte_address,
            line_continued: boolean,
            transfer_count: amt$transfer_count;

          status.normal := TRUE;

          line.value := '  ';
          amp$get_next (input_file_id, ^line.value, osc$max_string_size, transfer_count,
                ignore_byte_address, file_position, status);
          IF NOT status.normal THEN
            RETURN;
          IFEND;

          line.size := clp$trimmed_string_size (line.value (1, transfer_count));

        PROCEND get_parameter_input;
?? OLDTITLE, EJECT ??
        status.normal := TRUE;
        line.size := 0;
        line.value := '';

        generate_output_message (prompt_message_status, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        get_parameter_input (input_file_id, temp_line, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        line.size := temp_line.size;

        WHILE (line.size > 0) AND (temp_line.value (temp_line.size - line.size + 1) = ' ') DO
          line.size := line.size - 1;
        WHILEND;

        IF line.size = 0 THEN
          RETURN;
        IFEND;

        line.value (1, line.size) := temp_line.value (temp_line.size - line.size + 1, line.size);

      PROCEND put_prompt_get_reply;
?? OLDTITLE, EJECT ??
      status.normal := TRUE;

      account_valid := FALSE;
      osp$set_status_abnormal ('AV', ave$enter_account, '', prompt_message);
      WHILE NOT account_valid DO
        account_name := osc$null_name;
        put_prompt_get_reply (prompt_message, input_file_id, in_message, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;
        IF in_message.size = 0 THEN
          account_valid := TRUE;
        ELSEIF in_message.size <= osc$max_name_size THEN
          clp$convert_string_to_name (in_message.value (1, in_message.size), valid_name, status);
          IF status.normal THEN
            account_name := valid_name.value;
            account_valid := TRUE;
          IFEND;
        IFEND;
        IF account_name = 'LOGOUT' THEN
          osp$set_status_abnormal ('AV', ave$user_requests_logout, '', status);
          generate_output_message (status, ignore_status);
          RETURN;
        IFEND;
      WHILEND;

      project_valid := FALSE;
      osp$set_status_abnormal ('AV', ave$enter_project, '', prompt_message);
      WHILE NOT project_valid DO
        project_name := osc$null_name;
        put_prompt_get_reply (prompt_message, input_file_id, in_message, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;
        IF in_message.size = 0 THEN
          project_valid := TRUE;
        ELSEIF in_message.size <= osc$max_name_size THEN
          clp$convert_string_to_name (in_message.value (1, in_message.size), valid_name, status);
          IF status.normal THEN
            project_name := valid_name.value;
            project_valid := TRUE;
          IFEND;
        IFEND;
        IF project_name = 'LOGOUT' THEN
          osp$set_status_abnormal ('AV', ave$user_requests_logout, '', status);
          generate_output_message (status, ignore_status);
          RETURN;
        IFEND;
      WHILEND;

    PROCEND read_account_project;
?? OLDTITLE ??
?? NEWTITLE := 'open_interactive_files', EJECT ??
      PROCEDURE open_interactive_files
        (VAR input_file_id: amt$file_identifier;
         VAR status: ost$status);

        VAR
          file_attachment: ^fst$attachment_options,
          ignore_status: ost$status,
          mandated_creation_attributes: ^fst$file_cycle_attributes,
          connection_attribute: array [1 .. 2] of ift$connection_attribute;

        status.normal := TRUE;

        pmp$get_unique_name (input_file_name, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        pmp$get_unique_name (prompt_file_name, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        connection_attribute [1].key := ifc$null_connection_attribute;
        connection_attribute [2].key := ifc$null_connection_attribute;
        rmp$request_terminal (prompt_file_name, NIL, connection_attribute, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        connection_attribute [1].key := ifc$prompt_file;
        connection_attribute [1].prompt_file := prompt_file_name;
        connection_attribute [2].key := ifc$prompt_string;
        connection_attribute [2].prompt_string.value := ', ';
        connection_attribute [2].prompt_string.size := 2;
        rmp$request_terminal (input_file_name, NIL, connection_attribute, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        PUSH file_attachment: [1 .. 1];
        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];
        file_attachment^[1].share_modes.selector := fsc$specific_share_modes;
        file_attachment^[1].share_modes.value := $fst$file_access_options [];
        PUSH mandated_creation_attributes: [1 .. 1];
        mandated_creation_attributes^[1].selector := fsc$file_contents_and_processor;
        mandated_creation_attributes^[1].file_contents := fsc$list;
        mandated_creation_attributes^[1].file_processor := fsc$unknown_processor;
        fsp$open_file (prompt_file_name, amc$record, file_attachment, {default_creation_attributes} NIL,
              mandated_creation_attributes, {attribute_validation} NIL, {attribute_override} NIL,
              prompt_file_id, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        PUSH file_attachment: [1 .. 1];
        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 [];
        fsp$open_file (input_file_name, amc$record, file_attachment, {default_creation_attributes} NIL,
              {mandated_creation_attributes} NIL, {attribute_validation} NIL, {attribute_override} NIL,
              input_file_id, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

      PROCEND open_interactive_files;
?? OLDTITLE ??
?? NEWTITLE := 'genereate_output_message', EJECT ??
      PROCEDURE generate_output_message
        (    message_status: ost$status;
         VAR status: ost$status);

    VAR
      current_byte_address: amt$file_byte_address,
      line_count: ^ost$status_message_line_count,
      message: ^ost$status_message,
      message_size: ^ost$status_message_line_size,
      message_text: ^string (*);

        PUSH message;
        RESET message;
        osp$format_message (message_status, osc$brief_message_level, osc$max_status_message_line, message^,
              status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;
        RESET message;
        NEXT line_count IN message;
        WHILE line_count^ > 0 DO
          NEXT message_size IN message;
          NEXT message_text: [message_size^] IN message;
          amp$put_next (prompt_file_id, message_text, message_size^, current_byte_address, status);
          IF NOT status.normal THEN
            RETURN;
          IFEND;
          line_count^ := line_count^ - 1;
        WHILEND;
      PROCEND generate_output_message;
?? OLDTITLE, EJECT ??
    status.normal := TRUE;

    open_interactive_files (input_file_id, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

  /dual_state_prompt/

    WHILE TRUE DO
      read_account_project (account_name, project_name, status);
      IF NOT status.normal THEN
        EXIT /dual_state_prompt/;
      IFEND;

      PUSH validation_attributes: [1 .. 1];
      validation_attributes^ [1].key := avc$account_project_key;
      validation_attributes^ [1].account_name := account_name;
      validation_attributes^ [1].project_name := project_name;

      avp$prevalidate_job (user_name, family_name, validation_attributes, NIL, status);
      IF status.normal THEN
        avv$account_name := account_name;
        avv$project_name := project_name;
        EXIT /dual_state_prompt/;
      ELSE
        IF (status.condition = ave$account_does_not_exist) OR
              (status.condition = ave$acct_member_does_not_exist) OR
              (status.condition = ave$project_does_not_exist) OR
              (status.condition = ave$member_does_not_exist) THEN
          generate_output_message (status, ignore_status);
          status.normal := TRUE;
          CYCLE /dual_state_prompt/;
        ELSE
          EXIT /dual_state_prompt/;
        IFEND;
      IFEND;

    WHILEND /dual_state_prompt/;

    IF status.normal THEN
      fsp$close_file (input_file_id, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;
      amp$return (input_file_name, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;
      fsp$close_file (prompt_file_id, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;
      amp$return (prompt_file_name, status);
      IF NOT status.normal THEN
        RETURN;
      IFEND;
    ELSE
      fsp$close_file (input_file_id, ignore_status);
      amp$return (input_file_name, ignore_status);
      fsp$close_file (prompt_file_id, ignore_status);
      amp$return (prompt_file_name, ignore_status);
    IFEND;

  PROCEND avp$dual_state_prompt;
?? OLDTITLE ??
MODEND avm$dual_state_prompt;
