| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /* jsstrip: drop comments, indentation and blank lines from the browser's
- * JavaScript prelude before it is embedded (js_prelude.S), so the source
- * can stay commented without costing rootfs pack space.
- *
- * Usage: jsstrip <in.js> <out.js>
- *
- * Deliberately not a minifier: names stay and line breaks are kept (ASI and
- * error line numbers stay sane). Within a line a run of blanks (or a
- * removed comment) survives as one space only where dropping it could join
- * two tokens: between identifier characters, "+ +"/"- -", next to a '/',
- * and a digit before '.'. String and regex literals are copied verbatim;
- * a template literal is refused because `${}` nesting is not tracked (the
- * prelude has none). */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static const char *src;
- static long len, pos;
- static int line = 1;
- static int ident(char c)
- {
- return c == '_' || c == '$' || (unsigned char)c >= 0x80 ||
- (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
- (c >= 'A' && c <= 'Z');
- }
- static int need_space(char a, char b)
- {
- return (ident(a) && ident(b)) || ((a == '+' || a == '-') && a == b) ||
- a == '/' || b == '/' || (b == '.' && a >= '0' && a <= '9');
- }
- static void die(const char *msg)
- {
- fprintf(stderr, "jsstrip: line %d: %s\n", line, msg);
- exit(1);
- }
- /* A '/' starts a regex unless the previous significant token can end an
- * expression (identifier, number, ')' or ']'); "return /x/" etc. is
- * handled by the keyword check. */
- static int regex_allowed(const char *out, long n)
- {
- long i = n, j;
- while (i > 0 && (out[i - 1] == ' ' || out[i - 1] == '\n'))
- i--;
- if (i == 0)
- return 1;
- if (out[i - 1] == ')' || out[i - 1] == ']')
- return 0;
- j = i;
- while (j > 0 && (out[j - 1] == '_' || out[j - 1] == '$' ||
- (out[j - 1] >= '0' && out[j - 1] <= '9') ||
- (out[j - 1] >= 'a' && out[j - 1] <= 'z') ||
- (out[j - 1] >= 'A' && out[j - 1] <= 'Z')))
- j--;
- if (j == i)
- return 1; /* punctuation */
- return (i - j == 6 && !memcmp(out + j, "return", 6)) ||
- (i - j == 6 && !memcmp(out + j, "typeof", 6)) ||
- (i - j == 4 && !memcmp(out + j, "case", 4)) ||
- (i - j == 2 && !memcmp(out + j, "in", 2));
- }
- int main(int argc, char **argv)
- {
- FILE *f;
- char *out;
- long n = 0;
- int bol = 1; /* at the beginning of an output line */
- int gap = 0; /* blanks or a comment since the last output char */
- if (argc != 3) {
- fprintf(stderr, "usage: jsstrip in.js out.js\n");
- return 2;
- }
- f = fopen(argv[1], "rb");
- if (!f || fseek(f, 0, SEEK_END) || (len = ftell(f)) < 0)
- die("cannot read input");
- rewind(f);
- src = malloc(len + 1);
- out = malloc(len + 1);
- if (!src || !out || fread((char *)src, 1, len, f) != (size_t)len)
- die("cannot read input");
- fclose(f);
- while (pos < len) {
- char c = src[pos];
- if (c == '\n') {
- line++;
- pos++;
- /* drop empty lines */
- if (!bol)
- out[n++] = '\n';
- bol = 1;
- gap = 0;
- continue;
- }
- if (c == ' ' || c == '\t' || c == '\r') {
- pos++;
- gap = 1;
- continue;
- }
- if (c == '/' && pos + 1 < len && src[pos + 1] == '/') {
- while (pos < len && src[pos] != '\n')
- pos++;
- continue;
- }
- if (c == '/' && pos + 1 < len && src[pos + 1] == '*') {
- pos += 2;
- while (pos + 1 < len &&
- !(src[pos] == '*' && src[pos + 1] == '/')) {
- /* keep a newline a multi-line comment spanned, so
- * a comment cannot join two statements */
- if (src[pos] == '\n') {
- line++;
- if (!bol) {
- out[n++] = '\n';
- bol = 1;
- }
- }
- pos++;
- }
- if (pos + 1 >= len)
- die("unterminated comment");
- pos += 2;
- gap = 1;
- continue;
- }
- if (c == '`')
- die("template literals are not supported");
- if (gap && !bol && need_space(out[n - 1], c))
- out[n++] = ' ';
- gap = 0;
- bol = 0;
- if (c == '"' || c == '\'' ||
- (c == '/' && regex_allowed(out, n))) {
- char q = c, d;
- int cclass = 0; /* inside a regex [...] a '/' is literal */
- out[n++] = src[pos++];
- for (;;) {
- if (pos >= len || src[pos] == '\n')
- die("unterminated string or regex");
- d = src[pos];
- out[n++] = src[pos++];
- if (d == '\\') {
- if (pos >= len)
- die("unterminated literal");
- out[n++] = src[pos++];
- } else if (q == '/' && d == '[') {
- cclass = 1;
- } else if (q == '/' && d == ']') {
- cclass = 0;
- } else if (d == q && !cclass) {
- break;
- }
- }
- continue;
- }
- out[n++] = src[pos++];
- }
- while (n > 0 && out[n - 1] == '\n')
- n--;
- out[n++] = '\n';
- f = fopen(argv[2], "wb");
- if (!f || fwrite(out, 1, n, f) != (size_t)n || fclose(f))
- die("cannot write output");
- return 0;
- }
|