-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsmin.c
127 lines (120 loc) · 3.61 KB
/
jsmin.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
/**
* jsmin v2.1.5
*
* Copyright (C) 2023 Cromite.
* Released under the MIT license.
* see https://opensource.org/licenses/MIT
*
* Release-Date: 2023-1-6
* Bug reports to <admin@cromite.net>
*/
#include <stdio.h>
#include "lib/jsmin.h"
char* input = NULL;
char* output = NULL;
int mode = 0;
int overwrite = 0;
static int equal(char *string, char *sample) {
int i = 0;
while (string[i] != '\0' || sample[i] != '\0') {
if (string[i] != sample[i]) return 0;
i++;
}
return 1;
}
int main(int argc, char *argv[]) {
// options
if (argc > 0)
for (int i = 1; i < argc; i++) {
char *opt = *(argv + i);
if (opt[0] == '-') {
if (
equal(opt, "-v") ||
equal(opt, "--version")
) mode = 1;
else if (
equal(opt, "-h") ||
equal(opt, "--help")
) mode = 2;
else if (
equal(opt, "-o") ||
equal(opt, "--output")
) {
i++;
if (i < argc) {
if (*(argv + i)[0] == '-') {
printf("jsmin: argument to '%s' is missing\n", opt);
return 1;
} else output = *(argv + i);
}
else {
printf("jsmin: argument to '%s' is missing\n", opt);
return 1;
}
}
else if (
equal(opt, "-w") ||
equal(opt, "--overwrite")
) overwrite = 1;
else printf("jsmin: unknown option: '%s'\n", opt);
} else input = opt;
}
switch (mode) {
case 0:
if (input == NULL) {
printf("jsmin: no input file\n");
return 1;
}
if (output == NULL) {
if (overwrite) output = input;
else {
char raw[30];
int i = 0;
do raw[i] = input[i];
while (input[i++] != '\0' && !(
input[i] == '.' &&
input[i+1] == 'j' &&
input[i+2] == 's'
));
char min[] = ".min.js";
int m = 0;
do raw[i+m] = min[m];
while (min[m++] != '\0');
output = raw;
}
}
FILE *fp;
// input file
fp = fopen(input, "r");
if( fp == NULL ) {
printf("jsmin: no such file: '%s'\n", input);
return 1;
}
char code[1048576];
int i = 0;
while((code[i] = fgetc(fp)) != EOF) i++;
fclose(fp);
code[i] = '\0';
// output file
fp = fopen(output, "w");
fputs(jsmin(code), fp);
fclose(fp);
break;
case 1:
printf(
"jsmin 2.1.5\n"
"Copyright (C) 2023 Cromite.\n"
"Release-Date: 2023-1-6\n"
"Bug reports to <admin@cromite.net>\n");
break;
case 2:
printf(
"Usage: jsmin <file> [options...]\n\n"
"Options:\n"
" -o, --output <file> write to <file> instead of writing in '...min.js'\n"
" -w, --overwrite overwrite the file\n"
" -h, --help get help for commands\n"
" -v, --version show jsmin version\n\n");
}
return 0;
}