Advertisement
Guest User

bstamp_tokenize.cpp

a guest
Mar 5th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. /*
  2.    bstamp_tokenize.cpp
  3.  
  4.    This interfaces with Parallax, Inc.'s PBASIC Tokenizer Shared Library for
  5.    the Linux operating system.
  6.  
  7.    It reads a PBASIC source file and writes the tokenized results to a file
  8.    which can then be sent to the BASIC Stamp microcontroller using the
  9.    "bstamp_run" program.
  10.  
  11.    Based on "stampapp.cpp" example from the "PBASIC_Tokenizer.tar.gz" archive,
  12.    (c) 2002 Parallax, Inc.  All rights reserved.
  13.    http://www.parallax.com/
  14.  
  15.    Added more UI and stdin/stdout support so that it can be used in a pipe
  16.    Francis Esmonde-White
  17.    francis@esmonde-white.com
  18.    http://www.esmonde-white.com
  19.    May 12, 2004
  20.  
  21.    Cleaned up and UI added by Bill Kendrick.
  22.    bill@newbreedsoftware.com
  23.    http://www.newbreedsoftware.com/
  24.  
  25.    April 7, 2003 - April 11, 2003
  26.  
  27.    PBASIC is a trademark and BASIC Stamp is a registered trademark of
  28.    Parallax, Inc.
  29. */
  30.  
  31.  
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <dlfcn.h>
  36.  
  37. #include <fcntl.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40.  
  41. #include "tokenizer.h"
  42.  
  43.  
  44. /* Globals: */
  45.  
  46. TModuleRec *ModuleRec;
  47. char Source[MaxSourceSize];
  48.  
  49. /* --- MAIN --- */
  50.  
  51. int main(int argc, char * argv[])
  52. {
  53.   int fd,i;
  54.   int io_choice=-1;
  55.   void *hso;
  56.   char *error;
  57.  
  58.  
  59.  
  60.   /* Check for proper arguments: */
  61.   if (argc == 2)
  62.   {
  63.     fprintf(stdout, "2 arguments!\n");
  64.     if (argv[1][0]=='-')
  65.     {
  66.         if (argv[1][1]=='h'|argv[1][1]=='?')
  67.         {
  68.             fprintf(stderr, "\nBasic Stamp Linux Tokenizer\n\n");
  69.             fprintf(stderr, "This program is used to tokenize a basic stamp 2 source file.\n");
  70.             fprintf(stderr, "For more information about the Basic Stamp, see http://www.parallax.com\n\n");
  71.             fprintf(stderr, "This is a modified version of bstamp available at http://bstamp.sourceforge.net\n");
  72.             fprintf(stderr, "Options:\n");
  73.             fprintf(stderr, "\t-h or -? : this help listing\n");
  74.             fprintf(stderr, "\t-v : the program version\n");
  75.             fprintf(stderr, "\t-a : about the program (brief history)\n");
  76.             fprintf(stderr, "\tnote: standard UNIX pipes are now implemented!\n");
  77.             fprintf(stderr, "\t  to use stdin do not include any other arguments.\n\n");
  78.             fprintf(stderr, "Standard Usage: bstamp_tokenize INFILE.BS2 OUTFILE.TOK\n");
  79.             fprintf(stderr, " Or:\n cat INFILE.BS2 | bstamp_tokenize | OUTFILE.TOK \n Or:\n cat INFILE.BS2 | bstamp_tokenizer | bstamp_run\n");
  80.             exit(0);
  81.         }
  82.         else if (argv[1][1]=='v')
  83.         {
  84.             fprintf(stderr, "bstamp_tokenizer version 2004-05-12\n current filename: ");
  85.             fprintf(stderr, argv[0]);
  86.             fprintf(stderr, "\n");
  87.             exit(0);
  88.         }
  89.         else if (argv[1][1]=='a')
  90.         {
  91.             fprintf(stderr, "Modified by Francis Esmonde-White [francis@esmonde-white.com] 2004-05-12\n");
  92.             fprintf(stderr, "\tAdded POSIX pipes functionality\n");
  93.             fprintf(stderr, "\tnow using STDIN as the .BS2 file location if no arguments are given\n");
  94.             fprintf(stderr, "\tnow using STDOUT as the .TOK file location if no arguments are given\n");
  95.             exit(0);
  96.         }
  97.         else
  98.         {
  99.             fprintf(stderr, "%s: invalid option -- %s\n", argv[0], argv[1]);
  100.             fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
  101.             exit(EXIT_FAILURE);
  102.         }
  103.     }
  104.   }
  105.   else if (argc == 1) // use stdin and stdout
  106.   {
  107.     io_choice=1;
  108.   }
  109.   else if (argc == 3) // use given input and output files
  110.   {
  111.     io_choice=2;
  112.   }
  113.   else
  114.   {
  115.     fprintf(stderr, "%s: invalid option -- %s\n", argv[0], argv[1]);
  116.     fprintf(stderr, "%Try '%s -h' for more information.\n", argv[0]);
  117.     exit(EXIT_FAILURE);
  118.   }
  119.  
  120.  
  121.   /* Open shared library: */
  122.  
  123.  
  124.   /* (Function prototypes): */
  125.  
  126.   STDAPI (*GetVersion)(void);
  127.   STDAPI (*CompileIt)(TModuleRec *, char *Src, bool DirectivesOnly,
  128.               bool ParseStampDirectives, TSrcTokReference *Ref);
  129.   STDAPI (*doTestRecAlignment)(TModuleRec *);
  130.  
  131.  
  132.   /* (Open the .so; first try in $LD_LIBRARY_PATH) */
  133.  
  134.   hso = dlopen("libbstamptokenizer.so", RTLD_LAZY);
  135.   if (!hso)
  136.   {
  137.     perror(dlerror());
  138.  
  139.     hso = dlopen("./tokenizer.so", RTLD_LAZY);
  140.     if (!hso)
  141.     {
  142.       perror(dlerror());
  143.       exit(EXIT_FAILURE);
  144.     }
  145.   }
  146.  
  147.  
  148.   /* (Map functions in tokenizer.so) */
  149.  
  150.   GetVersion= (STDAPI(*)(void))dlsym(hso,"Version");
  151.   CompileIt= (STDAPI(*)(TModuleRec *, char *Src, bool DirectivesOnly,
  152.             bool ParseStampDirectives, TSrcTokReference *Ref))dlsym(hso,"Compile");
  153.   doTestRecAlignment= (STDAPI(*)(TModuleRec *))dlsym(hso,"TestRecAlignment");
  154.  
  155.  
  156.   /* (Test for any errors) */
  157.  
  158.   error = dlerror();
  159.  
  160.   if (error != NULL)
  161.   {
  162.     perror(error);
  163.     dlclose(hso);
  164.     exit(EXIT_FAILURE);
  165.   }
  166.  
  167.  
  168.   /* Allocate TModuleRec */
  169.   ModuleRec = (TModuleRec *)malloc(sizeof(TModuleRec));
  170.  
  171.   /* Display version of tokenizer */
  172.   fprintf(stderr, "PBASIC Tokenizer Library version %1.2f\n\n",
  173.       (float)(GetVersion())/100);
  174.  
  175.   /* Call TestRecAlignment and display the results of the TModuleRec fields. */
  176.   doTestRecAlignment(ModuleRec);
  177.  
  178.  
  179. if  (io_choice==1) // IO from stdin and stdout
  180. {
  181.   ModuleRec->SourceSize = fread(Source, 1, sizeof(Source), stdin);
  182.   // ModuleRec->SourceSize = read(fd, Source, sizeof(Source));
  183.   Source[ModuleRec->SourceSize] = '\0';
  184.  
  185.   /* Tokenize the code: */
  186.  
  187.   CompileIt(ModuleRec, Source, false, true, NULL);
  188.  
  189.   /* Close shared library: */
  190.  
  191.   dlclose(hso);
  192.  
  193.   /* Did it succeed? */
  194.  
  195.   if (!ModuleRec->Succeeded)
  196.   {
  197.         fprintf(stderr, "Error: Tokenization of %s failed.\n", argv[1]);
  198.     fprintf(stderr, "Failed compile: %s\n", ModuleRec->Error);
  199.     fprintf(stderr, "The following text gave the error: ");
  200.     for (i=0; i < ModuleRec->ErrorLength; i++)
  201.         putchar( (int) *(Source + ModuleRec->ErrorStart + i) );
  202.     putchar((int) '\n');
  203.         exit(-1);
  204.   }
  205.  
  206.   /* Save the tokenized results to a file: */
  207.  
  208.   fwrite(ModuleRec->PacketBuffer, 1, ModuleRec->PacketCount * 18, stdout);
  209.   // write(fd, ModuleRec->PacketBuffer, ModuleRec->PacketCount * 18);
  210.  
  211. }
  212. else // IO from files
  213. {
  214.   /* Load source code from file: */
  215.  
  216.   fd = open(argv[1], O_RDONLY);
  217.   if (fd == -1)
  218.   {
  219.     printf("Can't open %s\n", argv[1]);
  220.     exit(EXIT_FAILURE);
  221.   }
  222.  
  223.   ModuleRec->SourceSize = read(fd, Source, sizeof(Source));
  224.   Source[ModuleRec->SourceSize] = '\0';
  225.  
  226.   close(fd);
  227.  
  228.  
  229.   /* Tokenize the code: */
  230.  
  231.   CompileIt(ModuleRec, Source, false, true,NULL);
  232.  
  233.  
  234.   /* Close shared library: */
  235.  
  236.   dlclose(hso);
  237.  
  238.  
  239.   /* Did it succeed? */
  240.  
  241.   if (!ModuleRec->Succeeded)
  242.   {
  243.         fprintf(stderr, "Error: Tokenization of %s failed.\n", argv[1]);
  244.     fprintf(stderr, "Failed compile: %s\n", ModuleRec->Error);
  245.     fprintf(stderr, "The following text gave the error: ");
  246.     for (i=0; i < ModuleRec->ErrorLength; i++)
  247.         putchar( (int) *(Source + ModuleRec->ErrorStart + i) );
  248.     putchar((int) '\n');
  249.         exit(-1);
  250.   }
  251.  
  252.  
  253.   /* Save the tokenized results to a file: */
  254.  
  255.   fd = open(argv[2], O_CREAT | O_TRUNC | O_WRONLY, 0644);
  256.  
  257.   if (fd == -1)
  258.   {
  259.     fprintf(stderr, "Error: Can't create %s\n", argv[2]);
  260.     exit(EXIT_FAILURE);
  261.   }
  262.  
  263.   write(fd, ModuleRec->PacketBuffer, ModuleRec->PacketCount * 18);
  264.  
  265.   close(fd);
  266. } // End of file IO
  267.  
  268.  
  269.   /* All done! */
  270.  
  271.   return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement