begin new main in C
This commit is contained in:
		
							parent
							
								
									2fcc59270b
								
							
						
					
					
						commit
						30f97cf955
					
				
							
								
								
									
										183
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										183
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,183 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include "fs.h"
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include "hashtable.h"
 | 
			
		||||
 | 
			
		||||
#define _(x) x
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void free_filelist(struct filelist * list) {
 | 
			
		||||
	if (list == NULL) {
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	struct filelist * next = list->next;
 | 
			
		||||
 | 
			
		||||
	while(list->next) {
 | 
			
		||||
		next = list->next;
 | 
			
		||||
		free(next->path);
 | 
			
		||||
		free(next);
 | 
			
		||||
 | 
			
		||||
		list->next = list->next->next;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	free(list->path);
 | 
			
		||||
	free(list);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int filelist_push(struct filelist * pos, const char * path) {
 | 
			
		||||
	struct filelist * new = malloc(sizeof(struct filelist));
 | 
			
		||||
	if (!new) {
 | 
			
		||||
		errno = ENOMEM;
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	new->next = pos->next;
 | 
			
		||||
	new->path = strdup(path);
 | 
			
		||||
 | 
			
		||||
	pos->next = new;
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int good(FILE * file) {
 | 
			
		||||
	return !feof(file);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char fgetc_count(FILE *file, int * line, int * pos) {
 | 
			
		||||
	char temp = fgetc(file);
 | 
			
		||||
	if (temp == '\n') {
 | 
			
		||||
		(*line)++;
 | 
			
		||||
		*pos = 0;
 | 
			
		||||
	} else {
 | 
			
		||||
		(*pos)++;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return temp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int add_char(char ** str, char c) {
 | 
			
		||||
	if (str == NULL || *str == NULL) {
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	int len = strlen(*str);
 | 
			
		||||
 | 
			
		||||
	char *temp = realloc(*str, len + 1);
 | 
			
		||||
	if (temp == NULL) {
 | 
			
		||||
		errno = ENOMEM;
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
	*str = temp;
 | 
			
		||||
 | 
			
		||||
	(*str)[len-1] = c;
 | 
			
		||||
	(*str)[len] = 0;
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void eval_macro(struct filelist * last, FILE * file) {
 | 
			
		||||
	char * name = NULL;
 | 
			
		||||
	strhash_t hash = 0;
 | 
			
		||||
 | 
			
		||||
	char current = 0;
 | 
			
		||||
 | 
			
		||||
	// read macro name
 | 
			
		||||
	while(good(file) && current != '\\') {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (!good(file)) {
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void eval_file(struct filelist * last) {
 | 
			
		||||
	// open file
 | 
			
		||||
	FILE * file = fopen(last->path, "r");
 | 
			
		||||
 | 
			
		||||
	if (file == NULL) {
 | 
			
		||||
		fprintf(stderr, _("could not open file \"%s\":%s\n"), last->path, strerror(errno));
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	printf("processing file: %s\n", last->path);
 | 
			
		||||
 | 
			
		||||
	char current = 1;
 | 
			
		||||
	while(good(file)) {
 | 
			
		||||
		current = fgetc(file);
 | 
			
		||||
 | 
			
		||||
		switch(current) {
 | 
			
		||||
		case '%':
 | 
			
		||||
			// comment, skip line
 | 
			
		||||
			do {
 | 
			
		||||
				current = fgetc(file);
 | 
			
		||||
			} while(good(file) && current != '\n');
 | 
			
		||||
 | 
			
		||||
			break;
 | 
			
		||||
		case '\\':
 | 
			
		||||
			// macro
 | 
			
		||||
			eval_macro(last, file);
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			printf(_("unrecognized char! %c\n"), current);
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	fclose(file);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include <getopt.h>
 | 
			
		||||
 | 
			
		||||
struct option long_opts[] = {
 | 
			
		||||
	{"output", required_argument, NULL, 'o'},
 | 
			
		||||
	{"target", required_argument, NULL, 't'},
 | 
			
		||||
	{}, // terminator
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void help(int argc, char ** args) {
 | 
			
		||||
	printf("%s [OPTIONS] file1 file2...\n"
 | 
			
		||||
			"--output\n"
 | 
			
		||||
			"-o\tby default the program will create a depfile for every input\n"
 | 
			
		||||
			"\tnaming it like the tex file with a .d extension. By giving the -o Option\n"
 | 
			
		||||
			"\tthe output will instead be put in this file exculsivly\n"
 | 
			
		||||
			"--target\n"
 | 
			
		||||
			"-t\tOverride the target name\n", args[0]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char ** args) {
 | 
			
		||||
	int long_index = 0;
 | 
			
		||||
	int opt = 0;
 | 
			
		||||
 | 
			
		||||
	const char *outfile, *target;
 | 
			
		||||
 | 
			
		||||
	while((opt = getopt_long(argc, args, "o:t:", long_opts, &long_index)) != -1) {
 | 
			
		||||
		switch(opt) {
 | 
			
		||||
		case 'o':
 | 
			
		||||
			outfile = optarg;
 | 
			
		||||
			break;
 | 
			
		||||
		case 't':
 | 
			
		||||
			target = optarg;
 | 
			
		||||
			break;
 | 
			
		||||
		case '?':
 | 
			
		||||
			help(argc, args);
 | 
			
		||||
			return 0;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	char * current_file = NULL;
 | 
			
		||||
 | 
			
		||||
	// process all files
 | 
			
		||||
	for(;optind < argc; ++optind) {
 | 
			
		||||
		current_file = args[optind];
 | 
			
		||||
 | 
			
		||||
		struct filelist list;
 | 
			
		||||
		list.next = NULL;
 | 
			
		||||
		list.path = current_file;
 | 
			
		||||
 | 
			
		||||
		eval_file(&list);
 | 
			
		||||
		free_filelist(list.next);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user