forked from taviso/ctypes.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.c
129 lines (113 loc) · 4.12 KB
/
types.c
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
#define _GNU_SOURCE
#include <dlfcn.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <stdbool.h>
#include <link.h>
#include <ffi.h>
#include <inttypes.h>
#include "builtins.h"
#include "variables.h"
#include "arrayfunc.h"
#include "common.h"
#include "bashgetopt.h"
#include "types.h"
#include "util.h"
bool decode_primitive_type(const char *parameter, void **value, ffi_type **type)
{
const char *prefix;
prefix = NULL;
*value = NULL;
*type = NULL;
// If a colon exists, then everything before it is a type
if (strchr(parameter, ':')) {
// Extract the two components.
prefix = strndupa(parameter, strchr(parameter, ':') - parameter);
parameter = strchr(parameter, ':') + 1;
} else {
intmax_t n;
char *string;
// No type was specified, so there are only two possibilities,
// If this is a legal number, then it's an int. Otherwise, this is a
// string.
if (check_parse_long(parameter, &n)) {
*type = &ffi_type_sint;
*value = malloc(ffi_type_sint.size);
memcpy(*value, &n, ffi_type_sint.size);
return true;
}
// This must be a string.
*type = &ffi_type_pointer;
*value = malloc(ffi_type_pointer.size);
string = strdup(parameter);
memcpy(*value, &string, ffi_type_pointer.size);
return true;
}
if (decode_type_prefix(prefix, parameter, type, value, NULL) != true) {
builtin_warning("parameter decoding failed");
return false;
}
return true;
}
bool decode_type_prefix(const char *prefix, const char *value, ffi_type **type, void **result, char **pformat)
{
static struct {
char *prefix;
ffi_type *type;
char *sformat;
char *pformat;
} types[] = {
{ "uint8", &ffi_type_uint8, "%" SCNu8, "uint8:%" PRIu8 },
{ "int8", &ffi_type_sint8, "%" SCNd8, "int8:%" PRId8 },
{ "uint16", &ffi_type_uint16, "%" SCNu16, "uint16:%" PRIu16 },
{ "int16", &ffi_type_sint16, "%" SCNd16, "int16:%" PRId16 },
{ "uint32", &ffi_type_uint32, "%" SCNu32, "uint32:%" PRIu32 },
{ "int32", &ffi_type_sint32, "%" SCNd32, "int32:%" PRId32 },
{ "uint64", &ffi_type_uint64, "%" SCNu64, "uint64:%" PRIu64 },
{ "int64", &ffi_type_sint64, "%" SCNd64, "int64:%" PRId64 },
{ "float", &ffi_type_float, "%f", "float:%f" },
{ "double", &ffi_type_double, "%lf", "double:%lf" },
{ "char", &ffi_type_schar, "%c", "char:%c" },
{ "uchar", &ffi_type_uchar, "%c", "uchar:%c" },
{ "ushort", &ffi_type_ushort, "%hu", "ushort:%hu" },
{ "short", &ffi_type_sshort, "%hd", "short:%hd", },
{ "unsigned", &ffi_type_uint, "%u", "unsigned:%u" },
{ "int", &ffi_type_sint, "%d", "int:%d" },
{ "ulong", &ffi_type_ulong, "%lu", "ulong:%lu" },
{ "long", &ffi_type_slong, "%ld", "long:%ld" },
{ "longdouble", &ffi_type_longdouble, "%llg", "longdouble:%llg" },
{ "pointer", &ffi_type_pointer, "%p", "pointer:%p" },
{ "string", &ffi_type_pointer, "%ms", "string:%s" },
{ "void", &ffi_type_void, "", "" },
{ NULL },
};
for (int i = 0; types[i].prefix; i++) {
if (strcmp(types[i].prefix, prefix) == 0) {
// Prefix matched type, return information user requested.
if (type) {
*type = types[i].type;
}
if (pformat) {
*pformat = types[i].pformat;
}
// Caller wants us to decode it, lets go ahead.
if (result) {
*result = malloc(types[i].type->size);
if (sscanf(value, types[i].sformat, *result) != 1) {
builtin_warning("failed to parse %s as a %s", value, prefix);
free(*result);
return false;
}
}
return true;
}
}
builtin_warning("unrecognised type prefix %s", prefix);
return false;
}