#include <stdio.h>
#define isksc(c)   ( (unsigned char) (c) > (unsigned char) '\xa0'   && \
       (unsigned char)  (c) < (unsigned char) '\xff' ) 
#define is7ksc(c)   ( (unsigned char) (c) > (unsigned char) '\x20'   && \
       (unsigned char)  (c) < (unsigned char) '\x7f' ) 
#define BUF 4096
#define SI '\017'
#define SO '\016'


FILE *in,*out;
int isdecode;
char line[BUF];
int ishangul=0;

int openfile(char *,char *,FILE **);
int convert();



main (int argc, char **argv)
{

  in = stdin;
  out = stdout;
  isdecode = 0;


  if ( argc > 1 && ! strcmp(argv[1],"-h") ) {
     fprintf(stderr,"Usage: %s [-h] [-u] [input file] [output file]\n",\
                     argv[0]);
     fputs("\t -h : prints this help\n",stderr);
     fputs("\t -u : ISO-2022-KR -> KSC 5601\n",stderr);
     fputs("\t      without '-u', KSC 5601 -> ISO-2022-KR\n",stderr);
     fputs("\t standard input(output) is assumed when\n",stderr); 
     fputs("\t input(output) file isn't specified.\n",stderr);
     exit(0) ; 
  }

  switch (argc) {
   case 4:  
         if ( ! strcmp(argv[1], "-u")) {
              openfile(argv[2],"r",&in);
              openfile(argv[3],"w",&out);
              isdecode = 1;
         }
         else {
              openfile(argv[1],"r",&in);
              openfile(argv[2],"w",&out);
         }
         break;
   case 3: 
         if ( ! strcmp(argv[1], "-u")) {
              openfile(argv[2],"r",&in);
              isdecode = 1;
         }
         else {
              openfile(argv[1],"r",&in);
              openfile(argv[2],"w",&out);
         }
         break;
   case 2: 
         if ( ! strcmp(argv[1], "-u")) {
              isdecode = 1;
         }
         else {
              openfile(argv[1],"r",&in);
         }
   }

   while (  fgets(line,BUF,in) != NULL )  
       convert();
   

   fclose(in);
   fclose(out);

}

#define KSC 1
#define ASCII 0
int convert()
{

   int mode=ASCII;
   int i=0;
   int j=0;
   int c;
   
   if ( !isdecode ) {

      if ( !ishangul )
        while (  line[i] != '\n' && line[i] != EOF  ) {

           if ( isksc(line[i]) ) {
              ishangul = 1;
              fprintf(out,"\033$)C");
              break;
           }
           i++;
        }

      if ( !ishangul) {
         fputs(line,out);
         return;
      }

      i = 0 ;

      while (  line[i] != '\n' && line[i] != EOF ) {

        if ( mode == ASCII && isksc(line[i]))  {
         
          fputc(SO,out);
          fputc(0x7f & line[i],out);
          mode = KSC;
        }
        else if ( mode == ASCII && !isksc(line[i]) )
          fputc(line[i],out);
        else if ( mode == KSC && isksc(line[i]) )
          fputc(0x7f & line[i],out);
        else {
          fputc(SI,out);
          fputc(line[i],out);
          mode = ASCII;
        }
        i++;
     }
     if ( mode == KSC) 
        fputc(SI,out);

     fputc(line[i],out);
       
   } /* end of if */
   else {

/*      if ( ! strncmp(line,"\033$)C",4) ) {
         ishangul = 1;
         i+=4;
      }

      if ( !ishangul) {
         fputs(line,out);
         return(0);
      }

*/

      
      while (  line[i] != '\n' && line[i] != EOF ) {

        if ( ! strncmp(&line[i],"\033$)C",4) ) {
            ishangul = 1;
            i+=4;
            continue;
        }

        if ( ! ishangul )
           fputc(line[i],out);

        else {

           switch( line[i] ) {
   
             case SO:
               mode=KSC;
               break;
             case SI:
               mode=ASCII;
               break;
             default:
               if ( mode==ASCII)
                  fputc(line[i],out);
               else
                  if ( line[i] != '\x20') 
                     fputc(line[i] | 0x80,out);
                  else
                     fputc(line[i],out);
            } /* end of switch */
         }

         i++;

      } /* end of while */

       fputc(line[i],out);

    }  /* end of else */

    return(0);
}    

int openfile(char *name,char *mode,FILE **fp)
{
   if ( (*fp=fopen(name,mode)) == NULL ) {
      fprintf(stderr,"File %s open error !\n", name);
      exit(1);
   }
   return (0);
}   
