mixer: sleep/snooze: Close mixer during sleep.
[paraslash.git] / afh.c
1 /* Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file afh.c Paraslash's standalone audio format handler tool. */
4
5 #include <regex.h>
6 #include <lopsub.h>
7
8 #include "afh.lsg.h"
9 #include "para.h"
10 #include "string.h"
11 #include "fd.h"
12 #include "afh.h"
13 #include "error.h"
14 #include "version.h"
15
16 /** Array of error strings. */
17 DEFINE_PARA_ERRLIST;
18
19 static struct lls_parse_result *lpr;
20
21 #define CMD_PTR (lls_cmd(0, afh_suite))
22 #define OPT_RESULT(_name) (lls_opt_result(LSG_AFH_PARA_AFH_OPT_ ## _name, lpr))
23 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
24 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
25 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
26
27 static int loglevel;
28 INIT_STDERR_LOGGING(loglevel)
29
30 static inline bool tag_needs_update(bool given, const char *tag,
31                 const char *arg)
32 {
33         return given && (!tag || strcmp(tag, arg) != 0);
34 }
35
36 static int rewrite_tags(const char *name, int input_fd, void *map,
37                 size_t map_size, int audio_format_id, struct afh_info *afhi)
38 {
39         struct taginfo *tags = &afhi->tags;
40         bool modified = false;
41         char *tmp_name;
42         const char *arg;
43         int output_fd = -1, ret;
44         struct stat sb;
45
46         arg = OPT_STRING_VAL(YEAR);
47         if (tag_needs_update(OPT_GIVEN(YEAR), tags->year, arg)) {
48                 free(tags->year);
49                 tags->year = para_strdup(arg);
50                 modified = true;
51         }
52         arg = OPT_STRING_VAL(TITLE);
53         if (tag_needs_update(OPT_GIVEN(TITLE), tags->title, arg)) {
54                 free(tags->title);
55                 tags->title = para_strdup(arg);
56                 modified = true;
57         }
58         arg = OPT_STRING_VAL(ARTIST);
59         if (tag_needs_update(OPT_GIVEN(ARTIST), tags->artist, arg)) {
60                 free(tags->artist);
61                 tags->artist = para_strdup(arg);
62                 modified = true;
63         }
64         arg = OPT_STRING_VAL(ALBUM);
65         if (tag_needs_update(OPT_GIVEN(ALBUM), tags->album, arg)) {
66                 free(tags->album);
67                 tags->album = para_strdup(arg);
68                 modified = true;
69         }
70         arg = OPT_STRING_VAL(COMMENT);
71         if (tag_needs_update(OPT_GIVEN(COMMENT), tags->comment, arg)) {
72                 free(tags->comment);
73                 tags->comment = para_strdup(arg);
74                 modified = true;
75         }
76         if (!modified) {
77                 PARA_WARNING_LOG("no modifications necessary\n");
78                 return 0;
79         }
80         /*
81          * mkstmp() creates the temporary file with permissions 0600, but we
82          * like it to have the same permissions as the original file, so we
83          * have to get this information.
84          */
85         if (fstat(input_fd, &sb) < 0) {
86                 ret = -ERRNO_TO_PARA_ERROR(errno);
87                 PARA_ERROR_LOG("failed to fstat fd %d (%s)\n", input_fd, name);
88                 return ret;
89         }
90         tmp_name = make_message("%s.XXXXXX", name);
91         ret = mkstemp(tmp_name);
92         if (ret < 0) {
93                 ret = -ERRNO_TO_PARA_ERROR(errno);
94                 PARA_ERROR_LOG("could not create temporary file\n");
95                 goto out;
96         }
97         output_fd = ret;
98         if (fchmod(output_fd, sb.st_mode) < 0) {
99                 ret = -ERRNO_TO_PARA_ERROR(errno);
100                 PARA_ERROR_LOG("failed to fchmod fd %d (%s)\n", output_fd,
101                         tmp_name);
102                 goto out;
103         }
104         ret = afh_rewrite_tags(audio_format_id, map, map_size, tags, output_fd,
105                 tmp_name);
106         if (ret < 0)
107                 goto out;
108         if (OPT_GIVEN(BACKUP)) {
109                 char *backup_name = make_message("%s~", name);
110                 ret = xrename(name, backup_name);
111                 free(backup_name);
112                 if (ret < 0)
113                         goto out;
114         }
115         ret = xrename(tmp_name, name);
116 out:
117         if (ret < 0 && output_fd >= 0)
118                 unlink(tmp_name); /* ignore errors */
119         free(tmp_name);
120         if (output_fd >= 0)
121                 close(output_fd);
122         return ret;
123 }
124
125 static void print_info(int audio_format_num, struct afh_info *afhi)
126 {
127         char *msg;
128
129         afh_get_afhi_txt(audio_format_num, afhi, &msg);
130         printf("%s", msg);
131         free(msg);
132 }
133
134 static void print_chunk_table(struct afh_info *afhi, int audio_format_id,
135                 const void *map, size_t mapsize)
136 {
137         int i, ret;
138         void *ctx = NULL;
139
140         for (i = 0; i < afhi->chunks_total; i++) {
141                 struct timeval tv;
142                 long unsigned from, to;
143                 const char *buf;
144                 size_t len;
145                 tv_scale(i, &afhi->chunk_tv, &tv);
146                 from = tv2ms(&tv);
147                 tv_scale(i + 1, &afhi->chunk_tv, &tv);
148                 to = tv2ms(&tv);
149                 ret = afh_get_chunk(i, afhi, audio_format_id, map, mapsize,
150                         &buf, &len, &ctx);
151                 if (ret < 0) {
152                         PARA_ERROR_LOG("fatal: chunk %d: %s\n", i,
153                                 para_strerror(-ret));
154                         return;
155                 }
156                 if (!OPT_GIVEN(PARSER_FRIENDLY))
157                         printf("%d [%lu.%03lu - %lu.%03lu] ", i, from / 1000,
158                                 from % 1000, to / 1000, to % 1000);
159                 printf("%td - %td", buf - (const char *)map,
160                         buf + len - (const char *)map);
161                 if (!OPT_GIVEN(PARSER_FRIENDLY))
162                         printf(" (%zu)", len);
163                 printf("\n");
164         }
165         afh_close(ctx, audio_format_id);
166 }
167
168 static void handle_help_flags(void)
169 {
170         char *help;
171
172         if (OPT_GIVEN(DETAILED_HELP))
173                 help = lls_long_help(CMD_PTR);
174         else if (OPT_GIVEN(HELP) || lls_num_inputs(lpr) == 0)
175                 help = lls_short_help(CMD_PTR);
176         else
177                 return;
178         printf("%s", help);
179         free(help);
180         printf("Supported audio formats\n  %s\n", AUDIO_FORMAT_HANDLERS);
181         exit(EXIT_SUCCESS);
182 }
183
184 /**
185  * The main function of para_afh.
186  *
187  * \param argc Usual argument count.
188  * \param argv Usual argument vector.
189  *
190  * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
191  */
192 int main(int argc, char **argv)
193 {
194         int i, ret = 0, audio_format_num, fd;
195         void *audio_file_data;
196         size_t audio_file_size;
197         struct afh_info afhi;
198         char *errctx;
199
200         ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
201         if (ret < 0)
202                 goto out;
203         loglevel = OPT_UINT32_VAL(LOGLEVEL);
204         version_handle_flag("afh", OPT_GIVEN(VERSION));
205         handle_help_flags();
206         afh_init();
207         for (i = 0; i < lls_num_inputs(lpr); i++) {
208                 int ret2;
209                 const char *path = lls_input(i, lpr);
210                 ret = mmap_full_file(path, O_RDONLY, &audio_file_data,
211                         &audio_file_size, &fd);
212                 if (ret < 0) {
213                         PARA_ERROR_LOG("failed to mmap \"%s\"\n", path);
214                         goto out;
215                 }
216                 ret = compute_afhi(path, audio_file_data, audio_file_size,
217                         fd, &afhi);
218                 if (ret >= 0) {
219                         audio_format_num = ret;
220                         if (OPT_GIVEN(MODIFY)) {
221                                 ret = rewrite_tags(path, fd, audio_file_data,
222                                         audio_file_size, audio_format_num, &afhi);
223                         } else {
224                                 printf("File %d: %s\n", i + 1, path);
225                                 print_info(audio_format_num, &afhi);
226                                 if (OPT_GIVEN(CHUNK_TABLE))
227                                         print_chunk_table(&afhi, audio_format_num,
228                                                 audio_file_data, audio_file_size);
229                         }
230                         clear_afhi(&afhi);
231                 }
232                 close(fd);
233                 ret2 = para_munmap(audio_file_data, audio_file_size);
234                 if (ret2 < 0 && ret >= 0)
235                         ret = ret2;
236                 if (ret < 0)
237                         break;
238         }
239 out:
240         if (errctx)
241                 PARA_ERROR_LOG("%s\n", errctx);
242         if (ret < 0)
243                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
244         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
245 }