home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / unix_c / textproc / unsoft.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  5.0 KB  |  195 lines

  1.  
  2. /************************************************************************/
  3. /*         Program to convert Wordstar Document-Mode        */
  4. /*        files to plain-vanilla, non-document, ascii format.        */
  5. /*                                    */
  6. /*    by Paul Homchick, One Radnor Station #300, Radnor, PA 19087    */
  7. /* picked up by Richard McGee, BRL, from the SIMTEL archives under      */
  8. /*                  micro:<cpm.txtutl>unsoft1c.c                        */
  9. /* and severely edited to compile under UNIX and to delete extra CR's.  */
  10. /* last edited  on  September 24, 1984                                  */
  11. /* To Compile:   cc -o unsoft -O unsoft.c                               */
  12. /************************************************************************/
  13.  
  14. #include    <stdio.h>
  15. #define CR    0x0d
  16. #define LF    0x0a
  17. #define FF    0x0c        /* form feed ( )            */
  18. #define TAB    0x09
  19. #define CNTRL_O 0x0f        /* non-break space            */
  20. #define END_HYP 0x1f        /* soft hyphen at end of line        */
  21. #define TRUE    1
  22. #define FALSE    0
  23. #define BIGLINE    512        /* number of char in biggest line    */
  24.  
  25. int startline;
  26. FILE *inptr, *outptr, *fopen();
  27. main(argc,argv)
  28. int argc; char *argv[];
  29. {
  30.     int c;
  31.     startline= TRUE;
  32.     switch (argc)
  33.     {
  34.         case 1:
  35.             usage();
  36.             exit(0);
  37.         case 2:
  38.             if ( (!strcmp(argv[1],"h")) ||
  39.                  (!strcmp(argv[1],"H")) )
  40.                 help();
  41.             else
  42.             {
  43.                 usage();
  44.                 error("Not enough arguments on command line.");
  45.                 exit(0);
  46.             }
  47.         case 3:
  48.             break;
  49.         default:
  50.             usage();
  51.             error("Too many arguments on command line.");
  52.             exit(0);
  53.     }
  54.  
  55.     if (strcmp(argv[1],argv[2])==0)
  56.     {
  57.         printf("Input and output filenames must differ.");
  58.         printf("  Aborting...\007\n");
  59.         exit(0);
  60.     }
  61.  
  62.  
  63.     if ((inptr= fopen(argv[1],"r")) == NULL)
  64.     {
  65.         printf("Can't open '%s' for input.\n",argv[1]);
  66.         exit(0);
  67.     }
  68.  
  69.     if ((outptr=fopen(argv[2],"w")) == NULL)
  70.  
  71.     {
  72.         printf("Can't open '%s' for output.",argv[2]);
  73.         exit(0);
  74.     }
  75.  
  76. /************************************************************************/
  77. /*                 main loop                */
  78. /************************************************************************/
  79.  
  80.     printf("processing... ");
  81.     while ((c=getc(inptr))!=EOF)
  82.         putc(translate(c),outptr);
  83.     printf("done.\n");
  84. }
  85.  
  86. /************************************************************************/
  87. /*            wordstar translation routine            */
  88. /************************************************************************/
  89.  
  90. translate(c)
  91. int c;
  92. {
  93. char    buf[BIGLINE];
  94.  
  95.     c&= 0x7f;            /* strip high bit        */
  96.     if (startline)
  97.     {
  98.         switch (c)
  99.         {
  100.             case '.':    /* process dot commands        */
  101.                 fgets(buf,BIGLINE,inptr);
  102.                     /* .pa to form feed        */
  103.                 if ((cmdeq(buf,"PA")) || (cmdeq(buf,"pa")) )
  104.                     fputc(FF,outptr);
  105.                 startline= TRUE;
  106.                 return(translate(c= fgetc(inptr)));
  107.             case LF:
  108.                 return(c);
  109.             default:
  110.                 startline= FALSE;
  111.         }
  112.     }
  113.     if (c < ' ')            /* check for control character    */
  114.     {
  115.         switch (c)
  116.         {
  117.             case END_HYP:
  118.                 return('-');
  119.             case CNTRL_O:
  120.                 return(' ');
  121.             case LF:
  122.             case FF:
  123.             case TAB:
  124.                 return(c);
  125.             case CR:
  126.                 startline= TRUE;
  127.                 return(NULL);
  128.             default:
  129.                 c= fgetc(inptr);
  130.                 return(translate(c));
  131.         }    
  132.     }
  133.     else
  134.         return(c);
  135. }
  136.  
  137. /************************************************************************/
  138. /*            short usage prompt routine            */
  139. /************************************************************************/
  140.  
  141. usage()
  142. {
  143.     printf("usage: unsoft wordstar_input_name ascii_output_name\n");
  144.     printf("   or: unsoft (h | H) for help.\n");
  145.     printf("converts wordstar document-mode files ");
  146.     printf("to plain text format.\n");
  147. }
  148.  
  149. /************************************************************************/
  150. /*            error print routine                */
  151. /************************************************************************/
  152.  
  153. error(s)
  154. char *s;
  155. {
  156.     printf("\007Error: %s\n",s);
  157. }
  158.  
  159. /************************************************************************/
  160. /*            on-line program help routine            */
  161. /************************************************************************/
  162.  
  163. help()
  164. {
  165.     printf("\nUnsoft is a program to filter files prepared under Wordstar\n");
  166.     printf("document mode.  Given a Wordstar document mode file as an\n");
  167.     printf("input, unsoft will output a file having made the following\n");
  168.     printf("transformations:\n\n");
  169.     printf("\to High bits stripped from all characters.\n");
  170.     printf("\to Converts '.pa' dot commands to form feed (^L).\n");
  171.     printf("\to Removes all other dot command lines from file.\n");
  172.     printf("\to Converts 'non-break-space' (^O) to a space.\n");
  173.     printf("\to Converts soft hyphen at end of line (1F hex) to '-'.\n");
  174.     printf("\to Passes through LF, FF, and TAB characters.\n");
  175.     printf("\to Filters out all other control characters and CR.\n");
  176.     printf("\nUsage: unsoft wordstar_input_name");
  177.     printf(" ascii_output_name\n");
  178.     printf("where (names) are input and output file names.  Unsoft h,\n");
  179.     printf("or H will evoke this help message.\n");
  180.     exit(0);
  181. }
  182.  
  183. /************************************************************************/
  184. /*            test input commands                */
  185. /************************************************************************/
  186.  
  187. cmdeq(s,p)
  188. char *s, *p;
  189. {
  190.     while(*p)
  191.         if(*s++ != *p++)
  192.             return(FALSE);
  193.     return(TRUE);
  194. }
  195.