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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
* File: unhtml.c
* Program: unhtml
* Written by: Kevin Swan, 013639s@dragon.acadiau.ca
* Completed: February 3, 1998
* Version: 2.3
*
* Usage:
* unhtml -version | [ filename ]
*
* Specification:
* unhtml is a program which removes HTML formatting from a stream, writing
* to output to a stream. If it is invoked with a file name, it attempts
* to read from the named file. If it is invoked without a filename, it
* reads from stdin. It writes all output to stdout.
*/
#include <ctype.h>
#include <malloc.h>
#include <string.h>
#include "ops.h"
#include "esc.h"
char *VERSION = "unhtml Version 2.3 Copyright (C) 1998 by Kevin Swan";
char *USAGE = "unhtml -version | [ filename ]";
int main(int argc, char *argv[]) {
/*
* Variables local to the program.
*/
FILE *inStream;
char *tag;
char *tmp;
int tag_size;
int ch;
int i, j;
/*
* Do argument checking. If more than one command line argument was
* given, print a usage error.
*/
if (argc > 2)
fprintf (stderr, "Usage: %s\n", USAGE);
/*
* If the user simply requested the version of the program, print that
* information and terminate.
*/
if (argc == 2)
if (strcmp (argv[1], "-version") == 0) {
printf ("%s\n", VERSION);
return 0;
}
/*
* Allocate tag space, now that we know we need to do some actual work.
*/
tag_size = MAX_TAG_SIZE;
tag = (char *)malloc(tag_size);
if (!tag) {
fprintf (stderr, "Cannot malloc tag space (%d bytes).\n", tag_size);
return 1;
}
/*
* If an input file was specified, try to open a read stream on it.
*/
if (argc == 2) {
if ((inStream = fopen (argv[1], "r")) == NULL) {
fprintf (stderr, "Error opening file [%s] for reading.\n", argv[1]);
fprintf (stderr, "%s", USAGE);
return 1;
}
} else
/*
* Otherwise, just use the standard input stream.
*/
inStream = stdin;
/*
* Read tokens from the stream until we hit an opener for an HTML tag.
*/
while (1) {
ch = fgetc (inStream);
/*
* If we hit the end of the file, we're done.
*/
if (ch == EOF)
break;
/*
* If the character is not a tag opener, just print it.
*/
if (ch != '<') {
m_putchar (ch);
continue;
}
/*
* If we get this far, we've hit an HTML tag. Read it into the
* variable tag.
*/
memset(tag, 0, tag_size);
i = 0;
while (ch != EOF) {
tag[i] = ch;
if (i == 1 && ch != '/' && !isalpha(ch)) {
m_putchar(tag[0]);
m_putchar(ch);
break;
}
i++;
if (ch == '>') {
/*
* If it's really an html tag, then toss it. Otherwise, it could
* have been just a '<' sign in the text.
*/
if (!isRealHtmlTag(tag)) {
fprintf(stderr, "not: %s\n",tag);
for (j = 0; j < i; j++)
m_putchar(tag[j]);
}
break;
}
if (i >= tag_size-1) {
tag_size <<= 1;
tmp = realloc(tag, tag_size);
if (!tmp) {
fprintf (stderr, "Cannot malloc tag space (%d bytes).\n", tag_size);
return 1;
}
tag = tmp;
}
ch = fgetc (inStream);
}
tag[i] = '\0';
#ifdef DEBUG
fprintf (stderr, "Read in the tag \"%s\"\n", tag);
#endif
/*
* If it was a script opener, it is a special case. We may find
* '>' characters inside the <SCRIPT></SCRIPT> pair that are not
* associated with a tag. In addition, comment delimiters are
* found inside the script tag pairs. So, if we get a script
* tag, skip ahead to the closing script tag.
*/
if (isScriptOpeningTag (tag)) {
#ifdef DEBUG
fprintf (stderr, "\"%s\" is a script opener.\n", tag);
#endif
/*
* This loop is necessary to ensure that we don't swallow up the
* closing </SCRIPT> tag while filling the buffer if we happen to
* hit a '<' character in some comparison in the scripting language.
*/
while (1) {
#ifdef DEBUG
fprintf (stderr, "1. Read till we hit a '<'.\n");
#endif
/*
* Read until we hit a '<'.
*/
ch = fgetc (inStream);
while (ch != EOF)
if (ch == '<')
break;
else
ch = fgetc (inStream);
if (ch == EOF) {
ungetc (ch, inStream);
break;
}
#ifdef DEBUG
fprintf (stderr, "2. Read till we hit a '>' or a '<', filling the buffer.\n");
#endif
/*
* Hit a '<'. Read till we hit a '>' or a '<', filling the buffer.
*/
i = 1;
memset(tag, 0, tag_size);
tag[0] = '<';
ch = fgetc (inStream);
while (ch != EOF) {
tag[i] = ch;
i++;
if (i >= tag_size-1) {
tag_size <<= 1;
tmp=realloc(tag, tag_size);
if (!tmp) {
fprintf (stderr, "Cannot malloc tag space (%d bytes).\n", tag_size);
return 1;
}
tag = tmp;
}
if ((ch == '>') || (ch == '<'))
break;
ch = fgetc (inStream);
} /* while */
if (ch == EOF) {
ungetc (ch, inStream);
break;
}
tag[i] = '\0';
#ifdef DEBUG
fprintf (stderr, "Read tag: \"%s\"\n", tag);
#endif
if (ch == '<') {
ungetc (ch, inStream);
continue;
} else
if (isScriptClosingTag(tag))
break;
}
#ifdef DEBUG
fprintf(stderr, "Got to the end of the script, found \"%s\"\n", tag);
#endif
/*
* At this point, we should be ready to read the first character
* after the closing '>' of the </SCRIPT> tag.
*/
continue;
}
/*
* If it was a comment opener, skip to the comment closer.
*/
/*
ch = fgetc (inStream);
if (ch == EOF)
break;
for (i = 0 ; i < 10 ; i++) {
if ((ch = fgetc (inStream)) == EOF)
break;
if (ch == '>')
break;
tag[i] = ch;
}
if (ch == EOF)
break;
*/
}
m_putchar(EOF); /* for the rare case in which chars remain in bff */
/*
* Try to peacefully close the stream, if it is not stdin.
*/
if (argc == 2)
if (fclose(inStream))
fprintf (stderr, "Error %d closing file.\n", errno);
return 0;
}
|