Re: Sample program for parsing data and getting response from web server using get and post

Jonathan Leslie <jleslie48-/[email protected]>
Newsgroups gmane.comp.hardware.rabbit-semiconductor
Message-ID <[email protected]>
Here's a tried and true html/cgi file pair that I've been using for years.  Its written in straight C.  It uses the "post" method (look for the "enter command" button) There is a lot of other stuff in here, but I use this as the basis for all of my CGI programs.   I've ported it to various linux and windows pcs.    It gets all the parameters sent to it using a <form>...</form> called by an html page, and the CGI makes a lookup array of variables and their values.   
The program then parses the html page and replaces tags with values, kinda  poor mans dhtml page.   

hope this helps.  - jleslie48








ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics# cat pics01_ans.html
<!-- pics01_ans.html begin -->
<!-- Please note the usage of the JL:: tags.  I have static tags such as status1
as well as overloaded tags such as:
cgivarXXXX::            - where XXXX is the name of the input box so the C program
                          can know what value replaces the tag.
radiocgivarXXXX::YYYY:: - where XXXX is the name of the input box, and YYYY is the
                          value that must match in order for a translation to occur

Please look at the C code for pics01.c to see all of the available tags.

!!!! Please note this file is a mutatis mutandis of freak.html !!!!
-->

<html>

<head><title>PICS -home  </title>
<SCRIPT language=JavaScript>
/*
var NS = (window.Event) ? 1 : 0;

function checkKey(e) {
  var code = (NS) ? e.which : event.keyCode;

  if (code == "13"){
        return false
        }
}
*/

nextform ="lpform";
nextfield = "prod1"; // name of first box on page
netscape = "";
ver = navigator.appVersion; len = ver.length;
for(iln = 0; iln < len; iln++) if (ver.charAt(iln) == "(") break;
netscape = (ver.charAt(iln+1).toUpperCase() != "C");

function keyDown(DnEvents) { // handles keypress
  k = (netscape) ? DnEvents.which : window.event.keyCode;
  //alert ("hello "+k);
  if (k == 13) { // enter key pressed
       return true;
     } //then
} // function keyDown

document.onkeydown = keyDown; // work together to analyze keystrokes
if (netscape) document.captureEvents(Event.KEYDOWN|Event.KEYUP);

function window_onload(formobj, searchfield, endfocus)   {
/*
    this function will fill fields on the load of the html page.  it's not
    very elegant, and has to be manually adjusted for each field that you want
    updated on load.
History
JL   010708     First Creation.
MJL  010710     Mindy made this dynamic.  It uses the onblur to update all
                the fields that match inputed searchfield
*/

  var i           = 0

  var searchBox   = ""
  var searchName  = ""

  var firstElement = formobj.elements[0];

  while (i < formobj.elements.length ) {
        searchBox   = formobj.elements[i]
        searchName  = searchBox.name.substr( 0, searchfield.length )

        if( searchName.toUpperCase() == searchfield.toUpperCase() ) {
            searchBox.focus();
            if( searchBox.value == "" ) i= formobj.elements.length;
        }
        i++;
  }

    /* alert("elements.length is :" + formobj.elements.length +". I is :"+ i + "\n"); */
  endfocus.focus();
  endfocus.select();

}   /* window_onload */


function help_window(helppage)   {
/*
    this function will create a new window with the given html page.
History
JL   010709     First Creation.
*/
  newwindow = window.open( helppage,
                           "generalhelppage",
                           "resizable=yes,scrollbars=yes,status=yes,width=320,height=400"
                         )
  newwindow.focus()
}

</SCRIPT>

</head>

<body bgcolor="#ffffff" background="<!--JL::htmlloc::JL-->images/nothing.png" color="blue"
    onload="document.lpform.prod1.focus();">
<script language="JavaScript">
/* document.onkeypress = checkKey; */
</script>
<table> <!-- header 01 -->
   <tr>
   <td>
   <img src="<!--JL::htmlloc::JL-->images/mm04.png" width="300"> &nbsp; &nbsp; &nbsp; &nbsp;
   </td>

   <td>
   <font face=arial size="+2" color=blue>
   Welcome to Inventory Control System (PICS). <br>
   <br>
   From here you can create, look-up, and find all the pallets that have been <br>
   made from the production facility here at .
   <br>
   </font>
   </td>

   <td>
   <img src="<!--JL::htmlloc::JL-->images/nothing.png" width="50"> &nbsp; &nbsp; &nbsp; &nbsp;
   </td>

   </tr>
</table><!-- header 01 -->

<font face=arial size="+0" color="<!--JL::cgivarcolora::::JL-->" > <i>
<hr>
<br>
Submittal status:<br></i>
<!--JL::date::JL--> <!--JL::time::JL-->
<br>
</font>

<table width=100% border=0> <!-- table 100 -->

<tr>

<td width=70% nowrap >

<p align=left>


<table border="0" bgcolor="#dddddd" width="800"> <!--grey background -->
<tr><td>
<table border="1" bgcolor="#dddddd" width="800"> <!--buttons -->
  <tr>
    <td width="25%">
    parameters
    </td>
    <td width="25%">
         command
    </td>
    <td >
        descripton
    </td>
  </tr>  <!-- header row -->

  <tr><!-- dataline 1 -->
    <td width="25%">
    </td>

    <td width="25%">
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <a href="/cgi-bin/pics100.cgi">
      <button>Pallet Production</button></a>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    </td>

    <td >
       - Pallet production: create new pallet tags.
    </td>
  </tr>  <!-- dataline 1 -->

  <tr><!-- dataline 2 -->
    <td width="25%">
    </td>

    <td width="25%">
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <a href="/cgi-bin/pics_pallet101.cgi">
      <button>Pallet Prod101 </button></a>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    </td>

    <td >
       - create a new pallet 101.
    </td>
  </tr>  <!-- dataline 2 -->

  <tr><!-- dataline 3 -->
    <td width="25%">
    </td>

    <td width="25%">
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <a href="/cgi-bin/pics100.cgi">
      <button>Pallet Change</button></a>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    </td>

    <td >
       - Change the status/location of a pallet
    </td>
  </tr>  <!-- dataline 3 -->

  <tr><!-- dataline 4 -->
    <td width="25%">
    </td>

    <td width="25%">
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     <a href="/cgi-bin/pics100.cgi">
      <button>Pallet Report</button></a>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    </td>

    <td >
       - various reports, inventory lists, etc.
    </td>
  </tr>  <!-- dataline 4 -->


</table> <!--buttons -->

<form name="lpform" method="post" action=" /cgi-bin/pics01.cgi">
<font color="blue">

<input name="colora" type="hidden" value="maroon" >

<table border="1" bgcolor="#dddddd" width="800"> <!--form pt1 -->

  <tr><!-- dataline blank -->
    <td width="25%">
    </td>

    <td width="25%">
    </td>

    <td >
    </td>
  </tr>  <!-- dataline blank -->

  <tr><!-- dataline c2 -->
    <td width="25%">
        <input name="prod1" size=24 maxlength=44 value="<!--JL::cgivarprod1::::JL-->"
         onFocus="nextform='lpform';nextfield='um1';document.lpformtop.fnextfield.value=nextfield;document.lpformtop.currentprodfield.value='prod1';select()"
        >

    </td>
    <td width="25%">
        <select name="um1" title="units of measure"
         onFocus="nextform='lpform';nextfield='submt';"
        >
        <option value="1"   <!--JL::selectcgivarum1::1::::JL--> >choose:
        <option value="ms"  <!--JL::selectcgivarum1::ms::::JL--> >ms, milliseconds
        <option value="us"  <!--JL::selectcgivarum1::us::::JL--> >us, microseconds
        <option value="pmf"  <!--JL::selectcgivarum1::pmf::::JL--> >product master file
        <option value="khz" <!--JL::selectcgivarum1::khz::::JL--> >kilo-hertz
        </select><br>
    </td>
    <td >
        <!-- answer goes here -->
        <!--JL::calculatefreak1::JL-->
    </td>
  </tr>  <!-- dataline c2 -->

</table> <!--form pt1 -->
</td></tr>
</table border="0" bgcolor="#dddddd" width="800"> <!--grey background -->
<br>
<table> <!-- form pt2 -->
  <tr>
     <td>
     <input type="submit" name="submt" value="enter command">&nbsp; &nbsp;
     </td>

     <td>
     <a href="/cgi-bin/pics01.cgi">
      <img src="<!--JL::htmlloc::JL-->images/reset.png">
      </a>
     </td>
  </tr>
</table> <!-- form pt2 -->




</font>
</form>
<br>
</p>
</td>

<td width=30%>
&nbsp;
</td>

</tr>

</table> <!-- table 100 -->
<br>
<br>
<br>
<hr>
<u>Print_environ:</u>:<br>
<!--notJL::print_environ::JL-->
<u>cgi vars>:</u><br>
<!--JL::print_cgi_vars::JL-->
<hr>
<a name="edit_section"> </a>
<!--JL::me::JL--><br>
formid=pics01_ans 131005_01<br>
<br>
<br>
<br>
</body>

</html>
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics#
ts7500:/var/www/pics# cat pics01.c
/***********************************************************************\
    pics01.c
        - this program is based on the original freak02.c as a
        starting point. Lot's of extra stuff in here that I haven't
        bothered to delete.


source files:

build procedure:


080921 JL   copied over lostproduct.c to freak.c
130929 JL   copied over to debian linux on a ts-7500 embedded arm processor.
            gcc -ofreak.exe -DTRACE_ON=50 freak02.c
\***********************************************************************/

#include <stdio.h>
#include <string.h>
//  windows only #include <process.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
// windows version #include <Sys/stat.h>
#include <sys/stat.h>

#define  OUTPUT_FILE_SIZE  65536
#define  TRACE_FILENAME  "pics01.trace"
#define  JL_DELIMITER_TAG  "::"
#define  JL_DELIMITER_TAG_LEN  2
#define  JL_START_TAG      "<!--JL::"
#define  JL_START_TAG_LEN  8
#define  JL_END_TAG        "::JL-->"
#define  JL_END_TAG_LEN    7

#define  ANSWER_FILE       "/var/www/pics/pics01_ans.html"
#define  TBD_FILE          "/var/www/pics/tbd01_ans.html"
#define  HTML_LOCATION     "/pics/"

typedef struct {
    char **cgivars;
    char   request_method[22];
    char   status1[512];
    char   status2[512];
    char   alt_file[88];
    int    file_status;
    int    bposting;
} global_type;


char x2c(char *what) ;
void unescape_url(char *url) ;
char ** getcgivars          ( global_type *globals);
int mallocmakeFileToBuffer  ( char *szFile, char **szBuffer, global_type *globals);
int mallocmakeFileToBuffer2 ( char *szFile, char **szBuffer, global_type *globals);
int getvaluecgivar          ( char *szAnsbuffer, char *szCgivarname, global_type *globals );
int outputfreebuffer        ( char *szBuffer, global_type *globals);
int process_tag             ( char *szBuffer, char *szTag, global_type *globals);
int removedoublereturns     ( char *szCgivalue);
int hide_old_data           ( char *szBuffer, global_type *globals);
int make_display_data_from_arrayfield
                            ( char *szBuffer, char *szArraytagname, global_type *globals);


extern char **environ;

int  main()
{
    char        **cgivars ;
    char         *outputbuffer;
    int           i ;
    global_type  *globals;
    char          szPosting[11];

#if TRACE_ON >= 1
{
    FILE           *fileFile;
    fileFile = fopen(TRACE_FILENAME, "w");
    fprintf(fileFile, "hello trace file!! build 131005_03  -JL\n");

    fclose(fileFile);
}
#endif
    globals = malloc( sizeof(global_type) );

    /** First, get the CGI variables into a list of strings         **/
    globals->cgivars =getcgivars(globals);

    globals->bposting = 0;
    strcpy (globals->status1, "Your data is ready for posting!  Use the [post data!] \n");
    strcat (globals->status1, "button just below to complete this process, or to  \n");
    strcat (globals->status1, "change some of the data, use the [re-edit] button. \n ");
    strcat (globals->status1, "To start over, use the [reset] button. \n ");
    strcpy (globals->status2, "Data to post: \n");

    strcpy (globals->alt_file, "");     //not use yet. well see if a param activates it
    globals->file_status = 0;

    getvaluecgivar(szPosting, "posting", globals);
    if (szPosting[0]=='1') {
        globals->bposting = 1;
        strcpy (globals->status1, "Your data will be posted shortly! You are now done. \n");
        strcat (globals->status1, "Please press the [reset] button for the next person. \n");
        strcpy (globals->status2, "Data posted: \n");
    }

    mallocmakeFileToBuffer2(
                 ANSWER_FILE,
                 &outputbuffer,
                 globals
                 );
    if (-123==globals->file_status) { //then use -123 as a flag to uss the alt_file
         if (2<strlen(globals->alt_file)) { //then alt file is set
                free ( outputbuffer);
                mallocmakeFileToBuffer2(
                             globals->alt_file,
                             &outputbuffer,
                             globals
                             );

                }//alt file
         }//then -123

    outputfreebuffer(outputbuffer, globals);


    /** Print the CGI variables sent by the user.  Note the list of **/
    /**   variables alternates names and values, and ends in NULL.  **/

    /** Free anything that needs to be freed **/
    for (i=0; globals->cgivars[i]; i++) free(globals->cgivars[i]);
    free(globals->cgivars);
    free(globals);

                    #if TRACE_ON >= 50
                    {
                        FILE           *fileFile;
                        fileFile = fopen(TRACE_FILENAME, "a");
                        fprintf(fileFile, "main():exit 0\n");
                        fclose(fileFile);
                    }
                    #endif
    exit(0) ;
}  /* main */

/** Convert a two-char hex string into the char it represents **/
char x2c(char *what) {
   register char digit;

   digit = (what[0] >= 'A' ? (( (int)what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
   digit *= 16;
   digit += (what[1] >= 'A' ? (( (int)what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
   return(digit);
}

/** Reduce any %xx escape sequences to the characters they represent **/
void unescape_url(char *url) {
    register int i,j;

    for(i=0,j=0; url[j]; ++i,++j) {
        if((url[i] = url[j]) == '%') {
            url[i] = x2c(&url[j+1]) ;
            j+= 2 ;
        }
    }
    url[i] = '\0' ;
}


/** Read the CGI input and place all name/val pairs into list.        **/
/** Returns list containing name1, value1, name2, value2, ... , NULL  **/
char **getcgivars(global_type  *globals) {
    register int i ;
    int content_length;
    char *cgiinput ;
    char **cgivars ;
    char **pairlist ;
    int paircount ;
    char *nvpair ;
    char *eqpos ;

    /** Depending on the request method, read all CGI input into cgiinput **/
    /** (really should produce HTML error messages, instead of exit()ing) **/
    strcpy(globals->request_method, getenv("REQUEST_METHOD"));
    if (!strcmp(globals->request_method, "GET"  ) ||
        !strcmp(globals->request_method, "HEAD" )       ) {
        cgiinput= strdup(getenv("QUERY_STRING")) ;
    }
    else if (!strcmp(globals->request_method, "POST")) {
        /* strcasecmp() is not supported in Windows-- use strcmpi() instead */
//        if ( strcmpi(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) {
          if ( strcasecmp(getenv("CONTENT_TYPE"), "application/x-www-form-urlencoded")) {
            printf("getcgivars(): Unsupported Content-Type.\n") ;
            exit(1) ;
        }
        if ( !(content_length = atoi(getenv("CONTENT_LENGTH"))) ) {
            printf("getcgivars(): No Content-Length was sent with the POST request.\n") ;
            exit(1) ;
        }
        if ( !(int)(cgiinput= (char *) malloc(content_length+1)) ) {
            printf("getcgivars(): Could not malloc for cgiinput.\n") ;
            exit(1) ;
        }
        if (!fread(cgiinput, content_length, 1, stdin)) {
            printf("Couldn't read CGI input from STDIN.\n") ;
            exit(1) ;
        }
        cgiinput[content_length]='\0' ;
    }
    else {
        printf("getcgivars(): unsupported REQUEST_METHOD\n") ;
        exit(1) ;
    }

    /** Change all plusses back to spaces **/
    for(i=0; cgiinput[i]; i++) if(cgiinput[i] == '+') cgiinput[i] = ' ' ;

    /** First, split on "&" to extract the name-value pairs into pairlist **/
    pairlist= (char **) malloc(256*sizeof(char **)) ;
    paircount= 0 ;
    nvpair= strtok(cgiinput, "&") ;
    while (nvpair) {
        pairlist[paircount++]= strdup(nvpair) ;
        if (!(paircount%256))
            pairlist= (char **) realloc(pairlist,(paircount+256)*sizeof(char **)) ;
        nvpair= strtok(NULL, "&") ;
    }
    pairlist[paircount]= 0 ;    /* terminate the list with NULL */

    /** Then, from the list of pairs, extract the names and values **/
    cgivars= (char **) malloc((paircount*2+1)*sizeof(char **)) ;
    for (i= 0; i<paircount; i++) {
        if (eqpos=strchr(pairlist[i], '=')) {
            *eqpos= '\0' ;
            unescape_url(cgivars[i*2+1]= strdup(eqpos+1)) ;
        } else {
            unescape_url(cgivars[i*2+1]= strdup("")) ;
        }
        unescape_url(cgivars[i*2]= strdup(pairlist[i])) ;
    }
    cgivars[paircount*2]= 0 ;   /* terminate the list with NULL */

    /** Free anything that needs to be freed **/
    free(cgiinput) ;
    for (i=0; pairlist[i]; i++) free(pairlist[i]) ;
    free(pairlist) ;

    /** Return the list of name-value strings **/
    return (cgivars) ;

}


/*---------------------------------------------------------------------*\
    function mallocmakeFileToBuffer()
\*---------------------------------------------------------------------*/
int mallocmakeFileToBuffer
(
    char *szFile,
    char **szBuffer,
    global_type *globals
)
{
    int            bStatus = 0;
    FILE           *fileFile;
    struct stat    sFile;

#if TRACE_ON >= 2
{
    FILE           *fileFile;
    fileFile = fopen(TRACE_FILENAME, "a");
    fprintf(fileFile, "mallocmakeFileToBuffer():begin\n");
    fclose(fileFile);
}
#endif


    if (stat(szFile, &sFile) == 0)
    {
        if (!(*szBuffer = (char *)malloc(sFile.st_size + 1)))
            {
            return bStatus;
            }

        fileFile = fopen(szFile, "r");

        **szBuffer = '\0';
        while (fgets((*szBuffer + strlen(*szBuffer)), 256, fileFile))
        {
        }

        fclose(fileFile);

        bStatus = 1;
    }
    else
    {
        printf("bad stat mallocmakeFile, [%s]<br>\n",szFile);
    }

    return bStatus;

} /* END mallocmakeFileToBuffer */

/*---------------------------------------------------------------------*\
    function mallocmakeFileToBuffer2()
\*---------------------------------------------------------------------*/
int mallocmakeFileToBuffer2
(
    char *szFile,
    char **szBuffer,
    global_type *globals
)
{
    int            bStatus = 0;
    FILE          *fileFile;
    struct stat    sFile;
    char           szTempstring[255];
    char           szTag[55];
    char          *tag_start;
    char          *tag_end;
          #if TRACE_ON >= 2
          {
              FILE           *fileFile;
              fileFile = fopen(TRACE_FILENAME, "a");
              fprintf(fileFile, "mallocmakeFileToBuffer2():begin\n");
              fclose(fileFile);
          }
          #endif


    if (stat(szFile, &sFile) == 0)
    {
        if (!(*szBuffer = (char *)malloc(OUTPUT_FILE_SIZE)))
            {
            return bStatus;
            }

        fileFile = fopen(szFile, "r");

        **szBuffer = '\0';
        while (fgets(szTempstring, 256, fileFile))
        {
            tag_end = szTempstring;
            while (tag_start =strstr(tag_end, JL_START_TAG))
            {
                *tag_start = '\0';
                strcat(*szBuffer,tag_end);
                tag_start += (JL_START_TAG_LEN );
                if (tag_end =strstr(tag_start, JL_END_TAG))
                {
                    *tag_end = '\0';
                    tag_end += (JL_END_TAG_LEN );

                    strcpy(szTag, tag_start);
                    process_tag(*szBuffer, szTag, globals);

                } /*then*/
                else {
                    printf("missing end tag mallocmakeFile2, [%s]<br>\n",szFile);
                } /*else*/
            } /* while */

            strcat(*szBuffer,tag_end);
        } /* while */

        fclose(fileFile);

        bStatus = 1;
    }
    else
    {
          #if TRACE_ON >= 2
          {
              FILE           *fileFile;
              fileFile = fopen(TRACE_FILENAME, "a");
              fprintf(fileFile, "mallocmakeFileToBuffer2():bad stat on open:%s\n"
                 ,szFile);
              fclose(fileFile);
          }
          #endif
        printf("bad stat mallocmakeFile2, [%s]<br>\n",szFile);
    }

    return bStatus;

} /* END mallocmakeFileToBuffer2 */

/*---------------------------------------------------------------------*\
    function outputfreebuffer()
\*---------------------------------------------------------------------*/
int outputfreebuffer (
    char *szBuffer,
    global_type *globals
    )
{

                    #if TRACE_ON >= 2
                    {
                        FILE           *fileFile;
                        fileFile = fopen(TRACE_FILENAME, "a");
                        fprintf(fileFile, "outputfreebuffer():begin\n");
                        fclose(fileFile);
                    }
                    #endif

    /** Print the CGI response header, required for all HTML output. **/
    /** Note the extra \n, to send the blank line.                  **/

                    #if TRACE_ON >= 55
                    {
                        FILE           *fileFile;
                        fileFile = fopen(TRACE_FILENAME, "a");
                        fprintf(fileFile, "outputfreebuffer():szbuffer begin:\n");
                        fprintf(fileFile, "%s%\n",szBuffer);
                        fprintf(fileFile, "outputfreebuffer():szbuffer end\n");
                        fclose(fileFile);
                    }
                    #endif
    printf("Content-type: text/html\n\n") ;

    printf("%s\n<!--JL 10/12/13 11:00am -->\n",szBuffer);
    free ( szBuffer);
                    #if TRACE_ON >= 50
                    {
                        FILE           *fileFile;
                        fileFile = fopen(TRACE_FILENAME, "a");
                        fprintf(fileFile, "outputfreebuffer(): end\n");
                        fclose(fileFile);
                    }
                    #endif

    return 1;

} /* END outputfreebuffer */

/*---------------------------------------------------------------------*\
    function getvaluecgivar()
\*---------------------------------------------------------------------*/
int getvaluecgivar (
    char *szAnsbuffer,
    char *szCgivarname,
    global_type *globals
    )
{
    int i;
    int bFound = 0;

    szAnsbuffer[0] = '\0';

    for (i=0; (globals->cgivars[i])&& (!bFound); i+= 2)
        {
             if (!strcmp(globals->cgivars[i], szCgivarname))
             {
                strcpy(szAnsbuffer, globals->cgivars[i+1]);
                bFound = 1;
             } /* then */
        } /*for*/



    return (bFound);

} /* END getvaluecgivar */

/*---------------------------------------------------------------------*\
    function make_display_data_from_arrayfield()
\*---------------------------------------------------------------------*/
int make_display_data_from_arrayfield
                            ( char *szBuffer, char *szArraytagname, global_type *globals)
{

    time_t      t0;
    char        szDateString[22];
    char        szTimeString[22];
    char        szTempString[1256];
    char        sz2TempString[256];
    char        szdcf[256];
    char        szdwt[256];
    char        szdnt[256];
    char        szdept[256];
    char        szpname[256];
    char        Nametag[11];
    char        szValue[256];
    int         iNametagindex;
    int         iTempint;

    time(&t0);

    strftime( szDateString, sizeof (szDateString), "%y%m%d", localtime(&t0) );
    strftime( szTimeString, sizeof (szTimeString), "%H:%M:%S", localtime(&t0) );

    getvaluecgivar(szdept, "dept", globals);
    getvaluecgivar(szpname, "pname", globals);
    getvaluecgivar(szdcf, "dwicf", globals);
    getvaluecgivar(szdwt, "dwiwt", globals);
    getvaluecgivar(szdnt, "dnote", globals);
    iNametagindex = 1;

    sprintf(Nametag, "%s%d", szArraytagname, iNametagindex);
    getvaluecgivar(szValue, Nametag, globals);
    iTempint = 0;
    sscanf(szValue,"%d", &iTempint);
    while ( iTempint != 0 )  {
        /* the arraytagname has an input, so lets post the data for it. */

        sscanf(szValue,"%d", &iTempint);
        sprintf(szTempString, "%-s %-s ", szDateString, szTimeString);

        sprintf(sz2TempString, "d%-6s ", szdept);
        strcat (szTempString, sz2TempString);

        sprintf(sz2TempString, "n%-10s ", szpname);
        strcat (szTempString, sz2TempString);

        sprintf(sz2TempString, "p%06.6d ", iTempint);
        strcat (szTempString, sz2TempString);

        sprintf(Nametag, "%s%d", "cc", iNametagindex);
        getvaluecgivar(szValue, Nametag, globals);
        iTempint = 0;
        sscanf(szValue,"%d", &iTempint);
        sprintf(sz2TempString, "cc%04.4d ", iTempint);
        strcat (szTempString, sz2TempString);

        sprintf(Nametag, "%s%d", "wicf", iNametagindex);
        getvaluecgivar(szValue, Nametag, globals);
        if (!strcmp(szValue,"-00001") ) {
            strcpy (szValue, szdcf);
            }
        sprintf(sz2TempString, "cf%-6s ", szValue);
        strcat (szTempString, sz2TempString);

        sprintf(Nametag, "%s%d", "wiwt", iNametagindex);
        getvaluecgivar(szValue, Nametag, globals);
        if (!strcmp(szValue,"-00001") ) {
            strcpy (szValue, szdwt);
            }
        sprintf(sz2TempString, "wt%-6s ", szValue);
        strcat (szTempString, sz2TempString);

        sprintf(Nametag, "%s%d", "notes", iNametagindex);
        getvaluecgivar(szValue, Nametag, globals);
        if (!strncmp(szValue,"default note", 12) ) {
            strcpy (szValue, szdnt);
            }
        sprintf(sz2TempString, "nt%-s \n", szValue);
        strcat (szTempString, sz2TempString);

        strcat(szBuffer, szTempString);

        iNametagindex++;
        sprintf(Nametag, "%s%d", szArraytagname, iNametagindex);
        getvaluecgivar(szValue, Nametag, globals);
        iTempint = 0;
        sscanf(szValue,"%d", &iTempint);
    } /* while */

    return(1);
} /* END make_display_data_from_arrayfield */

/*---------------------------------------------------------------------*\
    function hide_old_data()
\*---------------------------------------------------------------------*/
int hide_old_data           ( char *szBuffer, global_type *globals)
{
    char         szTempstring[256];
    int          i;

    for (i=0; globals->cgivars[i]; i+= 2)
        {
            sprintf(szTempstring,"<input name=\"%s\" type=\"hidden\" value=\"%s\" >\n",
            globals->cgivars[i],
            globals->cgivars[i+1]);
            strcat(szBuffer, szTempstring);
        } /*for*/

    return(1);
} /* END hide_old_data */

/*---------------------------------------------------------------------*\
    function removedoublereturns()
\*---------------------------------------------------------------------*/
int removedoublereturns     ( char *szCgivalue)
{
    char  szTempbuf[5096];
    int   iCount;
    char *pszTemp;

    pszTemp = szTempbuf;
    for (iCount=0; szCgivalue[iCount] != 0; iCount++) {
        if ( (szCgivalue[iCount] == '\r') && (szCgivalue[iCount+1] == '\n') )
        ;
        else {
                *pszTemp = szCgivalue[iCount];
                pszTemp++;
             }
        } /* for */
    *pszTemp = 0;

    strcpy(szCgivalue, szTempbuf);

    return(1);
} /* END removedoublereturns */

/*---------------------------------------------------------------------*\
    function process_tag()
\*---------------------------------------------------------------------*/
int process_tag( char *szBuffer, char *szTagparam, global_type *globals)
/************************************************************************
ok, here is where you process the JL:: tags.   you can make up new ones,
and I'm sure a lot of them that are here are old and left over, but are
left as examples of some of the stuff you can do.  add to the nested
if then else structure to process your  instructions that are labeled in
the pics01_ans.html file.
*************************************************************************/
{
    int          ans = 1;
    int          i;
    char         szTempstring[256];
    char         szCgivalue[256];
    char         szCgivariable[256];
    char         szCgivariable2[256];
    char         szTag[256];
    char        *pszTag;
    char        *pszName;
    char        *pszValue;
    char        *pszEnd;
    char        *pszIndex;

    strcpy (szTag, szTagparam);
    if (!strcmp(szTag, "me"))
    {
        strcat(szBuffer,"Copyright 2013. PICS Jonathan Leslie. build 131005_01  To report issues or comment, ");
        strcat(szBuffer,"email me at [email protected] \n");
    }/*then*/
    else if (!strcmp(szTag, "print_cgi_vars"))
         {
            strcat(szBuffer,"<ul>\n");

            /** Print the CGI variables sent by the user.  Note the list of **/
            /**   variables alternates names and values, and ends in NULL.  **/
            for (i=0; globals->cgivars[i]; i+= 2)
            {
                sprintf(szTempstring,"<li>[%s] = [%s]\n",
                        globals->cgivars[i],
                        globals->cgivars[i+1]);
                strcat(szBuffer, szTempstring);
            } /*for*/

            strcat(szBuffer,"</ul>\n");

         } /* then */
    else if (!strcmp(szTag, "print_environ"))
         {
            strcat(szBuffer,"<ul>\n");

            /** Print the environment variables sent by the user.  Note the list of **/
            /**   variables alternates names and values, and ends in NULL.  **/
            for (i=0; environ[i] !=NULL; i++)
            {
                sprintf(szTempstring,"<li>[%s]\n",
                        environ[i]);
                strcat(szBuffer, szTempstring);
            } /*for*/

            strcat(szBuffer,"</ul>\n");

         } /* then */
    else if (!strcmp(szTag, "htmlloc"))
         {
            sprintf(szTempstring,"%s",HTML_LOCATION );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strcmp(szTag, "print_request_method"))
         {
            sprintf(szTempstring,"%s",globals->request_method );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strcmp(szTag, "status1"))
         {
            sprintf(szTempstring,"%s",globals->status1 );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strcmp(szTag, "status2"))
         {
            sprintf(szTempstring,"%s",globals->status2 );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strcmp(szTag, "post_button"))
         {
            strcat(szBuffer,"<input name=\"posting\" type=\"hidden\" value=\"1\" >\n" );
            hide_old_data(szBuffer, globals);

            if (!globals->bposting) {
            strcat(szBuffer,"<input type=\"submit\" name=\"postdata\" value=\"post data!\"> ");
                }/* then */
         } /* then */
    else if (!strcmp(szTag, "done_button"))
         {
            if (globals->bposting) {
                strcat(szBuffer," &nbsp; &nbsp; <a href=\"javascript:window.close()\">\n");
                strcat(szBuffer,"<img src=\"");
                strcat(szBuffer, HTML_LOCATION);
                strcat(szBuffer,"images/done.jpg\" border=\"0\"> </a>\n");
                } /*then posting*/
         } /* then */
    else if (!strcmp(szTag, "data_to_post"))
         {
            make_display_data_from_arrayfield(szBuffer, "prod", globals);
         } /* then */
    else if (!strcmp(szTag, "date"))
         {
            time_t      t0;

            time(&t0);

            strftime( szTempstring, sizeof (szTempstring), "%y%m%d", localtime(&t0) );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strcmp(szTag, "time"))
         {
            time_t      t0;

            time(&t0);

            strftime( szTempstring, sizeof (szTempstring), "%H:%M:%S", localtime(&t0) );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strncmp(szTag, "cgivar",6))
         {
            pszTag =&szTag[6];   /* this tag is of the form cgivarXX:: where XX is the name */
                                 /* in the cgivars array. */
            pszEnd = strstr(pszTag,JL_DELIMITER_TAG);
            pszEnd[0] = '\0';

            getvaluecgivar( szCgivalue, pszTag, globals );

            if ((globals->bposting                      ) &&
                (isdigit(pszTag[strlen(pszTag)-1])      )
               ) {
                 szCgivalue[0]='\0';
                 if (!strncmp(pszTag,"notes", 5) ) {
                    strcpy(szCgivalue,"default note");
                    } /* then this is the note field */
                 } /*then this is an array cgivar */


            sprintf(szTempstring,"%s", szCgivalue );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strncmp(szTag, "calculatefreak",14))
         {
            int    temp_int;
            char   param01[55];
            char   select01[55];

            pszIndex =&szTag[14];   /* this tag is of the form calculatefreakx where X is the index */
                                 /*  of the row to be used for the calculation.                   */

            strcpy(szCgivariable,"prod");
            strcat(szCgivariable,pszIndex);

            getvaluecgivar( szCgivalue, szCgivariable, globals );

                           #if TRACE_ON >= 2
                           {
                               FILE           *fileFile;
                               fileFile = fopen(TRACE_FILENAME, "a");
                               fprintf(fileFile,
                                  "process_tag():calculatefreak pszIndex =>%s<"
                                  " variable/value =%s/%s. \n"
                                  ,pszIndex
                                  ,szCgivariable
                                  ,szCgivalue
                                  );
                               fclose(fileFile);
                           }
                           #endif


            sprintf(szTempstring,"Reserved for Testing. dv=%s. ", szCgivalue );
            strcat(szBuffer, szTempstring);

            strcpy (param01,szCgivalue);

            temp_int =-55;
            sscanf(szCgivalue,"%i", &temp_int);

            strcpy(szCgivariable2,"um");
            strcat(szCgivariable2,pszIndex);

            getvaluecgivar( szCgivalue, szCgivariable2, globals );

            if (-55== temp_int) {

                }
              else {
                   temp_int = temp_int*100;
                   }
            sprintf(szTempstring,"v=%s. "
                                 "a =%d|", szCgivalue,temp_int );
            strcat(szBuffer, szTempstring);

            strcpy (select01,szCgivalue);


            // param01 and select01 are set and ready for processing.
           if ( (!strcmp(param01, "create"))  &&
                (!strcmp(select01, "pmf")  )     )  {

                      FILE           *fileFile;
                      fileFile = fopen("data/pmf_test.txt", "a");
                      fprintf(fileFile, "Creating/appending to the pmf file.\n");
                      fclose(fileFile);

                      strcpy(globals->alt_file,TBD_FILE);
                      globals->file_status = -123;


                      }//then create pmf




         } /* then calculatefreak*/
    else if (!strncmp(szTag, "textareacgivar",14))
         {
            pszTag =&szTag[14];   /* this tag is of the form cgivarXX:: where XX is the name */
                                 /* in the cgivars array. */
            pszEnd = strstr(pszTag,JL_DELIMITER_TAG);
            pszEnd[0] = '\0';

            getvaluecgivar( szCgivalue, pszTag, globals );
            removedoublereturns(szCgivalue);

            if (globals->bposting && isdigit(pszTag[strlen(pszTag)-1]) ) {
                szCgivalue[0]='\0';
                } /*then this is an array cgivar */



            sprintf(szTempstring,"%s", szCgivalue );
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strncmp(szTag, "radiocgivar",11))
         {
            pszName =&szTag[11];   /* this tag is of the form radiocgivarXX::yyy:: where XX is
                                    the name of the cgi variable in the cgivars array, and yyy is the
                                    value of the XX. */
                #if TRACE_ON >= 2
                {
                    FILE           *fileFile;
                    fileFile = fopen(TRACE_FILENAME, "a");
                    fprintf(fileFile, "process_tag(): pszTagparam =>%s<.\n",szTagparam);
                    fprintf(fileFile, "process_tag(): szTag =>%s<.\n",szTag);
                    fclose(fileFile);
                }
                #endif
            pszEnd = strstr(pszName,JL_DELIMITER_TAG);
            pszEnd[0] = '\0';
            pszValue =pszEnd + JL_DELIMITER_TAG_LEN;
            pszEnd = strstr(pszValue, JL_DELIMITER_TAG);
            pszEnd[0] = '\0';

            getvaluecgivar( szCgivalue, pszName, globals );

            if (globals->bposting && isdigit(pszName[strlen(pszTag)-1]) ) {
                szCgivalue[0]='\0';
                } /*then this is an array cgivar */


            if (!strcmp(szCgivalue, pszValue))
            {
                sprintf(szTempstring,"%s", "checked" );
            }
            else {
                sprintf(szTempstring,"%s", "       " );
            }
            strcat(szBuffer, szTempstring);
         } /* then */
    else if (!strncmp(szTag, "selectcgivar",12))
         {
            pszName =&szTag[12];   /* this tag is of the form selectcgivarXX::yyy:: where XX is
                                    the name of the cgi variable in the cgivars array, and yyy is the
                                    value of the XX. */
            pszEnd = strstr(pszName,JL_DELIMITER_TAG);
            pszEnd[0] = '\0';
            pszValue =pszEnd + JL_DELIMITER_TAG_LEN;
            pszEnd = strstr(pszValue, JL_DELIMITER_TAG);
            pszEnd[0] = '\0';

            getvaluecgivar( szCgivalue, pszName, globals );

                #if TRACE_ON >= 2
                {
                    FILE           *fileFile;
                    fileFile = fopen(TRACE_FILENAME, "a");
                    fprintf(fileFile, "process_tag():selectcgivar pszName =>%s< cgivalue=>%s< value=>%s<.\n"
                                      ,pszName, szCgivalue, pszValue);
                    fclose(fileFile);
                }
                #endif

            if (globals->bposting && isdigit(pszName[strlen(pszName)-1]) ) {
                strcpy(szCgivalue,"-00001");
                } /*then this is an array cgivar */

            if (!strcmp(szCgivalue, pszValue))
            {
                sprintf(szTempstring,"%s", "selected" );
            }
            else {
                sprintf(szTempstring,"%s", "        " );
            }
            strcat(szBuffer, szTempstring);
         } /* then */
    else {
        sprintf(szTempstring,"process_tag(): bad tag value [%s]<br>\n",szTag);
        strcat(szBuffer, szTempstring);
        ans = 0;
    }/* else */

    return (ans);
} /* END process_tag */

ts7500:/var/www/pics#
 



>________________________________
> From: Tom Collins <tom-lnEA/wrDJtNWk0Htik3J/[email protected]>
>To: [email protected] 
>Sent: Monday, January 27, 2014 11:38 AM
>Subject: Re: [rabbit-semi] Sample program for parsing data  and getting response from web server using get and post
> 
>
>
>
>
>Irfan,
>
>
>What's wrong with the samples provided with Dynamic C?  Which ones have you tried?  There is an HTTP client library with Dynamic C, and samples that show doing a GET on a remote server, and sending data with a POST.
>
>
>-Tom
>
>
>On Jan 26, 2014, at 11:37 PM, <sweetmazhar85-/[email protected]> wrote:
>I am new to dynamic c  and I am using rabbit 6710. I am totally stuck with code for get and post to send request to remote web server and get xml response from it. Can any one please help me in getting a sample code(Not the one in digi/samples). Any practical project code.. just example will do.
>
>
>
>
> 
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.