home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-2 / UNTIP.C < prev    next >
Text File  |  2000-06-30  |  2KB  |  94 lines

  1. /*
  2.     UNTIP.C
  3.     written sometime in 1980 by Leor Zolman
  4.  
  5.     This program takes a file of text that was accumulated during
  6.     a session on ITS where carriage-motion optimization was in effect,
  7.     meaning that there are "linefeed-backspace-backspace...text" sequences
  8.     all through it and it can't be edited using normal CP/M editors,
  9.     and turns it into normal text.
  10.  
  11.     Usage:
  12.         A>untip sourcefile [destfile] <cr>
  13.  
  14.     If only one name is given, a temp file is created and renamed at the
  15.     end to be the same as the source file (i.e., if something goes wrong,
  16.     you might lose the file...but probably not.)
  17. */
  18.  
  19. #include <bdscio.h>
  20. #define TB 0x09
  21. #define EOF 255
  22. #define CR 0x0d
  23. #define BS 0x08
  24. #define LF 0x0a
  25.  
  26. char ibuf[BUFSIZ], obuf[BUFSIZ];
  27. int col, maxcol;
  28. int fd1, fd2;
  29. char lbuf[132];
  30. char c;
  31. int i;
  32.  
  33. main(argc,argv)
  34. char **argv;
  35. {
  36.     if (argc < 2 || argc > 3) {
  37.         printf("usage: untip sourcefile [destfile] <cr>\n");
  38.         exit();
  39.     }
  40.  
  41.     fd1 = fopen(argv[1],ibuf);
  42.  
  43.     if (argc == 2) argv[2] = "untip.tmp";
  44.     fd2 = fcreat(argv[2],obuf);
  45.  
  46.     if (fd1 == ERROR || fd2 == ERROR) {
  47.         printf("Open error.\n");
  48.         exit();
  49.     }
  50.     col = maxcol = 0;
  51.     while ((c=getc(ibuf)) != EOF && c != CPMEOF) {
  52.         if (col > maxcol) maxcol = col;
  53.         switch(c) {
  54.              case CR: col = 0;
  55.                  continue;
  56.  
  57.             case BS: col--;
  58.                  continue;
  59.  
  60.             case LF: putl(lbuf,obuf);
  61.                  for (i=0; i<col; i++) lbuf[i] = ' ';
  62.                  maxcol = col;
  63.                  continue;
  64.  
  65.             case TB: do { lbuf[col++] = ' ';
  66.                   }   while (col%8);
  67.                  continue;
  68.  
  69.             default: lbuf[col++] = c;
  70.          }
  71.      }
  72.     putl(lbuf,obuf);
  73.     putc(CPMEOF,obuf);
  74.     fflush(obuf);
  75.     fclose(ibuf);
  76.     fclose(obuf);
  77.     if (argc == 2) {
  78.         unlink(argv[1]);
  79.         rename(argv[2],argv[1]);
  80.     }
  81. }
  82.  
  83. putl(line,obuf)
  84. char *line, *obuf;
  85. {
  86.     int i;
  87.     for (i = 0; i < maxcol; i++) {
  88.         putc(*line++,obuf);
  89.     }
  90.     putc(CR,obuf);    
  91.     putc(LF,obuf);
  92.     putchar('*');
  93. }
  94.