Set up --buffer and --keep

This commit is contained in:
Wilson Lin 2018-07-06 15:18:58 +12:00
parent 20bd56c08e
commit 2538e24a17
3 changed files with 78 additions and 34 deletions

View File

@ -100,18 +100,18 @@ Path to a file to write to; it will be created if it doesn't exist already. If o
#### `--keep` #### `--keep`
Don't automatically delete the output file if an error occurred. This option does nothing if the output is `stdout`, and cannot be used with `--buffer`. Don't automatically delete the output file if an error occurred. If the output is `stdout`, or the output is a file but `--buffer` is provided, this option does nothing.
#### `--buffer` #### `--buffer`
Buffer all output until the process is complete and successful. This can prevent many writes to storage (and won't cause any writes on error), but will use a non-constant amount of memory. Buffer all output until the process is complete and successful. This won't truncate or write anything to the output until the build process is done, but will use a non-constant amount of memory.
This applies even when the output is `stdout`, and cannot be used with `--keep`. This applies even when the output is `stdout`.
#### `--errorEx` #### `--suppress`
Suppress errors specified by this option. hyperbuild will quitely ignore and continue processing when otherwise one of the provided errors would occur. Suppress errors specified by this option. hyperbuild will quitely ignore and continue processing when otherwise one of the provided errors would occur.
Separate the error names by a comma. Suppressible errors are marked with a `⌫` in the [Errors](#errors) section. Separate the error names with commas. Suppressible errors are marked with a `⌫` in the [Errors](#errors) section.
## Processing ## Processing
@ -238,7 +238,7 @@ For brevity, hyperbuild has built-in sets of tags that can be used in place of d
|`$void`|`area`, `base`, `br`, `col`, `embed`, `hr`, `img`, `input`, `keygen`, `link`, `meta`, `param`, `source`, `track`, `wbr`|[voidtags.c](src/main/c/rule/tag/voidtags.c)| |`$void`|`area`, `base`, `br`, `col`, `embed`, `hr`, `img`, `input`, `keygen`, `link`, `meta`, `param`, `source`, `track`, `wbr`|[voidtags.c](src/main/c/rule/tag/voidtags.c)|
|`$wss`|`pre`, `code`|[wsstags.c](src/main/c/rule/tag/wsstags.c)| |`$wss`|`pre`, `code`|[wsstags.c](src/main/c/rule/tag/wsstags.c)|
#### `--collapseWhitespaceEx $wss` #### `--MXcollapseWhitespace $wss`
Reduce a sequence of whitespace characters in text nodes to a single space (U+0020), unless they are a child of the tags specified by this option. Reduce a sequence of whitespace characters in text nodes to a single space (U+0020), unless they are a child of the tags specified by this option.
@ -260,7 +260,7 @@ Reduce a sequence of whitespace characters in text nodes to a single space (U+00
</table> </table>
#### `--destroyWholeWhitespaceEx $wss,$content,$formatting` #### `--MXdestroyWholeWhitespace $wss,$content,$formatting`
Remove any text nodes that only consist of whitespace characters, unless they are a child of the tags specified by this option. Remove any text nodes that only consist of whitespace characters, unless they are a child of the tags specified by this option.
@ -286,7 +286,7 @@ Especially useful when using `display: inline-block` so that whitespace between
</table> </table>
#### `--trimWhitespaceEx $wss,$formatting` #### `--MXtrimWhitespace $wss,$formatting`
Remove any whitespace from the start and end of a tag, if the first and/or last node is a text node, unless the tag is one of the tags specified by this option. Remove any whitespace from the start and end of a tag, if the first and/or last node is a text node, unless the tag is one of the tags specified by this option.
@ -315,9 +315,9 @@ Basically, a tag should only either contain text and [inline text semantics](htt
</table> </table>
#### `--trimClassAttribute` #### `--MXtrimClassAttribute`
Trim and collapse whitespace in `class` attribute values. Don't trim and collapse whitespace in `class` attribute values.
<table><thead><tr><th>Before<th>After<tbody><tr><td> <table><thead><tr><th>Before<th>After<tbody><tr><td>
@ -339,29 +339,29 @@ Trim and collapse whitespace in `class` attribute values.
</table> </table>
#### `--decodeEntities` #### `--MXdecodeEntities`
Decode any valid entities into their UTF-8 values. Don't decode any valid entities into their UTF-8 values.
#### `--processConditionalComments` #### `--MXprocessConditionalComments`
Process the contents of conditional comments, including downlevel-revealed conditional comments. Don't process the contents of conditional comments, including downlevel-revealed conditional comments.
#### `--removeAttributeQuotes` #### `--MXremoveAttributeQuotes`
Remove quotes around attribute values when possible. Don't remove quotes around attribute values when possible.
#### `--removeComments` #### `--MXremoveComments`
Remove any comments, except conditional comments. Don't remove any comments, except conditional comments.
#### `--removeOptionalTags` #### `--MXremoveOptionalTags`
Remove optional starting or ending tags. Don't remove optional starting or ending tags.
#### `--removeTagWhitespace` #### `--MXremoveTagWhitespace`
Remove spaces between attributes when possible. Don't remove spaces between attributes when possible.
### Non-options ### Non-options

View File

@ -20,17 +20,21 @@ int main(int argc, char **argv) {
// Prepare config // Prepare config
char *input_path = NULL; char *input_path = NULL;
char *output_path = NULL; char *output_path = NULL;
int config_keep = 0;
int config_buffer = 0;
// Parse arguments // Parse arguments
while (1) { while (1) {
static struct option long_options[] = { static struct option long_options[] = {
{"keep", no_argument, NULL, 'k'},
{"buffer", no_argument, NULL, 'b'},
{"input", required_argument, NULL, 'i'}, {"input", required_argument, NULL, 'i'},
{"output", required_argument, NULL, 'o'}, {"output", required_argument, NULL, 'o'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
int option_index = 0; int option_index = 0;
int c = getopt_long(argc, argv, "i:o:", long_options, &option_index); int c = getopt_long(argc, argv, "kbi:o:", long_options, &option_index);
if (c == -1) { if (c == -1) {
break; break;
@ -44,26 +48,57 @@ int main(int argc, char **argv) {
case 'o': case 'o':
output_path = optarg; output_path = optarg;
break; break;
case 'k':
config_keep = 1;
break;
case 'b':
config_buffer = 1;
break;
} }
} }
hbe_debug("Input: %s", input_path); hbe_debug("Input: %s", input_path);
hbe_debug("Output: %s", output_path); hbe_debug("Output: %s", output_path);
if (config_buffer) {
hbu_fstreamin_t input = hbu_fstreamin_create(input_path); hbe_debug("Buffer: %d", config_buffer);
hbu_fstreamout_t output = hbu_fstreamout_create(output_path); }
if (config_keep) {
if (output != NULL) { hbe_debug("Keep: %d", config_keep);
// Set after opening output stream (file is created then)
hbe_fatal_set_autodelete(output_path);
} }
hbu_pipe_t pipe = hbu_pipe_create_blank(); hbu_pipe_t pipe = hbu_pipe_create_blank();
hbu_fstreamin_t input = hbu_fstreamin_create(input_path);
hbu_pipe_blank_set_input(pipe, input); hbu_pipe_blank_set_input(pipe, input);
hbu_pipe_blank_set_output_fstreamout(pipe, output);
hbu_fstreamout_t output;
hbu_buffer_t output_buffer;
if (config_buffer) {
output = NULL;
output_buffer = hbu_buffer_create();
hbu_pipe_blank_set_output_buffer(pipe, output_buffer);
} else {
output = hbu_fstreamout_create(output_path);
output_buffer = NULL;
hbu_pipe_blank_set_output_fstreamout(pipe, output);
}
if (output != NULL && !config_keep && !config_buffer) {
// Set after opening output stream (file is created then)
// Don't need to set if $config_buffer, as it won't write anything anyway
hbe_fatal_set_autodelete(output_path);
}
hbs_content(pipe); hbs_content(pipe);
if (config_buffer) {
output = hbu_fstreamout_create(output_path);
hbu_fstreamout_write_buffer(output, output_buffer);
}
hbe_debug("All done!"); hbe_debug("All done!");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@ -4,14 +4,23 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include "../error/error.c" #include "../error/error.c"
#include "fstream.h" #include "./buffer.c"
#include "./fstream.h"
HBU_FSTREAM_BUILD_INFRA(out, "w", "write", "writing", stdout) HBU_FSTREAM_BUILD_INFRA(out, "w", "write", "writing", stdout)
void hbu_fstreamout_write(hbu_fstreamout_t fstreamout, hb_char_t c) { static void _hbu_fstreamout_fwrite(hbu_fstreamout_t fstreamout, hb_char_t *source, size_t length) {
if (fwrite(&c, SIZEOF_CHAR, 1, fstreamout->fd) != SIZEOF_CHAR) { if (fwrite(source, SIZEOF_CHAR, length, fstreamout->fd) != SIZEOF_CHAR * length) {
hbe_fatal(HBE_IO_FWRITE_FAIL, "Failed to write to output file %s", fstreamout->name); hbe_fatal(HBE_IO_FWRITE_FAIL, "Failed to write to output file %s", fstreamout->name);
} }
} }
void hbu_fstreamout_write_buffer(hbu_fstreamout_t fstreamout, hbu_buffer_t buffer) {
_hbu_fstreamout_fwrite(fstreamout, hbu_buffer_underlying(buffer), buffer->length);
}
void hbu_fstreamout_write(hbu_fstreamout_t fstreamout, hb_char_t c) {
_hbu_fstreamout_fwrite(fstreamout, &c, 1);
}
#endif // _HDR_HYPERBUILD_UTIL_FSTREAMOUT #endif // _HDR_HYPERBUILD_UTIL_FSTREAMOUT