2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
21 static const char *VERSION
= "v1.4.0";
22 static const char *PERIOD_EXPRESSION
= "^([[:digit:]]{1,})(:([0-5][[:digit:]]))?$";
27 static struct option longopts
[] = {
28 {"directory", required_argument
, NULL
, 'd'},
29 {"min-length", required_argument
, NULL
, 'l'},
30 {"max-length", required_argument
, NULL
, 'L'},
31 {"expression", required_argument
, NULL
, 'x'},
32 {"exclude-expression", required_argument
, NULL
, 'X'},
33 {"invert", no_argument
, NULL
, 'v'},
34 {"help", no_argument
, NULL
, 'h'},
35 {"min-bitrate", required_argument
, NULL
, 'b'},
36 {"max-bitrate", required_argument
, NULL
, 'B'},
37 {"processes", required_argument
, NULL
, 'P'},
38 {"no-ignorecase", no_argument
, NULL
, 'I'},
39 {"print0", no_argument
, NULL
, '0'}
42 static void init_options(struct opt_options
*opts
);
43 static long parse_long(char *option
);
44 static double parse_period(char *period
);
45 static void prepend_expression(struct opt_options
*opts
, char *expression
, int invert
);
46 static void print_usage(void);
49 opt_get_options(int argc
, char *argv
[])
54 struct opt_options
*opts
= malloc(sizeof(struct opt_options
));
56 err(EXIT_FAILURE
, "malloc struct opt_options");
61 while ((opt
= getopt_long(argc
, argv
, "hd:l:L:x:X:vb:B:P:I0", longopts
, NULL
)) != -1)
64 opts
->pathprefix
= optarg
;
67 opts
->min_length
= parse_period(optarg
);
70 opts
->max_length
= parse_period(optarg
);
73 prepend_expression(opts
, optarg
, 0);
76 prepend_expression(opts
, optarg
, 1);
79 opts
->min_bitrate
= parse_long(optarg
) * 1000;
82 opts
->max_bitrate
= parse_long(optarg
) * 1000;
85 opts
->processes
= (int)parse_long(optarg
);
91 opts
->noignorecase
= true;
108 opt_free_options(struct opt_options
*opts
)
110 assert(opts
!= NULL
);
112 struct element
*e
= opts
->expressionlist
;
115 e
= destroy_element(e
);
126 printf("This is oggfilter %s\n\n", VERSION
);
127 puts("oggfilter [-l|--min-length period] [-L|--max-length period]");
128 puts(" [-b|--min-bitrate bitrate] [-B|--max-bitrate bitrate]");
129 puts(" [-x|--expression regexp] [-X|--exclude-expression regexp]");
130 puts(" [-d|--directory directory] [-P|--processes count]");
131 puts(" [-v|--invert] [-I|--no-ignorecase] [-0|--print0]\n");
132 puts("oggfilter {-h|--help}");
136 init_options(struct opt_options
*opts
)
138 assert(opts
!= NULL
);
140 opts
->min_length
= -1;
141 opts
->max_length
= -1;
142 opts
->min_bitrate
= -1;
143 opts
->max_bitrate
= -1;
144 opts
->expressionlist
= NULL
;
145 opts
->pathprefix
= NULL
;
146 opts
->invert
= false;
148 opts
->noignorecase
= false;
149 opts
->print0
= false;
153 parse_period(char *option
)
155 assert(option
!= NULL
);
159 int e
= regcomp(®ex
, PERIOD_EXPRESSION
, REG_EXTENDED
);
162 regerror(e
, ®ex
, errstr
, sizeof(errstr
));
163 errx(EXIT_FAILURE
, "regcomp \"%s\": %s", PERIOD_EXPRESSION
, errstr
);
167 regmatch_t groups
[PERIOD_GROUPS
];
168 if (regexec(®ex
, option
, PERIOD_GROUPS
, groups
, 0) == 0) {
169 char *minutes
= NULL
;
170 char *seconds
= NULL
;
171 if (groups
[2].rm_so
== -1)
175 option
[groups
[2].rm_so
] = '\0';
176 seconds
= &option
[groups
[3].rm_so
];
180 period
= parse_long(minutes
) * 60;
181 period
+= parse_long(seconds
);
183 errx(EXIT_FAILURE
, "invalid period \"%s\"", option
);
191 parse_long(char *option
)
193 assert(option
!= NULL
);
196 long parsed
= strtol(option
, &endptr
, 10);
197 if (endptr
[0] != '\0')
198 errx(EXIT_FAILURE
, "illegal number \"%s\"", option
);
204 prepend_expression(struct opt_options
*opts
, char *expression
, int invert
)
206 assert(opts
!= NULL
);
207 assert(expression
!= NULL
);
208 assert(invert
== 0 || invert
== 1);
210 struct opt_expression
*x
= malloc(sizeof(*x
));
212 err(EXIT_FAILURE
, "malloc opt_expression");
214 x
->expression
= expression
;
217 struct element
*e
= create_element(x
);
219 err(EXIT_FAILURE
, "create_element");
221 opts
->expressionlist
= prepend_element(e
, opts
->expressionlist
);