//*****************************************************************************
//******** Messages and error messages ****************************************
//*****************************************************************************
char license_msg[] =
{ "This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n"};
char help_msg[] =
{ "\n TFC converts text files between various file formats \n\n"
" Syntax: TFC [-ADELPUVX?] [-T [n]] [-N [n]] input_file [output_file]\n\n"
" -u : DOS -> Unix: Replace all CR/LF with LF, delete Ctrl-Z\n"
" -d : Unix -> Dos: Replace all LF with CR/LF\n"
" -a : DOS -> ASCII: Change DOS-Format to 7 Bit-ASCII-Format\n"
" -n [n]: Number lines: Number output lines. n: start line number\n"
" -t [n]: Expand tabs: Convert tabs to spaces. n: tab spacing (default 8)\n"
" -v : Text -> LaTeX: Generate a LaTeX verbatim list\n"
" -x : DOS -> LaTeX: Convert DOS special characters to LaTeX format\n"
" -p : par mode Check parentheses\n\n"
" -l : license Show software license\n"
" -q : quiet: No output on screen during operation\n\n"
" This is free software, and you are welcome to redistribute it\n"
" under certain conditions; type `tfc -l' for details.\n"
" If ouput_file is omitted, output is sent to input_file. This overwrites\n"
" input_file, so be careful! Return Code: 0 on success\n"};
#define MsgTitle1 "\nTFC: Text Format Conversion Utility\n"
#define MsgTitle2 "Version 1.3 <C> Copyright " __DATE__ " by M.Hofmann\n"
#define MsgOk "\nProgram completed with no errors\n\n"
#define MsgOpenF "\n\nOpening files:\n"
#define MsgIFile "\n Input file ======> (buffer length: %4XH) %s"
#define MsgOFile "\n Output file ====> (buffer length: %4XH) %s"
#define MsgRen "\n Output file will be renamed to %s after completion"
#define MsgConv "\n\n\nConverting data:\n\n"
#define MsgLComp "\r %i lines completed"
#define MsgOpenPar "WARNING: Unequal number of parentheses found (%i more '%c' than '%c')...\x07\n"
#define MsgOddQuot "WARNING: Odd number of \x22 detected...\x07\n"
#define ErrNoArg "Wrong number of arguments"
#define ErrNoOpt "Unknown Option specified"
#define ErrNoInFile "Can't open input file"
#define ErrNoInName "No input file name specified"
#define ErrNoOutFile "Can't open output file"
#define ErrClFile "Can't close files"
#define ErrReadF "Error reading file"
#define ErrWrtF "Error writing file"
#define ErrNoRename "Can't rename file"
//*****************************************************************************
//******** Some useful definitions ********************************************
//*****************************************************************************
char iname[100],oname[100];
FILE *ifile, *ofile;
int renflag = false;
// Line / column counting
int lstart = 1;
int lcount = 0; // actual line of output text
int pos = 0; // actual column of output text
// Flags for operation modes
int unixmode = false; // Flags for conversion modes
int dosmode = false;
int texlistmode = false;
int nummode = false;
int ascmode = false;
int texmode = false;
int qmode = false;
int parmode = false;
int tabmode = false;
int tabdist = 8;
// Conversion tables
char dos2unix[256][10];
char dos2tex[256][10];
char cr_str[10]; // String with proper CR/ CR/LF sequence
// Other global variables
int parenum = 0, pargnum = 0, parrnum = 0; // number of open braces
int quotenum = 0; // number of double quotes (")
/*** Translates the character c and writes it to the output file ***/
{
// Check for ASCII Control codes (c<20h)
if (unixmode && c==26) return;
if (tabmode && c==9) {
do { fp (' '); } while (pos % tabdist);
return;
}
// Check brace level
if (parmode) {
switch(c) {
case 34 : quotenum++;break; /* " */
case 40 : parrnum++;break; /* ( */
case 41 : parrnum--;break; /* ) */
case 91 : parenum++;break; /* [ */
case 93 : parenum--;break; /* ] */
case 123 : pargnum++;break; /* { */
case 125 : pargnum--;break; /* } */
} /* while */
}
// ASCII-Translation ?
if (ascmode) {
fps (dos2unix[c]);
return;
}
// TeX-Translation ?
if (texmode) {
fps (dos2tex[c]);
return;
}
// No Translation !
fp(c);
}
void convtext()
{
int register c = 0;
char s[10];
if (!qmode)
printf (MsgConv); // print message
if (texlistmode) { // if Latex List mode: print start commands
fps ("%***\n");
fps ("%*** File: "); fps (iname);
fps ("\n%*** Formatted for: LaTeX by TFC V1.3 ("__DATE__")\n%***\n");
fps ("{\\scriptsize\n\\begin{verbatim}\n");
lstart-=lcount;
}
while (EOF!=(c=fgetc(ifile))) {
if (c==26) break;
// Start of a new line ?
if (pos==0) {
if (!((lcount-1) % 64) && !qmode) // Print line number
printf(MsgLComp,lcount);
if (nummode) { // Number lines in output file
sprintf(s,"%5i: ",lcount+lstart);
fps(s);
}
}
// Translate and output character
convchar (c);
} /* while */
// Ok, the whole file was translated. Print messages to user
if (!qmode) {
printf(MsgLComp,lcount);
printf ("\n\n");
}
if (ferror(ifile)) ExitErr(ErrReadF,1);
if (texlistmode) // End of LaTeX environment
fps ("\\end{verbatim}\n}");
if (parmode) {
if (quotenum % 2) printf (MsgOddQuot); // Even Number of quotes?
if (pargnum>0) printf (MsgOpenPar, pargnum,'{','}'); // All Braces closed?
if (pargnum<0) printf (MsgOpenPar,-pargnum,'}','{');
if (parenum>0) printf (MsgOpenPar, parenum,'[',']');
if (parenum<0) printf (MsgOpenPar,-parenum,']','[');
if (parrnum>0) printf (MsgOpenPar, parrnum,'(',')');
if (parrnum<0) printf (MsgOpenPar,-parrnum,')','(');
}
}
//*****************************************************************************
//****** Main program *********************************************************
//*****************************************************************************
int main (int argc, char *argv[])
{
int i, j;
char c;
iname[0] = oname[0] = '\0'; // No filenames so far
// Scan input line
for (i=1; i<argc; i++) { // Scan input line for arguments
if (argv[i][0] == '-') { // is it an option ?
for (j = 1; j > 0 && 0 != (c=toupper(argv[i][j])); j++) {
switch(c) {
case ('U') : unixmode = true; break;
case ('D') : dosmode = true; break;
case ('A') : ascmode = true; break;
case ('P') : parmode = true; break;
case ('Q') : qmode = true; break;
case ('V') : texlistmode = true; break;
case ('X') : texmode = true; break;
case ('H') :
case ('?') : title();help(); break;
case ('L') : title();license(); break;
case ('N') : nummode = true;
if (1 == sscanf (argv[i+1], "%d", &lstart)) {
i++; j = -1;
}
break;
case ('T') : tabmode = true;
if (1 == sscanf (argv[i+1], "%d", &tabdist)) {
i++; j = -1;
}
break;
default : title();
ExitErr(ErrNoOpt,1);
} /* switch */
}
} else { // Seems to be a file name
if (iname[0]==0) {
strcpy(iname,argv[i]);
} else {
if (oname[0]!=0)
ExitErr(ErrNoArg,1);
strcpy(oname,argv[i]);
}
} /* if */
} /* for */
// Print Title
title(); // Print title message
// Check parameters and options
if (!iname[0]) ExitErr(ErrNoInName,1);
// Initialize system
setuptables();
// Convert text file
fileo(iname,oname); // Open files
convtext(); // Convert input file
filec(); // Close file