VOLGIT



LOG | FILES | OVERVIEW


#ifndef VOLGIT_SYSTEM_PART_C
#define VOLGIT_SYSTEM_PART_C VOLGIT_SYSTEM_PART_C
#include <system_part.h>

const char *const default_header="<!DOCTYPE html><html><head><title>GIT</title></head><body>";
const char *const default_footer="</body></html>";

struct Volgit_State state
			=
	{
		.input_path=NULL,
		.output_path=NULL,
		.header=default_header,
		.footer=default_footer,
		.output_dir_fd=-1,
		.tree_cutoff=NO_TREE_CUTOFF,
		.output_is_tree_like=0,
	};

const char *usage=
"Options: \n"
"	--input <repository path>\n"
"	--output <name of directory where output is going to be stored>\n"
"	--header <file containing content which is prepended to all generated files>\n"
"	--footer <file containing content which is appended to all generated files>\n"
"	--tree \n"
"		Use 'tree' like output for directory index\n"
"	--tree-cutoff <number>\n"
"		The tree output can get too bulky\n"
"		so you can set a depth at which the output\n"
"		is seperated into a different file\n"
"	-h --help\n"
"		Print this message\n";

static inline int get_next_option_or_die(int i,int argc,const char *error_message)
{
	if(i+1>=argc)
		die(error_message);
	return i+1;
}

void parse_options(int argc,char **argv)
{
#define if_option(option) if(!strncmp(argv[i],option,sizeof(option)))
	int i;
	if(argc==1)
		die(usage);

	for(i=1;i<argc;++i)
	{
		if_option("--output")
		{
			i=get_next_option_or_die(i,argc,"expected a file name after --output flag\n");

			state.output_path=argv[i];
			state.output_dir_fd=open(state.output_path,O_RDONLY|O_DIRECTORY);

			if(state.output_dir_fd==-1)
			{
				mkdir(state.output_path,0775);
				state.output_dir_fd=open(state.output_path,O_RDONLY|O_DIRECTORY);
				if(state.output_dir_fd==-1)
					die("Could not open output dir\n");
			}
		}else if_option("--input")
		{
			i=get_next_option_or_die(i,argc,"expected a file name after --input flag\n");
			state.input_path=argv[i];
		}else if_option("--header")
		{
			i=get_next_option_or_die(i,argc,"expected an argument after --header flag\n");	
			state.header=read_file(argv[i]);	
		}else if_option("--footer")
		{
			i=get_next_option_or_die(i,argc,"expected an argument after --footer flag\n");	
			state.footer=read_file(argv[i]);
		}else if_option("--tree")
		{
			state.output_is_tree_like=1;
		}else if_option("--tree-cutoff")
		{
			i=get_next_option_or_die(i,argc,"expected an argument after --tree-cutoff flag\n");	
			if(sscanf(argv[i],"%d",&state.tree_cutoff)!=1)
				die("expected a number after the --tree-cutoff flag\n");
			if(state.tree_cutoff>=MAX_CUTOFF-1)
				die("cutoff number is too large. Max cutoff is %d\n",MAX_CUTOFF);
		}else if_option("-h")
		{
			die(usage);
		}else if_option("--help")
		{
			die(usage);
		}else
		{
			die("Invalid command argument '%s'. Try using -h\n",argv[i]);	
		}

	}

	if(state.input_path==NULL)
		die("You need to specify an input path\n");
	if(state.output_path==NULL)
		die("You need to specify an output path\n");

#undef if_option
}
char* read_file(const char *filename)
{
	long file_size;
	int file;
	char *ret;
	struct stat file_stats;

	file=open(filename,O_RDONLY);
	if(file==-1)
		die("Could not open file %s\n",filename);

	fstat(file,&file_stats);

	ret=malloc(file_stats.st_size+1);

	if(ret==NULL)
		die("Ran out of memory!\n");

	read(file,ret,file_stats.st_size);
	ret[file_stats.st_size]='\0';

	close(file);


	return ret;
}

int create_branch_dir(const char *branch_name)
{
	int ret=0;

	ret=openat(state.output_dir_fd,branch_name,O_DIRECTORY|O_RDONLY);	
	if(ret==-1)
	{
		if(mkdirat(state.output_dir_fd,branch_name,0775)==-1)
			die("Could not create branch directory for %s\n",branch_name);

		ret=openat(state.output_dir_fd,branch_name,O_DIRECTORY|O_RDONLY);	
		if(ret==-1)
			die("Could not open newly created directory for branch %s\n",branch_name);
	}

	return ret;
}
FILE* create_file(int branch_dir,const char *filename)
{
	int fd;
	char *new_filename;
	size_t old_filename_len;
	FILE *ret;

	old_filename_len=strlen(filename);
	new_filename=calloc(old_filename_len+sizeof(".html"),1);
	if(new_filename==NULL)
		die("Ran out of memory!\n");

	memcpy(new_filename,filename,old_filename_len);
	memcpy(new_filename+old_filename_len,".html",sizeof(".html"));

	unlinkat(branch_dir,new_filename,0);
	fd=openat(branch_dir,new_filename,O_CREAT|O_RDWR,0664);
	if(fd==-1)
		die("Could not open the file '%s' for one of the branches\n",filename);

	free(new_filename);

	ret=fdopen(fd,"w");

	fprintf(ret,"%s",state.header);
	return ret;
}
void close_file(FILE *file)
{
	fprintf(file,"%s",state.footer);
	fclose(file);
}
int create_dir(int base_dir,const char *dir_name)
{
	int ret;
	ret=openat(base_dir,dir_name,O_DIRECTORY|O_RDONLY);	
	if(ret!=-1)
		return ret;

	mkdirat(base_dir,dir_name,0775);
	ret=openat(base_dir,dir_name,O_DIRECTORY|O_RDONLY);	
	if(ret==-1)
		die("Could not create the 'files' subdirectory of one of the branch dirs\n");

	return ret;
}

void die(const char *format, ...)
{
	va_list args;
	va_start(args,format);
	vfprintf(stderr,format,args);
	exit(1);
}
void push_html_link_for_blob(FILE *out,const char *filename,const char *oid)
{
	fprintf(out,"<a href=\"%s.html\" class=\"blob_entry\">%s</a>",oid,filename);	
}
void push_html_link_for_tree(FILE *out,const char *filename,const char *oid)
{
	fprintf(out,"<b><a href=\"%s/index.html\" class=\"tree_entry\">%s</a></b>",oid,filename);	
}
#endif