Script started on Thu Oct 11 23:46:49 2001
ws04% cat [K[K[Kps

   PID TTY      S           TIME CMD
 23671 ttyp0    S        0:00.22 -h -is (tcsh)
 19635 ttyp1    S        0:02.66 -tcsh (tcsh)
 23668 ttyp1    S  +     0:00.03 script
 23686 ttyp1    S  +     0:00.01 script
ws04% du

219     .
ws04% finm[Kd -ls

582528    8 drwx-----x   2 lowke    extdeg       8192 Oct 11 23:46 .
249602    1 -rwx------   1 lowke    extdeg        245 Oct  4 21:15 ./tt1.cgi
 83578   12 -rw----r--   1 lowke    extdeg      12288 Oct  4 21:29 ./.table1.html.swp
598476    1 -rw----r--   1 lowke    extdeg        496 Oct  4 22:13 ./table1.html
 65906    1 -rw----r--   1 lowke    extdeg        702 Oct 11 02:23 ./who.html
258143    4 -rw----r--   1 lowke    extdeg       3580 Oct 11 02:32 ./tt2ht1.c
258064    3 -rw----r--   1 lowke    extdeg       2823 Oct 11 01:46 ./tt.c
258170    1 -rw----r--   1 lowke    extdeg        320 Oct 11 21:13 ./data
257621    1 -rw-------   1 lowke    extdeg        843 Oct 11 23:45 ./README
 45067    1 -rwx------   1 lowke    extdeg        315 Oct 11 16:40 ./part3b.cgi
258114    1 -rw----r--   1 lowke    extdeg        137 Oct 10 22:57 ./weird.c
258078   24 -rwx------   1 lowke    extdeg      24576 Oct 11 16:38 ./tt2ht1
258137   24 -rw----r--   1 lowke    extdeg      24576 Oct 10 22:57 ./weird
304500   12 -rw-------   1 lowke    extdeg      12288 Oct 11 23:46 ./.README.swp
257937   16 -rw-------   1 lowke    extdeg      16384 Oct 11 23:41 ./.tt2ht3.c.swp
273849    1 lrwx------   1 lowke    extdeg         25 Oct 11 23:03 ./freshmen -> /home/l/i/lib113/freshmen
569670   24 -rwx------   1 lowke    extdeg      24576 Oct 11 23:42 ./tt2ht3
 83350    1 -rwx--x--x   1 lowke    extdeg        213 Oct 11 23:02 ./part5b.cgi
257970    5 -rw-------   1 lowke    extdeg       4802 Oct 11 23:40 ./tt2ht3.c
573920    1 -rwx------   1 lowke    extdeg        342 Oct 11 22:04 ./part4b.cgi
309057   12 -rw-------   1 lowke    extdeg      12288 Oct 11 19:50 ./.data.swo
257903    0 -rw-------   1 lowke    extdeg          0 Oct 11 23:46 ./typescript
271793    5 -rw-------   1 lowke    extdeg       4469 Oct 11 22:05 ./tt2ht2.c
257709   24 -rwx------   1 lowke    extdeg      24576 Oct 11 22:05 ./tt2ht2
 45060   12 -rw-------   1 lowke    extdeg      12288 Oct 11 21:05 ./.data.swn
 45020    1 -rw-------   1 lowke    extdeg        296 Oct 11 22:31 ./scanfiscool.c
257669   24 -rwx------   1 lowke    extdeg      24576 Oct 11 22:31 ./scanfiscool
ws04% cat who.html

<html>

<head>
        <title>'Who' table example</title>
</head>

<body bgcolor = white>
<center>'Who' table example</center>

        <table border = 2 cellpadding = 10 cellspacing = 1>
        
                <!-- Row 1 -->
                <tr>    
                        <td bgcolor = ltgreen >lowke</td>
                        <td bgcolor = yellow  >ttyp1</td>
                        <td bgcolor = yellow align = center>Oct</td>
                        <td bgcolor = yellow align = center>4</td>
                        <td bgcolor = yellow align = center>21:31</td>
                </tr>

                <!-- Row 2 -->
                <tr>    
                        <td bgcolor = ltgreen>lowke</td>
                        <td bgcolor = yellow>:0</td>
                        <td bgcolor = yellow align = center>Oct</td>
                        <td bgcolor = yellow align = center>4</td>
                        <td bgcolor = yellow align = center>21:31</td>
                </tr>


        </table>

</body></html>

ws04% cat tt2ht1.c

#include        <stdio.h>
#include        <ctype.h>
#include    <string.h>

/*
 *      tt2ht1.c
 *
 *      convert input file into html data table
 *
 */ 

#define LINESIZE                        512
#define MAXFIELDS               20
#define MAXFLDLEN               512
#define MAXDELIM                10


main()
{

        char    line[LINESIZE];                                                                 // line array of characters
        char    record[MAXFIELDS][MAXFLDLEN];                                   // the records  
        int     itemDelimeters[MAXDELIM] = {' ', '\t', '\n'};   // item delimeters
        int     i                        = 0,                                   // counter
                numberOfColumns          = 0,                                   // column count
                        numberOfRows             = 0;                                   // row count

        // output header
        printf("<html>\n");     
        printf("\n");
        printf("<head>\n");
        printf("        <title> table </title>\n");
        printf("</head>\n");
        printf("\n");
        printf("<body bgcolor = white>\n");
        printf("<center> table </center>\n");
        printf("\n");
        printf("        <table border = 2 cellpadding = 10 cellspacing = 1>\n");
        printf("\n");   
        
        
    // loop through each line of data 
    while ( fgets( line, LINESIZE, stdin) != NULL ) {
                
                numberOfRows++;
                
                // output fields
                printf("                <!-- Row %d -->\n", numberOfRows);
                printf("                <tr>\n");
                
                // parse line into records
                numberOfColumns = parse_line(itemDelimeters, line, record );

                // loop through each record
                for ( i = 0 ; i < numberOfColumns ; i++ ) {

                        if (i == 0) {
                                printf("                        <td bgcolor = ltgreen>%s</td>\n", record[i]);
                        }
                        else if (i == 1){
                                printf("                        <td bgcolor = yellow>%s</td>\n", record[i]);
                        }
                        else {
                                printf("                        <td bgcolor = yellow align = center>>%s</td>\n", record[i]);
                        }
                }
                printf("                </tr>\n");
                printf("\n");
        }

        // output footer
        printf("        </table>\n");
        printf("\n");
        printf("</body></html>\n");
        
}



int parse_line(int itemDel[], char mString[], char fields[MAXFIELDS][MAXFLDLEN]) 
//
// parse mString into an array of separate fields
// accepts an array of item delimiters to determine breaks
//
//  method: loop through mString looking for delimiters
//              while coping to target array.
//
        
{
                int     i           = 0,                // general purpose counter
                                endFlag     = 0,        // flag for indicating record is finished
                                readAt      = -1,       // reading position in source array
                currentFld  = 0,        // item in fields[] array
                                destination = 0;        // destination in current field                         
                
        do {
                // loop through mString
                        
                                readAt++;
                                
                                // loop through each possible delimiter
                                for (i = 0; itemDel[i] != NULL && ! endFlag ; i++) {
                                        
                                        // check for delimeter or the end of string
                                        if ( mString[readAt] == itemDel[i] || mString[readAt]  == NULL )
                                                endFlag = 1;                    // flag record as ended
                                }

                                // 

                if ( ! endFlag ) {
                                                //
                                // copy the char
                                        
                        fields[currentFld][destination] = mString[readAt];
                        destination++;
                }
                else {
                                                //
                                                // end record, terminate this field
                                        
                                                fields[currentFld][destination] = '\0';    

                                                // check if field is empty before incrementing
                                                if ( fields[currentFld][0] != '\0')
                                currentFld++;

                                                // reset for new field
                                                destination = 0;
                                                endFlag = 0;
                }    
                                
                                // error check: array limits
                                if ( readAt      == LINESIZE  ||
                                     currentFld  == MAXFIELDS ||
                                         destination == MAXFLDLEN )
                                        break;
                                
        } while ( mString[readAt] != NULL );
                
                // return number of fields found in line
        return currentFld;
}
ws04% cat p[Ktt1.cgi

#!/bin/sh
#
# cgi program to generate text tables from local Unix commands
#
        echo "Content-type : text/plain"
        echo ""

        echo "Here are the current users:"
        who
        echo "Here are the current files:"
        ls -l
        echo "Here is fstab:"
        cat /etc/fstab

ws04% cat part3b.cgi

#!/bin/sh
#
# cgi program to generate html tables from local Unix commands
#
        echo "Content-type: text/html"
        echo ""

        echo "<html><body bgcolor=white>"
        echo "<p>Here are the current users:"
        who | ./tt2ht1
        echo "<p>Here are the current files:"
        ls -l | ./tt2ht1
        echo "<p>Here is fstab:"
        ./tt2ht1 < /etc/fstab
ws04% cat tt2ht2.c

#include        <stdio.h>
#include        <ctype.h>
#include    <string.h>

/*
 *      tt2ht1.c
 *
 *      convert input file into html data table
 *
 */ 

#define LINESIZE                        512
#define MAXFIELDS               20
#define MAXFLDLEN               512
#define MAXDELIM                10




void trimWhiteSpace(char source[], char target[]) {
        //
        // remove all leading and trailing whitespace (including '\n')
        
        int i;
        
        // strip trailing whitespace
        for (i = strlen(source)-1; isspace(source[i]) && i >= 0 ; i-- ) {
                source[i] = '\0';
        }

        // count leading whitespace
        for (i = 0; isspace(source[i]) ; i++) {
        }

        // copy into target
        strcpy(target, &source[i]);
}


        
int parse_line(int itemDel[], char mString[], char fields[MAXFIELDS][MAXFLDLEN]) 
//
// parse mString into an array of separate fields
//                      accepts an array of item delimiters to determine breaks
//
//  method: loop through mString looking for delimiters
//              while coping to target array.
//
        
{
                int     i           = 0,                // general purpose counter
                                endFlag     = 0,        // flag for indicating record is finished
                                readAt      = -1,       // reading position in source array
                currentFld  = 0,        // item in fields[] array
                                destination = 0;        // destination in current field                         
                
        do {
                // loop through mString
                        
                                readAt++;
                                
                                // loop through each possible delimiter
                                for (i = 0; itemDel[i] != NULL && ! endFlag ; i++) {
                                        
                                        // check for delimeter or the end of string
                                        if ( mString[readAt] == itemDel[i] || mString[readAt]  == NULL )
                                                endFlag = 1;                    // flag record as ended
                                }

                                // 

                if ( ! endFlag ) {
                                                //
                                // copy the char
                                        
                        fields[currentFld][destination] = mString[readAt];
                        destination++;
                }
                else {
                                                //
                                                // end record, terminate this field
                                        
                                                fields[currentFld][destination] = '\0';    

                                                // check if field is empty before incrementing
                                                if ( fields[currentFld][0] != '\0')
                                currentFld++;

                                                // reset for new field
                                                destination = 0;
                                                endFlag = 0;
                }    
                                
                                // error check: array limits
                                if ( readAt      == LINESIZE  ||
                                     currentFld  == MAXFIELDS ||
                                         destination == MAXFLDLEN )
                                        break;
                                        
        } while ( mString[readAt] != NULL );
                
                // return number of fields found in line
        return currentFld;
}


main()
{
        char    oLine         [LINESIZE],                                               // original line from stdin
                        line          [LINESIZE],                                               // edited   line from stdin
                    attributes    [MAXFIELDS][MAXFLDLEN],                       // attributes   
                        record            [MAXFIELDS][MAXFLDLEN];                       // records from parse_line()
        int     itemDelimeters[MAXDELIM] = {' ', '\t', '\n'},   // item delimeters, space, tab, newline
                        i                        = 0,                                   // general purpose counter
                numberOfColumns          = 0,                                   // column count
                        numberOfRows             = 0,                                   // row count
                        mode                                     = 0,                                   // Modes, 0 = table, 1 = <top>, 2 = <attributes>
                        attCnt                                   = 0;                                   // attribute counter
        
                        
        // loop through input
        while ( fgets( oLine, LINESIZE, stdin) != NULL ) {
                                
                trimWhiteSpace(oLine, line);
                
                // switch various modes
                if (strcmp(line, "<top>") == 0) {
                        mode = 1; // mode = <top>
                }
                else if (strcmp(line, "<attributes>") == 0) {
                        mode = 2; // mode = <attributes>
                }
                else if (strcmp(line, "</top>") == 0) {
                        mode = 0; // mode = table
                }
                else if (strcmp(line, "</attributes>") == 0) {
                        mode = 0; // mode = table
                }
                else {
                        //
                        // handle data according to mode
                        
                        switch (mode) {
                                case 1:
                                        //
                                        // reading <top>
                                        
                                        printf("%s\n", line);
                                        break;
                                        
                                case 2:
                                        //
                                        // reading <attributes>
                                        
                                        strcpy(attributes[attCnt], line);
                                        
                                        attCnt++;
                                        break;
                                        
                                default:
                                        //
                                        // reading table

                                        // parse line into columns
                                        numberOfColumns = parse_line(itemDelimeters, line, record);
                
                                        // if a column, display a row
                                        if (numberOfColumns > 0) {
                                                
                                                // new row
                                                numberOfRows++;
                                                printf("<!-- Row %d -->\n", numberOfRows);
                                                printf("<tr>\n");

                                                // loop through each record
                                                for ( i = 0 ; i < numberOfColumns ; i++ ) {
                                                        printf("<td %s>%s</td>\n", attributes[i], record[i]);
                                                }
                                                printf("</tr>\n");
                                                printf("\n");
                        }
                                        
                        }
                }
        }       
}
ws04% cat part4b.cgi

#!/bin/sh
#
# cgi program to test tt2ht2
#
        echo "Content-type: text/html"
        echo ""

        echo "<html><body bgcolor=white>"

        {
                echo "
                <top>
                        <table border=0 cellspacing=1 cellpadding=3>
                </top>
                <attributes>
                        bgcolor=linen
                        align=right bgcolor=1emonchiffon

                        align=right
                        
                </attributes>"
                who

        } | ./tt2ht2

        echo "</table>"
ws04% cat tt2ht3.c

#include        <stdio.h>
#include        <ctype.h>
#include    <string.h>

/*
 *      tt2ht1.c
 *
 *      convert input file into html data table
 *
 */ 

#define LINESIZE                        512
#define MAXFIELDS               20
#define MAXFLDLEN               512
#define MAXDELIM                10




void trimWhiteSpace(char source[], char target[]) {
        //
        // remove all leading and trailing whitespace (including '\n')
        
        int i;
        
        // strip trailing whitespace
        for (i = strlen(source)-1; isspace(source[i]) && i >= 0 ; i-- ) {
                source[i] = '\0';
        }

        // count leading whitespace
        for (i = 0; isspace(source[i]) ; i++) {
        }

        // copy into target
        strcpy(target, &source[i]);
}


        
int parse_line(int itemDel[], char mString[], char fields[MAXFIELDS][MAXFLDLEN]) 
//
// parse mString into an array of separate fields
//                      accepts an array of item delimiters to determine breaks
//
//  method: loop through mString looking for delimiters
//              while coping to target array.
//
        
{
                int     i           = 0,                // general purpose counter
                                endFlag     = 0,        // flag for indicating record is finished
                                readAt      = -1,       // reading position in source array
                currentFld  = 0,        // item in fields[] array
                                destination = 0;        // destination in current field                         
                
        do {
                // loop through mString
                        
                                readAt++;
                                
                                // loop through each possible delimiter
                                for (i = 0; itemDel[i] != NULL && ! endFlag ; i++) {
                                        
                                        // check for delimeter or the end of string
                                        if ( mString[readAt] == itemDel[i] || mString[readAt]  == NULL )
                                                endFlag = 1;                    // flag record as ended
                                }

                                // 

                if ( ! endFlag ) {
                                                //
                                // copy the char
                                        
                        fields[currentFld][destination] = mString[readAt];
                        destination++;
                }
                else {
                                                //
                                                // end record, terminate this field
                                        
                                                fields[currentFld][destination] = '\0';    

                                                // check if field is empty before incrementing
                                                if ( fields[currentFld][0] != '\0')
                                currentFld++;

                                                // reset for new field
                                                destination = 0;
                                                endFlag = 0;
                }    
                                
                                // error check: array limits
                                if ( readAt      == LINESIZE  ||
                                     currentFld  == MAXFIELDS ||
 destination == MAXFLDLEN )
                                        break;
                                        
        } while ( mString[readAt] != NULL );
                
                // return number of fields found in line
        return currentFld;
}


main()
{
        char    oLine         [LINESIZE],                                                       // original line from stdin
                        line          [LINESIZE],                                                       // edited   line from stdin
                    attributes    [MAXFIELDS][MAXFLDLEN],                               // attributes   
                        record            [MAXFIELDS][MAXFLDLEN];                               // records from parse_line()
        int     itemDelimeters[MAXDELIM] = {' ','\n','\t','\0'},        // item delimeters, space, tab, newline
                        i                        = 0,                                           // general purpose counter
                numberOfColumns          = 0,                                           // column count
                        numberOfRows             = 0,                                           // row count
                        mode                                     = 0,                                           // Modes, 0 = table, 1 = <top>, 2 = <attributes>
                        attCnt                                   = 0;                                           // attribute counter
        char *cp;                                                                                                       // general purpose character pointer
        
                        
        // loop through input
        while ( fgets( oLine, LINESIZE, stdin) != NULL ) {
                                
                trimWhiteSpace(oLine, line);
                
                // switch various modes
                if (strcmp(line, "<top>")              == 0) {
                        mode = 1; // mode = <top>
                }
                else if (strcmp(line, "<attributes>")  == 0) {
                        mode = 2; // mode = <attributes>
                }
                else if (strcmp(line, "</top>")        == 0) {
                        mode = 0; // mode = table
                }
                else if (strcmp(line, "</attributes>") == 0) {
                        mode = 0; // mode = table
                }
                else if (strstr(line, "<delim")) {
                        cp = strstr(line, "value=");
                        itemDelimeters[0] = cp[6];
                        itemDelimeters[1] = ' ';
                        itemDelimeters[2] = '\n';
                        itemDelimeters[3] = '\0';

                        // sprintf(itemDelimeters, ":\n%c", cp[6]);  
                }
                else {
                        //
                        // handle data according to mode
                        
                        switch (mode) {
                                case 1:
                                        //
                                        // reading <top>
                                        
                                        printf("%s\n", line);
                                        break;
                                        
                                case 2:
                                        //
                                        // reading <attributes>
                                        
                                        strcpy(attributes[attCnt], line);
                                        
                                        attCnt++;
                                        break;
                                        
                                default:
                                        //

                                        // reading table

                                        // parse line into columns
                                        numberOfColumns = parse_line(itemDelimeters, line, record);
                
                                        // if a column, display a row
                                        if (numberOfColumns > 0) {
                                                
                                                // new row
                                                numberOfRows++;
                                                printf("<!-- Row %d -->\n", numberOfRows);
                                                printf("<tr>\n");

                                                // loop through each record
                                                for ( i = 0 ; i < numberOfColumns ; i++ ) {
                                                        printf("<td %s>%s</td>\n", attributes[i], record[i]);
                                                }
                                                printf("</tr>\n");
                                                printf("\n");
                                        }
                                        
                        }
                }
        }       
}
ws04% cat part5b.cgi

#!/bin/sh
#
# cgi program to test tt2ht2
#
        echo "Content-type: text/html"
        echo ""

        echo "<html><body bgcolor=white>"

        {
                echo "
                <delim value=:/>"

                grep LN=smith ./freshmen
        } | ./tt2ht3

        echo "</table>"
ws04% exit

exit

script done on Thu Oct 11 23:54:13 2001
