| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <R.h> |
| 16 |
#include <Rinternals.h> |
| 17 |
#include <R_ext/Riconv.h> |
| 18 |
|
| 19 |
#include <wchar.h> |
| 20 |
|
| 21 |
#ifdef WIN |
| 22 |
#include <windows.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#define UNUSED(x) (void)(x) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#ifdef WIN |
| 41 |
#define BSIZE 100000 |
| 42 |
wchar_t *filenameToWchar_wcc(const SEXP fn, const Rboolean expand, |
| 43 |
int C_fn_enc){ |
| 44 |
static wchar_t filename[BSIZE + 1]; |
| 45 |
void *obj; |
| 46 |
const char *from = "", *inbuf; |
| 47 |
char *outbuf; |
| 48 |
size_t inb, outb, res; |
| 49 |
|
| 50 |
if(!strlen(CHAR(fn))){ |
| 51 |
wcscpy(filename, L""); |
| 52 |
return filename; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
if(C_fn_enc == 1) from = "latin1"; |
| 61 |
if(C_fn_enc == 2) from = "UTF-8"; |
| 62 |
if(C_fn_enc == 3) warning("encoding of a filename cannot be 'bytes'"); |
| 63 |
|
| 64 |
obj = Riconv_open("UCS-2LE", from); |
| 65 |
if(obj == (void *)(-1)) |
| 66 |
warning("unsupported conversion from '%s' in shellexec_wcc.c", |
| 67 |
from); |
| 68 |
|
| 69 |
if(expand) inbuf = R_ExpandFileName(CHAR(fn)); else inbuf = CHAR(fn); |
| 70 |
|
| 71 |
inb = strlen(inbuf)+1; outb = 2*BSIZE; |
| 72 |
outbuf = (char *) filename; |
| 73 |
res = Riconv(obj, &inbuf , &inb, &outbuf, &outb); |
| 74 |
Riconv_close(obj); |
| 75 |
if(inb > 0) warning("file name conversion problem -- name too long?"); |
| 76 |
if(res == -1) warning("file name conversion problem"); |
| 77 |
|
| 78 |
return filename; |
| 79 |
} |
| 80 |
#endif |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#ifdef WIN |
| 85 |
static inline void internal_shellexecW_wcc(const wchar_t * file, Rboolean rhome, |
| 86 |
int C_SW_cmd){ |
| 87 |
const wchar_t *home; |
| 88 |
wchar_t home2[10000], *p; |
| 89 |
uintptr_t ret; |
| 90 |
|
| 91 |
if(rhome){ |
| 92 |
home = _wgetenv(L"R_HOME"); |
| 93 |
if(home == NULL) warning("R_HOME not set"); |
| 94 |
wcsncpy(home2, home, 10000); |
| 95 |
for(p = home2; *p; p++) if(*p == L'/') *p = L'\\'; |
| 96 |
home = home2; |
| 97 |
} else{ |
| 98 |
home = NULL; |
| 99 |
} |
| 100 |
|
| 101 |
ret = (uintptr_t) ShellExecuteW(NULL, L"open", file, NULL, home, |
| 102 |
C_SW_cmd); |
| 103 |
if(ret <= 32){ |
| 104 |
if(ret == ERROR_FILE_NOT_FOUND || ret == ERROR_PATH_NOT_FOUND |
| 105 |
|| ret == SE_ERR_FNF || ret == SE_ERR_PNF) |
| 106 |
warning("'%ls' not found", file); |
| 107 |
if(ret == SE_ERR_ASSOCINCOMPLETE || ret == SE_ERR_NOASSOC) |
| 108 |
warning("file association for '%ls' not available or invalid", |
| 109 |
file); |
| 110 |
if(ret == SE_ERR_ACCESSDENIED || ret == SE_ERR_SHARE) |
| 111 |
warning("access to '%ls' denied", file); |
| 112 |
warning("problem in displaying '%ls'", file); |
| 113 |
} |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
SEXP shellexec_wcc(SEXP R_file, SEXP R_SW_cmd, SEXP R_fn_enc){ |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#ifdef WIN |
| 140 |
internal_shellexecW_wcc( |
| 141 |
filenameToWchar_wcc(STRING_ELT(R_file, 0), FALSE, |
| 142 |
INTEGER(R_fn_enc)[0]), |
| 143 |
FALSE, INTEGER(R_SW_cmd)[0]); |
| 144 |
#else |
| 145 |
UNUSED(R_file); |
| 146 |
UNUSED(R_SW_cmd); |
| 147 |
#endif |
| 148 |
|
| 149 |
return(R_NilValue); |
| 150 |
} |