home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / manip / inkey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  4.4 KB  |  211 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <stdarg.h>
  6. #include "conio.h"
  7. #include "bios.h"
  8. #include "edt.h"
  9. #define true (!false)
  10. #define false 0
  11. void fner(va_list arg_list, ...);
  12. int scr_refresh(void);
  13. int mjl_key(int c);
  14. int scr_getch(void);
  15. char *function_bar()
  16. {
  17.     return "F1=Help  F2=Save  F3=Load    Pageup=Recall command     ^Z Quit";
  18. }
  19.  
  20. extern int in_recover,single_step;
  21. struct keymatch {int m; int val;};
  22. /* commands using hi 8 bits of bios, e.g. arrow keys */
  23. struct keymatch kmatch1[] = {
  24.     0x0e, edelete,
  25.     0x4d, eright,
  26.     0x4b, eleft,
  27.     0x48, eup,
  28.     0x53, edelright,
  29.     0x50, edown,
  30.     0x4a, edelline,
  31.     0x3b, ehelp,
  32.     0x3c, esave,
  33.     0x3d, eload,
  34.     0x3e, esaveas,
  35.     0x3f, eshowerror,
  36.     0x43, egraphmenu,
  37.     0x44, edrawit,
  38.     0x49, epageup,
  39.     0x51, epagedown,
  40.     0x47, ebol,
  41.     0x4f, eeol,
  42.     0,0
  43. };
  44. /* Normal key and ^ commands  commands */
  45. struct keymatch kmatch2[] = {
  46.     13, ereturn,
  47.     2, ebackline,
  48.     3, equit,
  49.     4, eword,
  50.     5, eshowerror,
  51.     8, ehelp,
  52.     19, eshell,
  53.     20, etrace,
  54.     12, efindnext,
  55.     21, eundelline,
  56.     25, edelline,
  57.     26, eescape,
  58.     27, eescape,
  59.     0,0
  60. };
  61. /* Control K commands */
  62. struct keymatch kmatch3[] = {
  63.     'b', eselect,
  64.     'v', emove,
  65.     'k', emark,
  66.     'c', ecopy,
  67.     'y', ecut,
  68.     'u', epaste,
  69.     'p', epaste,
  70.     'r', eblockread,
  71.     'w', eblockwrite,
  72.     'm', egraphmenu,
  73.     'l', eload,
  74.     'd', edrawit,
  75.     's', esave,
  76.     'x', equit,
  77.     0,0
  78. };
  79. /* Control Q commands */
  80. struct keymatch kmatch4[] = {
  81.     'f', esearch,
  82.     'c', eendoffile,
  83.     'r', etopoffile,
  84.     0,0
  85. };
  86.  
  87. extern int iserr;
  88. FILE *jouptr;
  89. text_inkey()
  90. {
  91.     int c;
  92.     unsigned char ccc;
  93.     static int rdone;
  94.     if (in_recover && !rdone) {
  95. #ifdef unix
  96.         if (jouptr==NULL) jouptr = fopen("manip_.j1","r");
  97. #else
  98.         if (jouptr==NULL) jouptr = fopen("manip_.j1","rb");
  99. #endif
  100.         if (jouptr==NULL) fner("Unable to open/read journal file manip_.j1");
  101.         if (fread(&ccc,1,1,jouptr)!=1) {
  102.             rdone = true;
  103.             fclose(jouptr);
  104.         }
  105.         if (single_step) if (getch()!=' ') rdone = true;
  106.         return ccc;
  107.     } else {
  108.         c = xtext_inkey();
  109.         mjl_key(c);
  110.         return c;
  111.     }
  112. }
  113. xtext_inkey()
  114. {
  115.     int cc,i,c1,c2,ff;
  116.     scr_refresh();
  117. loop1:    cc = bioskey(0);
  118.     ff = bioskey(2);
  119.     c1 = (cc & 0xff00)>>8;
  120.     c2 = (cc & 0xff);
  121. /*printf("%d %d ff=%d\n",c1,c2,ff); */
  122.     if (c1==0x48 && ((ff & 2)>0) ) return esearch;
  123.     if (c1==0x50 && ((ff & 2)>0) ) return edelline;
  124.     if (c1==0x4b && ((ff & 2)>0) ) return ebigleft;
  125.     if (c1==0x4d && ((ff & 2)>0) ) return ebigright;
  126.     if (c1==45 && ((ff & 8)>0) ) return eexitnow;
  127.     if (c2==0 || c2==8) {
  128.         for (i=0;kmatch1[i].m!=0;i++)
  129.         if (kmatch1[i].m==c1) {return kmatch1[i].val;}
  130.     }
  131.     switch(c2) {
  132.       default:
  133.         for (i=0;kmatch2[i].m!=0;i++)
  134.         if (kmatch2[i].m==c2) return kmatch2[i].val;
  135.         break;
  136.       case 17:
  137.         fner("^Q  F=Find string,  R=Top of file,  C=End of file");
  138.         cc = bioskey(0);
  139.         c2 = (cc & 0xff);
  140.         if (c2<32) c2 = c2 + 'a' - 1;
  141.         c2 = tolower(c2);
  142.         for (i=0;kmatch4[i].m!=0;i++)
  143.         if (kmatch4[i].m==c2) return kmatch4[i].val;
  144.         fner("Unrecognized Quick movement command");
  145.         goto loop1;
  146.       case 11:
  147.         fner("^K  B=begin block,  P=Paste,  Y=Cut,  K=End block");
  148.         cc = bioskey(0);
  149.         c2 = (cc & 0xff);
  150.         if (c2<32) c2 = c2 + 'a' - 1;
  151.         c2 = tolower(c2);
  152.         for (i=0;kmatch3[i].m!=0;i++)
  153.         if (kmatch3[i].m==c2) return kmatch3[i].val;
  154.         fner("Unrecognized block command");
  155.         goto loop1;
  156.     }
  157.     return c2;
  158. }
  159. init_logging(char *infile)
  160. {
  161.     FILE *jptr;
  162.     unsigned char ccc;
  163.     static char buff[80];
  164.     if (in_recover) {
  165. #ifdef unix
  166.         jouptr = fopen("manip_.j1","r");
  167. #else
  168.         jouptr = fopen("manip_.j1","rb");
  169. #endif
  170.         if (jouptr==NULL) fner("Unable to open/read journal file manip_.j1");
  171.         fread(&ccc,1,1,jouptr);
  172.         fread(buff,1,ccc,jouptr);
  173.         if (strlen(infile)==0) strcpy(infile,buff);
  174.         return;
  175.     }
  176.     unlink("manip_.j3");
  177.     rename("manip_.j2","manip_.j3");
  178.     rename("manip_.j1","manip_.j2");
  179. #ifdef unix
  180.     jptr = fopen("manip_.j1","w");
  181. #else
  182.     jptr = fopen("manip_.j1","wb");
  183. #endif
  184.     fputc(strlen(infile),jptr);
  185.     fwrite(infile,1,strlen(infile),jptr);
  186.     if (jptr==NULL) fner("Unable to open journal file manip_.j1");
  187.     fclose(jptr);
  188. }
  189. unsigned char mjl_buff[80];
  190. mjl_key(int c)
  191. {
  192.     int s;
  193.     if (in_recover) return;
  194.     if (c==0) return;
  195.     s = strlen(mjl_buff);
  196.     mjl_buff[s] = c;
  197.     mjl_buff[s+1] = 0;
  198.     if (s>40) mjl_flush();
  199. }
  200. mjl_flush()
  201. {
  202.     FILE *jptr;
  203.     if (in_recover) return;
  204.     if (strlen(mjl_buff)<7) return;
  205.     jptr = fopen("manip_.j1","ab");
  206.     if (jptr==NULL) fner("Unable to append to journal file manip_.j1");
  207.     fwrite(mjl_buff,1,strlen(mjl_buff),jptr);
  208.     fclose(jptr);
  209.     mjl_buff[0] = 0;
  210. }
  211.