Blame doc/mkdefsinc.c

Packit Service 672cf4
/* mkdefsinc.c - Tool to create defs.inc
Packit Service 672cf4
 * Copyright (C) 2015 g10 Code GmbH
Packit Service 672cf4
 *
Packit Service 672cf4
 * This file is free software; as a special exception the author gives
Packit Service 672cf4
 * unlimited permission to copy and/or distribute it, with or without
Packit Service 672cf4
 * modifications, as long as this notice is preserved.
Packit Service 672cf4
 *
Packit Service 672cf4
 * This file is distributed in the hope that it will be useful, but
Packit Service 672cf4
 * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
Packit Service 672cf4
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit Service 672cf4
 */
Packit Service 672cf4
Packit Service 672cf4
/* This tool needs to be build with command line supplied -D options
Packit Service 672cf4
   for the various directory variables.  It is easier to do this in
Packit Service 672cf4
   build file than to use fragile make rules and a template file.  */
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
#include <stdio.h>
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <string.h>
Packit Service 672cf4
#include <errno.h>
Packit Service 672cf4
#include <time.h>
Packit Service 672cf4
#include <sys/types.h>
Packit Service 672cf4
#include <sys/stat.h>
Packit Service 672cf4
#include <unistd.h>
Packit Service 672cf4
Packit Service 672cf4
#define PGM "mkdefsinc"
Packit Service 672cf4
Packit Service 672cf4
/* We include config.h after all include files because the config.h
Packit Service 672cf4
   values are not valid for the build platform but we need some values
Packit Service 672cf4
   nevertheless.  */
Packit Service 672cf4
#include "config.h"
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static int verbose;
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* The usual free wrapper.  */
Packit Service 672cf4
static void
Packit Service 672cf4
xfree (void *a)
Packit Service 672cf4
{
Packit Service 672cf4
  if (a)
Packit Service 672cf4
    free (a);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static char *
Packit Service 672cf4
xmalloc (size_t n)
Packit Service 672cf4
{
Packit Service 672cf4
  char *p;
Packit Service 672cf4
Packit Service 672cf4
  p = malloc (n);
Packit Service 672cf4
  if (!p)
Packit Service 672cf4
    {
Packit Service 672cf4
      fputs (PGM ": out of core\n", stderr);
Packit Service 672cf4
      exit (1);
Packit Service 672cf4
    }
Packit Service 672cf4
  return p;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static char *
Packit Service 672cf4
xstrdup (const char *string)
Packit Service 672cf4
{
Packit Service 672cf4
  char *p;
Packit Service 672cf4
Packit Service 672cf4
  p = xmalloc (strlen (string)+1);
Packit Service 672cf4
  strcpy (p, string);
Packit Service 672cf4
  return p;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Return a malloced string with the last modification date of the
Packit Service 672cf4
   FILES.  Returns NULL on error.  */
Packit Service 672cf4
static char *
Packit Service 672cf4
get_date_from_files (char **files)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *file;
Packit Service 672cf4
  const char *usedfile = NULL;
Packit Service 672cf4
  struct stat sb;
Packit Service 672cf4
  struct tm *tp;
Packit Service 672cf4
  int errors = 0;
Packit Service 672cf4
  time_t stamp = 0;
Packit Service 672cf4
  char *result;
Packit Service 672cf4
Packit Service 672cf4
  for (; (file = *files); files++)
Packit Service 672cf4
    {
Packit Service 672cf4
      if (!*file || !strcmp (file, ".") || !strcmp (file, ".."))
Packit Service 672cf4
        continue;
Packit Service 672cf4
      if (stat (file, &sb))
Packit Service 672cf4
        {
Packit Service 672cf4
          fprintf (stderr, PGM ": stat failed for '%s': %s\n",
Packit Service 672cf4
                   file, strerror (errno));
Packit Service 672cf4
          errors = 1;
Packit Service 672cf4
          continue;
Packit Service 672cf4
        }
Packit Service 672cf4
      if (sb.st_mtime > stamp)
Packit Service 672cf4
        {
Packit Service 672cf4
          stamp = sb.st_mtime;
Packit Service 672cf4
          usedfile = file;
Packit Service 672cf4
        }
Packit Service 672cf4
    }
Packit Service 672cf4
  if (errors)
Packit Service 672cf4
    exit (1);
Packit Service 672cf4
Packit Service 672cf4
  if (usedfile)
Packit Service 672cf4
    fprintf (stderr, PGM ": taking date from '%s'\n", usedfile);
Packit Service 672cf4
Packit Service 672cf4
  tp = gmtime (&stamp);
Packit Service 672cf4
  if (!tp)
Packit Service 672cf4
    return NULL;
Packit Service 672cf4
  result = xmalloc (4+1+2+1+2+1);
Packit Service 672cf4
  snprintf (result, 4+1+2+1+2+1, "%04d-%02d-%02d",
Packit Service 672cf4
            tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday);
Packit Service 672cf4
  return result;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* We need to escape file names for Texinfo.  */
Packit Service 672cf4
static void
Packit Service 672cf4
print_filename (const char *prefix, const char *name)
Packit Service 672cf4
{
Packit Service 672cf4
  const char *s;
Packit Service 672cf4
Packit Service 672cf4
  fputs (prefix, stdout);
Packit Service 672cf4
  for (s=name; *s; s++)
Packit Service 672cf4
    switch (*s)
Packit Service 672cf4
      {
Packit Service 672cf4
      case '@': fputs ("@atchar{}",        stdout); break;
Packit Service 672cf4
      case '{': fputs ("@lbracechar{}",    stdout); break;
Packit Service 672cf4
      case '}': fputs ("@rbracechar{}",    stdout); break;
Packit Service 672cf4
      case ',': fputs ("@comma{}",         stdout); break;
Packit Service 672cf4
      case '\\':fputs ("@backslashchar{}", stdout); break;
Packit Service 672cf4
      case '#': fputs ("@hashchar{}",      stdout); break;
Packit Service 672cf4
      default: putchar (*s); break;
Packit Service 672cf4
      }
Packit Service 672cf4
  putchar('\n');
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
int
Packit Service 672cf4
main (int argc, char **argv)
Packit Service 672cf4
{
Packit Service 672cf4
  int last_argc = -1;
Packit Service 672cf4
  char *opt_date = NULL;
Packit Service 672cf4
  int monthoff;
Packit Service 672cf4
  char *p, *pend;
Packit Service 672cf4
  size_t n;
Packit Service 672cf4
Packit Service 672cf4
  /* Option parsing.  */
Packit Service 672cf4
  if (argc)
Packit Service 672cf4
    {
Packit Service 672cf4
      argc--; argv++;
Packit Service 672cf4
    }
Packit Service 672cf4
  while (argc && last_argc != argc )
Packit Service 672cf4
    {
Packit Service 672cf4
      last_argc = argc;
Packit Service 672cf4
      if (!strcmp (*argv, "--"))
Packit Service 672cf4
        {
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
          break;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--help"))
Packit Service 672cf4
        {
Packit Service 672cf4
          fputs ("Usage: " PGM " [OPTION] [FILES]\n"
Packit Service 672cf4
                 "Create defs.inc file.\nOptions:\n"
Packit Service 672cf4
                 "  -C DIR         Change to DIR before doing anything\n"
Packit Service 672cf4
                 "  --date STRING  Take publication date from STRING\n"
Packit Service 672cf4
                 "  --verbose      Enable extra informational output\n"
Packit Service 672cf4
                 "  --help         Display this help and exit\n"
Packit Service 672cf4
                 , stdout);
Packit Service 672cf4
          exit (0);
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--verbose"))
Packit Service 672cf4
        {
Packit Service 672cf4
          verbose = 1;
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "-C"))
Packit Service 672cf4
        {
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
          if (argc)
Packit Service 672cf4
            {
Packit Service 672cf4
              if (chdir (*argv))
Packit Service 672cf4
                {
Packit Service 672cf4
                  fprintf (stderr, PGM ": chdir to '%s' failed: %s\n",
Packit Service 672cf4
                           *argv, strerror (errno));
Packit Service 672cf4
                  exit (1);
Packit Service 672cf4
                }
Packit Service 672cf4
              argc--; argv++;
Packit Service 672cf4
            }
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strcmp (*argv, "--date"))
Packit Service 672cf4
        {
Packit Service 672cf4
          argc--; argv++;
Packit Service 672cf4
          if (argc)
Packit Service 672cf4
            {
Packit Service 672cf4
              opt_date = xstrdup (*argv);
Packit Service 672cf4
              argc--; argv++;
Packit Service 672cf4
            }
Packit Service 672cf4
        }
Packit Service 672cf4
      else if (!strncmp (*argv, "--", 2))
Packit Service 672cf4
        {
Packit Service 672cf4
          fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
Packit Service 672cf4
          exit (1);
Packit Service 672cf4
        }
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  if (opt_date && *opt_date)
Packit Service 672cf4
    {
Packit Service 672cf4
      time_t stamp;
Packit Service 672cf4
      struct tm *tp;
Packit Service 672cf4
Packit Service 672cf4
      if (*opt_date == '2' && strlen (opt_date) >= 10
Packit Service 672cf4
          && opt_date[4] == '-' && opt_date[7] == '-')
Packit Service 672cf4
        {
Packit Service 672cf4
          opt_date[10] = 0;
Packit Service 672cf4
        }
Packit Service 672cf4
      else if ((stamp = strtoul (opt_date, NULL, 10)) > 0
Packit Service 672cf4
               && (tp = gmtime (&stamp)))
Packit Service 672cf4
        {
Packit Service 672cf4
          p = xmalloc (4+1+2+1+2+1);
Packit Service 672cf4
          snprintf (p, 4+1+2+1+2+1, "%04d-%02d-%02d",
Packit Service 672cf4
                    tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday);
Packit Service 672cf4
          xfree (opt_date);
Packit Service 672cf4
          opt_date = p;
Packit Service 672cf4
        }
Packit Service 672cf4
      else
Packit Service 672cf4
        {
Packit Service 672cf4
          fprintf (stderr, PGM ": bad date '%s'\n", opt_date);
Packit Service 672cf4
          exit (1);
Packit Service 672cf4
        }
Packit Service 672cf4
    }
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      xfree (opt_date);
Packit Service 672cf4
      opt_date = argc? get_date_from_files (argv) : NULL;
Packit Service 672cf4
    }
Packit Service 672cf4
  if (!opt_date)
Packit Service 672cf4
    {
Packit Service 672cf4
      opt_date = xstrdup ("unknown");
Packit Service 672cf4
      monthoff = 0;
Packit Service 672cf4
    }
Packit Service 672cf4
  else
Packit Service 672cf4
    {
Packit Service 672cf4
      const char *month = "?";
Packit Service 672cf4
Packit Service 672cf4
      switch (atoi (opt_date+5))
Packit Service 672cf4
        {
Packit Service 672cf4
        case  1: month = "January"; break;
Packit Service 672cf4
        case  2: month = "February"; break;
Packit Service 672cf4
        case  3: month = "March"; break;
Packit Service 672cf4
        case  4: month = "April"; break;
Packit Service 672cf4
        case  5: month = "May"; break;
Packit Service 672cf4
        case  6: month = "June"; break;
Packit Service 672cf4
        case  7: month = "July"; break;
Packit Service 672cf4
        case  8: month = "August"; break;
Packit Service 672cf4
        case  9: month = "September"; break;
Packit Service 672cf4
        case 10: month = "October"; break;
Packit Service 672cf4
        case 11: month = "November"; break;
Packit Service 672cf4
        case 12: month = "December"; break;
Packit Service 672cf4
        }
Packit Service 672cf4
      n = strlen (opt_date) + strlen (month) + 2 + 1;
Packit Service 672cf4
      p = xmalloc (n);
Packit Service 672cf4
      snprintf (p, n, "%d %n%s %d",
Packit Service 672cf4
                atoi (opt_date+8), &monthoff, month, atoi (opt_date));
Packit Service 672cf4
      xfree (opt_date);
Packit Service 672cf4
      opt_date = p;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
  fputs ("@c defs.inc                         -*- texinfo -*-\n"
Packit Service 672cf4
         "@c Common and build specific constants for the manuals.\n"
Packit Service 672cf4
         "@c This file has been created by " PGM ".\n\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
  fputs ("@ifclear defsincincluded\n"
Packit Service 672cf4
         "@set defsincincluded 1\n\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
  fputs ("\n@c Flags\n\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
  fputs ("\n@c Directories\n\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
  /* print_filename ("@set BINDIR         ", GNUPG_BINDIR ); */
Packit Service 672cf4
Packit Service 672cf4
  fputs ("\n@c Version information a la version.texi\n\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
  printf ("@set UPDATED %s\n", opt_date);
Packit Service 672cf4
  printf ("@set UPDATED-MONTH %s\n", opt_date + monthoff);
Packit Service 672cf4
  printf ("@set EDITION %s\n", PACKAGE_VERSION);
Packit Service 672cf4
  printf ("@set VERSION %s\n", PACKAGE_VERSION);
Packit Service 672cf4
Packit Service 672cf4
  fputs ("\n@c Macros\n\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
  /* Trailer.  */
Packit Service 672cf4
  fputs ("\n"
Packit Service 672cf4
         "@end ifclear\n"
Packit Service 672cf4
         "\n"
Packit Service 672cf4
         "@c Loc" "al Variables:\n"
Packit Service 672cf4
         "@c buffer-read-only: t\n"
Packit Service 672cf4
         "@c End:\n", stdout);
Packit Service 672cf4
Packit Service 672cf4
  if (ferror (stdout))
Packit Service 672cf4
    {
Packit Service 672cf4
      fprintf (stderr, PGM ": error writing to stdout: %s\n", strerror (errno));
Packit Service 672cf4
      return 1;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}