Advertisement
Guest User

Untitled

a guest
Mar 11th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  assembly
  4. //
  5. //  Created by Ethan Laur on 3/11/14.
  6. //  Copyright (c) 2014 Ethan Laur. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <queue>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include "Symtab.h"
  14.  
  15. #define DBGOUT(a); if (MDEBUG) { a; }
  16. #define MDEBUG 0x01
  17. #define MAX_LINE_LENGTH 1024
  18.  
  19. Symtab *symtab;
  20. typedef std::queue<char *> lineQueue;
  21.  
  22. lineQueue * readlines(char *buf)
  23. {
  24.     lineQueue *ret = new lineQueue;
  25.     char *str = strdup((const char *)buf);
  26.     char *tok = strtok(str, "\n");
  27.     do
  28.     {
  29.         ret->push(tok);
  30.     }  while ((tok = strtok(NULL, "\n")) != NULL);
  31.     return ret;
  32. }
  33.  
  34. char *getlinefromqueue(lineQueue * in)
  35. {
  36.     if (in->size() <= 0) return NULL;
  37.     char *str = (char *)malloc(MAX_LINE_LENGTH);
  38.     strcpy(str, in->front());
  39.     in->pop();
  40.     return str;
  41. }
  42.  
  43. void build_symtab(char *buf)
  44. {
  45.     lineQueue *lq = readlines(buf);
  46.     char *line, tmp[30], sym_name[28], sym_name_tmp[28];
  47.     int curr_location = 0, i;
  48.     char *repl = NULL, rbuf[30];
  49.     while ((line = getlinefromqueue(lq)) != NULL)
  50.     {
  51.         if (sscanf(line, "%s", tmp) > 0)
  52.         {
  53.             if (tmp[0] == ':')
  54.             {
  55.                 for (i = 1; i < 28 && tmp[i] != ':'; i++); //i now points to the next colon
  56.                 strncpy(sym_name_tmp, tmp + 1, i - 1); //grab the symbol
  57.                 sprintf(rbuf, "%s", sym_name_tmp);
  58.                 DBGOUT(printf("[DEBUG build_symtab] Inserting symbol %s to location 0x%04x\n",
  59.                             sym_name, curr_location););
  60.                 sprintf(sym_name, "$%s", sym_name_tmp);
  61.                 symtab->insertNode(sym_name, curr_location);
  62.                 //while ((repl = strstr(buf, rbuf)) != NULL)
  63.                 //{
  64.                 //  repl[0] = ';';
  65.                 //}
  66.             }
  67.             else if (tmp[0] == ';') {} //ignore comments
  68.             else //we have an instruction
  69.             {
  70.                 curr_location += 4;
  71.             }
  72.         }
  73.         free(line);
  74.     }
  75.     delete lq;
  76.     if (line != NULL)
  77.         free(line);
  78. }
  79.  
  80. char * insert_symtab(char *buf)
  81. {
  82.     char *ret = (char *)malloc(strlen(buf) + 512);
  83.     char *tmp = NULL;
  84.     char *repl = NULL;
  85.     char obuf[32]; //for converting int to hex
  86.     Symtab_struct *cursym;
  87.     lineQueue *lq = readlines(buf);
  88.     int i;
  89.     strcpy(ret, "");
  90.     while (!lq->empty())
  91.     {
  92.         tmp = getlinefromqueue(lq);
  93.         for (i = 0; (cursym = symtab->findNodeByNumber(i)) != NULL; i++)
  94.         {
  95.             sprintf(obuf, "0x%04x", cursym->sym_location);
  96.             //printf("-->looking for %s\n", cursym->sym_name);
  97.             if ((repl = strstr(tmp, "$start")) != NULL)
  98.             {
  99.                 printf("---->Found %s\n", cursym->sym_name);
  100.                 strncpy (repl, obuf, strlen(cursym->sym_name));
  101.             }
  102.         }
  103.         strcat(ret, tmp);
  104.         strcat(ret, "\n");
  105.     }
  106.     return ret;
  107. }
  108.  
  109. char *process_includes(char *buf)
  110. {
  111.     char *ret = NULL;
  112.     char *tmplstr = (char *)malloc(MAX_LINE_LENGTH);
  113.     char *tmpstr = (char *)malloc(30);
  114.     char *finpstr = (char *)malloc(MAX_LINE_LENGTH);
  115.     char *includepos = NULL;
  116.     FILE *f = NULL;
  117.     FILE *o = NULL;
  118.     struct stat st;
  119.     lineQueue *lq = readlines(buf);
  120.     lineQueue *tmplq = new lineQueue; //for reading files
  121.     ssize_t retsize;
  122.     int read_ret, num_inc = 0;
  123.     while (!lq->empty())
  124.     {
  125.         strcpy(tmplstr, lq->front());
  126.         lq->pop();
  127.         if ((includepos = strstr(tmplstr, "%include")) != NULL)
  128.         {
  129.             sscanf(tmplstr, "%%include '%s'", tmpstr);
  130.             tmpstr[strlen(tmpstr) - 1] = 0; //trim the '
  131.             if ((f = fopen(tmpstr, "r")) == NULL)
  132.             {
  133.                 printf("[WARN process_includes] Cannot find file included %s\n", tmpstr);
  134.             }
  135.             else
  136.             {
  137.                 read_ret = 0;
  138.                 while (read_ret != EOF)
  139.                 {
  140.                     int sp;
  141.                     for (sp = 0; sp < MAX_LINE_LENGTH - 1 && (read_ret = fscanf(f, "%c", &finpstr[sp])) != EOF;sp++);
  142.                     finpstr[sp] = 0;
  143.                     tmplq->push(finpstr);
  144.                 }
  145.                 if ((o = fopen("/tmp/as.tmp", "a")) == NULL)
  146.                 {
  147.                     if ((o = fopen("/tmp/as.tmp", "w")) == NULL)
  148.                     {
  149.                         printf("[ERR process_includes] Cannot write temporary files\n");
  150.                         exit(1);
  151.                     }
  152.                 }
  153.                 while (!tmplq->empty())
  154.                 {
  155.                     fprintf(o, "%s", tmplq->front());
  156.                     tmplq->pop();
  157.                 }
  158.                 fflush(o);
  159.                 fclose(o);
  160.                 ++num_inc;
  161.             }
  162.         }
  163.     }
  164.     if (stat("/tmp/as.tmp", &st) == 0)
  165.         retsize = strlen(buf) + st.st_size + 256;
  166.     else
  167.         retsize = strlen(buf) + 256;
  168.     ret = (char *)malloc(retsize);
  169.     memset(ret, 0, retsize);
  170.     strcpy(ret, buf);
  171.     if (num_inc > 0)
  172.     {
  173.         if ((o = fopen("/tmp/as.tmp", "r")) == NULL)
  174.         {
  175.             printf("[ERR process_includes] Cannot read temproary files\n");
  176.             exit(1);
  177.         }
  178.         fread(ret + strlen(buf), st.st_size, 1, o);
  179.         fclose(o);
  180.     }
  181.     unlink("/tmp/as.tmp");
  182.     while ((includepos = strstr(ret, "%include")) != NULL)
  183.     {
  184.         includepos[0] = ';';
  185.     }
  186.     //delete tmplq;
  187.     //delete lq;
  188.     return ret;
  189. }
  190.  
  191. int main(int argc, const char * argv[])
  192. {
  193.     symtab = new Symtab();
  194.     FILE *f = fopen(argv[1], "rb");
  195.     char buf[1024] = {0};
  196.     int sp = 0;
  197.     for (sp = 0; fscanf(f, "%c", &buf[sp]) != EOF; sp++);
  198.     char *buf2 = process_includes(buf);
  199.     build_symtab(buf2);
  200.     char *buf3 = insert_symtab(buf2);
  201.     printf("%s\n", buf3);
  202. }
  203.  
  204. //:start:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement