1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
Add support for uncompressing gzipped man pages.
If the requested page does not exist, try to serve gzipped one instead (closes: #418771).
diff -ruN -x '*.rej' -x '*.orig' man-1.6e-old/man2html/Makefile.in man-1.6e/man2html/Makefile.in
--- man-1.6e-old/man2html/Makefile.in 2007-04-16 23:16:14.000000000 +0200
+++ man-1.6e/man2html/Makefile.in 2007-04-16 22:55:37.000000000 +0200
@@ -1,4 +1,4 @@
-CFLAGS += -Wall -Wstrict-prototypes -Wmissing-prototypes $(DEBIAN_CFLAGS)
+CFLAGS += -Wall -Wstrict-prototypes -Wmissing-prototypes -DGUNZIP='"@gunzip@"' $(DEBIAN_CFLAGS)
OBJECTS = man2html.o cgibase.o abbrev.o strdefs.o
bindir = $(DESTDIR)$(PREFIX)/usr/bin
mandir = $(DESTDIR)$(PREFIX)@mandir@
diff -ruN -x '*.rej' -x '*.orig' man-1.6e-old/man2html/man2html.c man-1.6e/man2html/man2html.c
--- man-1.6e-old/man2html/man2html.c 2007-04-16 23:16:15.000000000 +0200
+++ man-1.6e/man2html/man2html.c 2007-04-16 23:17:33.000000000 +0200
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
@@ -57,6 +58,108 @@
static char charb[3];
+#ifdef GUNZIP
+/* from src/utils.c */
+static int
+is_shell_safe(const char *ss, int quoted) {
+ char *bad = " ;'\\\"<>|";
+ char *p;
+
+ if (quoted)
+ bad++; /* allow a space inside quotes */
+ for (p = bad; *p; p++)
+ if (strchr(ss, *p))
+ return 0;
+ return 1;
+}
+#endif
+
+/* reads the entire manpage into buffer *buf and returns number of chars read */
+static int
+read_manpage_into_buffer(char *path, char **buf) {
+ int compressed = 0;
+ FILE * f = NULL;
+ char * ext;
+ int l = 0;
+ struct stat stbuf;
+
+ *buf = NULL;
+ if (!path)
+ return -1;
+
+ if (!strcmp(path, "-"))
+ f = stdin;
+ else /* strcmp(path, "-") */
+ {
+ char * tmp = NULL;
+ char * command = NULL;
+ char * openpath = path;
+#ifdef GUNZIP
+
+ if (is_shell_safe(openpath, 1)) {
+ ext = strrchr(openpath, '.');
+ compressed = (ext && !strcmp(ext, ".gz"));
+
+ if (!compressed && stat(openpath, &stbuf)) {
+ tmp = (char*) xmalloc(strlen(path) + 4);
+ sprintf(tmp, "%s.gz", path);
+ if ((compressed = !stat(tmp, &stbuf)))
+ openpath = tmp;
+ }
+ }
+
+ if (compressed) {
+ command = (char*) xmalloc(strlen(openpath) + sizeof(GUNZIP) + 4);
+ sprintf(command, GUNZIP " '%s'", openpath);
+ f = popen(command, "r");
+ } else
+#endif
+ f = fopen(openpath, "r");
+
+ if (tmp) free(tmp);
+ if (command) free(command);
+
+ if (!f)
+ return -1;
+
+ } /* strcmp(path, "-") */
+
+
+ /* Read entire file into buf[1..l] */
+#define XTRA 5
+ /* buf has 1 extra byte at the start, and XTRA extra bytes at the end */
+ if (compressed || f == stdin) {
+ int sz = 1024;
+ int ct = 1, tot = 0;
+ char *p = NULL;
+
+ clearerr(f);
+ while (ct > 0) {
+ tot += ct;
+ if (feof(f))
+ break;
+ sz = 2*sz+tot;
+ p = xrealloc(p, sz);
+ ct = fread(p+tot,1,sz-tot-XTRA,f);
+ }
+
+ *buf = p;
+ l = tot-1;
+ } else {
+ int ct;
+
+ l = 0;
+ if (fstat(fileno(f), &stbuf) != -1)
+ l = stbuf.st_size;
+ *buf = (char *) xmalloc((l+1+XTRA)*sizeof(char));
+ ct = fread(*buf+1,1,l,f);
+ if (ct < l)
+ l = ct;
+ }
+ fclose(f);
+ return l;
+}
+
static char *
expand_char(int nr)
{
@@ -1841,8 +1944,6 @@
break;
case V('s','o'):
{
- FILE *f;
- struct stat stbuf;
int l; char *buf;
char *name = NULL;
@@ -1850,21 +1951,21 @@
c += j; /* skip .so part and whitespace */
if (*c == '/') {
h = c;
- } else { /* .so man3/cpow.3 -> ../man3/cpow.3 */
- h = c-3;
- h[0] = '.';
- h[1] = '.';
- h[2] = '/';
- }
+ } else { /* .so man3/cpow.3 -> ../man3/cpow.3 */
+/* h = c-3;
+ h[0] = '.';
+ h[1] = '.';
+ h[2] = '/';
+*/
+ h = c;
+ }
+
while (*c != '\n') c++;
while (c[-1] == ' ') c--;
while (*c != '\n') *c++ = 0;
*c = 0;
scan_troff(h,1, &name);
if (name[3] == '/') h=name+3; else h=name;
- l = 0;
- if (stat(h, &stbuf)!=-1) l=stbuf.st_size;
- buf = (char*) xmalloc((l+4)*sizeof(char));
#if NOCGI
if (!out_length) {
char *t,*s;
@@ -1882,7 +1983,7 @@
#endif
{
/* this works alright, except for section 3 */
- if (!l || !(f = fopen(h,"r"))) {
+ if ((l = read_manpage_into_buffer(h, &buf)) < 0) {
fprintf(stderr,
"man2html: unable to open or read file %s\n", h);
out_html("<BLOCKQUOTE>"
@@ -1890,13 +1991,11 @@
out_html(h);
out_html("</BLOCKQUOTE>\n");
} else {
- i=fread(buf+1,1,l,f);
- fclose(f);
buf[0]=buf[l]='\n';
buf[l+1]=buf[l+2]=0;
scan_troff(buf+1,0,NULL);
+ if (buf) free(buf);
}
- if (buf) free(buf);
}
*c++='\n';
break;
@@ -3140,6 +3239,8 @@
}
}
+
+
/*
* Call: man2html [-l] [filename]
*
@@ -3150,8 +3251,6 @@
*/
int
main(int argc, char **argv) {
- FILE *f;
- struct stat stbuf;
int l, c;
char *buf, *filename, *fnam = NULL;
@@ -3213,50 +3312,17 @@
/* Open input file */
if (!fnam || !strcmp(fnam, "-")) {
- f = stdin;
+ fnam = "-";
fname = "(stdin)";
} else {
/* do a chdir() first, to get .so expansion right */
goto_dir(fnam, &directory, &fnam);
-
- f = fopen(fnam, "r");
- if (f == NULL)
- error_page(404, "File not found", "Could not open %s\n", filename);
fname = fnam;
}
- /* Read entire file into buf[1..l] */
-#define XTRA 5
- /* buf has 1 extra byte at the start, and XTRA extra bytes at the end */
- if (f == stdin) {
- int sz = 1024;
- int ct = 1, tot = 0;
- char *p = NULL;
-
- clearerr(stdin);
- while (ct > 0) {
- tot += ct;
- if (feof(stdin))
- break;
- sz = 2*sz+tot;
- p = xrealloc(p, sz);
- ct = fread(p+tot,1,sz-tot-XTRA,stdin);
- }
-
- buf = p;
- l = tot-1;
- } else {
- int ct;
-
- l = 0;
- if (fstat(fileno(f), &stbuf) != -1)
- l = stbuf.st_size;
- buf = (char *) xmalloc((l+1+XTRA)*sizeof(char));
- ct = fread(buf+1,1,l,f);
- if (ct < l)
- l = ct;
- fclose(f);
- }
+ l = read_manpage_into_buffer(fnam, &buf);
+ if (l < 0)
+ error_page(404, "File not found", "Could not open %s\n", fname);
buf[0] = '\n';
buf[l+1] = '\n';
|