📄 termcap.texinfo
字号:
specifying how and where to encode the parameters for output. This languageuses character sequences starting with @samp{%}. (This is the same idea as@code{printf}, but the details are different.) The language for parameterencoding is described in this section.A program that is doing display output calls the functions @code{tparam} or@code{tgoto} to encode parameters according to the specifications. Thesefunctions produce a string containing the actual commands to be output (aswell a padding spec which must be processed with @code{tputs};@pxref{Padding}).@menu* Encode Parameters:: The language for encoding parameters.* Using Parameters:: Outputting a string command with parameters.@end menu@node Encode Parameters, Using Parameters, Parameters, Parameters@subsection Describing the Encoding@cindex %A terminal command string that requires parameters contains specialcharacter sequences starting with @samp{%} to say how to encode theparameters. These sequences control the actions of @code{tparam} and@code{tgoto}.The parameters values passed to @code{tparam} or @code{tgoto} areconsidered to form a vector. A pointer into this vector determinesthe next parameter to be processed. Some of the @samp{%}-sequencesencode one parameter and advance the pointer to the next parameter.Other @samp{%}-sequences alter the pointer or alter the parametervalues without generating output.For example, the @samp{cm} string for a standard ANSI terminal is writtenas @samp{\E[%i%d;%dH}. (@samp{\E} stands for @key{ESC}.) @samp{cm} byconvention always requires two parameters, the vertical and horizontal goalpositions, so this string specifies the encoding of two parameters. Here@samp{%i} increments the two values supplied, and each @samp{%d} encodesone of the values in decimal. If the cursor position values 20,58 areencoded with this string, the result is @samp{\E[21;59H}.First, here are the @samp{%}-sequences that generate output. Except for@samp{%%}, each of them encodes one parameter and advances the pointerto the following parameter.@table @samp@item %%Output a single @samp{%}. This is the only way to represent a literal@samp{%} in a terminal command with parameters. @samp{%%} does notuse up a parameter.@item %dAs in @code{printf}, output the next parameter in decimal.@item %2Like @samp{%02d} in @code{printf}: output the next parameter indecimal, and always use at least two digits.@item %3Like @samp{%03d} in @code{printf}: output the next parameter indecimal, and always use at least three digits. Note that @samp{%4}and so on are @emph{not} defined.@item %.Output the next parameter as a single character whose ASCII code isthe parameter value. Like @samp{%c} in @code{printf}.@item %+@var{char}Add the next parameter to the character @var{char}, and output theresulting character. For example, @samp{%+ } represents 0 as a space,1 as @samp{!}, etc.@end tableThe following @samp{%}-sequences specify alteration of the parameters(their values, or their order) rather than encoding a parameter for output.They generate no output; they are used only for their side effectson the parameters. Also, they do not advance the ``next parameter'' pointerexcept as explicitly stated. Only @samp{%i}, @samp{%r} and @samp{%>} aredefined in standard Unix termcap. The others are GNU extensions.@refill@table @samp@item %iIncrement the next two parameters. This is used for terminals thatexpect cursor positions in origin 1. For example, @samp{%i%d,%d} wouldoutput two parameters with @samp{1} for 0, @samp{2} for 1, etc.@item %rInterchange the next two parameters. This is used for terminals whosecursor positioning command expects the horizontal position first.@item %sSkip the next parameter. Do not output anything.@item %bBack up one parameter. The last parameter used will become once againthe next parameter to be output, and the next output command will useit. Using @samp{%b} more than once, you can back up any number ofparameters, and you can refer to each parameter any number of times.@item %>@var{c1}@var{c2}Conditionally increment the next parameter. Here @var{c1} and@var{c2} are characters which stand for their ASCII codes as numbers.If the next parameter is greater than the ASCII code of @var{c1}, theASCII code of @var{c2} is added to it.@refill@item %a @var{op} @var{type} @var{pos}Perform arithmetic on the next parameter, do not use it up, and do notoutput anything. Here @var{op} specifies the arithmetic operation,while @var{type} and @var{pos} together specify the other operand.Spaces are used above to separate the operands for clarity; the spacesdon't appear in the data base, where this sequence is exactly fivecharacters long.The character @var{op} says what kind of arithmetic operation toperform. It can be any of these characters:@table @samp@item =assign a value to the next parameter, ignoring its old value.The new value comes from the other operand.@item +add the other operand to the next parameter.@item -subtract the other operand from the next parameter.@item *multiply the next parameter by the other operand.@item /divide the next parameter by the other operand.@end tableThe ``other operand'' may be another parameter's value or a constant;the character @var{type} says which. It can be:@table @samp@item pUse another parameter. The character @var{pos} says whichparameter to use. Subtract 64 from its ASCII code to get theposition of the desired parameter relative to this one. Thus,the character @samp{A} as @var{pos} means the parameter after thenext one; the character @samp{?} means the parameter before thenext one.@item cUse a constant value. The character @var{pos} specifies thevalue of the constant. The 0200 bit is cleared out, so that 0200can be used to represent zero.@end table@end tableThe following @samp{%}-sequences are special purpose hacks to compensatefor the weird designs of obscure terminals. They modify the next parameteror the next two parameters but do not generate output and do not use up anyparameters. @samp{%m} is a GNU extension; the others are defined instandard Unix termcap.@table @samp@item %nExclusive-or the next parameter with 0140, and likewise the parameterafter next.@item %mComplement all the bits of the next parameter and the parameter after next.@item %BEncode the next parameter in BCD. It alters the value of theparameter by adding six times the quotient of the parameter by ten.Here is a C statement that shows how the new value is computed:@example@var{parm} = (@var{parm} / 10) * 16 + @var{parm} % 10;@end example@item %DTransform the next parameter as needed by Delta Data terminals.This involves subtracting twice the remainder of the parameter by 16.@example@var{parm} -= 2 * (@var{parm} % 16);@end example@end table@node Using Parameters,, Encode Parameters, Parameters@subsection Sending Display Commands with ParametersThe termcap library functions @code{tparam} and @code{tgoto} serve as theanalog of @code{printf} for terminal string parameters. The newer function@code{tparam} is a GNU extension, more general but missing from Unixtermcap. The original parameter-encoding function is @code{tgoto}, whichis preferable for cursor motion.@menu* tparam:: The general case, for GNU termcap only.* tgoto:: The special case of cursor motion.@end menu@node tparam, tgoto, Using Parameters, Using Parameters@subsubsection @code{tparam}@findex tparamThe function @code{tparam} can encode display commands with any number ofparameters and allows you to specify the buffer space. It is the preferredfunction for encoding parameters for all but the @samp{cm} capability. ItsANSI C declaration is as follows:@examplechar *tparam (char *@var{ctlstring}, char *@var{buffer}, int @var{size}, int @var{parm1},...)@end exampleThe arguments are a control string @var{ctlstring} (the value of a terminalcapability, presumably), an output buffer @var{buffer} and @var{size}, andany number of integer parameters to be encoded. The effect of@code{tparam} is to copy the control string into the buffer, encodingparameters according to the @samp{%} sequences in the control string.You describe the output buffer by its address, @var{buffer}, and its sizein bytes, @var{size}. If the buffer is not big enough for the data to bestored in it, @code{tparam} calls @code{malloc} to get a larger buffer. Ineither case, @code{tparam} returns the address of the buffer it ultimatelyuses. If the value equals @var{buffer}, your original buffer was used.Otherwise, a new buffer was allocated, and you must free it after you aredone with printing the results. If you pass zero for @var{size} and@var{buffer}, @code{tparam} always allocates the space with @code{malloc}.All capabilities that require parameters also have the ability to specifypadding, so you should use @code{tputs} to output the string produced by@code{tparam}. @xref{Padding}. Here is an example.@example@{ char *buf; char buffer[40]; buf = tparam (command, buffer, 40, parm); tputs (buf, 1, fputchar); if (buf != buffer) free (buf);@}@end exampleIf a parameter whose value is zero is encoded with @samp{%.}-styleencoding, the result is a null character, which will confuse @code{tputs}.This would be a serious problem, but luckily @samp{%.} encoding is usedonly by a few old models of terminal, and only for the @samp{cm}capability. To solve the problem, use @code{tgoto} rather than@code{tparam} to encode the @samp{cm} capability.@refill@node tgoto,, tparam, Using Parameters@subsubsection @code{tgoto}@findex tgotoThe special case of cursor motion is handled by @code{tgoto}. Thereare two reasons why you might choose to use @code{tgoto}:@itemize @bullet@itemFor Unix compatibility, because Unix termcap does not have @code{tparam}.@itemFor the @samp{cm} capability, since @code{tgoto} has a special featureto avoid problems with null characters, tabs and newlines on certain oldterminal types that use @samp{%.} encoding for that capability.@end itemizeHere is how @code{tgoto} might be declared in ANSI C:@examplechar *tgoto (char *@var{cstring}, int @var{hpos}, int @var{vpos})@end exampleThere are three arguments, the terminal description's @samp{cm} string andthe two cursor position numbers; @code{tgoto} computes the parametrizedstring in an internal static buffer and returns the address of that buffer.The next time you use @code{tgoto} the same buffer will be reused.@vindex UP@vindex BCParameters encoded with @samp{%.} encoding can generate null characters,tabs or newlines. These might cause trouble: the null character because@code{tputs} would think that was the end of the string, the tab becausethe kernel or other software might expand it into spaces, and the newlinebecaue the kernel might add a carriage-return, or padding charactersnormally used for a newline. To prevent such problems, @code{tgoto} iscareful to avoid these characters. Here is how this works: if the targetcursor position value is such as to cause a problem (that is to say, zero,nine or ten), @code{tgoto} increments it by one, then compensates byappending a string to move the cursor back or up one position.The compensation strings to use for moving back or up are found in globalvariables named @code{BC} and @code{UP}. These are actual external Cvariables with upper case names; they are declared @code{char *}. It is upto you to store suitable values in them, normally obtained from the@samp{le} and @samp{up} terminal capabilities in the terminal descriptionwith @code{tgetstr}. Alternatively, if these two variables are both zero,the feature of avoiding nulls, tabs and newlines is turned off.It is safe to use @code{tgoto} for commands other than @samp{cm} only ifyou have stored zero in @code{BC} and @code{UP}.Note that @code{tgoto} reverses the order of its operands: the horizontalposition comes before the vertical position in the arguments to@code{tgoto}, even though the vertical position comes before the horizontalin the parameters of the @samp{cm} string. If you use @code{tgoto} with acommand such as @samp{AL} that takes one parameter, you must pass theparameter to @code{tgoto} as the ``vertical position''.@refill@node Data Base, Capabilities, Library, Top@chapter The Format of the Data BaseThe termcap data base of terminal descriptions is stored in the file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -