//
// aegis - project change supervisor
// Copyright (C) 1999, 2001-2006, 2008-2012, 2014 Peter Miller
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
enum
{
arglex_token_base64,
arglex_token_base85,
arglex_token_bzip,
arglex_token_header_not,
arglex_token_input,
arglex_token_output,
arglex_token_quoted_printable,
arglex_token_unix_to_unix,
};
static arglex_table_ty argtab[] =
{
{ "-Base64", arglex_token_base64, },
{ "-Base85", arglex_token_base85, },
{ "-BZip", arglex_token_bzip, },
{ "-Quoted_Printable", arglex_token_quoted_printable, },
{ "-Input", arglex_token_input, },
{ "-Output", arglex_token_output, },
{ "-Unix_to_Unix_Encode", arglex_token_unix_to_unix, },
{ "-Unix_to_Unix_Decode", arglex_token_unix_to_unix, },
{ "-Unix_to_Unix", arglex_token_unix_to_unix, },
{ "-No_Header", arglex_token_header_not, },
ARGLEX_END_MARKER
};
bool has_header = true;
static void
usage(void)
{
const char *progname;
progname = progname_get();
fprintf
(
stderr,
"Usage: %s [ -i | -o ][ [ ]]\n",
progname
);
quit(1);
}
static void
skip_header(const input::pointer &ifp)
{
if (has_header)
{
//
// skip lines until we find a blank line
// as a crude way to pass over the rfc822 header
//
for (;;)
{
nstring s;
if (!ifp->one_line(s))
break;
if (s.empty())
break;
}
}
}
static void
test_input_bunzip(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open(ifn);
output::pointer ofp = output_file::text_open(ofn);
skip_header(ifp);
//
// filter the rest
//
ifp = input_bunzip2::create_if_candidate(ifp);
ofp << ifp;
}
static void
test_input_base64(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open(ifn);
output::pointer ofp = output_file::text_open(ofn);
skip_header(ifp);
//
// filter the rest
//
ifp = input_base64::create_if_candidate(ifp);
ofp << ifp;
}
static void
test_input_base85(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open(ifn);
output::pointer ofp = output_file::text_open(ofn);
skip_header(ifp);
//
// filter the rest
//
ifp = input_base85::create_if_candidate(ifp);
ofp << ifp;
}
static void
test_input_qp(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open(ifn);
output::pointer ofp = output_file::text_open(ofn);
skip_header(ifp);
ifp = input_quoted_printable::create(ifp);
ofp << ifp;
}
static void
test_input_uu(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open(ifn);
output::pointer ofp = output_file::text_open(ofn);
skip_header(ifp);
ifp = input_uudecode::create(ifp);
ofp << ifp;
}
static void
test_output_base64(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open_text(ifn);
output::pointer ofp = output_file::text_open(ofn);
if (has_header)
{
ofp->fputs("Content-Type: application/x-aegis-test\n");
ofp->fputs("Content-Transfer-Encoding: base64\n");
ofp->fputs("\n");
}
ofp = output_filter_base64::create(ofp);
ofp << ifp;
}
static void
test_output_base85(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open_text(ifn);
output::pointer ofp = output_file::text_open(ofn);
if (has_header)
{
ofp->fputs("Content-Type: application/x-aegis-test\n");
ofp->fputs("Content-Transfer-Encoding: base85\n");
ofp->fputs("\n");
}
ofp = output_filter_base85::create(ofp);
ofp << ifp;
}
static void
test_output_bzip(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open_text(ifn);
output::pointer ofp = output_file::text_open(ofn);
if (has_header)
{
ofp->fputs("Content-Type: application/x-aegis-test\n");
ofp->fputs("Content-Transfer-Encoding: 8bit\n");
ofp->fputs("\n");
}
ofp = output_filter_bzip2::create(ofp);
ofp << ifp;
}
static void
test_output_qp(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open_text(ifn);
output::pointer ofp = output_file::text_open(ofn);
if (has_header)
{
ofp->fputs("Content-Type: application/x-aegis-test\n");
ofp->fputs("Content-Transfer-Encoding: quoted-printable\n");
ofp->fputs("\n");
}
ofp = output_filter_quoted_printable::create(ofp, false);
ofp << ifp;
}
static void
test_output_uu(const nstring &ifn, const nstring &ofn)
{
input::pointer ifp = input_file::open_text(ifn);
output::pointer ofp = output_file::text_open(ofn);
if (has_header)
{
ofp->fputs("Content-Type: application/x-aegis-test\n");
ofp->fputs("Content-Transfer-Encoding: uuencode\n");
ofp->fputs("\n");
}
ofp = output_filter_uuencode::create(ofp);
ofp << ifp;
}
int
main(int argc, char **argv)
{
void (*ifunc)(const nstring &ifn, const nstring &ofn);
void (*ofunc)(const nstring &ifn, const nstring &ofn);
void (*func)(const nstring &ifn, const nstring &ofn);
ifunc = test_input_base64;
ofunc = test_output_base64;
arglex_init(argc, argv, argtab);
arglex();
os_become_init_mortal();
nstring ifn;
nstring ofn;
func = 0;
while (arglex_token != arglex_token_eoln)
{
switch (arglex_token)
{
default:
generic_argument(usage);
continue;
case arglex_token_quoted_printable:
if (func)
usage();
ifunc = test_input_qp;
ofunc = test_output_qp;
break;
case arglex_token_unix_to_unix:
if (func)
usage();
ifunc = test_input_uu;
ofunc = test_output_uu;
break;
case arglex_token_base85:
if (func)
usage();
ifunc = test_input_base85;
ofunc = test_output_base85;
break;
case arglex_token_base64:
if (func)
usage();
ifunc = test_input_base64;
ofunc = test_output_base64;
break;
case arglex_token_bzip:
if (func)
usage();
ifunc = test_input_bunzip;
ofunc = test_output_bzip;
break;
case arglex_token_stdio:
if (!ifn)
ifn = "-";
else if (!ofn)
ofn = "-";
else
usage();
break;
case arglex_token_string:
if (ifn.empty())
ifn = arglex_value.alv_string;
else if (ofn.empty())
ofn = arglex_value.alv_string;
else
usage();
break;
case arglex_token_input:
if (func)
{
too_many:
error_raw("too many test functions specified");
usage();
}
func = ifunc;
break;
case arglex_token_output:
if (func)
goto too_many;
func = ofunc;
break;
case arglex_token_header_not:
has_header = false;
break;
}
arglex();
}
if (!func)
{
error_raw("no test function specified");
usage();
}
os_become_orig();
func(ifn, ofn);
quit(0);
return 0;
}
// vim: set ts=8 sw=4 et :