Debian Codegraph @

src/detox.c

raw
1 /**
2 * This file is part of the Detox package.
3 *
4 * Copyright (c) Doug Harple <[email protected]>
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 */
9
10 #include <sys/stat.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15
16 #include "detox_struct.h"
17
18 #include "config_file_dump.h"
19 #include "config_file.h"
20 #include "file.h"
21 #include "filelist.h"
22 #include "parse_options.h"
23 #include "sequence.h"
24
25 int main(int argc, char **argv)
26 {
27 struct stat stat_info;
28 int err;
29
30 config_file_t *config_file = NULL;
31 options_t *main_options;
32
33 char *file_work = NULL;
34 char *file_walk;
35
36 main_options = parse_options_getopt(argc, argv);
37
38 if (main_options == NULL) {
39 fprintf(stderr, "detox: an error occurred while parsing command line arguments\n");
40 exit(EXIT_FAILURE);
41 }
42
43 config_file = config_file_load(main_options);
44
45 if (config_file == NULL) {
46 fprintf(stderr, "detox: no config file to work with\n");
47 exit(EXIT_FAILURE);
48 }
49
50 /*
51 * Store the files_to_ignore array in the main_options struct for use in
52 * parse_dir/file/special
53 */
54
55 main_options->files_to_ignore = config_file->files_to_ignore;
56
57 /*
58 * Determine which sequence to use
59 */
60 main_options->sequence_to_use = sequence_choose_default(config_file->sequences, main_options->sequence_name);
61
62 /*
63 * List sequences
64 */
65 if (main_options->list_sequences) {
66 dump_config_file(config_file, main_options);
67 exit(EXIT_SUCCESS);
68 }
69
70 /*
71 * Fail if no sequence is available
72 */
73 if (main_options->sequence_to_use == NULL) {
74 /*
75 * XXX - Explain this better
76 */
77 fprintf(stderr, "detox: no sequence to work with\n");
78 exit(EXIT_FAILURE);
79 }
80
81 /*
82 * Check translation tables
83 */
84
85 sequence_review(main_options->sequence_to_use);
86
87 /*
88 * Do some actual work
89 */
90
91 if (!main_options->is_inline_mode) {
92 while ((file_walk = filelist_get(main_options->files))) {
93 if (main_options->verbose) {
94 printf("Scanning: %s\n", file_walk);
95 }
96
97 err = lstat(file_walk, &stat_info);
98 if (err == -1) {
99 fprintf(stderr, "%s: %s\n", file_walk, strerror(errno));
100 } else {
101 if (S_ISDIR(stat_info.st_mode)) {
102 file_work = parse_file(file_walk, main_options);
103 parse_dir(file_work, main_options);
104 free(file_work);
105 } else if (S_ISREG(stat_info.st_mode)) {
106 parse_file(file_walk, main_options);
107 } else if (main_options->special) {
108 parse_file(file_walk, main_options);
109 }
110 }
111 }
112 } else {
113 if (filelist_count(main_options->files) > 0) {
114 while ((file_walk = filelist_get(main_options->files))) {
115 err = lstat(file_walk, &stat_info);
116 if (err == -1) {
117 fprintf(stderr, "%s: %s\n", file_walk, strerror(errno));
118 } else {
119 if (S_ISDIR(stat_info.st_mode)) {
120 fprintf(stderr, "%s: is a directory\n", file_walk);
121 } else {
122 parse_inline(file_walk, NULL, main_options);
123 }
124 }
125 }
126 } else {
127 parse_inline(NULL, NULL, main_options);
128 }
129 }
130
131 return 0;
132 }