File: irexec.c

package info (click to toggle)
lirc 0.7.1pre2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,852 kB
  • ctags: 2,924
  • sloc: ansic: 31,205; sh: 12,021; makefile: 631
file content (121 lines) | stat: -rw-r--r-- 2,626 bytes parent folder | download | duplicates (4)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*      $Id: irexec.c,v 5.6 2003/06/07 22:12:52 lirc Exp $      */

/****************************************************************************
 ** irexec.c ****************************************************************
 ****************************************************************************
 *
 * irexec  - execute programs according to the pressed remote control buttons
 *
 * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
 * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "lirc_client.h"

char *progname;

int main(int argc, char *argv[])
{
	struct lirc_config *config;
	int daemonize=0;
	char* program="irexec";

	progname="irexec " VERSION;
	while(1)
	{
		int c;
		static struct option long_options[] =
		{
			{"help",no_argument,NULL,'h'},
			{"version",no_argument,NULL,'v'},
			{"daemon",no_argument,NULL,'d'},
			{"name",required_argument,NULL,'n'},
			{0, 0, 0, 0}
		};
		c = getopt_long(argc,argv,"hvdn:",long_options,NULL);
		if(c==-1)
			break;
		switch (c)
		{
		case 'h':
			printf("Usage: %s [options] [config_file]\n",argv[0]);
			printf("\t -h --help\t\tdisplay usage summary\n");
			printf("\t -v --version\t\tdisplay version\n");
			printf("\t -d --daemon\t\trun in background\n");
			printf("\t -n --name\t\tuse this program name\n");
			return(EXIT_SUCCESS);
		case 'v':
			printf("%s\n",progname);
			return(EXIT_SUCCESS);
		case 'd':
			daemonize=1;
			break;
		case 'n':
			program=optarg;
			break;
		default:
			printf("Usage: %s [options] [config_file]\n",argv[0]);
			return(EXIT_FAILURE);
		}
	}
	if (optind < argc-1)
	{
		fprintf(stderr,"%s: too many arguments\n",progname);
		return(EXIT_FAILURE);
	}
	
	if(lirc_init(program, daemonize ? 0:1)==-1) exit(EXIT_FAILURE);

	if(lirc_readconfig(optind!=argc ? argv[optind]:NULL,&config,NULL)==0)
	{
		char *code;
		char *c;
		int ret;

		if(daemonize)
		{
			if(daemon(0,0)==-1)
			{
				fprintf(stderr,"%s: can't daemonize\n",
					progname);
				perror(progname);
				lirc_freeconfig(config);
				lirc_deinit();
				exit(EXIT_FAILURE);
			}
		}
		while(lirc_nextcode(&code)==0)
		{
			if(code==NULL) continue;
			while((ret=lirc_code2char(config,code,&c))==0 &&
			      c!=NULL)
			{
#ifdef DEBUG
				if(!daemonize)
				{
					printf("Execing command \"%s\"\n",c);
				}
#endif
				system(c);
			}
			free(code);
			if(ret==-1) break;
		}
		lirc_freeconfig(config);
	}

	lirc_deinit();
	exit(EXIT_SUCCESS);
}