#!/usr/local/bin/perl # # require 'cgi-lib.pl'; &ReadParse(*in); &read_db; $typE = $in{"actions"}; $namE = $in{"name"}; $phonE= $in{"phone"}; if ( $typE =~ /Add/ ) { if ( $List{$namE} ) { &write_alert($namE," ERROR: cannot add"); } else { $List{$namE} = $phonE; &write_alert($namE,"OK: this name has been added to the DB"); &write_db; } } elsif ( $typE =~ /Change/ ) { if ( ! $List{$namE} ) { &write_alert($namE," ERROR: cannot change"); } else { $List{$namE} = $phonE; &write_alert($namE," OK: this persons phone number is changed"); &write_db; } } elsif ( $typE =~ /Delete/ ) { if ( ! $List{$namE} ) { &write_alert($namE," ERROR: cannot delete"); } else { delete $List{$namE}; &write_alert($namE," OK: has been deleted"); &write_db; } } elsif ( $typE =~ /List/ ) { &write_alert($namE," OK: display all entries matching this name"); } else { &write_alert($typE, " ERROR: illegal command"); } # ^^^ write_db: writes out associative db ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sub write_db { local ( $len, $a, $b); open (MYDB,">zz-my-db"); $len = 0; while ( ($a, $b) = each(%List) ) { if ($len == 0) { print MYDB ($a, "&", $b); } else { print MYDB (",", $a, "&", $b); } $len = $len + length($a) + length($b) + 3; if ( $len >= 70 ) { print MYDB ("\n"); $len = 0; } } close (MYDB); } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # ^^^^^ read-db: reads NAME/PHONE list ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sub read_db { local ($key_values, $len, $i, $Key, $Value); open (MYDB, ") { chop; @key_values = split(/,/, $_); $len = @key_values; for ( $i=0; $i < $len; $i++ ) { ($Key, $Value) = split(/&/, $key_values[$i]); $List{$Key} = $Value; } } close(MYDB); } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # ^^^ MIME-header: writes a CGI header ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sub my_header { local ($title_string) = @_; print < $title_string

$title_string


END_OF_HEADER ; } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # ^^^ write_js: writes out Javascript code ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sub write_js { print < function is_Alpha( c ) { // see if char is alpha ^^^^^^^^^^^^^^^^^^^^^^ if ( ( c >= 'A' ) && ( c <= 'Z' ) ) return true; if ( ( c >= 'a' ) && ( c <= 'z' ) ) return true; return false; } // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv function is_Digit( c ) { // see if char is digit ^^^^^^^^^^^^^^^^^^^^^^ if ( ( c >= '0' ) && ( c <= '9' ) ) return true; return false; } // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv function is_Blank( c ) { // see if blank char ^^^^^^^^^^^^^^^^^^^^^^^^^^^ if ( ( c == ' ') || ( c == '\\n') || ( c == '\\t') ) return true; return false; } // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv function is_Name( str ) { // checks valid name ^^^^^^^^^^^^^^^^^^^^^^^^^ var state = 0 for ( i = 0; i < str.length; i++ ) { var c = str.charAt(i) if ( state == 0 ) { if ( is_Alpha(c) ) { state = 0 } else if ( is_Blank(c) ) { state = 1 } else { state = 99 } } else if ( state == 1 ) { if ( is_Alpha(c) ) { state = 1 } else if ( is_Blank(c) ) { state = 2 } else { state = 99 } } else if ( state == 2 ) { if ( is_Alpha(c) ) { state = 0 } else if ( is_Blank(c) ) { state = 1 } else { state = 99 } } } if ( state < 3 ) return true; return false; } // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv function is_Phone ( str ) { // checks for valid phone ^^^^^^^^^^^^^^^^^ if ( str.length != 12 ) return false; if ( str.substring(3,4) != '-' ) return false; if ( str.substring(7,8) != '-' ) return false; for ( var i = 0; i < str.length; i++ ) { if ( (i == 3) || (i == 7) ) continue if ( !is_Digit( str.charAt(i)) ) return false; } return true; } // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv function check_values() { // checks name, phone for syntax ^^^^^^^^^^^^ var xname; // name of person var xphone; // phone number var msg; // informative or error message xname = document.forms[0].name.value; xphone = document.forms[0].phone.value; if ( is_Name( xname ) && is_Phone( xphone ) ) { alert( 'OK: ' + xname + ' and ' + xphone + ' are valid ') } else if ( ! is_Name( xname ) ) { alert( 'ERROR: ' + xname + ' is not in correct form ' ) } else { alert( 'ERROR: ' + xphone + ' is not in correct form ' ) } } // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv END_OF_JS ; } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # ^^^^ write_html: writes HTML code ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sub write_html { print < Name:
Phone:
Add a name and Phone number to the DataBase
Change the phone number in the DataBase
Delete a person from the DataBase
List all entries that match a substring in -Name:-
Check the name and phone for valid syntax



END_OF_HTML } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # ^^^^ write_dummy_display_values ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sub write_dummy_display_values { print <<___js_dmy; ___js_dmy } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # ^^^^ write_display_values ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # Writes out JavaScript code to display a window with those name-phone # pairs where the name matches any part of the string in the NAME # sub write_display_values{ local ($Npairs); # Number of name-phone pairs retrieved local ($name, $phone); # name/phone pair retrieved print <<___js_db1; ___js_db3 } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv # ^^^^ write_alert: writes a JS alert message ^^^^^^^^^^^^^^^^^^^^^^^^^^^ sub write_alert { local ($nam,$msg) = @_; local ($msg1, $msg2 ); print &PrintHeader; &my_header(" Name and Phone Data Base"); &write_js; if ($typE =~ /List/) { &write_display_values(); } else { &write_dummy_display_values(); } &write_html; if ( $msg =~ /cannot add/ ) { $msg1 = "ERROR: the name " . $nam . " already exists and "; $msg2 = "cannot be added "; } elsif ( $msg =~ /cannot change/ ) { $msg1 = "ERROR: the name " . $nam . " does not exist in "; $msg2 = "the DB and so phone cannot be changed "; } elsif ( $msg =~ /cannot delete/ ) { $msg1 = "ERROR: the name " . $nam . " does not exist in the "; $msg2 = "DB and so cannot be deleted "; } elsif ( $msg =~ /illegal/ ) { $msg1 = "ERROR: an illegal command: " . $nam . " was issued "; $msg2 = " "; } elsif ( $msg =~ /has been added/ ) { $msg1 = "NOTE: the name " . $nam . " has been added to the DB"; $msg2 = " "; } elsif ( $msg =~ /is changed/ ) { $msg1 = "NOTE: the phone for " . $nam . " has been changed "; $msg2 = " "; } elsif ( $msg =~ /been deleted/ ) { $msg1 = "NOTE: " . $nam . " has been deleted from the DB "; $msg2 = " "; } elsif ( $msg =~ /display all/ ) { $msg1 = "NOTE: " . " List all entriesd matching " . $nam ; $msg2 = " "; } else { $msg1 = "ERROR: unspecified command "; $msg2 = " "; } # chop($msg1); print < alert("---------------------------------------------------------\\n" + " $msg1 " + "\\n" + " $msg2 " + "\\n" + "---------------------------------------------------------") END_OF_JS4 ; } # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv