[alsa-devel] [PATCH V4 0/4] Some fixes against previous comments
From: "Lu, Han" han.lu@intel.com
Some fixes and adjustments against previous comments of Takashi
Lu, Han (4): BAT: Remove redundant message strings BAT: Use colon instead of comma for separation BAT: Change comments and interface of usage() BAT: Use dynamic temp file
bat/bat.c | 106 +++++++++++++++++++++++++++++++---------------------------- bat/common.h | 2 +- 2 files changed, 57 insertions(+), 51 deletions(-)
From: "Lu, Han" han.lu@intel.com
Cutting down 6 message strings to 2, as translators need to work on each different variant.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 24c74e8..4320e22 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -41,43 +41,33 @@ static int get_duration(struct bat *bat) char *ptrf, *ptri;
duration_f = strtof(bat->narg, &ptrf); - if (duration_f == HUGE_VALF || duration_f == -HUGE_VALF) { - fprintf(bat->err, _("duration float overflow: %f %d\n"), - duration_f, -errno); - return -errno; - } else if (duration_f == 0.0 && errno != 0) { - fprintf(bat->err, _("duration float underflow: %f %d\n"), - duration_f, -errno); - return -errno; - } + if (duration_f == HUGE_VALF || duration_f == -HUGE_VALF + || (duration_f == 0.0 && errno != 0)) + goto err_exit;
duration_i = strtol(bat->narg, &ptri, 10); - if (duration_i == LONG_MAX) { - fprintf(bat->err, _("duration long overflow: %ld %d\n"), - duration_i, -errno); - return -errno; - } else if (duration_i == LONG_MIN) { - fprintf(bat->err, _("duration long underflow: %ld %d\n"), - duration_i, -errno); - return -errno; - } + if (duration_i == LONG_MAX || duration_i == LONG_MIN) + goto err_exit;
- if (*ptrf == 's') { + if (*ptrf == 's') bat->frames = duration_f * bat->rate; - } else if (*ptri == 0) { + else if (*ptri == 0) bat->frames = duration_i; - } else { - fprintf(bat->err, _("invalid duration: %s\n"), bat->narg); - return -EINVAL; - } + else + bat->frames = -1;
if (bat->frames <= 0 || bat->frames > MAX_FRAMES) { - fprintf(bat->err, _("duration out of range: (0, %d(%ds))\n"), - MAX_FRAMES, (bat->frames / bat->rate)); + fprintf(bat->err, _("Invalid duration. Range: (0, %d(%fs))\n"), + MAX_FRAMES, (double)MAX_FRAMES / bat->rate); return -EINVAL; }
return 0; + +err_exit: + fprintf(bat->err, _("Duration overflow/underflow: %d\n"), -errno); + + return -errno; }
static void get_sine_frequencies(struct bat *bat, char *freq)
From: "Lu, Han" han.lu@intel.com
Use colon instead of comma to separate frequency parameters, for in several locale comma may be handled as decimal point.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 4320e22..2320984 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -74,7 +74,7 @@ static void get_sine_frequencies(struct bat *bat, char *freq) { char *tmp1;
- tmp1 = strchr(freq, ','); + tmp1 = strchr(freq, ':'); if (tmp1 == NULL) { bat->target_freq[1] = bat->target_freq[0] = atof(optarg); } else {
From: "Lu, Han" han.lu@intel.com
1. Change comment strings to make the descriptions more clear; 2. Add indent for option lines that have no indent; 3. Use a const string instead of argv[0] as program name.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 2320984..2be3efb 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -268,28 +268,27 @@ static void test_capture(struct bat *bat) } }
-static void usage(struct bat *bat, char *argv[]) +static void usage(struct bat *bat) { fprintf(bat->log, -_("Usage:%s [Option]...\n" +_("Usage: bat [-options]...\n" "\n" -"-h, --help help\n" -"-D sound card\n" -"-P playback pcm\n" -"-C capture pcm\n" -"-f sample size\n" -"-c number of channels\n" -"-r sampling rate\n" -"-n frames to capture\n" -"-k sigma k\n" -"-F target frequency\n" -"-p total number of periods to play/capture\n" -" --log=# path of log file. if not set, logs be put to stdout,\n" -" and errors be put to stderr.\n" -" --file=# input file\n" -" --saveplay=# save playback content to target file, for debug\n" -" --local internal loop, bypass hardware\n" -), argv[0]); +" -h, --help this help\n" +" -D pcm device for both playback and capture\n" +" -P pcm device for playback\n" +" -C pcm device for capture\n" +" -f sample format\n" +" -c number of channels\n" +" -r sampling rate\n" +" -n frames to playback or capture\n" +" -k parameter for frequency detecting threshold\n" +" -F target frequency\n" +" -p total number of periods to play/capture\n" +" --log=# file that both stdout and strerr redirecting to\n" +" --file=# file for playback\n" +" --saveplay=# file that storing playback content, for debug\n" +" --local internal loop, set to bypass pcm hardware devices\n" +)); fprintf(bat->log, _("Recognized sample formats are: %s %s %s %s\n"), snd_pcm_format_name(SND_PCM_FORMAT_U8), snd_pcm_format_name(SND_PCM_FORMAT_S16_LE), @@ -402,7 +401,7 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[]) break; case 'h': default: - usage(bat, argv); + usage(bat); exit(EXIT_SUCCESS); } }
From: "Lu, Han" han.lu@intel.com
Use dynamic temp file instead of fixed temp file to store recorded wav data, for better security.
Signed-off-by: Lu, Han han.lu@intel.com
diff --git a/bat/bat.c b/bat/bat.c index 2be3efb..e1343fb 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -451,6 +451,7 @@ static int validate_options(struct bat *bat) static int bat_init(struct bat *bat) { int err = 0; + char name[] = TEMP_RECORD_FILE_NAME;
/* Determine logging to a file or stdout and stderr */ if (bat->logarg) { @@ -473,10 +474,23 @@ static int bat_init(struct bat *bat) }
/* Determine capture file */ - if (bat->local) + if (bat->local) { bat->capture.file = bat->playback.file; - else - bat->capture.file = TEMP_RECORD_FILE_NAME; + } else { + /* create temp file for sound record and analysis */ + err = mkstemp(name); + if (err == -1) { + fprintf(bat->err, _("Fail to create record file: %d\n"), + -errno); + return -errno; + } + /* store file name which is dynamically created */ + bat->capture.file = strdup(name); + if (bat->capture.file == NULL) + return -errno; + /* close temp file */ + close(err); + }
/* Initial for playback */ if (bat->playback.file == NULL) { @@ -590,8 +604,11 @@ analyze: err = analyze_capture(&bat); out: fprintf(bat.log, _("\nReturn value is %d\n"), err); + if (bat.logarg) fclose(bat.log); + if (!bat.local) + free(bat.capture.file);
return err; } diff --git a/bat/common.h b/bat/common.h index f0254ab..90bc3e2 100644 --- a/bat/common.h +++ b/bat/common.h @@ -15,7 +15,7 @@
#include <alsa/asoundlib.h>
-#define TEMP_RECORD_FILE_NAME "/tmp/bat.wav" +#define TEMP_RECORD_FILE_NAME "/tmp/bat.wav.XXXXXX"
#define OPT_BASE 300 #define OPT_LOG (OPT_BASE + 1)
On Tue, 20 Oct 2015 10:45:44 +0200, han.lu@intel.com wrote:
From: "Lu, Han" han.lu@intel.com
Some fixes and adjustments against previous comments of Takashi
Applied all four patches now. Thanks.
Takashi
Lu, Han (4): BAT: Remove redundant message strings BAT: Use colon instead of comma for separation BAT: Change comments and interface of usage() BAT: Use dynamic temp file
bat/bat.c | 106 +++++++++++++++++++++++++++++++---------------------------- bat/common.h | 2 +- 2 files changed, 57 insertions(+), 51 deletions(-)
-- 1.9.1
participants (2)
-
han.lu@intel.com
-
Takashi Iwai