-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
161 lines (160 loc) · 4.24 KB
/
main.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
/** =========
qo |) | workplace
_x/ , | utility
| ng | conversion
_opRWTng-rwt=
Universal line ends converter to "\n", "\r\n" or "\r"
- - - - - - - - - - - - - - - - - - - -
Automatycznie przełącza się do innego typu zakończenia wiersza, ale tylko wtedy gdy kolejna linia nie jest pusta.
---------------------------------------
==================================== */
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "cmd_arg.h"
//=====================================
static
char *const def_output_lineend = "\n";
//=====================================
static
char *output_lineend = NULL;
//=====================================
static
enum Z_arg_proc_ret
for_sys_arg_proc(
int switch_,
int param_no,
char *null[]
)
{ if( output_lineend )
return Z_arg_proc_ret_Z_ignore;
switch( param_no )
{ case 1:
case 3:
case 4:
case 5:
case 6:
output_lineend = "\n";
break;
case 0:
case 2:
case 8:
case 9:
output_lineend = "\r\n";
break;
case 7:
output_lineend = "\r";
break;
}
return Z_arg_proc_ret_Z_accept;
}
static
enum Z_arg_proc_ret
to_lineend_arg_proc(
int switch_,
int param_no,
char *null[]
)
{ if( output_lineend )
return Z_arg_proc_ret_Z_ignore;
switch( param_no )
{ case 0:
output_lineend = "\n";
break;
case 1:
output_lineend = "\r\n";
break;
case 2:
output_lineend = "\r";
break;
}
return Z_arg_proc_ret_Z_accept;
}
static
enum Z_arg_proc_ret
def_proc(
int argc,
char *argv[]
)
{ return Z_arg_proc_ret_Z_reject;
}
int
main(
int argc,
char *argv[]
)
{ if( isatty(0) )
fprintf( stderr, "\"universal line end converter\" copyright (c) 2010,2021 overcq\n(DOS-type fancy program header:->)\n" );
if( argc )
{ argc--;
argv++;
}
struct Z_avail_arg avail_args[] =
{ "for-sys", "s", false, false, false, 10, ( const char *const [] ){ "network", "unix", "Windows", "Macintosh", "Linux", "BSD", "Mac OS X", "Mac OS 9", "DOS", "DEC" }, for_sys_arg_proc, "for operating system",
"to-lineend", "e", false, false, false, 3, ( const char *const [] ){ "lf", "crlf", "cr" }, to_lineend_arg_proc, "to line end character(s)",
};
parse_cmd_args( argc, argv, def_proc, sizeof( avail_args ) / sizeof( struct Z_avail_arg ), avail_args );
if( !output_lineend )
output_lineend = def_output_lineend;
int c = getchar();
do
{ while( c != '\n'
&& c != '\r'
)
{ if( c == EOF )
return 0;
putchar(c);
c = getchar();
}
int input_lineend_1, input_lineend_2;
if( c == '\r' )
{ c = getchar();
if( c == '\n' )
{ input_lineend_1 = '\r';
input_lineend_2 = '\n';
fputs( output_lineend, stdout );
}else
{ input_lineend_1 = '\r';
input_lineend_2 = 0;
fputs( output_lineend, stdout );
if( c != EOF )
putchar(c);
}
}else
{ input_lineend_1 = '\n';
input_lineend_2 = 0;
fputs( output_lineend, stdout );
}
if( input_lineend_2 )
while( c = getchar(),
c == '\r'
|| c == '\n'
)
{ int c_2 = getchar();
if( c == input_lineend_1
&& c_2 == input_lineend_2
)
fputs( output_lineend, stdout );
else
{ if( c != EOF )
{ putchar(c);
c = c_2;
}
break;
}
}
else
while( c = getchar(),
c == '\r'
|| c == '\n'
)
if( c == input_lineend_1 )
fputs( output_lineend, stdout );
else
if( c != EOF )
putchar(c);
}while(true);
return 0;
}
//=====================================