summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2021-03-26 08:34:50 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2021-03-26 10:12:40 +1000
commit0828fdcea03a51309436e00c0b9e9823d37e91fe (patch)
tree4b11edbe23289c7ecc765dbc8a672fc98f5e069f
parent2babdba1d7eb925a5bd8c3acc21ab1ab331ab673 (diff)
downloadlibinput-0828fdcea03a51309436e00c0b9e9823d37e91fe.tar.gz
tools/record: factor out the output file collection
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r--tools/libinput-record.c128
1 files changed, 74 insertions, 54 deletions
diff --git a/tools/libinput-record.c b/tools/libinput-record.c
index 74b743ac..f493e784 100644
--- a/tools/libinput-record.c
+++ b/tools/libinput-record.c
@@ -2601,6 +2601,67 @@ is_char_dev(const char *path)
return S_ISCHR(st.st_mode) ? F_DEVICE : F_FILE;
}
+enum fposition {
+ ERROR,
+ NOFILE,
+ FIRST,
+ LAST,
+};
+
+static enum fposition
+find_output_file(int argc, char *argv[], const char **output_file)
+{
+ char *first, *last;
+ enum ftype ftype_first, ftype_last;
+
+ first = argv[0];
+
+ ftype_first = is_char_dev(first);
+ if (argc == 1) {
+ /* arg is *not* a char device, so let's assume it's
+ * the output file */
+ if (ftype_first != F_DEVICE) {
+ *output_file = first;
+ return FIRST;
+ }
+ }
+
+ /* multiple arguments, yay */
+ last = argv[argc - 1];
+ ftype_last = is_char_dev(last);
+ /*
+ first is device, last is file -> last
+ first is device, last is device -> noop
+ first is device, last !exist -> last
+ first is file, last is device -> first
+ first is file, last is file -> error
+ first is file, last !exist -> error
+ first !exist, last is device -> first
+ first !exist, last is file -> error
+ first !exit, last !exist -> error
+ */
+#define _m(f, l) (((f) << 8) | (l))
+ switch (_m(ftype_first, ftype_last)) {
+ case _m(F_FILE, F_DEVICE):
+ case _m(F_FILE, F_NOEXIST):
+ case _m(F_NOEXIST, F_DEVICE):
+ *output_file = first;
+ return FIRST;
+ case _m(F_DEVICE, F_FILE):
+ case _m(F_DEVICE, F_NOEXIST):
+ *output_file = last;
+ return LAST;
+ case _m(F_DEVICE, F_DEVICE):
+ break;
+ case _m(F_FILE, F_FILE):
+ case _m(F_NOEXIST, F_FILE):
+ case _m(F_NOEXIST, F_NOEXIST):
+ return ERROR;
+ }
+#undef _m
+ return NOFILE;
+}
+
enum options {
OPT_AUTORESTART,
OPT_HELP,
@@ -2696,62 +2757,21 @@ main(int argc, char **argv)
* because this will only backfire anyway.
*/
if (ndevices >= 1 && output_arg == NULL) {
- char *first, *last;
- enum ftype ftype_first;
-
- first = argv[optind];
- last = argv[argc - 1];
-
- ftype_first = is_char_dev(first);
- if (ndevices == 1) {
- /* arg is *not* a char device, so let's assume it's
- * the output file */
- if (ftype_first != F_DEVICE) {
- output_arg = first;
- optind++;
- ndevices--;
- }
- /* multiple arguments, yay */
- } else {
- enum ftype ftype_last = is_char_dev(last);
- /*
- first is device, last is file -> last
- first is device, last is device -> noop
- first is device, last !exist -> last
- first is file, last is device -> first
- first is file, last is file -> error
- first is file, last !exist -> error
- first !exist, last is device -> first
- first !exist, last is file -> error
- first !exit, last !exist -> error
- */
-#define _m(f, l) (((f) << 8) | (l))
- switch (_m(ftype_first, ftype_last)) {
- case _m(F_FILE, F_DEVICE):
- case _m(F_FILE, F_NOEXIST):
- case _m(F_NOEXIST, F_DEVICE):
- output_arg = first;
- optind++;
- ndevices--;
- break;
- case _m(F_DEVICE, F_FILE):
- case _m(F_DEVICE, F_NOEXIST):
- output_arg = last;
- ndevices--;
- break;
- case _m(F_DEVICE, F_DEVICE):
- break;
- case _m(F_FILE, F_FILE):
- case _m(F_NOEXIST, F_FILE):
- case _m(F_NOEXIST, F_NOEXIST):
- fprintf(stderr, "Ambiguous device vs output file list. Please use --output-file.\n");
- rc = EXIT_INVALID_USAGE;
- goto out;
- }
-#undef _m
+ enum fposition pos = find_output_file(argc - optind,
+ &argv[optind],
+ &output_arg);
+ if (pos == ERROR) {
+ fprintf(stderr,
+ "Ambiguous device vs output file list. "
+ "Please use --output-file.\n");
+ return EXIT_INVALID_USAGE;
}
- }
+ if (pos == FIRST || pos == LAST)
+ ndevices--;
+ if (pos == FIRST)
+ optind++;
+ }
if (ctx.timeout > 0 && output_arg == NULL) {
fprintf(stderr,