home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / compile.cpp < prev    next >
C/C++ Source or Header  |  2001-08-01  |  10KB  |  288 lines

  1. /*
  2. ** Module   :COMPILE.CPP
  3. ** Abstract :Keyboard macro compiler
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Fri  27/03/1998     Created
  8. */
  9.  
  10. #include <string.h>
  11.  
  12. #include <boxcoll.h>
  13. #include <version.h>
  14.  
  15. struct FuncMapping
  16. {
  17.     char *key;
  18.     char func;
  19. };
  20.  
  21. FuncMapping func_table[]=
  22. {
  23.     {"abort"            ,FUNC_ABORT            },
  24.     {"bind"             ,FUNC_LOAD_KEYS        },
  25.     {"bksp"             ,FUNC_BKSP             },
  26.     {"close"            ,FUNC_CLOSE            },
  27.     {"copy"             ,FUNC_COPY             },
  28.     {"copyright"        ,FUNC_COPYRIGHT2       },
  29.     {"cut"              ,FUNC_CUT              },
  30.     {"del"              ,FUNC_DEL              },
  31.     {"delline"          ,FUNC_DELLINE          },
  32.     {"deltoeol"         ,FUNC_DELTOEOL         },
  33.     {"delwordleft"      ,FUNC_DELWORDLEFT      },
  34.     {"delwordright"     ,FUNC_DELWORDRIGHT     },
  35.     {"down"             ,FUNC_DOWN             },
  36.     {"downmark"         ,FUNC_DOWNMARK         },
  37.     {"dupline"          ,FUNC_DUPLICATE_LINE   },
  38.     {"end"              ,FUNC_END              },
  39.     {"endmark"          ,FUNC_ENDMARK          },
  40.     {"exit"             ,FUNC_EXIT             },
  41.     {"filebegin"        ,FUNC_FILEBEGIN        },
  42.     {"filebeginmark"    ,FUNC_FILEBEGINMARK    },
  43.     {"fileend"          ,FUNC_FILEEND          },
  44.     {"fileendmark"      ,FUNC_FILEENDMARK      },
  45.     {"filelist"         ,FUNC_FILELIST         },
  46.     {"flipautoindent"   ,FUNC_FLIPAUTOINDENT   },
  47.     {"flipblockmode"    ,FUNC_FLIPBLOCKMODE    },
  48.     {"fliphiliting"     ,FUNC_FLIPHILITING     },
  49.     {"fliptype"         ,FUNC_FLIPTYPE         },
  50.     {"flipwordwrap"     ,FUNC_FLIPWORDWRAP     },
  51.     {"flipwordwrapmerge",FUNC_FLIPWWMERGE      },
  52.     {"flipwordwrappara" ,FUNC_FLIPWWLONG       },
  53.     {"helpscreen"       ,FUNC_HELPSCREEN       },
  54.     {"hiliteselect"     ,FUNC_HILITE_ACHOICE   },
  55.     {"home"             ,FUNC_HOME             },
  56.     {"homemark"         ,FUNC_HOMEMARK         },
  57.     {"indent"           ,FUNC_INDENT           },
  58.     {"ins"              ,FUNC_INS              },
  59.     {"insdate"          ,FUNC_INSDATE          },
  60.     {"insfilename"      ,FUNC_INSFILENAME      },
  61.     {"insfilenameshort" ,FUNC_INSFILENAMESHORT },
  62.     {"jumpcol"          ,FUNC_JUMPCOL          },
  63.     {"jumpline"         ,FUNC_JUMPLINE         },
  64.     {"left"             ,FUNC_LEFT             },
  65.     {"leftmark"         ,FUNC_LEFTMARK         },
  66.     {"load"             ,FUNC_LOAD             },
  67.     {"lower"            ,FUNC_LOWER            },
  68.     {"macrorecend"      ,FUNC_MACRORECEND      },
  69.     {"macrorecstart"    ,FUNC_MACRORECSTART    },
  70.     {"markgo0"          ,FUNC_BMK_GET_0        },
  71.     {"markgo1"          ,FUNC_BMK_GET_1        },
  72.     {"markgo2"          ,FUNC_BMK_GET_2        },
  73.     {"markgo3"          ,FUNC_BMK_GET_3        },
  74.     {"markgo4"          ,FUNC_BMK_GET_4        },
  75.     {"markgo5"          ,FUNC_BMK_GET_5        },
  76.     {"markgo6"          ,FUNC_BMK_GET_6        },
  77.     {"markgo7"          ,FUNC_BMK_GET_7        },
  78.     {"markgo8"          ,FUNC_BMK_GET_8        },
  79.     {"markgo9"          ,FUNC_BMK_GET_9        },
  80.     {"markset0"         ,FUNC_BMK_PUT_0        },
  81.     {"markset1"         ,FUNC_BMK_PUT_1        },
  82.     {"markset2"         ,FUNC_BMK_PUT_2        },
  83.     {"markset3"         ,FUNC_BMK_PUT_3        },
  84.     {"markset4"         ,FUNC_BMK_PUT_4        },
  85.     {"markset5"         ,FUNC_BMK_PUT_5        },
  86.     {"markset6"         ,FUNC_BMK_PUT_6        },
  87.     {"markset7"         ,FUNC_BMK_PUT_7        },
  88.     {"markset8"         ,FUNC_BMK_PUT_8        },
  89.     {"markset9"         ,FUNC_BMK_PUT_9        },
  90.     {"matchbracket"     ,FUNC_MATCHBRACKET     },
  91.     {"matchbracketmark" ,FUNC_MATCHBRACKETMARK },
  92.     {"new"              ,FUNC_NEW              },
  93.     {"nextfile"         ,FUNC_NEXTFILE         },
  94.     {"openjumplist0"    ,FUNC_JMP_LST_0        },
  95.     {"openjumplist1"    ,FUNC_JMP_LST_1        },
  96.     {"openjumplist2"    ,FUNC_JMP_LST_2        },
  97.     {"openjumplist3"    ,FUNC_JMP_LST_3        },
  98.     {"openjumplist4"    ,FUNC_JMP_LST_4        },
  99.     {"openjumplist5"    ,FUNC_JMP_LST_5        },
  100.     {"openjumplist6"    ,FUNC_JMP_LST_6        },
  101.     {"openjumplist7"    ,FUNC_JMP_LST_7        },
  102.     {"openjumplist8"    ,FUNC_JMP_LST_8        },
  103.     {"openjumplist9"    ,FUNC_JMP_LST_9        },
  104.     {"paste"            ,FUNC_PASTE            },
  105.     {"pgdn"             ,FUNC_PGDN             },
  106.     {"pgdnmark"         ,FUNC_PGDNMARK         },
  107.     {"pgup"             ,FUNC_PGUP             },
  108.     {"pgupmark"         ,FUNC_PGUPMARK         },
  109.     {"prevfile"         ,FUNC_PREVFILE         },
  110.     {"right"            ,FUNC_RIGHT            },
  111.     {"rightmark"        ,FUNC_RIGHTMARK        },
  112.     {"save"             ,FUNC_SAVE             },
  113.     {"saveall"          ,FUNC_SAVEALL          },
  114.     {"saveas"           ,FUNC_SAVEAS           },
  115.     {"search"           ,FUNC_SEARCH           },
  116.     {"searchagain"      ,FUNC_SEARCHAGAIN      },
  117.     {"setcp"            ,FUNC_SET_XLAT         },
  118.     {"sort"             ,FUNC_SORT             },
  119.     {"undo"             ,FUNC_UNDO             },
  120.     {"unindent"         ,FUNC_UNINDENT         },
  121.     {"up"               ,FUNC_UP               },
  122.     {"upmark"           ,FUNC_UPMARK           },
  123.     {"upper"            ,FUNC_UPPER            },
  124.     {"wordleft"         ,FUNC_WORDLEFT         },
  125.     {"wordleftmark"     ,FUNC_WORDLEFTMARK     },
  126.     {"wordright"        ,FUNC_WORDRIGHT        },
  127.     {"wordrightmark"    ,FUNC_WORDRIGHTMARK    }
  128. };
  129.  
  130. FunctionDictionary::FunctionDictionary():Dictionary(0,0,0)
  131. {
  132.     for(int i = 0; i < sizeof(func_table)/sizeof(func_table[0]); i++)
  133.         Add(&func_table[i]);
  134. }
  135.  
  136. static int cx2n(char chr)
  137. {
  138.     int n = 0;
  139.  
  140.     if(__isdd(chr))
  141.         return chr - '0';
  142.  
  143.     if(chr >= 'a' || chr <= 'z')
  144.         return chr - 'a' + 0x0A;
  145.  
  146.     if(chr >= 'A' || chr <= 'Z')
  147.         return chr -'A' + 0x0A;
  148.  
  149.     return 0;
  150. }
  151.  
  152. int KeyDefCollection::compile_keydef(char *str, char *out)
  153. {
  154.     int cnt = 0;
  155.     while(*str)
  156.     {
  157.         switch(*str)
  158.         {
  159.             case ' ':
  160.             case ',':
  161.             case '\r':
  162.             case '\n':
  163.             case '\t':
  164.                 break;
  165.  
  166.             case '"':
  167.             case '{':
  168.             case '\'':
  169.                 {
  170.                     int iRexx = 0;
  171.  
  172.                     char delim = *str++;
  173.  
  174.                     if(delim == '{') //This is rexx?
  175.                     {
  176.                         delim = '}';
  177.  
  178.                         cnt += 2;
  179.  
  180.                         if(out)
  181.                         {
  182.                             *out++ = FUNC_ESCAPE;
  183.                             *out++ = FUNC_REXX;
  184.                         }
  185.  
  186.                         iRexx++;
  187.                     }
  188.  
  189.                     while(*str)
  190.                     {
  191.                         if(delim != '}' && *str == delim)
  192.                             break;
  193.  
  194.                         if(delim == '}' && *str == delim && !str[1])
  195.                             break;
  196.  
  197.                         char chr = *str;
  198.  
  199.                         if(*str == '\\' && !iRexx) //Escape sequences
  200.                         {
  201.                             str++;
  202.  
  203.                             switch(*str)
  204.                             {
  205.                                 case 'n':
  206.                                     chr = '\n';
  207.                                     break;
  208.  
  209.                                 case 'r':
  210.                                     chr = '\r';
  211.                                     break;
  212.  
  213.                                 case 't':
  214.                                     chr = '\t';
  215.                                     break;
  216.  
  217.                                 case 'x':
  218.                                     {
  219.                                         str++;
  220.                                         chr = 0;
  221.                                         int k;
  222.  
  223.                                         for(k = 0; k < 2; k++)
  224.                                         {
  225.                                             if(!__ishd(*str))
  226.                                                 break;
  227.  
  228.                                             chr <<= 4;
  229.  
  230.                                             chr |= cx2n(*str++);
  231.                                         }
  232.  
  233.                                         if(out)
  234.                                             *out++ = chr++;
  235.                                     }
  236.                                     continue;
  237.  
  238.                                 default:
  239.                                     chr = *str;
  240.                                     break;
  241.                             }
  242.                         }
  243.  
  244.                         if(out)
  245.                             *out++ = chr;
  246.  
  247.                         cnt++;
  248.                         str++;
  249.                     }
  250.  
  251.                     if(*str && delim == '}')
  252.                         return cnt;
  253.                 }
  254.                 break;
  255.  
  256.             default:
  257.                 if(__isis(*str))
  258.                 {
  259.                     char *ptr = str;
  260.                     while(*str && __isic(*str))
  261.                         str++;
  262.  
  263.                     FuncMapping *pFunc = (FuncMapping *)Func_DIC.IsIn(ptr, str - ptr, 0);
  264.  
  265.                     if(pFunc)
  266.                     {
  267.                         cnt ++; //bytecode
  268.                         cnt ++; //function code
  269.  
  270.                         if(out)
  271.                         {
  272.                             *out++ = FUNC_ESCAPE;
  273.                             *out++ = pFunc->func;
  274.                         }
  275.                     }
  276.                     else
  277.                         return -2; //Wrong function name
  278.                 }
  279.                 else
  280.                     return -1; //Wrong char in keydef
  281.         }
  282.         if(*str)
  283.             str++;
  284.     }
  285.     return cnt;
  286. }
  287.  
  288.