source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* pod2mdoc: When -s is specified but -n is not, the filename extension is
@ 2014-07-11 20:45 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2014-07-11 20:45 UTC (permalink / raw)
  To: source

Log Message:
-----------
When -s is specified but -n is not, the filename extension is not needed
to determine the section number, but it still has to be trimmed from
the page title.  Keeping the code readable required a rewrite of the
first half of dofile().

Bug reported by bentley@, thanks!

Modified Files:
--------------
    pod2mdoc:
        pod2mdoc.c

Revision Data
-------------
Index: pod2mdoc.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/pod2mdoc/pod2mdoc.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -Lpod2mdoc.c -Lpod2mdoc.c -u -p -r1.28 -r1.29
--- pod2mdoc.c
+++ pod2mdoc.c
@@ -1178,24 +1178,40 @@ static void
 dofile(const struct args *args, const char *fname, 
 	const struct tm *tm, const char *buf, size_t sz)
 {
-	size_t		 sup, end, i, cur = 0;
-	struct state	 st;
-	const char	*section, *date;
 	char		 datebuf[64];
+	struct state	 st;
+	const char	*fbase, *fext, *section, *date;
 	char		*title, *cp;
+	size_t		 sup, end, i, cur = 0;
 
 	if (0 == sz)
 		return;
 
-	/* Title is last path component of the filename. */
+	/*
+	 * Parsing the filename is almost always required,
+	 * except when both the title and the section
+	 * are provided on the command line.
+	 */
+
+	if (NULL == args->title || NULL == args->section) {
+		fbase = strrchr(fname, '/');
+		if (NULL == fbase)
+			fbase = fname;
+		else
+			fbase++;
+		fext = strrchr(fbase, '.');
+	} else
+		fext = NULL;
+
+	/*
+	 * The title will be converted to uppercase,
+	 * so it needs to be copied.
+	 */
+
+	title = (NULL != args->title) ? strdup(args->title) :
+		(NULL != fext) ? strndup(fbase, fext - fbase) :
+		strdup(fbase);
 
-	if (NULL != args->title)
-		title = strdup(args->title);
-	else if (NULL != (cp = strrchr(fname, '/')))
-		title = strdup(cp + 1);
-	else
-		title = strdup(fname);
-	
 	if (NULL == title) {
 		perror(NULL);
 		exit(EXIT_FAILURE);
@@ -1203,14 +1219,9 @@ dofile(const struct args *args, const ch
 
 	/* Section is 1 unless suffix is "pm". */
 
-	if (NULL == (section = args->section)) {
-		section = "1";
-		if (NULL != (cp = strrchr(title, '.'))) {
-			*cp++ = '\0';
-			if (0 == strcmp(cp, "pm"))
-				section = PERL_SECTION;
-		}
-	} 
+	section = (NULL != args->section) ? args->section :
+	    (NULL == fext || strcmp(fext + 1, "pm")) ? "1" :
+	    PERL_SECTION;
 
 	/* Date.  Or the given "tm" if not supplied. */
 
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-07-11 20:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-11 20:45 pod2mdoc: When -s is specified but -n is not, the filename extension is schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).