aboutsummaryrefslogtreecommitdiff
path: root/src/Main.c
blob: 0acadf1fccdc6686275d9d44f5d04fd9d4c7bde4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "Cli/Cli.h"
#include "FileProcessor/FileProcessor.h"
#include "Logger/Logger.h"
#include "Path/Path.h"

#include <libxml/parser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *base_name(const char *path) {
  const char *slash = strrchr(path, '/');

  return slash == NULL ? path : slash + 1;
}

static int run(int argc, char **argv) {
  CliOptions options;
  CliParseResult parse_result;
  char *automatic_output = NULL;
  const char *output_path;
  int result;

  parse_result = cli_parse(argc, argv, &options);
  if (parse_result != CLI_PARSE_OK) {
    cli_free_options(&options);
    return parse_result == CLI_PARSE_ERROR ? EXIT_FAILURE : EXIT_SUCCESS;
  }

  logger_set_level(options.log_level);

  output_path = options.output_path;
  if (output_path == NULL) {
    automatic_output = path_make_default_output(options.input_path);
    if (automatic_output == NULL) {
      logger_error("unable to allocate output path");
      cli_free_options(&options);
      return EXIT_FAILURE;
    }
    output_path = automatic_output;
  }

  if (options.input_type == INPUT_TYPE_DIRECTORY) {
    result = process_directory(options.input_path, output_path,
                               options.overwrite_output, &options.patterns);
  } else if (pattern_list_resolve(
                 &options.patterns, base_name(options.input_path), 0,
                 pattern_list_default_kind(&options.patterns)) ==
             PATTERN_EXCLUDE) {
    logger_error("input file is excluded by the active patterns: %s",
                 options.input_path);
    result = -1;
  } else {
    result = process_file(options.input_path, output_path);
  }

  free(automatic_output);
  cli_free_options(&options);
  return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

int main(int argc, char **argv) {
  int result;

  xmlInitParser();
  result = run(argc, argv);
  xmlCleanupParser();
  return result;
}