-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbecho.c
389 lines (364 loc) · 9.12 KB
/
cbecho.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
/*****************************************************************************
Copyright 2021, G. Adam Stanislav.
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************
Link with User32.lib (the pragma below should take care of it).
*****************************************************************************/
#pragma comment(lib,"user32")
#define ENVPREFS "CBECHO="
int version(int i) {
fprintf(i ? stderr : stdout, "CBECHO version 1.10\n"
"Copyright 2021 G. Adam Stanislav\nAll rights reserved\n");
return i;
}
int usage(int i, char *prefs) {
version(i);
fprintf(i ? stderr : stdout, "\nUsage: cbecho [options] [file]\n\n"
"\tcbecho -# (strip off # number characters)\n"
"\tcbecho -B (insert BE BOM)\n"
"\tcbecho -C (do not convert Unicode to UTF-8)*\n"
"\tcbecho -L (insert LE BOM)\n"
"\tcbecho -O file (append the output to the file)\n"
"\tcbecho -P (preserve BOM)*\n"
"\tcbecho -R (remove BOM)\n"
"\tcbecho -U (try Unicode first)*\n"
"\tcbecho -a [[-o] file] (ANSI/text output)\n"
"\tcbecho -b (bypass Unicode)\n"
"\tcbecho -c (convert Unicode to UTF-8)\n"
"\tcbecho -e (empty the clipboard after output)\n"
"\tcbecho -h (return this message)\n"
"\tcbecho -k (do not empty the clipboard)*\n"
"\tcbecho -n (do not swap Unicode bytes)*\n"
"\tcbecho -o file (send the output to the file)\n"
"\tcbecho -s (swap Unicode bytes)\n"
"\tcbecho -u [[-o] file] (Unicode/binary output if available)*\n"
"\tcbecho -v (print the version)\n"
"\tcbecho -w (wipe the clipboard)\n"
"\n\t* marks the default setting."
"\n\tIf the last argument is just -o or -O, the output is stdout.\n"
"\tBecause this is Windows, you can use / instead of -.\n");
if ((prefs) && (prefs[strlen(ENVPREFS)]))
fprintf(i ? stderr : stdout, "\n\tYour current system preferences are set to '%s'.\n", prefs);
return i;
}
void utf(wchar_t wc, int swap, FILE *output) {
if (swap) wc = ((wc << 8) | ((wc >> 8) & 0xFF)) & 0xFFFF;
if (wc < 0x0080)
fprintf(output, "%c", (char)wc);
else if (wc < 0x0800)
fprintf(
output, "%c%c",
(char)(0xC0 | ((wc >> 6) & 0x1F)),
(char)(0x80 | (wc & 0x3F))
);
else
fprintf(
output, "%c%c%c",
(char)(0xE0 | ((wc >> 12) & 0x0F)),
(char)(0x80 | ((wc >> 6) & 0x3F)),
(char)(0x80 | (wc & 0x3F))
);
}
int main(int argc, char *argv[], char **envp) {
FILE *output;
char *filename = NULL;
char *mode;
char *prefs = NULL;
int n = 0;
int bin = -1;
int app = 0;
int empty = 0;
int bypass = 0;
int retval = 1;
int swap = 0;
int bom = 0;
int rbom = 0; // remove BOM
int convert = 0; // to UTF-8
unsigned int i;
static char koniec = 0;
/* Parse the environment for user defaults */
for (;*envp;envp++) {
if (strnicmp(ENVPREFS, *envp, strlen(ENVPREFS)) == 0) {
char *str;
prefs = *envp;
for (str = &(prefs[strlen(ENVPREFS)]); *str; str++) switch (*str) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
n = atoi(str);
while (isdigit(*str)) str++;
break;
case 'B':
bom = 1;
rbom = 1;
break;
case 'C':
convert = 0;
break;
case 'L':
bom = -1;
rbom = 1;
break;
case 'O':
app = 1;
break;
case 'P':
bom = 0;
rbom = 0;
break;
case 'R':
bom = 0;
rbom = 1;
break;
case 'U':
bypass = 0;
break;
case 'a':
bin = 0;
break;
case 'b':
bypass = 1;
break;
case 'c':
convert = 1;
break;
case 'e':
empty = 1;
break;
case 'k':
empty = 0;
break;
case 'n':
swap = 0;
break;
case 'o':
app = 0;
break;
case 's':
swap = 1;
case 'u':
bin = 1;
break;
case 'w':
empty = -1;
break;
}
break; /* from the envp for loop */
}
}
/* Parse the command line */
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--help")) return usage(0, prefs);
else if (!strcmp(argv[i], "--version")) return version(0);
else if ((argv[i][0] == '/') || (argv[i][0] == '-')) {
char *str = &(argv[i][1]);
if (*str == '\0') return usage(-2, prefs);
else for (; *str; str++) switch (*str) {
default:
return usage(-2, prefs);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
n = atoi(str);
while (isdigit(*str)) str++;
break;
case 'B':
bom = 1;
rbom = 1;
break;
case 'C':
convert = 0;
break;
case 'L':
bom = -1;
rbom = 1;
break;
case 'O':
app = 1;
if (str[1]) {
filename = &str[1];
str = &koniec;
}
else if (i < argc-1) filename = argv[++i];
else filename = NULL;
break;
case 'P':
bom = 0;
rbom = 0;
break;
case 'R':
bom = 0;
rbom = 1;
case 'U':
bypass = 0;
break;
case 'a':
bin = 0;
if (str[1]) {
filename = &(str[1]);
str = &koniec;
}
break;
case 'b':
bypass = 1;
break;
case 'c':
convert = 1;
break;
case 'e':
empty = 1;
break;
case 'h':
case '?':
return usage(0, prefs);
break;
case 'k':
empty = 0;
break;
case 'n':
swap = 0;
break;
case 'o':
app = 0;
if (str[1]) {
filename = &(str[1]);
str = &koniec;
}
else if (i < argc-1) filename = argv[++i];
else filename = NULL;
break;
case 's':
swap = 1;
break;
case 'u':
bin = 1;
if (str[1]) {
filename = &(str[1]);
str = &koniec;
}
break;
case 'v':
return version(0);
break;
case 'w':
empty = -1;
break;
}
}
else { /* No '-' or '/' switch, assume filename */
filename = argv[i];
}
}
if (empty < 0) filename = NULL;
if (filename == NULL) {
if (bin > 0) _setmode(_fileno(stdout), _O_BINARY);
output = stdout;
}
else {
if (app) mode = (bin) ? "ab" : "at";
else mode = (bin) ? "wb" : "wt";
if ((output = fopen(filename, mode)) == NULL) {
fprintf(stderr, "cbecho: Could not create file '%s'.\n", filename);
return -3;
}
}
if (OpenClipboard(NULL)) {
if (empty >= 0) {
HANDLE hClipboardData;
int temp;
if ((bypass == 0) && (IsClipboardFormatAvailable(CF_UNICODETEXT))) {
wchar_t *ws;
hClipboardData = GetClipboardData(CF_UNICODETEXT);
ws = (wchar_t*)GlobalLock(hClipboardData);
if (((rbom)||(convert))&&((*ws == 0xFFFE) || (*ws == 0xFEFF))) ws++;
if ((temp = wcslen(ws)) > n) {
temp -= n;
/* NOTE: fputwchar does not seem to do it, so we
use fwprintf even for a single wchar_t */
if (convert) {
if (bom)
utf((wchar_t)0xFEFF, 0, output);
for (i = 0; i < temp; i++, ws++) {
utf(*ws, swap, output);
}
}
else {
if ((filename == NULL) && (bin <= 0))
_setmode(_fileno(stdout), _O_U16TEXT);
if (bom)
// Windows is low-endian, so when we pass FEFF,
// it will output FF followed by FE, and vice versa.
fwprintf(output, L"%c",
bom < 0 ? (wchar_t)0xfeff : (wchar_t)0xfffe);
if (swap == 0) {
fwprintf(output, L"%.*s", temp, ws);
}
else {
wchar_t wc;
unsigned char *str;
for (i = 0; i < temp; i++, ws++) {
str = (char *)ws;
wc = (wchar_t)((str[0]<<8)|str[1]);
fwprintf(output, L"%c", wc);
}
}
}
}
GlobalUnlock(hClipboardData);
retval = 0;
}
else if (IsClipboardFormatAvailable(CF_TEXT) ||
IsClipboardFormatAvailable(CF_OEMTEXT)) {
char *str;
hClipboardData = GetClipboardData(CF_TEXT);
str = (char*)GlobalLock(hClipboardData);
if ((temp = strlen(str)) > n)
fprintf(output, "%.*s", temp-n, str);
GlobalUnlock(hClipboardData);
retval = 0;
}
if (filename != NULL) fclose(output);
}
if ((empty != 0) && (EmptyClipboard() == 0))
retval = -4;
CloseClipboard();
}
return retval;
}