xorp

keytab.h

00001 #ifndef keytab_h
00002 #define keytab_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 #include "libtecla.h"
00036 #include "hash.h"
00037 #include "strngmem.h"
00038 
00039 /*-----------------------------------------------------------------------*
00040  * This module defines a binary-search symbol table of key-bindings.     *
00041  *-----------------------------------------------------------------------*/
00042 
00043 /*
00044  * All key-binding functions are defined as follows.
00045  *
00046  * Input:
00047  *  gl    GetLine *  The resource object of this library.
00048  *  count     int    A positive repeat count specified by the user,
00049  *                   or 1. Action functions should ignore this if
00050  *                   repeating the action multiple times isn't
00051  *                   appropriate.
00052  * Output:
00053  *  return    int    0 - OK.
00054  *                   1 - Error.
00055  */
00056 #define KT_KEY_FN(fn) int (fn)(GetLine *gl, int count)
00057 
00058 typedef KT_KEY_FN(KtKeyFn);
00059 
00060 /*
00061  * Define an entry of a key-binding binary symbol table.
00062  */
00063 typedef struct {
00064   char *keyseq;         /* The key sequence that triggers the macro */
00065   int nc;               /* The number of characters in keyseq[] */
00066   KtKeyFn *user_fn;     /* A user specified binding (or 0 if none) */
00067   KtKeyFn *term_fn;     /* A terminal-specific binding (or 0 if none) */
00068   KtKeyFn *norm_fn;     /* The default binding (or 0 if none) */
00069   KtKeyFn *keyfn;       /* The function to execute when this key sequence */
00070                         /*  is seen. This is the function above which has */
00071                         /*  the highest priority. */
00072 } KeySym;
00073 
00074 /*
00075  * When allocating or reallocating the key-binding table, how
00076  * many entries should be added?
00077  */
00078 #define KT_TABLE_INC 100
00079 
00080 /*
00081  * Define the size of the hash table that is used to associate action
00082  * names with action functions. This should be a prime number.
00083  */
00084 #define KT_HASH_SIZE 113
00085 
00086 /*
00087  * Define a binary-symbol-table object.
00088  */
00089 typedef struct {
00090   int size;           /* The allocated dimension of table[] */
00091   int nkey;           /* The current number of members in the table */
00092   KeySym *table;      /* The table of lexically sorted key sequences */
00093   HashTable *actions; /* The hash table of actions */
00094   StringMem *smem;    /* Memory for allocating strings */
00095 } KeyTab;
00096 
00097 KeyTab *_new_KeyTab(void);
00098 KeyTab *_del_KeyTab(KeyTab *kt);
00099 
00100 /*
00101  * Enumerate the possible sources of key-bindings.
00102  */
00103 typedef enum {
00104   KTB_USER,         /* This is a binding being set by the user */
00105   KTB_TERM,         /* This is a binding taken from the terminal settings */
00106   KTB_NORM          /* This is the default binding set by the library */
00107 } KtBinder;
00108 
00109 int _kt_set_keybinding(KeyTab *kt, KtBinder binder,
00110                const char *keyseq, const char *action);
00111 int _kt_set_keyfn(KeyTab *kt, KtBinder binder, const char *keyseq,
00112           KtKeyFn *keyfn);
00113 
00114 int _kt_set_action(KeyTab *kt, const char *action, KtKeyFn *fn);
00115 
00116 typedef enum {
00117   KT_EXACT_MATCH,   /* An exact match was found */
00118   KT_AMBIG_MATCH,   /* An ambiguous match was found */
00119   KT_NO_MATCH,      /* No match was found */
00120   KT_BAD_MATCH      /* An error occurred while searching */
00121 } KtKeyMatch;
00122 
00123 KtKeyMatch _kt_lookup_keybinding(KeyTab *kt, const char *binary_keyseq,
00124                  int nc, int *first,int *last);
00125 
00126 /*
00127  * Remove all key bindings that came from a specified source.
00128  */
00129 void _kt_clear_bindings(KeyTab *kt, KtBinder binder);
00130 
00131 /*
00132  * When installing an array of keybings each binding is defined by
00133  * an element of the following type:
00134  */
00135 typedef struct {
00136   const char *keyseq;   /* The sequence of keys that trigger this binding */
00137   const char *action;   /* The name of the action function that is triggered */
00138 } KtKeyBinding;
00139 
00140 /*
00141  * Merge an array of bindings with existing bindings.
00142  */
00143 int _kt_add_bindings(KeyTab *kt, KtBinder binder, const KtKeyBinding *bindings,
00144              unsigned n);
00145 
00146 /*
00147  * Convert a keybinding string into a uniq binary representation.
00148  */
00149 int _kt_parse_keybinding_string(const char *keyseq,
00150                 char *binary, int *nc);
00151 
00152 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations