jsstrip.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* jsstrip: drop comments, indentation and blank lines from the browser's
  2. * JavaScript prelude before it is embedded (js_prelude.S), so the source
  3. * can stay commented without costing rootfs pack space.
  4. *
  5. * Usage: jsstrip <in.js> <out.js>
  6. *
  7. * Deliberately not a minifier: names stay and line breaks are kept (ASI and
  8. * error line numbers stay sane). Within a line a run of blanks (or a
  9. * removed comment) survives as one space only where dropping it could join
  10. * two tokens: between identifier characters, "+ +"/"- -", next to a '/',
  11. * and a digit before '.'. String and regex literals are copied verbatim;
  12. * a template literal is refused because `${}` nesting is not tracked (the
  13. * prelude has none). */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. static const char *src;
  18. static long len, pos;
  19. static int line = 1;
  20. static int ident(char c)
  21. {
  22. return c == '_' || c == '$' || (unsigned char)c >= 0x80 ||
  23. (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
  24. (c >= 'A' && c <= 'Z');
  25. }
  26. static int need_space(char a, char b)
  27. {
  28. return (ident(a) && ident(b)) || ((a == '+' || a == '-') && a == b) ||
  29. a == '/' || b == '/' || (b == '.' && a >= '0' && a <= '9');
  30. }
  31. static void die(const char *msg)
  32. {
  33. fprintf(stderr, "jsstrip: line %d: %s\n", line, msg);
  34. exit(1);
  35. }
  36. /* A '/' starts a regex unless the previous significant token can end an
  37. * expression (identifier, number, ')' or ']'); "return /x/" etc. is
  38. * handled by the keyword check. */
  39. static int regex_allowed(const char *out, long n)
  40. {
  41. long i = n, j;
  42. while (i > 0 && (out[i - 1] == ' ' || out[i - 1] == '\n'))
  43. i--;
  44. if (i == 0)
  45. return 1;
  46. if (out[i - 1] == ')' || out[i - 1] == ']')
  47. return 0;
  48. j = i;
  49. while (j > 0 && (out[j - 1] == '_' || out[j - 1] == '$' ||
  50. (out[j - 1] >= '0' && out[j - 1] <= '9') ||
  51. (out[j - 1] >= 'a' && out[j - 1] <= 'z') ||
  52. (out[j - 1] >= 'A' && out[j - 1] <= 'Z')))
  53. j--;
  54. if (j == i)
  55. return 1; /* punctuation */
  56. return (i - j == 6 && !memcmp(out + j, "return", 6)) ||
  57. (i - j == 6 && !memcmp(out + j, "typeof", 6)) ||
  58. (i - j == 4 && !memcmp(out + j, "case", 4)) ||
  59. (i - j == 2 && !memcmp(out + j, "in", 2));
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. FILE *f;
  64. char *out;
  65. long n = 0;
  66. int bol = 1; /* at the beginning of an output line */
  67. int gap = 0; /* blanks or a comment since the last output char */
  68. if (argc != 3) {
  69. fprintf(stderr, "usage: jsstrip in.js out.js\n");
  70. return 2;
  71. }
  72. f = fopen(argv[1], "rb");
  73. if (!f || fseek(f, 0, SEEK_END) || (len = ftell(f)) < 0)
  74. die("cannot read input");
  75. rewind(f);
  76. src = malloc(len + 1);
  77. out = malloc(len + 1);
  78. if (!src || !out || fread((char *)src, 1, len, f) != (size_t)len)
  79. die("cannot read input");
  80. fclose(f);
  81. while (pos < len) {
  82. char c = src[pos];
  83. if (c == '\n') {
  84. line++;
  85. pos++;
  86. /* drop empty lines */
  87. if (!bol)
  88. out[n++] = '\n';
  89. bol = 1;
  90. gap = 0;
  91. continue;
  92. }
  93. if (c == ' ' || c == '\t' || c == '\r') {
  94. pos++;
  95. gap = 1;
  96. continue;
  97. }
  98. if (c == '/' && pos + 1 < len && src[pos + 1] == '/') {
  99. while (pos < len && src[pos] != '\n')
  100. pos++;
  101. continue;
  102. }
  103. if (c == '/' && pos + 1 < len && src[pos + 1] == '*') {
  104. pos += 2;
  105. while (pos + 1 < len &&
  106. !(src[pos] == '*' && src[pos + 1] == '/')) {
  107. /* keep a newline a multi-line comment spanned, so
  108. * a comment cannot join two statements */
  109. if (src[pos] == '\n') {
  110. line++;
  111. if (!bol) {
  112. out[n++] = '\n';
  113. bol = 1;
  114. }
  115. }
  116. pos++;
  117. }
  118. if (pos + 1 >= len)
  119. die("unterminated comment");
  120. pos += 2;
  121. gap = 1;
  122. continue;
  123. }
  124. if (c == '`')
  125. die("template literals are not supported");
  126. if (gap && !bol && need_space(out[n - 1], c))
  127. out[n++] = ' ';
  128. gap = 0;
  129. bol = 0;
  130. if (c == '"' || c == '\'' ||
  131. (c == '/' && regex_allowed(out, n))) {
  132. char q = c, d;
  133. int cclass = 0; /* inside a regex [...] a '/' is literal */
  134. out[n++] = src[pos++];
  135. for (;;) {
  136. if (pos >= len || src[pos] == '\n')
  137. die("unterminated string or regex");
  138. d = src[pos];
  139. out[n++] = src[pos++];
  140. if (d == '\\') {
  141. if (pos >= len)
  142. die("unterminated literal");
  143. out[n++] = src[pos++];
  144. } else if (q == '/' && d == '[') {
  145. cclass = 1;
  146. } else if (q == '/' && d == ']') {
  147. cclass = 0;
  148. } else if (d == q && !cclass) {
  149. break;
  150. }
  151. }
  152. continue;
  153. }
  154. out[n++] = src[pos++];
  155. }
  156. while (n > 0 && out[n - 1] == '\n')
  157. n--;
  158. out[n++] = '\n';
  159. f = fopen(argv[2], "wb");
  160. if (!f || fwrite(out, 1, n, f) != (size_t)n || fclose(f))
  161. die("cannot write output");
  162. return 0;
  163. }