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
|
/*
* 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 "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[MAX_TAG_SIZE];
char ch;
int i;
/*
* 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;
}
/*
* 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, 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.
*/
i = 0;
while (ch != EOF) {
tag[i] = ch;
i++;
if (ch == '>')
break;
if (i > (MAX_TAG_SIZE - 1)) {
fprintf (stderr, "Error: encountered a tag larger than %d.\n", MAX_TAG_SIZE);
fprintf (stderr, "Recompile with a larger MAX_TAG_SIZE value.\n");
return 1;
}
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;
tag[0] = '<';
ch = fgetc (inStream);
while (ch != EOF) {
tag[i] = ch;
i++;
if (i > MAX_TAG_SIZE) {
fprintf (stderr, "Error: encountered a tag larger than %d.\n", MAX_TAG_SIZE);
fprintf (stderr, "Recompile with a larger MAX_TAG_SIZE value.\n");
return 1;
}
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;
}
|