home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / beehive / utilitys / convert.arc / CONVERT.C < prev    next >
Text File  |  1990-07-21  |  9KB  |  434 lines

  1. /*
  2.  *    CONVERT.C
  3.  *
  4.  *    converts text files from one operating system format to another.
  5.  *
  6.  *    SYPNOSIS:
  7.  *        convert [-r] [-v] [-w] [[[-OPTION] [filename] ... ] ... ]
  8.  *    where OPTION may be one of the following:
  9.  *    st    converts CR-LF sequence to CR (mnemonic: System to TRSDOS)
  10.  *    su    converts CR-LF sequence to LF (mnemonic: System to Unix)
  11.  *    ts    converts CR to CR-LF sequence (mnemonic: TRSDOS to System)
  12.  *    tu    converts CR to LF             (mnemonic: TRSDOS to Unix)
  13.  *    us    converts LF to CR-LF (DEFAULT)(mnemonic: Unix to System)
  14.  *    ut    converts LF to CR             (mnemonic: Unix to TRSDOS)
  15.  *    System stands for either PC/MSDOS or CP/M environments.
  16.  *    Additional options:
  17.  *    -r    renames original file extension to .BAK and replaces
  18.  *        original file with converted output.
  19.  *    -v    Verbose output of program progress to stdout.
  20.  *    -w    strips off high bit of each character (Wordstar files)
  21.  *
  22.  *    christie
  23.  *
  24.  *    Adapted to Hi-Tech C for CP/M by Jonathan Saxton - 28 Apr 87
  25.  *
  26.  *     (Note that the -r option is REQUIRED if you want to write a
  27.  *    converted file.  I think that is deliberate.  JRS)
  28.  *
  29.  */
  30. #include <stdio.h>
  31.  
  32. #ifndef HI_TECH_C
  33. #include <errno.h>
  34. #include <string.h>
  35. #endif
  36.  
  37. #ifdef MSC
  38. #include <fcntl.h>
  39. #include <io.h>
  40. #endif
  41.  
  42. /* constant declarations */
  43. #define FALSE    0        /* boolean false */
  44. #define TRUE    0xFF        /* boolean true */
  45. #define ERROR    -1        /* return error value */
  46. #define OK    0        /* okay exit status */
  47. #define NOTOK    1        /* error exit status */
  48. #define SUB    0x1A        /* CP/M and MSDOS end of file */
  49. #define NAMESIZ    128        /* size of valid filename */
  50.  
  51. #ifdef HI_TECH_C
  52. #define VERSION "1.1 (28 Apr 1987) Hi-Tech C 3.02"
  53. #define I_MODE "rb"
  54. #define O_MODE "wb"
  55. #else
  56. #ifdef MSC
  57. #define VERSION    "1.1 (11 Jan 87) Microsoft C v4.00"
  58. #define I_MODE "r"
  59. #define O_MODE "w"
  60. #else
  61. #define VERSION "1.1 (11 Jan 87)"
  62. #define I_MODE "rb"
  63. #define O_MODE "wb"
  64. #endif
  65. #endif
  66.  
  67. /* type declarations */
  68. typedef char    bool;        /* boolean type */
  69. #ifdef    NOENUM
  70. #define ST    0
  71. #define SU    1
  72. #define TS    2
  73. #define TU    3
  74. #define US    4
  75. #define UT    5
  76. typedef char    mtype;
  77. #else
  78. typedef enum
  79. {
  80.     ST, SU, TS, TU, US, UT
  81. }    mtype;
  82. #endif
  83.  
  84. /* global declarations */
  85. bool    replace = FALSE;    /* replace file flag */
  86. bool    verbose = FALSE;    /* verbose flag */
  87. bool    strip = FALSE;        /* strip high bit of character flag */
  88. int    done = 0;        /* number of files processed */
  89. mtype    mode = US;        /* default mode is LF -> CR-LF */
  90. char    *progname;        /* name of this program */
  91. #ifdef BUFFERED
  92. char    *obuf[BUFSIZ];        /* output buffer */
  93. #endif
  94. /* function declarations */
  95. void    usage();
  96. void    option();
  97. void    unknown();
  98. void    printmode();
  99. void    doconv();
  100. void    convert();
  101. int    lc();
  102.  
  103. main(argc, argv)
  104. int    argc;
  105. char    **argv;
  106. {
  107.     /* main program - handles command line processing */
  108.  
  109.     progname = argv[0];
  110.     /* progname = "convert"; */
  111. #ifdef BUFFERED
  112.     setbuf(stdout, obuf);
  113. #endif
  114.     if (argc == 1)
  115.     {
  116.         fprintf(stderr, "%s: no input!\n", progname);
  117.         usage();
  118.     }
  119.     while (--argc)
  120.         if (**++argv == '-')
  121.             /* do option processing */
  122.             option(*argv + 1);
  123.         else
  124.             /* convert argument */
  125.             doconv(*argv);
  126.     if (!done)
  127.     {
  128.         fprintf(stderr, "%s: no files processed!\n", progname);
  129.         usage();
  130.     }
  131.     return (OK);
  132. }
  133.  
  134. void
  135. usage()
  136. {
  137.     /* prints short help instructions */
  138.  
  139.     puts("CONVERT: converts text files from one operating system format to another.");
  140.     printf("version %s (c) Chris Tham\n\n", VERSION);
  141.     printf("usage: %s [-r] [-v] [-w] [[[-OPTION] [filename] ... ] ... ]\n\n", progname);
  142.     puts("where OPTION may be one of the following:");
  143.     puts("st    converts CR-LF sequence to CR (mnemonic: System to TRSDOS)");
  144.     puts("su    converts CR-LF sequence to LF (mnemonic: System to Unix)");
  145.     puts("ts    converts CR to CR-LF sequence (mnemonic: TRSDOS to System)");
  146.     puts("tu    converts CR to LF             (mnemonic: TRSDOS to Unix)");
  147.     puts("us    converts LF to CR-LF (DEFAULT)(mnemonic: Unix to System)");
  148.     puts("ut    converts LF to CR             (mnemonic: Unix to TRSDOS)");
  149.     puts("System stands for either PC/MSDOS or CP/M environments.\n");
  150.     puts("Additional options:");
  151.     puts("-r    renames original file extension to .BAK and replaces original file ");
  152.     puts("    with conversion.");
  153.     puts("-v    Verbose output of program progress to stdout.");
  154.     puts("-w    strips off high bit of each character (Wordstar files)");
  155.     puts("-    read from standard input rather than a file.");
  156.     exit(NOTOK);
  157. }
  158.  
  159. void
  160. option (optstr)
  161. char    *optstr;
  162. {
  163.     char    c;
  164.  
  165.     /* process command line options in optstr */
  166.  
  167.     if (*optstr == '\0')
  168.     {
  169.         if (verbose)
  170.             puts("    Processing standard input");
  171.         if (replace)
  172.             fprintf("%s: cannot replace stdin!\n", progname);
  173.         else
  174.             convert(stdout, stdin);
  175.     }
  176.     while (*optstr != '\0')
  177.     {
  178.         switch (lc(*optstr))
  179.         {
  180.         case    'r':
  181.             replace = !replace;
  182.             if (verbose)
  183.             {
  184.                 if (replace)
  185.                     puts("    Replace flag ON");
  186.                 else
  187.                     puts("    Replace flag OFF");
  188.             }
  189.             break;
  190.         case    'v':
  191.             verbose = !verbose;
  192.             if (verbose)
  193.                 puts("    Verbose flag ON");
  194.             break;
  195.         case    'w':
  196.             strip = !strip;
  197.             if (verbose)
  198.             {
  199.                 if (strip)
  200.                     puts("    Strip flag ON");
  201.                 else
  202.                     puts("    Strip flag OFF");
  203.             }
  204.             break;
  205.         case    's':
  206.             c = lc(*++optstr);
  207.             if (c == 't')
  208.                 mode = ST;
  209.             else if (c == 'u')
  210.                 mode = SU;
  211.             else
  212.                 unknown(--optstr);
  213.             if (verbose)
  214.                 printmode();
  215.             break;
  216.         case    't':
  217.             c = lc(*++optstr);
  218.             if (c == 's')
  219.                 mode = TS;
  220.             else if (c == 'u')
  221.                 mode = TU;
  222.             else
  223.                 unknown(--optstr);
  224.             if (verbose)
  225.                 printmode();
  226.             break;
  227.         case    'u':
  228.             c = lc(*++optstr);
  229.             if (c == 's')
  230.                 mode = US;
  231.             else if (c == 't')
  232.                 mode = UT;
  233.             else
  234.                 unknown(--optstr);
  235.             if (verbose)
  236.                 printmode();
  237.             break;
  238.         default:
  239.             unknown(optstr);
  240.         }
  241.         ++optstr;
  242.     }
  243. }
  244.  
  245. void
  246. unknown(str)
  247. char    *str;
  248. {
  249.     /* unknown option string */
  250.  
  251.     fprintf(stderr, "%s: unknown option string '%s'\n", progname, str);
  252.     usage();
  253. }
  254.  
  255. void
  256. printmode()
  257. {
  258.     /* prints current mode to screen */
  259.  
  260.     printf("    Current mode is ");
  261.     switch (mode)
  262.     {
  263.     case    ST:
  264.         puts("CR-LF to CR");
  265.         break;
  266.     case    SU:
  267.         puts("CR-LF to LF");
  268.         break;
  269.     case    TS:
  270.         puts("CR to CR-LF");
  271.         break;
  272.     case    TU:
  273.         puts("CR to LF");
  274.         break;
  275.     case    US:
  276.         puts("LF to CR-LF");
  277.         break;
  278.     case    UT:
  279.         puts("LF to CR");
  280.         break;
  281.     }
  282. }
  283.  
  284. void
  285. doconv(filename)
  286. char    *filename;
  287. {
  288.     FILE    *input, *output;
  289.     char    *dotp;
  290.     char    oldname[NAMESIZ];
  291. #ifdef HI_TECH_C
  292.     extern char *strrchr();
  293. #else
  294.     extern int errno;
  295. #endif
  296.     if (verbose)
  297.         printf("    Processing %s\n", filename);
  298.     if (replace)
  299.     {
  300.         strcpy(oldname, filename);
  301.         if ((dotp = strrchr(oldname, '.')) == NULL)
  302.             strcat(oldname, ".bak");
  303.         else
  304.             strcpy(dotp, ".bak");
  305.         if (verbose)
  306.             printf("    Renaming original to %s\n", oldname);
  307.         if (rename(filename, oldname) == ERROR)
  308.         {
  309. #ifndef HI_TECH_C
  310.             if (errno == EEXIST)
  311.                 fprintf(stderr, "%s: %s exists\n",
  312.                     progname, oldname);
  313.             else
  314. #endif
  315.                 fprintf(stderr, "%s: error renaming %s to %s\n",
  316.                     progname, filename, oldname);
  317.             exit(NOTOK);
  318.         }
  319.         if ((input = fopen(oldname, I_MODE)) == NULL)
  320.         {
  321.             fprintf(stderr, "%s: error opening %s\n", progname, oldname);
  322.             exit(NOTOK);
  323.         }
  324.         if ((output = fopen(filename, O_MODE)) == NULL)
  325.         {
  326.             fprintf(stderr, "%s: error opening %s\n", progname, filename);
  327.             exit(NOTOK);
  328.         }
  329.     }
  330.     else
  331.     {
  332.         if ((input = fopen(filename, I_MODE)) == NULL)
  333.         {
  334.             fprintf(stderr, "%s: error opening %s\n", progname, filename);
  335.             exit(NOTOK);
  336.         }
  337.         output = stdout;
  338.     }
  339. #ifdef MSC
  340.     setmode(fileno(input), O_BINARY);
  341.     setmode(fileno(output), O_BINARY);
  342. #endif
  343.     convert(output, input);
  344. }
  345.  
  346. void
  347. convert(output, input)
  348. FILE    *output, *input;
  349. {
  350.     /* converts input file and write to output file */
  351.  
  352.     int    c;
  353.     bool    finished = FALSE;
  354.  
  355.     while ((c = getc(input)) != EOF && !finished)
  356.     {
  357.         if (c == SUB && (mode == ST || mode == SU))
  358.         {
  359.             finished = TRUE;
  360.             continue;
  361.         }
  362.         if (strip)
  363.             c &= 0x7f;
  364.         if (c == '\r')
  365.             switch (mode)
  366.             {
  367.             case    TS:
  368.                 putc(c, output);
  369.                 putc('\n', output);
  370.                 break;
  371.             case    TU:
  372.                 putc('\n', output);
  373.                 break;
  374.             case    ST:
  375.                 putc(c, output);
  376.                 if ((c = getc(input)) == EOF || c == SUB)
  377.                     finished = TRUE;
  378.                 else if (c != '\n')
  379.                 {
  380.                     if (strip)
  381.                         c &= 0x7f;
  382.                     putc(c, output);
  383.                 }
  384.                 break;
  385.             case    SU:
  386.                 if ((c = getc(input)) == EOF || c == SUB)
  387.                 {
  388.                     finished = TRUE;
  389.                     putc('\r', output);
  390.                 }
  391.                 else if (c != '\n')
  392.                 {
  393.                     putc('\r', output);
  394.                     if (strip)
  395.                         c &= 0x7f;
  396.                     putc(c, output);
  397.                 }
  398.                 else
  399.                     putc('\n', output);
  400.                 break;
  401.             default:
  402.                 putc(c, output);
  403.                 break;
  404.             }
  405.         else if (c == '\n')
  406.             switch (mode)
  407.             {
  408.             case    US:
  409.                 putc('\r', output);
  410.                 putc(c, output);
  411.                 break;
  412.             case    UT:
  413.                 putc('\r', output);
  414.                 break;
  415.             default:
  416.                 putc(c, output);
  417.                 break;
  418.             }
  419.         else
  420.             putc(c, output);
  421.     }
  422.     if (output != stdout)
  423.         fclose(output);
  424.     if (input != stdin)
  425.         fclose(stdin);
  426.     ++done;
  427. }
  428.  
  429. int lc(c)
  430. int c;
  431. {
  432.     return tolower(c);
  433. }
  434.