[PATCH] Enhance wayland-scanner so it can use files for input and output.
Pekka Paalanen
ppaalanen at gmail.com
Sun Mar 1 23:52:54 PST 2015
On Thu, 26 Feb 2015 11:48:38 +0200
Jussi Pakkanen <jpakkane at gmail.com> wrote:
> * Enhance wayland-scanner so it can use files for input and output
> to improve flexibility and testability.
>
> Signed-off-by: Jussi Pakkanen <jpakkane at gmail.com>
>
> ---
> src/scanner.c | 34 +++++++++++++++++++++++++++++-----
> 1 file changed, 29 insertions(+), 5 deletions(-)
>
> diff --git a/src/scanner.c b/src/scanner.c
> index 1f1e59a..efaf3ad 100644
> --- a/src/scanner.c
> +++ b/src/scanner.c
> @@ -28,6 +28,8 @@
> #include <errno.h>
> #include <ctype.h>
> #include <expat.h>
> +#include <unistd.h>
> +#include <fcntl.h>
>
> #include "wayland-util.h"
>
> @@ -39,11 +41,12 @@ enum side {
> static int
> usage(int ret)
> {
> - fprintf(stderr, "usage: ./scanner [client-header|server-header|code]\n");
> + fprintf(stderr, "usage: ./scanner [client-header|server-header|code]"
> + " [input_file output_file]\n");
> fprintf(stderr, "\n");
> fprintf(stderr, "Converts XML protocol descriptions supplied on "
> - "stdin to client headers,\n"
> - "server headers, or protocol marshalling code.\n");
> + "stdin or input file to client\n"
> + "headers, server headers, or protocol marshalling code.\n");
> exit(ret);
> }
>
> @@ -1252,6 +1255,7 @@ int main(int argc, char *argv[])
> {
> struct parse_context ctx;
> struct protocol protocol;
> + FILE *input = stdin;
> int len;
> void *buf;
> enum {
> @@ -1260,7 +1264,7 @@ int main(int argc, char *argv[])
> CODE,
> } mode;
>
> - if (argc != 2)
> + if (argc != 2 && argc != 4)
> usage(EXIT_FAILURE);
> else if (strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0)
> usage(EXIT_SUCCESS);
> @@ -1273,6 +1277,26 @@ int main(int argc, char *argv[])
> else
> usage(EXIT_FAILURE);
>
> + if(argc == 4) {
> + int outfd;
> + input = fopen(argv[2], "r");
> + if(input == NULL) {
> + fprintf(stderr, "Could not open input file: %s\n",
> + strerror(errno));
> + exit(EXIT_FAILURE);
> + }
> + outfd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC,
> + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
> + S_IROTH);
> + if(outfd == -1) {
> + fprintf(stderr, "Could not open output file: %s\n",
> + strerror(errno));
> + exit(EXIT_FAILURE);
> + }
> + dup2(outfd, STDOUT_FILENO);
> + close(outfd);
Hi,
in addition to the other review commments, I believe the
standard way for changing the file association for stdout is to use
freopen(). Is there a reason why you didn't use freopen()?
From the freopen manual: "The primary use of the freopen() function
is to change the file associated with a standard text stream (stderr,
stdin, or stdout)."
Thanks,
pq
> + }
> +
> wl_list_init(&protocol.interface_list);
> protocol.type_index = 0;
> protocol.null_run_length = 0;
> @@ -1293,7 +1317,7 @@ int main(int argc, char *argv[])
>
> do {
> buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
> - len = fread(buf, 1, XML_BUFFER_SIZE, stdin);
> + len = fread(buf, 1, XML_BUFFER_SIZE, input);
> if (len < 0) {
> fprintf(stderr, "fread: %m\n");
> exit(EXIT_FAILURE);
More information about the wayland-devel
mailing list