-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemObject.c
More file actions
176 lines (140 loc) · 3.76 KB
/
SystemObject.c
File metadata and controls
176 lines (140 loc) · 3.76 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SystemObject.h"
const char *prog = "system";
static bool identifiersInitialized = false;
#define SYSTEM_VERSION "0.6"
#define SYSTEM_PROPERTY_VERSION 0
#define SYSTEM_NUM_PROPERTIES 1
static NPIdentifier systemPropertyIdentifiers[SYSTEM_NUM_PROPERTIES];
static const NPUTF8 *systemPropertyNames[SYSTEM_NUM_PROPERTIES] = {
"version",
};
#define SYSTEM_METHOD_EXEC 0
#define SYSTEM_NUM_METHODS 1
static NPIdentifier systemMethodIdentifiers[SYSTEM_NUM_METHODS];
static const NPUTF8 *systemMethodNames[SYSTEM_NUM_METHODS] = {
"exec",
};
static NPObject *systemAllocate(NPP npp, NPClass *theClass)
{
SystemObject *newInstance = (SystemObject *)malloc(sizeof(SystemObject));
if (!identifiersInitialized) {
browser->getstringidentifiers(systemPropertyNames,
SYSTEM_NUM_PROPERTIES, systemPropertyIdentifiers);
browser->getstringidentifiers(systemMethodNames,
SYSTEM_NUM_METHODS, systemMethodIdentifiers);
identifiersInitialized = true;
}
return &newInstance->header;
}
static void systemDeallocate(NPObject *obj)
{
free(obj);
}
static void systemInvalidate(NPObject *obj)
{
}
static bool systemHasMethod(NPObject *obj, NPIdentifier name)
{
int i;
for (i = 0; i < SYSTEM_NUM_METHODS; i++)
if (name == systemMethodIdentifiers[i])
return true;
return false;
}
static bool do_exec(const NPVariant cmd, NPVariant *result);
static bool systemInvoke(NPObject *obj, NPIdentifier name, const NPVariant *args,
uint32_t argCount, NPVariant *variant)
{
if (name == systemMethodIdentifiers[SYSTEM_METHOD_EXEC]) {
if (argCount != 1) {
fprintf(stderr, "%s: bad arguments", prog);
return false;
}
return do_exec(args[0], variant);
}
return false;
}
static bool systemInvokeDefault(NPObject *obj, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
return false;
}
static bool systemHasProperty(NPObject *obj, NPIdentifier name)
{
int i;
for (i = 0; i < SYSTEM_NUM_PROPERTIES; i++)
if (name == systemPropertyIdentifiers[i])
return true;
return false;
}
static bool systemGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant)
{
if (name == systemPropertyIdentifiers[SYSTEM_PROPERTY_VERSION]) {
STRINGZ_TO_NPVARIANT(strdup(SYSTEM_VERSION), *variant);
return true;
}
return false;
}
static bool systemSetProperty(NPObject *obj, NPIdentifier name,
const NPVariant *variant)
{
return false;
}
static NPClass systemClass = {
NP_CLASS_STRUCT_VERSION,
systemAllocate,
systemDeallocate,
systemInvalidate,
systemHasMethod,
systemInvoke,
systemInvokeDefault,
systemHasProperty,
systemGetProperty,
systemSetProperty,
};
NPClass *getSystemClass(void)
{
return &systemClass;
}
static bool do_exec(const NPVariant cmd, NPVariant *result)
{
bool ret = false;
const char *cmd_str;
char *out_str;
FILE *fp = NULL;
char buf[1024 * 1024];
/* set default return value */
NULL_TO_NPVARIANT(*result);
/* init algor */
if (!NPVARIANT_IS_STRING(cmd)) {
fprintf(stderr, "%s (%s %d): invalid argument type\n", prog, __FILE__, __LINE__);
goto end;
}
if (!(cmd_str = NPVARIANT_TO_STRING(cmd).UTF8Characters)) {
fprintf(stderr, "%s (%s %d): invalid argument value\n", prog, __FILE__, __LINE__);
goto end;
}
if (strlen(cmd_str) <= 0) {
fprintf(stderr, "%s (%s %d): invalid argument value\n", prog, __FILE__, __LINE__);
goto end;
}
if (!(fp = popen(cmd_str, "r"))) {
fprintf(stderr, "%s (%s %d): execute `%s' failed\n", prog, __FILE__, __LINE__, cmd_str);
goto end;
}
buf[0] = 0;
fread(buf, 1, sizeof(buf), fp);
if (!(out_str = strdup(buf))) {
fprintf(stderr, "%s (%s %d): inner error\n", prog, __FILE__, __LINE__);
goto end;
}
/* set output */
STRINGZ_TO_NPVARIANT(out_str, *result);
ret = true;
end:
if (fp) pclose(fp);
return ret;
}