Exemplo n.º 1
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;
}
Exemplo n.º 2
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;
}