-
Notifications
You must be signed in to change notification settings - Fork 3
/
short.c
177 lines (159 loc) · 4.29 KB
/
short.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
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#pragma GCC diagnostic ignored "-Wmissing-braces"
/* curly-braces, parenthesis, preprocessor */
int curly, parens, pre;
/* shortened codes */
struct { int c; char *s; } mapping[] = {
'A',"int main(int argc, char **argv){",
'B',"int main(",
'C'," char ",
'D',"#define ",
'E'," else ",
'F'," if(",
'G'," getchar()",
'H'," switch(",
'I'," int ",
'\\'," int (*f)() = \"",
'J'," puts(",
'K'," scanf(",
'L'," long ",
'M'," strcmp(",
'N'," stdin ",
'O'," for(",
'P'," putchar(",
'Q'," gets(",
'R'," printf(",
'S'," strlen(",
'T'," return ",
'U'," usleep(",
'V'," goto*&",
'W'," while(",
'X'," while(1){",
'Y'," fgets(",
'Z'," srand(time(0));",
'$'," system(",
'@'," argv ",
'`'," rand()",
'¡'," != ",
'«'," << ",
'»'," >> ",
'À'," char *a;",
'Á'," int a;",
'×',"*=",
'÷',"/=",
'Ç'," char*",
'ï',"++",
'',"--",
'¿',"==",
'ÿ'," 42 ",
'µ'," int f(int n){",
'¸',"-=",
'±'," sign(",
'ì'," input()",
'¯'," prime(",
'í'," printf(\"%d\",",
'Ì',"-1 ",
'Í',"+1 ",
'æ',"\"abcdefghijklmnopqrstuvwxyz\"",
'Æ',"\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"",
'ð'," int i; for (i = 0; i <",
'Ã'," char *s=malloc(1024);memset(s,0,1024);",
'ß',"=strdup(\"",
'©',"\");",
'Þ'," int c;while((c = getchar())!=EOF){",
'Ø'," strcat(",
'¶'," fgets(s,1000,stdin);"
};
/* shortify */
int translate(char c)
{
int i, match = 0;
for (i = 0; mapping[i].s; i++) {
if (mapping[i].c == c) {
printf("%s",mapping[i].s);
if (strchr(mapping[i].s,'{'))
curly++;
if (strchr(mapping[i].s,'}'))
curly--;
if (strchr(mapping[i].s,'('))
parens++;
if (strchr(mapping[i].s,')'))
parens--;
if (strchr(mapping[i].s,'#'))
pre = 1;
match++;
}
}
return match;
}
/* auto-insert parenthesis and semicolons */
void autoclose(void)
{
if (parens > 0)
while (parens--)
putchar(')');
if (!--curly)
putchar(';');
}
int main(int argc, char **argv)
{
if (argv[1] && strchr(argv[1],'h')) {
printf("Usage: shortc -hn\n" \
"Flags: h | Display help and exit\n" \
" n | No default functions\n" \
"short.c, copyright (c) 2017 MD XF Technologies\n");
return 0;
}
if (!argv[1] || !strchr(argv[1],'n')) {
puts("#include <math.h>\n#include <stdio.h>");
puts("int sign(int x){return x < 0 ? -1 : x == 0 ? 0 : 1;}");
puts("int prime(int x){if (x < 2) return 0; int ii; for (ii = sqrt(x); ii > 1; ii--) if (!(x % ii)) return 0; return 1;}");
puts("int input(void){int x; scanf(\"%d\",&x); return x;}");
}
int c, prev, chr, str;
c = prev = chr = str = curly = parens = pre = 0;
while ((c = getchar()) != EOF)
{
/* if uppercase and not in a string/char, shortify */
if (!str && !chr)
if (translate(c))
continue;
/* swap char/string flags */
if (c == '"' && prev != '\\' && !chr)
str = !str;
else if (c == '\'' && prev != '\\' && !str)
chr = !chr;
else if (c == '(' && !chr && !str)
parens++;
else if (c == ')' && !chr && !str)
parens--;
else if (c == '{' && !chr && !str)
curly++;
else if (c == '}' && !chr && !str)
autoclose();
/* print newline and swap preprocessor flag if preprocessor flag is 1 */
if (c == '\n' && prev != '\\')
pre = 0;
/* print closing ' if not present */
if (chr && prev != '\'' && c != '\'') {
chr = 0;
putchar('\'');
}
putchar(c);
prev = c;
}
/* close quotes */
if (chr)
putchar('\'');
else if (str)
putchar('\"');
/* close braces and auto-insert remaining characters */
if (curly > 0)
while (curly) /* autoclose decrements curly */
autoclose(), putchar('}');
/* prettiness */
putchar('\n');
return 0;
}