comparison recpt1/recpt1ctl.c @ 78:5a0126d8af17

landed ipc control functionality branch
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Tue, 01 Dec 2009 20:24:22 +0900
parents
children
comparison
equal deleted inserted replaced
77:517e61637f7b 78:5a0126d8af17
1 #include <sys/types.h>
2 #include <sys/ipc.h>
3 #include <sys/msg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <ctype.h>
9 #include <getopt.h>
10
11 #include "version.h"
12
13 #define MSGSZ 255
14
15 typedef struct msgbuf {
16 long mtype;
17 char mtext[MSGSZ];
18 } message_buf;
19
20 void
21 show_usage(char *cmd)
22 {
23 fprintf(stderr, "Usage: \n%s --pid pid [--channel channel] [--extend time_to_extend] [--time recording_time]\n", cmd);
24 fprintf(stderr, "\n");
25 }
26
27 void
28 show_options(void)
29 {
30 fprintf(stderr, "Options:\n");
31 fprintf(stderr, "--pid: Process id of recpt1 to control\n");
32 fprintf(stderr, "--channel: Tune to specified channel\n");
33 fprintf(stderr, "--extend: Extend recording time\n");
34 fprintf(stderr, "--time: Set total recording time\n");
35 fprintf(stderr, "--help: Show this help\n");
36 fprintf(stderr, "--version: Show version\n");
37 fprintf(stderr, "--list: Show channel list\n");
38 }
39
40 void
41 show_channels(void)
42 {
43 FILE *f;
44 char *home;
45 char buf[255], filename[255];
46
47 fprintf(stderr, "Available Channels:\n");
48
49 home = getenv("HOME");
50 sprintf(filename, "%s/.recpt1-channels", home);
51 f = fopen(filename, "r");
52 if(f) {
53 while(fgets(buf, 255, f))
54 fprintf(stderr, "%s", buf);
55 fclose(f);
56 }
57 else
58 fprintf(stderr, "13-62: Terrestrial Channels\n");
59
60 fprintf(stderr, "101ch: NHK BS1\n");
61 fprintf(stderr, "102ch: NHK BS2\n");
62 fprintf(stderr, "103ch: NHK BShi\n");
63 fprintf(stderr, "141ch: BS Nittele\n");
64 fprintf(stderr, "151ch: BS Asahi\n");
65 fprintf(stderr, "161ch: BS-TBS\n");
66 fprintf(stderr, "171ch: BS Japan\n");
67 fprintf(stderr, "181ch: BS Fuji\n");
68 fprintf(stderr, "191ch: WOWOW\n");
69 fprintf(stderr, "200ch: Star Channel\n");
70 fprintf(stderr, "211ch: BS11 Digital\n");
71 fprintf(stderr, "222ch: TwellV\n");
72 fprintf(stderr, "CS2-CS24: CS Channels\n");
73 }
74
75 int
76 parse_time(char *rectimestr, int *recsec)
77 {
78 /* indefinite */
79 if(!strcmp("-", rectimestr)) {
80 *recsec = -1;
81 }
82 /* colon */
83 else if(strchr(rectimestr, ':')) {
84 int n1, n2, n3;
85 if(sscanf(rectimestr, "%d:%d:%d", &n1, &n2, &n3) == 3)
86 *recsec = n1 * 3600 + n2 * 60 + n3;
87 else if(sscanf(rectimestr, "%d:%d", &n1, &n2) == 2)
88 *recsec = n1 * 3600 + n2 * 60;
89 }
90 /* HMS */
91 else {
92 char *tmpstr;
93 char *p1, *p2;
94
95 tmpstr = strdup(rectimestr);
96 p1 = tmpstr;
97 while(*p1 && !isdigit(*p1))
98 p1++;
99
100 /* hour */
101 if((p2 = strchr(p1, 'H')) || (p2 = strchr(p1, 'h'))) {
102 *p2 = '\0';
103 *recsec += atoi(p1) * 3600;
104 p1 = p2 + 1;
105 while(*p1 && !isdigit(*p1))
106 p1++;
107 }
108
109 /* minute */
110 if((p2 = strchr(p1, 'M')) || (p2 = strchr(p1, 'm'))) {
111 *p2 = '\0';
112 *recsec += atoi(p1) * 60;
113 p1 = p2 + 1;
114 while(*p1 && !isdigit(*p1))
115 p1++;
116 }
117
118 /* second */
119 *recsec += atoi(p1);
120
121 free(tmpstr);
122 }
123
124 return 0; /* success */
125 }
126
127 int
128 main(int argc, char **argv)
129 {
130 int msqid;
131 int msgflg = IPC_CREAT | 0666;
132 key_t key = 0;
133 int channel=0, recsec = 0, extsec=0;
134 message_buf sbuf;
135 size_t buf_length;
136
137 int result;
138 int option_index;
139 struct option long_options[] = {
140 { "pid", 1, NULL, 'p'},
141 { "channel", 1, NULL, 'c'},
142 { "extend", 1, NULL, 'e'},
143 { "time", 1, NULL, 't'},
144 { "help", 0, NULL, 'h'},
145 { "version", 0, NULL, 'v'},
146 { "list", 0, NULL, 'l'},
147 {0, 0, NULL, 0} /* terminate */
148 };
149
150 while((result = getopt_long(argc, argv, "p:c:e:t:hvl",
151 long_options, &option_index)) != -1) {
152 switch(result) {
153 case 'h':
154 fprintf(stderr, "\n");
155 show_usage(argv[0]);
156 fprintf(stderr, "\n");
157 show_options();
158 fprintf(stderr, "\n");
159 show_channels();
160 fprintf(stderr, "\n");
161 exit(0);
162 break;
163 case 'v':
164 fprintf(stderr, "%s %s\n", argv[0], version);
165 fprintf(stderr, "control command for recpt1.\n");
166 exit(0);
167 break;
168 case 'l':
169 show_channels();
170 exit(0);
171 break;
172 /* following options require argument */
173 case 'p':
174 key = (key_t)atoi(optarg);
175 fprintf(stderr, "Pid = %d\n", key);
176 break;
177 case 'c':
178 channel = atoi(optarg);
179 fprintf(stderr, "Channel = %d\n", channel);
180 break;
181 case 'e':
182 parse_time(optarg, &extsec);
183 fprintf(stderr, "Extend %d sec\n", extsec);
184 break;
185 case 't':
186 parse_time(optarg, &recsec);
187 fprintf(stderr, "Total recording time = %d sec\n", recsec);
188 break;
189 }
190 }
191
192 if(!key) {
193 fprintf(stderr, "Arguments are necessary!\n");
194 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
195 exit(1);
196 }
197
198 if ((msqid = msgget(key, msgflg )) < 0) {
199 perror("msgget");
200 exit(1);
201 }
202
203 sbuf.mtype = 1;
204 sprintf(sbuf.mtext, "ch=%d t=%d e=%d", channel, recsec, extsec);
205
206 buf_length = strlen(sbuf.mtext) + 1 ;
207
208 if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
209 perror("msgsnd");
210 exit(1);
211 }
212
213 exit(0);
214 }