Mercurial > hg > toybox
annotate toys/other/dos2unix.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
rev | line source |
---|---|
644
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * dos2unix.c - convert newline format |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 * |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 * Copyright 2012 Rob Landley <rob@landley.net> |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 USE_DOS2UNIX(NEWTOY(dos2unix, NULL, TOYFLAG_BIN)) |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 USE_DOS2UNIX(OLDTOY(unix2dos, dos2unix, NULL, TOYFLAG_BIN)) |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 config DOS2UNIX |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 bool "dos2unix/unix2dos" |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 default y |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
13 help |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 usage: dos2unix/unix2dos [file...] |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 Convert newline format between dos (\r\n) and unix (just \n) |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 If no files listed copy from stdin, "-" is a synonym for stdin. |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 */ |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
19 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
20 #define FOR_dos2unix |
644
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
21 #include "toys.h" |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
23 GLOBALS( |
644
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 char *tempfile; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 ) |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 static void do_dos2unix(int fd, char *name) |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 { |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 char c = toys.which->name[0]; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 int outfd = 1, catch = 0; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 if (fd) outfd = copy_tempfile(fd, name, &TT.tempfile); |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 for (;;) { |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 int len, in, out; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 len = read(fd, toybuf+(sizeof(toybuf)/2), sizeof(toybuf)/2); |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
38 if (len<0) { |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
39 perror_msg("%s",name); |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
40 toys.exitval = 1; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
41 } |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
42 if (len<1) break; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
44 for (in = out = 0; in < len; in++) { |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
45 char x = toybuf[in+sizeof(toybuf)/2]; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
47 // Drop \r only if followed by \n in dos2unix mode |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 if (catch) { |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
49 if (c == 'u' || x != '\n') toybuf[out++] = '\r'; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 catch = 0; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 // Add \r only if \n not after \r in unix2dos mode |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 } else if (c == 'u' && x == '\n') toybuf[out++] = '\r'; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
54 if (x == '\r') catch++; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
55 else toybuf[out++] = x; |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
56 } |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
57 xwrite(outfd, toybuf, out); |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
58 } |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 if (catch) xwrite(outfd, "\r", 1); |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
61 if (fd) replace_tempfile(-1, outfd, &TT.tempfile); |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 } |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
64 void dos2unix_main(void) |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
65 { |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
66 loopfiles(toys.optargs, do_dos2unix); |
6a096902309d
Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
67 } |