From 81dc4dbad250ff8e9433eea76c1cc15649a2f927 Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Tue, 28 Mar 2023 17:44:29 +0000
Subject: [PATCH] move parse_xrefs back next to main

Move parse_xrefs() back in its proper place as a helper to main(),
including the definition of the global variable 'infile' with the rest
of the command line arguments.

It had been moved in fbbe953f when the content processing code was
confusedly hooked into the function.

Also removes marker comments about "Start/End xref parsing". The code
between them is not exclusively concerned with xrefs and their sheer
size clashes with the rest of the coding style.
---
 pdf.c | 150 ++++++++++++++++++++++++++--------------------------------
 1 file changed, 67 insertions(+), 83 deletions(-)

diff --git a/pdf.c b/pdf.c
index 3b28218..1761f14 100644
--- a/pdf.c
+++ b/pdf.c
@@ -4415,11 +4415,6 @@ process_page_content(struct Env *aux)
 
 
 
-/*
- * ********************************************************************
- * Start xref parsing
- * ********************************************************************
- */
 /*
  * decode the bytes in 'b' according to metadata in the stream dictionary 'd'
  * and parse the result with 'p'.
@@ -4876,6 +4871,73 @@ kxstream(HAllocator *mm__, const HParsedToken *x, void *env)
 }
 
 
+/*
+ * main program
+ */
+
+#include <stdio.h>
+#include <string.h>	/* strtok() */
+#include <inttypes.h>
+#include <fcntl.h>	/* open() */
+#include <unistd.h>	/* lseek(), getopt() */
+#include <sys/mman.h>	/* mmap() */
+#include <errno.h>
+
+/* used with getopt() */
+extern char *optarg;
+extern int optind;
+
+/* command line arguments */
+const char *progname;
+const char *infile;
+const char *xfile, *Xfile;
+int qflag, vflag, dflag;
+int onum = -1, ogen = -1;	/* -1 means unspecified */
+
+/* helper: parse 's' as a natural number. returns -1 on error. */
+int
+strtonat(const char *s)
+{
+	char *p;
+	long l;
+
+	errno = 0;
+	l = strtol(s, &p, 10);
+	if (errno != 0)
+		return -1;
+	if (p == s || *p != '\0')	/* no parse */
+		return -1;
+	if (l < 0 || l > INT_MAX)	/* out of range */
+		return -1;
+	return l;
+}
+
+void
+usage(void)
+{
+	fprintf(stderr, "usage: %s [-qsv] [-d what] [-x out.txt] file [oid]\n",
+	    progname);
+	exit(2);
+}
+
+void
+dumpstream(FILE *f, const HParsedToken *obj)
+{
+	HParseResult *res;
+	HBytes data;
+
+	// XXX properly verify that obj is a stream (needs custom token type)
+	if (obj->token_type != TT_SEQUENCE || obj->seq->used != 2 ||
+	    obj->seq->elements[1]->token_type != TT_HParseResult)
+		errx(2, "%s: requested object is not a stream", infile);
+
+	res = H_INDEX(HParseResult, obj, 1);
+	assert(res != NULL);
+	assert(res->ast != NULL);
+	data = H_CAST_BYTES(res->ast);
+
+	fwrite(data.token, 1, data.len, f);
+}
 
 /*
  * This helper implements the standard backwards parsing strategy to read all
@@ -4887,8 +4949,6 @@ kxstream(HAllocator *mm__, const HParsedToken *x, void *env)
  * number of elements.
  */
 // XXX review changes to this function. see git -L /^parse_xrefs/,/^}/:pdf.c'
-const char *infile = NULL;
-
 void
 parse_xrefs(struct Env *aux)
 {
@@ -4981,82 +5041,6 @@ end:
 	aux->nxrefs = n;
 }
 
-/*
- * ********************************************************************
- * End xref parsing
- * ********************************************************************
- */
-
-
-
-
-/*
- * main program
- */
-
-#include <stdio.h>
-#include <string.h>	/* strtok() */
-#include <inttypes.h>
-#include <fcntl.h>	/* open() */
-#include <unistd.h>	/* lseek(), getopt() */
-#include <sys/mman.h>	/* mmap() */
-#include <errno.h>
-
-/* used with getopt() */
-extern char *optarg;
-extern int optind;
-
-/* command line arguments */
-const char *progname;
-const char *xfile, *Xfile;
-int qflag, vflag, dflag;
-int onum = -1, ogen = -1;	/* -1 means unspecified */
-
-/* helper: parse 's' as a natural number. returns -1 on error. */
-int
-strtonat(const char *s)
-{
-	char *p;
-	long l;
-
-	errno = 0;
-	l = strtol(s, &p, 10);
-	if (errno != 0)
-		return -1;
-	if (p == s || *p != '\0')	/* no parse */
-		return -1;
-	if (l < 0 || l > INT_MAX)	/* out of range */
-		return -1;
-	return l;
-}
-
-void
-usage(void)
-{
-	fprintf(stderr, "usage: %s [-qsv] [-d what] [-x out.txt] file [oid]\n",
-	    progname);
-	exit(2);
-}
-
-void
-dumpstream(FILE *f, const HParsedToken *obj)
-{
-	HParseResult *res;
-	HBytes data;
-
-	// XXX properly verify that obj is a stream (needs custom token type)
-	if (obj->token_type != TT_SEQUENCE || obj->seq->used != 2 ||
-	    obj->seq->elements[1]->token_type != TT_HParseResult)
-		errx(2, "%s: requested object is not a stream", infile);
-
-	res = H_INDEX(HParseResult, obj, 1);
-	assert(res != NULL);
-	assert(res->ast != NULL);
-	data = H_CAST_BYTES(res->ast);
-
-	fwrite(data.token, 1, data.len, f);
-}
-
 int
 main(int argc, char *argv[])
 {
-- 
GitLab