int main(int argc, char **argv)
{
  gtk_init(&argc, &argv);

#if 1
  if (argc != 3)
    p_err("%s a_file.gtab outfile", argv[0]);
#endif
#if 1
  char *infile = argv[1];
  char *outfile = argv[2];
#else
  char *infile = "data/ar30.gtab";
  char *outfile = "l";
#endif

  FILE *fr;
  if ((fr=fopen(infile, "rb"))==NULL)
      p_err("cannot err open %s", infile);

  FILE *fp_out;
  if ((fp_out=fopen(outfile,"w"))==NULL) {
    printf("Cannot open %s", outfile);
    exit(-1);
  }

  struct TableHead th;
  fread(&th,1, sizeof(th), fr);
#if NEED_SWAP
  swap_byte_4(&th.version);
  swap_byte_4(&th.flag);
  swap_byte_4(&th.space_style);
  swap_byte_4(&th.KeyS);
  swap_byte_4(&th.MaxPress);
  swap_byte_4(&th.M_DUP_SEL);
  swap_byte_4(&th.DefC);
  for(i=0; i <= KeyNum; i++)
    swap_byte_4(&idx1[i]);
#endif
  int KeyNum = th.KeyS;
  dbg("keys %d\n",KeyNum);

  if (!th.keybits)
    th.keybits = 6;
  dbg("keybits:%d  maxPress:%d\n", th.keybits, th.MaxPress);

  int max_keyN;
  if (th.MaxPress*th.keybits > 32) {
    max_keyN = 64 / th.keybits;
    key64 = TRUE;
    dbg("it's a 64-bit .gtab\n");
  } else {
    max_keyN = 32 / th.keybits;
    key64 = FALSE;
  }

  dbg("key64:%d\n", key64);

  char kname[128][CH_SZ];
  char keymap[128];
  gtab_idx1_t idx1[256];
  static char kno[128];

  itN = th.DefC;

  bzero(keymap, sizeof(keymap));
  fread(keymap, 1, th.KeyS, fr);
  fread(kname, CH_SZ, th.KeyS, fr);
  fread(idx1, sizeof(gtab_idx1_t), KeyNum+1, fr);

  int i;
  for(i=0; i < th.KeyS; i++) {
    kno[keymap[i]] = i;
  }

  fprintf(fp_out,TSIN_GTAB_KEY" %d %d %s\n", th.keybits, th.MaxPress, keymap+1);

  if (key64) {
    fread(it64, sizeof(ITEM64), th.DefC, fr);
    qsort(it64, th.DefC, sizeof(ITEM64), qcmp_ch64);
  }
  else {
    fread(it, sizeof(ITEM), th.DefC, fr);
    qsort(it, th.DefC, sizeof(ITEM), qcmp_ch);
  }

  itN = th.DefC;

//  dbg("itN:%d\n", itN);
#if 0
  for(i=0; i < itN; i++) {
    printf("\n%d ", i);
    utf8_putchar(it64[i].ch);
  }
#endif

  fclose(fr);

  char fname[128];
  get_gcin_user_fname(tsin32_f, fname);

  FILE *fp;
  if ((fp=fopen(fname,"rb"))==NULL) {
    printf("Cannot open %s", fname);
    exit(-1);
  }


  while (!feof(fp)) {
    int i;
    phokey_t phbuf[MAX_PHRASE_LEN];
    u_char clen;
    usecount_t usecount;

    fread(&clen,1,1,fp);
    fread(&usecount, sizeof(usecount_t), 1,fp);
    fread(phbuf,sizeof(phokey_t), clen, fp);

    char str[MAX_PHRASE_LEN * CH_SZ + 1];
    int strN = 0;
    KKARR kk[MAX_PHRASE_LEN];
    KKARR64 kk64[MAX_PHRASE_LEN];
    gboolean has_err = FALSE;

    if (key64)
      bzero(kk64, sizeof(kk64));
    else
      bzero(kk, sizeof(kk));

//    dbg("clen %d\n", clen);
    for(i=0;i<clen;i++) {
      char ch[CH_SZ];

      int n = fread(ch, 1, 1, fp);
      if (n<=0)
        goto stop;

      int len=utf8_sz(ch);

      fread(&ch[1], 1, len-1, fp);
//      utf8_putchar(ch);

      if (key64) {
        if (!(kk64[i].arr = find_ch64(ch, &kk64[i].N)))
          has_err = TRUE;
      } else {
        if (!(kk[i].arr = find_ch(ch, &kk[i].N)))
          has_err = TRUE;
      }

      memcpy(str+strN, ch, len);
      strN+=len;
    }

    if (has_err) {
//      dbg("has_error\n");
      continue;
    }
#if 0
    for(i=0; i < clen; i++)
      printf("%d ", kk64[i].N);
    printf("\n");
#endif
    str[strN]=0;

    int permN;
    if (key64) {
      permN=kk64[0].N;
      for(i=1;i<clen;i++)
        permN *= kk64[i].N;
    }
    else {
      permN=kk[0].N;
      for(i=1;i<clen;i++)
        permN *= kk[i].N;
    }

    int z;
    for(z=0; z < permN; z++) {
      char vz[MAX_PHRASE_LEN];

      int tz = z;

      if (key64) {
        for(i=0; i < clen; i++) {
          vz[i] = tz % kk64[i].N;
          tz /= kk64[i].N;
        }
      } else {
        for(i=0; i < clen; i++) {
          vz[i] = tz % kk[i].N;
          tz /= kk[i].N;
        }
      }

      char kstr[512];
      kstr[0]=0;

      for(i=0;i<clen;i++) {
         char tkey[16];
         u_int64_t k=0;

         if (key64) {
           memcpy(&k, kk64[i].arr[vz[i]].key, 8);
         } else {
           u_int t;
           memcpy(&t, kk[i].arr[vz[i]].key, 4);
           k = t;
         }

         get_keymap_str(k, keymap, th.keybits, tkey);

         strcat(kstr, tkey);
         strcat(kstr, " ");
      }

      fprintf(fp_out,"%s %s%d\n", str, kstr, usecount);
    }
  }
stop:

  fclose(fp);
  fclose(fp_out);
  return 0;
}
Example #2
0
static
int _parse_http_request(char *buf, ssize_t buf_len, VALUE env) {
  const char* method;
  size_t method_len;
  const char* path;
  size_t path_len;
  int minor_version;
  struct phr_header headers[MAX_HEADERS];
  size_t num_headers, question_at;
  size_t i;
  int ret;
  char tmp[MAX_HEADER_NAME_LEN + sizeof("HTTP_") - 1];
  VALUE last_value;

  num_headers = MAX_HEADERS;
  ret = phr_parse_request(buf, buf_len, &method, &method_len, &path,
                          &path_len, &minor_version, headers, &num_headers, 0);
  if (ret < 0)
    goto done;

  rb_hash_aset(env, request_method_key, rb_str_new(method,method_len));
  rb_hash_aset(env, request_uri_key, rb_str_new(path, path_len));
  rb_hash_aset(env, script_name_key, rb_str_new2(""));
  strcpy(tmp, "HTTP/1.");
  tmp[7] = 48 + ((minor_version > 1 || minor_version < 0 ) ? 0 : minor_version);
  rb_hash_aset(env, server_protocol_key, rb_str_new(tmp, sizeof("HTTP/1.0") - 1));

  /* PATH_INFO QUERY_STRING */
  path_len = find_ch(path, path_len, '#'); /* strip off all text after # after storing request_uri */
  question_at = find_ch(path, path_len, '?');
  if ( store_path_info(env, path, question_at) < 0 ) {
    rb_hash_clear(env);
    ret = -1;
    goto done;
  }
  if (question_at != path_len) ++question_at;
  rb_hash_aset(env, query_string_key, rb_str_new(path + question_at, path_len - question_at));
  last_value = Qnil;
  for (i = 0; i < num_headers; ++i) {
    if (headers[i].name != NULL) {
      const char* name;
      size_t name_len;
      VALUE slot;
      VALUE env_key;
      env_key = find_common_header(headers + i);
      if ( env_key == Qnil ) {
        const char* s;
        char* d;
        size_t n;
        if (sizeof(tmp) - 5 < headers[i].name_len) {
          rb_hash_clear(env);
          ret = -1;
          goto done;
        }
        strcpy(tmp, "HTTP_");
        for (s = headers[i].name, n = headers[i].name_len, d = tmp + 5;
          n != 0;
          s++, --n, d++) {
            *d = *s == '-' ? '_' : TOU(*s);
            name = tmp;
            name_len = headers[i].name_len + 5;
            env_key = rb_str_new(name, name_len);
        }
      }
      slot = rb_hash_aref(env, env_key);
      if ( slot != Qnil ) {
        rb_str_cat2(slot, ", ");
        rb_str_cat(slot, headers[i].value, headers[i].value_len);
      } else {
        slot = rb_str_new(headers[i].value, headers[i].value_len);
        rb_hash_aset(env, env_key, slot);
        last_value = slot;
      }
    } else {
      /* continuing lines of a mulitiline header */
      if ( last_value != Qnil )
        rb_str_cat(last_value, headers[i].value, headers[i].value_len);
    }
  }

 done:
  return ret;
}
Example #3
0
int
main(int argc, char **argv)
{
   FILE *file;

   cons_CharsetEntry *cp = 0;

   int len = 0, i, mb = 0;

   char buf[4096];

   UniRecord *rp;

   /* unicode-order */
   Coll uni;

   /* charset-order */
   Coll cs;

   Coll out;

   Coll cmp;

   char *fname;

   if (argc < 2)
   {
      fprintf(stderr, "usage: %s charset_file [cs_file1...] < unicode_data\n", argv[0]);
      exit(1);
   }

   for (i = 1; i < argc; i++)
   {
      fname = argv[i];
      file = fopen(fname, "r");
      if (!file)
      {
	 fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
	 exit(2);
      }

      fprintf(stderr, "load charset '%s'\n", fname);
      if (load_charset(file, &cp, &len))
      {
	 fprintf(stderr, "cannot read %s: %s\n", fname, strerror(errno));
	 exit(3);
      }
      fprintf(stderr, "loaded %d entries\n", len);
      fclose(file);
#ifdef DBG
      fprintf(stderr, "readed %d charset entries\n", len);
#endif
   }

   init_Coll(&uni, 0, cmp_UniRecord);
   init_Coll(&cs, 0, cmp_CSRecord);
   init_Coll(&out, 0, 0);
   init_Coll(&cmp, 0, cmp_UniRecord);

   while (fgets(buf, sizeof(buf), stdin))
   {
      int l;

      char *s;

      rp = (UniRecord *) calloc(1, sizeof(*rp));
      l = strlen(buf);
      if (l > 0 && buf[--l] == '\n')
	 buf[l] = 0;
      if (!l)
	 break;

      s = strdup(buf);
		rp->mem_of_UniRecord = s;

		rp->hex_of_UniRecord = s;
		rp->no_of_UniRecord = strtoul(s, 0, 16);
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->name_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->cat_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->comb_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->bidir_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->decomp_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->dec_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->dig_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->num_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->mirror_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->name1_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->comment_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->upper_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->lower_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->title_of_UniRecord = s;
      l = strcspn(s, ";");
      s += l;
      *s++ = 0;

		rp->cp_of_UniRecord = in_map(cp, len, rp->no_of_UniRecord);
		if (rp->cp_of_UniRecord)
      {
			remove_char(&uni, rp->cp_of_UniRecord->ch_of_cons_CharsetEntry);
			remove_char(&cs, rp->cp_of_UniRecord->ch_of_cons_CharsetEntry);
	 insert_Coll(&uni, rp);
	 insert_Coll(&cs, rp);
      }
      else
      {
			free(rp->mem_of_UniRecord);
	 free(rp);
      }
   }
   fprintf(stderr, "appended %d uni %d chars\n", uni.count_of_Coll, cs.count_of_Coll);

#ifdef DBG
fprintf(stderr, "uni: %d\n", uni.count_of_Coll);
for (i = 0; i < uni.count_of_Coll; i++)
   {
      rp = (UniRecord *) uni.items[i];
		fprintf(stderr, "hex; 	'%s'\n", rp->hex_of_UniRecord);
		fprintf(stderr, "name;   '%s'\n", rp->name_of_UniRecord);
		fprintf(stderr, "cat;    '%s'\n", rp->cat_of_UniRecord);
		fprintf(stderr, "comb;   '%s'\n", rp->comb_of_UniRecord);
		fprintf(stderr, "bidir;  '%s'\n", rp->bidir_of_UniRecord);
		fprintf(stderr, "decomp; '%s'\n", rp->decomp_of_UniRecord);
		fprintf(stderr, "dec;    '%s'\n", rp->dec_of_UniRecord);
		fprintf(stderr, "dig;    '%s'\n", rp->dig_of_UniRecord);
		fprintf(stderr, "num;    '%s'\n", rp->num_of_UniRecord);
		fprintf(stderr, "mirror; '%s'\n", rp->mirror_of_UniRecord);
		fprintf(stderr, "name1;  '%s'\n", rp->name1_of_UniRecord);
		fprintf(stderr, "comment;'%s'\n", rp->comment_of_UniRecord);
		fprintf(stderr, "upper;  '%s'\n", rp->upper_of_UniRecord);
		fprintf(stderr, "lower;  '%s'\n", rp->lower_of_UniRecord);
		fprintf(stderr, "title;  '%s'\n", rp->title_of_UniRecord);
      fprintf(stderr, "\n");
   }
#endif
#ifdef DBG1
fprintf(stderr, "cs: %d\n", cs.count_of_Coll);
for (i = 0; i < cs.count_of_Coll; i++)
   {
      rp = (UniRecord *) cs.items[i];
		fprintf(stderr, "ch=%d\n", rp->cp_of_UniRecord->ch_of_cons_CharsetEntry);
		fprintf(stderr, "hex; 	'%s'\n", rp->hex_of_UniRecord);
		fprintf(stderr, "name;   '%s'\n", rp->name_of_UniRecord);
		fprintf(stderr, "cat;    '%s'\n", rp->cat_of_UniRecord);
		fprintf(stderr, "comb;   '%s'\n", rp->comb_of_UniRecord);
		fprintf(stderr, "bidir;  '%s'\n", rp->bidir_of_UniRecord);
		fprintf(stderr, "decomp; '%s'\n", rp->decomp_of_UniRecord);
		fprintf(stderr, "dec;    '%s'\n", rp->dec_of_UniRecord);
		fprintf(stderr, "dig;    '%s'\n", rp->dig_of_UniRecord);
		fprintf(stderr, "num;    '%s'\n", rp->num_of_UniRecord);
		fprintf(stderr, "mirror; '%s'\n", rp->mirror_of_UniRecord);
		fprintf(stderr, "name1;  '%s'\n", rp->name1_of_UniRecord);
		fprintf(stderr, "comment;'%s'\n", rp->comment_of_UniRecord);
		fprintf(stderr, "upper;  '%s'\n", rp->upper_of_UniRecord);
		fprintf(stderr, "lower;  '%s'\n", rp->lower_of_UniRecord);
		fprintf(stderr, "title;  '%s'\n", rp->title_of_UniRecord);
      fprintf(stderr, "\n");
   }
#endif

   for (i = 0; i < 256; i++)
   {
      UniRecord *rp = find_ch(&cs, i);

      if (!rp)
      {
	 cons_CharsetEntry *cp = (cons_CharsetEntry *) calloc(sizeof(cons_CharsetEntry *), 1);

	 rp = (UniRecord *) calloc(sizeof(UniRecord), 1);
	 rp->no_of_UniRecord = i;
	 rp->ch_of_UniRecord = i;
	 cp->ch_of_cons_CharsetEntry = i;
	 cp->unich_of_cons_CharsetEntry = i;
	 rp->cp_of_UniRecord = cp;
	 rp->hex_of_UniRecord = "";
	 rp->name_of_UniRecord = "";
	 rp->cat_of_UniRecord = "";
	 rp->comb_of_UniRecord = "";
	 rp->bidir_of_UniRecord = "";
	 rp->decomp_of_UniRecord = "";
	 rp->dec_of_UniRecord = "";
	 rp->dig_of_UniRecord = "";
	 rp->num_of_UniRecord = "";
	 rp->mirror_of_UniRecord = "";
	 rp->name1_of_UniRecord = "";
	 rp->comment_of_UniRecord = "";
	 rp->upper_of_UniRecord = "";
	 rp->lower_of_UniRecord = "";
	 rp->title_of_UniRecord = "";
      }
            if (rp->cp_of_UniRecord->ch_of_cons_CharsetEntry < 256)
      {
	 append_Coll(&out, rp);
	 insert_Coll(&cmp, rp);
      }
   }

#ifdef DBG3
fprintf(stderr, "cmp: %d\n", cmp.count_of_Coll);
for (i = 0; i < cmp.count_of_Coll; i++)
   {
		rp = (UniRecord *) cmp.items_of_Coll[i];
		fprintf(stderr, "ch=%d\n", rp->cp_of_UniRecord->ch_of_cons_CharsetEntry);
		fprintf(stderr, "hex; 	'%s'\n", rp->hex_of_UniRecord);
		fprintf(stderr, "name;   '%s'\n", rp->name_of_UniRecord);
		fprintf(stderr, "cat;    '%s'\n", rp->cat_of_UniRecord);
		fprintf(stderr, "comb;   '%s'\n", rp->comb_of_UniRecord);
		fprintf(stderr, "bidir;  '%s'\n", rp->bidir_of_UniRecord);
		fprintf(stderr, "decomp; '%s'\n", rp->decomp_of_UniRecord);
		fprintf(stderr, "dec;    '%s'\n", rp->dec_of_UniRecord);
		fprintf(stderr, "dig;    '%s'\n", rp->dig_of_UniRecord);
		fprintf(stderr, "num;    '%s'\n", rp->num_of_UniRecord);
		fprintf(stderr, "mirror; '%s'\n", rp->mirror_of_UniRecord);
		fprintf(stderr, "name1;  '%s'\n", rp->name1_of_UniRecord);
		fprintf(stderr, "comment;'%s'\n", rp->comment_of_UniRecord);
		fprintf(stderr, "upper;  '%s'\n", rp->upper_of_UniRecord);
		fprintf(stderr, "lower;  '%s'\n", rp->lower_of_UniRecord);
		fprintf(stderr, "title;  '%s'\n", rp->title_of_UniRecord);
      fprintf(stderr, "\n");
   }
#endif

   printf("# generated by gen_tbl from sources");
   for (i = 1; i < argc; i++)
      printf(" %s", argv[i]);
   printf("\n# cmptbl\n");
   for (i = 0; i < 256; i++)
   {
      int ch;

      ch = find_cmp(&cmp, i);
      printf("%d\n", ch);
   }
   printf("# uptbl\n");
	for (i = 0; i < out.count_of_Coll; i++)
   {
      UniRecord *rp1;

      int ch;

		rp = (UniRecord *) out.items_of_Coll[i];
      if (!rp)
      {
	 printf("%d\n", i);
	 continue;
      }
      if (!strcasecmp(rp->cat_of_UniRecord, "Ll") && rp->upper_of_UniRecord)
      {
			unsigned long unich = strtoul(rp->upper_of_UniRecord, 0, 16);

	 rp1 = find_uni(&uni, unich);
	 if (rp1)
		 ch = rp1->cp_of_UniRecord->ch_of_cons_CharsetEntry;
	 else
		 ch = rp->cp_of_UniRecord->ch_of_cons_CharsetEntry;
      }
      else
			ch = rp->cp_of_UniRecord->ch_of_cons_CharsetEntry;

      printf("%d\n", ch);
   }

   printf("# lowtbl\n");
	for (i = 0; i < out.count_of_Coll; i++)
   {
      UniRecord *rp1;

      int ch;

		rp = (UniRecord *) out.items_of_Coll[i];
      if (!rp)
      {
	 printf("%d\n", i);
	 continue;
      }
      if (!strcasecmp(rp->cat_of_UniRecord, "Lu") && rp->lower_of_UniRecord)
      {
			unsigned long unich = strtoul(rp->lower_of_UniRecord, 0, 16);

	 rp1 = find_uni(&uni, unich);
	 if (rp1)
		 ch = rp1->cp_of_UniRecord->ch_of_cons_CharsetEntry;
	 else
		 ch = rp->cp_of_UniRecord->ch_of_cons_CharsetEntry;
      }
      else
			ch = rp->cp_of_UniRecord->ch_of_cons_CharsetEntry;

      printf("%d\n", ch);
   }

   printf("# isalpha\n");
	for (i = 0; i < out.count_of_Coll; i++)
   {
      int r = 0;

      char *s;

		rp = (UniRecord *) out.items_of_Coll[i];
      if (!rp)
      {
	 printf("0\n");
	 continue;
      }
      s = rp->cat_of_UniRecord;
      if (s && s[0] == 'L')
	 r = 1;
      printf("%d\n", r);
   }

   printf("# pgtbl\n");
	for (i = 0; i < out.count_of_Coll; i++)
   {
      int r = 0;

      char *s;

		rp = (UniRecord *) out.items_of_Coll[i];
      if (!rp)
      {
	 printf("0\n");
	 continue;
      }
      s = rp->cat_of_UniRecord;
		if (!strcmp(s, "So") && rp->no_of_UniRecord >= 0x2500 && rp->no_of_UniRecord < 0x25ff)
			r = pg_chars[rp->no_of_UniRecord - 0x2500];
      printf("%d\n", r);
   }

   printf("# multibyte\n%d\n", mb ? 1 : 0);

   return 0;
}