/*
* Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* to whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
* OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Except as contained in this notice, the name of a copyright holder
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* of the copyright holder.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "keytab.h"
#include "strngmem.h"
#include "getline.h"
#include "errmsg.h"
#include "hash.h"
/*
* When allocating or reallocating the key-binding table, how
* many entries should be added?
*/
/*
* Define the size of the hash table that is used to associate action
* names with action functions. This should be a prime number.
*/
/*
* Define a binary-symbol-table object.
*/
struct KeyTab {
};
static int _kt_parse_keybinding_string(const char *keyseq,
void *data);
static int _kt_is_emacs_meta(const char *string);
static int _kt_is_emacs_ctrl(const char *string);
/*.......................................................................
* Create a new key-binding symbol table.
*
* Output:
* return KeyTab * The new object, or NULL on error.
*/
{
/*
* Allocate the container.
*/
if(!kt) {
return NULL;
};
/*
* Before attempting any operation that might fail, initialize the
* container at least up to the point at which it can safely be passed
* to del_KeyTab().
*/
/*
* Allocate a place to record error messages.
*/
return _del_KeyTab(kt);
/*
* Allocate the table.
*/
return _del_KeyTab(kt);
};
/*
* Allocate a hash table of actions.
*/
return _del_KeyTab(kt);
/*
* Allocate a string allocation object. This allows allocation of
* small strings without fragmenting the heap.
*/
return _del_KeyTab(kt);
return kt;
}
/*.......................................................................
* Delete a KeyTab object.
*
* Input:
* kt KeyTab * The object to be deleted.
* Output:
* return KeyTab * The deleted object (always NULL).
*/
{
if(kt) {
};
return NULL;
}
/*.......................................................................
* Increase the size of the table to accomodate more keys.
*
* Input:
* kt KeyTab * The table to be extended.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
{
/*
* Attempt to increase the size of the table.
*/
/*
* Failed?
*/
if(!newtab) {
return 1;
};
/*
* Install the resized table.
*/
return 0;
}
/*.......................................................................
* Add, update or remove a keybinding to the table.
*
* Input:
* kt KeyTab * The table to add the binding to.
* binder KtBinder The source of the binding.
* keyseq const char * The key-sequence to bind.
* action char * The action to associate with the key sequence, or
* NULL to remove the action associated with the
* key sequence.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
const char *action)
{
/*
* Check arguments.
*/
if(kt)
return 1;
};
/*
* Lookup the function that implements the specified action.
*/
if(!action) {
keyfn = 0;
} else {
if(!sym) {
return 1;
};
};
/*
* Record the action in the table.
*/
}
/*.......................................................................
* Add, update or remove a keybinding to the table, specifying an action
* function directly.
*
* Input:
* kt KeyTab * The table to add the binding to.
* binder KtBinder The source of the binding.
* keyseq char * The key-sequence to bind.
* keyfn KtKeyFn * The action function, or NULL to remove any existing
* action function.
* data void * A pointer to anonymous data to be passed to keyfn
* whenever it is called.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
{
/* minimally match. */
int i;
/*
* Check arguments.
*/
if(kt)
return 1;
};
/*
* Work out a pessimistic estimate of how much space will be needed
* for the binary copy of the string, noting that binary meta characters
* embedded in the input string get split into two characters.
*/
/*
* Allocate a string that has the length of keyseq[].
*/
if(!binary) {
return 1;
};
/*
* Convert control and octal character specifications to binary characters.
*/
return 1;
};
/*
* Lookup the position in the table at which to insert the binding.
*/
/*
* If an exact match for the key-sequence is already in the table,
* simply replace its binding function (or delete the entry if
* the new binding is 0).
*/
case KT_EXACT_MATCH:
if(keyfn) {
} else {
};
break;
/*
* If an ambiguous match has been found and we are installing a
* callback, then our new key-sequence would hide all of the ambiguous
* matches, so we shouldn't allow it.
*/
case KT_AMBIG_MATCH:
if(keyfn) {
"\", because it is a prefix of another binding",
return 1;
};
break;
/*
* If the entry doesn't exist, create it.
*/
case KT_NO_MATCH:
/*
* Add a new binding?
*/
if(keyfn) {
/*
* We will need a new entry, extend the table if needed.
*/
if(_kt_extend_table(kt)) {
return 1;
};
};
/*
* Make space to insert the new key-sequence before 'last'.
*/
};
/*
* Insert the new binding in the vacated position.
*/
for(i=0; i<KTB_NBIND; i++) {
};
};
break;
case KT_BAD_MATCH:
return 1;
break;
};
return 0;
}
/*.......................................................................
* Perform a min-match lookup of a key-binding.
*
* Input:
* kt KeyTab * The keybinding table to lookup in.
* binary_keyseq char * The binary key-sequence to lookup.
* nc int the number of characters in keyseq[].
* first,last int * If there is an ambiguous or exact match, the indexes
* of the first and last symbols that minimally match
* will be assigned to *first and *last respectively.
* If there is no match, then first and last will
* bracket the location where the symbol should be
* inserted.
* Output:
* return KtKeyMatch One of the following enumerators:
* KT_EXACT_MATCH - An exact match was found.
* KT_AMBIG_MATCH - An ambiguous match was found.
* KT_NO_MATCH - No match was found.
* KT_BAD_MATCH - An error occurred while searching.
*/
{
/*
* Perform a binary search for the key-sequence.
*/
bot = 0;
binary_keyseq, nc);
if(test > 0)
else if(test < 0)
else {
return KT_EXACT_MATCH;
};
};
/*
* An exact match wasn't found, but top is the index just below the
* index where a match would be found, and bot is the index just above
* where the match ought to be found.
*/
/*
* See if any ambiguous matches exist, and if so make *first and *last
* refer to the first and last matches.
*/
(*last)++;
return KT_AMBIG_MATCH;
};
/*
* No match.
*/
return KT_NO_MATCH;
}
/*.......................................................................
* Lookup the sub-array of key-bindings who's key-sequences minimally
* match a given key-sequence.
*
* Input:
* kt KeyTab * The keybinding table to lookup in.
* binary_keyseq char * The binary key-sequence to lookup.
* nc int the number of characters in keyseq[].
* matches KeySym ** The array of minimally matching symbols
* can be found in (*matches)[0..nmatch-1], unless
* no match was found, in which case *matches will
* be set to NULL.
* nmatch int The number of ambiguously matching symbols. This
* will be 0 if there is no match, 1 for an exact
* match, and a number greater than 1 for an ambiguous
* match.
* Output:
* return KtKeyMatch One of the following enumerators:
* KT_EXACT_MATCH - An exact match was found.
* KT_AMBIG_MATCH - An ambiguous match was found.
* KT_NO_MATCH - No match was found.
* KT_BAD_MATCH - An error occurred while searching.
*/
{
/* in the symbol table. */
/*
* Check the arguments.
*/
if(kt)
return KT_BAD_MATCH;
};
/*
* Lookup the indexes of the binding-table entries that bracket the
* target key-sequence.
*/
/*
* Translate the indexes into the corresponding subarray of matching
* table entries.
*/
switch(status) {
case KT_EXACT_MATCH:
case KT_AMBIG_MATCH:
break;
default:
*nmatch = 0;
break;
};
return status;
}
/*.......................................................................
* Convert a keybinding string into a uniq binary representation.
*
* Control characters can be given directly in their binary form,
* expressed as either ^ or C-, followed by the character, expressed in
* octal, like \129 or via C-style backslash escapes, with the addition
* of '\E' to denote the escape key. Similarly, meta characters can be
* given directly in binary or expressed as M- followed by the character.
* Meta characters are recorded as two characters in the binary output
* string, the first being the escape key, and the second being the key
* that was modified by the meta key. This means that binding to
* \EA or ^[A or M-A are all equivalent.
*
* Input:
* keyseq char * The key sequence being added.
* binary char * The binary version of the key sequence will be
* assigned to binary[], which must have at least
* as many characters as keyseq[] plus the number
* of embedded binary meta characters.
* nc int * The number of characters assigned to binary[]
* will be recorded in *nc.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
int *nc)
{
char c; /* An intermediate character */
/*
* Parse the input characters until they are exhausted or the
* output string becomes full.
*/
while(*iptr) {
/*
* Check for special characters.
*/
switch(*iptr) {
case '^': /* A control character specification */
/*
* Convert the caret expression into the corresponding control
* character unless no character follows the caret, in which case
* record a literal caret.
*/
if(iptr[1]) {
/*
* Get the next, possibly escaped, character.
*/
} else {
c = iptr[1];
iptr += 2;
};
/*
* Convert the character to a control character.
*/
} else {
};
break;
/*
* A backslash-escaped character?
*/
case '\\':
/*
* Convert the escape sequence to a binary character.
*/
break;
/*
* Possibly an emacs-style meta character?
*/
case 'M':
if(_kt_is_emacs_meta(iptr)) {
*optr++ = GL_ESC_CHAR;
iptr += 2;
} else {
};
break;
/*
* Possibly an emacs-style control character specification?
*/
case 'C':
if(_kt_is_emacs_ctrl(iptr)) {
iptr += 3;
} else {
};
break;
default:
/*
* Convert embedded meta characters into an escape character followed
* by the meta-unmodified character.
*/
if(IS_META_CHAR(*iptr)) {
*optr++ = GL_ESC_CHAR;
iptr++;
/*
* To allow keysequences that start with printable characters to
* be distinguished from the cursor-key keywords, prepend a backslash
* to the former. This same operation is performed in gl_interpret_char()
* before looking up a keysequence that starts with a printable character.
*/
*optr++ = '\\';
} else {
};
};
};
/*
* How many characters were placed in the output array?
*/
return 0;
}
/*.......................................................................
* Add, remove or modify an action.
*
* Input:
* kt KeyTab * The key-binding table.
* action char * The name of the action.
* fn KtKeyFn * The function that implements the action, or NULL
* to remove an existing action.
* data void * A pointer to arbitrary callback data to pass to the
* action function whenever it is called.
* Output:
* return int 0 - OK.
* 1 - Error.
*/
{
/*
* Check the arguments.
*/
if(kt)
return 1;
};
/*
* If no function was provided, delete an existing action.
*/
if(!fn) {
return 0;
};
/*
* If the action already exists, replace its action function.
*/
if(sym) {
return 0;
};
/*
* Add a new action.
*/
return 1;
};
return 0;
}
/*.......................................................................
* Compare two strings of specified length which may contain embedded
* ascii NUL's.
*
* Input:
* s1 char * The first of the strings to be compared.
* n1 int The length of the string in s1.
* s2 char * The second of the strings to be compared.
* n2 int The length of the string in s2.
* Output:
* return int < 0 if(s1 < s2)
* 0 if(s1 == s2)
* > 0 if(s1 > s2)
*/
{
int i;
/*
* Find the first character where the two strings differ.
*/
;
/*
* Did we hit the end of either string before finding a difference?
*/
return 0;
else if(n1==i)
return -1;
else
return 1;
};
/*
* Compare the two characters that differed to determine which
* string is greatest.
*/
}
/*.......................................................................
* Assign a given action function to a binding table entry.
*
* Input:
* sym KeySym * The binding table entry to be modified.
* binder KtBinder The source of the binding.
* keyfn KtKeyFn * The action function.
* data void * A pointer to arbitrary callback data to pass to
* the action function whenever it is called.
*/
void *data)
{
int i;
/*
* Unknown binding source?
*/
return;
/*
* Record the action according to its source.
*/
/*
* Find the highest priority binding source that has supplied an
* action. Note that the actions[] array is ordered in order of
* descreasing priority, so the first entry that contains a function
* is the one to use.
*/
;
/*
* Record the index of this action for use during lookups.
*/
return;
}
/*.......................................................................
* Remove all key bindings that came from a specified source.
*
* Input:
* kt KeyTab * The table of key bindings.
* binder KtBinder The source of the bindings to be cleared.
*/
{
/*
* If there is no table, then no bindings exist to be deleted.
*/
if(!kt)
return;
/*
* Clear bindings of the given source.
*/
/*
* Delete entries that now don't have a binding from any source.
*/
newkey = 0;
} else {
newkey++;
};
};
/*
* Record the number of keys that were kept.
*/
return;
}
/*.......................................................................
* Translate a backslash escape sequence to a binary character.
*
* Input:
* string const char * The characters that follow the backslash.
* endp const char ** If endp!=NULL, on return *endp will be made to
* point to the character in string[] which follows
* the escape sequence.
* Output:
* return char The binary character.
*/
{
char c; /* The output character */
/*
* Is the backslash followed by one or more octal digits?
*/
switch(*string) {
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
break;
case 'a':
c = '\a';
string++;
break;
case 'b':
c = '\b';
string++;
break;
case 'e': case 'E': /* Escape */
c = GL_ESC_CHAR;
string++;
break;
case 'f':
c = '\f';
string++;
break;
case 'n':
c = '\n';
string++;
break;
case 'r':
c = '\r';
string++;
break;
case 't':
c = '\t';
string++;
break;
case 'v':
c = '\v';
string++;
break;
case '\0':
c = '\\';
break;
default:
c = *string++;
break;
};
/*
* Report the character which follows the escape sequence.
*/
if(endp)
return c;
}
/*.......................................................................
* Return non-zero if the next two characters are M- and a third character
* follows. Otherwise return 0.
*
* Input:
* string const char * The sub-string to scan.
* Output:
* return int 1 - The next two characters are M- and these
* are followed by at least one character.
* 0 - The next two characters aren't M- or no
* character follows a M- pair.
*/
{
}
/*.......................................................................
* Return non-zero if the next two characters are C- and a third character
* follows. Otherwise return 0.
*
* Input:
* string const char * The sub-string to scan.
* Output:
* return int 1 - The next two characters are C- and these
* are followed by at least one character.
* 0 - The next two characters aren't C- or no
* character follows a C- pair.
*/
{
}
/*.......................................................................
* Merge an array of bindings with existing bindings.
*
* Input:
* kt KeyTab * The table of key bindings.
* binder KtBinder The source of the bindings.
* bindings const KtKeyBinding * The array of bindings.
* n int The number of bindings in bindings[].
* Output:
* return int 0 - OK.
* 1 - Error.
*/
unsigned n)
{
int i;
/*
* Check the arguments.
*/
if(kt)
return 1;
};
/*
* Install the array of bindings.
*/
for(i=0; i<n; i++) {
return 1;
};
return 0;
}
/*.......................................................................
* Lookup the function that implements a given action.
*
* Input:
* kt KeyTab * The table of key bindings.
* action const char * The name of the action to look up.
* fn KtKeyFn ** If the action is found, the function that
* implements it will be assigned to *fn. Note
* that fn can be NULL.
* data void ** If the action is found, the callback data
* associated with the action function, will be
* assigned to *data. Note that data can be NULL.
* Output:
* return int 0 - OK.
* 1 - Action not found.
*/
{
/*
* Check the arguments.
*/
if(kt)
return 1;
};
/*
* Lookup the symbol table entry of the action.
*/
if(!sym)
return 1;
/*
* Return the function and ccallback data associated with the action.
*/
if(fn)
if(data)
return 0;
}
/*.......................................................................
* Return extra information (ie. in addition to that provided by errno)
* about the last error to occur in any of the public functions of this
* module.
*
* Input:
* kt KeyTab * The table of key bindings.
* Output:
* return const char * A pointer to the internal buffer in which
* the error message is temporarily stored.
*/
{
}