string.c: Improve documentation of create_argv().
[paraslash.git] / afh.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
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 inline bool tag_needs_update(bool given, const char *tag,
27                 const char *arg)
28 {
29         return given && (!tag || strcmp(tag, arg) != 0);
30 }
31
32 static int rewrite_tags(const char *name, int input_fd, void *map,
33                 size_t map_size, int audio_format_id, struct afh_info *afhi)
34 {
35         struct taginfo *tags = &afhi->tags;
36         bool modified = false;
37         char *tmp_name;
38         int output_fd = -1, ret;
39         struct stat sb;
40
41         if (tag_needs_update(conf.year_given, tags->year, conf.year_arg)) {
42                 free(tags->year);
43                 tags->year = para_strdup(conf.year_arg);
44                 modified = true;
45         }
46         if (tag_needs_update(conf.title_given, tags->title, conf.title_arg)) {
47                 free(tags->title);
48                 tags->title = para_strdup(conf.title_arg);
49                 modified = true;
50         }
51         if (tag_needs_update(conf.artist_given, tags->artist,
52                         conf.artist_arg)) {
53                 free(tags->artist);
54                 tags->artist = para_strdup(conf.artist_arg);
55                 modified = true;
56         }
57         if (tag_needs_update(conf.album_given, tags->album, conf.album_arg)) {
58                 free(tags->album);
59                 tags->album = para_strdup(conf.album_arg);
60                 modified = true;
61         }
62         if (tag_needs_update(conf.comment_given, tags->comment,
63                         conf.comment_arg)) {
64                 free(tags->comment);
65                 tags->comment = para_strdup(conf.comment_arg);
66                 modified = true;
67         }
68         if (!modified) {
69                 PARA_WARNING_LOG("no modifications necessary\n");
70                 return 0;
71         }
72         /*
73          * mkstmp() creates the temporary file with permissions 0600, but we
74          * like it to have the same permissions as the original file, so we
75          * have to get this information.
76          */
77         if (fstat(input_fd, &sb) < 0) {
78                 ret = -ERRNO_TO_PARA_ERROR(errno);
79                 PARA_ERROR_LOG("failed to fstat fd %d (%s)\n", input_fd, name);
80                 return ret;
81         }
82         tmp_name = make_message("%s.XXXXXX", name);
83         ret = mkstemp(tmp_name);
84         if (ret < 0) {
85                 ret = -ERRNO_TO_PARA_ERROR(errno);
86                 PARA_ERROR_LOG("could not create temporary file\n");
87                 goto out;
88         }
89         output_fd = ret;
90         if (fchmod(output_fd, sb.st_mode) < 0) {
91                 ret = -ERRNO_TO_PARA_ERROR(errno);
92                 PARA_ERROR_LOG("failed to fchmod fd %d (%s)\n", output_fd,
93                         tmp_name);
94                 goto out;
95         }
96         ret = afh_rewrite_tags(audio_format_id, map, map_size, tags, output_fd,
97                 tmp_name);
98         if (ret < 0)
99                 goto out;
100         if (conf.backup_given) {
101                 char *backup_name = make_message("%s~", name);
102                 ret = xrename(name, backup_name);
103                 free(backup_name);
104                 if (ret < 0)
105                         goto out;
106         }
107         ret = xrename(tmp_name, name);
108 out:
109         if (ret < 0 && output_fd >= 0)
110                 unlink(tmp_name); /* ignore errors */
111         free(tmp_name);
112         if (output_fd >= 0)
113                 close(output_fd);
114         return ret;
115 }
116
117 static void print_info(int audio_format_num, struct afh_info *afhi)
118 {
119         char *msg;
120
121         afh_get_afhi_txt(audio_format_num, afhi, &msg);
122         printf("%s", msg);
123         free(msg);
124 }
125
126 static void print_chunk_table(struct afh_info *afhi)
127 {
128         int i;
129
130         if (conf.parser_friendly_given) {
131                 printf("chunk_table: ");
132                 for (i = 0; i <= afhi->chunks_total; i++)
133                         printf("%u ", afhi->chunk_table[i]);
134                 printf("\n");
135                 return;
136         }
137         for (i = 1; i <= afhi->chunks_total; i++) {
138                 struct timeval tv;
139                 long unsigned from, to;
140                 tv_scale(i - 1, &afhi->chunk_tv, &tv);
141                 from = tv2ms(&tv);
142                 tv_scale(i, &afhi->chunk_tv, &tv);
143                 to = tv2ms(&tv);
144                 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
145                         from / 1000, from % 1000, to / 1000, to % 1000,
146                         afhi->chunk_table[i - 1], afhi->chunk_table[i],
147                         afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
148         }
149 }
150
151 __noreturn static void print_help_and_die(void)
152 {
153         struct ggo_help h = DEFINE_GGO_HELP(afh);
154         int d = conf.detailed_help_given;
155         unsigned flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
156
157         ggo_print_help(&h, flags);
158         printf("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
159         exit(EXIT_SUCCESS);
160 }
161
162 /**
163  * The main function of para_afh.
164  *
165  * \param argc Usual argument count.
166  * \param argv Usual argument vector.
167  *
168  * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
169  */
170 int main(int argc, char **argv)
171 {
172         int i, ret = 0, audio_format_num, fd;
173         void *audio_file_data;
174         size_t audio_file_size;
175         struct afh_info afhi;
176
177         afh_cmdline_parser(argc, argv, &conf);
178         loglevel = get_loglevel_by_name(conf.loglevel_arg);
179         version_handle_flag("afh", conf.version_given);
180         if (conf.help_given || conf.detailed_help_given || conf.inputs_num == 0)
181                 print_help_and_die();
182         afh_init();
183         for (i = 0; i < conf.inputs_num; i++) {
184                 int ret2;
185                 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
186                         &audio_file_size, &fd);
187                 if (ret < 0) {
188                         PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
189                         goto out;
190                 }
191                 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
192                         fd, &afhi);
193                 if (ret >= 0) {
194                         audio_format_num = ret;
195                         if (conf.modify_given) {
196                                 ret = rewrite_tags(conf.inputs[i], fd, audio_file_data,
197                                         audio_file_size, audio_format_num, &afhi);
198                         } else {
199                                 printf("File %d: %s\n", i + 1, conf.inputs[i]);
200                                 print_info(audio_format_num, &afhi);
201                                 if (conf.chunk_table_given)
202                                         print_chunk_table(&afhi);
203                                 printf("\n");
204                         }
205                         clear_afhi(&afhi);
206                 }
207                 close(fd);
208                 ret2 = para_munmap(audio_file_data, audio_file_size);
209                 if (ret2 < 0 && ret >= 0)
210                         ret = ret2;
211                 if (ret < 0)
212                         break;
213         }
214 out:
215         if (ret < 0)
216                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
217         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
218 }