1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/* mkdefsinc.c - Tool to create defs.inc
* Copyright (C) 2015 g10 Code GmbH
*
* This file is free software; as a special exception the author gives
* unlimited permission to copy and/or distribute it, with or without
* modifications, as long as this notice is preserved.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/* This tool needs to be build with command line supplied -D options
for the various directory variables. See ../am/cmacros.am. It is
easier to do this in build file than to use fragile make rules and
a template file. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define PGM "mkdefsinc"
/* We include config.h after all include files because the config.h
values are not valid for the build platform but we need some values
nevertheless. */
#include "config.h"
/* When building for Windows the -D macros do not have appropriate
values. We provide replacements here. */
#ifdef HAVE_W32_SYSTEM
# undef GNUPG_BINDIR
# undef GNUPG_LIBEXECDIR
# undef GNUPG_LIBDIR
# undef GNUPG_DATADIR
# undef GNUPG_SYSCONFDIR
# undef GNUPG_LOCALSTATEDIR
# define GNUPG_BINDIR "INSTDIR/bin"
# define GNUPG_LIBEXECDIR "INSTDIR/bin"
# define GNUPG_LIBDIR "INSTDIR/lib/" PACKAGE_NAME
# define GNUPG_DATADIR "INSTDIR/share/" PACKAGE_NAME
# define GNUPG_SYSCONFDIR "APPDATA/GNU/etc/" PACKAGE_NAME
# define GNUPG_LOCALSTATEDIR "APPDATA/GNU"
#endif /*HAVE_W32_SYSTEM*/
#if USE_GPG2_HACK
# define gpg2_suffix "2"
#else
# define gpg2_suffix ""
#endif
static int verbose;
/* The usual free wrapper. */
static void
xfree (void *a)
{
if (a)
free (a);
}
static char *
xmalloc (size_t n)
{
char *p;
p = malloc (n);
if (!p)
{
fputs (PGM ": out of core\n", stderr);
exit (1);
}
return p;
}
static char *
xstrdup (const char *string)
{
char *p;
p = xmalloc (strlen (string)+1);
strcpy (p, string);
return p;
}
/* Return a malloced string with the last modification date of the
FILES. Returns NULL on error. */
static char *
get_date_from_files (char **files)
{
const char *file;
const char *usedfile = NULL;
struct stat sb;
struct tm *tp;
int errors = 0;
time_t stamp = 0;
char *result;
for (; (file = *files); files++)
{
if (!*file || !strcmp (file, ".") || !strcmp (file, ".."))
continue;
if (stat (file, &sb))
{
fprintf (stderr, PGM ": stat failed for '%s': %s\n",
file, strerror (errno));
errors = 1;
continue;
}
if (sb.st_mtime > stamp)
{
stamp = sb.st_mtime;
usedfile = file;
}
}
if (errors)
exit (1);
if (usedfile)
fprintf (stderr, PGM ": taking date from '%s'\n", usedfile);
tp = gmtime (&stamp);
if (!tp)
return NULL;
result = xmalloc (4+1+2+1+2+1);
snprintf (result, 4+1+2+1+2+1, "%04d-%02d-%02d",
tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday);
return result;
}
/* We need to escape file names for Texinfo. */
static void
print_filename (const char *prefix, const char *name)
{
const char *s;
fputs (prefix, stdout);
for (s=name; *s; s++)
switch (*s)
{
case '@': fputs ("@atchar{}", stdout); break;
case '{': fputs ("@lbracechar{}", stdout); break;
case '}': fputs ("@rbracechar{}", stdout); break;
case ',': fputs ("@comma{}", stdout); break;
case '\\':fputs ("@backslashchar{}", stdout); break;
case '#': fputs ("@hashchar{}", stdout); break;
default: putchar (*s); break;
}
putchar('\n');
}
int
main (int argc, char **argv)
{
int last_argc = -1;
char *opt_date = NULL;
int monthoff;
char *p, *pend;
size_t n;
/* Option parsing. */
if (argc)
{
argc--; argv++;
}
while (argc && last_argc != argc )
{
last_argc = argc;
if (!strcmp (*argv, "--"))
{
argc--; argv++;
break;
}
else if (!strcmp (*argv, "--help"))
{
fputs ("Usage: " PGM " [OPTION] [FILES]\n"
"Create defs.inc file.\nOptions:\n"
" -C DIR Change to DIR before doing anything\n"
" --date STRING Take publication date from STRING\n"
" --verbose Enable extra informational output\n"
" --help Display this help and exit\n"
, stdout);
exit (0);
}
else if (!strcmp (*argv, "--verbose"))
{
verbose = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "-C"))
{
argc--; argv++;
if (argc)
{
if (chdir (*argv))
{
fprintf (stderr, PGM ": chdir to '%s' failed: %s\n",
*argv, strerror (errno));
exit (1);
}
argc--; argv++;
}
}
else if (!strcmp (*argv, "--date"))
{
argc--; argv++;
if (argc)
{
opt_date = xstrdup (*argv);
argc--; argv++;
}
}
else if (!strncmp (*argv, "--", 2))
{
fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
exit (1);
}
}
if (opt_date && *opt_date)
{
time_t stamp;
struct tm *tp;
if (*opt_date == '2' && strlen (opt_date) >= 10
&& opt_date[4] == '-' && opt_date[7] == '-')
{
opt_date[10] = 0;
}
else if ((stamp = strtoul (opt_date, NULL, 10)) > 0
&& (tp = gmtime (&stamp)))
{
p = xmalloc (4+1+2+1+2+1);
snprintf (p, 4+1+2+1+2+1, "%04d-%02d-%02d",
tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday);
xfree (opt_date);
opt_date = p;
}
else
{
fprintf (stderr, PGM ": bad date '%s'\n", opt_date);
exit (1);
}
}
else
{
xfree (opt_date);
opt_date = argc? get_date_from_files (argv) : NULL;
}
if (!opt_date)
{
opt_date = xstrdup ("unknown");
monthoff = 0;
}
else
{
const char *month = "?";
switch (atoi (opt_date+5))
{
case 1: month = "January"; break;
case 2: month = "February"; break;
case 3: month = "March"; break;
case 4: month = "April"; break;
case 5: month = "May"; break;
case 6: month = "June"; break;
case 7: month = "July"; break;
case 8: month = "August"; break;
case 9: month = "September"; break;
case 10: month = "October"; break;
case 11: month = "November"; break;
case 12: month = "December"; break;
}
n = strlen (opt_date) + strlen (month) + 2 + 1;
p = xmalloc (n);
snprintf (p, n, "%d %n%s %d",
atoi (opt_date+8), &monthoff, month, atoi (opt_date));
xfree (opt_date);
opt_date = p;
}
fputs ("@c defs.inc -*- texinfo -*-\n"
"@c Common and build specific constants for the manuals.\n"
"@c This file has been created by " PGM ".\n\n", stdout);
fputs ("@ifclear defsincincluded\n"
"@set defsincincluded 1\n\n", stdout);
fputs ("\n@c Flags\n\n", stdout);
#if USE_GPG2_HACK
fputs ("@set gpgtwohack 1\n\n", stdout);
#endif
fputs ("\n@c Directories\n\n", stdout);
print_filename ("@set BINDIR ", GNUPG_BINDIR );
print_filename ("@set LIBEXECDIR ", GNUPG_LIBEXECDIR );
print_filename ("@set LIBDIR ", GNUPG_LIBDIR );
print_filename ("@set DATADIR ", GNUPG_DATADIR );
print_filename ("@set SYSCONFDIR ", GNUPG_SYSCONFDIR );
print_filename ("@set LOCALSTATEDIR ", GNUPG_LOCALSTATEDIR );
print_filename ("@set LOCALCACHEDIR ", (GNUPG_LOCALSTATEDIR
"/cache/" PACKAGE_NAME));
print_filename ("@set LOCALRUNDIR ", (GNUPG_LOCALSTATEDIR
"/run/" PACKAGE_NAME));
p = xstrdup (GNUPG_SYSCONFDIR);
pend = strrchr (p, '/');
fputs ("@set SYSCONFSKELDIR ", stdout);
if (pend)
{
*pend = 0;
fputs (p, stdout);
}
fputs ("/skel/." PACKAGE_NAME "\n", stdout);
xfree (p);
fputs ("\n@c Version information a la version.texi\n\n", stdout);
printf ("@set UPDATED %s\n", opt_date);
printf ("@set UPDATED-MONTH %s\n", opt_date + monthoff);
printf ("@set EDITION %s\n", PACKAGE_VERSION);
printf ("@set VERSION %s\n", PACKAGE_VERSION);
fputs ("\n@c Algorithm defaults\n\n", stdout);
/* Fixme: Use a config.h macro here: */
fputs ("@set GPGSYMENCALGO AES-128\n", stdout);
fputs ("\n@c Macros\n\n", stdout);
printf ("@macro gpgname\n%s%s\n@end macro\n", GPG_NAME, gpg2_suffix);
printf ("@macro gpgvname\n%sv%s\n@end macro\n", GPG_NAME, gpg2_suffix);
/* Trailer. */
fputs ("\n"
"@end ifclear\n"
"\n"
"@c Loc" "al Variables:\n"
"@c buffer-read-only: t\n"
"@c End:\n", stdout);
if (ferror (stdout))
{
fprintf (stderr, PGM ": error writing to stdout: %s\n", strerror (errno));
return 1;
}
return 0;
}
|