0
|
1 /*
|
|
2 Package: dyncall
|
|
3 File: bindings/shell.c
|
|
4 Description: printf(1) style function call mechanism
|
|
5 License:
|
1
|
6 Copyright (c) 2007-2015 Daniel Adler <dadler@uni-goettingen.de>,
|
0
|
7 Tassilo Philipp <tphilipp@potion-studios.com>
|
|
8
|
|
9 Permission to use, copy, modify, and distribute this software for any
|
|
10 purpose with or without fee is hereby granted, provided that the above
|
|
11 copyright notice and this permission notice appear in all copies.
|
|
12
|
|
13 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
14 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
15 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
16 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
|
21 */
|
|
22
|
|
23 #include "../../dyncall/dyncall/dyncall.h"
|
|
24 #include "../../dyncall/dynload/dynload.h"
|
|
25 #include "../../dyncall/dyncall/dyncall_signature.h"
|
|
26 #include <stdio.h>
|
40
|
27 #include <string.h>
|
0
|
28 #include <stdlib.h> /* needed on some platforms to make atof work _at_runtime_ */
|
|
29
|
24
|
30 #define SHDC_VERSION "1.0"
|
0
|
31
|
|
32
|
|
33 void usage(const char* s)
|
|
34 {
|
|
35 printf(
|
|
36 "Usage: %s -l SO\n"
|
|
37 " %s -c SO SYM SIG [ARGS]\n"
|
|
38 " %s -v\n"
|
|
39 " where SO is the name of the shared object.\n"
|
|
40 "\n"
|
|
41 " -l lists all symbol names in the shared object\n"
|
|
42 " -c calls function in the shared object, where SYM is the symbol name,\n"
|
|
43 " SIG the symbol's type signature, and ARGS the arguments.\n"
|
|
44 " -v displays the binding's version\n",
|
|
45 s, s, s
|
|
46 );
|
|
47 }
|
|
48
|
|
49
|
|
50 int main(int argc, char* argv[])
|
|
51 {
|
|
52 const char* libPath;
|
|
53 const char* symName;
|
|
54 const DCsigchar* sig;
|
|
55 const DCsigchar* i;
|
|
56 void* sym;
|
|
57 DCCallVM* vm;
|
|
58 DLLib* dlLib;
|
|
59 DLSyms* dlSyms;
|
|
60 int c, l;
|
|
61
|
|
62 if(argc == 2 && strcmp(argv[1], "-v") == 0) {
|
|
63 printf(SHDC_VERSION"\n");
|
|
64 return 0;
|
|
65 }
|
|
66
|
|
67 /* Parse arguments and check validity. */
|
|
68 /* Need at least shared object name and action, and symbol name and signature string for call. */
|
|
69 if(argc < 2) {
|
|
70 usage(argv[0]);
|
|
71 return 1;
|
|
72 }
|
|
73
|
|
74 c = strcmp(argv[1], "-c");
|
|
75 l = strcmp(argv[1], "-l");
|
|
76 if((c != 0 && l != 0) || (c == 0 && argc < 4)) {
|
|
77 usage(argv[0]);
|
|
78 return 1;
|
|
79 }
|
|
80
|
40
|
81 /* if lib path is empty string, use NULL as reference to own process/exe */
|
|
82 libPath = argv[2][0] == '\0' ? NULL : argv[2];
|
0
|
83
|
|
84 /* List symbols, if 'ls', else it must be 'call', so proceed to call. */
|
|
85 if(l == 0) {
|
|
86 dlSyms = dlSymsInit(libPath);
|
|
87 if(!dlSyms) {
|
40
|
88 printf("Can't load \"%s\".\n", libPath?libPath:"<NULL>");
|
0
|
89 usage(argv[0]);
|
|
90 return 1;
|
|
91 }
|
|
92
|
|
93 /* hacky: reuse c and l */
|
|
94 for(c=dlSymsCount(dlSyms), l=0; l<c; ++l)
|
|
95 printf("%s\n", dlSymsName(dlSyms, l));
|
|
96
|
|
97 dlSymsCleanup(dlSyms);
|
|
98 }
|
|
99 else {
|
|
100 /* Check if number of arguments matches sigstring spec. */
|
|
101 /*if(n != argc-4)@@@*/ /* 0 is prog, 1 is flag, 2 is lib, 3 is symbol name, 4 is sig */
|
|
102
|
|
103 /* Load library and get a pointer to the symbol to call. */
|
|
104 dlLib = dlLoadLibrary(libPath);
|
|
105 if(!dlLib) {
|
40
|
106 printf("Can't load \"%s\".\n", libPath?libPath:"<NULL>");
|
0
|
107 usage(argv[0]);
|
|
108 return 1;
|
|
109 }
|
|
110
|
|
111 symName = argv[3];
|
|
112 sig = i = argv[4];
|
|
113
|
|
114 sym = dlFindSymbol(dlLib, symName);
|
|
115 if(!sym) {
|
40
|
116 /* this might be a syscall attempt, check if "symbol" is numeric */
|
|
117 int n;
|
|
118 if(sscanf(symName, "%d", &n) == 0) {
|
|
119 printf("Can't find symbol \"%s\".\n", symName);
|
|
120 dlFreeLibrary(dlLib);
|
|
121 usage(argv[0]);
|
|
122 return 1;
|
|
123 }
|
|
124 sym = (void*)(size_t)n;
|
0
|
125 }
|
|
126
|
|
127
|
|
128 vm = dcNewCallVM(4096/*@@@*/);/*@@@ error checking */
|
|
129 dcReset(vm);
|
|
130
|
|
131 while(*i != '\0' && *i != DC_SIGCHAR_ENDARG) {
|
|
132 switch(*i) {
|
|
133 case DC_SIGCHAR_CC_PREFIX:
|
40
|
134 if(*(i+1) != '\0')
|
|
135 {
|
|
136 DCint mode = dcGetModeFromCCSigChar(*++i);
|
|
137 if(mode != DC_ERROR_UNSUPPORTED_MODE)
|
|
138 dcMode(vm, mode);
|
0
|
139 }
|
|
140 sig += 2;
|
|
141 break;
|
|
142
|
|
143 case DC_SIGCHAR_BOOL: dcArgBool (vm, (DCbool) atoi (argv[5+i-sig] )); break;
|
|
144 case DC_SIGCHAR_CHAR: dcArgChar (vm, (DCchar) atoi (argv[5+i-sig] )); break;
|
|
145 case DC_SIGCHAR_UCHAR: dcArgChar (vm, (DCchar)(DCuchar) atoi (argv[5+i-sig] )); break;
|
|
146 case DC_SIGCHAR_SHORT: dcArgShort (vm, (DCshort) atoi (argv[5+i-sig] )); break;
|
|
147 case DC_SIGCHAR_USHORT: dcArgShort (vm, (DCshort)(DCushort)atoi (argv[5+i-sig] )); break;
|
|
148 case DC_SIGCHAR_INT: dcArgInt (vm, (DCint) strtol (argv[5+i-sig],NULL,10)); break;
|
|
149 case DC_SIGCHAR_UINT: dcArgInt (vm, (DCint)(DCuint) strtoul (argv[5+i-sig],NULL,10)); break;
|
|
150 case DC_SIGCHAR_LONG: dcArgLong (vm, (DClong) strtol (argv[5+i-sig],NULL,10)); break;
|
|
151 case DC_SIGCHAR_ULONG: dcArgLong (vm, (DCulong) strtoul (argv[5+i-sig],NULL,10)); break;
|
|
152 case DC_SIGCHAR_LONGLONG: dcArgLongLong(vm, (DClonglong) strtoll (argv[5+i-sig],NULL,10)); break;
|
|
153 case DC_SIGCHAR_ULONGLONG: dcArgLongLong(vm, (DCulonglong) strtoull(argv[5+i-sig],NULL,10)); break;
|
|
154 case DC_SIGCHAR_FLOAT: dcArgFloat (vm, (DCfloat) atof (argv[5+i-sig] )); break;
|
|
155 case DC_SIGCHAR_DOUBLE: dcArgDouble (vm, (DCdouble) atof (argv[5+i-sig] )); break;
|
|
156 case DC_SIGCHAR_POINTER: dcArgPointer (vm, (DCpointer) argv[5+i-sig] ); break;
|
|
157 case DC_SIGCHAR_STRING: dcArgPointer (vm, (DCpointer) argv[5+i-sig] ); break;
|
|
158 }
|
|
159 ++i;
|
|
160 }
|
|
161
|
|
162 if(*i == DC_SIGCHAR_ENDARG)
|
|
163 ++i;
|
|
164
|
|
165 switch(*i) {
|
|
166 case '\0':
|
|
167 case DC_SIGCHAR_VOID: dcCallVoid (vm,sym) ; break;
|
|
168 case DC_SIGCHAR_BOOL: printf("%d\n", dcCallBool (vm,sym)); break;
|
|
169 case DC_SIGCHAR_CHAR: printf("%d\n", dcCallChar (vm,sym)); break;
|
|
170 case DC_SIGCHAR_UCHAR: printf("%d\n", dcCallChar (vm,sym)); break;
|
|
171 case DC_SIGCHAR_SHORT: printf("%d\n", dcCallShort (vm,sym)); break;
|
|
172 case DC_SIGCHAR_USHORT: printf("%d\n", dcCallShort (vm,sym)); break;
|
|
173 case DC_SIGCHAR_INT: printf("%d\n", dcCallInt (vm,sym)); break;
|
|
174 case DC_SIGCHAR_UINT: printf("%d\n", dcCallInt (vm,sym)); break;
|
40
|
175 case DC_SIGCHAR_LONG: printf("%ld\n", dcCallLong (vm,sym)); break;
|
|
176 case DC_SIGCHAR_ULONG: printf("%ld\n", dcCallLong (vm,sym)); break;
|
0
|
177 case DC_SIGCHAR_LONGLONG: printf("%lld\n", dcCallLongLong(vm,sym)); break;
|
|
178 case DC_SIGCHAR_ULONGLONG: printf("%lld\n", dcCallLongLong(vm,sym)); break;
|
|
179 case DC_SIGCHAR_FLOAT: printf("%g\n", dcCallFloat (vm,sym)); break;
|
|
180 case DC_SIGCHAR_DOUBLE: printf("%g\n", dcCallDouble (vm,sym)); break;
|
40
|
181 case DC_SIGCHAR_POINTER: printf("%p\n", dcCallPointer (vm,sym)); break;
|
|
182 case DC_SIGCHAR_STRING: printf("%s", dcCallPointer (vm,sym)); break;
|
0
|
183 }
|
|
184
|
|
185 dlFreeLibrary(dlLib);
|
|
186 dcFree(vm);
|
|
187 }
|
|
188
|
|
189 return 0;
|
|
190 }
|
|
191
|