source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: implement -h (synopsis only) for preformatted (cat) pages;
Date: Mon, 10 Nov 2014 21:10:04 -0500 (EST)	[thread overview]
Message-ID: <201411110210.sAB2A4iB012587@krisdoz.my.domain> (raw)

Log Message:
-----------
implement -h (synopsis only) for preformatted (cat) pages;
requested by tedu@

Modified Files:
--------------
    mdocml:
        main.c

Revision Data
-------------
Index: main.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/main.c,v
retrieving revision 1.196
retrieving revision 1.197
diff -Lmain.c -Lmain.c -u -p -r1.196 -r1.197
--- main.c
+++ main.c
@@ -88,7 +88,7 @@ static	void		  mmsg(enum mandocerr, enum
 				const char *, int, int, const char *);
 static	void		  parse(struct curparse *, int,
 				const char *, enum mandoclevel *);
-static	enum mandoclevel  passthrough(const char *, int);
+static	enum mandoclevel  passthrough(const char *, int, int);
 static	void		  spawn_pager(void);
 static	int		  toptions(struct curparse *, char *);
 static	void		  usage(enum argmode) __attribute__((noreturn));
@@ -119,6 +119,7 @@ main(int argc, char *argv[])
 	int		 fd;
 	int		 show_usage;
 	int		 use_pager;
+	int		 synopsis_only;
 	int		 options;
 	int		 c;
 
@@ -155,6 +156,7 @@ main(int argc, char *argv[])
 
 	use_pager = 1;
 	show_usage = 0;
+	synopsis_only = 0;
 	outmode = OUTMODE_DEF;
 
 	while (-1 != (c = getopt(argc, argv,
@@ -174,6 +176,7 @@ main(int argc, char *argv[])
 			break;
 		case 'h':
 			(void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
+			synopsis_only = 1;
 			outmode = OUTMODE_ALL;
 			break;
 		case 'I':
@@ -389,7 +392,8 @@ main(int argc, char *argv[])
 				chdir(paths.paths[resp->ipath]);
 				parse(&curp, fd, resp->file, &rc);
 			} else
-				rc = passthrough(resp->file, fd);
+				rc = passthrough(resp->file, fd,
+				    synopsis_only);
 			resp++;
 		} else
 #endif
@@ -579,27 +583,63 @@ parse(struct curparse *curp, int fd, con
 }
 
 static enum mandoclevel
-passthrough(const char *file, int fd)
+passthrough(const char *file, int fd, int synopsis_only)
 {
-	char		 buf[BUFSIZ];
+	const char	 synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
+	const char	 synr[] = "SYNOPSIS";
+
+	FILE		*stream;
 	const char	*syscall;
-	ssize_t		 nr, nw, off;
+	char		*line;
+	size_t		 len, off;
+	ssize_t		 nw;
+	int		 print;
+
+	if ((stream = fdopen(fd, "r")) == NULL) {
+		close(fd);
+		syscall = "fdopen";
+		goto fail;
+	}
 
-	while ((nr = read(fd, buf, BUFSIZ)) != -1 && nr != 0)
-		for (off = 0; off < nr; off += nw)
-			if ((nw = write(STDOUT_FILENO, buf + off,
-			    (size_t)(nr - off))) == -1 || nw == 0) {
-				close(fd);
+	print = 0;
+	while ((line = fgetln(stream, &len)) != NULL) {
+		if (synopsis_only) {
+			if (print) {
+				if ( ! isspace((unsigned char)*line))
+					goto done;
+				while (len &&
+				    isspace((unsigned char)*line)) {
+					line++;
+					len--;
+				}
+			} else {
+				if ((len == sizeof(synb) &&
+				     ! strncmp(line, synb, len - 1)) ||
+				    (len == sizeof(synr) &&
+				     ! strncmp(line, synr, len - 1)))
+					print = 1;
+				continue;
+			}
+		}
+		for (off = 0; off < len; off += nw)
+			if ((nw = write(STDOUT_FILENO, line + off,
+			    len - off)) == -1 || nw == 0) {
+				fclose(stream);
 				syscall = "write";
 				goto fail;
 			}
+	}
 
-	close(fd);
+	if (ferror(stream)) {
+		fclose(stream);
+		syscall = "fgetln";
+		goto fail;
+	}
 
-	if (nr == 0)
-		return(MANDOCLEVEL_OK);
+done:
+	fclose(stream);
+	return(MANDOCLEVEL_OK);
 
-	syscall = "read";
 fail:
 	fprintf(stderr, "%s: %s: SYSERR: %s: %s",
 	    progname, file, syscall, strerror(errno));
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2014-11-11  2:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201411110210.sAB2A4iB012587@krisdoz.my.domain \
    --to=schwarze@mdocml.bsd.lv \
    --cc=source@mdocml.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).