home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
beehive
/
utilitys
/
convert.arc
/
CONVERT.C
< prev
next >
Wrap
Text File
|
1990-07-21
|
9KB
|
434 lines
/*
* CONVERT.C
*
* converts text files from one operating system format to another.
*
* SYPNOSIS:
* convert [-r] [-v] [-w] [[[-OPTION] [filename] ... ] ... ]
* where OPTION may be one of the following:
* st converts CR-LF sequence to CR (mnemonic: System to TRSDOS)
* su converts CR-LF sequence to LF (mnemonic: System to Unix)
* ts converts CR to CR-LF sequence (mnemonic: TRSDOS to System)
* tu converts CR to LF (mnemonic: TRSDOS to Unix)
* us converts LF to CR-LF (DEFAULT)(mnemonic: Unix to System)
* ut converts LF to CR (mnemonic: Unix to TRSDOS)
* System stands for either PC/MSDOS or CP/M environments.
* Additional options:
* -r renames original file extension to .BAK and replaces
* original file with converted output.
* -v Verbose output of program progress to stdout.
* -w strips off high bit of each character (Wordstar files)
*
* christie
*
* Adapted to Hi-Tech C for CP/M by Jonathan Saxton - 28 Apr 87
*
* (Note that the -r option is REQUIRED if you want to write a
* converted file. I think that is deliberate. JRS)
*
*/
#include <stdio.h>
#ifndef HI_TECH_C
#include <errno.h>
#include <string.h>
#endif
#ifdef MSC
#include <fcntl.h>
#include <io.h>
#endif
/* constant declarations */
#define FALSE 0 /* boolean false */
#define TRUE 0xFF /* boolean true */
#define ERROR -1 /* return error value */
#define OK 0 /* okay exit status */
#define NOTOK 1 /* error exit status */
#define SUB 0x1A /* CP/M and MSDOS end of file */
#define NAMESIZ 128 /* size of valid filename */
#ifdef HI_TECH_C
#define VERSION "1.1 (28 Apr 1987) Hi-Tech C 3.02"
#define I_MODE "rb"
#define O_MODE "wb"
#else
#ifdef MSC
#define VERSION "1.1 (11 Jan 87) Microsoft C v4.00"
#define I_MODE "r"
#define O_MODE "w"
#else
#define VERSION "1.1 (11 Jan 87)"
#define I_MODE "rb"
#define O_MODE "wb"
#endif
#endif
/* type declarations */
typedef char bool; /* boolean type */
#ifdef NOENUM
#define ST 0
#define SU 1
#define TS 2
#define TU 3
#define US 4
#define UT 5
typedef char mtype;
#else
typedef enum
{
ST, SU, TS, TU, US, UT
} mtype;
#endif
/* global declarations */
bool replace = FALSE; /* replace file flag */
bool verbose = FALSE; /* verbose flag */
bool strip = FALSE; /* strip high bit of character flag */
int done = 0; /* number of files processed */
mtype mode = US; /* default mode is LF -> CR-LF */
char *progname; /* name of this program */
#ifdef BUFFERED
char *obuf[BUFSIZ]; /* output buffer */
#endif
/* function declarations */
void usage();
void option();
void unknown();
void printmode();
void doconv();
void convert();
int lc();
main(argc, argv)
int argc;
char **argv;
{
/* main program - handles command line processing */
progname = argv[0];
/* progname = "convert"; */
#ifdef BUFFERED
setbuf(stdout, obuf);
#endif
if (argc == 1)
{
fprintf(stderr, "%s: no input!\n", progname);
usage();
}
while (--argc)
if (**++argv == '-')
/* do option processing */
option(*argv + 1);
else
/* convert argument */
doconv(*argv);
if (!done)
{
fprintf(stderr, "%s: no files processed!\n", progname);
usage();
}
return (OK);
}
void
usage()
{
/* prints short help instructions */
puts("CONVERT: converts text files from one operating system format to another.");
printf("version %s (c) Chris Tham\n\n", VERSION);
printf("usage: %s [-r] [-v] [-w] [[[-OPTION] [filename] ... ] ... ]\n\n", progname);
puts("where OPTION may be one of the following:");
puts("st converts CR-LF sequence to CR (mnemonic: System to TRSDOS)");
puts("su converts CR-LF sequence to LF (mnemonic: System to Unix)");
puts("ts converts CR to CR-LF sequence (mnemonic: TRSDOS to System)");
puts("tu converts CR to LF (mnemonic: TRSDOS to Unix)");
puts("us converts LF to CR-LF (DEFAULT)(mnemonic: Unix to System)");
puts("ut converts LF to CR (mnemonic: Unix to TRSDOS)");
puts("System stands for either PC/MSDOS or CP/M environments.\n");
puts("Additional options:");
puts("-r renames original file extension to .BAK and replaces original file ");
puts(" with conversion.");
puts("-v Verbose output of program progress to stdout.");
puts("-w strips off high bit of each character (Wordstar files)");
puts("- read from standard input rather than a file.");
exit(NOTOK);
}
void
option (optstr)
char *optstr;
{
char c;
/* process command line options in optstr */
if (*optstr == '\0')
{
if (verbose)
puts(" Processing standard input");
if (replace)
fprintf("%s: cannot replace stdin!\n", progname);
else
convert(stdout, stdin);
}
while (*optstr != '\0')
{
switch (lc(*optstr))
{
case 'r':
replace = !replace;
if (verbose)
{
if (replace)
puts(" Replace flag ON");
else
puts(" Replace flag OFF");
}
break;
case 'v':
verbose = !verbose;
if (verbose)
puts(" Verbose flag ON");
break;
case 'w':
strip = !strip;
if (verbose)
{
if (strip)
puts(" Strip flag ON");
else
puts(" Strip flag OFF");
}
break;
case 's':
c = lc(*++optstr);
if (c == 't')
mode = ST;
else if (c == 'u')
mode = SU;
else
unknown(--optstr);
if (verbose)
printmode();
break;
case 't':
c = lc(*++optstr);
if (c == 's')
mode = TS;
else if (c == 'u')
mode = TU;
else
unknown(--optstr);
if (verbose)
printmode();
break;
case 'u':
c = lc(*++optstr);
if (c == 's')
mode = US;
else if (c == 't')
mode = UT;
else
unknown(--optstr);
if (verbose)
printmode();
break;
default:
unknown(optstr);
}
++optstr;
}
}
void
unknown(str)
char *str;
{
/* unknown option string */
fprintf(stderr, "%s: unknown option string '%s'\n", progname, str);
usage();
}
void
printmode()
{
/* prints current mode to screen */
printf(" Current mode is ");
switch (mode)
{
case ST:
puts("CR-LF to CR");
break;
case SU:
puts("CR-LF to LF");
break;
case TS:
puts("CR to CR-LF");
break;
case TU:
puts("CR to LF");
break;
case US:
puts("LF to CR-LF");
break;
case UT:
puts("LF to CR");
break;
}
}
void
doconv(filename)
char *filename;
{
FILE *input, *output;
char *dotp;
char oldname[NAMESIZ];
#ifdef HI_TECH_C
extern char *strrchr();
#else
extern int errno;
#endif
if (verbose)
printf(" Processing %s\n", filename);
if (replace)
{
strcpy(oldname, filename);
if ((dotp = strrchr(oldname, '.')) == NULL)
strcat(oldname, ".bak");
else
strcpy(dotp, ".bak");
if (verbose)
printf(" Renaming original to %s\n", oldname);
if (rename(filename, oldname) == ERROR)
{
#ifndef HI_TECH_C
if (errno == EEXIST)
fprintf(stderr, "%s: %s exists\n",
progname, oldname);
else
#endif
fprintf(stderr, "%s: error renaming %s to %s\n",
progname, filename, oldname);
exit(NOTOK);
}
if ((input = fopen(oldname, I_MODE)) == NULL)
{
fprintf(stderr, "%s: error opening %s\n", progname, oldname);
exit(NOTOK);
}
if ((output = fopen(filename, O_MODE)) == NULL)
{
fprintf(stderr, "%s: error opening %s\n", progname, filename);
exit(NOTOK);
}
}
else
{
if ((input = fopen(filename, I_MODE)) == NULL)
{
fprintf(stderr, "%s: error opening %s\n", progname, filename);
exit(NOTOK);
}
output = stdout;
}
#ifdef MSC
setmode(fileno(input), O_BINARY);
setmode(fileno(output), O_BINARY);
#endif
convert(output, input);
}
void
convert(output, input)
FILE *output, *input;
{
/* converts input file and write to output file */
int c;
bool finished = FALSE;
while ((c = getc(input)) != EOF && !finished)
{
if (c == SUB && (mode == ST || mode == SU))
{
finished = TRUE;
continue;
}
if (strip)
c &= 0x7f;
if (c == '\r')
switch (mode)
{
case TS:
putc(c, output);
putc('\n', output);
break;
case TU:
putc('\n', output);
break;
case ST:
putc(c, output);
if ((c = getc(input)) == EOF || c == SUB)
finished = TRUE;
else if (c != '\n')
{
if (strip)
c &= 0x7f;
putc(c, output);
}
break;
case SU:
if ((c = getc(input)) == EOF || c == SUB)
{
finished = TRUE;
putc('\r', output);
}
else if (c != '\n')
{
putc('\r', output);
if (strip)
c &= 0x7f;
putc(c, output);
}
else
putc('\n', output);
break;
default:
putc(c, output);
break;
}
else if (c == '\n')
switch (mode)
{
case US:
putc('\r', output);
putc(c, output);
break;
case UT:
putc('\r', output);
break;
default:
putc(c, output);
break;
}
else
putc(c, output);
}
if (output != stdout)
fclose(output);
if (input != stdin)
fclose(stdin);
++done;
}
int lc(c)
int c;
{
return tolower(c);
}