6 char *server = "freedb.freedb.org";
9 #define DPRINT if(debug)fprint
13 typedef struct Track Track;
23 typedef struct Toc Toc;
38 sysfatal("can't malloc: %r");
48 t = emalloc(strlen(s)+1);
58 print("title %s\n", t->title);
59 for(i=0; i<t->ntrack; i++){
64 s = (n - t->track[i].n)/75;
65 print("%d\t%s\t%d:%2.2d\n", i+1, t->track[i].title, s/60, s%60);
68 print("%d\t%s\n", i+1, t->track[i].title);
72 print("Total time: %d:%2.2d\n", s/60, s%60);
77 append(char *a, char *b)
81 c = emalloc(strlen(a)+strlen(b)+1);
98 fd = dial(netmkaddr(server, "tcp", "888"), 0, 0, 0);
100 fprint(2, "%s: %s: cannot dial: %r\n", argv0, server);
103 Binit(&bin, fd, OREAD);
105 if((p=Brdline(&bin, '\n')) == nil || atoi(p)/100 != 2) {
109 fprint(2, "%s: error talking to cddb server %s\n",
112 p[Blinelen(&bin)-1] = 0;
113 fprint(2, "%s: server says: %s\n", argv0, p);
118 fprint(fd, "cddb hello gre plan9 9cd 1.0\r\n");
119 if((p = Brdline(&bin, '\n')) == nil || atoi(p)/100 != 2)
123 * Protocol level 6 is the same as level 5 except that
124 * the character set is now UTF-8 instead of ISO-8859-1.
126 fprint(fd, "proto 6\r\n");
127 DPRINT(2, "proto 6\r\n");
128 if((p = Brdline(&bin, '\n')) == nil || atoi(p)/100 != 2)
130 p[Blinelen(&bin)-1] = 0;
131 DPRINT(2, "cddb: %s\n", p);
133 fprint(fd, "cddb query %8.8lux %d", t->diskid, t->ntrack);
134 DPRINT(2, "cddb query %8.8lux %d", t->diskid, t->ntrack);
135 for(i=0; i<t->ntrack; i++) {
136 fprint(fd, " %d", t->track[i].n);
137 DPRINT(2, " %d", t->track[i].n);
139 fprint(fd, " %d\r\n", t->track[t->ntrack].n);
140 DPRINT(2, " %d\r\n", t->track[t->ntrack].n);
142 if((p = Brdline(&bin, '\n')) == nil || atoi(p)/100 != 2)
144 p[Blinelen(&bin)-1] = 0;
145 DPRINT(2, "cddb: %s\n", p);
146 nf = tokenize(p, f, nelem(f));
151 case 200: /* exact match */
157 case 210: /* exact matches */
158 case 211: /* close matches */
159 if((p = Brdline(&bin, '\n')) == nil)
161 if(p[0] == '.') /* no close matches? */
163 p[Blinelen(&bin)-1] = '\0';
165 /* accept first match */
166 nf = tokenize(p, f, nelem(f));
172 /* snarf rest of buffer */
174 if((p = Brdline(&bin, '\n')) == nil)
176 p[Blinelen(&bin)-1] = '\0';
177 DPRINT(2, "cddb: %s\n", p);
180 case 202: /* no match */
186 for(i=0; i<t->ntrack; i++)
187 t->track[i].title = "";
189 /* fetch results for this cd */
190 fprint(fd, "cddb read %s %s\r\n", categ, id);
192 if((p = Brdline(&bin, '\n')) == nil)
194 q = p+Blinelen(&bin)-1;
197 DPRINT(2, "cddb %s\n", p);
198 if(strncmp(p, "DTITLE=", 7) == 0)
199 t->title = append(t->title, p+7);
200 else if(strncmp(p, "TTITLE", 6) == 0 && isdigit(p[6])) {
209 t->track[i].title = append(t->track[i].title, estrdup(p));
214 fprint(fd, "quit\r\n");
224 fprint(2, "usage: aux/cddb [-DTt] [-s server] query diskid n ...\n");
229 main(int argc, char **argv)
239 server = EARGF(usage());
249 if(argc < 3 || strcmp(argv[0], "query") != 0)
252 toc.diskid = strtoul(argv[1], 0, 16);
253 toc.ntrack = atoi(argv[2]);
254 if(argc != 3+toc.ntrack+1)
255 sysfatal("argument count does not match given ntrack");
257 for(i=0; i<=toc.ntrack; i++)
258 toc.track[i].n = atoi(argv[3+i]);
260 if(cddbfilltoc(&toc) < 0)