(DEFINE-FILE-INFO PACKAGE "INTERLISP" READTABLE "INTERLISP" BASE 10) (FILECREATED "19-Jul-2022 17:13:23" {DSK}kaplan>Local>medley3.5>working-medley>sources>XCCS.;54 13309 :CHANGES-TO (FNS \XCCSBACKCCODE) :PREVIOUS-DATE "19-Jul-2022 14:56:54" {DSK}kaplan>Local>medley3.5>working-medley>sources>XCCS.;53) (PRETTYCOMPRINT XCCSCOMS) (RPAQQ XCCSCOMS [(FNS ACCESS-CHARSET) (FNS \XCCSINCCODE \XCCSPEEKCCODE \XCCSOUTCHAR \XCCSBACKCCODE \XCCSFORMATBYTESTREAM) (FNS \CREATE.XCCS.EXTERNALFORMAT) (FNS \NSIN.24BITENCODING.ERROR) (INITVARS (*SIGNAL-24BIT-NSENCODING-ERROR*)) (DECLARE%: EVAL@COMPILE DONTCOPY (EXPORT (CONSTANTS (\NORUNCODE 255) (NSCHARSETSHIFT 255)) (MACROS \RUNCODED) (OPTIMIZERS ACCESS-CHARSET))) (DECLARE%: DONTEVAL@LOAD DOCOPY (P (\CREATE.XCCS.EXTERNALFORMAT]) (DEFINEQ (ACCESS-CHARSET [LAMBDA (STREAM NEWVALUE) (* ; "Edited 11-Sep-87 15:46 by bvm:") (FDEVOP 'CHARSETFN (fetch (STREAM DEVICE) of STREAM) STREAM NEWVALUE]) ) (DEFINEQ (\XCCSINCCODE [LAMBDA (STREAM COUNTP) (* ; "Edited 6-Aug-2021 15:57 by rmk:") (* ;;; "Returns a 16 bit character code. SHIFTEDCSET is STREAM's char set left shifted 8.") (* ;;; "If COUNTP is non-NIL, the variable *BYTECOUNTER* is set freely to the number of bytes read.") (* ;;; "This doesn't do EOL conversion, \INCHAR does that") (DECLARE (USEDFREE *BYTECOUNTER*)) (LET (NUMBYTES (CSET (ACCESS-CHARSET STREAM)) (CHAR (\BIN STREAM))) (* ;  "Error on EOF unless ENDOFSTREAMOP does something else.") (* ;; " NUMBYTES tracks the number of \BINs. ") (IF (EQ CHAR NSCHARSETSHIFT) THEN (* ;  "Shifting character sets, toss CHAR") (SETQ CSET (\BIN STREAM)) (IF (NEQ NSCHARSETSHIFT CSET) THEN (* ;  "Shift to new runcode CSET: SH CS CH") (ACCESS-CHARSET STREAM CSET) (SETQ CHAR (\BIN STREAM)) (SETQ NUMBYTES 3) ELSEIF (EQ 0 (\BIN STREAM)) THEN (* ; "SH SH CSH CS CH where CSH is 0") (* ;;  "The high-order character set byte must be 0, because we don't support obese characters (24 bit)") (SETQ CSET (\BIN STREAM)) (SETQ CHAR (\BIN STREAM)) (* ; "To align with below") (SETQ NUMBYTES 5) (ACCESS-CHARSET STREAM \NORUNCODE) ELSE (\NSIN.24BITENCODING.ERROR STREAM)) (* ;; "The stream now knows the new character set, runcoded or not.") ELSEIF (EQ CSET \NORUNCODE) THEN (* ; "2-bytes") (SETQ CSET CHAR) (SETQ CHAR (\BIN STREAM)) (SETQ NUMBYTES 2) ELSE (* ;; "Runcoded CSET and CHAR") (SETQ NUMBYTES 1)) (CL:WHEN COUNTP (SETQ *BYTECOUNTER* NUMBYTES)) (CL:WHEN CHAR (* ;  "Typically NIL if ENDOFSTREAMOP returned NIL at EOF ") (LOGOR (UNFOLD CSET 256) CHAR))]) (\XCCSPEEKCCODE [LAMBDA (STREAM NOERROR) (* ; "Edited 21-Jun-2021 23:44 by rmk:") (* ;;  "Modeled on \XCCSINCCODE, but peeks at the last byte in the sequence, leaves the stream unchanged") (LET ((CSET (ACCESS-CHARSET STREAM)) (CHAR (\PEEKBIN STREAM NOERROR))) (* ;;  "Returns a 16 bit character code. Doesn't do EOL conversion--\PEEKCCODE does that. ") (* ;; "We don't change the charset in the stream, put the file ptr back the way it was.") (CL:WHEN CHAR (IF (EQ CHAR NSCHARSETSHIFT) THEN (\BIN STREAM) (* ; "Read the peeked shifting byte") (SETQ CSET (\BIN STREAM)) (* ; "Consume the char shift byte") (IF (NEQ CSET NSCHARSETSHIFT) THEN (* ;;  "Shift to new runcode CSET: SH CS CH. We have to BIN what we peeked, BIN, and peek again") (SETQ CHAR (\PEEKBIN STREAM NOERROR)) (\BACKFILEPTR STREAM) (\BACKFILEPTR STREAM) ELSEIF (EQ 0 (\BIN STREAM)) THEN (* ; "SH SH CSH CS CH where CSH is 0") (* ;;  "Note: no eof error check on this \BIN -- an eof in the middle of a charset shift is an error") (SETQ CSET (\BIN STREAM)) (SETQ CHAR (\PEEKBIN STREAM NOERROR)) (\BACKFILEPTR STREAM) (\BACKFILEPTR STREAM) (\BACKFILEPTR STREAM) (\BACKFILEPTR STREAM) ELSE (\NSIN.24BITENCODING.ERROR STREAM)) ELSEIF (EQ CSET \NORUNCODE) THEN (* ; "2 byte runs, BIN/PEEK/BACK") (SETQ CSET CHAR) (\BIN STREAM) (SETQ CHAR (\PEEKBIN STREAM NOERROR)) (* ; "One BACKFILEPTR seems OK") (\BACKFILEPTR STREAM)) (* ;; "No need to back up for the runcoded case") (CL:WHEN CHAR (LOGOR (UNFOLD CSET 256) CHAR)))]) (\XCCSOUTCHAR [LAMBDA (STREAM CHARCODE) (* ; "Edited 13-Aug-2021 10:24 by rmk:") (* ;; "Closed function for the :XCCS external format, also called when :XCCS is the default") (COND ((EQ CHARCODE (CHARCODE EOL)) (FREPLACE (STREAM CHARPOSITION) OF STREAM WITH 0) [COND [(NOT (\RUNCODED STREAM)) (* ;  "Charset is a constant 0, we put out the high-order byte.") (\BOUT STREAM (\CHARSET (CHARCODE EOL] ((EQ (\CHARSET (CHARCODE EOL)) (ffetch (STREAM CHARSET) of STREAM))) (T (* ;  "We are runcoded, and not in character set 0, have to shift.") (\BOUT STREAM NSCHARSETSHIFT) (\BOUT STREAM (freplace (STREAM CHARSET) of STREAM with (\CHARSET (CHARCODE EOL] (* ;; "We are now in the right charset (0) for the first EOL byte. For CRLF, the CR is immediately followed by the LF byte, without the prefix 0 byte even if not runcoded, i.e. the 2 bytes are though of as a composite. The stream is left in CSET0 (the freplace above), read for another shift according to the next shift in a runcoded file.") (\BOUTEOL STREAM)) (T (CHANGE (FFETCH (STREAM CHARPOSITION) OF STREAM) (IPLUS16 1 DATUM)) (COND ((NOT (\RUNCODED STREAM)) (\BOUT STREAM (\CHARSET CHARCODE)) (\BOUT STREAM (\CHAR8CODE CHARCODE))) ((EQ (\CHARSET CHARCODE) (ffetch (STREAM CHARSET) of STREAM)) (\BOUT STREAM (\CHAR8CODE CHARCODE))) (T (\BOUT STREAM NSCHARSETSHIFT) (\BOUT STREAM (freplace (STREAM CHARSET) of STREAM with (\CHARSET CHARCODE)) ) (\BOUT STREAM (\CHAR8CODE CHARCODE]) (\XCCSBACKCCODE [LAMBDA (STREAM COUNTP) (* ; "Edited 19-Jul-2022 17:12 by rmk") (* ; "Edited 13-Aug-2021 14:08 by rmk:") (DECLARE (USEDFREE *BYTECOUNTER*)) (LET ((BYTE (AND (\BACKFILEPTR STREAM) (\PEEKBIN STREAM))) (CSET (ACCESS-CHARSET STREAM))) (CL:WHEN BYTE (* ;; "The immediately preceding byte must be a character byte. If it is a byte in a runcode, then we are done, even if the byte before is part of a shift sequence.") (* ;; "But if we are currently in a nonruncoded file, we have to go back one more to get the character set byte.") (* ;; "If we can't back up, we are already at the beginning.") (IF (EQ \NORUNCODE CSET) THEN (IF (\BACKFILEPTR STREAM) THEN (CL:WHEN COUNTP (SETQ *BYTECOUNTER* -2)) (LOGOR (UNFOLD (\PEEKBIN STREAM) 256) BYTE) ELSE (CL:WHEN COUNTP (SETQ *BYTECOUNTER* -1)) NIL) ELSE (CL:WHEN COUNTP (SETQ *BYTECOUNTER* -1)) (LOGOR (UNFOLD CSET 256) BYTE)))]) (\XCCSFORMATBYTESTREAM [LAMBDA (STREAM BYTESTREAM) (* ; "Edited 24-Jun-2021 16:47 by rmk:") (REPLACE (STREAM CHARSET) OF BYTESTREAM WITH (FETCH (STREAM CHARSET) OF STREAM]) ) (DEFINEQ (\CREATE.XCCS.EXTERNALFORMAT [LAMBDA (NAME EOL) (* ; "Edited 30-Jun-2022 18:08 by rmk") (* ; "Edited 10-Sep-2021 19:49 by rmk:") (* ;;; "Create the :XCCS external format. Stream's EOL overrides the (vacuous) default here") (CL:UNLESS NAME (SETQ NAME :XCCS)) (CL:UNLESS EOL (SETQ EOL 'LF)) (MAKE-EXTERNALFORMAT NAME (FUNCTION \XCCSINCCODE) (FUNCTION \XCCSPEEKCCODE) (FUNCTION \XCCSBACKCCODE) (FUNCTION \XCCSOUTCHAR) (FUNCTION \XCCSFORMATBYTESTREAM) EOL T NIL]) ) (DEFINEQ (\NSIN.24BITENCODING.ERROR [LAMBDA (STREAM) (* bvm%: "12-Mar-86 15:35") (DECLARE (USEDFREE *SIGNAL-24BIT-NSENCODING-ERROR*)) (* ;;; "Called if we see the sequence shift,shift on STREAM -- means shift to 24-bit character set, which we don't support. Usually this just means we're erroneously reading a binary file as text. If this function returns, its value is taken as a character set to shift to") (COND (*SIGNAL-24BIT-NSENCODING-ERROR* (* ;  "Only cause error if user/reader cares") (ERROR "24-bit NS encoding not supported" STREAM))) (* ; "Return charset zero") 0]) ) (RPAQ? *SIGNAL-24BIT-NSENCODING-ERROR* ) (DECLARE%: EVAL@COMPILE DONTCOPY (* "FOLLOWING DEFINITIONS EXPORTED")(DECLARE%: EVAL@COMPILE (RPAQQ \NORUNCODE 255) (RPAQQ NSCHARSETSHIFT 255) (CONSTANTS (\NORUNCODE 255) (NSCHARSETSHIFT 255)) ) (DECLARE%: EVAL@COMPILE (PUTPROPS \RUNCODED MACRO (OPENLAMBDA (STREAM) (* ;; "returns NIL is the stream is not runcoded, that is, if the stream has 16 bit bytes explicitly represented") (* ;  "note that neq is ok since charsets are known to be SMALLP's") (NEQ (fetch CHARSET of STREAM) \NORUNCODE))) ) (DEFOPTIMIZER ACCESS-CHARSET (STREAM &OPTIONAL NEWVALUE) `((OPENLAMBDA (STRM) (FDEVOP 'CHARSETFN (fetch (STREAM DEVICE) of STRM) STRM ,NEWVALUE)) ,STREAM)) (* "END EXPORTED DEFINITIONS") ) (DECLARE%: DONTEVAL@LOAD DOCOPY (\CREATE.XCCS.EXTERNALFORMAT) ) (DECLARE%: DONTCOPY (FILEMAP (NIL (1023 1252 (ACCESS-CHARSET 1033 . 1250)) (1253 10546 (\XCCSINCCODE 1263 . 4035) ( \XCCSPEEKCCODE 4037 . 6573) (\XCCSOUTCHAR 6575 . 8795) (\XCCSBACKCCODE 8797 . 10217) ( \XCCSFORMATBYTESTREAM 10219 . 10544)) (10547 11222 (\CREATE.XCCS.EXTERNALFORMAT 10557 . 11220)) (11223 12054 (\NSIN.24BITENCODING.ERROR 11233 . 12052))))) STOP