4
* Copyright Ericsson AB 1997-2009. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
7
* Version 1.1, (the "License"); you may not use this file except in
8
* compliance with the License. You should have received a copy of the
9
* Erlang Public License along with this software. If not, it can be
10
* retrieved online at http://www.erlang.org/.
12
* Software distributed under the License is distributed on an "AS IS"
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
* the License for the specific language governing rights and limitations
21
* Purpose: Test the erl_print_term() function.
22
* Author: Bjorn Gustavsson
27
#include <sys/types.h>
34
#include "erl_interface.h"
38
#define _setmode(fd, mode)
43
static int readn(int, unsigned char*, int);
46
* This program doesn't use the runner, because it needs a packet
47
* on input, but the result will be as a stream of bytes (since
48
* erl_print_term() prints directly on a file).
50
* Input is a package of with a packet header size of two bytes.
52
* +------------------------------------------------------------+
53
* | length | Encoded term... |
54
* | (2 bytes) | (as given by "length") |
55
* +------------------------------------------------------------+
57
* <------------------- length --------------------->
59
* This program decodes the encoded terms and passes it to
60
* erl_print_term(). Then this program prints
64
* and waits for a new package. <result> is the return value from
65
* erl_print_term(), formatted as an ASCII string.
74
_setmode(0, _O_BINARY);
75
_setmode(1, _O_BINARY);
85
if (readn(0, buf, 2) <= 0) {
86
/* fprintf(stderr, "error reading message header\n"); */
87
/* actually this is where we leave the infinite loop */
90
n = buf[0] * 256 + buf[1];
91
if (readn(0, buf, n) < 0) {
92
fprintf(stderr, "error reading message contents\n");
96
term = erl_decode(buf);
98
fprintf(stderr, "erl_decode() failed\n");
101
n = erl_print_term(stdout, term);
102
erl_free_compound(term);
103
fprintf(stdout,"\r%d\n", n);
109
* Reads len number of bytes.
114
int fd; /* File descriptor to read from. */
115
unsigned char *buf; /* Store in this buffer. */
116
int len; /* Number of bytes to read. */
118
int n; /* Byte count in last read call. */
119
int sofar = 0; /* Bytes read so far. */
122
if ((n = read(fd, buf+sofar, len-sofar)) <= 0)
123
/* error or EOF in read */
126
} while (sofar < len);