Exemple #1
0
/**
 * @short Generates the necesary data to the output stream.
 */
void parse_file(const char *prefix, const char *filename, FILE * outfd,
                onion_assets_file * assets) {
  FILE *fd = fopen(filename, "r");
  if (!fd) {
    fprintf(stderr, "ERROR: Cant open file %s: ", filename);
    perror("");
    onion_assets_file_free(assets);
    exit(3);
  }
  char *fname = funcname(prefix, basename((char *)filename));
  char buffer[4096];
  buffer[4095] = 0;

  snprintf(buffer, sizeof(buffer) - 1,
           "onion_connection_status %s(void *_, onion_request *req, onion_response *res);",
           fname);

  onion_assets_file_update(assets, buffer);

  fprintf(stderr, "Parsing: %s to '%s'.\n", filename, buffer);
  fprintf(outfd,
          "onion_connection_status %s(void *_, onion_request *req, onion_response *res){\n  static const char data[]={\n",
          fname);
  int r, i, l = 0;
  while ((r = fread(buffer, 1, sizeof(buffer) - 1, fd)) != 0) {
    for (i = 0; i < r; i++) {
      fprintf(outfd, "0x%02X, ", buffer[i] & 0x0FF);
      if ((i % 16) == 15) {
        fprintf(outfd, "\n");
      }
    }
    l += r;
  }
  fprintf(outfd, "};\n");

  const char *mime_type = onion_mime_get(filename);
  fprintf(outfd, "  onion_response_set_length(res, %d);\n", l);
  fprintf(outfd,
          "  onion_response_set_header(res, \"Content-Type\", \"%s\");\n",
          mime_type);
  fprintf(outfd,
          "  return onion_response_write(res, data, sizeof(data));\n}\n\n");

  fprintf(outfd, "const unsigned int %s_length = %d;\n\n", fname, l);

  fclose(fd);
  free(fname);
}
Exemple #2
0
int main(int argc, char **argv) {
  if (argc == 1)
    print_help(argv[0]);
  int i;
  char *outfile = NULL;
  char *assetfile = "assets.h";
  // First pass cancel out the options, let only the files
  for (i = 1; i < argc; i++) {
    if (strcmp(argv[i], "--help") == 0)
      print_help();
    else if (strcmp(argv[i], "-o") == 0) {
      if (i >= argc - 1) {
        fprintf(stderr, "ERROR: Need an argument for -o");
        exit(2);
      }
      outfile = argv[i + 1];
      argv[i] = NULL;           // cancel them out.
      argv[i + 1] = NULL;
      i++;
    } else if (strcmp(argv[i], "-a") == 0) {
      if (i >= argc - 1) {
        fprintf(stderr, "ERROR: Need an argument for -a");
        exit(2);
      }
      assetfile = argv[i + 1];
      argv[i] = NULL;           // cancel them out.
      argv[i + 1] = NULL;
      i++;
    }
  }

  FILE *outfd = stdout;
  if (outfile) {
    outfd = fopen(outfile, "w");
    if (!outfd) {
      perror("ERROR: Could not open output file");
      exit(2);
    }
  }
  onion_assets_file *assets = onion_assets_file_new(assetfile);

  // Some header...
  fprintf(outfd, "/** File autogenerated by opack **/\n\n");
  fprintf(outfd, "#include <onion/request.h>\n\n");
  fprintf(outfd, "#include <onion/response.h>\n\n");
  fprintf(outfd, "#include <string.h>\n\n");

  for (i = 1; i < argc; i++) {
    if (argv[i]) {
      struct stat st;
      stat(argv[i], &st);
      if (S_ISDIR(st.st_mode)) {
        parse_directory(basename(argv[1]), argv[i], outfd, assets);
      } else {
        parse_file("", argv[i], outfd, assets);
      }
    }
  }

  if (outfile) {
    fclose(outfd);
  }
  onion_assets_file_free(assets);

  return 0;
}
int main(int argc, char **argv){
	// Add some plugin searhc paths
	plugin_search_path=list_new(free);

	const char *infilename=NULL;
	const char *outfilename=NULL;
	char tmp[256];
	char *assetfilename="assets.h";

	int i;
	for (i=1;i<argc;i++){
		if (strcmp(argv[i], "--help")==0){
			help(NULL);
			return 0;
		}
		else if ((strcmp(argv[i], "--templatetagsdir")==0) || (strcmp(argv[i], "-t")==0)){
			i++;
			if (argc<=i){
				help("Missing templatedir name");
				return 3;
			}
			snprintf(tmp, sizeof(tmp), "%s/lib%%s.so", argv[i]);
			ONION_DEBUG("Added templatedir %s", tmp);
			list_add(plugin_search_path, strdup(tmp)); // dup, remember to free later.
		}
		else if ((strcmp(argv[i], "--no-orig-lines")==0) || (strcmp(argv[i], "-n")==0)){
			use_orig_line_numbers=0;
			ONION_DEBUG("Disable original line numbers");
		}
		else if ((strcmp(argv[i], "--asset-file")==0) || (strcmp(argv[i], "-a")==0)){
			i++;
			if (argc<=i){
				help("Missing assets file name");
				return 3;
			}
			assetfilename=argv[i];
			ONION_DEBUG("Assets file: %s", assetfilename);
		}
		else{
			if (infilename){
				if (outfilename){
					help("Too many arguments");
					return 1;
				}
				outfilename=argv[i];
				ONION_DEBUG("Set outfilename %s", outfilename);
			}
			else{
				infilename=argv[i];
				ONION_DEBUG("Set infilename %s", infilename);
			}
		}
	}
	
	if (!infilename || !outfilename){
		help("Missing input or output filename");
		return 2;
	}

	if (strcmp(infilename,"-")==0){
		infilename="";
	}
	else{
		char tmp2[256];
		strncpy(tmp2, argv[1], sizeof(tmp2)-1);
		snprintf(tmp, sizeof(tmp), "%s/lib%%s.so", dirname(tmp2));
		list_add(plugin_search_path, strdup(tmp));
		strncpy(tmp2, argv[1], sizeof(tmp2)-1);
		snprintf(tmp, sizeof(tmp), "%s/templatetags/lib%%s.so", dirname(tmp2));
		list_add(plugin_search_path, strdup(tmp));
	}

	// Default template dirs
	list_add_with_flags(plugin_search_path, "lib%s.so", LIST_ITEM_NO_FREE);
	list_add_with_flags(plugin_search_path, "templatetags/lib%s.so", LIST_ITEM_NO_FREE);
	char tmp2[256];
	strncpy(tmp2, argv[0], sizeof(tmp2)-1);
	snprintf(tmp, sizeof(tmp), "%s/templatetags/lib%%s.so", dirname(tmp2));
	list_add(plugin_search_path, strdup(tmp)); // dupa is ok, as im at main.
	strncpy(tmp2, argv[0], sizeof(tmp2)-1);
	snprintf(tmp, sizeof(tmp), "%s/lib%%s.so", dirname(tmp2));
	list_add(plugin_search_path, strdup(tmp)); // dupa is ok, as im at main.
	list_add_with_flags(plugin_search_path, "/usr/local/lib/otemplate/templatetags/lib%s.so", LIST_ITEM_NO_FREE);
	list_add_with_flags(plugin_search_path, "/usr/lib/otemplate/templatetags/lib%s.so", LIST_ITEM_NO_FREE);

	onion_assets_file *assetsfile=onion_assets_file_new(assetfilename);
	int error=work(infilename, outfilename, assetsfile);
	onion_assets_file_free(assetsfile);
	
	list_free(plugin_search_path);
	
	return error;
}