Revamp of the Halibut error handling mechanism.
[sgt/halibut] / bk_text.c
index 1a1796e..315317f 100644 (file)
--- a/bk_text.c
+++ b/bk_text.c
@@ -311,7 +311,7 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
     word spaceword;
     textfile tf;
     wchar_t *prefixextra;
-    int nesting, nestindent;
+    int nesting, nestbase, nestindent;
     int indentb, indenta;
 
     IGNORE(unused);
@@ -323,9 +323,12 @@ 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);
+       err_cantopenw(conf.filename);
        return;
     }
     tf.charset = conf.charset;
@@ -338,7 +341,8 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
                         conf.atitle, conf.indent, conf.width, &conf);
 
     nestindent = conf.listindentbefore + conf.listindentafter;
-    nesting = (conf.indent_preambles ? 0 : -conf.indent);
+    nestbase = (conf.indent_preambles ? 0 : -conf.indent);
+    nesting = nestbase;
 
     /* Do the main document */
     for (p = sourceform; p; p = p->next) switch (p->type) {
@@ -356,7 +360,7 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
        break;
       case para_LcontPop:
        nesting -= nestindent;
-       assert(nesting >= 0);
+       assert(nesting >= nestbase);
        break;
 
        /*
@@ -459,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);
 }
@@ -492,7 +497,7 @@ static void text_output_many(textfile *tf, int n, wchar_t c)
     wchar_t s[2];
     s[0] = c;
     s[1] = L'\0';
-    while (n--)
+    while (n-- > 0)
        text_output(tf, s);
 }
 
@@ -747,7 +752,7 @@ static void text_codepara(textfile *tf, word *text, int indent, int width) {
     for (; text; text = text->next) if (text->type == word_WeakCode) {
        int wid = ustrwid(text->text, tf->charset);
        if (wid > width)
-           error(err_text_codeline, &text->fpos, wid, width);
+           err_text_codeline(&text->fpos, wid, width);
        text_output_many(tf, indent, L' ');
        text_output(tf, text->text);
        text_output(tf, L"\n");