home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / refont < prev    next >
Encoding:
Text File  |  1989-12-31  |  6.9 KB  |  440 lines

  1. /* refont.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    16820 SW Tallac Way
  6.  *    Beaverton, OR 97006
  7.  *    kirkenda@jove.cs.pdx.edu, or ...uunet!tektronix!psueea!jove!kirkenda
  8.  */
  9.  
  10.  
  11. /* This file contains the complete source code for the refont program */
  12.  
  13. /* The refont program converts font designations to the format of your choice.
  14.  * Known font formats are:
  15.  *    -b    overtype notation, using backspaces
  16.  *    -c    overtype notation, using carriage returns
  17.  *    -d    the "dot" command notation used by nroff
  18.  *    -e    Epson-compatible line printer codes
  19.  *    -f    the "\f" notation
  20.  *    -x    none (strip out the font codes)
  21.  */
  22.  
  23. #include <stdio.h>
  24.  
  25. /* the program name, for diagnostics */
  26. char    *progname;
  27.  
  28. /* remembers which output format to use */
  29. outfmt = 'f';
  30.  
  31. main(argc, argv)
  32.     int    argc;    /* number of command-line args */
  33.     char    **argv;    /* values of the command line args */
  34. {
  35.     FILE    *fp;
  36.     int    i;
  37.  
  38.     progname = argv[0];
  39.  
  40.     /* parse the flags */
  41.     i = 1;
  42.     if (i < argc && *argv[i] == '-')
  43.     {
  44.         if (argv[i][1] >= 'b' && argv[i][1] <= 'f' || argv[i][1] == 'x')
  45.         {
  46.             outfmt = argv[i][1];
  47.         }
  48.         else
  49.         {
  50.             usage();
  51.         }
  52.         i++;
  53.     }
  54.  
  55.     if (i == argc)
  56.     {
  57.         /* probably shouldn't read from keyboard */
  58.         if (isatty(fileno(stdin)))
  59.         {
  60.             usage();
  61.         }
  62.  
  63.         /* no files named, so use stdin */
  64.         refont(stdin);
  65.     }
  66.     else
  67.     {
  68.         for (; i < argc; i++)
  69.         {
  70.             fp = fopen(argv[i], "r");
  71.             if (!fp)
  72.             {
  73.                 perror(argv[i]);
  74.             }
  75.             else
  76.             {
  77.                 refont(fp);
  78.                 if (i < argc - 1)
  79.                 {
  80.                     putchar('\f');
  81.                 }
  82.                 fclose(fp);
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. usage()
  89. {
  90.     fputs("usage: ", stderr);
  91.     fputs(progname, stderr);
  92.     fputs(" [-bcdefx] [filename]...\n", stderr);
  93.     exit(2);
  94. }
  95.  
  96. /* This function does the refont thing to a single file */
  97. /* I apologize for the length of this function.  It is gross. */
  98. refont(fp)
  99.     FILE    *fp;
  100. {
  101.     char    textbuf[1025];    /* chars of a line of text */
  102.     char    fontbuf[1025];    /* fonts of chars in fontbuf */
  103.     int    col;        /* where we are in the line */
  104.     int    font;        /* remembered font */
  105.     int    more;        /* more characters to be output? */
  106.     int    ch;
  107.  
  108.     /* reset some stuff */
  109.     for (col = sizeof fontbuf; --col >= 0; )
  110.     {
  111.         fontbuf[col] = 'R';
  112.         textbuf[col] = '\0';
  113.     }
  114.     col = 0;
  115.     font = 'R';
  116.  
  117.     /* get the first char - quit if eof */
  118.     while ((ch = getc(fp)) != EOF)
  119.     {
  120.         /* if "dot" command, read the rest of the command */
  121.         if (ch == '.' && col == 0)
  122.         {
  123.  
  124.             textbuf[col++] = '.';
  125.             textbuf[col++] = getc(fp);
  126.             textbuf[col++] = getc(fp);
  127.             textbuf[col++] = ch = getc(fp);
  128.  
  129.             /* is it a font line? */
  130.             font = 0;
  131.             if (textbuf[1] == 'u' && textbuf[2] == 'l')
  132.             {
  133.                 font = 'U';
  134.             }
  135.             else if (textbuf[1] == 'b' && textbuf[2] == 'o')
  136.             {
  137.                 font = 'B';
  138.             }
  139.             else if (textbuf[1] == 'i' && textbuf[2] == 't')
  140.             {
  141.                 font = 'I';
  142.             }
  143.  
  144.             /* if it is, discard the stuff so far but remember font */
  145.             if (font)
  146.             {
  147.                 while (col > 0)
  148.                 {
  149.                     textbuf[--col] = '\0';
  150.                 }
  151.             }
  152.             else /* some other format line - write it literally */
  153.             {
  154.                 while (ch != '\n')
  155.                 {
  156.                     textbuf[col++] = ch = getc(fp);
  157.                 }
  158.                 fputs(textbuf, fp);
  159.                 while (col > 0)
  160.                 {
  161.                     textbuf[--col] = '\0';
  162.                 }
  163.             }
  164.             continue;
  165.         }
  166.  
  167.         /* is it a "\f" formatted font? */
  168.         if (ch == '\\')
  169.         {
  170.             ch = getc(fp);
  171.             if (ch == 'f')
  172.             {
  173.                 font = getc(fp);
  174.             }
  175.             else
  176.             {
  177.                 textbuf[col++] = '\\';
  178.                 textbuf[col++] = ch;
  179.             }
  180.             continue;
  181.         }
  182.  
  183.         /* is it an Epson font? */
  184.         if (ch == '\033')
  185.         {
  186.             ch = getc(fp);
  187.             switch (ch)
  188.             {
  189.               case '4':
  190.                 font = 'I';
  191.                 break;
  192.  
  193.               case 'E':
  194.               case 'G':
  195.                 font = 'B';
  196.                 break;
  197.  
  198.               case '5':
  199.               case 'F':
  200.               case 'H':
  201.                 font = 'R';
  202.                 break;
  203.  
  204.               case '-':
  205.                 font = (getc(fp) & 1) ? 'U' : 'R';
  206.                 break;
  207.             }
  208.             continue;
  209.         }
  210.  
  211.         /* control characters? */
  212.         if (ch == '\b')
  213.         {
  214.             if (col > 0)
  215.                 col--;
  216.             continue;
  217.         }
  218.         else if (ch == '\t')
  219.         {
  220.             do
  221.             {
  222.                 if (textbuf[col] == '\0')
  223.                 {
  224.                     textbuf[col] = ' ';
  225.                 }
  226.                 col++;
  227.             } while (col & 7);
  228.             continue;
  229.         }
  230.         else if (ch == '\r')
  231.         {
  232.             col = 0;
  233.             continue;
  234.         }
  235.         else if (ch == ' ')
  236.         {
  237.             if (textbuf[col] == '\0')
  238.             {
  239.                 textbuf[col] = ' ';
  240.                 fontbuf[col] = font;
  241.                 col++;
  242.             }
  243.             continue;
  244.         }
  245.  
  246.         /* newline? */
  247.         if (ch == '\n')
  248.         {
  249. #if 0
  250.             fputs(textbuf, stdout);
  251. #else
  252.             more = 0;
  253.             for (col = 0, font = 'R'; textbuf[col]; col++)
  254.             {
  255.                 if (fontbuf[col] != font)
  256.                 {
  257.                     switch (outfmt)
  258.                     {
  259.                       case 'd':
  260.                         putchar('\n');
  261.                         switch (fontbuf[col])
  262.                         {
  263.                           case 'B':
  264.                             fputs(".bo\n", stdout);
  265.                             break;
  266.  
  267.                           case 'I':
  268.                             fputs(".it\n", stdout);
  269.                             break;
  270.  
  271.                           case 'U':
  272.                             fputs(".ul\n", stdout);
  273.                             break;
  274.                         }
  275.                         while (textbuf[col] == ' ')
  276.                         {
  277.                             col++;
  278.                         }
  279.                         break;
  280.  
  281.                       case 'e':
  282.                         switch (fontbuf[col])
  283.                         {
  284.                           case 'B':
  285.                             fputs("\033E", stdout);
  286.                             break;
  287.  
  288.                           case 'I':
  289.                             fputs("\0334", stdout);
  290.                             break;
  291.  
  292.                           case 'U':
  293.                             fputs("\033-1", stdout);
  294.                             break;
  295.  
  296.                           default:
  297.                             switch (font)
  298.                             {
  299.                               case 'B':
  300.                                 fputs("\033F", stdout);
  301.                                 break;
  302.  
  303.                               case 'I':
  304.                                   fputs("\0335", stdout);
  305.                                   break;
  306.  
  307.                               case 'U':
  308.                                   fputs("\033-0", stdout);
  309.                                   break;
  310.                             }
  311.                         }
  312.                         break;
  313.  
  314.                       case 'f':
  315.                           putchar('\\');
  316.                           putchar('f');
  317.                           putchar(fontbuf[col]);
  318.                           break;
  319.                     }
  320.  
  321.                     font = fontbuf[col];
  322.                 }
  323.  
  324.                 if (fontbuf[col] != 'R' && textbuf[col] != ' ')
  325.                 {
  326.                     switch (outfmt)
  327.                     {
  328.                       case 'b':
  329.                         if (fontbuf[col] == 'B')
  330.                         {
  331.                             putchar(textbuf[col]);
  332.                         }
  333.                         else
  334.                         {
  335.                             putchar('_');
  336.                         }
  337.                         putchar('\b');
  338.                         break;
  339.  
  340.                       case 'c':
  341.                           more = col + 1;
  342.                           break;
  343.                     }
  344.                 }
  345.  
  346.                 putchar(textbuf[col]);
  347.             }
  348.  
  349.             /* another pass? */
  350.             if (more > 0)
  351.             {
  352.                 putchar('\r');
  353.                 for (col = 0; col < more; col++)
  354.                 {
  355.                     switch (fontbuf[col])
  356.                     {
  357.                       case 'I':
  358.                       case 'U':
  359.                         putchar('_');
  360.                         break;
  361.  
  362.                       case 'B':
  363.                         putchar(textbuf[col]);
  364.                         break;
  365.  
  366.                       default:
  367.                         putchar(' ');
  368.                     }
  369.                 }
  370.             }
  371.  
  372.             /* newline */
  373.             if (font != 'R')
  374.             {
  375.                 switch (outfmt)
  376.                 {
  377.                   case 'f':
  378.                     putchar('\\');
  379.                     putchar('f');
  380.                     putchar('R');
  381.                     break;
  382.  
  383.                   case 'e':
  384.                     switch (font)
  385.                     {
  386.                       case 'B':
  387.                         fputs("\033F", stdout);
  388.                         break;
  389.  
  390.                       case 'I':
  391.                           fputs("\0335", stdout);
  392.                           break;
  393.  
  394.                       case 'U':
  395.                           fputs("\033-0", stdout);
  396.                           break;
  397.                     }
  398.                 }
  399.             }
  400. #endif
  401.             putchar('\n');
  402.  
  403.             /* reset some stuff */
  404.             for (col = sizeof fontbuf; --col >= 0; )
  405.             {
  406.                 fontbuf[col] = 'R';
  407.                 textbuf[col] = '\0';
  408.             }
  409.             col = 0;
  410.             font = 'R';
  411.             continue;
  412.         }
  413.  
  414.         /* normal character */
  415.         if (font != 'R')
  416.         {
  417.             textbuf[col] = ch;
  418.             fontbuf[col] = font;
  419.         }
  420.         else if (textbuf[col] == '_')
  421.         {
  422.             textbuf[col] = ch;
  423.             fontbuf[col] = 'U';
  424.         }
  425.         else if (textbuf[col] && textbuf[col] != ' ' && ch == '_')
  426.         {
  427.             fontbuf[col] = 'U';
  428.         }
  429.         else if (textbuf[col] == ch)
  430.         {
  431.             fontbuf[col] = 'B';
  432.         }
  433.         else
  434.         {
  435.             textbuf[col] = ch;
  436.         }
  437.         col++;
  438.     }
  439. }
  440.