Example #1
0
static void instrument_file(const char * source_file, const char * destination_file, const char * id, int instrumenting) {
  /* check if they are the same */
  char * canonical_source_file = make_canonical_path(source_file);
  char * canonical_destination_file = make_canonical_path(destination_file);
  check_same_file(canonical_source_file, canonical_destination_file);
  free(canonical_source_file);
  free(canonical_destination_file);

  if (instrumenting) {
    enum FileType file_type = get_file_type(source_file);
    switch (file_type) {
    case FILE_TYPE_OTHER:
    case FILE_TYPE_HTML:
      if (g_verbose) {
        printf("Copying file %s\n", id);
      }
      copy_file(source_file, destination_file);
      break;
    case FILE_TYPE_JS:
      {
        if (g_verbose) {
          printf("Instrumenting file %s\n", id);
        }

        FILE * input = xfopen(source_file, "rb");
        FILE * output = xfopen(destination_file, "wb");

        Stream * input_stream = Stream_new(0);
        Stream * output_stream = Stream_new(0);

        Stream_write_file_contents(input_stream, input);

        /*
        Check if the source file looks like an instrumented JavaScript file.
        */
        if (input_stream->length >= JSCOVERAGE_INSTRUMENTED_HEADER_LENGTH &&
            memcmp(input_stream->data, JSCOVERAGE_INSTRUMENTED_HEADER, JSCOVERAGE_INSTRUMENTED_HEADER_LENGTH) == 0) {
          fatal_command_line("file %s in the source directory appears to be already instrumented", id);
        }

        size_t num_characters = input_stream->length;
        uint16_t * characters = NULL;
        int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters);
        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
          fatal("encoding %s not supported", jscoverage_encoding);
        }
        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
          fatal("error decoding %s in file %s", jscoverage_encoding, id);
        }
        jscoverage_instrument_js(id, characters, num_characters, output_stream);
        free(characters);

        if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) {
          fatal("cannot write to file: %s", destination_file);
        }

        Stream_delete(input_stream);
        Stream_delete(output_stream);

        fclose(input);
        fclose(output);
      }
      break;
    }
  }
  else {
    if (g_verbose) {
      printf("Copying file %s (on --no-instrument list)\n", id);
    }

    copy_file(source_file, destination_file);
  }
}
int main(int argc, char ** argv) {
  int verbose = 0;

  // program = argv[0];
  program = "jscoverage";

  char * source = NULL;
  char * destination = NULL;

  char ** no_instrument = xnew(char *, argc - 1);
  int num_no_instrument = 0;

  char ** exclude = xnew(char *, argc - 1);
  int num_exclude = 0;
  
  jscoverage_highlight = false;

  for (int i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
      copy_resource_to_stream("jscoverage-help.txt", stdout);
      exit(EXIT_SUCCESS);
    }
    else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) {
      version();
    }
    else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
      verbose = 1;
    }
    else if (strcmp(argv[i], "--mozilla") == 0) {
      jscoverage_mozilla = true;
      jscoverage_set_js_version("180");
    }
    else if (strcmp(argv[i], "--no-instrument") == 0) {
      i++;
      if (i == argc) {
        fatal_command_line("--no-instrument: option requires an argument");
      }
      no_instrument[num_no_instrument] = argv[i];
      num_no_instrument++;
    }
    else if (strncmp(argv[i], "--no-instrument=", 16) == 0) {
      no_instrument[num_no_instrument] = argv[i] + 16;
      num_no_instrument++;
    }
    else if (strcmp(argv[i], "--exclude") == 0) {
      i++;
      if (i == argc) {
        fatal_command_line("--exclude: option requires an argument");
      }
      exclude[num_exclude] = argv[i];
      num_exclude++;
    }
    else if (strncmp(argv[i], "--exclude=", 10) == 0) {
      exclude[num_exclude] = argv[i] + 10;
      num_exclude++;
    }
    else if (strcmp(argv[i], "--encoding") == 0) {
      i++;
      if (i == argc) {
        fatal_command_line("--encoding: option requires an argument");
      }
      jscoverage_encoding = argv[i];
    }
    else if (strncmp(argv[i], "--encoding=", 11) == 0) {
      jscoverage_encoding = argv[i] + 11;
    }
    else if (strcmp(argv[i], "--js-version") == 0) {
      i++;
      if (i == argc) {
        fatal_command_line("--js-version: option requires an argument");
      }
      jscoverage_set_js_version(argv[i]);
    }
    else if (strncmp(argv[i], "--js-version=", 13) == 0) {
      jscoverage_set_js_version(argv[i] + 13);
    }
    else if (strncmp(argv[i], "-", 1) == 0) {
      fatal_command_line("unrecognized option `%s'", argv[i]);
    }
    else if (source == NULL) {
      source = argv[i];
    }
    else if (destination == NULL) {
      destination = argv[i];
    }
    else {
      fatal_command_line("too many arguments");
    }
  }

  if (source == NULL || destination == NULL) {
    fatal_command_line("missing argument");
  }

  source = make_canonical_path(source);
  destination = make_canonical_path(destination);

  jscoverage_init();
  jscoverage_instrument(source, destination, verbose, exclude, num_exclude, no_instrument, num_no_instrument);
  jscoverage_cleanup();

  free(source);
  free(destination);
  free(exclude);
  free(no_instrument);

  exit(EXIT_SUCCESS);
}