Example sources

Some small tricks I have done, working with development tools and interesting code I have written. It is not a tutorial site as much just an interesting place to shuffle around. I do have a tiny software collection available (all of which are an order of magnitude larger than anything here).

Contents

Some Small PHP Tidbits

This site really only uses a little PHP at the moment and in three ways; to include common files/data, a feedback form, and a search form. Including files is pretty straightforward, but I found two articles on a feedback and search form thatI thought would be nice to put in the examples.

Simple Feedback Form in PHP

My form is similar to this one:

<?php
if ($_POST) {
        $subject        = $_POST['subject'];
        $email          = $_POST['email'];
        $comments       = $_POST['comments'];

        $message = Message from $name ($email)\n\nComments:\n\n$comments;
        mail(foo@bu.com, Feedback, $message);

        echo "<h2>Thank you!</h2>\n";
        echo "<p>Your feedback has been sent.</p>\n";
} else {
?>

<h2>Send Feedback</h2>
<form method = post action=<?= $PHP_SELF ?>>

Email Address:<br />
        <input type=text name=email /> <br />

Subject:<br />
        <input type=text name=subject /> <br />

<br />Subject:<br />
<textarea name=comments rows=5 cols=50> 
        </textarea><br /><br />

        <input type=submit value=Send Feedback />

Grep Search

The search PHP script while clever has one slight drawback, it is using a non portable grep syntax. Generally speaking, on any Open Source OS grep -r is supported. I found this method right here:

<Search>
<form action=<?=$PHP_SELF;?> method=post
        <input type=text name=searchstr
                value=<?php echo $searchstr; ?> size=20 maxlength=30 />
        <input type=submit value=Search />
</form>

<?php
        $cmdstr = grep -ir $searchstr dir1/* dir2/*.html about/*;
        $fp = popen( $cmdstr, r );
        $myresult = array();

        echo (<h3>Search Results</h3>\n);

        while( $buffer = fgetss ( $fp, 4096 )) {
                list( $fname, $fline ) = split( :, $buffer, 2 );
                if ( !defined( $myresult[$fname] ))
                        $myresult[$fname] = $fline;
                }

                if ( count( $myresult )) {
                        echo <ul>\n;
                        while ( list( $fname, $fline ) = each( $myresult))
                                echo <li><a href=\$fname\>$fname</a> : $fline </li>\n;
        } else {
                echo <p>Sorry. Search on <b>$searchstr</b> returned no results.</p>\n;
        }
        pclose( $fp );
}
?>

Note how the feedback uses a reverse logic, if we have not done a post then print the form yet the search just replaces part of the page on the fly.

The Four States of Nothingness in Perl

I didn't believe it so I had to try it for myself; Perl has four states of nothing. When I have time I will definitely play around with other languages and the nefarious nothing.

my $darkman;
if ($darkman == 0) { print "Darkman is a numerical 0\n";}
if ($darkman == NULL) { print "Darkman is a NULL but not \'NULL\'\n";}
if (!$darkman) { print "Darkman is not !defined\n";}
if ($darkman eq '') { print "Darkman has nothing inside of any quotes\n";}

die $darkman;

Apparently, nothing can die...

Mini Floppy boot using NASM

While trying to troubleshoot a BIOS problem, I found this and made some minor modifications to it.

; read from a floppy to boot up

[ORG 0]

        jmp 07C0h:start     ; Goto segment 07C0

; message string
msg     db      'Booting....'

start:
        ; Update the segment registers
        mov ax, cs
        mov ds, ax
        mov es, ax

        mov si, msg     ; put the message string into si

reset:                      ; Reset the floppy drive
        mov ax, 0           ;
        mov dl, 0           ; Drive=0 (=A)
        int 13h             ;
        jc reset            ; ERROR => reset again


read:
        mov ax, 1000h       ; ES:BX = 1000:0000
        mov es, ax          ;
        mov bx, 0           ;

        mov ah, 2           ; Load disk data to ES:BX
        mov al, 5           ; Load 5 sectors
        mov ch, 0           ; Cylinder=0
        mov cl, 2           ; Sector=2
        mov dh, 0           ; Head=0
        mov dl, 0           ; Drive=0
        int 13h             ; Read!

        jc read             ; ERROR => Try again


        jmp 1000h:0000      ; Jump to the program

times 510-($-$$) db 0
dw 0AA55h

Basically, we init nasm, update the registers, stick the message string in then reset and read. Not much but another good starting point.

Tiny inline asm in C using gnu-as

This is about as small as it gets for inline assembly using the gnu assembler.

/* Simplistic example */
int
main(void)
{
__asm__ (" mov %ah,5");
        return 0;
}

Small Demo in NASM

NASM is the netwide assembler.

This is a very small demo file in nasm that has a lot of comments regarding x86 based systems (there is some conjecture in there too...):

; For 16 bit machines
; bit:          1 or 0
; nibble:       4 bits
; byte:         8 bits
; word:         2 bytes (16 bit)
; dword:        2 words (32 bit)
;
; word is dependent on the bus width, e.g a 32 bit processoer has a 32bit
; word and a dword is 64bit....

First, some functions:

; stackplay
;       What we are doing is using a stack to swap out the contents of 
;       two general purpose registers.
stackplay:
        push cx ; put cx contents on the stack, aka BP or SP, or Stack Pointer
        push ax ; put ax contents on the stack on top of cx
        pop cx  ; put value from stack (the last item which would have been from
                ; ax into the cx register
        pop ax  ; put the value from the stack (the last item whih would've been
                ; from the cx rgister 3 lines up) into the ax register
; zerogp
;       zero out the general purpose registers
zerogp:
        mov ax,00
        mov bx,00
        mov cx,00
        mov dx,00

Now onto the main where we shuffle more stuff around and print a message.

message db "hello world!$"      ; array byte that is stored using offsets

; Move some stuff around
; NOTES:
;       the AX register is actually bits 0-7 of AL and 7-15
;       of AH 8 bit registers.
mov ax,10       ; put 10 in general purpose ax register
mov bx,20       ; put 20 in general purpose bx register
mov cx,30       ; put 30 in general purpose cx register
mov dx,40       ; put 40 in general purpose dx register

; move stuff from one GP register to another
mov ax,dx       ; move the contents from dx into ax

jmp stackplay
jmp zerogp

Elementry Congruence with python

Elementry numerical congruence says m and n are integers and m!=0. Using the division algorithm n can be expressed as:

        n = qm + r,  where  0 <= r < |m|

q is the quotient, r the remainder and |m| is absolute m. To prove this statement, a simple python session can help out:

        >>>q = 3
        >>>m = 9
        >>>r = 7
        >>>q * m + r
        >>>34

Tiny inlince C in Perl

A really small example of some inline C in perl, but it is pretty obvious that it is easy to go from here if you wished to (provided you have the Inline Perl module).

#!/usr/pkg/bin/perl 

use Inline C => <<'END_C';
void greet() {
        printf("Hello World\n");
}
END_C

greet;

LISP for ISBN Processing

This is an extract from an article I wrote a few years ago that I found interesting (and at the time actually was quite useful to me for some reason ... ).

An ISBN is built using four sets of numbers:

  1. group code
  2. publisher code
  3. id number assigned by publisher
  4. check digit

In this example, I will use the same one as I used in the article:

        0-673-38582-5

The check digit has 11 possible values:

        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, X(10)

The final digit is determined by multiplying the first 9 digits by 10, 9, 8, 7, 6, 5, 4, 3, 2 respectively then add them (in our example we will call this y). The check digit is then chosen so that y+check_digit is divisible by 11.

   [6]> (setq y-first (+ (* 10 0) (* 9 6) (* 8 7) (* 7 3) (* 6 3)))
   149
   [7]> (setq y-second (+ (* 5 8) (* 4 5) (* 3 8) (* 2 2)))
   88
   [8]> (setq y (+ y-first y-second))
   237
   [9]> (setq check-digit 5)
   5
   [10]> (setq mod 11)
   11
   [11]> (+ y check-digit)
   242
   [12]> (/ 242 mod)
   22

Nuke Ms in Perl

From Matt's Script Archive, this little script is a classic for getting rid of pesky ^Ms in a file:

#!/usr/bin/perl
while (<>) {
        $_ =~ s/\cM\n/\n/g;
        print $_;
}

Branch/Release (rtagging) in CVS

In this example;

mmw
is the project dir name:
cd ~mmw/
cd ..
cvs rtag mmw-MAJOR_NUMBER-MINOR_NUMBER-NEXT_NUMBER mmw/

Since it uses MAJOR.MINOR, for the 1.9 release ....

cd ~src/mmw
cd ..
cvs rtag mmw-1-9 mmw/

Of course,

$CVSROOT
has to be set (might be a good idea to make sure
$CVS_RSH
is set as well).

SVN move

This is really a migration since svn move never works right...

server_from: svanadmin dump /path/to/repo > /path/to/repo.dump
server_to: svnadmin create /path/to/newrepo
scp /path/to/repo.dump server_to:/path/to/newrepo.dump
(on server_to)
svnadmin load < /path/to/newrepo /path/to/newrepo.dump

The Magic Number In Perl

I had to prove this after I read it:

Ask a friend to think of any number. Then have them double it, add 12, divide by 2, and subtract the original number. Before they're done, tell them the answer: 6. It will always be 6.

Well lo and behold it works:

#!/usr/bin/perl

sub pn {
    print "&_\n";
}
$n = $ARGV[0];
$orig = $n;

$n = $n * 2;
pn($n);
$n = $n + 12;
pn($n);
$n = $n/2;
pn($n);
$n = $n - $orig;
pn($n);

Here is some sample output:

~]$ perl magic_number 1095
2190
2202
1101
6
~]$ perl magic_number 444
888
900
450
6