Commit a6bffe94 authored by Niels Möller's avatar Niels Möller
Browse files

* src/sexp_conv.c (main): Added argp support.

* src/sexp.c (sexp_argp): -"-
* src/werror.c (werror_argp): -"-

Rev: src/lsh_argp.h:1.1
Rev: src/sexp.c:1.16
Rev: src/sexp.h:1.19
Rev: src/sexp_conv.c:1.5
Rev: src/werror.c:1.36
Rev: src/werror.h:1.20
parent fb3803df
/* lsh_argp.h
*
* Include the right argp header.
*
* $Id$ */
/* lsh, an implementation of the ssh protocol
*
* Copyright (C) 1998 Niels Möller
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef LSH_LSH_ARGP_H_INCLUDED
#define LSH_LSH_ARGP_H_INCLUDED
#include "lsh_types.h"
#if HAVE_ARGP_PARSE
/* We're using the system's argp implementation */
# if HAVE_ARGP_H
# include <argp.h>
# endif
#else
# include "argp/argp.h"
#endif
#endif /* LSH_LSH_ARGP_H_INCLUDED */
......@@ -716,5 +716,82 @@ int sexp_get_un(struct sexp_iterator *i, const char *name, mpz_t n)
return 1;
}
/* Command line options */
struct sexp_format
{
char *name;
int id;
};
static const struct sexp_format sexp_formats[] = {
{ "transport", SEXP_TRANSPORT },
{ "canonical", SEXP_CANONICAL },
{ "advanced", SEXP_ADVANCED },
{ "international", SEXP_INTERNATIONAL },
{ NULL, 0 }
};
#if 0
static void list_formats(void)
{
int i;
werror("Available formats are:\n");
for (i = 0; sexp_formats[i].name; i++)
werror(" %z\n", sexp_formats[i].name);
}
#endif
static int lookup_sexp_format(const char *name)
{
int i;
for (i = 0; sexp_formats[i].name; i++)
{
if (strcasecmp(sexp_formats[i].name, name) == 0)
return sexp_formats[i].id;
}
return -1;
}
static const struct argp_option
sexp_options[] =
{
#if 0
{ "input-format", 'i', "format", 0,
"Variant of the s-expression syntax to accept", 0},
#endif
{ "output-format", 'o', "format", 0,
"Variant of the s-expression syntax to generate", 0},
{ NULL, 0, NULL, 0, NULL, 0 }
};
static error_t
sexp_argp_parser(int key, char *arg, struct argp_state *state)
{
switch(key)
{
default:
return ARGP_ERR_UNKNOWN;
case 'o':
{
int format = lookup_sexp_format(arg);
if (format < 0)
argp_error(state, "Unknown s-expression format '%s'", arg);
else
(sexp_argp_input *) (state->input) = format;
break;
}
}
return 0;
}
const struct argp sexp_argp =
{
sexp_options,
sexp_argp_parser,
"\vValid sexp-formats are transport, canonical, "
"advanced and international.",
NULL, NULL, NULL
};
......@@ -29,6 +29,7 @@
#include "bignum.h"
#include "command.h"
#include "list.h"
#include "lsh_argp.h"
#include "sexp_table.h"
......@@ -181,6 +182,9 @@ make_read_sexp(int style, int goon,
struct command_continuation *c,
struct exception_handler *e);
extern const struct argp sexp_argp;
#define sexp_argp_input int
#endif /* LSH_SEXP_H_INCLUDED */
/* sexpconv.c
/* sexp_conv.c
*
* Reads a sexp in given form from, and writes it in given form.
*
......@@ -25,12 +25,11 @@
#include "io.h"
#include "lsh.h"
#include "lsh_argp.h"
#include "sexp_commands.h"
#include "werror.h"
#include "xalloc.h"
#include "getopt.h"
#include <string.h>
#include <unistd.h>
......@@ -39,41 +38,6 @@
/* Global, for simplicity */
int exit_code = EXIT_SUCCESS;
struct sexp_format
{
char *name;
int id;
};
static const struct sexp_format sexp_formats[] = {
{ "transport", SEXP_TRANSPORT },
{ "canonical", SEXP_CANONICAL },
{ "advanced", SEXP_ADVANCED },
{ "international", SEXP_INTERNATIONAL },
{ NULL, 0 }
};
static void list_formats(void)
{
int i;
werror("Available formats are:\n");
for (i = 0; sexp_formats[i].name; i++)
werror(" %z\n", sexp_formats[i].name);
}
static int lookup_sexp_format(const char *name)
{
int i;
for (i = 0; sexp_formats[i].name; i++)
{
if (strcasecmp(sexp_formats[i].name, name) == 0)
return sexp_formats[i].id;
}
return -1;
}
/* GABA:
(expr
(name make_sexp_conv)
......@@ -118,75 +82,59 @@ do_exc_sexp_conv_handler(struct exception_handler *self,
}
}
/* Option parsing */
static const struct argp_child
sub_parsers[] =
{
{ &sexp_argp, 0, NULL, 0 },
{ &werror_argp, 0, "", 0 },
{ NULL, 0, NULL, 0}
};
static error_t
main_argp_parser(int key, char *arg UNUSED, struct argp_state *state UNUSED)
{
switch(key)
{
default:
return ARGP_ERR_UNKNOWN;
case ARGP_KEY_INIT:
state->child_inputs[0] = state->input;
state->child_inputs[1] = NULL;
break;
}
return 0;
}
static const struct argp
main_argp =
{ NULL, main_argp_parser, NULL,
"Reads an s-expression on stdin, and outputs the same "
"s-expression on stdout, possibly using a different "
"encoding. By default, output uses the advanced encoding. ",
sub_parsers,
NULL
};
#define SEXP_BUFFER_SIZE 1024
int main(int argc, char **argv)
{
int option;
int input_format = SEXP_ADVANCED;
int output_format = SEXP_ADVANCED;
int input_format = SEXP_TRANSPORT;
sexp_argp_input output_format = SEXP_ADVANCED;
struct exception_handler *e;
NEW(io_backend, backend);
for (;;)
{
static const struct option options[] =
{
{ "verbose", no_argument, NULL, 'v' },
{ "quiet", no_argument, NULL, 'q' },
{ "debug", no_argument, &debug_flag, 1},
{ "input", required_argument, NULL, 'i'},
{ "output", required_argument, NULL, 'o'},
{ NULL, 0, NULL, 0 }
};
option = getopt_long(argc, argv, "qvi:o:", options, NULL);
switch(option)
{
case -1:
goto options_done;
case 'q':
quiet_flag = 1;
break;
case 'v':
verbose_flag = 1;
break;
case 'i':
/* specify input format */
input_format = lookup_sexp_format(optarg);
if (input_format < 0)
{
werror("Invalid input format.\n");
list_formats();
return EXIT_FAILURE;
}
break;
case 'o':
/* specify output format */
output_format = lookup_sexp_format(optarg);
if (output_format < 0)
{
werror("Invalid output format.\n");
list_formats();
return EXIT_FAILURE;
}
break;
}
}
argp_parse(&main_argp, argc, argv, 0, NULL, &output_format);
options_done:
init_backend(backend);
/* Patch the parent pointer later */
e = make_exception_handler(do_exc_sexp_conv_handler, NULL);
e = make_exception_handler(do_exc_sexp_conv_handler,
NULL, HANDLER_CONTEXT);
{
CAST_SUBTYPE(command, work,
......
......@@ -45,11 +45,57 @@
#include <syslog.h>
#endif
/* Global flags */
int trace_flag = 0;
int debug_flag = 0;
int quiet_flag = 0;
int verbose_flag = 0;
int syslog_flag = 0;
#define WERROR_TRACE -1
#define WERROR_DEBUG -2
static const struct argp_option
werror_options[] =
{
{ "quiet", 'q', NULL, 0, "Supress all warnings and diagnostic messages", 0 },
{ "verbose", 'v', NULL, 0, "Verbose diagnostic messages", 0},
{ "trace", WERROR_TRACE, NULL, 0, "Detailed trace", 0 },
{ "debug", WERROR_DEBUG, NULL, 0, "Print huge amounts of debug information", 0 },
{ NULL, 0, NULL, 0, NULL, 0 }
};
static error_t
werror_argp_parser(int key, char *arg UNUSED,
struct argp_state *state UNUSED)
{
switch(key)
{
default:
return ARGP_ERR_UNKNOWN;
case 'q':
quiet_flag = 1;
break;
case 'v':
verbose_flag = 1;
break;
case WERROR_TRACE:
trace_flag = 1;
break;
case WERROR_DEBUG:
debug_flag = 1;
break;
}
return 0;
}
const struct argp werror_argp =
{
werror_options,
werror_argp_parser,
NULL, NULL, NULL, NULL
};
int error_fd = STDERR_FILENO;
#define BUF_SIZE 500
......@@ -382,6 +428,19 @@ void werror(const char *format, ...)
}
}
void trace(const char *format, ...)
{
va_list args;
if (trace_flag)
{
va_start(args, format);
werror_vformat(format, args);
va_end(args);
werror_flush();
}
}
void debug(const char *format, ...)
{
va_list args;
......
......@@ -27,12 +27,16 @@
#define LSH_ERROR_H_INCLUDED
#include "bignum.h"
#include "lsh_argp.h"
/* Global variables */
extern int trace_flag;
extern int debug_flag;
extern int quiet_flag;
extern int verbose_flag;
extern const struct argp werror_argp;
void set_error_stream(int fd, int with_poll);
void set_error_ignore(void);
......@@ -62,11 +66,10 @@ void set_error_syslog(void);
void werror_vformat(const char *f, va_list args);
void werror(const char *format, ...);
void trace(const char *format, ...);
void debug(const char *format, ...);
void verbose(const char *format, ...);
/* FIXME: Use a separate flag to enable tracing */
#define trace debug
void fatal(const char *format, ...) NORETURN;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment