| 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 <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "detox_struct.h" |
| 15 |
|
| 16 |
#include "filter.h" |
| 17 |
#include "sequence.h" |
| 18 |
#include "wrapped.h" |
| 19 |
|
| 20 |
/** |
| 21 |
* Chooses which sequence to use. |
| 22 |
* |
| 23 |
* @param sequences Sequences from the config file. |
| 24 |
* @param name The sequence name from the command line, if any. |
| 25 |
* |
| 26 |
* @return The chosen sequence. |
| 27 |
*/ |
| 28 |
sequence_t *sequence_choose_default(sequence_t *sequences, const char *name) |
| 29 |
{ |
| 30 |
sequence_t *which = NULL; |
| 31 |
sequence_t *work = sequences; |
| 32 |
|
| 33 |
while (work != NULL) { |
| 34 |
if (strcmp(work->name, (name == NULL) ? "default" : name) == 0) { |
| 35 |
which = work; |
| 36 |
break; |
| 37 |
} |
| 38 |
|
| 39 |
work = work->next; |
| 40 |
} |
| 41 |
|
| 42 |
/* |
| 43 |
* If no sequence was found, and the user didn't specify a sequence |
| 44 |
* to use, just use the first sequence. |
| 45 |
*/ |
| 46 |
|
| 47 |
if (which == NULL && name == NULL) { |
| 48 |
if (sequences != NULL) { |
| 49 |
which = sequences; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return which; |
| 54 |
} |
| 55 |
|
| 56 |
sequence_t *sequence_init(const char *name) |
| 57 |
{ |
| 58 |
sequence_t *ret; |
| 59 |
|
| 60 |
ret = wrapped_malloc(sizeof(sequence_t)); |
| 61 |
memset(ret, 0, sizeof(sequence_t)); |
| 62 |
|
| 63 |
ret->name = wrapped_strdup(name); |
| 64 |
|
| 65 |
return ret; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Reviews a sequence to confirm that it's valid. |
| 70 |
* |
| 71 |
* @param sequence_t *sequence |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
void sequence_review(sequence_t *sequence) |
| 76 |
{ |
| 77 |
filter_t *filter = sequence->filters; |
| 78 |
table_t *table = NULL; |
| 79 |
|
| 80 |
while (filter != NULL) { |
| 81 |
table = filter_load_table(filter); |
| 82 |
|
| 83 |
if (table != NULL) { |
| 84 |
filter->table = table; |
| 85 |
} |
| 86 |
filter = filter->next; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Runs the filters associated with a sequence. |
| 92 |
* |
| 93 |
* @param sequence The sequence of filters to run. |
| 94 |
* @param filename The filename to run through the filters. |
| 95 |
* |
| 96 |
* @return The filtered filename, or NULL if a problem was encountered. |
| 97 |
*/ |
| 98 |
char *sequence_run_filters(sequence_t *sequence, char *filename) |
| 99 |
{ |
| 100 |
filter_t *filter; |
| 101 |
char *hold; |
| 102 |
char *work; |
| 103 |
|
| 104 |
if (sequence == NULL) { |
| 105 |
fprintf(stderr, "internal error\n"); |
| 106 |
exit(EXIT_FAILURE); |
| 107 |
} |
| 108 |
|
| 109 |
if (filename == NULL) { |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
filter = sequence->filters; |
| 114 |
work = wrapped_strdup(filename); |
| 115 |
|
| 116 |
while (filter != NULL && work != NULL) { |
| 117 |
hold = filter_run(filter, work); |
| 118 |
free(work); |
| 119 |
work = hold; |
| 120 |
filter = filter->next; |
| 121 |
} |
| 122 |
|
| 123 |
return work; |
| 124 |
} |