tech@mandoc.bsd.lv
 help / color / Atom feed
* [texi2mdoc] [PATCH] Use mtime of input file for date, and allow override from command-line
@ 2018-11-13  1:00 Michael Forney
  2018-11-13  8:50 ` Ingo Schwarze
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Forney @ 2018-11-13  1:00 UTC (permalink / raw)
  To: tech

Hi,

I'm using texi2mdoc to generate ffmpeg manuals, and thought it would
be useful to be a bit smarter about choosing the document date so that
the output is reproducible.

This patch changes texi2mdoc to use the mtime of the input file if
given by name, falling back to the current time if reading from stdin
or stat fails. In either case, the date can be given explicitly with
the -d flag.

Index: extern.h
===================================================================
RCS file: /cvs/texi2mdoc/extern.h,v
retrieving revision 1.29
diff -u -p -r1.29 extern.h
--- extern.h	12 Mar 2015 10:44:34 -0000	1.29
+++ extern.h	13 Nov 2018 00:53:02 -0000
@@ -422,6 +422,7 @@ struct	texi {
 	size_t		  valstackpos; /* position in valstack */
 	char		 *title; /* title of document */
 	char		 *subtitle; /* subtitle of document */
+	char		 *date; /* date of document */
 	int		  secoffs; /* see sectioner() */
 	struct texidex 	 *indexs; /* index entries */
 	size_t		  indexsz; /* entries in indexs */
Index: main.c
===================================================================
RCS file: /cvs/texi2mdoc/main.c,v
retrieving revision 1.71
diff -u -p -r1.71 main.c
--- main.c	19 Mar 2015 09:53:35 -0000	1.71
+++ main.c	13 Nov 2018 00:53:03 -0000
@@ -26,6 +26,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <time.h>
 #include <unistd.h>

 #include "extern.h"
@@ -2342,8 +2344,10 @@ main(int argc, char *argv[])
 {
 	struct texi	 texi;
 	int		 c;
-	char		*dirpath, *dir, *ccp;
+	char		*dirpath, *dir, *ccp, date[32];
 	const char	*progname, *Idir, *cp;
+	struct stat	 st;
+	time_t		 t;

 	progname = strrchr(argv[0], '/');
 	if (progname == NULL)
@@ -2357,11 +2361,14 @@ main(int argc, char *argv[])
 	texi.seenvs = -1;
 	Idir = NULL;

-	while (-1 != (c = getopt(argc, argv, "C:I:")))
+	while (-1 != (c = getopt(argc, argv, "C:d:I:")))
 		switch (c) {
 		case ('C'):
 			texi.chapters = optarg;
 			break;
+		case ('d'):
+			texi.date = optarg;
+			break;
 		case ('I'):
 			Idir = optarg;
 			break;
@@ -2393,16 +2400,26 @@ main(int argc, char *argv[])
 			*ccp = '\0';
 		texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
 		free(dirpath);
+		if (NULL == texi.date) {
+			t = stat(argv[0], &st) == 0 ? st.st_mtime : time(NULL);
+			strftime(date, sizeof(date), "%F", localtime(&t));
+			texi.date = date;
+		}
 		parsefile(&texi, argv[0], 1);
 	} else {
 		texi.title = strdup("Unknown Manual");
 		texi.dirs = parsedirs(&texi, NULL, Idir, &texi.dirsz);
+		if (NULL == texi.date) {
+			t = time(NULL);
+			strftime(date, sizeof(date), "%F", localtime(&t));
+			texi.date = date;
+		}
 		parsestdin(&texi);
 	}

 	texiexit(&texi);
 	exit(EXIT_SUCCESS);
 usage:
-	fprintf(stderr, "usage: %s [-Cdir] [-Idirs] [file]\n", progname);
+	fprintf(stderr, "usage: %s [-Cdir] [-ddate] [-Idirs] [file]\n", progname);
 	return(EXIT_FAILURE);
 }
Index: texi2mdoc.1
===================================================================
RCS file: /cvs/texi2mdoc/texi2mdoc.1,v
retrieving revision 1.15
diff -u -p -r1.15 texi2mdoc.1
--- texi2mdoc.1	12 Mar 2015 04:24:19 -0000	1.15
+++ texi2mdoc.1	13 Nov 2018 00:53:03 -0000
@@ -23,6 +23,7 @@
 .Sh SYNOPSIS
 .Nm texi2mdoc
 .Op Fl C Ar dir
+.Op Fl d Ar date
 .Op Fl I Ar dirs
 .Op Ar file
 .Sh DESCRIPTION
@@ -47,6 +48,10 @@ node-level documents placed within
 which must exist.
 These are created as
 .Pa prefix-NNN.7 .
+.It Fl d Ar date
+Set the output document date to
+.Ar date
+instead of the modification time of the input file.
 .It Fl I Ar dirs
 Colon-separated directories to search for
 .Li @include
Index: util.c
===================================================================
RCS file: /cvs/texi2mdoc/util.c,v
retrieving revision 1.34
diff -u -p -r1.34 util.c
--- util.c	19 Mar 2015 09:53:36 -0000	1.34
+++ util.c	13 Nov 2018 00:53:03 -0000
@@ -25,7 +25,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <unistd.h>

 #include "extern.h"
@@ -1731,21 +1730,16 @@ texicache(struct texi *p, const char *bu
  * We use the title set with @settitle for the `Nd' description
  * and the source document filename (the first one as invoked on
  * the command line) for the title.
- * The date is set to the current date.
+ * The date is set to the modification time of the input.
  */
 void
 teximdocopen(struct texi *p, size_t *pos)
 {
 	const char	*cp;
-	time_t		 t;
-	char		 date[32];
-
-	t = time(NULL);
-	strftime(date, sizeof(date), "%F", localtime(&t));

 	p->seenvs = -1;
 	teximacroopen(p, "Dd");
-	texiputchars(p, date);
+	texiputchars(p, p->date);
 	teximacroclose(p);
 	teximacroopen(p, "Dt");
 	for (cp = p->title; '\0' != *cp; cp++)
--
 To unsubscribe send an email to tech+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [texi2mdoc] [PATCH] Use mtime of input file for date, and allow override from command-line
  2018-11-13  1:00 [texi2mdoc] [PATCH] Use mtime of input file for date, and allow override from command-line Michael Forney
@ 2018-11-13  8:50 ` Ingo Schwarze
  2018-11-13  9:12   ` Michael Forney
  0 siblings, 1 reply; 4+ messages in thread
From: Ingo Schwarze @ 2018-11-13  8:50 UTC (permalink / raw)
  To: Michael Forney; +Cc: tech

Hi Michael,

Michael Forney wrote on Mon, Nov 12, 2018 at 05:00:13PM -0800:

> I'm using texi2mdoc to generate ffmpeg manuals,

Just personally, or are you somehow affiliated with the ffmpeg
project (beyond sending occasional patches) and is that project
considering to use texi2mdoc(1)?

> and thought it would be useful to be a bit smarter about choosing
> the document date so that the output is reproducible.

True.

> This patch changes texi2mdoc to use the mtime of the input file if
> given by name, falling back to the current time if reading from stdin
> or stat fails. In either case, the date can be given explicitly with
> the -d flag.

I agree, that's the right thing too do, pod2mdoc(1) behaves in a
similar way.

So i committed your patch with minimal tweaks (sorting headers,
sorting stack variables roughly by size, making the manual page
more similar too pod2mdoc(1)), also tweaking two nits while here:
%F was the wrong format for mdoc(7), and the usage string lacked
blanks between option letters and option arguments.

Thanks for the patch,
  Ingo


Log Message:
-----------
Add the -d option to specify the .Dd date,
and fall back to the mtime before resorting to the current time.
Patch from Michael Forney <mforney at mforney dot org>
with minimal tweaks by me.

While here, fix the date format, "%F" is wrong for mdoc(7).
Change it to "%B %e, %Y" which isn't perfect due to the
spurious blank before single-digit day numbers, but closer.

Modified Files:
--------------
    texi2mdoc:
        extern.h
        main.c
        texi2mdoc.1
        util.c

Revision Data
-------------
Index: texi2mdoc.1
===================================================================
RCS file: /home/cvs/mdocml/texi2mdoc/texi2mdoc.1,v
retrieving revision 1.15
retrieving revision 1.16
diff -Ltexi2mdoc.1 -Ltexi2mdoc.1 -u -p -r1.15 -r1.16
--- texi2mdoc.1
+++ texi2mdoc.1
@@ -23,6 +23,7 @@
 .Sh SYNOPSIS
 .Nm texi2mdoc
 .Op Fl C Ar dir
+.Op Fl d Ar date
 .Op Fl I Ar dirs
 .Op Ar file
 .Sh DESCRIPTION
@@ -47,6 +48,17 @@ node-level documents placed within
 which must exist.
 These are created as
 .Pa prefix-NNN.7 .
+.It Fl d Ar date
+Set the output document date in the
+.Ic \&Dd
+macro to
+.Ar date ,
+to be specified in the format
+.Dq Ar Month Day , Year .
+If unspecified,
+.Nm
+uses the file modification date
+or the current date when reading from standard input.
 .It Fl I Ar dirs
 Colon-separated directories to search for
 .Li @include
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/texi2mdoc/main.c,v
retrieving revision 1.71
retrieving revision 1.72
diff -Lmain.c -Lmain.c -u -p -r1.71 -r1.72
--- main.c
+++ main.c
@@ -17,6 +17,9 @@
 #if defined(__linux__) || defined(__MINT__)
 # define _GNU_SOURCE /* memmem */
 #endif
+
+#include <sys/stat.h>
+
 #include <assert.h>
 #include <ctype.h>
 #include <getopt.h>
@@ -26,6 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 
 #include "extern.h"
@@ -2341,9 +2345,12 @@ int
 main(int argc, char *argv[])
 {
 	struct texi	 texi;
-	int		 c;
+	char		 date[32];
+	struct stat	 st;
 	char		*dirpath, *dir, *ccp;
 	const char	*progname, *Idir, *cp;
+	time_t		 t;
+	int		 c;
 
 	progname = strrchr(argv[0], '/');
 	if (progname == NULL)
@@ -2357,11 +2364,14 @@ main(int argc, char *argv[])
 	texi.seenvs = -1;
 	Idir = NULL;
 
-	while (-1 != (c = getopt(argc, argv, "C:I:")))
+	while (-1 != (c = getopt(argc, argv, "C:d:I:")))
 		switch (c) {
 		case ('C'):
 			texi.chapters = optarg;
 			break;
+		case ('d'):
+			texi.date = optarg;
+			break;
 		case ('I'):
 			Idir = optarg;
 			break;
@@ -2393,16 +2403,29 @@ main(int argc, char *argv[])
 			*ccp = '\0';
 		texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
 		free(dirpath);
+		if (NULL == texi.date) {
+			t = stat(argv[0], &st) == 0 ? st.st_mtime : time(NULL);
+			strftime(date, sizeof(date),
+			     "%B %e, %Y", localtime(&t));
+			texi.date = date;
+		}
 		parsefile(&texi, argv[0], 1);
 	} else {
 		texi.title = strdup("Unknown Manual");
 		texi.dirs = parsedirs(&texi, NULL, Idir, &texi.dirsz);
+		if (NULL == texi.date) {
+			t = time(NULL);
+			strftime(date, sizeof(date),
+			     "%B %e, %Y", localtime(&t));
+			texi.date = date;
+		}
 		parsestdin(&texi);
 	}
 
 	texiexit(&texi);
 	exit(EXIT_SUCCESS);
 usage:
-	fprintf(stderr, "usage: %s [-Cdir] [-Idirs] [file]\n", progname);
+	fprintf(stderr, "usage: %s [-C dir] [-d date] [-I dirs] [file]\n",
+            progname);
 	return(EXIT_FAILURE);
 }
Index: util.c
===================================================================
RCS file: /home/cvs/mdocml/texi2mdoc/util.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -Lutil.c -Lutil.c -u -p -r1.34 -r1.35
--- util.c
+++ util.c
@@ -25,7 +25,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <unistd.h>
 
 #include "extern.h"
@@ -1731,21 +1730,16 @@ texicache(struct texi *p, const char *bu
  * We use the title set with @settitle for the `Nd' description
  * and the source document filename (the first one as invoked on
  * the command line) for the title.
- * The date is set to the current date.
+ * The date is set to the modification time of the input.
  */
 void
 teximdocopen(struct texi *p, size_t *pos)
 {
 	const char	*cp;
-	time_t		 t;
-	char		 date[32];
-
-	t = time(NULL);
-	strftime(date, sizeof(date), "%F", localtime(&t));
 
 	p->seenvs = -1;
 	teximacroopen(p, "Dd");
-	texiputchars(p, date);
+	texiputchars(p, p->date);
 	teximacroclose(p);
 	teximacroopen(p, "Dt");
 	for (cp = p->title; '\0' != *cp; cp++)
Index: extern.h
===================================================================
RCS file: /home/cvs/mdocml/texi2mdoc/extern.h,v
retrieving revision 1.29
retrieving revision 1.30
diff -Lextern.h -Lextern.h -u -p -r1.29 -r1.30
--- extern.h
+++ extern.h
@@ -422,6 +422,7 @@ struct	texi {
 	size_t		  valstackpos; /* position in valstack */
 	char		 *title; /* title of document */
 	char		 *subtitle; /* subtitle of document */
+	char		 *date; /* date of document */
 	int		  secoffs; /* see sectioner() */
 	struct texidex 	 *indexs; /* index entries */
 	size_t		  indexsz; /* entries in indexs */
--
 To unsubscribe send an email to tech+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [texi2mdoc] [PATCH] Use mtime of input file for date, and allow override from command-line
  2018-11-13  8:50 ` Ingo Schwarze
@ 2018-11-13  9:12   ` Michael Forney
  2018-11-13  9:20     ` Ingo Schwarze
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Forney @ 2018-11-13  9:12 UTC (permalink / raw)
  To: Ingo Schwarze; +Cc: tech

On 2018-11-13, Ingo Schwarze <schwarze@usta.de> wrote:
> Hi Michael,
>
> Michael Forney wrote on Mon, Nov 12, 2018 at 05:00:13PM -0800:
>
>> I'm using texi2mdoc to generate ffmpeg manuals,
>
> Just personally, or are you somehow affiliated with the ffmpeg
> project (beyond sending occasional patches) and is that project
> considering to use texi2mdoc(1)?

Just personally in my build scripts for ffmpeg to avoid a build-time
dependency on perl. They use a bundled texi2pod.pl from gcc, followed
by pod2man to generate the manuals, and they are not pre-generated in
the source tar.

>> This patch changes texi2mdoc to use the mtime of the input file if
>> given by name, falling back to the current time if reading from stdin
>> or stat fails. In either case, the date can be given explicitly with
>> the -d flag.
>
> I agree, that's the right thing too do, pod2mdoc(1) behaves in a
> similar way.
>
> So i committed your patch with minimal tweaks (sorting headers,
> sorting stack variables roughly by size, making the manual page
> more similar too pod2mdoc(1)), also tweaking two nits while here:
> %F was the wrong format for mdoc(7), and the usage string lacked
> blanks between option letters and option arguments.

Great, thank you!
--
 To unsubscribe send an email to tech+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [texi2mdoc] [PATCH] Use mtime of input file for date, and allow override from command-line
  2018-11-13  9:12   ` Michael Forney
@ 2018-11-13  9:20     ` Ingo Schwarze
  0 siblings, 0 replies; 4+ messages in thread
From: Ingo Schwarze @ 2018-11-13  9:20 UTC (permalink / raw)
  To: Michael Forney; +Cc: tech

Hi Micheal,

Michael Forney wrote on Tue, Nov 13, 2018 at 01:12:47AM -0800:

> Just personally in my build scripts for ffmpeg

I see.

> They use a bundled texi2pod.pl from gcc, followed by pod2man
> to generate the manuals, and they are not pre-generated in
> the source tar.

Yikes.

At some point, texi2mdoc(1) might also help them upstream,
but it is not yet mature enough to be advertised, and very
little work is done on texi2mdoc(1) nowadays.

Yours,
  Ingo
--
 To unsubscribe send an email to tech+unsubscribe@mandoc.bsd.lv

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-13  1:00 [texi2mdoc] [PATCH] Use mtime of input file for date, and allow override from command-line Michael Forney
2018-11-13  8:50 ` Ingo Schwarze
2018-11-13  9:12   ` Michael Forney
2018-11-13  9:20     ` Ingo Schwarze

tech@mandoc.bsd.lv

Archives are clonable: git clone --mirror http://inbox.vuxu.org/mandoc-tech

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.mandoc.tech


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git