/*
 * VEC.H (VEC)
 *
 * Copyright (c) 1993 Ville Saari
 * All rights reserved
 *
 * Created: 07-Apr-93
 * Updated: 06-Dec-93
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define UENAME "VEC"
#define UDNAME "VDC"
#define ENAME "vec"
#define DNAME "vdc"

#ifndef MACH_HDR
	#define MACH_HDR "gener.h"
#endif

#include "version.h"
#include MACH_HDR

#ifndef SYSTEM
	#define SYSTEM    "g"
#endif

#ifndef COPYRSIGN
	#define COPYRSIGN "(c)"
#endif

#ifndef BOLDON
	#define BOLDON    ""
#endif

#ifndef BOLDOFF
	#define BOLDOFF   ""
#endif

#ifndef DEFMETHOD
	#define DEFMETHOD '1'
#endif

#ifndef DEFWIDTH
	#define DEFWIDTH  80
#endif

#ifndef DEFWIDTHS
	#define DEFWIDTHS "80"
#endif

#ifndef OK
	#define OK        0
#endif

#ifndef WARN
	#define WARN      1
#endif

#ifndef ERROR
	#define ERROR     2
#endif

#ifndef FAIL
	#define FAIL      3
#endif

#ifndef BUFSIZE
	#define BUFSIZE   32768
#endif

#define DEF0 ""
#define DEF1 ""
#define DEF2 ""
#define DEF3 ""
#define DEFa ""
#define DEFi ""
#define DEFu ""
#define DEFx ""

#ifndef FINNISH
	#if (DEFMETHOD=='0')
		#undef DEF0
		#define DEF0 " (default)"
	#elif (DEFMETHOD=='1')
		#undef DEF1
		#define DEF1 " (default)"
	#elif (DEFMETHOD=='2')
		#undef DEF2
		#define DEF2 " (default)"
	#elif (DEFMETHOD=='3')
		#undef DEF3
		#define DEF3 " (default)"
	#elif (DEFMETHOD=='a')
		#undef DEFa
		#define DEFa " (default)"
	#elif (DEFMETHOD=='i')
		#undef DEFi
		#define DEFi " (default)"
	#elif (DEFMETHOD=='u')
		#undef DEFu
		#define DEFu " (default)"
	#elif (DEFMETHOD=='x')
		#undef DEFx
		#define DEFx " (default)"
	#endif

	#define COPYRIGHT \
		BOLDON UENAME "/" UDNAME BOLDOFF " V" RELEASE "." VERSION SYSTEM"\n" \
		"Copyright " COPYRSIGN " 1991, 1992, 1993 Ville Saari\n" \
		"All rights reserved\n" \
		"\n"

	#ifdef SMALLUSAGE
		#define USAGE \
			COPYRIGHT \
			"Usage: " ENAME " [-(0|1|2|3|a|d|e|f|i|N|p|q|r|s|t|u|x)] [-(A|n|o) <file>]\n" \
			"       " "   " " [-(b|w) <number>] [-] [<files>]\n" \
			"  or   " DNAME " [-(d|e|l|L|N|p|q|R|s)] [-(A|o) <file>] [-b <number>]\n" \
			"       " "   " " [-D <directory>] [-] [<files>]\n"
	#else
		#define USAGE \
			COPYRIGHT \
			"Usage: " ENAME " [<options>] [<files>]\n" \
			"  or   " DNAME " [<options>] [<files>]\n" \
			"\n" \
			"Options:\n" \
			"\t-0\t\t"              "encoding method 0 - 7 bit charset, fast" DEF0 "\n" \
			"\t-1\t\t"              "encoding method 1 - 7 bit charset, good" DEF1 "\n" \
			"\t-2\t\t"              "encoding method 2 - 8 bit charset, fast" DEF2 "\n" \
			"\t-3\t\t"              "encoding method 3 - 8 bit charset, good" DEF3 "\n" \
			"\t-a\t\t"              "ASCII escape encoding method" DEFa "\n" \
			"\t-A <file_name>\t"    "append output to named file\n" \
			"\t-b <number>\t"       "set size of I/O buffers\n" \
			"\t-d\t\t"              "decode\n" \
			"\t-D <directory>\t"    "select destination directory for decoded files\n" \
			"\t-e\t\t"					"encode\n" \
			"\t-f\t\t"              "write full lines in escape modes\n" \
			"\t-i\t\t"              "ISO 8859-1 escape encoding method" DEFi "\n" \
			"\t-l\t\t"              "filter non-decoded lines to stdout\n" \
			"\t-L\t\t"              "same as above without file name lines\n" \
			"\t-n <file_name>\t"    "set new name for encoded file\n" \
			"\t-N\t\t"					"do not store or set file mode flags\n" \
			"\t-o <file_name>\t"    "output to named file, overwrite existing file\n" \
			"\t-p\t\t"              "output to standard output (default for " ENAME ")\n" \
			"\t-q\t\t"              "quick mode - checksum not calculated\n" \
			"\t-r\t\t"              "use CR-LF pair as newline character\n" \
			"\t-R\t\t"              "reset uuencode table after each decoded file\n" \
			"\t-s\t\t"              "silent mode - file names not displayed\n" \
			"\t-t\t\t"              "write TABs literally in escape modes\n" \
			"\t-u\t\t"              "encoding method compatible with uuencode" DEFu "\n" \
			"\t-w <number>\t"       "set output line length (default " DEFWIDTHS ")\n" \
			"\t-x\t\t"              "hexdump encoding method" DEFx "\n" \
			"\t-\t\t"               "option delimiter - all parameters beyond it are\n" \
			"\t\t\t"                "file names even if they start with `-'\n" \
			"\n" \
			"if no file names given, the standard input will be read\n"
	#endif
#else
	#ifndef A_
		#define A_ "\304"
	#endif

	#ifndef O_
		#define O_ "\326"
	#endif

	#ifndef a_
		#define a_ "\344"
	#endif

	#ifndef o_
		#define o_ "\366"
	#endif

	#if (DEFMETHOD=='0')
		#undef DEF0
		#define DEF0 " (oletus)"
	#elif (DEFMETHOD=='1')
		#undef DEF1
		#define DEF1 " (oletus)"
	#elif (DEFMETHOD=='2')
		#undef DEF2
		#define DEF2 " (oletus)"
	#elif (DEFMETHOD=='3')
		#undef DEF3
		#define DEF3 " (oletus)"
	#elif (DEFMETHOD=='a')
		#undef DEFa
		#define DEFa " (oletus)"
	#elif (DEFMETHOD=='i')
		#undef DEFi
		#define DEFi " (oletus)"
	#elif (DEFMETHOD=='u')
		#undef DEFu
		#define DEFu " (oletus)"
	#elif (DEFMETHOD=='x')
		#undef DEFx
		#define DEFx " (oletus)"
	#endif

	#define COPYRIGHT \
		BOLDON UENAME "/" UDNAME BOLDOFF " V" RELEASE "." VERSION SYSTEM"\n" \
		"Copyright " COPYRSIGN " 1991, 1992, 1993 Ville Saari\n" \
		"Kaikki oikeudet pid" a_ "tet" a_ a_ "n\n" \
		"\n"

	#ifdef SMALLUSAGE
		#define USAGE \
			COPYRIGHT \
			"K" a_ "ytt" o_ ": " ENAME " [-(0|1|2|3|a|d|e|f|i|N|p|q|r|s|t|u|x)] [-(A|n|o) <tiedosto>]\n" \
			"        "           "   " " [-(b|w) <numero>] [-] [<tiedostot>]\n" \
			"  tai   "           DNAME " [-(d|e|l|L|N|p|q|R|s)] [-(A|o) <tiedosto>] [-b <numero>]\n" \
			"        "           "   " " [-D <hakemisto>] [-] [<tiedostot>]\n"
	#else
		#define USAGE \
			"K" a_ "ytt" o_ ": " ENAME " [<optiot>] [<tiedostot>]\n" \
			"  tai   " DNAME " [<optiot>] [<tiedostot>]\n" \
			"\n" \
			"Optiot:\n" \
			"\t-0\t\t"              "koodausmenetelm" a_ " 0 - 7-bittinen, nopea" DEF0 "\n" \
			"\t-1\t\t"              "koodausmenetelm" a_ " 1 - 7-bittinen, tiivis" DEF1 "\n" \
			"\t-2\t\t"              "koodausmenetelm" a_ " 2 - 8-bittinen, nopea" DEF2 "\n" \
			"\t-3\t\t"              "koodausmenetelm" a_ " 3 - 8-bittinen, tiivis" DEF3 "\n" \
			"\t-a\t\t"              "ASCII escape-koodausmenetelm" a_ "" DEFa "\n" \
			"\t-A <tiedosto>\t"     "tulosta annetun tiedoston per" a_ a_ "n\n" \
			"\t-b <numero>\t"       "aseta I/O-puskureiden koko\n" \
			"\t-d\t\t"              "pura\n" \
			"\t-D <hakemisto>\t"    "valitse kohdehakemisto purettaville tiedostoille\n" \
			"\t-e\t\t"					"koodaa\n" \
			"\t-f\t\t"              "kirjoita t" a_ "ysleveit" a_ " rivej" a_ " escapemenetelmill" a_ "\n" \
			"\t-i\t\t"              "ISO 8859-1 escape-koodausmenetelm" a_ "" DEFi "\n" \
			"\t-l\t\t"              "tulosta purkamattomat rivit stdouttiin\n" \
			"\t-L\t\t"              "kuten edell" _a ", mutta ilman tiedostonimi" _a "\n" \
			"\t-n <tiedosto>\t"     "aseta uusi nimi koodatulle tiedostolle\n" \
			"\t-N\t\t"					"" a_ "l" a_ " talleta tai aseta tiedostojen suojausbittej" a_ "\n" \
			"\t-o <tiedosto>\t"     "tulosta annettuun tiedostoon, tuhoa vanha, jos oli\n" \
			"\t-p\t\t"              "tulosta standard outputtiin (oletusarvo " ENAME "ille)\n" \
			"\t-q\t\t"              "nopea tila - tarkistussummia ei lasketa\n" \
			"\t-r\t\t"              "k" a_ "yt" a_ " CR-LF paria rivisiirtona\n" \
			"\t-R\t\t"              "nollaa uuencode taulukko joka tiedoston j" a_ "lkeen\n" \
			"\t-s\t\t"              "hiljainen tila - tiedostonimi" a_ " ei n" a_ "ytet" a_ "\n" \
			"\t-t\t\t"              "tulosta TABit sellaisenaan escape-menetelmill" a_ "\n" \
			"\t-u\t\t"              "uuencodeyhteensopiva koodausmenetelm" a_ "" DEFu "\n" \
			"\t-w <numero>\t"       "aseta rivileveys (oletusarvo " DEFWIDTHS ")\n" \
			"\t-x\t\t"              "heksadesimaalinen koodausmenetelm" a_ "" DEFx "\n" \
			"\t-\t\t"               "optiorajoitin - parametrit t" a_ "m" a_ "n j" a_ "lkeen tulkitaan\n" \
			"\t\t\t"                "tiedostonimiksi, vaikka ne alkaisivat '-':lla\n" \
			"\n" \
			"jos tiedostonimi" a_ " ei ole annettu, luetaan standard inputtia\n"
	#endif
#endif

#define SPAREBUF 48

void vec(const char *);
int vdc(void);
unsigned char *get(unsigned long);
void flushoutput(void);
void put(const unsigned char *, unsigned long);
void errcleanup(const char *);
void emittable(void);
int fillbuffer(int);
char *ltoa(unsigned long);
void printerrh(const char *);

#ifndef init
void init(void);
#endif

#ifndef cleanup
void cleanup(void);
#endif

#ifndef interrupt
int interrupt(void);
#endif

#ifndef getstdin
filehandle getstdin(int);
#endif

#ifndef getstdout
filehandle getstdout(int);
#endif

#ifndef openread
filehandle openread(const char *, int);
#endif

#ifndef openwrite
filehandle openwrite(const char *, int);
#endif

#ifndef openappend
filehandle openappend(const char *, int);
#endif

#ifndef failedopen
int failedopen(filehandle);
#endif

#ifndef closefile
void closefile(filehandle);
#endif

#ifndef readfile
int readfile(filehandle, unsigned char *, int);
#endif

#ifndef writefile
int writefile(filehandle, const unsigned char *, int);
#endif

#ifndef basename
char *basename(const char *);
#endif

#ifndef setfilemode
void setfilemode(const char *, const unsigned char *);
#endif

#ifndef getfilemode
void getfilemode(const char *, unsigned char *, int);
#endif

#ifndef modifyname
void modifyname(char *, unsigned int);
#endif

#ifndef firstmatch
char *firstmatch(const char *);
#endif

#ifndef nextmatch
char *nextmatch(void);
#endif

#ifndef printerr
void printerr(const char *);
#endif

#ifndef changedir
int changedir(const char *);
#endif

extern int quick, cr, silent, nomode, outfileflag, shortlines;
extern int pipe, literaltabs, lines, resetuu;
extern long width;
extern unsigned long bufsize, trueread;
extern unsigned char method;
extern const unsigned short crc16tab[];
extern char *name;
extern unsigned char *inbuf, *outptr, *inptr, *outbufend, *inbufend;
extern filehandle outfile, stdoutfile;

#define put1(c) { if(outptr>=outbufend) flushoutput(); *outptr++=(c); }
#define get1() (inptr>=inbufend && !fillbuffer(1)?-1:*inptr++)
