net: Split makesock(), part 2: Introduce makesock_addrinfo().
[paraslash.git] / afh.c
1 /*
2  * Copyright (C) 2008-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file afh.c Paraslash's standalone audio format handler tool. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "string.h"
13 #include "afh.cmdline.h"
14 #include "fd.h"
15 #include "afh.h"
16 #include "error.h"
17 #include "version.h"
18 #include "ggo.h"
19
20 static struct afh_args_info conf;
21 INIT_AFH_ERRLISTS;
22
23 static int loglevel;
24 INIT_STDERR_LOGGING(loglevel)
25
26 static void print_info(int audio_format_num, struct afh_info *afhi)
27 {
28         char *msg;
29
30         afh_get_afhi_txt(audio_format_num, afhi, &msg);
31         printf("%s", msg);
32         free(msg);
33 }
34
35 static void print_chunk_table(struct afh_info *afhi)
36 {
37         int i;
38
39         if (conf.parser_friendly_given) {
40                 printf("chunk_table: ");
41                 for (i = 0; i <= afhi->chunks_total; i++)
42                         printf("%u ", afhi->chunk_table[i]);
43                 printf("\n");
44                 return;
45         }
46         for (i = 1; i <= afhi->chunks_total; i++) {
47                 struct timeval tv;
48                 long unsigned from, to;
49                 tv_scale(i - 1, &afhi->chunk_tv, &tv);
50                 from = tv2ms(&tv);
51                 tv_scale(i, &afhi->chunk_tv, &tv);
52                 to = tv2ms(&tv);
53                 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
54                         from / 1000, from % 1000, to / 1000, to % 1000,
55                         afhi->chunk_table[i - 1], afhi->chunk_table[i],
56                         afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
57         }
58 }
59
60 __noreturn static void print_help_and_die(void)
61 {
62         struct ggo_help h = DEFINE_GGO_HELP(afh);
63         int d = conf.detailed_help_given;
64         unsigned flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
65
66         ggo_print_help(&h, flags);
67         printf("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
68         exit(EXIT_SUCCESS);
69 }
70
71 /**
72  * The main function of para_afh.
73  *
74  * \param argc Usual argument count.
75  * \param argv Usual argument vector.
76  *
77  * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
78  */
79 int main(int argc, char **argv)
80 {
81         int i, ret, audio_format_num, fd;
82         void *audio_file_data;
83         size_t audio_file_size;
84         struct afh_info afhi;
85
86         afh_cmdline_parser(argc, argv, &conf);
87         loglevel = get_loglevel_by_name(conf.loglevel_arg);
88         version_handle_flag("afh", conf.version_given);
89         if (conf.help_given || conf.detailed_help_given)
90                 print_help_and_die();
91         afh_init();
92         ret = -E_AFH_SYNTAX;
93         if (conf.inputs_num == 0)
94                 goto out;
95         for (i = 0; i < conf.inputs_num; i++) {
96                 int ret2;
97                 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
98                         &audio_file_size, &fd);
99                 if (ret < 0) {
100                         PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
101                         goto out;
102                 }
103                 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
104                         fd, &afhi);
105                 if (ret < 0)
106                         goto out;
107
108                 audio_format_num = ret;
109                 printf("File %d: %s\n", i + 1, conf.inputs[i]);
110                 print_info(audio_format_num, &afhi);
111                 if (conf.chunk_table_given)
112                         print_chunk_table(&afhi);
113                 printf("\n");
114                 clear_afhi(&afhi);
115                 ret2 = para_munmap(audio_file_data, audio_file_size);
116                 if (ret2 < 0 && ret >= 0)
117                         ret = ret2;
118                 if (ret < 0)
119                         break;
120         }
121 out:
122         if (ret < 0)
123                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
124         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
125 }