summaryrefslogtreecommitdiff
path: root/axfer/main.c
blob: 21d3b4aeecbf7dac55b9b9c6429da260b7d9e520 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: GPL-2.0
// main.c - an entry point for this program.
//
// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
//
// Originally written as 'aplay', by Michael Beck and Jaroslav Kysela.
//
// Licensed under the terms of the GNU General Public License, version 2.

#include "subcmd.h"
#include "misc.h"

#include "version.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

enum subcmds {
	SUBCMD_TRANSFER = 0,
	SUBCMD_LIST,
	SUBCMD_HELP,
	SUBCMD_VERSION,
};

char *arg_duplicate_string(const char *str, int *err)
{
	char *ptr;

	// For safe.
	if (strlen(str) > 1024) {
		*err = -EINVAL;
		return NULL;
	}

	ptr = strdup(str);
	if (ptr == NULL)
		*err = -ENOMEM;

	return ptr;
}

long arg_parse_decimal_num(const char *str, int *err)
{
	long val;
	char *endptr;

	errno = 0;
	val = strtol(str, &endptr, 0);
	if (errno > 0) {
		*err = -errno;
		return 0;
	}
	if (*endptr != '\0') {
		*err = -EINVAL;
		return 0;
	}

	return val;
}

static void print_version(const char *const cmdname)
{
	printf("%s: version %s\n", cmdname, SND_UTIL_VERSION_STR);
}

static void print_help(void)
{
	printf(
"Usage:\n"
"  axfer transfer DIRECTION OPTIONS\n"
"  axfer list DIRECTION OPTIONS\n"
"  axfer version\n"
"  axfer help\n"
"\n"
"  where:\n"
"    DIRECTION = capture | playback\n"
"    OPTIONS = -h | --help | (subcommand specific)\n"
	);
}

// Backward compatibility to aplay(1).
static bool decide_subcmd(int argc, char *const *argv, enum subcmds *subcmd)
{
	static const struct {
		const char *const name;
		enum subcmds subcmd;
	} long_opts[] = {
		{"--list-devices",	SUBCMD_LIST},
		{"--list-pcms",		SUBCMD_LIST},
		{"--help",  		SUBCMD_HELP},
		{"--version",  		SUBCMD_VERSION},
	};
	static const struct {
		unsigned char c;
		enum subcmds subcmd;
	} short_opts[] = {
		{'l', SUBCMD_LIST},
		{'L', SUBCMD_LIST},
		{'h', SUBCMD_HELP},
	};
	char *pos;
	int i, j;

	if (argc == 1)
		return false;

	// Original command system. For long options.
	for (i = 0; i < ARRAY_SIZE(long_opts); ++i) {
		for (j = 0; j < argc; ++j) {
			if (!strcmp(long_opts[i].name, argv[j])) {
				*subcmd = long_opts[i].subcmd;
				return true;
			}
		}
	}

	// Original command system. For short options.
	for (i = 1; i < argc; ++i) {
		// Pick up short options only.
		if (argv[i][0] != '-' || argv[i][0] == '\0' ||
		    argv[i][1] == '-' || argv[i][1] == '\0')
			continue;
		for (pos = argv[i]; *pos != '\0'; ++pos) {
			for (j = 0; j < ARRAY_SIZE(short_opts); ++j) {
				if (*pos == short_opts[j].c) {
					*subcmd = short_opts[j].subcmd;
					return true;
				}
			}
		}
	}

	return false;
}

// Backward compatibility to aplay(1).
static bool decide_direction(int argc, char *const *argv,
			     snd_pcm_stream_t *direction)
{
	static const struct {
		const char *const name;
		snd_pcm_stream_t direction;
	} long_opts[] = {
		{"--capture",	SND_PCM_STREAM_CAPTURE},
		{"--playback",	SND_PCM_STREAM_PLAYBACK},
	};
	static const struct {
		unsigned char c;
		snd_pcm_stream_t direction;
	} short_opts[] = {
		{'C',		SND_PCM_STREAM_CAPTURE},
		{'P',		SND_PCM_STREAM_PLAYBACK},
	};
	static const char *const aliases[] = {
		[SND_PCM_STREAM_CAPTURE] = "arecord",
		[SND_PCM_STREAM_PLAYBACK] = "aplay",
	};
	int i, j;
	char *pos;

	// Original command system. For long options.
	for (i = 0; i < ARRAY_SIZE(long_opts); ++i) {
		for (j = 0; j < argc; ++j) {
			if (!strcmp(long_opts[i].name, argv[j])) {
				*direction = long_opts[i].direction;
				return true;
			}
		}
	}

	// Original command system. For short options.
	for (i = 1; i < argc; ++i) {
		// Pick up short options only.
		if (argv[i][0] != '-' || argv[i][0] == '\0' ||
		    argv[i][1] == '-' || argv[i][1] == '\0')
			continue;
		for (pos = argv[i]; *pos != '\0'; ++pos) {
			for (j = 0; j < ARRAY_SIZE(short_opts); ++j) {
				if (*pos == short_opts[j].c) {
					*direction = short_opts[j].direction;
					return true;
				}
			}
		}
	}

	// If not decided yet, judge according to command name.
	for (i = 0; i < ARRAY_SIZE(aliases); ++i) {
		for (pos = argv[0] + strlen(argv[0]); pos != argv[0]; --pos) {
			if (strstr(pos, aliases[i]) != NULL) {
				*direction = i;
				return true;
			}
		}
	}

	return false;
}

static bool detect_subcmd(int argc, char *const *argv, enum subcmds *subcmd)
{
	static const char *const subcmds[] = {
		[SUBCMD_TRANSFER] = "transfer",
		[SUBCMD_LIST] = "list",
		[SUBCMD_HELP] = "help",
		[SUBCMD_VERSION] = "version",
	};
	int i;

	if (argc < 2)
		return false;

	for (i = 0; i < ARRAY_SIZE(subcmds); ++i) {
		if (!strcmp(argv[1], subcmds[i])) {
			*subcmd = i;
			return true;
		}
	}

	return false;
}

static bool detect_direction(int argc, char *const *argv,
			     snd_pcm_stream_t *direction)
{
	if (argc < 3)
		return false;

	if (!strcmp(argv[2], "capture")) {
		*direction = SND_PCM_STREAM_CAPTURE;
		return true;
	}

	if (!strcmp(argv[2], "playback")) {
		*direction = SND_PCM_STREAM_PLAYBACK;
		return true;
	}

	return false;
}

int main(int argc, char *const *argv)
{
	snd_pcm_stream_t direction;
	enum subcmds subcmd;
	int err = 0;

	// For compatibility to aplay(1) implementation.
	if (strstr(argv[0], "arecord") == argv[0] + strlen(argv[0]) - 7 ||
	    strstr(argv[0], "aplay") == argv[0] + strlen(argv[0]) - 5) {
		if (!decide_direction(argc, argv, &direction))
			direction = SND_PCM_STREAM_PLAYBACK;
		if (!decide_subcmd(argc, argv, &subcmd))
			subcmd = SUBCMD_TRANSFER;
	} else {
		// The first option should be one of subcommands.
		if (!detect_subcmd(argc, argv, &subcmd))
			subcmd = SUBCMD_HELP;
		// The second option should be either 'capture' or 'direction'
		// if subcommand is neither 'version' nor 'help'.
		if (subcmd != SUBCMD_VERSION && subcmd != SUBCMD_HELP) {
			if (!detect_direction(argc, argv, &direction)) {
				subcmd = SUBCMD_HELP;
			} else {
				// argv[0] is needed for unparsed option to use
				// getopt_long(3).
				argc -= 2;
				argv += 2;
			}
		}
	}

	if (subcmd == SUBCMD_TRANSFER)
		err = subcmd_transfer(argc, argv, direction);
	else if (subcmd == SUBCMD_LIST)
		err = subcmd_list(argc, argv, direction);
	else if (subcmd == SUBCMD_VERSION)
		print_version(argv[0]);
	else
		print_help();
	if (err < 0)
		return EXIT_FAILURE;

	return EXIT_SUCCESS;
}