PageRenderTime 254ms CodeModel.GetById 121ms app.highlight 10ms RepoModel.GetById 121ms app.codeStats 0ms

/parsleyc_main.c

http://github.com/fizx/parsley
C | 108 lines | 91 code | 16 blank | 1 comment | 5 complexity | 21663bddae8059c65fbfd7bd76f595f3 MD5 | raw file
  1#include <stdio.h>
  2#include <stdlib.h>
  3#include <argp.h>
  4#include <string.h>
  5#include <json/printbuf.h>
  6#include "parsley.h"
  7#include "util.h"
  8
  9struct list_elem {
 10	int has_next;
 11	struct list_elem *next;
 12	char *string;
 13};
 14
 15struct arguments
 16{
 17	struct list_elem *include_files;
 18	char *parsley;
 19  char *output_file;
 20};
 21
 22const char *argp_program_version = "parsleyc 0.1";
 23const char *argp_program_bug_address = "<kyle@kylemaxwell.com>";
 24static char args_doc[] = "DEX_FILE";
 25static char doc[] = "Parsleyc is a parselet to XSLT compiler";
 26
 27static struct argp_option options[] = {
 28  {"debug",    'd', 0, 0, 	"Turn on Bison parser debugging" },
 29	{"output",   'o', "FILE", 0, 	"Output to FILE instead of standard output" },
 30  {"include",  'i', "FILE", 0, 	"Include the contents of FILE in the produced XSLT" },
 31  { 0 }
 32};
 33
 34static error_t parse_opt (int key, char *arg, struct argp_state *state)
 35{
 36  struct arguments *arguments = state->input;
 37	struct list_elem *base = arguments->include_files;
 38	struct list_elem *e;
 39
 40  switch (key)
 41    {
 42    case 'i':
 43			e = (struct list_elem *) calloc(1, sizeof(e));
 44			e->string = arg;
 45			while(base->has_next) base = base->next;
 46			base->next = e;
 47			base->has_next = 1;
 48      break;
 49    case 'd':	
 50			// parsley_set_debug_mode(1);
 51			break;
 52    case 'o':
 53      arguments->output_file = arg;
 54      break;
 55    case ARGP_KEY_ARG:
 56      if (state->arg_num >= 1) argp_usage (state);
 57      arguments->parsley = arg;
 58      break;
 59    case ARGP_KEY_END:
 60      if (state->arg_num < 1) argp_usage (state);
 61      break;
 62    default:
 63      return ARGP_ERR_UNKNOWN;
 64    }
 65  return 0;
 66}
 67
 68static struct argp argp = { options, parse_opt, args_doc, doc };
 69
 70int main (int argc, char **argv) {
 71	struct arguments arguments;
 72	struct list_elem elem;
 73	struct list_elem *elemptr = &elem;
 74	elem.has_next = 0;
 75	
 76	arguments.include_files = elemptr;
 77	arguments.output_file = "-";
 78	arguments.parsley = "-";
 79	argp_parse (&argp, argc, argv, 0, 0, &arguments);
 80	
 81	struct printbuf* parsley = printbuf_new();
 82	struct printbuf* incl = printbuf_new();
 83  sprintbuf(parsley, "");
 84  sprintbuf(incl, "");
 85
 86	FILE* in = parsley_fopen(arguments.parsley, "r");
 87	
 88	printbuf_file_read(in, parsley);
 89	while(elemptr->has_next) {
 90		elemptr = elemptr->next;
 91		FILE* f = parsley_fopen(elemptr->string, "r");
 92		printbuf_file_read(f, incl);
 93		fclose(f);
 94	}
 95	
 96	parsleyPtr compiled = parsley_compile(parsley->buf, incl->buf);
 97	if(compiled->error != NULL) {
 98		fprintf(stderr, "%s\n", compiled->error);
 99	 	exit(1);
100	}
101	
102  FILE* fo = parsley_fopen(arguments.output_file, "w");
103  xmlDocFormatDump(fo, compiled->stylesheet->doc, 1);
104  fclose(fo);
105  
106	return 0;
107}
108