?? LEFT := 1, RIGHT := 110 ??
MODULE nam$open_di_dump_file;
?? PUSH (LISTEXT := ON) ??
*copyc nac$network_management_catalog
*copyc nae$file_access_me_conditions
*copyc amt$file_attributes
*copyc amt$file_identifier
*copyc amt$local_file_name
*copyc amt$segment_pointer
*copyc mmt$attribute_keyword
*copyc nat$network_address
*copyc ost$status
*copyc pft$cycle_selector
*copyc pft$name
*copyc pft$password
*copyc pft$retention
?? POP ??
*copyc amp$get_segment_pointer
*copyc amp$return
*copyc fsp$close_file
*copyc fsp$open_file
*copyc mmp$set_access_selections
*copyc nap$get_catalog_file_count
*copyc osp$set_status_abnormal
*copyc pfp$convert_pf_path_to_fs_path
*copyc pfp$define
*copyc pfp$define_catalog
*copyc pmp$log

  CONST
    dump_catalog_label = 'SYSTEM_',
    dump_catalog_label_size = 7,
    dump_file_label = '    _',
    dump_file_label_size = 5;

?? TITLE := '[XDCL] nap$open_di_dump_file', EJECT ??

  PROCEDURE [XDCL] nap$open_di_dump_file (system: string (12);
        timestamp: string (12);
        dump_type: string (4);
        max_dumps: 0 .. 1000;
        max_dump_size: 0 .. 100000000;
    VAR dump_file_id: amt$file_identifier;
    VAR dump_data: ^SEQ ( * );
    VAR dump_file_opened: boolean;
    VAR status: ost$status);

{ PURPOSE: This procedure creates and opens a dump file in the network dump
{          catalog.
{
{ DESIGN:  The dump file is created as a segment access file and is accessed
{          as a sequence. The dump file name is created in the form
{          'xxxx_yymmddhhmmss.SYSTEM_ssssssssssss' where ssssssssssss is the device
{          interface's system address, yymmddhhmmss is the year, month, day,
{          hour, minute, and second at the start of the dump and xxxx is 'FULL'
{          or 'PART'.
{          It is the responsibility of the calling program to close and return
{          the dump file.

    VAR
      attachment_selections: [STATIC, READ] array [1 .. 3] of fst$attachment_option :=
            [[fsc$access_and_share_modes, [fsc$specific_access_modes, [fsc$append]], * ],
            [fsc$sequential_access, TRUE], [fsc$free_behind, TRUE]],
      dump_catalog: [STATIC] array [1 .. 4] of pft$name := [nac$management_family,
        nac$management_master_catalog, nac$cdcnet_subcatalog, nac$dump_catalog],
      dump_catalog_name: amt$local_file_name,
      dump_file: [STATIC] array [1 .. 6] of pft$name := [nac$management_family, nac$management_master_catalog,
        nac$cdcnet_subcatalog, nac$dump_catalog, * , * ],
      dump_file_name: amt$local_file_name,
      dump_file_pointer: amt$segment_pointer,
      files: 0 .. 7fffffff(16),
      fs_path: fst$path,
      fs_path_size: fst$path_size,
      highest_cycle: [STATIC, READ] pft$cycle_selector := [pfc$highest_cycle],
      local_status: ost$status,
      mandated_creation_attributes: array [1..1] of fst$file_cycle_attribute,
      password: [STATIC, READ] pft$password := ' ',
      retention_period: [STATIC, READ] pft$retention := pfc$maximum_retention,
      system_dump_catalog: [STATIC] array [1 .. 5] of pft$name := [nac$management_family,
        nac$management_master_catalog, nac$cdcnet_subcatalog, nac$dump_catalog, * ];

    status.normal := TRUE;
    dump_file_opened := FALSE;
    dump_catalog_name := dump_catalog_label;
    dump_catalog_name (dump_catalog_label_size + 1, * ) := system;
    dump_file_name := dump_file_label;
    dump_file_name (1, #SIZE (dump_type)) := dump_type;
    dump_file_name (dump_file_label_size + 1, * ) := timestamp;

    system_dump_catalog [UPPERBOUND (system_dump_catalog)] := dump_catalog_name;
    dump_file [UPPERBOUND (dump_file) - 1] := dump_catalog_name;
    dump_file [UPPERBOUND (dump_file)] := dump_file_name;
    dump_file_name (dump_file_label_size + 1 + #SIZE (timestamp), * ) := system;
    mandated_creation_attributes[1].selector := fsc$ring_attributes;
    mandated_creation_attributes[1].ring_attributes.r1 := 11;
    mandated_creation_attributes[1].ring_attributes.r2 := 11;
    mandated_creation_attributes[1].ring_attributes.r3 := 11;

    pfp$define_catalog (dump_catalog, status);
    IF NOT status.normal AND (status.condition <> pfe$name_already_subcatalog) THEN
      pmp$log ('pf define catalog failed - dump', local_status);
      RETURN;
    IFEND;
    status.normal := TRUE;
    pfp$define_catalog (system_dump_catalog, status);
    IF NOT status.normal AND (status.condition <> pfe$name_already_subcatalog) THEN
      pmp$log ('pf define catalog failed - system dump subcatalog', local_status);
      RETURN;
    IFEND;
    status.normal := TRUE;
    nap$get_catalog_file_count (system_dump_catalog, files, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;
    IF files >= max_dumps THEN
      pfp$convert_pf_path_to_fs_path (system_dump_catalog, fs_path, fs_path_size);
      osp$set_status_abnormal (nac$status_id, nae$max_files_reached, fs_path (1, fs_path_size), status);
      RETURN;
    IFEND;

    pfp$define (dump_file_name, dump_file, highest_cycle, password, retention_period, pfc$no_log, status);
    IF NOT status.normal THEN
      pmp$log ('pf define failed - dump', local_status);
      RETURN;
    IFEND;

    fsp$open_file (dump_file_name, amc$segment, ^attachment_selections, {default_creation_attributes =} NIL,
          ^mandated_creation_attributes, {attribute_validation =} NIL, {attribute_override =} NIL,
          dump_file_id, status);
    IF NOT status.normal THEN
      pmp$log ('open failed - dump', local_status);
      amp$return (dump_file_name, local_status);
      RETURN;
    IFEND;
    amp$get_segment_pointer (dump_file_id, amc$sequence_pointer, dump_file_pointer, status);
    IF NOT status.normal THEN
      pmp$log ('segment access failed - dump', local_status);
      fsp$close_file (dump_file_id, local_status);
      amp$return (dump_file_name, local_status);
      RETURN;
    IFEND;
    RESET dump_file_pointer.sequence_pointer;
    NEXT dump_data: [[REP max_dump_size OF cell]] IN dump_file_pointer.sequence_pointer;
    mmp$set_access_selections (dump_data, mmc$as_sequential, status);
    IF NOT status.normal THEN
      pmp$log ('sequential access failed - dump', local_status);
      fsp$close_file (dump_file_id, local_status);
      amp$return (dump_file_name, local_status);
      RETURN;
    IFEND;
    dump_file_opened := TRUE;
  PROCEND nap$open_di_dump_file;
MODEND nam$open_di_dump_file;
