• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011-2012 - Mauro Carvalho Chehab
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation version 2
7  * of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18  *
19  */
20 
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <argp.h>
31 
32 #ifdef ENABLE_NLS
33 # define _(string) gettext(string)
34 # include "gettext.h"
35 # include <locale.h>
36 # include <langinfo.h>
37 # include <iconv.h>
38 #else
39 # define _(string) string
40 #endif
41 
42 # define N_(string) string
43 
44 #include "libdvbv5/dvb-file.h"
45 #include "libdvbv5/dvb-demux.h"
46 #include "libdvbv5/dvb-scan.h"
47 
48 #define PROGRAM_NAME	"dvb-format-convert"
49 
50 struct arguments {
51 	char *input_file, *output_file;
52 	enum dvb_file_formats input_format, output_format;
53 	int delsys;
54 };
55 
56 static const struct argp_option options[] = {
57 	{"input-format",	'I',	N_("format"),	0, N_("Valid input formats: ZAP, CHANNEL, DVBV5"), 0},
58 	{"output-format",	'O',	N_("format"),	0, N_("Valid output formats: VDR, ZAP, CHANNEL, DVBV5"), 0},
59 	{"delsys",		's',	N_("system"),	0, N_("Delivery system type. Needed if input or output format is ZAP"), 0},
60 	{"help",        '?',	0,		0,	N_("Give this help list"), -1},
61 	{"usage",	-3,	0,		0,	N_("Give a short usage message")},
62 	{"version",	'V',	0,		0,	N_("Print program version"), -1},
63 	{ 0, 0, 0, 0, 0, 0 }
64 };
65 
66 const char *argp_program_version = PROGRAM_NAME " version " V4L_UTILS_VERSION;
67 const char *argp_program_bug_address = "Mauro Carvalho Chehab <mchehab@kernel.org>";
68 
parse_opt(int k,char * optarg,struct argp_state * state)69 static error_t parse_opt(int k, char *optarg, struct argp_state *state)
70 {
71 	struct arguments *args = state->input;
72 	switch (k) {
73 	case 'I':
74 		args->input_format = dvb_parse_format(optarg);
75 		break;
76 	case 'O':
77 		args->output_format = dvb_parse_format(optarg);
78 		break;
79 	case 's':
80 		args->delsys = dvb_parse_delsys(optarg);
81 		break;
82 	case '?':
83 		argp_state_help(state, state->out_stream,
84 				ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG
85 				| ARGP_HELP_DOC);
86 		fprintf(state->out_stream, _("\nReport bugs to %s.\n"), argp_program_bug_address);
87 		exit(0);
88 	case 'V':
89 		fprintf (state->out_stream, "%s\n", argp_program_version);
90 		exit(0);
91 	case -3:
92 		argp_state_help(state, state->out_stream, ARGP_HELP_USAGE);
93 		exit(0);
94 	default:
95 		return ARGP_ERR_UNKNOWN;
96 	};
97 	return 0;
98 }
99 
convert_file(struct arguments * args)100 static int convert_file(struct arguments *args)
101 {
102 	struct dvb_file *dvb_file = NULL;
103 	int ret;
104 
105 	printf(_("Reading file %s\n"), args->input_file);
106 
107 	dvb_file = dvb_read_file_format(args->input_file, args->delsys,
108 				    args->input_format);
109 	if (!dvb_file) {
110 		fprintf(stderr, _("Error reading file %s\n"), args->input_file);
111 		return -1;
112 	}
113 
114 	printf(_("Writing file %s\n"), args->output_file);
115 	ret = dvb_write_file_format(args->output_file, dvb_file,
116 				    args->delsys, args->output_format);
117 	dvb_file_free(dvb_file);
118 
119 	return ret;
120 }
121 
main(int argc,char ** argv)122 int main(int argc, char **argv)
123 {
124 	struct arguments args = {};
125 	int idx = -1, missing = 0;
126 	const struct argp argp = {
127 		.options = options,
128 		.parser = parse_opt,
129 		.doc = N_("scan DVB services using the channel file"),
130 		.args_doc = N_("<input file> <output file>"),
131 	};
132 
133 #ifdef ENABLE_NLS
134 	setlocale (LC_ALL, "");
135 	bindtextdomain (PACKAGE, LOCALEDIR);
136 	textdomain (PACKAGE);
137 #endif
138 
139 	memset(&args, 0, sizeof(args));
140 	argp_parse(&argp, argc, argv, ARGP_NO_HELP | ARGP_NO_EXIT, &idx, &args);
141 
142 	if (idx + 1 < argc) {
143 		args.input_file = argv[idx];
144 		args.output_file = argv[idx + 1];
145 	}
146 
147 	if (args.input_format == FILE_UNKNOWN) {
148 		fprintf(stderr, _("ERROR: Please specify a valid input format\n"));
149 		missing = 1;
150 	} else if (!args.input_file) {
151 		fprintf(stderr, _("ERROR: Please specify a valid input file\n"));
152 		missing = 1;
153 	} else if (args.output_format == FILE_UNKNOWN) {
154 		fprintf(stderr, _("ERROR: Please specify a valid output format\n"));
155 		missing = 1;
156 	} else if (!args.output_file) {
157 		fprintf(stderr, _("ERROR: Please specify a valid output file\n"));
158 		missing = 1;
159 	} else if (((args.input_format == FILE_ZAP) ||
160 		   (args.output_format == FILE_ZAP)) &&
161 		   (args.delsys <= 0 || args.delsys == SYS_ISDBS)) {
162 		fprintf(stderr, _("ERROR: Please specify a valid delivery system for ZAP format\n"));
163 		missing = 1;
164 	}
165 	if (missing) {
166 		argp_help(&argp, stderr, ARGP_HELP_STD_HELP, PROGRAM_NAME);
167 		return -1;
168 	}
169 
170 	return convert_file(&args);
171 }
172