⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 termcap.texi

📁 linux下bash的源码
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
Here is how you might use the function @code{tgetent}:@smallexample#ifdef unixstatic char term_buffer[2048];#else#define term_buffer 0#endifinit_terminal_data ()@{  char *termtype = getenv ("TERM");  int success;  if (termtype == 0)    fatal ("Specify a terminal type with `setenv TERM <yourtype>'.\n");  success = tgetent (term_buffer, termtype);  if (success < 0)    fatal ("Could not access the termcap data base.\n");  if (success == 0)    fatal ("Terminal type `%s' is not defined.\n", termtype);@}@end smallexample@noindentHere we assume the function @code{fatal} prints an error message and exits.If the environment variable @code{TERMCAP} is defined, its value is used tooverride the terminal type data base.  The function @code{tgetent} checksthe value of @code{TERMCAP} automatically.  If the value starts with@samp{/} then it is taken as a file name to use as the data base file,instead of @file{/etc/termcap} which is the standard data base.  If thevalue does not start with @samp{/} then it is itself used as the terminaldescription, provided that the terminal type @var{termtype} is among thetypes it claims to apply to.  @xref{Data Base}, for information on theformat of a terminal description.@refill@node Interrogate, Initialize, Find, Library@section Interrogating the Terminal DescriptionEach piece of information recorded in a terminal description is called a@dfn{capability}.  Each defined terminal capability has a two-letter codename and a specific meaning.  For example, the number of columns is named@samp{co}.  @xref{Capabilities}, for definitions of all the standardcapability names.Once you have found the proper terminal description with @code{tgetent}(@pxref{Find}), your application program must @dfn{interrogate} it forvarious terminal capabilities.  You must specify the two-letter code ofthe capability whose value you seek.Capability values can be numeric, boolean (capability is either present orabsent) or strings.  Any particular capability always has the same valuetype; for example, @samp{co} always has a numeric value, while @samp{am}(automatic wrap at margin) is always a flag, and @samp{cm} (cursor motioncommand) always has a string value.  The documentation of each capabilitysays which type of value it has.@refillThere are three functions to use to get the value of a capability,depending on the type of value the capability has.  Here are theirdeclarations in ANSI C:@findex tgetnum@findex tgetflag@findex tgetstr@exampleint tgetnum (char *@var{name});int tgetflag (char *@var{name});char *tgetstr (char *@var{name}, char **@var{area});@end example@table @code@item tgetnumUse @code{tgetnum} to get a capability value that is numeric.  Theargument @var{name} is the two-letter code name of the capability.  Ifthe capability is present, @code{tgetnum} returns the numeric value(which is nonnegative).  If the capability is not mentioned in theterminal description, @code{tgetnum} returns @minus{}1.@item tgetflagUse @code{tgetflag} to get a boolean value.  If the capability@var{name} is present in the terminal description, @code{tgetflag}returns 1; otherwise, it returns 0.@item tgetstrUse @code{tgetstr} to get a string value.  It returns a pointer to astring which is the capability value, or a null pointer if thecapability is not present in the terminal description.There are two ways @code{tgetstr} can find space to store the string value:@itemize @bullet@itemYou can ask @code{tgetstr} to allocate the space.  Pass a nullpointer for the argument @var{area}, and @code{tgetstr} will use@code{malloc} to allocate storage big enough for the value.Termcap will never free this storage or refer to it again; youshould free it when you are finished with it.This method is more robust, since there is no need to guess howmuch space is needed.  But it is supported only by the GNUtermcap library.@itemYou can provide the space.  Provide for the argument @var{area} theaddress of a pointer variable of type @code{char *}.  Before calling@code{tgetstr}, initialize the variable to point at available space.Then @code{tgetstr} will store the string value in that space and willincrement the pointer variable to point after the space that has beenused.  You can use the same pointer variable for many calls to@code{tgetstr}.There is no way to determine how much space is needed for a singlestring, and no way for you to prevent or handle overflow of the areayou have provided.  However, you can be sure that the total size ofall the string values you will obtain from the terminal description isno greater than the size of the description (unless you get the samecapability twice).  You can determine that size with @code{strlen} onthe buffer you provided to @code{tgetent}.  See below for an example.Providing the space yourself is the only method supported by the Unixversion of termcap.@end itemize@end tableNote that you do not have to specify a terminal type or terminaldescription for the interrogation functions.  They automatically use thedescription found by the most recent call to @code{tgetent}.Here is an example of interrogating a terminal description for variouscapabilities, with conditionals to select between the Unix and GNU methodsof providing buffer space.@examplechar *tgetstr ();char *cl_string, *cm_string;int height;int width;int auto_wrap;char PC;   /* For tputs.  */char *BC;  /* For tgoto.  */char *UP;interrogate_terminal ()@{#ifdef UNIX  /* Here we assume that an explicit term_buffer     was provided to tgetent.  */  char *buffer    = (char *) malloc (strlen (term_buffer));#define BUFFADDR &buffer#else#define BUFFADDR 0#endif  char *temp;  /* Extract information we will use.  */  cl_string = tgetstr ("cl", BUFFADDR);  cm_string = tgetstr ("cm", BUFFADDR);  auto_wrap = tgetflag ("am");  height = tgetnum ("li");  width = tgetnum ("co");  /* Extract information that termcap functions use.  */  temp = tgetstr ("pc", BUFFADDR);  PC = temp ? *temp : 0;  BC = tgetstr ("le", BUFFADDR);  UP = tgetstr ("up", BUFFADDR);@}@end example@noindent@xref{Padding}, for information on the variable @code{PC}.  @xref{UsingParameters}, for information on @code{UP} and @code{BC}.@node Initialize, Padding, Interrogate, Library@section Initialization for Use of Termcap@cindex terminal flags (kernel)Before starting to output commands to a terminal using termcap,an application program should do two things:@itemize @bullet@itemInitialize various global variables which termcap library outputfunctions refer to.  These include @code{PC} and @code{ospeed} forpadding (@pxref{Output Padding}) and @code{UP} and @code{BC} forcursor motion (@pxref{tgoto}).@refill@itemTell the kernel to turn off alteration and padding of horizontal-tabcharacters sent to the terminal.@end itemizeTo turn off output processing in Berkeley Unix you would use @code{ioctl}with code @code{TIOCLSET} to set the bit named @code{LLITOUT}, and clearthe bits @code{ANYDELAY} using @code{TIOCSETN}.  In POSIX or System V, youmust clear the bit named @code{OPOST}.  Refer to the system documentationfor details.@refillIf you do not set the terminal flags properly, some older terminals willnot work.  This is because their commands may contain the characters thatnormally signify newline, carriage return and horizontal tab---characterswhich the kernel thinks it ought to modify before output.When you change the kernel's terminal flags, you must arrange to restorethem to their normal state when your program exits.  This implies that theprogram must catch fatal signals such as @code{SIGQUIT} and @code{SIGINT}and restore the old terminal flags before actually terminating.Modern terminals' commands do not use these special characters, so if youdo not care about problems with old terminals, you can leave the kernel'sterminal flags unaltered.@node Padding, Parameters, Initialize, Library@section Padding@cindex padding@dfn{Padding} means outputting null characters following a terminal displaycommand that takes a long time to execute.  The terminal description sayswhich commands require padding and how much; the function @code{tputs},described below, outputs a terminal command while extracting from it thepadding information, and then outputs the padding that is necessary.@menu* Why Pad::     Explanation of padding.* Not Enough::  When there is not enough padding.* Describe Padding::  The data base says how much padding a terminal needs.* Output Padding::  Using @code{tputs} to output the needed padding.@end menu@node Why Pad, Not Enough,  , Padding@subsection Why Pad, and HowMost types of terminal have commands that take longer to execute than theydo to send over a high-speed line.  For example, clearing the screen maytake 20msec once the entire command is received.  During that time, on a9600 bps line, the terminal could receive about 20 additional outputcharacters while still busy clearing the screen.  Every terminal has acertain amount of buffering capacity to remember output characters thatcannot be processed yet, but too many slow commands in a row can cause thebuffer to fill up.  Then any additional output that cannot be processedimmediately will be lost.To avoid this problem, we normally follow each display command with enoughuseless charaters (usually null characters) to fill up the time that thedisplay command needs to execute.  This does the job if the terminal throwsaway null characters without using up space in the buffer (which mostterminals do).  If enough padding is used, no output can ever be lost.  Theright amount of padding avoids loss of output without slowing downoperation, since the time used to transmit padding is time that nothingelse could be done.The number of padding characters needed for an operation depends on theline speed.  In fact, it is proportional to the line speed.  A 9600 baudline transmits about one character per msec, so the clear screen command inthe example above would need about 20 characters of padding.  At 1200 baud,however, only about 3 characters of padding are needed to fill up 20msec.@node Not Enough, Describe Padding, Why Pad, Padding@subsection When There Is Not Enough PaddingThere are several common manifestations of insufficient padding.@itemize @bullet@itemEmacs displays @samp{I-search: ^Q-} at the bottom of the screen.This means that the terminal thought its buffer was getting full ofdisplay commands, so it tried to tell the computer to stop sendingany.@itemThe screen is garbled intermittently, or the details of garbling varywhen you repeat the action.  (A garbled screen could be due to acommand which is simply incorrect, or to user option in the terminalwhich doesn't match the assumptions of the terminal description, butthis usually leads to reproducible failure.)This means that the buffer did get full, and some commands were lost.Many changeable factors can change which ones are lost.@itemScreen is garbled at high output speeds but not at low speeds.Padding problems nearly always go away at low speeds, usually even at1200 baud.This means that a high enough speed permits commands to arrive fasterthan they can be executed.@end itemizeAlthough any obscure command on an obscure terminal might lack padding,in practice problems arise most often from the clearing commands@samp{cl} and @samp{cd} (@pxref{Clearing}), the scrolling commands@samp{sf} and @samp{sr} (@pxref{Scrolling}), and the line insert/deletecommands @samp{al} and @samp{dl} (@pxref{Insdel Line}).Occasionally the terminal description fails to define @samp{sf} and someprograms will use @samp{do} instead, so you may get a problem with@samp{do}.  If so, first define @samp{sf} just like @samp{do}, thenadd some padding to @samp{sf}.The best strategy is to add a lot of padding at first, perhaps 200 msec.This is much more than enough; in fact, it should cause a visible slowdown.(If you don't see a slowdown, the change has not taken effect;@pxref{Changing}.)  If this makes the problem go away, you have found theright place to add padding; now reduce the amount until the problem comesback, then increase it again.  If the problem remains, either it is in someother capability or it is not a matter of padding at all.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -