[hackers] [last] Minor changes, what I don't know. || Kris Maglione

From: <hg_AT_suckless.org>
Date: Mon, 24 Aug 2009 17:56:31 +0000 (UTC)

changeset: 2:a086e162583a
tag: tip
user: Kris Maglione <jg_AT_suckless.org>
date: Mon Aug 24 13:56:37 2009 -0400
files: http.c last.c pass player.c posix.c util.c
description:
Minor changes, what I don't know.

diff -r 4931463a5050 -r a086e162583a http.c
--- a/http.c Thu Aug 09 15:14:34 2007 -0400
+++ b/http.c Mon Aug 24 13:56:37 2009 -0400
@@ -66,10 +66,11 @@
                 for(p=s; (c = *p); p++) {
                         if(c & 0x80 || !isprint(c) || isspace(c))
                                 break;
+ if(c == '%' && !(f->flags & FmtComma))
+ break;
                         switch(c) {
                         default:
                                 continue;
- case '%':
                         case '<':
                         case '>':
                         case '{':
@@ -85,6 +86,7 @@
                         case '+':
                                 break;
                         }
+ break;
                 }
                 if(p != s)
                         ret += fmtprint(f, "%.*s", p-s, s);
@@ -92,7 +94,7 @@
                         if(c == ' ')
                                 ret += fmtstrcpy(f, "+");
                         else
- ret += fmtprint(f, "%%%02X", c);
+ ret += fmtprint(f, "%%%02uX", (uchar)c);
                         p++;
                 }
         }
@@ -122,11 +124,12 @@
                         case '\\':
                                 break;
                         }
+ break;
                 }
                 if(p != s)
                         ret += fmtprint(f, "%.*s", p-s, s);
                 if(c) {
- ret += fmtprint(f, "%%%02X", c);
+ ret += fmtprint(f, "%%%02uX", (uchar)c);
                         p++;
                 }
         }
diff -r 4931463a5050 -r a086e162583a last.c
--- a/last.c Thu Aug 09 15:14:34 2007 -0400
+++ b/last.c Mon Aug 24 13:56:37 2009 -0400
@@ -1,10 +1,13 @@
 #define EXTERN
+#define _GNU_SOURCE
 #include <u.h>
 #include <libc.h>
 #include <auth.h>
 #include <bio.h>
 #include <mp.h>
 #include <libsec.h>
+#include <signal.h>
+#include <string.h>
 #include <thread.h>
 #include "last.h"
 
@@ -52,8 +55,9 @@
         Rartist, "Conlon Nancarrow",
         Rartist, "Iannis Xenakis",
         Rartist, "Olivier Messiaen",
+ Rartist, "Mauricio Kagel",
         Ralbum, "Music Box",
- 0, 0,
+ 0,
 };
 
 char*
@@ -66,9 +70,15 @@
 }
 
 void
-exitsall(char *err) {
- if(err)
+exitsall(char *err, ...) {
+ va_list ap;
+
+ if(err) {
+ va_start(ap, err);
+ err = vsmprint(err, ap);
+ va_end(ap);
                 print("threadexitsall(%s)\n", err);
+ }
         threadexitsall(err);
 }
 
@@ -125,7 +135,10 @@
 
         if(debug['a'] && !debug['h'])
                 print("GET %q %s\n", host, uri);
- b = httpget(host, uri);
+ do {
+ b = httpget(host, uri);
+ rerrstr(buf, sizeof buf);
+ }while(b == nil && strcasestr(buf, "interrupted"));
         free(uri);
         if(b == nil) {
                 resp[Rresponse] = smprint("get fails: %r");
@@ -177,6 +190,24 @@
         return i;
 }
 
+void
+erpc(const char *cmd, const char *fmt, ...) {
+ VFmt v;
+
+ v.fmt = query(nil, fmt);
+ va_start(v.args, fmt);
+ if(get("%s/%s.php", "session=%h %V", basepath, cmd, session, &v))
+ sysfatal("rpc %s: %r\n", cmd);
+ va_end(v.args);
+ free(v.fmt);
+}
+
+int
+skip(char *cmd) {
+ skipping = 1;
+ return rpc("control", "command=%h", cmd);
+}
+
 static void
 printresp(void) {
         char *s;
@@ -203,8 +234,7 @@
         do {
                 if(n > 50)
                         exitsall("meta");
- if(rpc("np", ""))
- exitsall("rpc np");
+ erpc("np", "");
         }while(resp[Rstreaming] == nil || strcmp(resp[Rstreaming], "true"));
 
         s = resp[Rtrackduration];
@@ -215,7 +245,7 @@
 
         print("\n");
         print("Station: %R\n", Rstation);
- print("Atrist: %R <%R>\n", Rartist, Rartist_url);
+ print("Artist: %R <%R>\n", Rartist, Rartist_url);
         print("Album: %R\n", Ralbum);
         print("Track: %R\n", Rtrack);
         print("Duration: %d:%02d\n", sec/60, sec%60);
@@ -223,14 +253,13 @@
         print("\n");
         print("> ");
 
- label("Playing \"%R\" by %R", Rtrack, Rartist);
+ label("Playing “%R” by %R", Rtrack, Rartist);
 
         for(a=junk; a->str; a++)
                 if(resp[a->field] && cistrstr(resp[a->field], a->str)) {
                         print("Junk %s; banning.\n", respnam[a->field]);
- rpc("control", "command=ban");
+ skip("ban");
                         printresp();
- skipping = 1;
                         break;
                 }
 }
@@ -246,7 +275,7 @@
         if(up == nil)
                 sysfatal("no username/password: %r");
 
- md5(up->passwd, strlen(up->passwd), digest, nil);
+ md5((uchar*)up->passwd, strlen(up->passwd), digest, nil);
         for(i=0; i < MD5dlen; i++)
                 sprint(buf+2*i, "%02x", digest[i]);;
 
@@ -274,6 +303,9 @@
         fmtinstall('R', Rconv);
         quotefmtinstall();
 
+ signal(SIGCHLD, SIG_IGN);
+ notifyoff("sys: child");
+
         label("Wait...");
 
         httpinit();
@@ -297,7 +329,11 @@
 
         label("Stopped.");
         Binit(&in, 0, OREAD);
- while(print("> "), s = Brdline(&in, '\n')) {
+ while(print("> "), Bgetc(&in) != Beof) {
+ Bungetc(&in);
+ s = Brdline(&in, '\n');
+ if(s == nil)
+ continue;
                 s[BLINELEN(&in)-1] = '\0';
                 q = tok(&s);
                 if(!strcmp(q, "start")) {
@@ -306,18 +342,28 @@
                 if(!strcmp(q, "stop")) {
                         endstream();
                 }else
+ if(!strcmp(q, "info")) {
+ getmeta();
+ }else
                 if(!strcmp(q, "skip") || !strcmp(q, "ban")) {
- skipping = 1;
                         label("Skipping...");
- rpc("control", "command=%h", q);
+ skip(q);
                         printresp();
                 }else
                 if(!strcmp(q, "love")) {
                         rpc("control", "command=love");
                         printresp();
                 }else
+ if(!strcmp(q, "artist")) {
+ rpc("adjust", "url=%h%h", "lastfm://artist/", s);
+ printresp();
+ }else
+ if(!strcmp(q, "tag")) {
+ rpc("adjust", "url=%h%h", "lastfm://globaltags/", s);
+ printresp();
+ }else
                 if(!strcmp(q, "station")) {
- rpc("adjust", "url=%h", s);
+ rpc("adjust", "url=%,h", s);
                         printresp();
                 }else
                 if(!strcmp(q, "time")) {
diff -r 4931463a5050 -r a086e162583a pass
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pass Mon Aug 24 13:56:37 2009 -0400
@@ -0,0 +1,16 @@
+#!/bin/rc
+. 9.rc
+
+echo Enter your last.fm login credentials.
+echo -n Username: ''
+username=`{read}
+echo -n Password: ''
+stty -echo
+pass=`{read}
+stty echo
+
+9p read factotum/ctl >/dev/null >[2=1] || 9 factotum &
+
+echo 'delkey dom=last.fm' | 9p write factotum/ctl >[2]/dev/null
+echo 'key user='$"username' !password='$"pass' dom=last.fm proto=pass' | 9p write factotum/ctl >/dev/null >[2=1]
+
diff -r 4931463a5050 -r a086e162583a player.c
--- a/player.c Thu Aug 09 15:14:34 2007 -0400
+++ b/player.c Mon Aug 24 13:56:37 2009 -0400
@@ -25,7 +25,9 @@
                 fd[0] = dup(fdin, -1);
                 fd[1] = open("/dev/null", OWRITE);
                 fd[2] = open("/dev/null", OWRITE);
+ notedisable("alarm");
                 cpid = threadspawn(fd, player[0], player);
+ noteenable("alarm");
                 do{
                         m = recvp(cwait);
                         i = 0;
@@ -65,12 +67,29 @@
 #define GETC(bp) \
         ((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:tgetc((bp)))
 
+static int
+scan(int c, char *str) {
+ char *s;
+
+ s=str;
+ do {
+ if(*s++ != c)
+ break;
+ if(*s == '\0')
+ return 1;
+ }while((c = GETC(stream)) != Beof);
+ while(--s > str)
+ Bungetc(stream);
+ return 0;
+}
+
 static void
 proxyproc(void *v) {
         char errbuf[16];
- int c, i;
+ int c, i, j, bytes;
 
         qlock(&lk);
+ j = 0;
         while(playing) {
                 label("Starting stream.");
                 if(debug['p'])
@@ -82,34 +101,37 @@
                         if(stream)
                                 break;
                         if(i++ > 5) {
- fprint(2, "can't get stream: %r\n");
+ msg("Can't get stream: %r\n");
                                 playing = 0;
                         }
                         rerrstr(errbuf, sizeof errbuf);
- }while(playing && !strcmp(errbuf, "interrupted"));
+ }while(playing && !strcasestr(errbuf, "interrupted"));
 
                 c = 0;
+ bytes = 0;
                 while(playing && (c = GETC(stream)) != Beof) {
- if(c == 'S') {
- i = 0;
- if(i++, 'Y' == GETC(stream))
- if(i++, 'N' == GETC(stream))
- if(i++, 'C' == GETC(stream)) {
- if(debug['v'])
- msg("SYNC\n");
- skipping = 0;
- getmeta();
- continue;
+ if(scan(c, "SYNC")) {
+ if(debug['v'])
+ msg("SYNC\n");
+ skipping = 0;
+ getmeta();
+ }else
+ if(scan(c, "HTTP/1.0 ")) {
+ msg("Stream died: %.*s\n", Blinelen(stream)-1, Brdline(stream, '\n'));
+ playing = 0;
+ }else {
+ bytes++;
+ if(!skipping)
+ BPUTC(&playerin, c);
+ else {
+ playerin.ocount = -playerin.bsize;
+ /* Waited too long, probably won't actually skip.
+ * Just start a new stream.
+ */
+ /* 8192 is arbitrary. */
+ if(skipping++ > 8192)
+ break;
                                 }
- while(i--)
- Bungetc(stream);
- }
- if(!skipping)
- BPUTC(&playerin, c);
- else {
- playerin.ocount = -playerin.bsize;
- if(skipping++ > 8192)
- break;
                         }
                 }
                 if(debug['p'] && !playing)
@@ -117,6 +139,12 @@
                 if(c == Beof)
                         msg("Stream died\n");
                 Bterm(stream);
+ if(bytes > 2048)
+ j = 0;
+ else if(j++ > 15) {
+ msg("Stream won't stay open\n");
+ playing = 0;
+ }
         }
         qunlock(&lk);
 }
@@ -139,6 +167,7 @@
                 return;
 
         playing = 0;
+ postnote(PNGROUP, getpid(), "alarm");
         qlock(&lk);
         qunlock(&lk);
 
diff -r 4931463a5050 -r a086e162583a posix.c
--- a/posix.c Thu Aug 09 15:14:34 2007 -0400
+++ b/posix.c Mon Aug 24 13:56:37 2009 -0400
@@ -6,7 +6,10 @@
 
 void
 noblock(int fd) {
- if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
+ int old;
+
+ old = fcntl(fd, F_GETFL, 0);
+ if(fcntl(fd, F_SETFL, old | O_NONBLOCK) == -1)
                 sysfatal("Can't set O_NONBLOCK for %d: %r", fd);
 }
 
@@ -24,7 +27,7 @@
         print("\033];LastFM: %V\007", &v);
         va_end(v.args);
 
- return;
+ /* return; */
 
         if(status == nil) {
                 home = getenv("HOME");
diff -r 4931463a5050 -r a086e162583a util.c
--- a/util.c Thu Aug 09 15:14:34 2007 -0400
+++ b/util.c Mon Aug 24 13:56:37 2009 -0400
@@ -7,7 +7,7 @@
 eat(char **s, int (*p)(int), int r) {
         char *q;
 
- for(q=*s; p(*q) == r; q++)
+ for(q=*s; *q && p(*q) == r; q++)
                 ;
         *s = q;
 }
@@ -19,6 +19,8 @@
         eat(s, isspace, 1);
         p = *s;
         eat(s, isspace, 0);
+ if(**s)
+ *(*s)++ = '\0';
         eat(s, isspace, 1);
         return p;
 }
@@ -28,10 +30,9 @@
         va_list ap;
         int fd;
 
- fd = open(file, OWRITE);
+ fd = open(file, OWRITE|ONONBLOCK);
         if(fd < 0)
                 return;
- noblock(fd);
 
         va_start(ap, fmt);
         vfprint(fd, (char*)fmt, ap);
Received on Mon Aug 24 2009 - 17:56:31 UTC

This archive was generated by hypermail 2.2.0 : Mon Aug 24 2009 - 18:00:07 UTC