-
Notifications
You must be signed in to change notification settings - Fork 21
/
html.c
242 lines (218 loc) · 6.17 KB
/
html.c
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
/* $Copyright: $
* Copyright (c) 1996 - 2022 by Steve Baker ([email protected])
* All Rights reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tree.h"
extern char *version, *hversion;
extern bool dflag, lflag, pflag, sflag, Fflag, aflag, fflag, uflag, gflag;
extern bool Dflag, inodeflag, devflag, Rflag, duflag, hflag, siflag;
extern bool noindent, force_color, xdev, nolinks, flimit, metafirst, noreport;
extern char *host, *sp, *title;
extern const char *charset;
extern FILE *outfile;
extern int Level, *dirs, maxdirs;
extern bool colorize, linktargetcolor;
extern char *endcode;
extern const struct linedraw *linedraw;
int htmldirlen = 0;
char *class(struct _info *info)
{
return
info->isdir ? "DIR" :
info->isexe ? "EXEC" :
info->isfifo ? "FIFO" :
info->issok ? "SOCK" : "NORM";
}
void html_encode(FILE *fd, char *s)
{
for(;*s;s ) {
switch(*s) {
case '<':
fputs("<",fd);
break;
case '>':
fputs(">",fd);
break;
case '&':
fputs("&",fd);
break;
case '"':
fputs(""",fd);
break;
default:
fputc(*s,fd);
// fputc(isprint(*s)?*s:'?',fd);
break;
}
}
}
void url_encode(FILE *fd, char *s)
{
for(;*s;s ) {
switch(*s) {
case ' ':
case '"':
case '#':
case '%':
case '<':
case '>':
case '[':
case ']':
case '^':
case '\\':
case '?':
case ' ':
fprintf(fd,"%%X",*s);
break;
case '&':
fprintf(fd,"&");
break;
default:
fprintf(fd,isprint((u_int)*s)?"%c":"%%X",(u_char)*s);
break;
}
}
}
void html_intro(void)
{
fprintf(outfile,
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n"
" <meta name=\"Author\" content=\"Made by 'tree'\">\n"
" <meta name=\"GENERATOR\" content=\"%s\">\n"
" <title>%s</title>\n"
" <style type=\"text/css\">\n"
" BODY { font-family : monospace, sans-serif; color: black;}\n"
" P { font-family : monospace, sans-serif; color: black; margin:0px; padding: 0px;}\n"
" A:visited { text-decoration : none; margin : 0px; padding : 0px;}\n"
" A:link { text-decoration : none; margin : 0px; padding : 0px;}\n"
" A:hover { text-decoration: underline; background-color : yellow; margin : 0px; padding : 0px;}\n"
" A:active { margin : 0px; padding : 0px;}\n"
" .VERSION { font-size: small; font-family : arial, sans-serif; }\n"
" .NORM { color: black; }\n"
" .FIFO { color: purple; }\n"
" .CHAR { color: yellow; }\n"
" .DIR { color: blue; }\n"
" .BLOCK { color: yellow; }\n"
" .LINK { color: aqua; }\n"
" .SOCK { color: fuchsia;}\n"
" .EXEC { color: green; }\n"
" </style>\n"
"</head>\n"
"<body>\n"
"\t<h1>%s</h1><p>\n",charset ? charset : "iso-8859-1", version, title, title);
}
void html_outtro(void)
{
fprintf(outfile,"\t<hr>\n");
fprintf(outfile,"\t<p class=\"VERSION\">\n");
fprintf(outfile,hversion,linedraw->copy, linedraw->copy, linedraw->copy, linedraw->copy);
fprintf(outfile,"\t</p>\n");
fprintf(outfile,"</body>\n");
fprintf(outfile,"</html>\n");
}
void html_print(char *s)
{
for(int i=0; s[i]; i ) {
if (s[i] == ' ') fprintf(outfile,"%s",sp);
else fprintf(outfile,"%c", s[i]);
}
fprintf(outfile,"%s%s", sp, sp);
}
int html_printinfo(char *dirname, struct _info *file, int level)
{
char info[512];
fillinfo(info,file);
if (metafirst) {
if (info[0] == '[') {
html_print(info);
fprintf(outfile,"%s%s", sp, sp);
}
if (!noindent) indent(level);
} else {
if (!noindent) indent(level);
if (info[0] == '[') {
html_print(info);
fprintf(outfile,"%s%s", sp, sp);
}
}
return 0;
}
// descend == add 00Tree.html to the link
int html_printfile(char *dirname, char *filename, struct _info *file, int descend)
{
// Switch to using 'a' elements only. Omit href attribute if not a link
fprintf(outfile,"<a");
if (file) {
if (force_color) fprintf(outfile," class=\"%s\"", class(file));
if (file->comment) {
fprintf(outfile," title=\"");
for(int i=0; file->comment[i]; i ) {
html_encode(outfile, file->comment[i]);
if (file->comment[i 1]) fprintf(outfile, "\n");
}
fprintf(outfile, "\"");
}
if (!nolinks) {
fprintf(outfile," href=\"%s",host);
if (dirname != NULL) {
int len = strlen(dirname);
int off = (len >= htmldirlen? htmldirlen : 0);
url_encode(outfile, dirname off);
putc('/',outfile);
url_encode(outfile, filename);
fprintf(outfile,"%s%s\"",(descend > 1? "/00Tree.html" : ""), (file->isdir?"/":""));
} else {
fprintf(outfile,"%s\"",(descend > 1? "/00Tree.html" : ""));
}
}
}
fprintf(outfile, ">");
if (dirname) html_encode(outfile,filename);
else html_encode(outfile, host);
fprintf(outfile,"</a>");
return 0;
}
int html_error(char *error)
{
fprintf(outfile, " [%s]", error);
return 0;
}
void html_newline(struct _info *file, int level, int postdir, int needcomma)
{
fprintf(outfile, "<br>\n");
}
void html_close(struct _info *file, int level, int needcomma)
{
fprintf(outfile, "</%s><br>\n", file->tag);
}
void html_report(struct totals tot)
{
char buf[256];
fprintf(outfile,"<br><br><p>\n\n");
if (duflag) {
psize(buf, tot.size);
fprintf(outfile,"%s%s used in ", buf, hflag || siflag? "" : " bytes");
}
if (dflag)
fprintf(outfile,"%ld director%s\n",tot.dirs,(tot.dirs==1? "y":"ies"));
else
fprintf(outfile,"%ld director%s, %ld file%s\n",tot.dirs,(tot.dirs==1? "y":"ies"),tot.files,(tot.files==1? "":"s"));
fprintf(outfile, "\n</p>\n");
}