htex

simple incorrect html parser
git clone git://git.relim.de/htex.git
Log | Files | Refs | README

htex.c (2400B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdbool.h>
      4 #include <stdlib.h>
      5 #include <getopt.h>
      6 #include <inttypes.h>
      7 #include <grapheme.h>
      8 #include "src/misc.h"
      9 #include "src/html.h"
     10 
     11 int main(int argc, char *argv[])
     12 {
     13 	int o = 0;
     14 	int option_index = 0;
     15 	char *output = NULL;
     16 	bool is_except = false;
     17 	char *text = NULL;
     18 	char *search_pattern = NULL;
     19 	int limit = -1;
     20 	static struct option long_options[] = {
     21 		{ "output", required_argument, 0, 'o' },
     22 		{ "except", no_argument, 0, 'e' },
     23 		{ "limit", required_argument, 0, 'l' },
     24 		{ 0, 0, 0, 0 }
     25 	};
     26 	while ((o = getopt_long(argc, argv, "o:el:", long_options, &option_index)) != -1) {
     27 		switch(o) {
     28 			case 'o':
     29 				output = realloc(output, (strlen(optarg)+1) * sizeof(char));
     30 				strcpy(output, optarg);
     31 				break;
     32 			case 'e':
     33 				is_except = true;
     34 				break;
     35 			case 'l':
     36 				limit = atoi(optarg);
     37                 if (limit <= 0) {
     38                     fprintf(stderr, "htex: Provide a valid limit value.\n");
     39                     return -1;
     40                 }
     41 				break;
     42 		}
     43 	}
     44 	enum OutType out = output_type_parse(output);
     45 	if (out == -1) {
     46 		fprintf(stderr, "htex: Provide a valid output type!\n");
     47 		free(output);
     48 		return -1;
     49 	}
     50 	if (limit == 0) {
     51 		fprintf(stderr, "htex: Provide a valid limit value.\n");
     52 		free(output);
     53 		return -1;
     54 	}
     55 	if (argc == optind) {
     56 		fprintf(stderr, "htex: Provide a search pattern!\n");
     57 		return -1;
     58 	}
     59 	if (argc > optind+2) {
     60 		fprintf(stderr, "htex: Provide only one file!\n");
     61 		return -1;
     62 	}
     63 	if (argc == optind+1) {
     64 		search_pattern = argv[argc-1];
     65 		text = file_read(stdin);
     66 	} else if (argc == optind+2) {
     67 		search_pattern = argv[argc-2];
     68 		char *filepath = argv[argc-1];
     69 		FILE *fp = fopen(filepath, "r");
     70 		if (fp == NULL) {
     71 			perror("htex: fopen failed: ");
     72 			return -1;
     73 		}
     74 		text = file_read(fp);
     75 		fclose(fp);
     76 		if (strlen(text) == 0) {
     77 			fprintf(stderr, "htex: No data in file.\n");
     78 			free(output);
     79 			free(text);
     80 			return 0;
     81 		}
     82 	}
     83 	struct FindOpts *opts = find_opts_parse(search_pattern);
     84 	opts->out = out;
     85 	opts->is_except = is_except;
     86 	opts->limit = limit;
     87     struct HTMLDocument *document = html_document_parse(text);
     88     struct TagList *found_tags = html_document_find(document, opts);
     89     html_document_print_find_result(document, found_tags, opts);
     90     html_document_free(document);
     91     tag_list_free(found_tags);
     92 	find_opts_free(opts);
     93 	free(output);
     94 	free(text);
     95 	return 0;
     96 }