Mostly minor fixes up until version 0.8.10.
[irreco.git] / lirc-0.8.4a / tools / ircat.c
blob61540c626c8302fc5a5f91cc2a199513dd4184a1
1 /****************************************************************************
2 ** ircat.c *****************************************************************
3 ****************************************************************************
5 * ircat - prints config strings to standard output, can be used to
6 * provide remote control input to scripts
8 * The first agrument to the program is the program name, as it
9 * appears in the prog entries in .lircrc.
11 * For example if .lircrc contains:
13 * begin
14 * prog = myprog
15 * button = tv_p+
16 * config = next_file
17 * end
19 * then
21 * $ ircat myprog
23 * will print "next_file" (followed by newline) every time the
24 * button tv_p+ is pressed.
27 * Copyright (C) 2002 Bjorn Bringert <bjorn@bringert.net>
29 * Based on irexec.c
34 #ifdef HAVE_CONFIG_H
35 # include <config.h>
36 #endif
38 #include <errno.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <getopt.h>
45 #include "lirc_client.h"
47 #define PROG_NAME "ircat"
48 #define PROG_VERSION PROG_NAME " " VERSION
51 void print_usage (char *prog_name)
53 printf("Usage: %s [options] <prog>\n", prog_name);
54 printf("\t -h --help\t\tdisplay usage summary\n");
55 printf("\t -v --version\t\tdisplay version\n");
56 printf("\t -c --config=<file>\tset config file\n");
59 int main(int argc, char *argv[])
61 struct lirc_config *config;
62 char *config_file = NULL;
64 while (1) {
65 int c;
66 static struct option long_options[] = {
67 {"config", required_argument, NULL, 'c'},
68 {"help", no_argument, NULL, 'h'},
69 {"version", no_argument, NULL, 'v'},
70 {0, 0, 0, 0}
72 c = getopt_long(argc, argv, "chv", long_options, NULL);
73 if (c == -1)
74 break;
75 switch (c) {
76 case 'c':
77 config_file = optarg;
78 break;
79 case 'h':
80 print_usage(argv[0]);
81 return EXIT_SUCCESS;
82 case 'v':
83 printf("%s\n", PROG_VERSION);
84 return EXIT_SUCCESS;
85 default:
86 print_usage(argv[0]);
87 return EXIT_FAILURE;
91 if (optind != argc - 1) {
92 print_usage(argv[0]);
93 return EXIT_FAILURE;
96 if (lirc_init(argv[argc-1], 1) == -1) exit(EXIT_FAILURE);
98 if (lirc_readconfig(config_file, &config, NULL) == 0) {
99 char *code;
100 char *c;
101 int ret;
103 while (lirc_nextcode(&code) == 0) {
104 if (code == NULL) continue;
105 while ((ret = lirc_code2char(config, code, &c)) == 0 &&
106 c != NULL) {
107 printf("%s\n", c);
108 fflush(stdout);
110 free(code);
111 if (ret == -1) break;
113 lirc_freeconfig(config);
116 lirc_deinit();
117 exit(EXIT_SUCCESS);