Add backslash escape option to pathcvt and test

This commit is contained in:
Dennis Heimbigner 2021-05-30 16:12:40 -06:00
parent 3c5740dde5
commit a99afdefc6
3 changed files with 78 additions and 35 deletions

View File

@ -27,16 +27,16 @@
/*
Synopsis
pathcvt [-u|-w|-m|-c] PATH
pathcvt [-u|-w|-m|-c] [-e] PATH
Options
-e add backslash escapes to '\' and ' '
Output type options:
-u convert to Unix form of path
-w convert to Windows form of path
-m convert to MSYS form of path
-c convert to Cygwin form of path
Default is to convert to the format used by the platform.
*/
@ -45,16 +45,12 @@ Default is to convert to the format used by the platform.
struct Options {
int target;
int escape;
int debug;
} cvtoptions;
static void
usage(const char* msg)
{
if(msg != NULL) fprintf(stderr,"%s\n",msg);
fprintf(stderr,"pathcvt [-u|-w|-m|-c] PATH\n");
if(msg == NULL) exit(0); else exit(1);
}
static char* escape(const char* path);
static void usage(const char* msg);
int
main(int argc, char** argv)
@ -65,9 +61,10 @@ main(int argc, char** argv)
memset((void*)&cvtoptions,0,sizeof(cvtoptions));
while ((c = getopt(argc, argv, "cD:hmuw")) != EOF) {
while ((c = getopt(argc, argv, "cD:ehmuw")) != EOF) {
switch(c) {
case 'c': cvtoptions.target = NCPD_CYGWIN; break;
case 'e': cvtoptions.escape = 1; break;
case 'h': usage(NULL); break;
case 'm': cvtoptions.target = NCPD_MSYS; break;
case 'u': cvtoptions.target = NCPD_NIX; break;
@ -90,12 +87,51 @@ main(int argc, char** argv)
if (argc > 1)
usage("more than one path specified");
inpath = argv[0];
if(cvtoptions.target == NCPD_UNKNOWN)
cvtpath = NCpathcvt(inpath);
else
cvtpath = NCpathcvt_test(inpath,cvtoptions.target,'c');
if(cvtpath && cvtoptions.escape) {
char* path = cvtpath; cvtpath = NULL;
cvtpath = escape(path);
free(path);
}
printf("%s",cvtpath);
if(cvtpath) free(cvtpath);
return 0;
}
static void
usage(const char* msg)
{
if(msg != NULL) fprintf(stderr,"%s\n",msg);
fprintf(stderr,"pathcvt [-u|-w|-m|-c] PATH\n");
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;
epath = (char*)malloc((2*slen) + 1);
if(epath == NULL) usage("out of memtory");
p = path;
q = epath;
for(;*p;p++) {
switch (*p) {
case '\\': case ' ':
*q++ = '\\';
/* fall thru */
default:
*q++ = *p;
break;
}
}
*q = '\0';
return epath;
}

View File

@ -1,40 +1,45 @@
path: /xxx/a/b:
path: /xxx/a/b
/xxx/a/b
/cygdrive/c/xxx/a/b
/c/xxx/a/b
c:\xxx\a\b
path: d:/x/y:
c:\\xxx\\a\\b
path: d:/x/y
/d/x/y
/cygdrive/d/x/y
/d/x/y
d:\x\y
path: d:\x\y:
d:\\x\\y
path: /cygdrive/d/x/y
/d/x/y
/cygdrive/d/x/y
/d/x/y
d:\x\y
path: /cygdrive/d/x/y:
d:\\x\\y
path: /d/x/y
/d/x/y
/cygdrive/d/x/y
/d/x/y
d:\x\y
path: /d/x/y:
/d/x/y
/cygdrive/d/x/y
/d/x/y
d:\x\y
path: /cygdrive/d:
d:\\x\\y
path: /cygdrive/d
/d
/cygdrive/d
/d
d:
path: /d:
path: /d
/d
/cygdrive/d
/d
d:
path: /cygdrive/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn:
path: /cygdrive/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn
/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn
/cygdrive/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn
/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn
d:\git\netcdf-c\dap4_test\test_anon_dim.2.syn
d:\\git\\netcdf-c\\dap4_test\\test_anon_dim.2.syn
path: d:\x\y
/d/x/y
/cygdrive/d/x/y
/d/x/y
d:\\x\\y
path: d:\x\y w\z
/d/x/y w/z
/cygdrive/d/x/y w/z
/d/x/y w/z
d:\\x\\y\\ w\\z

View File

@ -8,29 +8,31 @@ set -e
testcase1() {
T="$1"
P="$2"
echo -n ' '
${execdir}/pathcvt $T "$P"
echo ""
C=`${execdir}/pathcvt $T "$P"`
echo " $C"
}
testcase() {
echo "path: $1:"
#X=`echo -n "$1" | sed -e 's/\\\/\\\\\\\/g'`
X="$1"
echo "path:" "$X"
testcase1 "-u" "$1"
testcase1 "-c" "$1"
testcase1 "-m" "$1"
testcase1 "-w" "$1"
testcase1 "-ew" "$1" | sed -e 's/\\/\\\\/g'
}
rm -f tmp_pathcvt.txt
testcase "/xxx/a/b" >> tmp_pathcvt.txt
testcase "d:/x/y" >> tmp_pathcvt.txt
testcase "d:\\x\\y" >> tmp_pathcvt.txt
testcase "/cygdrive/d/x/y" >> tmp_pathcvt.txt
testcase "/d/x/y" >> tmp_pathcvt.txt
testcase "/cygdrive/d" >> tmp_pathcvt.txt
testcase "/d" >> tmp_pathcvt.txt
testcase "/cygdrive/d/git/netcdf-c/dap4_test/test_anon_dim.2.syn" >> tmp_pathcvt.txt
testcase "d:\\x\\y" >> tmp_pathcvt.txt
testcase "d:\\x\\y w\\z" >> tmp_pathcvt.txt
diff -w ${srcdir}/ref_pathcvt.txt ./tmp_pathcvt.txt