oscartheduckin’ around

May 9, 2008

code -> pdf

Filed under: how to — Tags: , , — oscartheduck @ 2:59 pm

I’m on my way out right now, but I’m going to write up a script tonight called code2pdf, and wanted to write down the history of it before it leaves my head.

I have been taking a CS class this semester, and have been writing code as a consequence. The requirements for submitting this code are twofold:

1. pop all code and a scriptfile proving the code works on a USB stick

2. print out copies of all code and the scriptfile and submit with above.

Now, most of the folks are using some kind of windows kludge to write their code, so they were taking screenshots of command windows and handing that in as a script file. I just used script, because I figured that’s what it’s there for. The problems were twofold:

1. I don’t have a printer in my house.

2. The printers at school are all connected to windows boxes.

So when I come in with a nice raw text file and attempt to print it, it looks _awful_. Same goes for code; my text files looked horrible.

But my instructor is a unix geek, so she didn’t mind at first. However, the driver being used to run the printer has a quirk in it; it has its own definition of what a tab is. I couldn’t believe it; my nice sensible “two spaces to a tab” code was running and running and running over line after line of blank space. Which didn’t worry me, but it made trying to read the code a horrible task for the instructor. True to her form, she mildly pointed this out to me and asked me to work on the issue, no fuss just a pleasant request. Which is the best way to get results.

So, I started thinking about it. First things first, I stopped using tabs and started using spaces. The printouts looked a lot better from that alone.

But I decided not to stop there; my instructor had been polite, and I have the power of FreeBSD available. I installed OpenOffice from the port (which took ages), after which my hard drive killed itself. So I installed anew and installed OpenOffice again, which took forever again. And I copied my code into OpenOffice and output it as a .pdf.

.pdf rules. I format things nicely, go through the file and make sure everything’s pretty and I’m happy as a clam at high tide. The script file was more difficult than the code, funnily enough, as openoffice is smart enough to read the .txt extension, scan the interior and then kindly offer to format everything as a spreadsheet.

Remove the extension and open the file again, and a different preprocessor asks for advice. I told it to use luxi mono as the font, and everything looks pretty. From there to .pdf is as simple as using the filter.

But why stop there? With the ports system at my fingertips, I can do anything! And I don’t want to start up openoffice just to spew out a .pdf file, more importantly. After a couple of creative searches, I found  a beautiful program called highlight, which takes code in and spews out one of several forms, HTML, rtf, TeX, LaTeX, XML, a few others.

I export everything to .rtf and then open it in openoffice, just to take a look. It looks great; colour code highlighting, the works. Export to pdf, to make sure I have it in time for my deadline, and then get back to work.

I export everything as TeX, then use pdftex and it comes out as a gorgeous looking pdf, but only in greyscale. I wanted to hand in colour, so I didn’t worry about this and instead used openoffice, but tonight I’ll be looking for a .rtf to .pdf filter on the command line so I can deal with everything and get colour output.

Whether I find it or not, I’ll write up a quick script to take in code and output a greyscale pdf file. Then I’ll investigate script files, which I imagine will be as simple as txt2pdf or something named similarly. Then I’ll submit it as a port; it’ll be a front end to other tools, but I think the ability to ensure my code looked gorgeous from any operating system and printed out correctly was sufficiently useful that other students would like the same thing. And lo! It will be called something witty.

I’m thinking it should be a simple thing that offers flags to output colour or greyscale pdfs (eventually), and also can handle script files. Pretty code all day long.

May 3, 2008

ca-ca-catching up

Filed under: Uncategorized — Tags: , , — oscartheduck @ 4:39 am

I did something today that I’ve never done before. I decided that I was going to see how far that FreeBSD and gnome integrate with each other. Whenever I install FreeBSD, I usually run it as a CLI os. I install a window manager, but it’s not there as anything really other than support for multiple terminals. I use ubuntu for a nice pretty OS with integration between all the disparate elements, and FreeBSD for real work.

So I started following the instructions from the FreeBSD Gnome folks for auto-mounting this and pleasant integration that. In the process, amazing things happened. Screensavers started working brilliantly, icons that were missing showed up out of nowhere, and I started to see the truth of a friend of mine’s observation to me two years ago that FreeBSD and Gnome are exceptionally well integrated with each other.

I almost feel like writing a port that’s just a shell script to execute the relevant bits and pieces to make FreeBSD shine with Gnome; it’s amazing that all this time I could have had this on the desktop and instead was using linux.

I privately noted an interesting thing today. I started using the _excellent_ portmaster tool to solve ports issues instead of portupgrade recently, and out of nowhere the amount of circular dependencies registered in pkgb skyrocketed. It’s probably merely a coincidence, but still.

trap just became my new best friend: http://docs.hp.com/en/B2355-90046/ch23s07.html

I’ve been following the code for NanoBSD of late; it’s extremely clearly written and provides excellent examples of shell code. Well worth a look.

I’ve also been following my interests down the path to the source code of common utilities. What’s neat is the little things I hadn’t thought of. For example, from boot0cfg, in the case statement that reads argv, Robert Nordier sets a variable to equal 1:

case ‘f’:
fpath = optarg;
break;

Then,

/* save the existing MBR if we are asked to do so */
if (fpath)
write_mbr(fpath, O_CREAT | O_TRUNC, mbr, mbr_size);

Robert directly tests for truth on the fpath variable. I’ve always called my variables something like fpath_test to remind myself that they’re essentially booleans; however, looking at this I’m suddenly hit by the “Oh, there’s no real need to do what I do, and it might sacrifice a little clarity”.

This suddenly makes me wonder: would there be any performance impact of using boolean types here? I can’t imagine there would be, as I assume the boole types are little more than static constants.

I noticed this little gem in the code for boot0cfg, too:

opttbl[] = {
{”packet”, 0},
{”update”, 1},
{”setdrv”, 0}
};

Is it just me, or is that sucker a dictionary/hash?

I need to include the following to satisfy the license:

/*
* Copyright (c) 1999 Robert Nordier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS“AS IS” AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

April 30, 2008

Reading code

Filed under: freebsd — oscartheduck @ 1:58 am

Today, I finally got to really start a beginning on my embedded BSD proposal; I started reading NanoBSD and TinyBSD, to see what they’re doing.

I love reading other coders’ code, because it’s always a learning experience. Poul-Henning Kamp, for example, writes very nice shell script. Very nice indeed.

As I am about to redistribute his code a little, let me include this :

<—-snip—->

# Copyright (c) 2005 Poul-Henning Kamp.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS “AS IS” AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD: src/tools/tools/nanobsd/nanobsd.sh,v 1.30 2008/01/12 22:58:06 simon Exp $

<—-/snip—->

Now that that’s over with, a little code is in order:

<—-snip—->

# Name of this NanoBSD build. (Used to construct workdir names)
NANO_NAME=full

# Source tree directory
NANO_SRC=/usr/src

# Where nanobsd additional files live under the source tree
NANO_TOOLS=tools/tools/nanobsd

# Where cust_pkg() finds packages to install
NANO_PACKAGE_DIR=${NANO_SRC}/${NANO_TOOLS}/Pkg

# Object tree directory
# default is subdir of /usr/obj
# XXX: MAKEOBJDIRPREFIX handling… ?
#NANO_OBJ=”"

# The directory to put the final images
# default is ${NANO_OBJ}
#NANO_DISKIMGDIR=”"

<—-/snip—->

See what he did there? He’s declaring variables. Every one of them is prefixed with a comment that says “This next variable means this”. I swear, I started reading this and admired the time put in simply to make the code easier to follow.

I was a little worried, though. It looks wordy. But I got down to the code:

<—-snip—->
###########################################
#
# The functions which do the real work.
# Can be overridden from the config file(s)
#
#######################################################################

clean_build ( ) (
echo “## Clean and create object directory (${MAKEOBJDIRPREFIX})”

if rm -rf ${MAKEOBJDIRPREFIX} > /dev/null 2>&1 ; then
true
else
chflags -R noschg ${MAKEOBJDIRPREFIX}
rm -rf ${MAKEOBJDIRPREFIX}
fi
mkdir -p ${MAKEOBJDIRPREFIX}
printenv > ${MAKEOBJDIRPREFIX}/_.env
)
make_conf_build ( ) (
echo “## Construct build make.conf ($NANO_MAKE_CONF)”

echo “${CONF_WORLD}” > ${NANO_MAKE_CONF}
echo “${CONF_BUILD}” >> ${NANO_MAKE_CONF}
)

build_world ( ) (
echo “## run buildworld”
echo “### log: ${MAKEOBJDIRPREFIX}/_.bw”

cd ${NANO_SRC}
${NANO_PMAKE} __MAKE_CONF=${NANO_MAKE_CONF} buildworld \
> ${MAKEOBJDIRPREFIX}/_.bw 2>&1
)
<—-/snip—->

A note which tells us “variables are over now, busters”, then straight into code.

It’s a good example of how to write a shell script. There’s enough information to get one oriented, but after that you’re on your own. And let’s face it, if you can’t work this stuff out reading it then no number of comments are going to help.

In reading it, I ran across a couple of programs I’ve never used. I figured, though, what better time to get familiar with them than by also reading their source code? So I gleefully started wandering through my source tree, discovering what there is to discover there. And as ever, I was impressed by how simple a lot of programs are.

chflags, for example, is basically two case statements.

I love usage statements. This code is covered under the same copyright as above, though I cannot see its author listed in the file. Author, please let me know who you are!

void
usage(void)
{
(void)fprintf(stderr,
“usage: chflags [-h] [-R [-H | -L | -P]] flags file …\n”);
exit(1);
}

My favourite program in the source tree right now is hostname; I remember the first time I saw it being struck by how well the various pieces of the operating system fall together to form a cohesive whole. It features a case statement that becomes a cascading case statement. The logic of the program is tiny: In substance it reads:

if (*argv) {
if (sethostname(*argv, (int)strlen(*argv)))
err(1, “sethostname”);
} else {
if (gethostname(hostname, (int)sizeof(hostname)))
err(1, “gethostname”);
if (sflag) {
p = strchr(hostname, ‘.’);
if (p != NULL)
*p = ”;
}
(void)printf(”%s\n”, hostname);
}
I love it for several reasons. It’s simple, it taught me a lot about unix, it’s just plain awesome.

For example, sethostname? Where is that function? Well, it’s declared as: extern int sethostname(char *, int);

Where? Well, use unix to tell you! cd /usr/src && grep -R sethostname *. /usr/src/contrib/gcc/sys-protos.h

Ach, what’m I talkin’ about, anyway. I just like greppin’ code and seeing what there is to be found.

April 21, 2008

GSOC

Filed under: Uncategorized — oscartheduck @ 10:28 pm

http://code.google.com/soc/2008/freebsd/appinfo.html?csaid=AB2F0224F1C4AD72

Yeah. That’s me.

Awesome.

April 9, 2008

person.glow(oscar)

Filed under: Uncategorized — oscartheduck @ 2:10 am

I’ve been writing lots of OOP code of late, after a long stint writing functional programming code, and I’m in this weird space.

On the one hand, it’s interesting writing the code and seeing all kinds of magic appear to happen with almost nothing done. On the other hand, I don’t trust objects to keep track of themselves.

What’s a person to do in this situation? It’s especially bad with java; I don’t expect it to be able to do anything as a language because of its uniformly weird implementation. Having default types which override the types that a programmer enters in the code is _just_plain_wrong_. But java’s also where the employment is right now.

I much prefer ruby. Open classes, isThisBoolean?, uniformly object oriented. It’s plain old gorgeousness on stilts.

For those of you out there clustering away, consider this: to reimage a bunch of nodes without too much hassle, unplug your dhcp server from a switch, plug in a freshly configured SystemImager master box and copy of the dhcpd.conf file, bob’s your uncle you’ve got instant reconfiguration of all nodes. Plug the old dhcp server back in and everything’s ready to rock and roll.

February 15, 2008

init not found pass init= to kernel

Filed under: linux — oscartheduck @ 10:04 pm

So you’ve rolled your own kernel, you’re maybe trying to build a ramdisk or something, and you’re faced with “init not found, try passing init= to kernel”. And you’ve tried everything else, you’ve looked forever.

What worked for me was suddenly remembering that the 2.6.x series of the kernel uses a cpio archive as an initial ram disk, not  a file that has been formatted with a file system. I mounted my file as a loop device and then created a cpio archive from it. Suddenly init could be found! A MIRACLE I TELLYA

Of course, this hasn’t solved the problem I have that init cannot be executed, but I suspect that’s a kernel misconfiguration….

January 15, 2008

FreeBSD ports management articles

Filed under: freebsd — oscartheduck @ 3:10 pm

I found these useful over the last twelve months or so. All well worth reading, not more than about half an hour’s worth of reading there to increase your comfort with ports by an order of magnitude:

http://www2.papamike.ca:8082/tutorials/pub/fbsd-ports2.html#pruning

http://www.math.colostate.edu/~reinholz/freebsd/pkgdb_F.html

http://lists.freebsd.org/pipermail/freebsd-chat/2006-January/003661.html

http://lists.freebsd.org/pipermail/freebsd-questions/2007-October/160881.html

_________


An honorary OpenBSD mention, on how (not) to use a mailing list:
http://marc.info/?l=openbsd-misc&m=119350344427347&w=2

EXT2-fs: blocksize too small ramdisk

Filed under: GNU/linux, ext2, how to, linux, ramdisk — oscartheduck @ 3:08 pm

I received this error creating ramdisks, and it was somewhat annoying finding a solution, though the error is perfectly clear.

It turns out that I had created my ramdisk with an ext2 file system and a blocksize of 1024, the default size. However, the kernel has built into it that it wants a size of 4096 for the block.

I couldn’t quite believe that it needed blocks that large, but the kernel guys know better than I do what to do. At first, I had tried a mere 2048 for the blocksize, but that didn’t cut it. Finding out that it wanted something that large was a shock, though.

Especially as the amount of RAM I have in my client is 256 meg. I have a 20 meg or so ramdisk image I am pushing down. Using a blocksize of 4096 resulted in a whopping 300 meg image. Too much for the memory.

Fortunately, you can pass the following as an option to the kernel:

ramdisk_blocksize=1024

or indeed any abitrary ramdisk blocksize, as long as it matches your real blocksize. If you’re using pxe, as I am, put it in your APPEND line. If you’re using grub to do this somehow, then add it in the boot line, I assume.

November 25, 2007

PordWress

Filed under: freebsd, wordpress — oscartheduck @ 4:53 pm

I’ve been wondering about the upgrade path for a wordpress installation I have on a webserver.

If I’d just left wordpress in the default location, ports would automatically upgrade it for me. I’d occasionally have issues from that, but that’s not what I did, so I don’t have issues from it. Instead, I have wordpress permanently stuck around version 2.1. Which is non-optimal.

So I’ve been thinking about how to resolve this. I imagine it’s as simple as finding the default location and cping a binary, but I’m uncertain. And if I break that website, there will be hell to pay, where hell is a hot place that’s very uncomfortable.

Aq recently wrote a post about this very conundrum: http://www.kryogenix.org/days/2007/11/23/wordpress-through-subversion  - so good for him. I’m going to try out a dual pronged approach. First, I’ll clone the mysql database/wordpress install from the website and throw it over to the vm, then see if I can update it safely by copying a port-installed binary. If so, score. If not, I’ll try the subversion approach.

October 21, 2007

(failed)

Filed under: Uncategorized — oscartheduck @ 2:49 pm

I didn’t get 24 pages last night. At 6 AM, with 1 hour to go, I was falling asleep in my chair on page 18.

The problem was that I never really saw how my different stories gelled.  Alas.

But I did work out one of the most subtle ways a comic can be a comic. And exploited it maximally.

Had a fun time, folks. See you next year.

Older Posts »

Blog at WordPress.com.