Debian Codegraph @

src/shellexec_wcc.c

raw symbols SCIP
1 /* This file partially modifies from
2 *
3 * R-devel\src\include\Defn.h
4 * R-devel\src\main\sysutils.c
5 * R-devel\src\main\names.c
6 * R-devel\src\gnuwin32\extra.c
7 *
8 * WCC: The default shell.exec did not have any option. The application is
9 * always active in a normal window. This modification is trying to add
10 * options such that the application can be in a minimized window or an
11 * invisible window or a background running window. The application should
12 * not block the R GUI in any case.
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 /* From R-devel\src\include\Defn.h */
29 /*
30 #define BYTES_MASK (1<<1)
31 #define LATIN1_MASK (1<<2)
32 #define UTF8_MASK (1<<3)
33 #define IS_BYTES(x) (LEVELS(x) & BYTES_MASK)
34 #define IS_LATIN1(x) (LEVELS(x) & LATIN1_MASK)
35 #define IS_UTF8(x) (LEVELS(x) & UTF8_MASK)
36 */
37
38
39 /* From R-devel\src\main\sysutils.c */
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 if(IS_LATIN1(fn)) from = "latin1";
57 if(IS_UTF8(fn)) from = "UTF-8";
58 if(IS_BYTES(fn)) warning("encoding of a filename cannot be 'bytes'");
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 } /* End of filenameToWchar_wcc(). */
80 #endif
81
82
83 /* From R-devel\src\gnuwin32\extra.c */
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){ /* an error condition */
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 } /* End of internal_shellexecW_wcc().*/
115 #endif
116
117
118 /* From R-devel\src\main\names.c and R-devel\src\gnuwin32\extra.c */
119 SEXP shellexec_wcc(SEXP R_file, SEXP R_SW_cmd, SEXP R_fn_enc){
120 /* Possible values of R_SW_cmd, see MS website for details.
121 * https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx
122 *
123 * SW_SHOW (5)
124 * Activates the window and displays it in its current size
125 * and position.
126 *
127 * SW_SHOWMINIMIZED (2)
128 * Activates the window and displays it as a minimized window.
129 *
130 * SW_SHOWMINNOACTIVE (7)
131 * Displays the window as a minimized window.
132 * The active window remains active.
133 *
134 * SW_SHOWNA (8)
135 * Displays the window in its current state.
136 * The active window remains active.
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 } /* End of shellexec_wcc(). */