1 /* mkdefsinc.c - Tool to create defs.inc
2 * Copyright (C) 2015 g10 Code GmbH
4 * This file is free software; as a special exception the author gives
5 * unlimited permission to copy and/or distribute it, with or without
6 * modifications, as long as this notice is preserved.
8 * This file is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
10 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 /* This tool needs to be build with command line supplied -D options
14 for the various directory variables. See ../am/cmacros.am. It is
15 easier to do this in build file than to use fragile make rules and
24 #include <sys/types.h>
28 #define PGM "mkdefsinc"
30 /* We include config.h after all include files because the config.h
31 values are not valid for the build platform but we need some values
34 /* When building for Windows the -D macros do not have appropriate
35 values. We provide replacements here. */
36 #ifdef HAVE_W32_SYSTEM
38 # undef GNUPG_LIBEXECDIR
41 # undef GNUPG_SYSCONFDIR
42 # undef GNUPG_LOCALSTATEDIR
43 # define GNUPG_BINDIR "INSTDIR/bin"
44 # define GNUPG_LIBEXECDIR "INSTDIR/bin"
45 # define GNUPG_LIBDIR "INSTDIR/lib/" PACKAGE_NAME
46 # define GNUPG_DATADIR "INSTDIR/share/" PACKAGE_NAME
47 # define GNUPG_SYSCONFDIR "APPDATA/GNU/etc/" PACKAGE_NAME
48 # define GNUPG_LOCALSTATEDIR "APPDATA/GNU"
49 #endif /*HAVE_W32_SYSTEM*/
53 # define gpg2_suffix "2"
55 # define gpg2_suffix ""
62 /* The usual free wrapper. */
79 fputs (PGM ": out of core\n", stderr);
87 xstrdup (const char *string)
91 p = xmalloc (strlen (string)+1);
97 /* Return a malloced string with the last modification date of the
98 FILES. Returns NULL on error. */
100 get_date_from_files (char **files)
103 const char *usedfile = NULL;
110 for (; (file = *files); files++)
112 if (!*file || !strcmp (file, ".") || !strcmp (file, ".."))
114 if (stat (file, &sb))
116 fprintf (stderr, PGM ": stat failed for '%s': %s\n",
117 file, strerror (errno));
121 if (sb.st_mtime > stamp)
131 fprintf (stderr, PGM ": taking date from '%s'\n", usedfile);
133 tp = gmtime (&stamp);
136 result = xmalloc (4+1+2+1+2+1);
137 snprintf (result, 4+1+2+1+2+1, "%04d-%02d-%02d",
138 tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday);
143 /* We need to escape file names for Texinfo. */
145 print_filename (const char *prefix, const char *name)
149 fputs (prefix, stdout);
150 for (s=name; *s; s++)
153 case '@': fputs ("@atchar{}", stdout); break;
154 case '{': fputs ("@lbracechar{}", stdout); break;
155 case '}': fputs ("@rbracechar{}", stdout); break;
156 case ',': fputs ("@comma{}", stdout); break;
157 case '\\':fputs ("@backslashchar{}", stdout); break;
158 case '#': fputs ("@hashchar{}", stdout); break;
159 default: putchar (*s); break;
166 main (int argc, char **argv)
169 char *opt_date = NULL;
174 /* Option parsing. */
179 while (argc && last_argc != argc )
182 if (!strcmp (*argv, "--"))
187 else if (!strcmp (*argv, "--help"))
189 fputs ("Usage: " PGM " [OPTION] [FILES]\n"
190 "Create defs.inc file.\nOptions:\n"
191 " -C DIR Change to DIR before doing anything\n"
192 " --date STRING Take publication date from STRING\n"
193 " --verbose Enable extra informational output\n"
194 " --help Display this help and exit\n"
198 else if (!strcmp (*argv, "--verbose"))
203 else if (!strcmp (*argv, "-C"))
210 fprintf (stderr, PGM ": chdir to '%s' failed: %s\n",
211 *argv, strerror (errno));
217 else if (!strcmp (*argv, "--date"))
222 opt_date = xstrdup (*argv);
226 else if (!strncmp (*argv, "--", 2))
228 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
233 if (opt_date && *opt_date)
238 if (*opt_date == '2' && strlen (opt_date) >= 10
239 && opt_date[4] == '-' && opt_date[7] == '-')
243 else if ((stamp = strtoul (opt_date, NULL, 10)) > 0
244 && (tp = gmtime (&stamp)))
246 p = xmalloc (4+1+2+1+2+1);
247 snprintf (p, 4+1+2+1+2+1, "%04d-%02d-%02d",
248 tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday);
254 fprintf (stderr, PGM ": bad date '%s'\n", opt_date);
261 opt_date = argc? get_date_from_files (argv) : NULL;
265 opt_date = xstrdup ("unknown");
270 const char *month = "?";
272 switch (atoi (opt_date+5))
274 case 1: month = "January"; break;
275 case 2: month = "February"; break;
276 case 3: month = "March"; break;
277 case 4: month = "April"; break;
278 case 5: month = "May"; break;
279 case 6: month = "June"; break;
280 case 7: month = "July"; break;
281 case 8: month = "August"; break;
282 case 9: month = "September"; break;
283 case 10: month = "October"; break;
284 case 11: month = "November"; break;
285 case 12: month = "December"; break;
287 n = strlen (opt_date) + strlen (month) + 2 + 1;
289 snprintf (p, n, "%d %n%s %d",
290 atoi (opt_date+8), &monthoff, month, atoi (opt_date));
296 fputs ("@c defs.inc -*- texinfo -*-\n"
297 "@c Common and build specific constants for the manuals.\n"
298 "@c This file has been created by " PGM ".\n\n", stdout);
300 fputs ("@ifclear defsincincluded\n"
301 "@set defsincincluded 1\n\n", stdout);
304 fputs ("\n@c Flags\n\n", stdout);
307 fputs ("@set gpgtwohack 1\n\n", stdout);
310 fputs ("\n@c Directories\n\n", stdout);
312 print_filename ("@set BINDIR ", GNUPG_BINDIR );
313 print_filename ("@set LIBEXECDIR ", GNUPG_LIBEXECDIR );
314 print_filename ("@set LIBDIR ", GNUPG_LIBDIR );
315 print_filename ("@set DATADIR ", GNUPG_DATADIR );
316 print_filename ("@set SYSCONFDIR ", GNUPG_SYSCONFDIR );
317 print_filename ("@set LOCALSTATEDIR ", GNUPG_LOCALSTATEDIR );
318 print_filename ("@set LOCALCACHEDIR ", (GNUPG_LOCALSTATEDIR
319 "/cache/" PACKAGE_NAME));
320 print_filename ("@set LOCALRUNDIR ", (GNUPG_LOCALSTATEDIR
321 "/run/" PACKAGE_NAME));
323 p = xstrdup (GNUPG_SYSCONFDIR);
324 pend = strrchr (p, '/');
325 fputs ("@set SYSCONFSKELDIR ", stdout);
331 fputs ("/skel/." PACKAGE_NAME "\n", stdout);
334 fputs ("\n@c Version information a la version.texi\n\n", stdout);
336 printf ("@set UPDATED %s\n", opt_date);
337 printf ("@set UPDATED-MONTH %s\n", opt_date + monthoff);
338 printf ("@set EDITION %s\n", PACKAGE_VERSION);
339 printf ("@set VERSION %s\n", PACKAGE_VERSION);
341 fputs ("\n@c Algorithm defaults\n\n", stdout);
343 /* Fixme: Use a config.h macro here: */
344 fputs ("@set GPGSYMENCALGO AES-128\n", stdout);
346 fputs ("\n@c Macros\n\n", stdout);
348 printf ("@macro gpgname\n%s%s\n@end macro\n", GPG_NAME, gpg2_suffix);
349 printf ("@macro gpgvname\n%sv%s\n@end macro\n", GPG_NAME, gpg2_suffix);
356 "@c Loc" "al Variables:\n"
357 "@c buffer-read-only: t\n"
358 "@c End:\n", stdout);
362 fprintf (stderr, PGM ": error writing to stdout: %s\n", strerror (errno));