📄 cslang.txt
字号:
} Name_Type; is not supported since char name[32] is not a SLANG_STRING_TYPE object. Always keep in mind that a SLANG_STRING_TYPE object is a char *. 3.5.2.2. Intrinsic Structures Here we show how to make intrinsic structures available to the interpreter. The simplest interface is to structure pointers and not to the actual structures themselves. The latter would require the interpreter to be involved with the creation and destruction of the structures. Dealing with the pointers themselves is far simpler. As an example, consider an object such as typedef struct _Window_Type { char *title; int row; int col; int width; int height; } Window_Type; which defines a window object with a title, size (width, height), and location (row, col). We can make variables of type Window_Type available to the interpreter via a table as follows: static SLang_IStruct_Field_Type Window_Type_Field_Table [] = { MAKE_ISTRUCT_FIELD(Window_Type, title, "title", SLANG_STRING_TYPE, 1), MAKE_ISTRUCT_FIELD(Window_Type, row, "row", SLANG_INT_TYPE, 0), MAKE_ISTRUCT_FIELD(Window_Type, col, "col", SLANG_INT_TYPE, 0), MAKE_ISTRUCT_FIELD(Window_Type, width, "width", SLANG_INT_TYPE, 0), MAKE_ISTRUCT_FIELD(Window_Type, height, "height", SLANG_INT_TYPE, 0), SLANG_END_ISTRUCT_TABLE }; More precisely, this defines the layout of the Window_Type structure. Here, the title has been declared as a read-only field. Using MAKE_ISTRUCT_FIELD(Window_Type, title, "title", SLANG_STRING_TYPE, 0), would allow read-write access. Now suppose that My_Window is a pointer to a Window_Type object, i.e., Window_Type *My_Window; We can make this variable available to the interpreter via the SLadd_istruct_table function: if (-1 == SLadd_istruct_table (Window_Type_Field_Table, (VOID_STAR) &My_Window, "My_Win")) exit (1); This creates a S-Lang interpreter variable called My_Win whose value corresponds to the My_Win structure. This would permit one to access the fields of My_Window via S-Lang statements such as define set_width_and_height (w,h) { My_Win.width = w; My_Win.height = h; } It is extremely important to understand that the interface described in this section does not allow the interpreter to create new instances of Window_Type objects. The interface merely defines an association or correspondence between an intrinsic structure pointer and a S-Lang variable. For example, if the value of My_Window is NULL, then My_Win would also be NULL. One should be careful in allowing read/write access to character string fields. If read/write access is allowed, then the application should always use the SLang_create_slstring and SLang_free_slstring functions to set the character string field of the structure. 4. Keyboard Interface S-Lang's keyboard interface has been designed to allow an application to read keyboard input from the user in a system-independent manner. The interface consists of a set of low routines for reading single character data as well as a higher level interface (SLkp) which utilize S-Lang's keymap facility for reading multi-character sequences. To initialize the interface, one must first call the function SLang_init_tty. Before exiting the program, the function SLang_reset_tty must be called to restore the keyboard interface to its original state. Once initialized, the low-level SLang_getkey function may be used to read simgle keyboard characters from the terminal. An application using the higher-level SLkp interface will read charcters using the SLkp_getkey function. In addition to these basic functions, there are also functions to ``unget'' keyboard characters, flush the input, detect pending-input with a timeout, etc. These functions are defined below. 4.1. Initializing the Keyboard Interface The function SLang_init_tty must be called to initialize the terminal for single character input. This puts the terminal in a mode usually referred to as ``raw'' mode. The prototype for the function is: int SLang_init_tty (int abort_char, int flow_ctrl, int opost); It takes three parameters that are used to specify how the terminal is to be initialized. The first parameter, abort_char, is used to specify the interrupt character (SIGINT). Under MSDOS, this value corresponds to the scan code of the character that will be used to generate the interrupt. For example, under MSDOS, 34 should be used to make Ctrl-G generate an interrupt signal since 34 is the scan code for G. On other systems, the value of abort_char will simply be the ascii value of the control character that will be used to generate the interrupt signal, e.g., 7 for Ctrl-G. If -1 is passed, the interrupt character will not be changed. Pressing the interrupt character specified by the first argument will generate a signal (SIGINT) that may or not be caught by the application. It is up to the application to catch this signal. S- Lang provides the function Slang_set_abort_signal to make it easy to facilitate this task. The second parameter is used to specify whether or not flow control should be used. If this parameter is zero, flow control is enabled otherwise it is disabled. Disabling flow control is necessary to pass certain characters to the application (e.g., Ctrl-S and Ctrl-Q). For some systems such as MSDOS, this parameter is meaningless. The third parameter, opost, is used to turn output processing on or off. If opost is zero, output processing is not turned on otherwise, output processing is turned on. The SLang_init_tty function returns -1 upon failure. In addition, after it returns, the S-Lang global variable SLang_TT_Baud_Rate will be set to the baud rate of the terminal if this value can be determined. Example: if (-1 == SLang_init_tty (7, 0, 0)) /* For MSDOS, use 34 as scan code */ { fprintf (stderr, "Unable to initialize the terminal.\n"); exit (1); } SLang_set_abort_signal (NULL); Here the terminal is initialized such that flow control and output processing are turned off. In addition, the character Ctrl-G (-- For MSDOS systems, use the scan code 34 instead of 7 for Ctrl-G--) has been specified to be the interrupt character. The function SLang_set_abort_signal is used to install the default S-Lang interrupt signal handler. 4.2. Resetting the Keyboard Interface The function SLang_reset_tty must be called to reset the terminal to the state it was in before the call to SLang_init_tty. The prototype for this function is: void SLang_reset_tty (void); Usually this function is only called before the program exits. How- ever, if the program is suspended it should also be called just before suspension. 4.3. Initializing the SLkp Routines Extra initialization of the higher-level SLkp functions are required because they are layered on top of the lower level routines. Since the SLkp_getkey function is able to process function and arrow keys in a terminal independent manner, it is necessary to call the SLtt_get_terminfo function to get information about the escape character sequences that the terminal's function keys send. Once that information is available, the SLkp_init function can construct the proper keymaps to process the escape sequences. This part of the initialization process for an application using this interface will look something like: SLtt_get_terminfo (); if (-1 == SLkp_init ()) { SLang_doerror ("SLkp_init failed."); exit (1); } if (-1 == SLang_init_tty (-1, 0, 1)) { SLang_doerror ("SLang_init_tty failed."); exit (1); } It is important to check the return status of the SLkp_init function which can failed if it cannot allocate enough memory for the keymap. 4.4. Setting the Interrupt Handler The function SLang_set_abort_signal may be used to associate an interrupt handler with the interrupt character that was previously specified by the SLang_init_tty function call. The prototype for this function is: void SLang_set_abort_signal (void (*)(int)); This function returns nothing and takes a single parameter which is a pointer to a function taking an integer value and returning void. If a NULL pointer is passed, the default S-Lang interrupt handler will be used. The S-Lang default interrupt handler under Unix looks like: static void default_sigint (int sig) { SLsignal_intr (SIGINT, default_sigint); SLKeyBoard_Quit = 1; if (SLang_Ignore_User_Abort == 0) SLang_Error = USER_BREAK; } It simply sets the global variable SLKeyBoard_Quit to one and if the variable SLang_Ignore_User_Abort is non-zero, SLang_Error is set to indicate a user break condition. (The function SLsignal_intr is simi- lar to the standard C signal function except that it will interrupt system calls. Some may not like this behavior and may wish to call this SLang_set_abort_signal with a different handler.) Although the function expressed above is specific to Unix, the analogous routines for other operating systems are equivalent in functionality even though the details of the implementation may vary drastically (e.g., under MSDOS, the hardware keyboard interrupt int 9h is hooked). 4.5. Reading Keyboard Input with SLang_getkey After initializing the keyboard via SLang_init_tty, the S-Lang function SLang_getkey may be used to read characters from the terminal interface. In addition, the function SLang_input_pending may be used to determine whether or not keyboard input is available to be read. These functions have prototypes: unsigned int SLang_getkey (void); int SLang_input_pending (int tsecs); The SLang_getkey function returns a single character from the termi- nal. Upon failure, it returns 0xFFFF. If the interrupt character specified by the SLang_init_tty function is pressed while this func- tion is called, the function will return the value of the interrupt character and set the S-Lang global variable SLKeyBoard_Quit to a non- zero value. In addition, if the default S-Lang interrupt handler has been specified by a NULL argument to the SLang_set_abort_signal func- tion, the global variable SLang_Error will be set to USER_BREAK unless the variable SLang_Ignore_User_Abort is non-zero. The SLang_getkey function waits until input is available to be read. The SLang_input_pending function may be used to determine whether or not input is ready. It takes a single parameter that indicates the amount of time to wait for input before returning with information regarding the availability of input. This parameter has units of one tenth (1/10) of a second, i.e., to wait one second, the value of the parameter should be 10. Passing a value of zero causes the function to return right away. SLang_input_pending returns a positive integer if input is available or zero if input is not available. It will return -1 if an error occurs. Here is a simple example that reads keys from the terminal until one presses Ctrl-G or until 5 seconds have gone by with no input: #include <stdio.h> #include "slang.h" int main () { int abort_char = 7; /* For MSDOS, use 34 as scan code */ unsigned int ch; if (-1 == SLang_init_tty (abort_char, 0, 1)) { fprintf (stderr, "Unable to initialize the terminal.\n"); exit (-1); } SLang_set_abort_signal (NULL); while (1) { fputs ("\nPress any key. To quit, press Ctrl-G: ", stdout); fflush (stdout); if (SLang_input_pending (50) == 0) /* 50/10 seconds */ { fputs ("Waited too long! Bye\n", stdout); break; } ch = SLang_getkey (); if (SLang_Error == USER_BREAK) { fputs ("Ctrl-G pressed! Bye\n", stdout); break; } putc ((int) ch, stdout); } SLang_reset_tty (); return 0; } 4.6. Reading Keyboard Input with SLkp_getkey Unlike the low-level function SLang_getkey, the SLkp_getkey function can read a multi-character sequence associated with function keys. The SLkp_getkey function uses SLang_getkey and S-Lang's keymap facility to process escape sequences. It returns a single integer which describes the key that was pressed: int SLkp_getkey (void); That is, the SLkp_getkey function simple provides a mapping between keys and integers. In this context the integers are called keysyms. For single character input such as generated by the a key on the keyboard, the function returns the character that was generated, e.g., 'a'. For single characters, SLkp_getkey will always return an keysym whose value ranges from 0 to 256. For keys that generate multiple character sequences, e.g., a function or arrow key, the function returns an keysym whose value is greater that 256. The actual values of these keysyms are represented as macros defined in the slang.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -