|
xorp
|
00001 #ifndef libtecla_h 00002 #define libtecla_h 00003 00004 /* 00005 * Copyright (c) 2000, 2001 by Martin C. Shepherd. 00006 * 00007 * All rights reserved. 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining a 00010 * copy of this software and associated documentation files (the 00011 * "Software"), to deal in the Software without restriction, including 00012 * without limitation the rights to use, copy, modify, merge, publish, 00013 * distribute, and/or sell copies of the Software, and to permit persons 00014 * to whom the Software is furnished to do so, provided that the above 00015 * copyright notice(s) and this permission notice appear in all copies of 00016 * the Software and that both the above copyright notice(s) and this 00017 * permission notice appear in supporting documentation. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00020 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00021 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00022 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00023 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00024 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00025 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00026 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00027 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00028 * 00029 * Except as contained in this notice, the name of a copyright holder 00030 * shall not be used in advertising or otherwise to promote the sale, use 00031 * or other dealings in this Software without prior written authorization 00032 * of the copyright holder. 00033 */ 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 #include <stdio.h> /* FILE * */ 00040 #include <stdlib.h> /* size_t */ 00041 #include <time.h> /* time_t */ 00042 00043 /* 00044 * The following are the three components of the libtecla version number. 00045 * Note that it is better to use the libtecla_version() function than these 00046 * macros since the macros only tell you which version of the library your 00047 * code was compiled against, whereas the libtecla_version() function 00048 * tells you which version of the shared tecla library your program is 00049 * actually linked to. 00050 */ 00051 #define TECLA_MAJOR_VER 1 00052 #define TECLA_MINOR_VER 4 00053 #define TECLA_MICRO_VER 0 00054 00055 /*....................................................................... 00056 * Query the version number of the tecla library. 00057 * 00058 * Input: 00059 * major int * The major version number of the library 00060 * will be assigned to *major. This number is 00061 * only incremented when a change to the library is 00062 * made that breaks binary (shared library) and/or 00063 * compilation backwards compatibility. 00064 * minor int * The minor version number of the library 00065 * will be assigned to *minor. This number is 00066 * incremented whenever new functions are added to 00067 * the public API. 00068 * micro int * The micro version number of the library will be 00069 * assigned to *micro. This number is incremented 00070 * whenever internal changes are made that don't 00071 * change the public API, such as bug fixes and 00072 * performance enhancements. 00073 */ 00074 void libtecla_version(int *major, int *minor, int *micro); 00075 00076 /*----------------------------------------------------------------------- 00077 * The getline module provides interactive command-line input, recall 00078 * and editing by users at terminals. See the gl_getline(3) man page for 00079 * more details. 00080 *-----------------------------------------------------------------------*/ 00081 00082 /* 00083 * Provide an opaque handle for the resource object that is defined in 00084 * getline.h. 00085 */ 00086 typedef struct GetLine GetLine; 00087 00088 /* 00089 * The following two functions are used to create and delete the 00090 * resource objects that are used by the gl_getline() function. 00091 */ 00092 GetLine *new_GetLine(size_t linelen, size_t histlen); 00093 GetLine *del_GetLine(GetLine *gl); 00094 00095 /* 00096 * Read a line into an internal buffer of gl. 00097 */ 00098 char *gl_get_line(GetLine *gl, const char *prompt, const char *start_line, 00099 int start_pos); 00100 00101 /* 00102 * Configure the application specific and/or user-specific behavior of 00103 * gl_get_line(). 00104 */ 00105 int gl_configure_getline(GetLine *gl, const char *app_string, 00106 const char *app_file, const char *user_file); 00107 00108 /* 00109 * Read a line from the network into an internal buffer of gl. 00110 */ 00111 char *gl_get_line_net(GetLine *gl, const char *prompt, const char *start_line, 00112 int start_pos, int val); 00113 00114 /* 00115 * Return the current position of the cursor within the internal buffer, 00116 * and the cursor position on the terminal. 00117 */ 00118 int gl_is_net(GetLine *gl); 00119 void gl_set_is_net(GetLine *gl, int is_net); 00120 int gl_get_buff_curpos(const GetLine *gl); 00121 int gl_place_cursor(GetLine *gl, int buff_curpos); 00122 int gl_redisplay_line(GetLine *gl); 00123 int gl_reset_line(GetLine *gl); 00124 int gl_get_user_event(GetLine *gl); 00125 void gl_reset_user_event(GetLine *gl); 00126 const char *gl_get_key_binding_action_name(GetLine *gl, const char *keyseq); 00127 00128 /*----------------------------------------------------------------------- 00129 * The file-expansion module provides facilities for expanding ~user/ and 00130 * $envvar expressions, and for expanding glob-style wildcards. 00131 * See the ef_expand_file(3) man page for more details. 00132 *-----------------------------------------------------------------------*/ 00133 00134 /* 00135 * ExpandFile objects contain the resources needed to expand pathnames. 00136 */ 00137 typedef struct ExpandFile ExpandFile; 00138 00139 /* 00140 * The following functions are used to create and delete the resource 00141 * objects that are used by the ef_expand_file() function. 00142 */ 00143 ExpandFile *new_ExpandFile(void); 00144 ExpandFile *del_ExpandFile(ExpandFile *ef); 00145 00146 /* 00147 * A container of the following type is returned by ef_expand_file(). 00148 */ 00149 typedef struct { 00150 int exists; /* True if the files in files[] currently exist. */ 00151 /* This only time that this may not be true is if */ 00152 /* the input filename didn't contain any wildcards */ 00153 /* and thus wasn't matched against existing files. */ 00154 /* In this case the single entry in 'nfile' may not */ 00155 /* refer to an existing file. */ 00156 int nfile; /* The number of files in files[] */ 00157 char **files; /* An array of 'nfile' filenames. */ 00158 } FileExpansion; 00159 00160 /* 00161 * The ef_expand_file() function expands a specified pathname, converting 00162 * ~user/ and ~/ patterns at the start of the pathname to the 00163 * corresponding home directories, replacing $envvar with the value of 00164 * the corresponding environment variable, and then, if there are any 00165 * wildcards, matching these against existing filenames. 00166 * 00167 * If no errors occur, a container is returned containing the array of 00168 * files that resulted from the expansion. If there were no wildcards 00169 * in the input pathname, this will contain just the original pathname 00170 * after expansion of ~ and $ expressions. If there were any wildcards, 00171 * then the array will contain the files that matched them. Note that 00172 * if there were any wildcards but no existing files match them, this 00173 * is counted as an error and NULL is returned. 00174 * 00175 * The supported wildcards and their meanings are: 00176 * * - Match any sequence of zero or more characters. 00177 * ? - Match any single character. 00178 * [chars] - Match any single character that appears in 'chars'. 00179 * If 'chars' contains an expression of the form a-b, 00180 * then any character between a and b, including a and b, 00181 * matches. The '-' character looses its special meaning 00182 * as a range specifier when it appears at the start 00183 * of the sequence of characters. 00184 * [^chars] - The same as [chars] except that it matches any single 00185 * character that doesn't appear in 'chars'. 00186 * 00187 * Wildcard expressions are applied to individual filename components. 00188 * They don't match across directory separators. A '.' character at 00189 * the beginning of a filename component must also be matched 00190 * explicitly by a '.' character in the input pathname, since these 00191 * are UNIX's hidden files. 00192 * 00193 * Input: 00194 * fe ExpandFile * The pathname expansion resource object. 00195 * path const char * The path name to be expanded. 00196 * pathlen int The length of the suffix of path[] that 00197 * constitutes the filename to be expanded, 00198 * or -1 to specify that the whole of the 00199 * path string should be used. 00200 * Output: 00201 * return FileExpansion * A pointer to a results container within the 00202 * given ExpandFile object. This contains an 00203 * array of the pathnames that resulted from 00204 * expanding ~ and $ expressions and from 00205 * matching any wildcards, sorted into lexical 00206 * order. 00207 * 00208 * This container and its contents will be 00209 * recycled on subsequent calls, so if you need 00210 * to keep the results of two successive runs, 00211 * you will either have to allocate a private 00212 * copy of the array, or use two ExpandFile 00213 * objects. 00214 * 00215 * On error, NULL is returned. A description 00216 * of the error can be acquired by calling the 00217 * ef_last_error() function. 00218 */ 00219 FileExpansion *ef_expand_file(ExpandFile *ef, const char *path, int pathlen); 00220 00221 /*....................................................................... 00222 * Print out an array of matching files. 00223 * 00224 * Input: 00225 * result FileExpansion * The container of the sorted array of 00226 * expansions. 00227 * fp FILE * The output stream to write to. 00228 * term_width int The width of the terminal. 00229 * Output: 00230 * return int 0 - OK. 00231 * 1 - Error. 00232 */ 00233 int ef_list_expansions(FileExpansion *result, FILE *fp, int term_width); 00234 00235 /* 00236 * The ef_last_error() function returns a description of the last error 00237 * that occurred in a call ef_expand_file(). Note that this message is 00238 * contained in an array which is allocated as part of *ef, and its 00239 * contents thus potentially change on every call to ef_expand_file(). 00240 */ 00241 const char *ef_last_error(ExpandFile *ef); 00242 00243 /*----------------------------------------------------------------------- 00244 * The WordCompletion module is used for completing incomplete words, such 00245 * as filenames. Programs can use functions within this module to register 00246 * their own customized completion functions. 00247 *-----------------------------------------------------------------------*/ 00248 00249 /* 00250 * Ambiguous completion matches are recorded in objects of the 00251 * following type. 00252 */ 00253 typedef struct WordCompletion WordCompletion; 00254 00255 /* 00256 * Create a new completion object. 00257 */ 00258 WordCompletion *new_WordCompletion(void); 00259 00260 /* 00261 * Delete a redundant completion object. 00262 */ 00263 WordCompletion *del_WordCompletion(WordCompletion *cpl); 00264 00265 /*....................................................................... 00266 * Callback functions declared and prototyped using the following macro 00267 * are called upon to return an array of possible completion suffixes 00268 * for the token that precedes a specified location in the given 00269 * input line. It is up to this function to figure out where the token 00270 * starts, and to call cpl_add_completion() to register each possible 00271 * completion before returning. 00272 * 00273 * Input: 00274 * cpl WordCompletion * An opaque pointer to the object that will 00275 * contain the matches. This should be filled 00276 * via zero or more calls to cpl_add_completion(). 00277 * data void * The anonymous 'data' argument that was 00278 * passed to cpl_complete_word() or 00279 * gl_customize_completion()). 00280 * line const char * The current input line. 00281 * word_end int The index of the character in line[] which 00282 * follows the end of the token that is being 00283 * completed. 00284 * Output 00285 * return int 0 - OK. 00286 * 1 - Error. 00287 */ 00288 #define CPL_MATCH_FN(fn) int (fn)(WordCompletion *cpl, void *data, \ 00289 const char *line, int word_end) 00290 typedef CPL_MATCH_FN(CplMatchFn); 00291 00292 /*....................................................................... 00293 * Optional callback functions declared and prototyped using the 00294 * following macro are called upon to return non-zero if a given 00295 * file, specified by its pathname, is to be included in a list of 00296 * completions. 00297 * 00298 * Input: 00299 * data void * The application specified pointer which 00300 * was specified when this callback function 00301 * was registered. This can be used to have 00302 * anything you like passed to your callback. 00303 * pathname const char * The pathname of the file to be checked to 00304 * see if it should be included in the list 00305 * of completions. 00306 * Output 00307 * return int 0 - Ignore this file. 00308 * 1 - Do include this file in the list 00309 * of completions. 00310 */ 00311 #define CPL_CHECK_FN(fn) int (fn)(void *data, const char *pathname) 00312 typedef CPL_CHECK_FN(CplCheckFn); 00313 00314 /* 00315 * You can use the following CplCheckFn callback function to only 00316 * have executables included in a list of completions. 00317 */ 00318 CPL_CHECK_FN(cpl_check_exe); 00319 00320 /* 00321 * cpl_file_completions() is the builtin filename completion callback 00322 * function. This can also be called by your own custom CPL_MATCH_FN() 00323 * callback functions. To do this pass on all of the arguments of your 00324 * custom callback function to cpl_file_completions(), with the exception 00325 * of the (void *data) argument. The data argument should either be passed 00326 * NULL to request the default behaviour of the file-completion function, 00327 * or be passed a pointer to a CplFileConf structure (see below). In the 00328 * latter case the contents of the structure modify the behavior of the 00329 * file-completer. 00330 */ 00331 CPL_MATCH_FN(cpl_file_completions); 00332 00333 /* 00334 * Objects of the following type can be used to change the default 00335 * behavior of the cpl_file_completions() callback function. 00336 */ 00337 typedef struct CplFileConf CplFileConf; 00338 00339 /* 00340 * If you want to change the behavior of the cpl_file_completions() 00341 * callback function, call the following function to allocate a 00342 * configuration object, then call one or more of the subsequent 00343 * functions to change any of the default configuration parameters 00344 * that you don't want. This function returns NULL when there is 00345 * insufficient memory. 00346 */ 00347 CplFileConf *new_CplFileConf(void); 00348 00349 /* 00350 * If backslashes in the prefix being passed to cpl_file_completions() 00351 * should be treated as literal characters, call the following function 00352 * with literal=1. Otherwise the default is to treat them as escape 00353 * characters which remove the special meanings of spaces etc.. 00354 */ 00355 void cfc_literal_escapes(CplFileConf *cfc, int literal); 00356 00357 /* 00358 * Before calling cpl_file_completions(), call this function if you 00359 * know the index at which the filename prefix starts in the input line. 00360 * Otherwise by default, or if you specify start_index to be -1, the 00361 * filename is taken to start after the first unescaped space preceding 00362 * the cursor, or the start of the line, which ever comes first. 00363 */ 00364 void cfc_file_start(CplFileConf *cfc, int start_index); 00365 00366 /* 00367 * If you only want certain types of files to be included in the 00368 * list of completions, use the following function to specify a 00369 * callback function which will be called to ask whether a given file 00370 * should be included. The chk_data argument is will be passed to the 00371 * callback function whenever it is called and can be anything you want. 00372 */ 00373 void cfc_set_check_fn(CplFileConf *cfc, CplCheckFn *chk_fn, void *chk_data); 00374 00375 /* 00376 * The following function deletes a CplFileConf objects previously 00377 * returned by new_CplFileConf(). It always returns NULL. 00378 */ 00379 CplFileConf *del_CplFileConf(CplFileConf *cfc); 00380 00381 /* 00382 * The following configuration structure is deprecated. Do not change 00383 * its contents, since this will break any programs that still use it, 00384 * and don't use it in new programs. Instead use opaque CplFileConf 00385 * objects as described above. cpl_file_completions() figures out 00386 * what type of structure you pass it, by virtue of a magic int code 00387 * placed at the start of CplFileConf object by new_CplFileConf(). 00388 */ 00389 typedef struct { 00390 int escaped; /* Opposite to the argument of cfc_literal_escapes() */ 00391 int file_start; /* Equivalent to the argument of cfc_file_start() */ 00392 } CplFileArgs; 00393 /* 00394 * This initializes the deprecated CplFileArgs structures. 00395 */ 00396 void cpl_init_FileArgs(CplFileArgs *cfa); 00397 00398 /*....................................................................... 00399 * When an error occurs while performing a completion, custom completion 00400 * callback functions should register a terse description of the error 00401 * by calling cpl_record_error(). This message will then be returned on 00402 * the next call to cpl_last_error() and used by getline to display an 00403 * error message to the user. 00404 * 00405 * Input: 00406 * cpl WordCompletion * The string-completion resource object that was 00407 * originally passed to the callback. 00408 * errmsg const char * The description of the error. 00409 */ 00410 void cpl_record_error(WordCompletion *cpl, const char *errmsg); 00411 00412 /*....................................................................... 00413 * This function can be used to replace the builtin filename-completion 00414 * function with one of the user's choice. The user's completion function 00415 * has the option of calling the builtin filename-completion function 00416 * if it believes that the token that it has been presented with is a 00417 * filename (see cpl_file_completions() above). 00418 * 00419 * Input: 00420 * gl GetLine * The resource object of the command-line input 00421 * module. 00422 * data void * This is passed to match_fn() whenever it is 00423 * called. It could, for example, point to a 00424 * symbol table that match_fn() would look up 00425 * matches in. 00426 * match_fn CplMatchFn * The function that will identify the prefix 00427 * to be completed from the input line, and 00428 * report matching symbols. 00429 * Output: 00430 * return int 0 - OK. 00431 * 1 - Error. 00432 */ 00433 int gl_customize_completion(GetLine *gl, void *data, CplMatchFn *match_fn); 00434 00435 /*....................................................................... 00436 * Change the terminal (or stream) that getline interacts with. 00437 * 00438 * Input: 00439 * gl GetLine * The resource object of the command-line input 00440 * module. 00441 * input_fp FILE * The stdio stream to read from. 00442 * output_fp FILE * The stdio stream to write to. 00443 * term const char * The terminal type. This can be NULL if 00444 * either or both of input_fp and output_fp don't 00445 * refer to a terminal. Otherwise it should refer 00446 * to an entry in the terminal information database. 00447 * Output: 00448 * return int 0 - OK. 00449 * 1 - Error. 00450 */ 00451 int gl_change_terminal(GetLine *gl, FILE *input_fp, FILE *output_fp, 00452 const char *term); 00453 00454 /*....................................................................... 00455 * The following functions can be used to save and restore the contents 00456 * of the history buffer. 00457 * 00458 * Input: 00459 * gl GetLine * The resource object of the command-line input 00460 * module. 00461 * filename const char * The name of the new file to write to. 00462 * comment const char * Extra information such as timestamps will 00463 * be recorded on a line started with this 00464 * string, the idea being that the file can 00465 * double as a command file. Specify "" if 00466 * you don't care. Be sure to specify the 00467 * same string to both functions. 00468 * max_lines int The maximum number of lines to save, or -1 00469 * to save all of the lines in the history 00470 * list. 00471 * Output: 00472 * return int 0 - OK. 00473 * 1 - Error. 00474 */ 00475 int gl_save_history(GetLine *gl, const char *filename, const char *comment, 00476 int max_lines); 00477 int gl_load_history(GetLine *gl, const char *filename, const char *comment); 00478 00479 /* 00480 * Enumerate file-descriptor events that can be waited for. 00481 */ 00482 typedef enum { 00483 GLFD_READ, /* Watch for data waiting to be read from a file descriptor */ 00484 GLFD_WRITE, /* Watch for ability to write to a file descriptor */ 00485 GLFD_URGENT /* Watch for urgent out-of-band data on the file descriptor */ 00486 } GlFdEvent; 00487 00488 /* 00489 * The following enumeration is used for the return status of file 00490 * descriptor event callbacks. 00491 */ 00492 typedef enum { 00493 GLFD_ABORT, /* Cause gl_get_line() to abort with an error */ 00494 GLFD_REFRESH, /* Redraw the input line and continue waiting for input */ 00495 GLFD_CONTINUE /* Continue to wait for input, without redrawing the line */ 00496 } GlFdStatus; 00497 00498 /*....................................................................... 00499 * While gl_get_line() is waiting for terminal input, it can also be 00500 * asked to listen for activity on arbitrary file descriptors. 00501 * Callback functions of the following type can be registered to be 00502 * called when activity is seen. If your callback needs to write to 00503 * the terminal or use signals, please see the gl_get_line(3) man 00504 * page. 00505 * 00506 * Input: 00507 * gl GetLine * The gl_get_line() resource object. You can use 00508 * this safely to call gl_watch_fd() or 00509 * gl_watch_time(). The effect of calling other 00510 * functions that take a gl argument is undefined, 00511 * and must be avoided. 00512 * data void * A pointer to arbitrary callback data, as originally 00513 * registered with gl_watch_fd(). 00514 * fd int The file descriptor that has activity. 00515 * event GlFdEvent The activity seen on the file descriptor. The 00516 * inclusion of this argument allows the same 00517 * callback to be registered for multiple events. 00518 * Output: 00519 * return GlFdStatus GLFD_ABORT - Cause gl_get_line() to abort with 00520 * an error (set errno if you need it). 00521 * GLFD_REFRESH - Redraw the input line and continue 00522 * waiting for input. Use this if you 00523 * wrote something to the terminal. 00524 * GLFD_CONTINUE - Continue to wait for input, without 00525 * redrawing the line. 00526 */ 00527 #define GL_FD_EVENT_FN(fn) GlFdStatus (fn)(GetLine *gl, void *data, int fd, \ 00528 GlFdEvent event) 00529 typedef GL_FD_EVENT_FN(GlFdEventFn); 00530 00531 /*....................................................................... 00532 * Where possible, register a function and associated data to be called 00533 * whenever a specified event is seen on a file descriptor. 00534 * 00535 * Input: 00536 * gl GetLine * The resource object of the command-line input 00537 * module. 00538 * fd int The file descriptor to watch. 00539 * event GlFdEvent The type of activity to watch for. 00540 * callback GlFdEventFn * The function to call when the specified 00541 * event occurs. Setting this to 0 removes 00542 * any existing callback. 00543 * data void * A pointer to arbitrary data to pass to the 00544 * callback function. 00545 * Output: 00546 * return int 0 - OK. 00547 * 1 - Either gl==NULL, or this facility isn't 00548 * available on the the host system 00549 * (ie. select() isn't available). No 00550 * error message is generated in the latter 00551 * case. 00552 */ 00553 int gl_watch_fd(GetLine *gl, int fd, GlFdEvent event, 00554 GlFdEventFn *callback, void *data); 00555 00556 /*....................................................................... 00557 * Switch history streams. History streams represent separate history 00558 * lists recorded within a single history buffer. Different streams 00559 * are distinguished by integer identifiers chosen by the calling 00560 * appplicaton. Initially new_GetLine() sets the stream identifier to 00561 * 0. Whenever a new line is appended to the history list, the current 00562 * stream identifier is recorded with it, and history lookups only 00563 * consider lines marked with the current stream identifier. 00564 * 00565 * Input: 00566 * gl GetLine * The resource object of gl_get_line(). 00567 * id unsigned The new history stream identifier. 00568 * Output: 00569 * return int 0 - OK. 00570 * 1 - Error. 00571 */ 00572 int gl_group_history(GetLine *gl, unsigned id); 00573 00574 /*....................................................................... 00575 * Display the contents of the history list. 00576 * 00577 * Input: 00578 * gl GetLine * The resource object of gl_get_line(). 00579 * fp FILE * The stdio output stream to write to. 00580 * fmt const char * A format string. This containing characters to be 00581 * written verbatim, plus any of the following 00582 * format directives: 00583 * %D - The date, formatted like 2001-11-20 00584 * %T - The time of day, formatted like 23:59:59 00585 * %N - The sequential entry number of the 00586 * line in the history buffer. 00587 * %G - The number of the history group that 00588 * the line belongs to. 00589 * %% - A literal % character. 00590 * %H - The history line itself. 00591 * Note that a '\n' newline character is not 00592 * appended by default. 00593 * all_groups int If true, display history lines from all 00594 * history groups. Otherwise only display 00595 * those of the current history group. 00596 * max_lines int If max_lines is < 0, all available lines 00597 * are displayed. Otherwise only the most 00598 * recent max_lines lines will be displayed. 00599 * Output: 00600 * return int 0 - OK. 00601 * 1 - Error. 00602 */ 00603 int gl_show_history(GetLine *gl, FILE *fp, const char *fmt, int all_groups, 00604 int max_lines); 00605 00606 /*....................................................................... 00607 * Resize or delete the history buffer. 00608 * 00609 * Input: 00610 * gl GetLine * The resource object of gl_get_line(). 00611 * bufsize size_t The number of bytes in the history buffer, or 0 00612 * to delete the buffer completely. 00613 * Output: 00614 * return int 0 - OK. 00615 * 1 - Insufficient memory (the previous buffer 00616 * will have been retained). No error message 00617 * will be displayed. 00618 */ 00619 int gl_resize_history(GetLine *gl, size_t bufsize); 00620 00621 /*....................................................................... 00622 * Set an upper limit to the number of lines that can be recorded in the 00623 * history list, or remove a previously specified limit. 00624 * 00625 * Input: 00626 * gl GetLine * The resource object of gl_get_line(). 00627 * max_lines int The maximum number of lines to allow, or -1 to 00628 * cancel a previous limit and allow as many lines 00629 * as will fit in the current history buffer size. 00630 */ 00631 void gl_limit_history(GetLine *gl, int max_lines); 00632 00633 /*....................................................................... 00634 * Discard either all historical lines, or just those associated with the 00635 * current history group. 00636 * 00637 * Input: 00638 * gl GetLine * The resource object of gl_get_line(). 00639 * all_groups int If true, clear all of the history. If false, 00640 * clear only the stored lines associated with the 00641 * currently selected history group. 00642 */ 00643 void gl_clear_history(GetLine *gl, int all_groups); 00644 00645 /*....................................................................... 00646 * Temporarily enable or disable the gl_get_line() history mechanism. 00647 * 00648 * Input: 00649 * gl GetLine * The resource object of gl_get_line(). 00650 * enable int If true, turn on the history mechanism. If 00651 * false, disable it. 00652 */ 00653 void gl_toggle_history(GetLine *gl, int enable); 00654 00655 /* 00656 * Objects of the following type are returned by gl_terminal_size(). 00657 */ 00658 typedef struct { 00659 int nline; /* The terminal has nline lines */ 00660 int ncolumn; /* The terminal has ncolumn columns */ 00661 } GlTerminalSize; 00662 00663 /*....................................................................... 00664 * Update if necessary, and return the current size of the terminal. 00665 * 00666 * Input: 00667 * gl GetLine * The resource object of gl_get_line(). 00668 * def_ncolumn int If the number of columns in the terminal 00669 * can't be determined, substitute this number. 00670 * def_nline int If the number of lines in the terminal can't 00671 * be determined, substitute this number. 00672 * Output: 00673 * return GlTerminalSize The current terminal size. 00674 */ 00675 GlTerminalSize gl_terminal_size(GetLine *gl, int def_ncolumn, int def_nline); 00676 00677 /* 00678 * The gl_lookup_history() function returns information in an 00679 * argument of the following type. 00680 */ 00681 typedef struct { 00682 const char *line; /* The requested history line */ 00683 unsigned group; /* The history group to which the */ 00684 /* line belongs. */ 00685 time_t timestamp; /* The date and time at which the */ 00686 /* line was originally entered. */ 00687 } GlHistoryLine; 00688 00689 /*....................................................................... 00690 * Lookup a history line by its sequential number of entry in the 00691 * history buffer. 00692 * 00693 * Input: 00694 * gl GetLine * The resource object of gl_get_line(). 00695 * id unsigned long The identification number of the line to 00696 * be returned, where 0 denotes the first line 00697 * that was entered in the history list, and 00698 * each subsequently added line has a number 00699 * one greater than the previous one. For 00700 * the range of lines currently in the list, 00701 * see the gl_range_of_history() function. 00702 * Input/Output: 00703 * line GlHistoryLine * A pointer to the variable in which to 00704 * return the details of the line. 00705 * Output: 00706 * return int 0 - The line is no longer in the history 00707 * list, and *line has not been changed. 00708 * 1 - The requested line can be found in 00709 * *line. Note that the string in 00710 * line->line is part of the history 00711 * buffer and will change, so a private 00712 * copy should be made if you wish to 00713 * use it after subsequent calls to any 00714 * functions that take gl as an argument. 00715 */ 00716 int gl_lookup_history(GetLine *gl, unsigned long id, GlHistoryLine *line); 00717 00718 /* 00719 * The gl_state_of_history() function returns information in an argument 00720 * of the following type. 00721 */ 00722 typedef struct { 00723 int enabled; /* True if history is enabled */ 00724 unsigned group; /* The current history group */ 00725 int max_lines; /* The current upper limit on the number of lines */ 00726 /* in the history list, or -1 if unlimited. */ 00727 } GlHistoryState; 00728 00729 /*....................................................................... 00730 * Query the state of the history list. Note that any of the input/output 00731 * pointers can be specified as NULL. 00732 * 00733 * Input: 00734 * gl GetLine * The resource object of gl_get_line(). 00735 * Input/Output: 00736 * state GlHistoryState * A pointer to the variable in which to record 00737 * the return values. 00738 */ 00739 void gl_state_of_history(GetLine *gl, GlHistoryState *state); 00740 00741 /* 00742 * The gl_range_of_history() function returns information in an argument 00743 * of the following type. 00744 */ 00745 typedef struct { 00746 unsigned long oldest; /* The sequential entry number of the oldest */ 00747 /* line in the history list. */ 00748 unsigned long newest; /* The sequential entry number of the newest */ 00749 /* line in the history list. */ 00750 int nlines; /* The number of lines in the history list */ 00751 } GlHistoryRange; 00752 00753 /*....................................................................... 00754 * Query the number and range of lines in the history buffer. 00755 * 00756 * Input: 00757 * gl GetLine * The resource object of gl_get_line(). 00758 * range GlHistoryRange * A pointer to the variable in which to record 00759 * the return values. If range->nline=0, the 00760 * range of lines will be given as 0-0. 00761 */ 00762 void gl_range_of_history(GetLine *gl, GlHistoryRange *range); 00763 00764 /* 00765 * The gl_size_of_history() function returns information in an argument 00766 * of the following type. 00767 */ 00768 typedef struct { 00769 size_t size; /* The size of the history buffer (bytes) */ 00770 size_t used; /* The number of bytes of the history buffer */ 00771 /* that are currently occupied. */ 00772 } GlHistorySize; 00773 00774 /*....................................................................... 00775 * Return the size of the history buffer and the amount of the 00776 * buffer that is currently in use. 00777 * 00778 * Input: 00779 * gl GetLine * The resource object of gl_get_line(). 00780 * Input/Output: 00781 * GlHistorySize size * A pointer to the variable in which to return 00782 * the results. 00783 */ 00784 void gl_size_of_history(GetLine *gl, GlHistorySize *size); 00785 00786 /*....................................................................... 00787 * Specify whether text that users type should be displayed or hidden. 00788 * In the latter case, only the prompt is displayed, and the final 00789 * input line is not archived in the history list. 00790 * 00791 * Input: 00792 * gl GetLine * The input-line history maintenance object. 00793 * enable int 0 - Disable echoing. 00794 * 1 - Enable echoing. 00795 * -1 - Just query the mode without changing it. 00796 * Output: 00797 * return int The echoing disposition that was in effect 00798 * before this function was called: 00799 * 0 - Echoing was disabled. 00800 * 1 - Echoing was enabled. 00801 */ 00802 int gl_echo_mode(GetLine *gl, int enable); 00803 00804 /*....................................................................... 00805 * This function can be called from gl_get_line() callbacks to have 00806 * the prompt changed when they return. It has no effect if gl_get_line() 00807 * is not currently being invoked. 00808 * 00809 * Input: 00810 * gl GetLine * The resource object of gl_get_line(). 00811 * prompt const char * The new prompt. 00812 */ 00813 void gl_replace_prompt(GetLine *gl, const char *prompt); 00814 00815 /* 00816 * Enumerate the available prompt formatting styles. 00817 */ 00818 typedef enum { 00819 GL_LITERAL_PROMPT, /* Display the prompt string literally */ 00820 GL_FORMAT_PROMPT /* The prompt string can contain any of the */ 00821 /* following formatting directives: */ 00822 /* %B - Display subsequent characters */ 00823 /* with a bold font. */ 00824 /* %b - Stop displaying characters */ 00825 /* with the bold font. */ 00826 /* %U - Underline subsequent characters. */ 00827 /* %u - Stop underlining characters. */ 00828 /* %S - Highlight subsequent characters */ 00829 /* (also known as standout mode). */ 00830 /* %s - Stop highlighting characters */ 00831 /* %% - Display a single % character. */ 00832 } GlPromptStyle; 00833 00834 /*....................................................................... 00835 * Specify whether to heed text attribute directives within prompt 00836 * strings. 00837 * 00838 * Input: 00839 * gl GetLine * The resource object of gl_get_line(). 00840 * style GlPromptStyle The style of prompt (see the definition of 00841 * GlPromptStyle in libtecla.h for details). 00842 */ 00843 void gl_prompt_style(GetLine *gl, GlPromptStyle style); 00844 00845 /*....................................................................... 00846 * Remove a signal from the list of signals that gl_get_line() traps. 00847 * 00848 * Input: 00849 * gl GetLine * The resource object of gl_get_line(). 00850 * signo int The number of the signal to be ignored. 00851 * Output: 00852 * return int 0 - OK. 00853 * 1 - Error. 00854 */ 00855 int gl_ignore_signal(GetLine *gl, int signo); 00856 00857 /* 00858 * A bitwise union of the following enumerators is passed to 00859 * gl_trap_signal() to specify the environment in which the 00860 * application's signal handler is to be called. 00861 */ 00862 typedef enum { 00863 GLS_RESTORE_SIG=1, /* Restore the caller's signal environment */ 00864 /* while handling the signal. */ 00865 GLS_RESTORE_TTY=2, /* Restore the caller's terminal settings */ 00866 /* while handling the signal. */ 00867 GLS_RESTORE_LINE=4, /* Move the cursor to the start of the next line */ 00868 GLS_REDRAW_LINE=8, /* Redraw the input line when the signal handler */ 00869 /* returns. */ 00870 GLS_UNBLOCK_SIG=16, /* Normally a signal who's delivery is found to */ 00871 /* be blocked by the calling application is not */ 00872 /* trapped by gl_get_line(). Including this flag */ 00873 /* causes it to be temporarily unblocked and */ 00874 /* trapped while gl_get_line() is executing. */ 00875 GLS_DONT_FORWARD=32,/* Don't forward the signal to the signal handler */ 00876 /* of the calling program. */ 00877 GLS_RESTORE_ENV = GLS_RESTORE_SIG | GLS_RESTORE_TTY | GLS_REDRAW_LINE, 00878 GLS_SUSPEND_INPUT = GLS_RESTORE_ENV | GLS_RESTORE_LINE 00879 } GlSignalFlags; 00880 00881 /* 00882 * The following enumerators are passed to gl_trap_signal() to tell 00883 * it what to do after the application's signal handler has been called. 00884 */ 00885 typedef enum { 00886 GLS_RETURN, /* Return the line as though the user had pressed the */ 00887 /* return key. */ 00888 GLS_ABORT, /* Cause gl_get_line() to return NULL */ 00889 GLS_CONTINUE /* After handling the signal, resume command line editing */ 00890 } GlAfterSignal; 00891 00892 /*....................................................................... 00893 * Tell gl_get_line() how to respond to a given signal. This can be used 00894 * both to override the default responses to signals that gl_get_line() 00895 * normally catches and to add new signals to the list that are to be 00896 * caught. 00897 * 00898 * Input: 00899 * gl GetLine * The resource object of gl_get_line(). 00900 * signo int The number of the signal to be caught. 00901 * flags unsigned A bitwise union of GlSignalFlags enumerators. 00902 * after GlAfterSignal What to do after the application's signal 00903 * handler has been called. 00904 * errno_value int The value to set errno to. 00905 * Output: 00906 * return int 0 - OK. 00907 * 1 - Insufficient memory to record the 00908 * new signal disposition. 00909 */ 00910 int gl_trap_signal(GetLine *gl, int signo, unsigned flags, 00911 GlAfterSignal after, int errno_value); 00912 00913 /*....................................................................... 00914 * Return the last signal that was caught by the most recent call to 00915 * gl_get_line(), or -1 if no signals were caught. This is useful if 00916 * gl_get_line() returns errno=EINTR and you need to find out what signal 00917 * caused it to abort. 00918 * 00919 * Input: 00920 * gl GetLine * The resource object of gl_get_line(). 00921 * Output: 00922 * return int The last signal caught by the most recent 00923 * call to gl_get_line(), or -1 if no signals 00924 * were caught. 00925 */ 00926 int gl_last_signal(const GetLine *gl); 00927 00928 /*....................................................................... 00929 * This function is designed to be called by CPL_MATCH_FN() callback 00930 * functions. It adds one possible completion of the token that is being 00931 * completed to an array of completions. If the completion needs any 00932 * special quoting to be valid when displayed in the input line, this 00933 * quoting must be included in the string. 00934 * 00935 * Input: 00936 * cpl WordCompletion * The argument of the same name that was passed 00937 * to the calling CPL_MATCH_FN() callback function. 00938 * line const char * The input line, as received by the callback 00939 * function. 00940 * word_start int The index within line[] of the start of the 00941 * word that is being completed. If an empty 00942 * string is being completed, set this to be 00943 * the same as word_end. 00944 * word_end int The index within line[] of the character which 00945 * follows the incomplete word, as received by the 00946 * callback function. 00947 * suffix const char * The appropriately quoted string that could 00948 * be appended to the incomplete token to complete 00949 * it. A copy of this string will be allocated 00950 * internally. 00951 * type_suffix const char * When listing multiple completions, gl_get_line() 00952 * appends this string to the completion to indicate 00953 * its type to the user. If not pertinent pass "". 00954 * Otherwise pass a literal or static string. 00955 * cont_suffix const char * If this turns out to be the only completion, 00956 * gl_get_line() will append this string as 00957 * a continuation. For example, the builtin 00958 * file-completion callback registers a directory 00959 * separator here for directory matches, and a 00960 * space otherwise. If the match were a function 00961 * name you might want to append an open 00962 * parenthesis, etc.. If not relevant pass "". 00963 * Otherwise pass a literal or static string. 00964 * Output: 00965 * return int 0 - OK. 00966 * 1 - Error. 00967 */ 00968 int cpl_add_completion(WordCompletion *cpl, const char *line, 00969 int word_start, int word_end, const char *suffix, 00970 const char *type_suffix, const char *cont_suffix); 00971 00972 /* 00973 * Each possible completion string is recorded in an array element of 00974 * the following type. 00975 */ 00976 typedef struct { 00977 char *completion; /* The matching completion string */ 00978 char *suffix; /* The pointer into completion[] at which the */ 00979 /* string was extended. */ 00980 const char *type_suffix; /* A suffix to be added when listing completions */ 00981 /* to indicate the type of the completion. */ 00982 } CplMatch; 00983 00984 /* 00985 * Completions are returned in a container of the following form. 00986 */ 00987 typedef struct { 00988 char *suffix; /* The common initial part of all of the */ 00989 /* completion suffixes. */ 00990 const char *cont_suffix; /* Optional continuation string to be appended to */ 00991 /* the sole completion when nmatch==1. */ 00992 CplMatch *matches; /* The array of possible completion strings, */ 00993 /* sorted into lexical order. */ 00994 int nmatch; /* The number of elements in matches[] */ 00995 } CplMatches; 00996 00997 /*....................................................................... 00998 * Given an input line and the point at which completion is to be 00999 * attempted, return an array of possible completions. 01000 * 01001 * Input: 01002 * cpl WordCompletion * The word-completion resource object. 01003 * line const char * The current input line. 01004 * word_end int The index of the character in line[] which 01005 * follows the end of the token that is being 01006 * completed. 01007 * data void * Anonymous 'data' to be passed to match_fn(). 01008 * match_fn CplMatchFn * The function that will identify the prefix 01009 * to be completed from the input line, and 01010 * record completion suffixes. 01011 * Output: 01012 * return CplMatches * The container of the array of possible 01013 * completions. The returned pointer refers 01014 * to a container owned by the parent Completion 01015 * object, and its contents thus potentially 01016 * change on every call to cpl_matches(). 01017 */ 01018 CplMatches *cpl_complete_word(WordCompletion *cpl, const char *line, 01019 int word_end, void *data, 01020 CplMatchFn *match_fn); 01021 01022 /*....................................................................... 01023 * Print out an array of matching completions. 01024 * 01025 * Input: 01026 * result CplMatches * The container of the sorted array of 01027 * completions. 01028 * fp FILE * The output stream to write to. 01029 * term_width int The width of the terminal. 01030 * Output: 01031 * return int 0 - OK. 01032 * 1 - Error. 01033 */ 01034 int cpl_list_completions(CplMatches *result, FILE *fp, int term_width); 01035 01036 /*....................................................................... 01037 * Return a description of the error that occurred on the last call to 01038 * cpl_complete_word() or cpl_add_completion(). 01039 * 01040 * Input: 01041 * cpl WordCompletion * The string-completion resource object. 01042 * Output: 01043 * return const char * The description of the last error. 01044 */ 01045 const char *cpl_last_error(WordCompletion *cpl); 01046 01047 /* 01048 * PathCache objects encapsulate the resources needed to record 01049 * files of interest from comma-separated lists of directories. 01050 */ 01051 typedef struct PathCache PathCache; 01052 01053 /*....................................................................... 01054 * Create an object who's function is to maintain a cache of filenames 01055 * found within a list of directories, and provide quick lookup and 01056 * completion of selected files in this cache. 01057 * 01058 * Output: 01059 * return PathCache * The new, initially empty cache, or NULL 01060 * on error. 01061 */ 01062 PathCache *new_PathCache(void); 01063 01064 /*....................................................................... 01065 * Delete a given cache of files, returning the resources that it 01066 * was using to the system. 01067 * 01068 * Input: 01069 * pc PathCache * The cache to be deleted (can be NULL). 01070 * Output: 01071 * return PathCache * The deleted object (ie. allways NULL). 01072 */ 01073 PathCache *del_PathCache(PathCache *pc); 01074 01075 /*....................................................................... 01076 * Return a description of the last path-caching error that occurred. 01077 * 01078 * Input: 01079 * pc PathCache * The filename cache that suffered the error. 01080 * Output: 01081 * return char * The description of the last error. 01082 */ 01083 const char *pca_last_error(PathCache *pc); 01084 01085 /*....................................................................... 01086 * Build the list of files of interest contained in a given 01087 * colon-separated list of directories. 01088 * 01089 * Input: 01090 * pc PathCache * The cache in which to store the names of 01091 * the files that are found in the list of 01092 * directories. 01093 * path const char * A colon-separated list of directory 01094 * paths. Under UNIX, when searching for 01095 * executables, this should be the return 01096 * value of getenv("PATH"). 01097 * Output: 01098 * return int 0 - OK. 01099 * 1 - An error occurred. 01100 */ 01101 int pca_scan_path(PathCache *pc, const char *path); 01102 01103 /*....................................................................... 01104 * If you want subsequent calls to pca_lookup_file() and 01105 * pca_path_completions() to only return the filenames of certain 01106 * types of files, for example executables, or filenames ending in 01107 * ".ps", call this function to register a file-selection callback 01108 * function. This callback function takes the full pathname of a file, 01109 * plus application-specific data, and returns 1 if the file is of 01110 * interest, and zero otherwise. 01111 * 01112 * Input: 01113 * pc PathCache * The filename cache. 01114 * check_fn CplCheckFn * The function to call to see if the name of 01115 * a given file should be included in the 01116 * cache. This determines what type of files 01117 * will reside in the cache. To revert to 01118 * selecting all files, regardless of type, 01119 * pass 0 here. 01120 * data void * You can pass a pointer to anything you 01121 * like here, including NULL. It will be 01122 * passed to your check_fn() callback 01123 * function, for its private use. 01124 */ 01125 void pca_set_check_fn(PathCache *pc, CplCheckFn *check_fn, void *data); 01126 01127 /*....................................................................... 01128 * Given the simple name of a file, search the cached list of files 01129 * in the order in which they where found in the list of directories 01130 * previously presented to pca_scan_path(), and return the pathname 01131 * of the first file which has this name. 01132 * 01133 * Input: 01134 * pc PathCache * The cached list of files. 01135 * name const char * The name of the file to lookup. 01136 * name_len int The length of the filename substring at the 01137 * beginning of name[], or -1 to assume that the 01138 * filename occupies the whole of the string. 01139 * literal int If this argument is zero, lone backslashes 01140 * in name[] are ignored during comparison 01141 * with filenames in the cache, under the 01142 * assumption that they were in the input line 01143 * soley to escape the special significance of 01144 * characters like spaces. To have them treated 01145 * as normal characters, give this argument a 01146 * non-zero value, such as 1. 01147 * Output: 01148 * return char * The pathname of the first matching file, 01149 * or NULL if not found. Note that the returned 01150 * pointer points to memory owned by *pc, and 01151 * will become invalid on the next call. 01152 */ 01153 char *pca_lookup_file(PathCache *pc, const char *name, int name_len, 01154 int literal); 01155 01156 /* 01157 * Objects of the following type can be used to change the default 01158 * behavior of the pca_path_completions() callback function. 01159 */ 01160 typedef struct PcaPathConf PcaPathConf; 01161 01162 /* 01163 * pca_path_completions() is a completion callback function for use directly 01164 * with cpl_complete_word() or gl_customize_completions(), or indirectly 01165 * from your own completion callback function. It requires that a PcaPathConf 01166 * object be passed via its 'void *data' argument (see below). 01167 */ 01168 CPL_MATCH_FN(pca_path_completions); 01169 01170 /*....................................................................... 01171 * Allocate and initialize a pca_path_completions() configuration object. 01172 * 01173 * Input: 01174 * pc PathCache * The filename cache in which to look for 01175 * file name completions. 01176 * Output: 01177 * return PcaPathConf * The new configuration structure, or NULL 01178 * on error. 01179 */ 01180 PcaPathConf *new_PcaPathConf(PathCache *pc); 01181 01182 /*....................................................................... 01183 * Deallocate memory, previously allocated by new_PcaPathConf(). 01184 * 01185 * Input: 01186 * ppc PcaPathConf * Any pointer previously returned by 01187 * new_PcaPathConf() [NULL is allowed]. 01188 * Output: 01189 * return PcaPathConf * The deleted structure (always NULL). 01190 */ 01191 PcaPathConf *del_PcaPathConf(PcaPathConf *ppc); 01192 01193 /* 01194 * If backslashes in the prefix being passed to pca_path_completions() 01195 * should be treated as literal characters, call the following function 01196 * with literal=1. Otherwise the default is to treat them as escape 01197 * characters which remove the special meanings of spaces etc.. 01198 */ 01199 void ppc_literal_escapes(PcaPathConf *ppc, int literal); 01200 01201 /* 01202 * Before calling pca_path_completions, call this function if you know 01203 * the index at which the filename prefix starts in the input line. 01204 * Otherwise by default, or if you specify start_index to be -1, the 01205 * filename is taken to start after the first unescaped space preceding 01206 * the cursor, or the start of the line, whichever comes first. 01207 */ 01208 void ppc_file_start(PcaPathConf *ppc, int start_index); 01209 01210 #ifdef __cplusplus 01211 } 01212 #endif 01213 01214 #endif