home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 April / PCWorld_2004-04_cd.bin / software / temacd / remotany / RemotelyAnywhere.msi / File.sma < prev    next >
Text File  |  2003-08-19  |  6KB  |  255 lines

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //                                                                            //
  3. // RemotelyAnywhere Sample 'SMALL' Scripts                                    //
  4. //                                                                            //
  5. // RemotelyAnywhere provides a mechanism for scripting via a built-in         //
  6. // language. The language is called Small, and more information about         //
  7. // it can be found at http://compuphase.com/small.htm.                        //
  8. //                                                                            //
  9. // Before you use these sample scripts, you should tailor their               //
  10. // behavior to better suit your configuration and your needs.                 //
  11. //                                                                            //
  12. // This script demonstrates a simple form that takes user input and then      //
  13. // processes it via a callback function.                                      //
  14. //                                                                            //
  15. // THIS SOFTWARE IS PROVIDED BY 3AM LABS LTD ``AS IS'' AND                    //
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE      //
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE //
  18. // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE    //
  19. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL //
  20. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS    //
  21. // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)      //
  22. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT //
  23. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  //
  24. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF     //
  25. // SUCH DAMAGE.                                                               //
  26. //                                                                            //
  27. ////////////////////////////////////////////////////////////////////////////////
  28.  
  29. #include <ra>
  30.  
  31. //
  32. // Main program. Print out the form that will take input from the user.
  33. //
  34.  
  35. public main()
  36. {
  37.     // Begin output
  38.     
  39.     htmlBeginOutput("File handling");
  40.     
  41.     // Draw a dialog box
  42.     
  43.     htmlBeginDialog("Dump file");
  44.     
  45.     // Start an input form
  46.     
  47.     htmlBeginForm();
  48.  
  49.     // Set up the input form
  50.     
  51.     htmlWrite("<TABLE><TR><TD><!FFONT!>Dump using</TD></TR><TR><TD><!FFONT!>");
  52.     htmlRadioButton("type", "1", true);
  53.     htmlWrite("Byte<BR></TD></TR><TR><TD><!FFONT!>");
  54.     htmlRadioButton("type", "2");
  55.     htmlWrite("Word<BR></TD></TR><TR><TD><!FFONT!>");
  56.     htmlRadioButton("type", "4");
  57.     htmlWrite("Double word<BR></TD></TR></TABLE><BR><TABLE><TR><TD><!FFONT!>Width:</TD><TD>");
  58.     htmlEdit("width", "16");
  59.     htmlWrite("</TD></TR><TR><TD><!FFONT!>File:</TD><TD><!FFONT!>");
  60.     htmlEdit("file");
  61.     htmlWrite("</TD></TR></TABLE><BR>");
  62.     
  63.     // Set up the submit button. It will call the function dump() in this script.
  64.     
  65.     htmlButton("Dump", "dump");
  66.     
  67.     // Finish the form
  68.     
  69.     htmlWrite("    ");
  70.     htmlButtonBack("Back", false);
  71.  
  72.     htmlEndForm();
  73.     htmlEndDialog();
  74.     htmlEndOutput();
  75. }
  76.  
  77. //
  78. // The dump() callback function.
  79. //
  80.  
  81. public dump()
  82. {
  83.  
  84.     //
  85.     // Start output
  86.     //
  87.     
  88.     htmlBeginOutput("File handling");
  89.  
  90.     new param[64]="", f, g, w;
  91.  
  92.     //
  93.     // Read the 'type' parameter
  94.     //
  95.     
  96.     htmlGetParam("type", param);
  97.     g=atoi(param);
  98.     
  99.     //
  100.     // Read the 'width' parameter
  101.     //
  102.     
  103.     htmlGetParam("width", param);
  104.     w=atoi(param);
  105.  
  106.     //
  107.     // Try to open the file specified in the 'file' parameter
  108.     //
  109.     
  110.     if (g!=0 && htmlGetParam("file", param) &&
  111.         (f=fopen(param, FILE_READ)))
  112.     {
  113.         new buf[1024], out[4], format[2];
  114.         new rd, p=0;
  115.  
  116.         //
  117.         // Set up the page header
  118.         //
  119.         
  120.         sprintf(buf, "Dump of \"%s\": <BR><BR></CENTER><PRE>", param);
  121.         htmlWrite(buf);
  122.  
  123.         //
  124.         // Create the format string according to the output type
  125.         // (byte, word, dword)
  126.         //
  127.         
  128.         if (g==1) {
  129.             sprintf(format, "%%02x ");
  130.         } else if (g==2) {
  131.             sprintf(format, "%%04x ");
  132.         } else {
  133.             sprintf(format, "%%08x ");
  134.         }
  135.  
  136.  
  137.         //
  138.         // Attempt to read from the file
  139.         //
  140.         
  141.         rd=fread(f, buf, 4096);
  142.  
  143.         //
  144.         // Main loop
  145.         //
  146.         
  147.         do {        
  148.             new i=0, j, k, t, val;
  149.  
  150.             //
  151.             // Loop until we run out of bytes in the buffer
  152.             //
  153.             
  154.             while (i<rd) {
  155.             
  156.                 //
  157.                 // Display the offset
  158.                 //
  159.                 
  160.                 sprintf(out, "%08x:  ", p);
  161.                 htmlWrite(out);
  162.  
  163.                 //
  164.                 // Display each byte, word or dword in the buffer
  165.                 //
  166.                 
  167.                 for (j=0; j<w*g; j+=g) {
  168.                 
  169.                     //
  170.                     // Convert the value to word or dword if
  171.                     // neccessary
  172.                     //
  173.                     
  174.                     val=0;
  175.                     for (k=0; k<g && i+j+k<rd; k++) {
  176.                         t=gb(buf, i+j+k);
  177.                         t<<=k*8;
  178.                         val+=t;
  179.                     }
  180.  
  181.                     //
  182.                     // If end of buffer
  183.                     //
  184.                     
  185.                     if (k==0) {
  186.                         if (g==1) {
  187.                             strcpy(out, "   ");
  188.                         } else if (g==2) {
  189.                             strcpy(out, "     ");
  190.                         } else {
  191.                             strcpy(out, "         ");
  192.                         }
  193.                     } else {
  194.                     
  195.                         //
  196.                         // Display the hex dump
  197.                         //
  198.                         
  199.                         sprintf(out, format, val);
  200.                     }
  201.  
  202.                     htmlWrite(out);
  203.                 }
  204.  
  205.                 htmlWrite("  ");
  206.  
  207.                 //
  208.                 // Display the ASCII representation of the HEX data
  209.                 //
  210.                 
  211.                 out{1}=0;
  212.                 for (j=0; j<w*g && i+j<rd; j++) {
  213.                     val=gb(buf, i+j);
  214.  
  215.                     if (val>=32 && val<128)
  216.                         out{0}=val;
  217.                     else
  218.                         out{0}='.';
  219.  
  220.                     // The output needs to be converted
  221.                     // to html format according to the
  222.                     // special characters that might
  223.                     // appear so we pass "true" in the
  224.                     // "htmlize" parameter overriding
  225.                     // its default value
  226.  
  227.                     htmlWrite(out, true);
  228.                 }
  229.  
  230.                 htmlBR();
  231.  
  232.                 i+=j;
  233.                 p+=j;
  234.             }
  235.  
  236.             //
  237.             // Loop until we reach the end of the file
  238.             //
  239.             
  240.             rd=fread(f, buf, 4096);
  241.             
  242.         } while (rd>0);
  243.  
  244.         fclose(f);
  245.  
  246.         htmlWrite("</PRE><BR><CENTER>");
  247.     } else {
  248.         htmlWrite("<BR>File not found!<BR>");
  249.     }
  250.  
  251.     htmlButtonBack();
  252.  
  253.     htmlEndOutput();
  254. }
  255.