filter.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include "kcc.h"
  6. #include "libkcc.h"
  7. unsigned incode, outcode;
  8. char shiftin[7] = "\033$B";
  9. char shiftout[4] = "\033(J";
  10. /**********************************************************************
  11. * *
  12. * Filter *
  13. * *
  14. **********************************************************************/
  15. enum mode gsmode; /* guess: M_ASCII M_KANJI M_SO */
  16. enum mode inmode; /* input: M_ASCII M_KANJI M_GAIJI
  17. * M_SO M_ESCI */
  18. enum mode outmode; /* output: M_ASCII M_KANJI M_GAIJI
  19. * M_SO M_ESCI */
  20. unsigned long insi; /* JIS shift-in sequence flag */
  21. unsigned long inso; /* JIS shift-out sequence flag
  22. * including "ESC(I" */
  23. unsigned long innj; /* JIS 1990 sequence flag */
  24. unsigned long ingj; /* JIS 1990 aux flag */
  25. bool nogaiji = 0;
  26. /*---------------------------------------------------------------------
  27. NAME
  28. filter - filtering routine
  29. ---------------------------------------------------------------------*/
  30. int KCC_filter(ddd, outcode_name, sss, incode_name, extend, zenkaku, gaiji)
  31. char *sss, *ddd;
  32. int extend, zenkaku;
  33. char *incode_name, *outcode_name;
  34. int gaiji;
  35. {
  36. register bool hold;
  37. register unsigned code, c = ASCII;
  38. register int len;
  39. char str[LENLINE];
  40. char *dummy, *dst;
  41. unsigned incode, outcode;
  42. unsigned size = HOLDBUFSIZ;
  43. char s[3];
  44. s[0]='\0'; s[1]='\0'; s[2]='\0';
  45. nogaiji = gaiji;
  46. if (extend<0) {extend=0;} ; if (extend>1) {extend=1;}
  47. if (zenkaku<0) {zenkaku=0;}; if (zenkaku>1) {zenkaku=1;}
  48. if (nogaiji<0) {nogaiji=0;}; if (nogaiji>1) {nogaiji=1;}
  49. /* allocate hold buf */
  50. if (Kcc_buffalloc(size) == NULL) return (-1);
  51. incode =0; outcode = EUC;
  52. if (!strcasecmp(incode_name,"AUTO")) { incode=0; }
  53. if (!strcasecmp(incode_name,"SJIS")) { incode=SJIS; }
  54. if (!strcasecmp(incode_name,"DEC")) { incode=DEC; }
  55. if (!strcasecmp(incode_name,"JIS")) { incode=JIS; }
  56. if (!strcasecmp(incode_name,"JIS8")) { incode=JIS8; }
  57. if (!strcasecmp(incode_name,"JISI")) { incode=ESCI; }
  58. if (!strcasecmp(outcode_name,"EUC")) { outcode=EUC; }
  59. if (!strcasecmp(outcode_name,"SJIS")) { outcode=SJIS; }
  60. if (!strcasecmp(outcode_name,"DEC")) { outcode=DEC; }
  61. if (!strncasecmp(outcode_name,"JIS", 3))
  62. { outcode=JIS;
  63. if (outcode_name[3]!='\0' && outcode_name[3]!='8' && outcode_name[3]!='I' )
  64. { s[0]=outcode_name[3] ; s[1]=outcode_name[4]; }
  65. }
  66. if (!strncasecmp(outcode_name,"JIS8",4))
  67. { outcode=JIS8;
  68. if (outcode_name[4]!='\0')
  69. { s[0]=outcode_name[4] ; s[1]=outcode_name[5]; }
  70. }
  71. if (!strncasecmp(outcode_name,"JISI",4))
  72. { outcode=ESCI;
  73. if (outcode_name[4]!='\0')
  74. { s[0]=outcode_name[4] ; s[1]=outcode_name[5]; }
  75. }
  76. if ((s[0] == 'B' || s[0] == '@' || s[0] == '+') &&
  77. (s[1] == 'B' || s[1] == 'J' || s[1] == 'H'))
  78. {
  79. if (s[0] == '+')
  80. sprintf(shiftin, "\033&@\033$B");
  81. else
  82. sprintf(shiftin, "\033$%c", s[0]);
  83. sprintf(shiftout, "\033(%c", s[1]);
  84. }
  85. Kcc_setfunc(outcode);
  86. dummy = sss;
  87. dst = ddd;
  88. code = incode ? incode : extend ? BIT8 : BIT8 & ~DEC;
  89. gsmode = inmode = outmode = M_ASCII;
  90. insi = inso = innj = ingj = 0;
  91. hold = 0;
  92. while ((len = Kcc_getstr(str, sizeof str, &dummy)) != 0) {
  93. if ((!(code & NONASCII) && code & BIT8) ||
  94. (code & (EUC | DEC) && code & SJIS && !(code & ASSUME))) {
  95. /*
  96. * So far, no kanji has been seen, or ambiguous.
  97. */
  98. c = Kcc_guess(str, len, extend, zenkaku, &gsmode, &insi, &inso, &innj, &ingj);
  99. code |= c & (JIS | NONASCII), code &= c | ~BIT8;
  100. if (code & NONASCII && code & (EUC | DEC) && code & SJIS) {
  101. /*
  102. * If ambiguous, store the line in hold buffer.
  103. */
  104. if (Kcc_append(str, len)) {
  105. hold = 1;
  106. continue;
  107. }
  108. /*
  109. * When buffer is full, assume EUC/DEC.
  110. */
  111. code |= ASSUME;
  112. }
  113. }
  114. if (hold) {
  115. /*
  116. * Flush hold buffer.
  117. */
  118. Kcc_flush(code, &dst, outcode, &inmode, &insi, &inso, &innj, &ingj);
  119. hold = 0;
  120. }
  121. c = Kcc_out(&dst, str, len, code, outcode, &inmode, &insi, &inso, &innj, &ingj);
  122. code |= c & JIS, code &= c | ~BIT8;
  123. }
  124. if (hold)
  125. /*
  126. * Assume EUC.
  127. */
  128. Kcc_flush(code |= ASSUME, &dst, outcode, &inmode, &insi, &inso, &innj, &ingj);
  129. *dst = '\0';
  130. Kcc_bufffree();
  131. return (Kcc_showcode(c));
  132. }