Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 231 lines (209 sloc) 5.783 kB
6b8ed1d @ft Initial commit
authored
1 /*
2 * Copyright (c) 2009 taggit workers, All rights reserved.
3 * Terms for redistribution and use can be found in LICENCE.
4 */
5
25607c6 @ft Adding source code documentation to taggit.c
authored
6 /**
7 * @file taggit.c
8 * @brief main() and high level option handling
9 */
10
6b8ed1d @ft Initial commit
authored
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "taggit.h"
15 #include "bsdgetopt.c"
16
25607c6 @ft Adding source code documentation to taggit.c
authored
17 /**
18 * ASCII End-Of-Transmission character code. Used to seperate output for
19 * different files in machine readable mode.
20 */
1837d0f @ft Add tag value listing code (both -m and -l)
authored
21 #define ASCII_EOT (char)0x04
22
25607c6 @ft Adding source code documentation to taggit.c
authored
23 /**
24 * Head of the linked list that holds the information gathered
25 * from all (one or more) -t tag=value options.
26 */
47f01fa @ft Added -t handling code
authored
27 struct taglist *tags_head = NULL;
28
c633894 @ft Starting to actually tag files
authored
29 #if 0
47f01fa @ft Added -t handling code
authored
30 #define TAGGIT_DEBUG
31 #endif
32
25607c6 @ft Adding source code documentation to taggit.c
authored
33 /** possible operation modes */
6b8ed1d @ft Initial commit
authored
34 enum t_mode {
25607c6 @ft Adding source code documentation to taggit.c
authored
35 /** no mode defined, yet; or broken operation mode */
6b8ed1d @ft Initial commit
authored
36 TAGGIT_MODE_INVALID = 0,
25607c6 @ft Adding source code documentation to taggit.c
authored
37 /** list file's tags in human readable form */
6b8ed1d @ft Initial commit
authored
38 TAGGIT_LIST_HUMAN,
25607c6 @ft Adding source code documentation to taggit.c
authored
39 /** list file's tags in machine readable form */
6b8ed1d @ft Initial commit
authored
40 TAGGIT_LIST_MACHINE,
25607c6 @ft Adding source code documentation to taggit.c
authored
41 /** modify meta information in file(s) */
6b8ed1d @ft Initial commit
authored
42 TAGGIT_TAG
43 };
44
25607c6 @ft Adding source code documentation to taggit.c
authored
45 /** global variable describing taggit's operation mode */
34f760f @ft taggit mode should be static in taggit.c
authored
46 static enum t_mode taggit_mode = TAGGIT_MODE_INVALID;
6b8ed1d @ft Initial commit
authored
47
25607c6 @ft Adding source code documentation to taggit.c
authored
48 /**
49 * Check that -m, -l and -t are not used with one another
50 *
51 * @param mode the enum t_mode value to check
52 *
53 * @return void
54 * @sideeffects Exists with EXIT_FAILURE on failure.
55 */
d177e07 @ft Changing scope of a few functions
authored
56 static inline void
6b8ed1d @ft Initial commit
authored
57 check_mode(enum t_mode mode)
58 {
59 if (mode != TAGGIT_MODE_INVALID) {
60 printf("-m, -l and -t may *not* be used at the same time.\n");
61 exit(EXIT_FAILURE);
62 }
63 }
64
25607c6 @ft Adding source code documentation to taggit.c
authored
65 /**
66 * Mode sanity check for tagging mode
67 *
68 * -t may be used multiple times, so run check_mode() only if not in
69 * TAGGIT_TAG mode already.
70 *
71 * @param mode the enum t_mode value to check
72 *
73 * @return void
74 * @sideeffects see check_mode()
75 */
d177e07 @ft Changing scope of a few functions
authored
76 static inline void
47f01fa @ft Added -t handling code
authored
77 check_mode_tag(enum t_mode mode)
78 {
79 if (mode != TAGGIT_MODE_INVALID && mode != TAGGIT_TAG)
80 check_mode(mode);
81 }
82
25607c6 @ft Adding source code documentation to taggit.c
authored
83 /**
84 * Parsing command line options
85 *
86 * Uses NetBSD's getopt() defined in bsdgetopt.c, which is a
87 * traditional getopt implementation that supports short options
88 * and doesn't do any permutations.
89 *
90 * This function should not perform any actions itself, but set
91 * taggit_mode accordingly and let main()'s main loop handle everything.
92 *
93 * @param argc number of entries in *argv[]
94 * @param argv[] list of arguments at startup.
95 *
96 * @return void
97 * @sideeffects Exists using EXIT_SUCCESS or EXIT_FAILURE when appropriate.
98 */
d177e07 @ft Changing scope of a few functions
authored
99 static void
6b8ed1d @ft Initial commit
authored
100 parse_options(int argc, const char *argv[])
101 {
9c8b369 @ft Clean up option handling a little
authored
102 int opt;
47f01fa @ft Added -t handling code
authored
103 struct t_tag tag;
6b8ed1d @ft Initial commit
authored
104
01a9ab2 @ft Read map code
authored
105 while ((opt = bsd_getopt(argc, argv, "hLlmsR:t:v")) != -1) {
6b8ed1d @ft Initial commit
authored
106 switch (opt) {
107 case 'L':
0be4aa5 @ft Remove the giant string macros from taggit.h
authored
108 taggit_licence();
9c8b369 @ft Clean up option handling a little
authored
109 exit(EXIT_SUCCESS);
6b8ed1d @ft Initial commit
authored
110 case 'l':
111 check_mode(taggit_mode);
112 taggit_mode = TAGGIT_LIST_HUMAN;
113 break;
114 case 'm':
115 check_mode(taggit_mode);
116 taggit_mode = TAGGIT_LIST_MACHINE;
117 break;
9983840 @ft Add -s option to list supported tags
authored
118 case 's':
119 printf("Supported tags:\n");
120 list_tags();
9c8b369 @ft Clean up option handling a little
authored
121 exit(EXIT_SUCCESS);
01a9ab2 @ft Read map code
authored
122 case 'R':
123 setup_readmap(optarg);
124 break;
6b8ed1d @ft Initial commit
authored
125 case 't':
47f01fa @ft Added -t handling code
authored
126 check_mode_tag(taggit_mode);
6b8ed1d @ft Initial commit
authored
127 taggit_mode = TAGGIT_TAG;
47f01fa @ft Added -t handling code
authored
128 tag = next_tag(optarg);
129 if (tag.type == TAG_INVALID) {
130 fprintf(stderr, "Invalid tag name: \"%s\"\n", tag.name);
131 exit(EXIT_FAILURE);
132 }
133 add_tag(&tag);
134 #ifdef TAGGIT_DEBUG
135 printf("[%s] {%s} <%d>\n", tag.name, tag.value, tag.type);
136 #endif
6b8ed1d @ft Initial commit
authored
137 break;
138
139 case 'v':
0be4aa5 @ft Remove the giant string macros from taggit.h
authored
140 taggit_version();
9c8b369 @ft Clean up option handling a little
authored
141 exit(EXIT_SUCCESS);
6b8ed1d @ft Initial commit
authored
142 case 'h':
0be4aa5 @ft Remove the giant string macros from taggit.h
authored
143 taggit_usage();
9c8b369 @ft Clean up option handling a little
authored
144 exit(EXIT_SUCCESS);
6b8ed1d @ft Initial commit
authored
145 default:
0be4aa5 @ft Remove the giant string macros from taggit.h
authored
146 taggit_usage();
9c8b369 @ft Clean up option handling a little
authored
147 exit(EXIT_FAILURE);
6b8ed1d @ft Initial commit
authored
148 }
149 }
150
151 return;
152 }
153
25607c6 @ft Adding source code documentation to taggit.c
authored
154 /**
155 * taggit: command line utility for listing and modifying meta
156 * information in audio files
157 *
158 * Interfacing KDE's taglib:
159 * <http://developer.kde.org/~wheeler/taglib.html>
160 *
161 * @param argc number of entries in *argv[]
162 * @param argv[] list of arguments at startup.
163 *
164 * @return EXIT_SUCCESS on normal execution;
165 * EXIT_FAILURE upon failure.
166 * @sideeffects none
167 */
6b8ed1d @ft Initial commit
authored
168 int
169 main(int argc, const char *argv[])
170 {
130f551 @ft Be cleaner about boolean values
authored
171 int i;
172 boolean first;
6b8ed1d @ft Initial commit
authored
173
174 if (argc < 2) {
0be4aa5 @ft Remove the giant string macros from taggit.h
authored
175 taggit_usage();
6b8ed1d @ft Initial commit
authored
176 return EXIT_FAILURE;
177 }
178
179 parse_options(argc, argv);
180
181 if (optind == argc) {
0be4aa5 @ft Remove the giant string macros from taggit.h
authored
182 taggit_usage();
6b8ed1d @ft Initial commit
authored
183 return EXIT_FAILURE;
184 }
185
47f01fa @ft Added -t handling code
authored
186 #ifdef TAGGIT_DEBUG
187 if (taggit_mode == TAGGIT_TAG) {
188 struct taglist *ptr;
189
190 ptr = tags_head;
191 while (ptr != NULL) {
192 if (ptr->type == TAG_INT) {
193 printf("[%s|%u] [%u]\n", ptr->name, ptr->id, ptr->integer);
194 } else if (ptr->type == TAG_NUKE) {
195 printf("[%s|%u] <-NUKE->\n", ptr->name, ptr->id);
196 } else {
197 printf("[%s|%u] [%s]\n", ptr->name, ptr->id, ptr->string);
198 }
199 ptr = ptr->next;
200 }
201 }
202 #endif
130f551 @ft Be cleaner about boolean values
authored
203 first = true;
6b8ed1d @ft Initial commit
authored
204 for (i = optind; i < argc; ++i) {
205 switch (taggit_mode) {
206 case TAGGIT_LIST_HUMAN:
1837d0f @ft Add tag value listing code (both -m and -l)
authored
207 if (!first)
208 printf("\n");
209 else
130f551 @ft Be cleaner about boolean values
authored
210 first = false;
6b8ed1d @ft Initial commit
authored
211 taggit_list_human(argv[i]);
212 break;
213 case TAGGIT_LIST_MACHINE:
1837d0f @ft Add tag value listing code (both -m and -l)
authored
214 if (!first)
215 printf("%c", ASCII_EOT);
216 else
130f551 @ft Be cleaner about boolean values
authored
217 first = false;
6b8ed1d @ft Initial commit
authored
218 taggit_list_machine(argv[i]);
219 break;
220 case TAGGIT_TAG:
221 taggit_tag(argv[i]);
222 break;
223 default:
224 printf("Please use one action option (-m, -l or -t).\n");
225 return EXIT_FAILURE;
226 }
227 }
228
229 return EXIT_SUCCESS;
230 }
Something went wrong with that request. Please try again.