-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabconv.l
More file actions
157 lines (139 loc) · 3.55 KB
/
Copy pathtabconv.l
File metadata and controls
157 lines (139 loc) · 3.55 KB
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
%{
#include <stdio.h>
int iLine = 1;
int iCol = 1;
int iTS = 8;
FILE *fOut = NULL;
#define YY_USER_ACTION { iCol += yyleng; }
#define wl( s, i ) (fwrite( s, 1, i, fOut ))
#define wyy (wl( yytext, yyleng ))
%}
%option nounput noyywrap
%x dq
%%
\t {
iCol--;
int x, tc=iTS-((iCol-1)%iTS);
if( tc == 0 )
tc = iTS;
for( x = 0; x < tc; x++ )
wl(" ", 1 );
iCol += tc;
}
\n { wyy; iCol = 1; iLine++; }
[^\t\n\"]* { wyy; }
\" { wyy; BEGIN(dq); }
<dq>\" { wyy; BEGIN(INITIAL); }
<dq>[^\"]* { wyy; }
%%
int main( int argc, char *argv[] )
{
char **p, *sIn=NULL, *sOut=NULL, *sTmp=NULL;
int bInPlace = 0;
int bKeepBackup = 0;
fOut = stdout;
yyin = stdin;
argc--, argv++;
for( p = argv; *p; p++ )
{
if( (*p)[0] == '-' )
{
switch( (*p)[1] )
{
case 't':
iTS = strtol( *(++p), NULL, 10 );
break;
case 'i':
bInPlace = -1;
break;
case 'k':
bKeepBackup = -1;
break;
default:
case 'h': // This isn't really necesarry, but I like it.
printf("tabconv [options] <input> <output>\n\n"
" -t <tabsize> Set the tabsize (default 8 "
"spaces).\n"
" -i Process in-place, only specify "
"the input file.\n"
" -k If -i is specified then the "
"backup file is not deleted.\n"
" -h This help.\n\n"
"If no input or output are provided then standard "
"in/out are used.\n\n"
);
return 0;
break;
}
}
else
{
if( !sIn )
{
sIn = *p;
}
else if( !sOut )
{
sOut = *p;
}
else
{
fprintf( stderr, "Confused by extra parameter: %s\n", *p );
return 1;
}
}
}
if( bInPlace )
{
if( sOut )
{
fprintf( stderr,
"You cannot specify -i (in-place) and an output file.\n"
);
return 1;
}
sTmp = malloc( strlen( sIn )+2 );
strcpy( sTmp, sIn );
strcat( sTmp, "~");
if( rename( sIn, sTmp ) )
{
fprintf( stderr,
"Cannot create temporary backup of input: %s\n",
strerror( errno )
);
return 1;
}
sOut = sIn;
sIn = sTmp;
}
if( sIn )
{
yyin = fopen( sIn, "r" );
if( yyin == NULL )
{
fprintf( stderr, "Cannot open input file '%s': %s\n",
sIn, strerror( errno ) );
return 1;
}
}
if( sOut )
{
fOut = fopen( sOut, "w" );
if( fOut == NULL )
{
fprintf( stderr, "Cannot open output file '%s': %s\n",
sOut, strerror( errno ) );
return 1;
}
}
while( yylex() ) { }
if( yyin != stdin )
fclose( yyin );
if( fOut != stdout )
fclose( fOut );
if( bInPlace && !bKeepBackup )
{
unlink( sIn );
}
return 0;
}