home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / Sources / src / keybind / set.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  7.3 KB  |  349 lines

  1. /*{{{}}}*/
  2. /*{{{  #includes*/
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. #define SET_C
  9.  
  10. #include "keybind.h"
  11.  
  12. #include <h/envvar_str.h>
  13. #include <h/rcformat.h>
  14. #include <lib/ori_rc_lib.h>
  15. #include <lib/ori_add_lib.h>
  16. /*}}}  */
  17.  
  18. /*{{{  types*/
  19. typedef struct charset
  20.  { unsigned char *name;
  21.    int  no;
  22.    SetField set;
  23.    struct charset *next;
  24.  } charset;
  25. /*}}}  */
  26. /*{{{  variables*/
  27. private int set_used=(int)first_user_set;
  28. private charset *sets=0;
  29. /*}}}  */
  30.  
  31. /*{{{  poke_char*/
  32. private void poke_char(int x,SetField setcode)
  33.  {
  34.    setcode[x/BITS_P_SET_PAKET] |= (1<<(x & (BITS_P_SET_PAKET-1)));
  35.  }
  36. /*}}}  */
  37. /*{{{  init_sets*/
  38. public void init_sets(void)
  39. {
  40.   charset *x;
  41.   int i;
  42.  
  43.   sets=0;
  44.   /*{{{  alnum*/
  45.   x=(charset*)calloc(1,sizeof(charset));
  46.   if (!x)
  47.      goto mem_exit;
  48.   x->name=(unsigned char*)ALNUM_STR;
  49.   x->no=alnum_set;
  50.   for (i=0;i<O_NOP;i++)
  51.      if (isalnum(i))
  52.         poke_char(i,x->set);
  53.   x->next=sets;
  54.   sets=x;
  55.   /*}}}  */
  56.   /*{{{  digit*/
  57.   x=(charset*)calloc(1,sizeof(charset));
  58.   if (!x)
  59.      goto mem_exit;
  60.   x->name=(unsigned char*)DIGIT_STR;
  61.   x->no=digit_set;
  62.   for (i=0;i<O_NOP;i++)
  63.      if (isdigit(i))
  64.         poke_char(i,x->set);
  65.   x->next=sets;
  66.   sets=x;
  67.   /*}}}  */
  68.   /*{{{  alpha*/
  69.   x=(charset*)calloc(1,sizeof(charset));
  70.   if (!x)
  71.      goto mem_exit;
  72.   x->name=(unsigned char*)ALPHA_STR;
  73.   x->no=alpha_set;
  74.   for (i=0;i<O_NOP;i++)
  75.      if (isalpha(i))
  76.         poke_char(i,x->set);
  77.   x->next=sets;
  78.   sets=x;
  79.   /*}}}  */
  80.   /*{{{  upper*/
  81.   x=(charset*)calloc(1,sizeof(charset));
  82.   if (!x)
  83.      goto mem_exit;
  84.   x->name=(unsigned char*)UPPER_STR;
  85.   x->no=upper_set;
  86.   for (i=0;i<O_NOP;i++)
  87.      if (isupper(i))
  88.         poke_char(i,x->set);
  89.   x->next=sets;
  90.   sets=x;
  91.   /*}}}  */
  92.   /*{{{  lower*/
  93.   x=(charset*)calloc(1,sizeof(charset));
  94.   if (!x)
  95.      goto mem_exit;
  96.   x->name=(unsigned char*)LOWER_STR;
  97.   x->no=lower_set;
  98.   for (i=0;i<O_NOP;i++)
  99.      if (islower(i))
  100.         poke_char(i,x->set);
  101.   x->next=sets;
  102.   sets=x;
  103.   /*}}}  */
  104.   /*{{{  blank*/
  105.   x=(charset*)calloc(1,sizeof(charset));
  106.   if (!x)
  107.      goto mem_exit;
  108.   x->name=(unsigned char*)BLANK_STR;
  109.   x->no=blank_set;
  110.   poke_char(' ',x->set);
  111.   poke_char('\t',x->set);
  112.   x->next=sets;
  113.   sets=x;
  114.   /*}}}  */
  115.   /*{{{  xdigit*/
  116.   x=(charset*)calloc(1,sizeof(charset));
  117.   if (!x)
  118.      goto mem_exit;
  119.   x->name=(unsigned char*)XDIGIT_STR;
  120.   x->no=xdigi_set;
  121.   for (i=0;i<O_NOP;i++)
  122.      if (isxdigit(i))
  123.         poke_char(i,x->set);
  124.   x->next=sets;
  125.   sets=x;
  126.   /*}}}  */
  127.   /*{{{  cntrl*/
  128.   x=(charset*)calloc(1,sizeof(charset));
  129.   if (!x)
  130.      goto mem_exit;
  131.   x->name=(unsigned char*)CNTRL_STR;
  132.   x->no=cntrl_set;
  133.   for (i=0;i<O_NOP;i++)
  134.      if (iscntrl(i))
  135.         poke_char(i,x->set);
  136.   x->next=sets;
  137.   sets=x;
  138.   /*}}}  */
  139.   /*{{{  punct*/
  140.   x=(charset*)calloc(1,sizeof(charset));
  141.   if (!x)
  142.      goto mem_exit;
  143.   x->name=(unsigned char*)PUNCT_STR;
  144.   x->no=punct_set;
  145.   for (i=0;i<O_NOP;i++)
  146.      if (ispunct(i))
  147.         poke_char(i,x->set);
  148.   x->next=sets;
  149.   sets=x;
  150.   /*}}}  */
  151.   /*{{{  print*/
  152.   x=(charset*)calloc(1,sizeof(charset));
  153.   if (!x)
  154.      goto mem_exit;
  155.   x->name=(unsigned char*)PRINT_STR;
  156.   x->no=print_set;
  157.   for (i=0;i<O_NOP;i++)
  158.      if (isprint(i))
  159.         poke_char(i,x->set);
  160.   x->next=sets;
  161.   sets=x;
  162.   /*}}}  */
  163.   /*{{{  space*/
  164.   x=(charset*)calloc(1,sizeof(charset));
  165.   if (!x)
  166.      goto mem_exit;
  167.   x->name=(unsigned char*)SPACE_STR;
  168.   x->no=space_set;
  169.   for (i=0;i<O_NOP;i++)
  170.      if (isspace(i))
  171.         poke_char(i,x->set);
  172.   x->next=sets;
  173.   sets=x;
  174.   /*}}}  */
  175.   /*{{{  graph*/
  176.   x=(charset*)calloc(1,sizeof(charset));
  177.   if (!x)
  178.      goto mem_exit;
  179.   x->name=(unsigned char*)GRAPH_STR;
  180.   x->no=graph_set;
  181.   for (i=0;i<O_NOP;i++)
  182.      if (isgraph(i))
  183.         poke_char(i,x->set);
  184.   x->next=sets;
  185.   sets=x;
  186.   /*}}}  */
  187.   return;
  188.  
  189. mem_exit:
  190.   m_exit(M_NOMEMORY);
  191. }
  192. /*}}}  */
  193. /*{{{  parse_set*/
  194. public void parse_set(void)
  195. {
  196.   charset *x;
  197.  
  198.   /*{{{  get memory for set*/
  199.   x=kbd_malloc(sizeof(charset));
  200.   memset(x->set,0,(size_t)SET_LG);
  201.   /*}}}  */
  202.   /*{{{  get name for set*/
  203.   if (get_full_token()!=NAME)
  204.      m_exit(M_SNAME);
  205.   x->name=mstrcpy(tk_string);
  206.   if (verbose_level>0)
  207.      fprintf(stderr,F_SETDEC,tk_string,set_used);
  208.   /*}}}  */
  209.   /*{{{  get all set members*/
  210.   for (begin_parse();;)
  211.    { switch (get_full_token())
  212.       { case END:
  213.            break;
  214.         case NAME:
  215.          /*{{{  predefined set or ctrl-char*/
  216.          { charset *y;
  217.  
  218.            /*{{{  try to set y to the set, belonging to tk_string*/
  219.            for (y=sets;y;y=y->next)
  220.               if (!ustrcmp(tk_string,y->name))
  221.                /*{{{  or the codes and*/
  222.                { int j;
  223.  
  224.                  for (j=0;j<SET_LG;j++) x->set[j] |= y->set[j];
  225.                  break;
  226.                }
  227.                /*}}}  */
  228.            /*}}}  */
  229.            if (!y)
  230.               if
  231.                /*{{{  a ctrl-string*/
  232.                (    tk_string[0]=='C'
  233.                  && tk_string[1]=='-'
  234.                  && tk_string[2]!='\0'
  235.                  && tk_string[3]=='\0'
  236.                )
  237.                /*}}}  */
  238.                /*{{{  ctrl-char*/
  239.                  poke_char(ctrl_c[(int)tk_string[2]],x->set);
  240.                /*}}}  */
  241.               else
  242.                  goto set_expected;
  243.            continue;
  244.          }
  245.          /*}}}  */
  246.         case MACRO:
  247.          /*{{{  single char parsing*/
  248.            if (ustrlen(tk_string)==1)
  249.             /*{{{  single char*/
  250.             { poke_char((int)(tk_string[0]),x->set);
  251.               continue;
  252.             }
  253.             /*}}}  */
  254.            else
  255.               goto set_expected;
  256.          /*}}}  */
  257.         set_expected:
  258.         default:
  259.            m_exit(M_NOSETCHAR);
  260.       }
  261.      break;
  262.    }
  263.   /*}}}  */
  264.   /*{{{  add to list of sets*/
  265.   x->no=set_used;
  266.   x->next=sets;
  267.   sets=x;
  268.   /*}}}  */
  269.   set_used+=add_new_set;
  270. }
  271. /*}}}  */
  272. /*{{{  get_set*/
  273. public int get_set(void)
  274. { charset *x=sets;
  275.  
  276.   /*{{{  name?*/
  277.   if (get_full_token()!=NAME)
  278.      m_exit(M_SNAME);
  279.   /*}}}  */
  280.   /*{{{  look in list and return no*/
  281.   while (x)
  282.      if (!ustrcmp(tk_string,x->name))
  283.         return(x->no);
  284.      else
  285.         x=x->next;
  286.   /*}}}  */
  287.   m_exit(M_SETMISS);
  288. }
  289. /*}}}  */
  290. /*{{{  parse_set_macro*/
  291. public TOKEN *parse_set_macro(TOKEN* buff)
  292. {
  293.   *buff++=M_SET_COPY;
  294.   if ((*buff++=get_set())<0)
  295.      m_exit(M_SNAME);
  296.   *buff++=(TOKEN)get_set();
  297.  
  298.   return(buff);
  299. }
  300. /*}}}  */
  301. /*{{{  writesets*/
  302. public void writesets(FILE *rc_file)
  303. {
  304.   if (set_used)
  305.    { if (verbose_level>0)
  306.         fprintf(stderr,F_SETUSE,set_used);
  307.      rc_put_c(RC_CHARSET,rc_file);
  308.      rc_put_w(set_used,rc_file);
  309.      /*{{{  write all sets*/
  310.      { charset *x;
  311.  
  312.        for (x=sets;x;x=x->next)
  313.           if (x->no>=(int)first_user_set)
  314.            { int i;
  315.  
  316.              for (i=0;i<SET_LG;i++)
  317.                 rc_put_c(x->set[i],rc_file);
  318.            }
  319.      }
  320.      /*}}}  */
  321.      /*{{{  put names*/
  322.      { int lg;
  323.        charset *x;
  324.  
  325.        /*{{{  get length of all names*/
  326.        for (lg=0,x=sets;x;x=x->next)
  327.           if (x->no>=(int)first_user_set)
  328.              lg+=1+ustrlen(x->name);
  329.        /*}}}  */
  330.        rc_put_w(lg,rc_file);
  331.        for (x=sets;x;x=x->next)
  332.           if (x->no>=(int)first_user_set)
  333.            { unsigned char *s;
  334.  
  335.              for (s=x->name;;s++)
  336.               { unsigned char c;
  337.  
  338.                 c = *s;
  339.                 rc_put_c(c,rc_file);
  340.                 if (!c)
  341.                    break;
  342.               }
  343.            }
  344.      }
  345.      /*}}}  */
  346.    }
  347. }
  348. /*}}}  */
  349.