1From 5b40a091f4d31fa6d21a84536f5998135faf6dfa Mon Sep 17 00:00:00 2001
2From: schwarze <schwarze>
3Date: Tue, 13 Nov 2018 08:45:29 +0000
4Subject: [PATCH] Add the -d option to specify the .Dd date, and fall back to
5 the mtime before resorting to the current time. Patch from Michael Forney
6 <mforney at mforney dot org> with minimal tweaks by me.
7Upstream: Yes, main.c@1.72 util.c@1.35
8
9While here, fix the date format, "%F" is wrong for mdoc(7).
10Change it to "%B %e, %Y" which isn't perfect due to the
11spurious blank before single-digit day numbers, but closer.
12---
13 extern.h | 1 +
14 main.c | 35 ++++++++++++++++++++++++++---------
15 texi2mdoc.1 | 14 +++++++++++++-
16 util.c | 1 -
17 4 files changed, 40 insertions(+), 11 deletions(-)
18
19diff --git a/extern.h b/extern.h
20index f713e0b..643edde 100644
21--- a/extern.h
22+++ b/extern.h
23@@ -365,6 +365,7 @@ struct texi {
24 size_t dirsz; /* number of texi directories */
25 char *title; /* title of document */
26 char *subtitle; /* subtitle of document */
27+ char *date; /* date of document */
28 int secoffs; /* see sectioner() */
29 char **indexs; /* @defindex indices */
30 size_t indexsz; /* entries in indexs */
31diff --git a/main.c b/main.c
32index d343727..308fe5e 100644
33--- a/main.c
34+++ b/main.c
35@@ -17,6 +17,9 @@
36 #if defined(__linux__) || defined(__MINT__)
37 # define _GNU_SOURCE /* memmem */
38 #endif
39+
40+#include <sys/stat.h>
41+
42 #include <assert.h>
43 #include <ctype.h>
44 #include <getopt.h>
45@@ -1587,8 +1590,6 @@ static void
46 dotop(struct texi *p, enum texicmd cmd, size_t *pos)
47 {
48 const char *cp;
49- time_t t;
50- char date[32];
51
52 if (--p->ign)
53 texierr(p, "@top command while ignoring");
54@@ -1598,13 +1599,11 @@ dotop(struct texi *p, enum texicmd cmd, size_t *pos)
55 * We use the title set with @settitle for the `Nd' description
56 * and the source document filename (the first one as invoked on
57 * the command line) for the title.
58- * The date is set to the current date.
59+ * The date is set to the modification time of the input.
60 */
61- t = time(NULL);
62- strftime(date, sizeof(date), "%F", localtime(&t));
63
64 teximacroopen(p, "Dd");
65- texiputchars(p, date);
66+ texiputchars(p, p->date);
67 teximacroclose(p);
68 teximacroopen(p, "Dt");
69 for (cp = p->title; '\0' != *cp; cp++)
70@@ -1864,9 +1863,12 @@ int
71 main(int argc, char *argv[])
72 {
73 struct texi texi;
74- int c;
75+ char date[32];
76+ struct stat st;
77 char *dirpath, *dir, *ccp;
78 const char *progname, *Idir, *cp;
79+ time_t t;
80+ int c;
81
82 progname = strrchr(argv[0], '/');
83 if (progname == NULL)
84@@ -1878,8 +1880,11 @@ main(int argc, char *argv[])
85 texi.ign = 1;
86 Idir = NULL;
87
88- while (-1 != (c = getopt(argc, argv, "I:")))
89+ while (-1 != (c = getopt(argc, argv, "d:I:")))
90 switch (c) {
91+ case ('d'):
92+ texi.date = optarg;
93+ break;
94 case ('I'):
95 Idir = optarg;
96 break;
97@@ -1905,16 +1910,28 @@ main(int argc, char *argv[])
98 *ccp = '\0';
99 texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
100 free(dirpath);
101+ if (NULL == texi.date) {
102+ t = stat(argv[0], &st) == 0 ? st.st_mtime : time(NULL);
103+ strftime(date, sizeof(date),
104+ "%B %e, %Y", localtime(&t));
105+ texi.date = date;
106+ }
107 parsefile(&texi, argv[0], 1);
108 } else {
109 texi.title = strdup("Unknown Manual");
110 texi.dirs = parsedirs(&texi, NULL, Idir, &texi.dirsz);
111+ if (NULL == texi.date) {
112+ t = time(NULL);
113+ strftime(date, sizeof(date),
114+ "%B %e, %Y", localtime(&t));
115+ texi.date = date;
116+ }
117 parsestdin(&texi);
118 }
119
120 texiexit(&texi);
121 return(EXIT_FAILURE);
122 usage:
123- fprintf(stderr, "usage: %s [-Idirs] [file]\n", progname);
124+ fprintf(stderr, "usage: %s [-d date] [-I dirs] [file]\n", progname);
125 return(EXIT_FAILURE);
126 }
127diff --git a/texi2mdoc.1 b/texi2mdoc.1
128index 9f8169e..f479adc 100644
129--- a/texi2mdoc.1
130+++ b/texi2mdoc.1
131@@ -14,7 +14,7 @@
132 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
133 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
134 .\"
135-.Dd $Mdocdate: February 25 2015 $
136+.Dd $Mdocdate: November 13 2018 $
137 .Dt TEXI2MDOC 1
138 .Os
139 .Sh NAME
140@@ -22,6 +22,7 @@
141 .Nd convert texinfo documents to mdoc
142 .Sh SYNOPSIS
143 .Nm texi2mdoc
144+.Op Fl d Ar date
145 .Op Fl I Ar dirs
146 .Op Ar file
147 .Sh DESCRIPTION
148@@ -39,6 +40,17 @@ By default,
149 reads from standard input.
150 Its arguments are as follows:
151 .Bl -tag -width Ds
152+.It Fl d Ar date
153+Set the output document date in the
154+.Ic \&Dd
155+macro to
156+.Ar date ,
157+to be specified in the format
158+.Dq Ar Month Day , Year .
159+If unspecified,
160+.Nm
161+uses the file modification date
162+or the current date when reading from standard input.
163 .It Fl I Ar dirs
164 Colon-separated directories to search for
165 .Li @include
166diff --git a/util.c b/util.c
167index 944872f..c6de818 100644
168--- a/util.c
169+++ b/util.c
170@@ -27,7 +27,6 @@
171 #include <stdio.h>
172 #include <stdlib.h>
173 #include <string.h>
174-#include <time.h>
175 #include <unistd.h>
176
177 #include "extern.h"
178--
1792.19.1
180