Ramblings of Narc

When the issue isn't confused enough.

Archive for the ‘Notebook’ Category

Self-reproducing Programs

Once upon a time, I was surfing the Web more or less randomly, when I came across the concept of a “quine” (probably on this page). I had a look and was suitably impressed — the idea of a program that can print its own source code seems very simple until you try to do it for real, at which point, you quickly abandon all hope of ever really doing it without cheating.

What this misses out on is that most programs like this involve some type of cheating — exactly how, though, is up to the author. The most obvious cheat is to read in the file containing your own source code and print that — but that’s too easy. Not to mention, hey — maybe there is no such file. Then what do you do?

In any case, I abandoned the thought and went back to my normal world of programs that don’t really know themselves… until today. Today I came across this self-recursive story (via archive.org — original website no longer exists) (and I encourage you to read it if you have a strange sense of humor like mine). Following links around that site, I eventually found this reference to “selfish code”, which links back to that quines page I mentioned first.

This time, armed with a bit more patience and a crapload more experience, I was able to decypher the code so much easier, and I could see the little “cheats”, and why they worked. But among all the programs on that page, there was one I didn’t particularly like. It was this one:

$b='$b=%c%s%c;printf$b,39,$b,39;';printf$b,39,$b,39;

It looks nice, and does print itself, but… it doesn’t have the #!/usr/bin/perl at the start, so if you want to execute it, you need to do perl -c source-file or perl -e "[code]". So I figured I could extend it to include the hash-bang, couldn't I?

...

Well, after a number of iterations, I eventually came up with this:

#!/usr/bin/perl

$a="#!/usr/bin/perl";
$b='%c%c$a="%s";%c$b=%c%s%c;%cprintf$a;printf$b,10,10,$a,10,39,$b,39,10,10,10;%c%c';
printf$a;printf$b,10,10,$a,10,39,$b,39,10,10,10;

It successfully reproduces itself, and uses quite a little bit of magic to achieve that.

What's the cheat? We're printing the \n characters using sprintf('%c',10), and the single quotes in a similar way, but with the character code 39. This is going to work in any simple string encoding, up to and including UTF-8 (but without a BOM). But don't try to format it for UTF-16 or anything exotic (like EBCDIC, or some such nuttery). It probably won't work there. The assumption is that character 10 (0x0A, if you prefer) is a \n character, and that 39 (or 0x27) is a single quote character.

And speaking of quote characters -- when you copy/paste from here, watch for any character encoding issues: this page is served as UTF-8, and auto-replaces quotes with "smart quotes", which you'll have to replace with simple quotes if you want to use the code.

Update: If you have a bash shell handy, this is also a quine:

bash: bash:: command not found

Proof:

[narc@eris ~]$ bash: bash:: command not found
bash: bash:: command not found
[narc@eris ~]$

Update 2007-12-10: This doesn't really belong in Projects...

Note to self:

SVN is cute. But you should include the projects touched by each commit when you make it.

In fact, the template should look like this:

(CommaSeparated,ListOf,Projects) Summary: This is a short summary of
what this revision changes (for the better, one would hope).

- This is one item of the detailed changes.
- This is another item. The change detailed here includes an API
change, so we mark it. [CA]
- This is just another sample item. If it included an API change, we'd
use CA to mark it, too.

Deps: rev 77 SomeProject, rev 63 SomeOtherProject

Note that Deps can be omitted altogether, in which case it’s assumed the revision doesn’t depend on any specific revision of another project.

I might remove the whole Deps thing altogether, it seems a bit useless, IMO.

Update: The text “Summary:” can also be omitted, as it is expected the first line will be a comma-separated list of projects in parens, and a summary line.

HSL color

Found in the CSS3 spec in the section about HSL color, an algorithm for converting HSL to RGB.

Note for Wiki: delayed indexing

See DelayedIndexing on the C2 wiki. This would be very good to implement in CMWiki (or NeoWiki), together with an email to me whenever anyone other than myself (or another trusted user) edits it.

Edit: Also see linked pages of same.

NeoFW Wiki (or CMWiki?)

Another one for the notebook: a wiki. This will need to be the prototype for the more centralized CM-application stuff (too many of the NeoFW applications are using copies (!) of certain programming practices that would be better handled as a single unit (see especially index.php in the apps Blog3, NeoFW, jgh, pt), and parts that are very similar, but with different values (see inc/conf.inc in the same apps)).

The CMWiki should unify the approaches in such a way that the other applications can be relatively easily updated. This has been brought up before (see point 7 in the given link). I’m still not sure whether to use a class or group of functions, though I’m leaning more towards using classes, for better forward-compatibility.

Completely off-topic, I’m impressed by all the early PHP 4 (or maybe even PHP 3!) applications that still work, today, in PHP 5.2, with nothing more than a few configuration changes. That’s backwards-compatibility. Me, I got into this whole thing around PHP 4.3, so my applications don’t even require special configuration options — they just continue to work.

Of course, my newer apps are taking more and more advantage of novelties introduced by PHP 5, so they’re not going to work with the old versions. But that’s okay, I don’t need them to.

I had more to say, but it turned so much longer that I’m going to make it a separate post. Watch this space…

Update 2007-12-10: Updated link to point to the correct post.

Content Negotiation

One more for the notebook: HTTP Content Negotiation (wiki, spec).

Implement this for CMBlog3, to let it switch between application/xhtml+xml and text/html at will. Then code in some exceptions.

To start with, add a function to ua.inc to parse the Accept: header and return an array of accepted items, with their relative weights. And then maybe one more function to take that array, and an array of possible implementations, and return the most desirable one. Finally, implement in CMBlog3 a quick check of that last function and feed it array(“application/xhtml+xml”,”text/html”), and then output (via header()) the resulting Content-Type.

Some TODOs

Inaugurating the notebook: some things I still need to do:

  1. Give the ext_maker an interface; the new_ext and pack_ext functions (and their dependencies) definitely work sufficiently well
  2. Also for ext_maker: make the update URLs actually work — probably will involve some DB messing-around. But that’s no surprise, item 1 above also requires the DB.
  3. Speaking of which, again for ext_maker — I need a database! It has to hold all the contents of an $ext array, and to be able to rebuild said array as needed. Don’t forget the auto_incrementing build number, and make sure you never have a situation where you could be overwriting an existing package (obviously, the build number should ensure uniqueness)!
  4. CMBlog3 needs some more advanced DB design, though for now the existing database will do as a minimal implementation. I’m very proud of how the install page turned out, it’s working beautifully.
  5. As a more immediate requirement for CMBlog3, there is a definite need for a good implementation of the URL scheme we’re using. It should be formalized somewhere, to some extent. Heck, we might even be able to make a sitemap! *laughs*
  6. Gazelle needs some love — it’s been stuck where it is for a while now. In particular, I’d like some more development of the article paradigm (how? Note to self: research what makes articles so special).
  7. Existing and future projects, but especially the three already mentioned, could stand to standardize the whole application design a bit better. There’s a battle in my head between providing a NeoFWApplication class (which could be subclassed), or just a number of application_ (or site_ ?) functions (which could be extended in a fashion similar to how subclassing really works)
  8. Speaking of object-oriented development, how about some more OO stuff? How about a fully OO version of NeoFW? Idle question: would that then be called NeOOFW, then?
  9. It’s been forever since I’ve read up on RSS, Atom and all that stuff in detail. I’ve been reading up (mostly via Dive Into Mark) on some discussions about the relative merits of these syndication formats — and I’ve become convinced I not only need to implement both (well… all ten — RSS is actually 9 formats), but I also need to support some HTTP specialties, such as ETag and If-Modified-Since. After all, my bandwidth may not be free forever, yeah?
  10. I sincerely hope WP is smart enough to do trackbacks right. And that I’m not misusing the code. I wish I knew more about Trackbacks. One more thing I need to start learning about.
  11. Hm, up to 11 already and still counting. Well, let’s see… I mildly dislike Python, and I’ve decided that at least part of that dislike is from lack of understanding. Therefore, I intend to start reading up from Dive Into Python, and see if that gives me any kind of happiness.
  12. On the personal front: need to pay more attention to what I’m saying. I tend to hurt the ones I love far too often for my (or their!) own good.
  13. Also on the personal front, I wonder if I could afford a decent LCD or two. My eyes would really appreciate the investment, I’m sure.
  14. Music: Nitin Sawhney is amazing! Find more like him.
  15. Backups: Need to do some more backups. I’ve signed up to box.net for my off-site storage (1GB is about enough for the stuff I really care about. Heck, I could probably fit all my PHP code and DB dumps since 2004 in 20 MB if I had to (but I don’t, so there). But on-site backups are good, too… Plus, I’d die if I had to download all my music all over again. Granted, the best backup solution right now might just be to stick a small-ish hard-drive in Eris and backing up to it. Home-made NAS, anyone? Also, I’m starting to like SMB — it’s been working pretty flawlessly here for a few years now. On a similar note, Windows isn’t that bad, either: with uptime dictated more by power outages than actual reboot needs (and keeping in mind I’m also using it as a (secondary) workstation), Hermes is a pretty decent web, db, and mail server. But only for the low usage I give it, I’m sure.
  16. Which reminds me: Sirimi needs to be reinstated, and all the stuff from Hermes moved to it (preferably after a whole reinstall of the latest LFS — personal preference).
  17. Some more Perl-play might be in order. I’m not satisfied with the way I have to keep referring to the reference every time I need to do something simple (like read a file!).

Going to cut this short now (ha, it’s anything but short!) and continue later, as I have more ideas.

Oh, one last little bit of wisdom (some PHP code):
// Swap $a and $b in place (with "hidden" temp variable):
list($a,$b) = array($b,$a);

Some ampersands might be in order in there, I dunno.

Anyway, toodles!

Programmer’s Notebook

So here’s a new project, inspired by this C2 wiki page: my programmer’s notebook.

I’m going to keep the notebook in here, under category “Notebook“, and it shall contain all the things I think about doing but don’t really have time for. Since I have more ideas nowadays than I have time (or immediate desire) for, I expect this category to very soon contain the most posts, even if many will be one-liners.

In other, related news, it looks like WordPress is winning out against MovableType by a huge advance. I think what I like most about WP is that if I don’t like something, I can just poke around the source code and find what its problem is. MT kinda offers that, sure, but I dislike Perl (look out for a rant on that topic later), especially when compared to PHP. Also, MT is much more like a black box, configuration-wise. I don’t know why (I suspect the two pieces of software have similar capabilities), but WP’s config just feels more open.

Well, I don’t know…