Error Handling

Obviously there would be possible errors that occur when the program runs. This module, error.c contains all the error handling functions. The first such function is usage_exit() which simply prints a brief summary of the program's usage information.

      
--Code fragment from file: error.c--


#include <stdio.h>
#include <stdarg.h>
#include <expat.h>
#include "xml-lit.h"

void
usage_exit(void)
{
  fprintf(stderr, "Usage: %s [options] literate-program.xml\n", progname);
  fprintf(stderr, "Tangle or weave a literate program.\n\n");
  fprintf(stderr, "-c, --c-mode      generate preprocessor #line directives from XML file\n");
  fprintf(stderr, "-h, -?, --help    print this usage message\n");
  fprintf(stderr, "-n, --no-line     do not make #line directives\n");
  fprintf(stderr, "-t, --tangle      tangle the input literate program\n");
  fprintf(stderr, "-T size, --hash-table-size=size  Set the size of the hash table\n");
  fprintf(stderr, "-v, --version     print XML-Lit's version\n");
  fprintf(stderr, "-w, --weave       weave the input literate program\n");
  fprintf(stderr, "-x, --xmltangle   xmltangle-compatible operation\n");
  fprintf(stderr, "-X, --Xml-lit     New-style xml-lit operation\n\n");
  fprintf(stderr, "The -c and -X options are default.\n");
  exit(1);
}
  
      
    

The other function is print_warn(), which prints a message on standard error. It is essentially a wrapper to vfprintf() that would print the error message properly formatted. It accepts a warning level. Level 5 is a fatal error, and causes print_warn to terminate the program.

      
--Code fragment from file: error.c--


static void
print_warn(int warnlevel, const char *fmt, va_list ap)
{
  fprintf(stderr, "%s[%d]: ", progname, warnlevel);
  vfprintf(stderr, fmt, ap);
  if (warnlevel > 4)
    exit(1);
}

      
    

We have a wrapper to this called warning() which prints a levelled warning.

      
--Code fragment from file: error.c--


void
warning(int warnlevel, const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  print_warn(warnlevel, fmt, ap);
  va_end(ap);
}

      
    

We have another wrapper to print_warn() called error_exit() which produces a level 5 warning, or fatal error.

      
--Code fragment from file: error.c--


void
error_exit(const char *fmt, ...)
{
  va_list ap;

  va_start(ap, fmt);
  print_warn(5, fmt, ap);
  va_end(ap);
}