1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
|
Author: Nathan Rutman <nathan@gordian.com>
Last-Update: 2025-11-02
Description: CDDB support
This patch completes and enables the CDDB support in libworkman, so libworkman
will now lookup unknown CD's in CDDB, store them in the local database, and
AScd will show that info.
--- /dev/null
+++ b/README_CDDB
@@ -0,0 +1,24 @@
+AScd CDDB patch
+N. Rutman 10/2/2001
+nathan@gordian.com
+
+
+AScd 0.13.2
+http://worldserver.oleane.com/rsn/ascd-download.html
+
+The modification to ascd/faktory.c fixes a segfault bug. The other mods
+finish and enable the CDDB support in libworkman, so libworkman will now
+lookup unknown CD's in CDDB, store them in the local database, and AScd
+will show that info. (Compiling libworkman with -DDEBUG will print the
+CDDB query and response.)
+
+To use CDDB support, add a ~/.workmanrc file with something like:
+cddbprotocol cddbp
+cddbmailadress nathan@nowhere.com
+cddbserver freedb.freedb.org
+
+Full CDDB options in ~/.workmanrc are:
+cddbprotocol [cddbp|http]
+cddbserver [server(:port)] default port is 888 for cddbp, 80 for http
+cddbpathtocgi [cgipath] default is /~cddb/cddb.cgi
+cddbproxy [proxyserver(:port)]
--- /dev/null
+++ b/config.h
@@ -0,0 +1,1 @@
+#define VERSION "0.13.2"
--- a/libworkman/cddb.c
+++ b/libworkman/cddb.c
@@ -33,7 +33,7 @@
#include <stdio.h>
#include <stdlib.h>
-#include <strings.h>
+#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -49,6 +49,12 @@
#include "include/wm_cdinfo.h"
#include "include/wm_helpers.h"
+#ifdef DEBUG
+#define Trace(x) fprintf x
+#else
+#define Trace(x)
+#endif
+
/*
* This is for identifying WorkMan at CDDB servers
*/
@@ -57,41 +63,9 @@
struct wm_cddb cddb;
-int cur_cddb_protocol;
-char *cur_cddb_server;
-char *cur_cddb_mail_adress;
-char *cur_cddb_path_to_cgi;
-char *cur_cddb_proxy_server;
-
int Socket;
FILE *Connection;
-/*
- *
- */
-void
-cddb_cur2struct(void)
-{
- cddb.protocol = cur_cddb_protocol;
- strcpy(cddb.cddb_server, cur_cddb_server);
- strcpy(cddb.mail_adress, cur_cddb_mail_adress);
- strcpy(cddb.path_to_cgi, cur_cddb_path_to_cgi);
- strcpy(cddb.proxy_server, cur_cddb_proxy_server);
-} /* cddb_cur2struct() */
-
-/*
- *
- */
-void
-cddb_struct2cur(void)
-{
- cur_cddb_protocol = cddb.protocol;
- cur_cddb_server = (cddb.cddb_server);
- cur_cddb_mail_adress = (cddb.mail_adress);
- cur_cddb_path_to_cgi = (cddb.path_to_cgi);
- cur_cddb_proxy_server = (cddb.proxy_server);
-} /* cddb_struct2cur() */
-
/*
* Subroutine from cddb_discid
@@ -146,23 +120,17 @@
return ((n % 0xff) << 24 | t << 8 | thiscd.ntracks);
} /* cddb_discid() */
-/*
- * Split a string into two components according to the first occurance of
- * the delimiter.
- */
-char *
-string_split(char *line, char delim)
+
+/* Split string at delim */
+char *string_split(char *line, char delim)
{
char *p1;
- for (p1=line;*p1;p1++)
- {
- if(*p1 == delim)
- {
+ p1 = strchr(line, (int)delim);
+ if (p1) {
*p1 = 0;
return ++p1;
}
- }
return (NULL);
} /* string_split() */
@@ -176,8 +144,18 @@
char mail[84],*host;
strcpy(mail,cddb.mail_adress);
- host=string_split(mail,'@');
-
+ host = string_split(mail,'@');
+ if (!host) {
+ int len;
+ if (getlogin())
+ strncpy(mail, getlogin(), 83);
+ else
+ strcpy(mail, "nobody");
+ len = strlen(mail);
+ host = mail + len + 1;
+ gethostname(host, 83-len);
+ Trace((stderr, "default email %s@%s\n", mail, host));
+ }
sprintf(line,"%shello%c%s%c%s%c%s%c%s",
delim == ' ' ? "cddb " : "&",
delim == ' ' ? ' ' : '=',
@@ -193,26 +171,40 @@
int
connect_open(void)
{
- char *host;
+ char host[200], *prt;
struct hostent *hp;
struct sockaddr_in soc_in;
- int port;
+ int port = 0;
+
+ Trace((stderr, "opening connection to cddb server\n"));
+
+ /* in case of failure */
+ Connection = NULL;
if(cddb.protocol == 3) /* http proxy */
- host = strdup(cddb.proxy_server);
+ strcpy(host, cddb.proxy_server);
else
- host = strdup(cddb.cddb_server);
- /*
- * t=string_split(host,':');
- */
- port=atoi(string_split(host,':'));
+ strcpy(host, cddb.cddb_server);
+
+ Trace((stderr, "host %s\n", host));
+
+ prt = string_split(host,':');
+ if (prt)
+ port = atoi(prt);
+
+ /* Defaults */
if(!port)
- port=8880;
+ if (cddb.protocol == 1)
+ port = 888;
+ else
+ port = 80;
+ if (cddb.path_to_cgi[0] == 0)
+ sprintf(cddb.path_to_cgi, "/~cddb/cddb.cgi");
- printf("%s:%d\n",host,port);
- hp =gethostbyname(host);
+ Trace((stderr, "%s : %d\n",host,port));
- if (hp == NULL)
+ hp = gethostbyname(host);
+ if (!hp)
{
static struct hostent def;
static struct in_addr defaddr;
@@ -220,10 +212,12 @@
static char namebuf[128];
int inet_addr();
+ Trace((stderr, "gethostbyname failed: %s\n", host));
+
defaddr.s_addr = inet_addr(host);
if (defaddr.s_addr == -1)
{
- printf("unknown host: %s\n", host);
+ Trace((stderr, "unknown host: %s\n", host));
return (-1);
}
strcpy(namebuf, host);
@@ -234,6 +228,7 @@
def.h_aliases = 0;
hp = &def;
}
+
soc_in.sin_family = hp->h_addrtype;
bcopy(hp->h_addr, (char *)&soc_in.sin_addr, hp->h_length);
soc_in.sin_port = htons(port);
@@ -274,6 +269,8 @@
{
char c;
+ /* FIXME timeout, return empty string (not null) */
+
while ((c = getc(Connection)) != '\n')
{
*line = c;
@@ -298,15 +295,15 @@
{
connect_getline(tempbuf);
- t=string_split(tempbuf,'=');
- if(t != NULL)
+ t = string_split(tempbuf,'=');
+ if (t != NULL)
{
type=tempbuf[0];
if(strncmp("TITLE",tempbuf+1,5))
continue;
- if('D' == type)
+ if(type == 'D')
{
/*
* Annahme: "Interpret / Titel" ist falsch.
@@ -326,18 +323,11 @@
}
strcpy(cd->artist,t);
}
-
- if('T' == type)
+ else if(type == 'T')
{
trknr=atoi(tempbuf+6);
- /*
- * printf("Track %d:%s\n",trknr,t);
- */
wm_strmcpy(&cd->trk[trknr].songname,t);
}
- /*
- * fprintf(stderr, "%s %s\n",tempbuf,t);
- */
}
}
} /* connect_read_entry() */
@@ -372,22 +362,22 @@
char tempbuf[2000];
write(Socket, "GET ", 4);
- printf("GET ");
+ Trace((stderr, "GET "));
if(cddb.protocol == 3)
{
write(Socket, "http://", 7);
write(Socket, cddb.cddb_server, strlen(cddb.cddb_server));
- printf("http://%s",cddb.cddb_server);
+ Trace((stderr, "http://%s",cddb.cddb_server));
}
write(Socket, cddb.path_to_cgi, strlen(cddb.path_to_cgi));
write(Socket, "?cmd=" ,5);
write(Socket, line, strlen(line));
- printf("%s?cmd=%s",cddb.path_to_cgi,line);
+ Trace((stderr, "%s?cmd=%s",cddb.path_to_cgi,line));
string_makehello(tempbuf,'+');
write(Socket, tempbuf, strlen(tempbuf));
- printf("%s",tempbuf);
+ Trace((stderr, "%s",tempbuf));
write(Socket, "&proto=1 HTTP/1.0\n\n", 19);
- printf("&proto=1 HTTP/1.0\n");
+ Trace((stderr, "&proto=1 HTTP/1.0\n"));
do
connect_getline(tempbuf);
while(strcmp(tempbuf,""));
@@ -406,120 +396,111 @@
/*
* The main routine called from the ui
+ * returns 0 on success, -1 on err
*/
-void
+int
cddb_request(void)
{
int i;
char tempbuf[2000];
extern int cur_ntracks;
-
- int status;
+ int status, rv = 0;
char category[20];
unsigned int id;
- strcpy(cddb.cddb_server,"localhost:888");
- strcpy(cddb.mail_adress,"svolli@bigfoot.com");
- /*
- * cddb.protocol = 1;
- */
wipe_cdinfo();
switch(cddb.protocol)
{
case 1: /* cddbp */
- printf("USING CDDBP\n");
- printf("open\n");
- connect_open();
+ Trace((stderr, "USING CDDBP\n"));
+ Trace((stderr, "open\n"));
+ if (connect_open()) {
+ Trace((stderr, "connect failed.\n"));
+ return -1;
+ }
connect_getline(tempbuf);
- printf("[%s]\n",tempbuf);
- /*
- * if(atoi(tempbuf) == 201) return;
- */
-
- /*
- * strcpy(tempbuf,"cddb hello svolli bigfoot.com Eierkratzer eins");
- */
+ Trace((stderr, "[%s]\n",tempbuf));
+
string_makehello(tempbuf,' ');
- fprintf(stderr, "%s\n", tempbuf);
+ Trace((stderr, "%s\n", tempbuf));
cddbp_send(tempbuf);
connect_getline(tempbuf);
- printf("[%s]\n",tempbuf);
+ Trace((stderr, "[%s]\n",tempbuf));
- printf("query\n");
+ Trace((stderr, "query\n"));
sprintf(tempbuf, "cddb query %08x %d",thiscd.cddbid,thiscd.ntracks);
for (i = 0; i < cur_ntracks; i++)
if (thiscd.trk[i].section < 2)
sprintf(tempbuf + strlen(tempbuf), " %d",
thiscd.trk[i].start);
sprintf(tempbuf + strlen(tempbuf), " %d\n", thiscd.length);
- printf(">%s<\n",tempbuf);
+ Trace((stderr, ">%s<\n",tempbuf));
cddbp_send(tempbuf);
connect_getline(tempbuf);
- printf("[%s]\n",tempbuf);
+ Trace((stderr, "[%s]\n",tempbuf));
status=atoi(tempbuf);
- /*
- * fprintf(stderr, "status:%d\n",status);
- * fprintf(stderr,"category:%s\n",category);
- * fprintf(stderr,"id:%s\n",id);
- */
+ Trace((stderr, "status:%d\n",status));
+
if(status == 200) /* Exact match */
{
sscanf(tempbuf,"%d %s %08x",&status,category,&id);
cddbp_read(category,id);
connect_read_entry();
}
-
- if(status == 211) /* Unexact match, multiple possible
+ else if(status == 211) /* Unexact match, multiple possible
* Hack: always use first. */
{
connect_getline(tempbuf);
+ Trace((stderr, "[%s]\n",tempbuf));
sscanf(tempbuf,"%s %08x",category,&id);
- while(strcmp(tempbuf,"."))
+ while(strcmp(tempbuf,".")) {
connect_getline(tempbuf);
+ Trace((stderr, "ignoring [%s]\n",tempbuf));
+ }
cddbp_read(category,id);
connect_read_entry();
}
-
+ else
+ rv = -1;
cddbp_send("quit");
connect_close();
- printf("close\n");
- break;
+ Trace((stderr, "close\n"));
+ return rv;
case 2: /* http */
case 3: /* http proxy */
- printf("USING HTTP%s\n",
- (cddb.protocol == 3) ? " WITH PROXY" : "");
- printf("query\n");
+ Trace((stderr, "USING HTTP%s\n",
+ (cddb.protocol == 3) ? " WITH PROXY" : ""));
+ Trace((stderr, "query\n"));
sprintf(tempbuf, "cddb+query+%08x+%d",thiscd.cddbid,thiscd.ntracks);
for (i = 0; i < cur_ntracks; i++)
if (thiscd.trk[i].section < 2)
sprintf(tempbuf + strlen(tempbuf), "+%d",
thiscd.trk[i].start);
sprintf(tempbuf + strlen(tempbuf), "+%d", thiscd.length);
- printf(">%s<\n",tempbuf);
- connect_open();
+ Trace((stderr, ">%s<\n",tempbuf));
+ if (connect_open()) {
+ Trace((stderr, "connect failed.\n"));
+ return -1;
+ }
http_send(tempbuf);
connect_getline(tempbuf);
- printf("[%s]\n",tempbuf);
+ Trace((stderr, "[%s]\n",tempbuf));
status=atoi(tempbuf);
- /*
- * fprintf(stderr, "status:%d\n",status);
- * fprintf(stderr, "category:%s\n",category);
- * fprintf(stderr, "id:%s\n",id);
- */
-
if(status == 200) /* Exact match */
{
connect_close();
- connect_open();
+ if (connect_open()) {
+ Trace((stderr, "connect failed.\n"));
+ return -1;
+ }
sscanf(tempbuf,"%d %s %08x",&status,category,&id);
http_read(category,id);
connect_read_entry();
}
-
- if(status == 211) /* Unexact match, multiple possible
+ else if(status == 211) /* Unexact match, multiple possible
* Hack: always use first. */
{
connect_getline(tempbuf);
@@ -527,15 +508,24 @@
while(strcmp(tempbuf,"."))
connect_getline(tempbuf);
connect_close();
- connect_open();
+ if (connect_open()) {
+ Trace((stderr, "connect failed.\n"));
+ return -1;
+ }
http_read(category,id);
connect_read_entry();
}
+ else
+ rv = -1;
/* moved close above break */
connect_close();
- break;
+ return rv;
default: /* off */
break;
}
+ return -1;
} /* cddb_request() */
+
+
+
--- a/libworkman/database.c
+++ b/libworkman/database.c
@@ -33,6 +33,8 @@
#define DBFILE "/.workmandb"
#define FUZZFRAMES 75
+#define ASYNC_CDDB 1
+
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
@@ -44,6 +46,10 @@
#include <sys/param.h>
#include <sys/time.h>
#include <sys/stat.h>
+#ifdef ASYNC_CDDB
+#include <signal.h>
+#include <sys/wait.h>
+#endif
#include "include/wm_config.h"
#include "include/wm_helpers.h"
#include "include/wm_struct.h"
@@ -80,6 +86,12 @@
int mark_a = 0;
int mark_b = 0;
+static int _save(int saverc);
+static int check_in_cddb();
+static int wakeup_from_cddb();
+static int cddb_lock = 0;
+
+extern void set_abtimer(int a, int b);
/*
*
@@ -1011,9 +1023,7 @@
if (searching > 1) /* A near match has been found. Good enough. */
searching = 0;
- cddb_struct2cur();
return (! searching);
-
} /* search_db() */
/*
@@ -1164,6 +1174,29 @@
free(trklist);
+ /* If we didn't find it, try CDDB */
+#ifndef ASYNC_CDDB
+ /* Syncronous - wait for query to return */
+ if (cddb.protocol && !found_in_rc && !found_in_db)
+ check_in_cddb();
+#else
+ /* Asyncronous */
+ if (cddb.protocol && !found_in_rc && !found_in_db && !cddb_lock) {
+ /* Make sure we only have one request out to CDDB at a time.
+ In case server is down or something, we don't want to keep
+ forking children. */
+ cddb_lock = 1;
+ signal(SIGCHLD, (sig_t)wakeup_from_cddb);
+ if (!fork()) {
+ /* Child */
+ int rv;
+ cddb_lock = 1; /* extra safety, can't hurt */
+ rv = check_in_cddb();
+ exit(rv);
+ }
+ }
+#endif
+
if (cur_playnew == -1)
cur_playnew = 0;
@@ -1177,6 +1210,41 @@
#endif
} /* load() */
+
+/* Lookup cddb data and save it to workman database */
+static int check_in_cddb()
+{
+ if (!cddb_request()) {
+ /* Found it, let's save it so we don't have to look it
+ up again. */
+#ifdef DEBUG
+ fprintf(stderr, "\nFound in CDDB:");
+ fprintf(stderr, "%s", print_cdinfo(cd, 0));
+#endif
+ /* Save in db only, not rc, because we haven't put any
+ customizations in, and I don't want to clutter the
+ rc needlessly. */
+ _save(0);
+ return 0;
+ }
+ return -1;
+}
+
+/* Called when the fork above finishes. */
+static int wakeup_from_cddb()
+{
+ int status;
+ cddb_lock = 0;
+ /* Child had new data loaded, but we don't share memory, so we have to
+ reload here for updated data */
+ /* Only reload if child was successful, otherwise we keep trying */
+ wait(&status);
+ if (WIFEXITED(status) && (!WEXITSTATUS(status))) {
+ load();
+ }
+}
+
+
/*
* Load program settings from the rcfile.
*/
@@ -1221,6 +1289,8 @@
long curpos;
int globesize, hit_cdent = 0, c = 0;
+ printf("saving globals\n");
+
if (otherrc)
wm_strmcpy(&globes, otherrc);
@@ -1252,6 +1322,7 @@
if(cddb.cddb_server[0])
{
+ printf("writing cddb server data\n");
sprintf(temp,"cddbserver %s\n",
cddb.cddb_server);
wm_strmcat(&globes, temp);
@@ -1260,14 +1331,14 @@
if(cddb.path_to_cgi[0])
{
sprintf(temp,"cddbpathtocgi %s\n",
- cddb.mail_adress);
+ cddb.path_to_cgi);
wm_strmcat(&globes, temp);
}
if(cddb.proxy_server[0])
{
sprintf(temp,"cddbproxy %s\n",
- cddb.mail_adress);
+ cddb.proxy_server);
wm_strmcat(&globes, temp);
}
}
@@ -1409,6 +1480,8 @@
locked = 1;
buf = print_cdinfo(cd, pref);
+ if (!buf)
+ fprintf(stderr, "save_entry: print_cdinfo failed\n");
len = strlen(buf); /* doesn't return if there's an error */
rcpos = -1;
@@ -1500,13 +1573,17 @@
* list, unless the entry came from another database file) and to the
* personal prefs file.
*/
-int
-save( void )
+int save(void)
+{
+ return _save(1);
+}
+
+static int _save(int saverc)
{
if( wm_db_save_disabled == FALSE )
{
- if (save_entry(rcfile, 1))
+ if (saverc && save_entry(rcfile, 1))
return (0);
if (cd->whichdb == NULL || access(cd->whichdb, W_OK))
@@ -1519,4 +1596,4 @@
} else {
return( WM_DB_SAVE_DISABLED );
}
-} /* save() */
+} /* _save() */
--- a/libworkman/include/wm_cdda.h
+++ b/libworkman/include/wm_cdda.h
@@ -91,7 +91,6 @@
* if there is no support.
*/
#ifdef linux
-# include <bytesex.h>
# include <endian.h>
/*
* XXX could this be a problem? The results are only 0 and 1 because
--- a/libworkman/wm_helpers.c
+++ b/libworkman/wm_helpers.c
@@ -29,6 +29,7 @@
static char wm_helpers_id[] = "$Id: wm_helpers.c,v 1.7 1999/03/07 08:36:41 dirk Exp $";
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>
--- /dev/null
+++ b/patchlevel.h
@@ -0,0 +1,7 @@
+/*
+ AScd/libworkman CDDB patch
+ Nathan Rutman
+ 10/2/2001
+*/
+
+#define CDDB_PATCHLEVEL 0.13.2_1
--- a/libworkman/include/wm_cddb.h
+++ b/libworkman/include/wm_cddb.h
@@ -37,6 +37,6 @@
void cddb_select();
void connect_cddb();
void update_cddbserver();
-void cddb_request();
+int cddb_request();
#endif /* WM_CDDB_H */
|