home *** CD-ROM | disk | FTP | other *** search
- /* -*- c-mode -*- -*- outline -*-
- *
- ** hkeyset: hkey key table set program
- *
- * Author: HIRANO Satoshi
- * (C) 1990 Halca Computer Science Laboratory TM
- * University of Tokyo
- *
- * Edition History:
- * 1.1 90/04/30 Halca.Hirano creation
- *
- * NOTE:
- *
- * When Compile hkeyset.c, don't pack struct to word boundary!
- *
- * Desctiption:
- *
- * 'hkeyset' reads the setup file made by the terminal emulator
- * software 'hterm' then copies its key table to keyboard environment
- * regident software 'hkey'.
- *
- */
-
- static char version[] = "$Header: hkeyset.cv 1.4 90/07/05 03:06:08 hirano Exp $";
-
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #include <stdlib.h>
- #include <io.h>
- #include "config.h"
- #include "hterm.h"
- #include "option.h"
- #include "default.h"
- #include "version.h"
- #include "hkey.h"
-
- #include "keytab98.h"
-
- /*
- ** global variables
- */
- #undef TEST
- #define COMPAT_VERSION 0
-
- struct setups setups;
- char hkeyVersion[] = HKEY_MARK;
- char usage[] = "\
- hkeyset: set hkey keytable by hterm setup file\n\
- usage: hkeyset <hterm setup file>\n";
- struct SREGS segRegs;
- union REGS rg;
-
- void loadSetup(char *path);
- u_char FAR *searchHkey(void);
- u_char FAR *copyTable(u_char FAR *to, u_char *from, int count);
- int cmp(char far *x,char *y);
-
- /*
- ** main(int argc, char **argv)
- *
- * program entry point
- */
- main(argc, argv)
- char **argv;
- {
- char *path;
- u_char FAR *at;
-
- *strchr(hkeyVersion, '$') = '\0'; /* remove '$' from hkey1.2$ */
-
- path = *++argv;
- if (argc != 2 || *path == '-') {
- fprintf(stderr, usage);
- exit(1);
- }
-
- if ((at = searchHkey()) == 0) {
- fprintf(stderr, "hkeyset: %s not found in memory\n", hkeyVersion);
- exit(1);
- }
- #ifdef TEST
- fprintf(stderr, "key table at $%lx\n", at);
- #endif
- loadSetup(path);
-
- at = copyTable(at, keySwapTable, sizeof(keySwapTable));
- at = copyTable(at, anoshiftbl, sizeof(anoshiftbl));
- at = copyTable(at, actrltbl, sizeof(actrltbl));
- at = copyTable(at, ashiftbl, sizeof(ashiftbl));
- at = copyTable(at, akanatbl, sizeof(akanatbl));
- at = copyTable(at, ashkanatbl, sizeof(ashkanatbl));
- at = copyTable(at, pnoshiftbl, sizeof(anoshiftbl));
- at = copyTable(at, pctrltbl, sizeof(actrltbl));
- copyTable(at, pshiftbl, sizeof(ashiftbl));
- fprintf(stderr, "hkeyset: %s key table is updated.\n", hkeyVersion);
- }
-
- /*
- ** loadSetup(char *path)
- *
- * Load key table from setup file made by hterm into hkeyset internal table.
- * Version number is also checked.
- */
- static void loadSetup(path)
- char *path;
- {
- int fd;
-
- if ((fd = open(path, O_RDONLY|O_BINARY)) < 0) {
- fprintf(stderr, "hkeyset: can't open %s\n", path);
- exit(1);
- }
- if (read(fd, (char *)&setups, sizeof(setups)) != sizeof(setups) ||
- strcmp(setups.ids, "wet semaphore")) {
- close(fd);
- fprintf(stderr, "hkeyset: '%s' is not hterm set-up.\n", path);
- exit(1);
- }
- if (setups.version != VERSION || setups.revision != REVISION || setups.edition != EDITION) {
- fprintf(stderr, "hkeyset: set-up '%s' version mismatch.\n", path);
- fprintf(stderr, "%s was delived from hterm%d.%d.%d but setup file was made by hterm%d.%d.%d.\n",
- hkeyVersion, VERSION, REVISION, EDITION, setups.version, setups.revision, setups.edition);
- if (setups.version *1000 + setups.revision *100 +setups.edition*10 <
- COMPAT_VERSION) {
- fprintf(stderr, "hkey is too old. The key table is not changed.\n");
- exit(1); /* incompatible set-up file */
- }
- fprintf(stderr, "OK, let's try to change the key table.\n");
- }
- read(fd, (char *)keySwapTable, sizeof(keySwapTable));
- read(fd, (char *)NORMALFunkey, sizeof(NORMALFunkey));
- read(fd, (char *)SHIFTFunkey, sizeof(SHIFTFunkey));
- read(fd, (char *)NORMALPad, sizeof(NORMALPad));
- read(fd, (char *)APPLPad, sizeof(APPLPad));
- read(fd, (char *)anoshiftbl, sizeof(anoshiftbl));
- read(fd, (char *)actrltbl, sizeof(actrltbl));
- read(fd, (char *)ashiftbl, sizeof(ashiftbl));
- read(fd, (char *)akanatbl, sizeof(akanatbl));
- read(fd, (char *)pnoshiftbl, sizeof(pnoshiftbl));
- read(fd, (char *)pctrltbl, sizeof(pctrltbl));
- read(fd, (char *)pshiftbl, sizeof(pshiftbl));
- read(fd, (char *)pctrltbl, sizeof(pctrltbl));
- }
-
- /*
- ** static char FAR *searchHkey()
- *
- * search hkey and return its key table address
- * If not found, return NO
- */
- static u_char FAR *searchHkey()
- {
- struct _mcb { /* memory control block */
- char mark;
- /* don't pack to baundary */
- u_short psp;
- u_short size; /* paragraph size */
- } FAR *mcb;
- short FAR *topMcb;
- char FAR *p;
- int found;
- long size;
- int i;
-
- /*
- * get internal work area address
- */
- rg.h.ah = 0x52;
- int86x(0x21, &rg, &rg, &segRegs);
-
- /*
- * make top of MCB chain
- */
- FP_SEG(topMcb) = segRegs.es;
- FP_OFF(topMcb) = rg.x.bx - 2; /* mcb points first MCB address */
- FP_SEG(mcb) = *topMcb;
- FP_OFF(mcb) = 0;
-
- /*
- * find hkey
- */
- for (;;) {
- if (mcb->mark == 'Z') /* last MCB */
- break;
- if (mcb->mark != 'M') /* illegal MCB */
- break;
- #ifdef TEST
- fprintf(stderr, "$%lx\n", mcb);
- #endif
- /*
- * found legal MCB, test whether hkey
- * hkey sets its program name in its psp area (<$110).
- */
- size = mcb->size * 16;
- found = NO;
- for (p = (char FAR *)mcb, i = 0x110; --i > 0; p++, --size) {
- if (cmp(p, hkeyVersion)) {
- found = YES;
- break;
- }
- }
- if (found) {
- /*
- * search hkey keyword
- * key tables just follow the keyword.
- */
- #ifdef TEST
- fprintf(stderr, "hkey found\n");
- #endif
- found = NO;
- for (; --size > 0; p++) {
- if (cmp(p, KEYTAB_MARK)) {
- found = YES;
- break;
- }
- }
- if (found)
- return((char FAR *)(p+6)); /* return keytable address */
- }
-
- /*
- * try next mcb
- */
- FP_SEG(mcb) = FP_SEG(mcb) + mcb->size + 1;
- FP_OFF(mcb) = 0;
- }
- return((char FAR *)NO); /* not found */
- }
-
- /*
- ** static char FAR *copyTable(char FAR *to, char *from, int count)
- *
- * copy keytable 'from' to 'to'
- */
- static u_char FAR *copyTable(to, from, count)
- u_char FAR *to;
- u_char *from;
- int count;
- {
- #ifndef TEST
- while (--count >= 0)
- *to++ = *from++;
- #endif
- return(to);
- }
-
- /*
- ** static int cmp(register char FAR *x, register char *y)
- *
- * compare string
- */
- static int cmp(x, y)
- register char FAR *x;
- register char *y;
- {
- while (*y && (*y++ == *x++))
- ;
- return(*y == 0);
- }
-