Example #1
0
static void
compile(char *base_name)
{
  /* symbol table */
  state.global = push_symbol_table(state.global, 1);

  init_nodes();
  state.root = NULL;
  
  /* open and parse the source public file */
  state.src = NULL;
  open_src(state.basefilename, source_with);
  if (state.src)
    yyparse();

  /* open input file */
  open_src(state.srcfilename, source);

  /* parse the input file */
  yyparse();

  /* optimize the intermediate code */
  /* optimize(); */

  /* open output filename */
  strcpy(state.asmfilename, base_name);
  strcat(state.asmfilename, ".asm");
  state.output.f = fopen(state.asmfilename, "w");
  if (state.output.f == NULL) {
    perror(state.asmfilename);
    exit(1);
  }

  /* write the assembly output */
  write_asm();
  fclose(state.output.f);

  /* destory symbol table for the current module */
  state.global = pop_symbol_table(state.global);

  /* free all the memory */
  free_nodes();

  return;
}
Example #2
0
int main( int argc, char *argv[] )
{
  extern char *optarg;
  extern int optind;
  int c;
  int usage = 0;
  int memory_dump = 0;
  int dos_newlines = 0;
  char *pc;
  int  cmd_processor = 0;
  char *processor_name = NULL;

  state.i_memory = i_memory_create();
  
  state.pass = 0;
  state.quiet = 0;

  #ifdef PARSE_DEBUG
  {
    extern int yydebug;
    yydebug = 1; /* enable parse debug */
  }
  #endif

  #ifdef USE_GPASM_HEADER_PATH
    /* add the header path to the include paths list */
    #ifndef __MSDOS__
      include_paths[n_include_paths++] = GPASM_HEADER_PATH;
    #else
      include_paths[n_include_paths++] = "c:\\gputils\\header";    
    #endif
  #endif

  while ((c = GETOPT_FUNC) != EOF) {
    switch (c) {
    case '?':
    case 'h':
      usage = 1;
      break;
    case 'a':
      select_hexformat(optarg);
      state.cmd_line.hex_format = 1;
      break;
    case 'c':
      state.case_insensitive = 1;
      if (state.stDefines != NULL) {
        printf("Warning: The -c option must be called before the -d option.\n");
      }
      break;
    case 'D':
    case 'd':
      if ((optarg != NULL) && (strlen(optarg) > 0)) {
	struct symbol *sym;
	char *lhs, *rhs;

        /* the Defines symbol table is not yet defined*/
        if (state.stDefines == NULL) {
          state.stDefines = push_symbol_table(NULL, state.case_insensitive);
        }

	lhs = strdup(optarg);
	rhs = strchr(lhs, '=');
	if (rhs != NULL) {
	  *rhs = '\0';	/* Terminate the left-hand side */
	  rhs++;	/* right-hand side begins after the '=' */
	}

	sym = get_symbol(state.stDefines, lhs);
	if (sym == NULL)
	  sym = add_symbol(state.stDefines, lhs);
	if (rhs)
	  annotate_symbol(sym, rhs);
      }
      break;
    case 'e':
      select_expand(optarg);
      state.cmd_line.macro_expand = 1;
      break;
    case 'I':
       if(n_include_paths < MAX_INCLUDE_PATHS) {
 	 include_paths[n_include_paths++] = optarg;
       } else {
 	 fprintf(stderr, "too many -I paths\n");
 	 exit(1);
       }
       break;    
 
    case 'l':
      dump_processor_list();
      exit(0);
      break;
    case 'm':
      memory_dump = 1;
      break;
    case 'n':
      #ifndef __MSDOS__
        dos_newlines = 1;
      #endif
      break;
    case 'o':
	    strcpy(state.hexfilename, optarg);
	    strcpy(state.basefilename, optarg);
	    pc = strrchr(state.basefilename, '.');
	    if (pc)
		   *pc = 0;
      break;
    case 'p':
      cmd_processor = 1;
      processor_name = optarg;
      break;
    case 'q':
      state.quiet = 1;
      break;
    case 'r':
      select_radix(optarg);
      state.cmd_line.radix = 1;
      break;
    case 'w':
      select_errorlevel(atoi(optarg));
      state.cmd_line.error_level = 1;
      break;
    case 'v':
      fprintf(stderr, "%s\n", GPASM_VERSION_STRING);
      exit(0);
      
    }
    if (usage)
      break;
  }
  
  if (optind < argc)
    state.srcfilename = argv[optind];
  else
    usage = 1;

  if (usage) {
    show_usage();
  }

  if(state.basefilename[0] == '\0') {
	  strcpy(state.basefilename, state.srcfilename);
	  pc = strrchr(state.basefilename, '.');
	  if (pc)
		  *pc = 0;
  }

  /* the Defines symbol table is not yet defined*/
  if (state.stDefines == NULL) {
    state.stDefines = push_symbol_table(NULL, state.case_insensitive);
  }

  /* Builtins are always case insensitive */
  state.stBuiltin = push_symbol_table(NULL, 1);
  state.stDirective = state.stBuiltin;
  state.stMacros = push_symbol_table(NULL, state.case_insensitive);
  state.stTop = 
    state.stGlobal = push_symbol_table(NULL, state.case_insensitive);

  opcode_init(0);

  /* the tables are built, select the processor if -p was used */
  if (cmd_processor) {
    select_processor(processor_name);
    state.cmd_line.processor = 1;
  }

  state.maxram = (MAX_RAM - 1);

  open_src(state.srcfilename, 0);
  state.pass = 1;
  yyparse();
 
  open_src(state.srcfilename, 0);
  state.pass++;
  state.org = 0;
  state.cblock = 0;
  if (state.cmd_line.radix != 1)
    state.radix = 16;
  cod_init();
  lst_init();
  yyparse();

  assert(state.pass == 2);
  
  pop_symbol_table(state.stBuiltin);
  
  if (check_writehex(state.i_memory, state.hex_format)) {
    gperror(GPE_IHEX,NULL); 
  } else {
    int byte_words;
    
    if (state.device.core_size > 0xff) {
      byte_words = 0;
    } else {
      byte_words = 1;
      if (state.hex_format != inhx8m) {
        gpwarning(GPW_UNKNOWN,"Must use inhx8m format for EEPROM8");
        state.hex_format = inhx8m;
      }
    }
    
    if (writehex(state.basefilename, state.i_memory, 
                 state.hex_format, state.num.errors,
                 byte_words, dos_newlines)) {
      gperror(GPE_UNKNOWN,"Error generating hex file");
    }
  }

  if(memory_dump)
    print_i_memory(state.i_memory);

  /* Maybe produce a symbol table */
  if (state.lst.symboltable) {
    lst_throw(); /* Start symbol table on a fresh page */
    lst_symbol_table(state.stGlobal);
    lst_defines_table(state.stDefines);
  }

  /* Maybe produce a memory map */
  if (state.lst.memorymap) {
    lst_memory_map(state.i_memory);
  }
  
  /* Finish off the listing and symbol files*/
  lst_close();
  if (state.processor_info)
    cod_close_file();
  free_files();

  if (state.num.errors > 0)
    return 1;
  else
    return 0;
}
Example #3
0
int main (int argc, char **argv){
	/* extract program name from path. e.g. /path/to/MArCd -> MArCd */
	const char* separator = strrchr(argv[0], '/');
	if ( separator ){
		program_name = separator + 1;
	} else {
		program_name = argv[0];
	}

  /* setup capture header */
  struct cap_header cp;
  strncpy(cp.nic, "CONV", CAPHEAD_NICLEN);
  gethostname(cp.mampid, 8);

  /* defaults */
  char* comments = strdup("converted from pcap with pcap2cap-" CAPUTILS_VERSION);
  stream_addr_t dst = STREAM_ADDR_INITIALIZER;

  int op;
  int option_index;
  while ( (op = getopt_long  (argc, argv, shortopts, longopts, &option_index)) != -1 ){
    switch (op){
    case 'c':
      comments = strdup(optarg);
      break;

    case 'm':
      strncpy(cp.mampid, optarg, 8);
      break;

    case 'i': /* --iface */
      strncpy(cp.nic, optarg, CAPHEAD_NICLEN);
      cp.nic[CAPHEAD_NICLEN-1] = 0; /* force null-terminator */
      break;

    case 'l':
      caplen = atoi(optarg);
      break;

    case 'o':
      stream_addr_str(&dst, optarg, 0);
      break;

    case 'q': /* --quiet */
	    quiet = 1;
	    break;

    case 'h':
	    show_usage();
      return 0;
      break;
    default:
      printf ("?? getopt returned character code 0%o ??\n", op);
    }
  }

  /* open input/output */
  pcap_t* pcap = open_src(argc, argv, &cp);
  stream_t st = open_dst(&dst, &cp, comments);
  if ( !(pcap && st) ){
	  return 1; /* error already shown */
  }

  /* comment is no longer needed */
  free(comments);
  comments = NULL;

  /* setup signal handler so it can handle ctrl-c etc with proper closing of streams */
  signal(SIGINT, sighandler);

  const u_char* packet;
  struct pcap_pkthdr pcapHeader;
  unsigned long long pktCount = 0;
  while ( (packet=pcap_next(pcap, &pcapHeader)) && run ){
    cp.ts.tv_sec  = pcapHeader.ts.tv_sec;  /* Copy and convert the timestamp provided by PCAP, assumes _usec. If nsec will be present adjust! */
    cp.ts.tv_psec = pcapHeader.ts.tv_usec * 1e6;
    cp.len = pcapHeader.len; /* The Wire-lenght of the frame */
    cp.caplen = min(pcapHeader.caplen, caplen);

    // Let the user know that we are alive, good when processing large files.
    if ( !quiet && pktCount++ % 1000 == 0 ) {
      fprintf(stderr, ".");
      fflush(stderr);
    }

    // Save a copy of the frame to the new file.
    int ret;
    if ( (ret=stream_write_separate(st, &cp, packet, cp.caplen)) != 0 ) {
	    fprintf(stderr, "stream_write(..) returned %d: %s\n", ret, caputils_error_string(ret));
    }
  }

  /* Release resources */
  stream_close(st);
  stream_addr_reset(&dst);
  pcap_close(pcap);

  if ( !quiet ){
	  fprintf(stderr, "\n%s: There was a total of %lld packets converted.\n", program_name, pktCount);
  }

  return 0;
}
Example #4
0
int
assemble(void)
{
  char *pc; 
  struct symbol_table *cmd_defines;

  /* store the command line defines to restore on second pass */
  cmd_defines = state.stDefines;
  state.c_memory = state.i_memory = i_memory_create();

  if(state.basefilename[0] == '\0') {
    strcpy(state.basefilename, state.srcfilename);
    pc = strrchr(state.basefilename, '.');
    if (pc)
      *pc = 0;
  }

  /* Builtins are always case insensitive */
  state.stBuiltin = push_symbol_table(NULL, 1);
  state.stDirective = state.stBuiltin;
  state.stMacros = push_symbol_table(NULL, state.case_insensitive);
  state.stTop = 
    state.stGlobal = push_symbol_table(NULL, state.case_insensitive);
  state.stTopDefines = 
    state.stDefines = push_symbol_table(cmd_defines, state.case_insensitive);

  opcode_init(0);

  /* the tables are built, select the processor if -p was used */
  if (cmd_processor) {
    select_processor(processor_name);
    state.cmd_line.processor = 1;
  }

  state.pass = 1;
  open_src(state.srcfilename, 0);
  yyparse();
 
  state.pass++;
  state.org = 0;
  state.cblock = 0;
  /* clean out defines for second pass */
  state.stTopDefines = 
    state.stDefines = push_symbol_table(cmd_defines, state.case_insensitive);
  if (state.cmd_line.radix != 1)
    state.radix = 16;
  state.obj.symbol_num = 0;
  state.obj.section_num = 0;
  state.obj.org_num = 0;
  state.found_config = 0;
  state.found_devid = 0;
  state.found_idlocs = 0;
  coff_init();
  cod_init();
  lst_init();
  open_src(state.srcfilename, 0);
  yyparse();

  assert(state.pass == 2);
  
  pop_symbol_table(state.stBuiltin);
  
  hex_init();

  if(state.memory_dump)
    print_i_memory(state.i_memory, 
                   state.device.class == PROC_CLASS_PIC16E ? 1 : 0);

  /* Maybe produce a symbol table */
  if (state.lst.symboltable) {
    lst_throw(); /* Start symbol table on a fresh page */
    lst_symbol_table(state.stGlobal);
    lst_defines_table(state.stDefines);
  }

  /* Maybe produce a memory map */
  if ((state.mode == absolute) && (state.lst.memorymap)) {
    lst_memory_map(state.i_memory);
  }
  
  /* Finish off the object, listing, and symbol files*/
  coff_close_file();
  lst_close();
  if (state.processor_info)
    cod_close_file();
  free_files();

  if ((state.num.errors > 0) ||
      (gp_num_errors > 0))
    return EXIT_FAILURE;
  else
    return EXIT_SUCCESS;
}