From 41902b2076b5a06b6b779fa9bfa812794ce0ef3e Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Sun, 28 Jan 2018 20:26:30 +0000
Subject: [PATCH] Some minor changes to DigitalMan's CSI = Ps... n
 implementation.

1) Use a functional default (1) for P1.
2) Respond with an empty P2 parameter if nothing is set.
3) Only respond if there is a single parameter.  If there is more than one,
   it's not a request.

While we're here, clean up the formatting of cterm.txt.
Tabs for indentation, spaces for alignment, don't wrap with 80-column
displays using 8-char tabs (default with more).  The ECMA-048 reference
will always wrap however.
---
 src/conio/cterm.c   |  68 ++++++-------
 src/conio/cterm.txt | 233 +++++++++++++++++++++++---------------------
 2 files changed, 154 insertions(+), 147 deletions(-)

diff --git a/src/conio/cterm.c b/src/conio/cterm.c
index 474a1ecfb0..e74d1e6300 100644
--- a/src/conio/cterm.c
+++ b/src/conio/cterm.c
@@ -1079,43 +1079,43 @@ static void do_ansi(struct cterminal *cterm, char *retbuf, size_t retsize, int *
 							cterm->doorway_mode=0;
 						break;
 					case 'n':	/* Query (extended) state information */
-						if(cterm->escbuf[1] != '=' || retbuf == NULL)
+						if(retbuf == NULL)
 							break;
 						tmp[0] = 0;
-						switch(strtoul(cterm->escbuf+2,NULL,10)) {
-							case 1:		/* Font state set via "CSI sp D" */
-								sprintf(tmp, "\x1b[=1;%u;%u;%u;%u;%u;%un"
-									,CONIO_FIRST_FREE_FONT
-									,(uint8_t)cterm->setfont_result
-									,(uint8_t)cterm->altfont[0]
-									,(uint8_t)cterm->altfont[1]
-									,(uint8_t)cterm->altfont[2]
-									,(uint8_t)cterm->altfont[3]
-								);
-								break;
-							case 2:		/* Video and emulation modes/flags set via "CSI ? N h" */
-							{
-								int vidflags = GETVIDEOFLAGS();
-								strcpy(tmp, "\x1b[=2");
-								if(cterm->origin_mode)
-									strcat(tmp, ";6");
-								if(cterm->autowrap)
-									strcat(tmp, ";7");
-								if(cterm->cursor == _NORMALCURSOR)
-									strcat(tmp, ";25");
-								if(vidflags & CIOLIB_VIDEO_ALTCHARS)
-									strcat(tmp, ";31");
-								if(vidflags & CIOLIB_VIDEO_NOBRIGHT)
-									strcat(tmp, ";32");
-								if(vidflags & CIOLIB_VIDEO_BGBRIGHT)
-									strcat(tmp, ";33");
-								if(vidflags & CIOLIB_VIDEO_BLINKALTCHARS)
-									strcat(tmp, ";34");
-								if(vidflags & CIOLIB_VIDEO_NOBLINK)
-									strcat(tmp, ";35");
-								strcat(tmp, "n");
-								break;
+						if ((strcmp(cterm->escbuf,"[=n") == 0) || (strcmp(cterm->escbuf,"[=1n"))) {
+							sprintf(tmp, "\x1b[=1;%u;%u;%u;%u;%u;%un"
+								,CONIO_FIRST_FREE_FONT
+								,(uint8_t)cterm->setfont_result
+								,(uint8_t)cterm->altfont[0]
+								,(uint8_t)cterm->altfont[1]
+								,(uint8_t)cterm->altfont[2]
+								,(uint8_t)cterm->altfont[3]
+							);
+						}
+						if (!strcmp(cterm->escbuf,"[=2n")) {
+							int vidflags = GETVIDEOFLAGS();
+							strcpy(tmp, "\x1b[=2");
+							if(cterm->origin_mode)
+								strcat(tmp, ";6");
+							if(cterm->autowrap)
+								strcat(tmp, ";7");
+							if(cterm->cursor == _NORMALCURSOR)
+								strcat(tmp, ";25");
+							if(vidflags & CIOLIB_VIDEO_ALTCHARS)
+								strcat(tmp, ";31");
+							if(vidflags & CIOLIB_VIDEO_NOBRIGHT)
+								strcat(tmp, ";32");
+							if(vidflags & CIOLIB_VIDEO_BGBRIGHT)
+								strcat(tmp, ";33");
+							if(vidflags & CIOLIB_VIDEO_BLINKALTCHARS)
+								strcat(tmp, ";34");
+							if(vidflags & CIOLIB_VIDEO_NOBLINK)
+								strcat(tmp, ";35");
+							if (strlen(tmp) == 4) {	// Nothing set
+								strcat(tmp, ";");
 							}
+							strcat(tmp, "n");
+							break;
 						}
 						if(*tmp && strlen(retbuf) + strlen(tmp) < retsize)
 							strcat(retbuf, tmp);
diff --git a/src/conio/cterm.txt b/src/conio/cterm.txt
index 9b8b87012f..00268e8197 100644
--- a/src/conio/cterm.txt
+++ b/src/conio/cterm.txt
@@ -113,37 +113,41 @@ CSI = [ p1 [ ; p2 ] ] {
 	SOURCE: CTerm only.
 
 CSI = [ p1 [ ; ... ] ] n
-    NON-STANDARD EXTENSION
+	NON-STANDARD EXTENSION
 	State/Mode Request/Report
-	Defaults: p1 = 0
+	Defaults: p1 = 1
 
-    When p1 is 1, CTerm will respond with a Font State Report of the form
+	When p1 is 1, CTerm will respond with a Font State Report of the form
         CSI = 1 ;pF ;pR ;pS0 ;pS1 ;pS2 ;pS3 n
 	pF is the first available loadable-font slot number
 	pR is the result of the previous "Font Selection" request:
 		0 = successful font selection
 		1 = font selection requests are not supported by client
 		2 = required subsystem for font use has not been initialized
-		3 = selected font is not available or is incompatible with current video mode
+		3 = selected font is not available or is incompatible with
+		    current video mode
 		4 = invalid font slot number specified in request
-		5 = required video mode for selected font is incompatible with current video mode
+		5 = required video mode for selected font is incompatible
+		    with current video mode
 		6 = memory allocation failure
-        99 = no font selection request has been received
-    pS0 - pS3 contain the font slots numbers of previously successful "Font Selection" requests
-              into the 4 available alternate-font style/attribute values:
-              pS0 - normal attribute font slot
-              PS1 - high intensity foreground attribute font slot
-              PS2 - blink attribute font slot
-              PS3 - high intensity blink attribute font slot
-
-    When p1 is 2, CTerm will respond with a Mode Report of the form
-        CSI = 2[;pN [;pN] [...]] n
-    Where pN represent zero or more mode values set previously (e.g. via CSI ? pN h).
-    Mode values cleared (disabled via CSI ? pN l) will not be included in the set of values
-    returned in the Mode Report.
-
-    Other values of p1 will be ignored by CTerm.
+		99 = no font selection request has been received
+	pS0 - pS3 contain the font slots numbers of previously successful
+	"Font Selection" requests into the 4 available alternate-font
+	style/attribute values:
+		pS0 - normal attribute font slot
+		PS1 - high intensity foreground attribute font slot
+		PS2 - blink attribute font slot
+		PS3 - high intensity blink attribute font slot
+
+	When p1 is 2, CTerm will respond with a Mode Report of the form
+	CSI = 2[;pN [;pN] [...]] n
+	Where pN represent zero or more mode values set previously
+	(e.g. via CSI ? pN h).	Mode values cleared (disabled via CSI ? pN l)
+	will not be included in the set of values returned in the Mode
+	Report.  If no modes are currently set, an empty parameter will be
+	included as the first and only pN.
 
+	SOURCE: CTerm only.
 
 CSI = 255 h
 	NON-STANDARD EXTENSION
@@ -153,22 +157,22 @@ CSI = 255 h
 
 CSI ? 6 h
 	NON-STANDARD EXTENSION
-    Enable origin mode.
-    
-    In this mode, position parameters are relative to the top left of the
-    scrolling region, not the screen.
-    
-    SOURCE: Digital VT102 User Guide
+	Enable origin mode.
+
+	In this mode, position parameters are relative to the top left of the
+	scrolling region, not the screen.
+
+	SOURCE: Digital VT102 User Guide
 
 CSI ? 7 h
 	NON-STANDARD EXTENSION
-    Enable auto wrap.
-    
-    This is the normal mode in which a write to the last column of a
-    row will move the cursor to the start of the next line triggering a
-    scroll if required to create a new line.
-    
-    SOURCE: Digital VT102 User Guide
+	Enable auto wrap.
+
+	This is the normal mode in which a write to the last column of a
+	row will move the cursor to the start of the next line triggering a
+	scroll if required to create a new line.
+
+	SOURCE: Digital VT102 User Guide
 
 CSI ? 25 h
 	NON-STANDARD EXTENSION
@@ -180,8 +184,8 @@ CSI ? 31 h
 	NON-STANDARD EXTENSION
 	Enable bright alt character set
 
-	With this mode set, the bright (1) graphic rendition selects characters
-	from an alternate character set.
+	With this mode set, the bright (1) graphic rendition selects
+	characters from an alternate character set.
 
 CSI ? 32 h
 	NON-STANDARD EXTENSION
@@ -221,22 +225,22 @@ CSI = 255 l
 
 CSI ? 6 h
 	NON-STANDARD EXTENSION
-    Disable origin mode.
-    
-    In this mode, position parameters are relative to the top left of the
-    screen, not the scrolling region.
-    
-    SOURCE: Digital VT102 User Guide
+	Disable origin mode.
+
+	In this mode, position parameters are relative to the top left of the
+	screen, not the scrolling region.
+
+	SOURCE: Digital VT102 User Guide
 
 CSI ? 7 l
 	NON-STANDARD EXTENSION
-    Disable auto wrap.
-    
-    This mode causes a write to the last column of a to leave the
-    cursor where it was before the write occurred, overwriting anything
-    which was previously written to the same position.
-    
-    SOURCE: Digital VT102 User Guide
+	Disable auto wrap.
+
+	This mode causes a write to the last column of a to leave the
+	cursor where it was before the write occurred, overwriting anything
+	which was previously written to the same position.
+
+	SOURCE: Digital VT102 User Guide
 
 CSI ? 25 l
 	NON-STANDARD EXTENSION
@@ -302,12 +306,12 @@ CSI [ p1 [ ; p2 ] ] * r
 
 CSI [ p1 [ ; p2 ] ] r
 	NON-STANDARD EXTENSION.
-    Set Top and Bottom Margins
-    Defaults: p1 = 1
-              p2 = last line on screen
-    Selects top and bottom margins, defining the scrolling region. P1 is
-    the line number of the first line in the scrolling region. P2 is the line
-    number of the bottom line.
+	Set Top and Bottom Margins
+	Defaults: p1 = 1
+		p2 = last line on screen
+	Selects top and bottom margins, defining the scrolling region. P1 is
+	the line number of the first line in the scrolling region. P2 is the
+	line number of the bottom line.
 
 CSI [ p1 ] @
 	Insert Character(s)
@@ -363,12 +367,12 @@ CSI [ p1 [ ; p2 ] ] sp D
 	font selection.  Only X11 and SDL currently do.
 	Supported p1 values:
 		0  - Default font
-		1  - Font selected by the high intensity bit when CSI [ ? 31 h is
-			 enabled
-		2  - Font selected by the blink intensity bit when CSI [ ? 34 h is
-			 enabled
-		3  - Font selected by both the high intensity and blink bits when
-			 both CSI [ ? 31 h and CSI [ ? 34 h are enabled
+		1  - Font selected by the high intensity bit when CSI [ ? 31 h
+		     is enabled
+		2  - Font selected by the blink intensity bit when CSI [ ? 34 h
+		     is enabled
+		3  - Font selected by both the high intensity and blink bits
+		     when both CSI [ ? 31 h and CSI [ ? 34 h are enabled
 	Currently included fonts are:
 		0  - Codepage 437 English
 		1  - Codepage 1251 Cyrillic, (swiss)
@@ -413,27 +417,28 @@ CSI [ p1 [ ; p2 ] ] sp D
 		40 - Topaz Plus (Amiga)      
 		41 - MicroKnight (Amiga)
 		42 - Topaz (Amiga)      
-	Not all fonts are supported in all modes.  If a font is not supported in
-	the current mode, no action is taken, but there should be a non-zero
-	'Font Selection result' value in the Font State Report.
+	Not all fonts are supported in all modes.  If a font is not supported
+	in the current mode, no action is taken, but there should be a
+	non-zero 'Font Selection result' value in the Font State Report.
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 
 CSI [ p1 ] E
 	Cursor Next Line
 	Defaults: p1 = 1
-	Moves the cursor to the first column of the line p1 down from the current position.
-	Moving past the bottom of the screen scrolls the screen up the remaining
-	number of lines filling newly added lines with the current attribute.
+	Moves the cursor to the first column of the line p1 down from the
+	current position. Moving past the bottom of the screen scrolls the
+	screen up the remaining number of lines filling newly added lines
+	with the current attribute.
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 
 CSI [ p1 ] F
 	Cursor Preceding Line
 	Defaults: p1 = 1
-	Moves the cursor to the first column if the row p1 up from the current position.
-	Attempting to move past the screen boundaries stops the cursor
-	at the screen boundary.
+	Moves the cursor to the first column if the row p1 up from the
+	current position.  Attempting to move past the screen boundaries
+	stops the cursor at the screen boundary.
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 
@@ -441,7 +446,7 @@ CSI [ p1 ] G
 	Cursor Character Absolute
 	Defaults: p1 = 1
 	Movies the cursor to column p1 of the current row.
-	
+
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 
 CSI [ p1 [ ; p2 ] ] H
@@ -480,8 +485,8 @@ CSI [ p1 ] L
 	Insert Line(s)
 	Defaults: p1 = 1
 	Inserts p1 lines at the current line position.  The current line and
-	those after it are scrolled down and the new empty lines are filled with
-	the current attribute.
+	those after it are scrolled down and the new empty lines are filled
+	with the current attribute.
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 
@@ -509,9 +514,10 @@ CSI N
 CSI [ p1 ] P
 	Delete Character
 	Defaults: p1 = 1
-	Deletes the character at the current position by shifting all characters
-	from the current column + p1 left to the current column.  Opened blanks
-	at the end of the line are filled with the current attribute.
+	Deletes the character at the current position by shifting all
+	characters from the current column + p1 left to the current column.
+	Opened blanks at the end of the line are filled with the current
+	attribute.
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 
@@ -542,8 +548,8 @@ CSI U
 CSI [ p1 ] X
 	Erase Character
 	Defaults: p1 = 1
-	Erase p1 characters starting at the current character.  Will not erase past the end
-	of line.
+	Erase p1 characters starting at the current character.  Will not erase
+	past the end of line.
 	Erased characters are set to the current attribute.
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
@@ -562,11 +568,11 @@ CSI [ p1 ] c
 	If p1 is 0, CTerm will reply with the sequence:
         CSI = 67;84;101;114;109;pN c
 	64;84;101;114;109 is the ASCII values of the "CTerm" string.  pN is the
-	CVS revision ID of CTerm with dots converted to semi-colons (e.g. "1;156").
-	Use the CVS revision to detect if a specific feature is available.  If
-	you are adding features to a forked version of cterm, please do so by
-	adding an extra parameter to the end, not by incrementing any existing
-	one!
+	CVS revision ID of CTerm with dots converted to semi-colons
+	(e.g. "1;156").  Use the CVS revision to detect if a specific feature
+	is available.  If you are adding features to a forked version of cterm,
+	please do so by	adding an extra parameter to the end, not by
+	incrementing any existing one!
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 
@@ -586,13 +592,13 @@ CSI [ p1 [ ; pX ... ] ] m
 	7 -  Negative Image - Reverses FG and BG           X  X
 	8 -  Concealed characters, sets the                X
 	     foreground colour to the background
-		 colour.
+	     colour.
 	22 - Normal intensity                           X
 	25 - Steady (Not blinking)                 X
 	27 - Positive Image - Reverses FG and BG           X  X
 	     NOTE: This should be a separate
-		       attribute than 7 but this
-			   implementation makes them equal
+	           attribute than 7 but this
+	           implementation makes them equal
 	30 - Black foreground                              X
 	31 - Red foreground                                X
 	32 - Green foreground                              X
@@ -630,8 +636,8 @@ CSI [ p1 ] n
 		  the current row.
 	255	- NON-STANDARD EXTENSION
 		  Replies as though a CSI [ 6 n was received with the cursor in
-		  the bottom right corner.  i.e.: Returns the terminal size as a
-		  position report.
+		  the bottom right corner.  i.e.: Returns the terminal size as
+		  a position report.
 
 	SOURCE: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-048.pdf
 		(parameters 5 and 6 only)
@@ -639,12 +645,12 @@ CSI [ p1 ] n
 
 CSI ? [ p1 [ pX ... ] ] s
 	NON-STANDARD EXTENSION
-    Save Mode Setting
-    Saves the current mode states as specified by CSI l and CSI h.  If
-    p1 is omitted, saves all such states.  If pX is included, saves only
-    the specified states (arguments to CSI l/h).
-    
-    SOURCE: SyncTERM only
+	Save Mode Setting
+	Saves the current mode states as specified by CSI l and CSI h.  If
+	p1 is omitted, saves all such states.  If pX is included, saves only
+	the specified states (arguments to CSI l/h).
+
+	SOURCE: SyncTERM only
 
 CSI s
 	NON-STANDARD EXTENSION
@@ -657,18 +663,18 @@ CSI s
 
 CSI ? [ p1 [ pX ... ] ]  u
 	NON-STANDARD EXTENSION
-    Restore Mode Setting
-    Saves the current mode states as specified by CSI l and CSI h.  If
-    p1 is omitted, saves all such states.  If pX is included, restores
-    all the specified states (arguments to CSI l/h)
-    
-    SOURCE: SyncTERM only
+	Restore Mode Setting
+	Saves the current mode states as specified by CSI l and CSI h.  If
+	p1 is omitted, saves all such states.  If pX is included, restores
+	all the specified states (arguments to CSI l/h)
+
+	SOURCE: SyncTERM only
 
 CSI u
 	NON-STANDARD EXTENSION
 	Restore Cursor Position
-	Move the cursor to the last position saved by CSI s.  If no position has
-	been saved, the cursor is not moved.
+	Move the cursor to the last position saved by CSI s.  If no position
+	has been saved, the cursor is not moved.
 
 	SOURCE: ANSI.SYS
 
@@ -689,13 +695,13 @@ considered as part of the music string.  You would see things such as:
 CSI MFABCD 0x0e and the F would not be played as a note.  This just added
 further confusion to the mess.
 
-Later on, BananaCom realized the conflict between delete line and music, so they
-added *another* broken code CSI N (Properly, erase in field... not implemented
-in many BBS clients) which was to provide an "unbroken" method of playing music
-strings.  They also used CSI Y to disambiguate delete line, CSI Y is supposed
-to be a vertical tab (also not implemented in very many clients).  BananaCom
-also introduced many more non-standard and standard-breaking control sequences
-which are not supported by CTerm.
+Later on, BananaCom realized the conflict between delete line and music, so
+they added *another* broken code CSI N (Properly, erase in field... not
+implemented in many BBS clients) which was to provide an "unbroken" method of
+playing music strings.  They also used CSI Y to disambiguate delete line, CSI Y
+is supposed to be a vertical tab (also not implemented in very many clients).
+BananaCom also introduced many more non-standard and standard-breaking control
+sequences which are not supported by CTerm.
 
 CTerm has further introduced a standard compliant ANSI music introducer CSI |
 
@@ -756,9 +762,9 @@ A, B, C, D, E, F, G, or P Plays the named note or pause from the current
 		play the note for.  For example, 4 would indicate a 1/4 note.
 		The default note length is 4.
 L### Set the notelength parameter for all following notes which do not have
-	one specified (ie: override the quarter-note default)  Legal note lengths
-	are 1-64 indicating the reciprocal of the fraction (ie: 4 indicates a 1/4
-	note).
+	one specified (ie: override the quarter-note default)  Legal note
+	lengths are 1-64 indicating the reciprocal of the fraction (ie: 4
+	indicates a 1/4 note).
 <	Move the next lowest octave.
 	Octave cannot go above six or below zero.
 >	Move to the next highest octave.
@@ -776,5 +782,6 @@ notes) I am willing to do that assuming the notes still sound "right".
 
 !!!PLEASE NOTE!!! If you are playing some ANSI Music then ask the user if they
 heard it, ALWAYS follow it with an 0x0f 0x0e is the shift lock character which
-*will* cause people with anything but an ANSI-BBS terminal (ie: *nix users using
-the bundled telnet app) to have their screen messed up.  0x0f "undoes" the 0x0e.
+*will* cause people with anything but an ANSI-BBS terminal (ie: *nix users
+using the bundled telnet app) to have their screen messed up.  0x0f "undoes"
+the 0x0e.
-- 
GitLab