-- Em3270CharTransPack.mesa:
-- Copyright (C) 1983, 1984 by Xerox Corporation

-- Last revised for Star 3.3L by Caro:	  6-Jul-84 11:02:47            
-- Owner:  Terminal Emulation.

-- Overview: 
--   Em3270CharTransPack provides the necessary procedures to convert EBCDIC to/from OIS-Character and procedures which handles deadkey accent(Star Document to/from a 3270 window). 
--   Since Em3270CharTransPack gets its translations tables from a system data file, it can be used in any country without having to modify any existing code.
  

DIRECTORY 
  CharDefs USING [Char, Code, Chset, Roman],
  Em3270Defs USING [IBMlanguages],
  Em3270CharTransDefs,
  Em3270PrivDefs,
  File USING [firstPageNumber],
  Inline USING [LowHalf],
  NSFile USING [allControlSelections, ChangeControls, Delete, Error, fullAccess, Handle],
  NSSegment USING [GetSizeInPages, Map, Origin],
  Space USING [Interval, Unmap],
  StandardDefs USING [Bv, Ct, Sv],
  StarControlDefs USING [GetSystemFile, sessionSystem];
  
  
Em3270CharTransPack: PROGRAM
  IMPORTS CharDefs, Inline, NSFile, NSSegment, Space, StarControlDefs 
  
  EXPORTS Em3270CharTransDefs, Em3270PrivDefs = 

  BEGIN OPEN CharDefs, Em3270PrivDefs, StandardDefs, StarControlDefs;
  -- =======================
  --     Types
  -- =======================
  Aq3270TransFile: PUBLIC TYPE = MACHINE DEPENDENT RECORD [
    ctHostAccents: CARDINAL,     -- the number of Accent Character.
    charFromEBCDIC: CharFromEBCDIC,      -- array containing EBCDIC to OIS character.
    chSetIndex: ChSetIndexType,          -- array containing relative pointers indexed by ChSet. Set to LAST[CARDINAL] if the ChSet is never referenced.
    hostAccentPart: HostAccentPart,
    chCodeIndex: ARRAY[0..0) OF ChCodeArray];
    
  CharFromEBCDIC: TYPE =  ARRAY CHARACTER OF CharDefs.Char;
  
  BaseOISToEFile: TYPE = LONG BASE POINTER TO Aq3270TransFile;    -- base pointer to a file which contains the OIS to EBCDIC translations
  
  ChSetIndexType: TYPE = ARRAY CharDefs.Chset OF RelIndex;    -- set to LAST[CARDINAL] if the Chset is not used in that particular country;
  RelIndex: TYPE = BaseOISToEFile RELATIVE POINTER [0..LAST[CARDINAL]] TO ChCodeArray;
  ChCodeArray: TYPE = ARRAY CharDefs.Code OF CHARACTER;
  
  HostAccentPart: TYPE = MACHINE DEPENDENT RECORD [
    accentArray: AccentArray ];
  AccentArray: TYPE = ARRAY [0..0) OF AccentEntry;
  AccentEntry: TYPE = MACHINE DEPENDENT RECORD [  -- oldChar => dead part, accent => accent Part, hostChar => Star Char(complex)
     oldChar, accent, hostChar: CharDefs.Char];
      
    
  -- =======================
  --    Global Variables & Constants
  -- =======================
  chSetNotUsed: RelIndex = LAST[RelIndex];
  ebcdicDft: CHARACTER = 140C;
  
  -- =======================
  
  Init: PROC =
    BEGIN
    --  Note the following code is for deleting a file name 3270Translations, the name of the translation file used in Star2.0. This code should get deleted in the future. Since we are no longer using the file 3270Translations, we must delete the the file from all the customers workstation.
    -- delete old version of 3270Translations 
    DeleteOldFile["3270Translations"L];
    DeleteOldFile["UK3270Translations"L];
    DeleteOldFile["Ge3270Translations"L];
    DeleteOldFile["Sw3270Translations"L];
    RETURN;
    END;
  
  DeleteOldFile: PROC[svName: Sv] =
    BEGIN
    fileHandle: NSFile.Handle;
    fileHandle ← StarControlDefs.GetSystemFile[svName ! 
      NSFile.Error => GOTO exit];
    NSFile.ChangeControls[fileHandle, NSFile.allControlSelections, [access: NSFile.fullAccess], sessionSystem];
    NSFile.Delete[fileHandle, StarControlDefs.sessionSystem];
    EXITS
     exit => RETURN;
    END;
    
    
  GetTransFile: PUBLIC PROC [hostLang: Em3270Defs.IBMlanguages]
    RETURNS [ptrToTransFile: BaseOISToEFile, sp: Space.Interval] =
    -- This proc will return a pointer to the beginning of the file corresponding to hostLang.
    BEGIN
    ctpgFile: Ct;
    file: NSFile.Handle;
    
    file ← StarControlDefs.GetSystemFile[
      SELECT hostLang FROM
         USenglish => "US3270Translations"L,
	 UKenglish => "GB3270Translations"L,    -- ++ "UK3270Translations"L, 
	 German =>    "DE3270Translations"L,    -- ++ "Ge3270Translations"L,
	 Swedish =>   "SE3270Translations"L,   -- ++ "Sw3270Translations"L,
	 French =>    "FR3270Translations"L,    -- ++ "Fr3270Translations"L, 
	 JapaneseKana => "JKana3270Translations"L,  --  = JKana3270Translations 
	 JapaneseEnglish => "JE3270Translations"L,   --  JapaneseEnglish.
        ENDCASE => "US3270Translations"L ];
        -- this use of US as a default for languages without a Translation file is only temporary,
	-- there should be a separate translation file for each foreign Star.
  
    ctpgFile ← Inline.LowHalf[
      NSSegment.GetSizeInPages[file:file, session: StarControlDefs.sessionSystem]];

    sp ← NSSegment.Map[
      origin: NSSegment.Origin[
        file: file, base: File.firstPageNumber,
	count: ctpgFile],
        swapUnits: [unitary[]],
      session: StarControlDefs.sessionSystem];
    ptrToTransFile ← sp.pointer;
    RETURN[ptrToTransFile, sp];    
    END; -- Start
    
    
  DeleteFileSpace: PUBLIC PROC[sp: Space.Interval] =
    BEGIN
    [] ← Space.Unmap[sp.pointer];
    END;
      
    
  EBCDICFromOISChar: PUBLIC PROC[ptrToTransFile: BaseOISToEFile, oisChar: CharDefs.Char] 
    RETURNS[char: CHARACTER] =  
    BEGIN
    relPtrToCode: RelIndex;
    char ← ebcdicDft;    -- EBCDIC 60h (hyphen) = default
    IF ptrToTransFile.chSetIndex[oisChar.chset] = chSetNotUsed THEN
      RETURN[char]
    ELSE
      BEGIN
      relPtrToCode ← ptrToTransFile.chSetIndex[oisChar.chset];
      char ← ptrToTransFile[relPtrToCode][oisChar.code];
      RETURN[char];
      END;
    END;
    
  OISRunFromEBCDIC: PUBLIC PROC[ptrToTransFile: BaseOISToEFile, 
    inRun: LptETextRun, outRun: LptTextRun, ctChar: TextIndexType] =
    BEGIN
    -- This proc take a run of EBCDIC characters and translates it to a run of OISChars.
    i:TextIndexType;
    FOR i IN [0..ctChar) DO
      outRun[i] ← ptrToTransFile.charFromEBCDIC[inRun[i]];
      ENDLOOP;
    END; 
    
  OISCharFromEBCDIC: PUBLIC PROC[ptrToTransFile: BaseOISToEFile, char: CHARACTER] 
    RETURNS[oisChar: CharDefs.Char] =
    BEGIN
    oisChar ← ptrToTransFile.charFromEBCDIC[char];
    RETURN[oisChar];
    END;
    
  -- The following two procedures provides the capability to handle deadkey accents. Deadkey accent arises from convertion between any Star Document to/from a 3270 window.
    
  ExpandLegalDeadchar: PUBLIC PROC[ptrToTransFile: BaseOISToEFile, inChar: CharDefs.Char]
    RETURNS[outChar1, outChar2: CharDefs.Char] = 
    BEGIN
    -- This pocedure is called to move text from a Star Document into a 3270 window.  ie. one-to-many.
    -- This procedure is needed because some complex characters can be represented as one character in Star, but it must be split up into two characters in 3270.
    -- IMPORTANT Note: Accents are always on the left in Japanese, but in European languages accents are always to the right.  
    --   Instead of using a select statment to implement the distinction(the correct method), I switched the entry around in the data file for JapaneseKana(i.e. the accent entries in the Japanese data file is actually the oldChar, and the oldChar entry is actually accent). This was done straightly for performance considerations only and for no other reason.   
    FOR i: CARDINAL IN [0..ptrToTransFile.ctHostAccents) DO
      IF inChar = ptrToTransFile.hostAccentPart.accentArray[i].hostChar THEN
        RETURN[ptrToTransFile.hostAccentPart.accentArray[i].accent, ptrToTransFile.hostAccentPart.accentArray[i].oldChar];
    ENDLOOP;
--$    RETURN[inChar, Roman[unused177B]];
--    /* The following is NOT a NOOP!  Illegal OISChars get mapped into */
--    /* Roman[hyphen] so Move/Copy works right */
    RETURN[OISCharFromEBCDIC[ptrToTransFile, EBCDICFromOISChar[ptrToTransFile, inChar]], Roman[unused177B]];
    END;   -- ExpandLegalDeadchar
    
    
  CoalesceDeadChar: PUBLIC PROC[ptrToTransFile: BaseOISToEFile, firstChar, secondChar: CharDefs.Char]
    RETURNS[outChar: CharDefs.Char, skip: Bv] = 
    BEGIN
    -- This procedure is called to move text from a 3270 window to a Star Document. ie. many-to-one.
    -- IMPORTANT NOTE: Accents are always on the left in Japanese, but in European languages accents are always to the right.  
    --   Instead of using a select statment to implement the distinction(the correct method), I switched the entry around in the data file for JapaneseKana(i.e. the accent entries in the Japanese data file is actually the oldChar, and the oldChar entry is actually accent). This was done straightly for performance considerations only and for no other reason.  
    FOR i: CARDINAL IN [0..ptrToTransFile.ctHostAccents) DO
      IF firstChar # ptrToTransFile.hostAccentPart.accentArray[i].accent THEN LOOP
      ELSE
       IF secondChar = ptrToTransFile.hostAccentPart.accentArray[i].oldChar THEN 
         RETURN[ptrToTransFile.hostAccentPart.accentArray[i].hostChar, TRUE];
    ENDLOOP;
    RETURN[firstChar, FALSE];
    END;   -- CoalesceDeadChar

-- Mainline codes;
  Init[];  
    
END.
LOG
 1-Feb-82  - Lui    - Created
18-Mar-82  - Lui    - Changed to use 3270DataFile.
23-Mar-82  - Lui    - Added ExpandLegalDeadchar, CoalesceDeadChar.   
24-Mar-82  - Lui    - Changed name of file from 3270dataFile to 3270Translations 
22-Apr-82  - Lui    - Added code for JStar to access 3270Translations. ( AR 7254).
18-Jun-82  - Lui    - Added OISRunFromEBCDIC.
 1-Sep-82  - Lui    - Added hostLang
10-Sep-82  - Lui    - removed Init Proc and added GetTransFile Proc
16-Sep-82  - Lui    - fixed bug in CoalesceDeadChar and ExpandLegalDeadchar
September 21, 1982 - Hanzel - Update to Filing 5.0 for 3.0i.
16-Sep-82  - Lui    - added UKenglish.
12-Nov-82  - Lui    - activiated mapping of mutliNational translation files
23-Nov-82  - Lui    - added code to delete the file 3270Translations.
 2-Dec-82  - Lui    - changed name of data files.
 9-Dec-82  - Lui    - added DeleteFileSpace.
20-Dec-82  - Lui    - changed size of swap unit from 1 to ctpgSpace(entire space)
28-Jan-83  - Lui    - changed name of data files and add codes to delete all old ones.
10-Jun-83  - Lui    - AR 13585: added support for JapaneseEnglish by adding JE3270Translations.
30-Nov-83  - Pettit - Klamath conversion: Drop use of SpDefs and use new Map/Unmap protocols.
 5-Apr-84 15:57:51 - Caro - Upgrade to new Em3270Defs
 9-Apr-84 14:41:02 - Caro - Added fix to ExpandLegalDeadchar so that funny 
 			    characters in Move/Copy show up as proper EBCDIC 
			    chars.
26-Apr-84 13:51:24 - Caro - Fixed AR???? for illegal chars in Move/Copy in
			    ExpandLegalDeadchar
6-Jul-84 11:03:31 - Caro -  Change access of translation file from full to read only.