sexp_test.c 2.34 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/* sexp_test.c
 *
 * Reads sexp on stdin, and writes them out again on stdout.
 *
 * $Id$
 */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "io.h"
#include "sexp.h"
#include "werror.h"
#include "xalloc.h"

#include <unistd.h>

#include "sexp_test.c.x"

/* CLASS:
   (class
     (name output_sexp)
     (super sexp_handler)
     (vars
       (write object abstract_write)
       (style . int)))
*/

static int do_output_sexp(struct sexp_handler *h, struct sexp *e)
{
  CAST(output_sexp, closure, h);

  return A_WRITE(closure->write, sexp_format(e, closure->style));
}

/* CLASS:
   (class
     (name input_closed)
     (super close_callback)
     (vars
       (status . "int *")
       (output object write_buffer)))
*/

static int do_close(struct close_callback *c, int reason)
{
  CAST(input_closed, closure, c);

  write_buffer_close(closure->output);  

  *closure->status = (reason == CLOSE_EOF) ? EXIT_SUCCESS : EXIT_FAILURE;

  return 4711;
}

#define BLOCK_SIZE 2000

int main(int argc UNUSED, char **argv UNUSED)
{
  NEW(io_backend, backend);
  NEW(output_sexp, out);
  NEW(input_closed, close);
  struct write_buffer *write;

  int status = 17;
  
  init_backend(backend);
  set_error_stream(STDERR_FILENO, 1);

  write = io_write(backend, STDOUT_FILENO, BLOCK_SIZE, NULL)->buffer;

  out->super.handler = do_output_sexp;
  out->write = &write->super;
  out->style = SEXP_ADVANCED;
  
  close->super.f = do_close;
  close->output = write;
  close->status = &status;
  
  io_read(backend, STDIN_FILENO,
	  make_read_sexp(&out->super, BLOCK_SIZE, SEXP_TRANSPORT, 1),
	  &close->super);

  io_run(backend);

  return status;
}