Advertisement
Guest User

bstamp_tokenize.cpp

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