From 0041ffdbc8d0dc2db4a7c9dadccfc1a8f518d1d3 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 24 Oct 2009 09:33:21 +0000 Subject: [PATCH] Allow a hastily selected subset of the output formats to also accept "-" as a special file name meaning standard output. I've restricted it to just those output formats which can predictably output only one file, just for the sake of not having to faff too much with the others. Probably what I should have done for all of this would have been to write a set of wrappers around fopen, fclose and everything in between, and use them everywhere. Those wrappers would uniformly detect "-" and convert it into stdin or stdout as appropriate, would avoid fclosing those files for real when told to close them, and would also be able to handle reading a little bit of data from the start of a file and then pushing it all back even if the file were not seekable (which would allow input.c to lose the ugly special case whereby it can't currently read font files from standard input). git-svn-id: svn://svn.tartarus.org/sgt/halibut@8729 cda61777-01e9-0310-a592-d414129be87e --- bk_html.c | 7 +++++-- bk_man.c | 8 ++++++-- bk_pdf.c | 8 ++++++-- bk_ps.c | 8 ++++++-- bk_text.c | 8 ++++++-- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/bk_html.c b/bk_html.c index fad891c..90800f1 100644 --- a/bk_html.c +++ b/bk_html.c @@ -884,7 +884,10 @@ void html_backend(paragraph *sourceform, keywordlist *keywords, #define listname(lt) ( (lt)==UL ? "ul" : (lt)==OL ? "ol" : "dl" ) #define itemname(lt) ( (lt)==LI ? "li" : (lt)==DT ? "dt" : "dd" ) - ho.fp = fopen(f->filename, "w"); + if (!strcmp(f->filename, "-")) + ho.fp = stdout; + else + ho.fp = fopen(f->filename, "w"); if (!ho.fp) error(err_cantopenw, f->filename); @@ -2507,7 +2510,7 @@ static void html_text_limit_internal(htmloutput *ho, wchar_t const *text, static void cleanup(htmloutput *ho) { return_to_neutral(ho); - if (ho->fp) + if (ho->fp && ho->fp != stdout) fclose(ho->fp); } diff --git a/bk_man.c b/bk_man.c index dce77d4..bb4e5b5 100644 --- a/bk_man.c +++ b/bk_man.c @@ -235,7 +235,10 @@ void man_backend(paragraph *sourceform, keywordlist *keywords, /* * Open the output file. */ - fp = fopen(conf.filename, "w"); + if (!strcmp(conf.filename, "-")) + fp = stdout; + else + fp = fopen(conf.filename, "w"); if (!fp) { error(err_cantopenw, conf.filename); return; @@ -425,7 +428,8 @@ void man_backend(paragraph *sourceform, keywordlist *keywords, /* * Tidy up. */ - fclose(fp); + if (fp != stdout) + fclose(fp); man_conf_cleanup(conf); } diff --git a/bk_pdf.c b/bk_pdf.c index 6f6349e..525e7d9 100644 --- a/bk_pdf.c +++ b/bk_pdf.c @@ -665,7 +665,10 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, * Write out the PDF file. */ - fp = fopen(filename, "wb"); + if (!strcmp(filename, "-")) + fp = stdout; + else + fp = fopen(filename, "wb"); if (!fp) { error(err_cantopenw, filename); return; @@ -712,7 +715,8 @@ void pdf_backend(paragraph *sourceform, keywordlist *keywords, olist.tail->number + 1, cat->number, info->number); fprintf(fp, "startxref\n%d\n%%%%EOF\n", fileoff); - fclose(fp); + if (fp != stdout) + fclose(fp); sfree(filename); } diff --git a/bk_ps.c b/bk_ps.c index 452d562..95c0451 100644 --- a/bk_ps.c +++ b/bk_ps.c @@ -48,7 +48,10 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords, } } - fp = fopen(filename, "w"); + if (!strcmp(filename, "-")) + fp = stdout; + else + fp = fopen(filename, "w"); if (!fp) { error(err_cantopenw, filename); return; @@ -304,7 +307,8 @@ void ps_backend(paragraph *sourceform, keywordlist *keywords, fprintf(fp, "%%%%EOF\n"); - fclose(fp); + if (fp != stdout) + fclose(fp); sfree(filename); } diff --git a/bk_text.c b/bk_text.c index c8ded79..4f8cdd7 100644 --- a/bk_text.c +++ b/bk_text.c @@ -323,7 +323,10 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, /* * Open the output file. */ - tf.fp = fopen(conf.filename, "w"); + if (!strcmp(conf.filename, "-")) + tf.fp = stdout; + else + tf.fp = fopen(conf.filename, "w"); if (!tf.fp) { error(err_cantopenw, conf.filename); return; @@ -460,7 +463,8 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, * Tidy up */ text_output(&tf, NULL); /* end charset conversion */ - fclose(tf.fp); + if (tf.fp != stdout) + fclose(tf.fp); sfree(conf.asect); sfree(conf.filename); } -- 2.11.0