home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol181 / addlf.cq / addlf.c
Encoding:
C/C++ Source or Header  |  1985-02-10  |  4.8 KB  |  214 lines

  1. /*
  2.     Program to insert a line feed after carriage return
  3.     in text files with no line feeds in them (i.e. FLEX
  4.     text files)
  5.  
  6. VERSION LIST, most recent version first
  7.  
  8. 25/Apr/84
  9.     ADDLF created from SCRUB. Bill Bolton
  10.  
  11. 26/Sep/82
  12.     Forces MSB of all characters to 0, then scans for control
  13.     codes. TAB, CR and LF are passed unchanged to the output
  14.     file. US (soft hyphen) is replaced by a hard hyphen.
  15.     Checking for legal CP/M filename on destination file
  16.     added. Expanded "usage" message. Added "working" messages.
  17.     Bill Bolton. 
  18.  
  19.     This program was developed from a program called SCRUB
  20.     on BDS "C" User Group disk "Utilities 2" (Volume 2 in
  21.     the Software Tools RCPM BDSCAT.ALL).
  22. */
  23.  
  24. /*
  25.     Macros for constant definitions
  26. */
  27.  
  28. #include BDSCIO.H
  29.  
  30. #define VERSION 1    /* main version number */
  31. #define REVISION 0    /* sub version number */
  32. #define DEL 0x7F    /* ASCII delete character */
  33. #define WORKING 1024    /* number of chars between progress markers */
  34. #define NEXTLINE (WORKING * 32) /* number of progess chars on a screen line */
  35.  
  36. /*
  37.     Argument vector indices
  38. */
  39.  
  40. #define FROM_FILE 1
  41. #define TO_FILE   2
  42.  
  43. /*
  44.     main to open the files for addlf()
  45.     and handle invocation errors.
  46. */
  47.  
  48. main(argc,argv)
  49. int argc;
  50. char *argv[];
  51.  
  52. {
  53.     int fdin,fdout;
  54.     char inbuf[BUFSIZ],outbuf[BUFSIZ];
  55.     char buf[12];
  56.  
  57.     printf("\nChanges CR to CR + LF, Version %d.%d\n",VERSION,REVISION);
  58.     printf("Bill Bolton, Software Tools\n");
  59.     if( argc != 3 )
  60.             usage();
  61.     else {
  62.         if( (fdin = fopen(argv[FROM_FILE],inbuf)) == ERROR){
  63.             printf("\nCannot find file %s\n",argv[FROM_FILE]);
  64.             usage();
  65.         }
  66.         else {
  67.             if(bad_name(argv[TO_FILE])) {
  68.                 printf("\nBad file name %s\n",argv[2]);
  69.                 usage();
  70.             }
  71.             else {
  72.                 if( (fdout  = fcreat(argv[TO_FILE],outbuf)) == ERROR )
  73.                     printf("\nCan't open %s\n",argv[TO_FILE]);
  74.                 else {
  75.                     printf("\nWorking ");
  76.                     addlf(inbuf,outbuf);
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     exit();
  82. }
  83.  
  84. /*
  85.     procedure addlf -- copy file to file adding line feed after each
  86.                carriage return
  87. */
  88.  
  89. addlf(filein,fileout)
  90. char filein[];    /* the input file buffer */
  91. char fileout[];    /* the output file buffer */
  92.  
  93. {
  94.     int c;            /* 1 char buffer */
  95.     unsigned count;        /* count of characters processed */
  96.     unsigned inserted;    /* numbers of bytes inserted */
  97.  
  98.     count  = 0;
  99.     inserted = 0;
  100.  
  101.     while( (c = getc(filein) & 0x7F) != EOF  && c != CPMEOF ){
  102.         count++;
  103.         if (count % WORKING == 0)
  104.             printf("*");        /* still alive */
  105.         if (count % NEXTLINE == 0)
  106.             printf("\n\t");        /* new line every so often */
  107.         if( c >= ' ' && c < '\177' )    /* visable character ? */
  108.             putc(c,fileout);
  109.         else
  110.             switch(c) {
  111.                 case '\r':
  112.                     putc(c,fileout); /* ok control chars */
  113.                     putc('\n',fileout);
  114.                     inserted++;
  115.                     break;
  116.  
  117.                 default:
  118.                     putc(c,fileout);
  119.                     break;
  120.               }
  121.     }
  122.     putc(CPMEOF,fileout);             /* sent textual end of file */
  123.     printf("\n");
  124.     if( fflush(fileout) == ERROR)
  125.         exit(puts("\nOutput file flush error\n"));
  126.     printf("\n%u characters were processed\n",count);
  127.     printf("%u line feeds were inserted\n",inserted);
  128. }
  129.  
  130. int bad_name(buf)
  131.  
  132. char    *buf;
  133.  
  134. {
  135.     char    fcb[36];
  136.  
  137.     setfcb(fcb,buf);
  138.     if (fcb[0] < 0 || fcb[0] > 15)    /* check for valid drive */
  139.         return(ERROR);
  140.     return(valid_check(fcb));
  141. }
  142.  
  143. int valid_check(buf)
  144.  
  145. char    *buf;
  146.  
  147. {
  148.     int    index;
  149.  
  150.     for (index = 1; index < 12; ++index){
  151.         if (bad_char(index,buf))
  152.             return(-1);
  153.     }
  154.     return(0);
  155. }
  156.  
  157. /* Checks if a character is legal in a CP/M directory entry or file control
  158.    block */
  159.  
  160. int bad_char(index,buf)
  161.  
  162. int    index;
  163. char    *buf;
  164.  
  165. {
  166.     char    temp[0];        /* Transient storage for > del test */
  167.     int    space;
  168.  
  169.     if (index == 1 || index == 9)    /* Reset at start of filename & typ */
  170.         space = 0;         
  171.     switch (buf[index]){
  172.     case  '*':
  173.     case  ',':
  174.     case  '.':
  175.     case  ':':
  176.     case  ';':
  177.     case  '<':
  178.     case  '=':
  179.     case  '>':
  180.     case  '?':
  181.     case  '[':
  182.     case  ']':
  183.     case  DEL:
  184.         return(-1);
  185.     case  ' ':            /* Space is conditionally illegal */
  186.         if (space == 7)        /* Filename is all spaces is a */
  187.             return(-1);    /* definite error */
  188.         space++;        /* Else just keep track of how many */
  189.         return(0);
  190.     }
  191.     if (buf[index] < ' ' || (buf[index] >= 'a' && buf[index] <= 'z'))
  192.         return(-1);
  193.     if (buf[index] > DEL){        /* Bit 7 set may be legal attribute */
  194.         *temp = buf[index] & DEL; /* so force it to 0 and try again */
  195.         return (bad_char(0,temp));
  196.     }
  197.     else
  198.         return(space);         /* Space preceeding char IS illegal */
  199. }
  200.  
  201. usage()
  202.  
  203. {
  204.         printf("\nUsage:\n\n");
  205.         printf("\tADDLF d:file1 d:file2\n\n");
  206.         printf("Where:\n");
  207.         printf("\tfile1 = source file, (* and ? not allowed)\n");
  208.         printf("\tfile2 = destination file, (* and ? not allowed)\n");
  209.         printf("\td:    = optional drive identifier\n\n");
  210.         printf("i.e.\tADDLF A:FOOBAR.TXT B:FUBAR.DOC\n");
  211. }
  212.  
  213. /* end of addlf */
  214.