-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathncpathcvt.c
306 lines (274 loc) · 6.6 KB
/
ncpathcvt.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
/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#if defined(_WIN32) && !defined(__MINGW32__)
#include "XGetopt.h"
#else
#include <getopt.h>
#endif
#include "netcdf.h"
#include "netcdf_filter.h"
#include "netcdf_aux.h"
#include "ncpathmgr.h"
#include "ncbytes.h"
static const char* USAGE =
"ncpathcvt [-c|-m|-u|-w] [-e] [-h] [-k] [-p] [-x] [-F] [-D <driveletter>] [-B<char>] [-S<char>] PATH\n"
"Options\n"
" -h help"
" -e add backslash escapes to '\' and ' '\n"
" -B <char> convert occurrences of <char> to blank\n"
" -D <driveletter> use driveletter when needed; defaults to 'c'\n"
" -F convert occurrences of '\\' to '/'"
" -S <char> use <char> as path separator when parsing;\n"
" currently limited to ';' or ':' but defaults to ';'\n"
"Output type options:\n"
" -c convert to Cygwin form of path\n"
" -m convert to MSYS form of path: currently an alias for -w\n"
" -u convert to Unix form of path\n"
" -w convert to Windows form of path\n"
"Other options:\n"
" -k return kind of the local environment\n"
" -p return kind of the input path\n"
"\n"
"Default is to convert to the format used by the platform.\n"
;
#undef DEBUG
struct Options {
int target;
int escapes;
int drive;
int debug;
int blank;
int slash;
int pathkind;
int sep;
} cvtoptions;
static char* escape(const char* path);
static void usage(const char* msg);
static void
usage(const char* msg)
{
if(msg != NULL) fprintf(stderr,"%s\n",msg);
fprintf(stderr,"%s",USAGE);
if(msg == NULL) exit(0); else exit(1);
}
static char*
escape(const char* path)
{
size_t slen = strlen(path);
const char* p;
char* q;
char* epath = NULL;
const char* escapes = " \\";
epath = (char*)malloc((2*slen) + 1);
if(epath == NULL) usage("out of memtory");
p = path;
q = epath;
for(;*p;p++) {
if(strchr(escapes,*p) != NULL)
*q++ = '\\';
*q++ = *p;
}
*q = '\0';
return epath;
}
static char*
slash(const char* path)
{
size_t slen = strlen(path);
const char* p;
char* q;
char* epath = NULL;
epath = (char*)malloc(slen + 1);
if(epath == NULL) usage("out of memtory");
p = path;
q = epath;
for(;*p;p++) {
if(*p == '\\')
*q++ = '/';
else *q++ = *p;
}
*q = '\0';
return epath;
}
void
printlocalkind(void)
{
const char* s = NULL;
int kind = NCgetlocalpathkind();
switch (kind) {
case NCPD_NIX: s = "unix"; break;
case NCPD_MSYS: s = "msys"; break;
case NCPD_CYGWIN: s = "cygwin"; break;
case NCPD_WIN: s = "win"; break;
default: s = "unknown"; break;
}
printf("%s",s);
exit(0);
}
void
printenv(void)
{
#ifdef __CYGWIN__
printf(" __CYGWIN__");
#else
printf(" !__CYGWIN__");
#endif
#ifdef _MSC_VER
printf(" _MSC_VER");
#else
printf(" !_MSC_VER");
#endif
#ifdef _WIN32
printf(" _WIN32");
#else
printf(" !_WIN32");
#endif
#ifdef __MSYS__
printf(" __MSYS__");
#else
printf(" !__MSYS__");
#endif
#ifdef __MSYS2__
printf(" __MSYS2__");
#else
printf(" !__MSYS2__");
#endif
#ifdef __MINGW32__
printf(" __MINGW32__");
#else
printf(" !__MINGW32__");
#endif
printf("\n");
exit(0);
}
void
printpathkind(const char* path)
{
const char* s = NULL;
int kind = NCgetinputpathkind(path);
switch (kind) {
case NCPD_NIX: s = "unix"; break;
case NCPD_MSYS: s = "msys"; break;
case NCPD_CYGWIN: s = "cygwin"; break;
case NCPD_WIN: s = "win"; break;
case NCPD_REL: s = "relative"; break;
default: s = "unknown"; break;
}
printf("%s",s);
exit(0);
}
int
processdir(const char* indir, char** cvtdirp)
{
char* cvtdir = NULL;
if(cvtoptions.target == NCPD_UNKNOWN) {
cvtdir = NCpathcvt(indir);
} else {
cvtdir = NCpathcvt_test(indir,cvtoptions.target,(char)cvtoptions.drive);
}
if(cvtdir && cvtoptions.escapes) {
char* dir = cvtdir; cvtdir = NULL;
cvtdir = escape(dir);
free(dir);
}
if(cvtdir && cvtoptions.slash) {
char* dir = cvtdir; cvtdir = NULL;
cvtdir = slash(dir);
free(dir);
}
if(cvtdirp) {*cvtdirp = cvtdir; cvtdir = NULL;}
if(cvtdir) free(cvtdir);
return 0;
}
int
main(int argc, char** argv)
{
int c;
char* inpath = NULL;
NCPluginList indirs = {0,NULL};
NCbytes* outpath = ncbytesnew();
int stat = NC_NOERR;
size_t i;
memset((void*)&cvtoptions,0,sizeof(cvtoptions));
cvtoptions.drive = 'c';
cvtoptions.sep = ';';
while ((c = getopt(argc, argv, "B:D:FS:Xchkmpuwx")) != EOF) {
switch(c) {
case 'c': cvtoptions.target = NCPD_CYGWIN; break;
case 'h': usage(NULL); break;
case 'k': printlocalkind(); break;
case 'm': cvtoptions.target = NCPD_WIN; break; /* Aliased */
case 'p': cvtoptions.pathkind = 1; break;
case 'u': cvtoptions.target = NCPD_NIX; break;
case 'w': cvtoptions.target = NCPD_WIN; break;
case 'x': cvtoptions.escapes = 1; break;
case 'B':
cvtoptions.blank = optarg[0];
if(cvtoptions.blank < ' ' || cvtoptions.blank == '\177')
usage("Bad -B argument");
break;
case 'D': cvtoptions.drive = optarg[0]; break;
case 'F': cvtoptions.slash = 1; break;
case 'S': cvtoptions.sep = optarg[0]; break;
case 'X': printenv(); break;
case '?':
usage("unknown option");
break;
}
}
argc -= optind;
argv += optind;
/* If no file arguments left or more than one, print usage message. */
if (argc == 0)
usage("no path specified");
if (argc > 1)
usage("more than one path specified");
/* translate blanks */
inpath = (char*)malloc(strlen(argv[0])+1);
if(inpath == NULL) usage("Out of memory");
{
const char* p = argv[0];
char* q = inpath;
for(;*p;p++) {
char c = *p;
if(c == cvtoptions.blank) c = ' ';
*q++ = c;
}
*q = '\0';
}
if(cvtoptions.pathkind) {
printpathkind(inpath);
goto done;
}
/* Break using the path separator */
if((stat = ncaux_plugin_path_parse(inpath,cvtoptions.sep,&indirs)))
{usage(nc_strerror(stat));}
for(i=0;i<indirs.ndirs;i++) {
char* outdir = NULL;
if((stat = processdir(indirs.dirs[i],&outdir)))
{usage(nc_strerror(stat));}
if(i > 0) ncbytesappend(outpath,cvtoptions.sep);
ncbytescat(outpath,outdir);
nullfree(outdir);
}
printf("%s",ncbytescontents(outpath));
done:
if(inpath) free(inpath);
ncaux_plugin_path_clear(&indirs);
ncbytesfree(outpath);
return 0;
}