int main (int argc, char *argv[]) { struct input_event ev[64]; int fd, rd, value, size = sizeof (struct input_event); char name[256] = "Unknown"; const char *device = NULL; if(daemon(0,0)){ printf("Could not start as daemon\n"); return -1; } device = "/dev/naga_keyboard"; //Open Device if ((fd = open (device, O_RDONLY)) == -1){ printf ("%s is not a vaild device.\n", device); return -1; } //Print Device Name ioctl (fd, EVIOCGNAME (sizeof (name)), name); printf ("Reading From : %s (%s)\n", device, name); init(); while (1){ if ((rd = read (fd, ev, size * 64)) < size) perror_exit ("read()"); value = ev[0].value; if (value != ' ' && ev[1].value == 1 && ev[1].type == 1){ // Only read the key press event printf ("Code[%d]\n", (ev[1].code)); docode(ev[1].code); } } return 0; }
Multab* mulcon0(long v) { int a1, a2, g; Multab *m, *m1; char hint[10]; if(v < 0) v = -v; /* * look in cache */ m = multab; for(g=0; g<nelem(multab); g++) { if(m->val == v) { if(m->code[0] == 0) return 0; return m; } m++; } /* * select a spot in cache to overwrite */ multabp++; if(multabp < 0 || multabp >= nelem(multab)) multabp = 0; m = multab+multabp; m->val = v; mulval = v; /* * look in execption hint table */ a1 = 0; a2 = hintabsize; for(;;) { if(a1 >= a2) goto no; g = (a2 + a1)/2; if(v < hintab[g].val) { a2 = g; continue; } if(v > hintab[g].val) { a1 = g+1; continue; } break; } if(docode(hintab[g].hint, m->code, 1, 0)) return m; print("multiply table failure %ld\n", v); m->code[0] = 0; return 0; no: /* * try to search */ hint[0] = 0; for(g=1; g<=6; g++) { if(g >= 6 && v >= 65535) break; mulcp = hint+g; *mulcp = 0; if(gen1(g)) { if(docode(hint, m->code, 1, 0)) return m; print("multiply table failure %ld\n", v); break; } } /* * try a recur followed by a shift */ g = 0; while(!(v & 1)) { g++; v >>= 1; } if(g) { m1 = mulcon0(v); if(m1) { strcpy(m->code, m1->code); sprint(strchr(m->code, 0), "%c0", g+'a'); return m; } } m->code[0] = 0; return 0; }
/* * It all starts here... */ int main(int argc, char *argv[]) { int i; char tmpfname[256], *cp, *ifile; opterr = nflag = mflag = 0; active = NULL; fofile = hofile = NULL; /* * Deal with the command-line options. */ while ((i = getopt(argc, argv, "h:t:o:xNmv")) != -1) { switch (i) { case 'h': hofile = optarg; break; case 't': if ((active = findtype(optarg)) == NULL) { fprintf(stderr, "dbow: unsupported code type: %s\n", optarg); exit(1); } break; case 'o': fofile = optarg; break; #ifdef YYDEBUG case 'x': yydebug = 1; break; #endif case 'm': mflag = 1; break; case 'N': nflag = 1; break; case 'v': printf("DBOW version %s\n", DBOW_VERSION); exit(0); break; default: usage(); break; } } /* * The default generator is for C code. */ if (active == NULL && (active = findtype("c")) == NULL) { fprintf(stderr, "dbow: can't find default generator type.\n"); exit(1); } /* * Make sure we have an input file, and call the lexical * analyzer to open it. Report an error if that doesn't work. */ if ((argc - optind) != 1) usage(); ifile = strdup(argv[optind]); if (lexopen(ifile) < 0) { fprintf(stderr, "dbow: "); perror(ifile); exit(1); } /* * If no output file has been specified, then carve an * output file by changing the <file>.d to <file>.<fext> * where 'fext' is the generators file extension. */ if (fofile == NULL) { strncpy(tmpfname, ifile, sizeof(tmpfname) - 5); if ((cp = strrchr(tmpfname, '.')) != NULL && cp[1] == 'd' && cp[2] == '\0') { *cp = '\0'; } else cp = strchr(tmpfname, '\0'); *cp++ = '.'; if (mflag) strcpy(cp, "m4"); else strcpy(cp, active->fext); if ((cp = strrchr(tmpfname, '/')) != NULL) cp++; else cp = tmpfname; fofile = strdup(cp); } /* * Open the pipe to the M4 command. * XXX - don't use M4 when generating SQL (for now!). */ if (mflag || active->cdtype == CDT_DBASE) { if (strcmp(fofile, "-") == 0) fofp = stdout; else if ((fofp = fopen(fofile, "w")) == NULL) { fprintf(stderr, "dbow: cannot open file for writing: "); perror(fofile); exit(1); } hofile = NULL; } else fofp = m4open(fofile, active); linesync(ifile, 1, fofp); if (hofile == NULL) hofp = fofp; else { hofp = m4open(hofile, active); linesync(ifile, 1, hofp); } tofp = NULL; /* * Parse the input file using YACC and close the input stream. * If there are no errors, call each table generator. */ nerrors = 0; yyparse(); lexclose(); if (nerrors == 0) { /* * Perform function optimizations and check all * tables to make sure they have the basic quorum * of functions. */ functioncleanup(); /* * If we've diverted the prolog to a separate file, then * we'll need to include it in the main file. */ if (fofp != hofp) fileinc(hofile, fofp); doproto(ifile, 0); docode(NULL, 0); /* * Emit the epilog code. */ genepilog(fofp); if (hofp != fofp) genepilog(hofp); if (tofp != NULL) { fclose(tofp); if ((tofp = fopen(tofile, "r")) != NULL) { while ((i = fgetc(tofp)) != EOF) fputc(i, fofp); fclose(tofp); } unlink(tofile); } } /* * Close out the output file (and delete it if it's bad), * then exit. */ pclose(fofp); if (hofp != fofp) pclose(hofp); exit(nerrors > 0 ? 1 : 0); }
static int docode(char *hp, char *cp, int r0, int r1) { int c, i; c = *hp++; *cp = c; cp += 2; switch(c) { default: c -= 'a'; if(c < 1 || c >= 30) break; for(i=0; i<4; i++) { switch(i) { case 0: if(docode(hp, cp, r0<<c, r1)) goto out; break; case 1: if(docode(hp, cp, r1<<c, r1)) goto out; break; case 2: if(docode(hp, cp, r0, r0<<c)) goto out; break; case 3: if(docode(hp, cp, r0, r1<<c)) goto out; break; } } break; case '+': for(i=0; i<8; i++) { cp[-1] = i+'0'; switch(i) { case 1: if(docode(hp, cp, r0+r1, r1)) goto out; break; case 5: if(docode(hp, cp, r0, r0+r1)) goto out; break; } } break; case '-': for(i=0; i<8; i++) { cp[-1] = i+'0'; switch(i) { case 1: if(docode(hp, cp, r0-r1, r1)) goto out; break; case 2: if(docode(hp, cp, r1-r0, r1)) goto out; break; case 5: if(docode(hp, cp, r0, r0-r1)) goto out; break; case 6: if(docode(hp, cp, r0, r1-r0)) goto out; break; } } break; case 0: if(r0 == mulval) return 1; } return 0; out: cp[-1] = i+'0'; return 1; }