/*{{{}}}*/
/*{{{  #includes*/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define NAME_C

#include "keybind.h"
#include <h/envvar_str.h>
#include <h/lg.h>
#include <lib/ori_add_lib.h>
/*}}}  */

/*{{{  keywords*/
#include "namelist.h"
/*}}}  */

/*{{{  name2func*/
public KEYNAME *name2func(unsigned char *name)
{
  int n,diff;

  for (n=0,diff=S_bindings;;)
   { if (n+diff<N_bindings)
      { int d;

        d=ustrcmp(bindings[n+diff].name,name);
        if (d==0 && bindings[n+diff].use)
           return(bindings+n+diff);
        else if (d<0)
           n+=diff;
      }
     if (diff) diff=diff>>1; else break;
   }

  return(0);
}
/*}}}  */
/*{{{  name2keyword*/
public keywords const *name2keyword(unsigned char *name)
{
  int n,diff;

  for (n=0,diff=S_keytab;;)
   { if (n+diff<N_keytab)
      { int d;

        d=ustrcmp(keytab[n+diff].name,name);
        if (d==0)
           return(keytab+n+diff);
        else if (d<0)
           n+=diff;
      }
     if (diff) diff=diff>>1; else break;
   }

  return(0);
}
/*}}}  */
/*{{{  history table*/
/*{{{  type history_list*/
typedef struct history_list
 { char *name;
   histories id;
   struct history_list *next;
 } history_list;
/*}}}  */
/*{{{  list of known histories*/
private history_list history_field[]=
{{NO_HIST,	no_history,	&(history_field[1])},
 {MISC_HIST,	misc_history,	&(history_field[2])},
 {FILE_HIST,	file_history,	&(history_field[3])},
 {SHELL_HIST,	shell_history,	&(history_field[4])},
 {SEARCH_HIST,	search_history,	&(history_field[5])},
 {ERR_HIST,	error_history,	&(history_field[6])},
 {ARG_HIST,	arg_history,	&(history_field[7])},
 {MATCH_HIST,	match_history,	&(history_field[8])},
 {NO_MATCH_HIST,nomatch_history,&(history_field[9])},
 {REPL_HIST,	replace_history,0}
};
private history_list *h_list=history_field;
private histories new_history=user_history;
/*}}}  */

public histories name_to_history(tokens t, boolean define)
{ histories ret;

  if (t==DEFAULT)
     return(default_history);
  ret=unknown_history;
  if (t==NAME || t==OPCODE || t==OPERATION)
   /*{{{  search in list*/
   { history_list *x;

     x=h_list;
     while (x)
        if (strcmp(x->name,(char*)tk_string))
           x=x->next;
        else
         { ret=x->id;break; }
     if (define && !x)
      { if (verbose_level>0) printf(F_HISTORY,tk_string);
        x=kbd_malloc(sizeof(history_list)+ustrlen(tk_string)+1);
        strcpy(x->name=(char*)(x+1),(char*)tk_string);
        ret=x->id=new_history++;
        x->next=h_list;
        h_list=x;
      }
   }
   /*}}}  */

  return(ret);
}
/*}}}  */
/*{{{  name2lg*/
typedef enum
 { F_C_NONE=0,
   LANGTYP
   F_C_USER,
   F_C_TDS,           /* this has to be the last defined language!!! */
   F_C_SIZE
 } f_c_types;
#define LG_DAT(name,st,sl,et,el) name
public char *f_c_name[]={ "None",LANGDATA "User","Inmos",0 };

public unsigned char name2lg(unsigned char *lgname)
{ f_c_types t;

  for (t=F_C_NONE;t<F_C_SIZE;t++)
     if (!strcmp(f_c_name[t],(char*)lgname))
        return((unsigned char)(f_c_name[t][0]));
  return((unsigned char)'\0');
}
/*}}}  */
/*{{{  name2asm*/
public KEYNAME const *name2asm(unsigned char *name)
{
  int n,diff;

  for (n=0,diff=S_asm_token;;)
   { if (n+diff<N_asm_token)
      { int d;

        d=ustrcmp(asm_token[n+diff].name,name);
        if (d==0 && asm_token[n+diff].use)
           return(asm_token+n+diff);
        else if (d<0)
           n+=diff;
      }
     if (diff) diff=diff>>1; else break;
   }

  return(0);
}
/*}}}  */
/*{{{  name2msg*/
public int name2msg(unsigned char *n)
{ int msg_no=0;

  while (msg_list[msg_no])
     if (!strcmp((char*)n,msg_list[msg_no++]))
        return(msg_no);
  return(-1);
}
/*}}}  */
/*{{{  name_list*/
public void name_list(void)
{
  /*{{{  histories*/
  { history_list *h=history_field;

    while (h) { printf("%s\n",h->name);h=h->next; }
  }
  /*}}}  */
  /*{{{  languages*/
  { int l;

    for (l=F_C_NONE;l<=F_C_TDS;l++) printf("%s\n",f_c_name[l]);
  }
  /*}}}  */
}
/*}}}  */
