-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathrun_stubs.c
115 lines (102 loc) · 3.63 KB
/
run_stubs.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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Sebastien Hinderer, projet Gallium, INRIA Paris */
/* */
/* Copyright 2016 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
/* Stubs to let OCaml programs use the run library */
#define _GNU_SOURCE
#define CAML_INTERNALS
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include "run.h"
#include "caml/mlvalues.h"
#include "caml/memory.h"
#include "caml/io.h"
#include "caml/osdeps.h"
/* cstringvect: inspired by similar function in otherlibs/unix/cstringv.c */
static array cstringvect(value arg)
{
array res;
mlsize_t size, i;
size = Wosize_val(arg);
res = (array) caml_stat_alloc((size + 1) * sizeof(char_os *));
for (i = 0; i < size; i++)
res[i] = caml_stat_strdup_to_os(String_val(Field(arg, i)));
res[size] = NULL;
return res;
}
static void free_cstringvect(array v)
{
char_os **p;
for (p = v; *p != NULL; p++)
caml_stat_free(*p);
caml_stat_free(v);
}
#define caml_channel_lock Lock
#define caml_channel_unlock Unlock
static void logToChannel(void *voidchannel, const char *fmt, va_list ap)
{
struct channel *channel = (struct channel *) voidchannel;
int length, initialTextLength = 512;
char *text = malloc(512);
if (text == NULL) return;
length = vsnprintf(text, initialTextLength, fmt, ap);
if (length <= 0)
{
free(text);
return;
}
if (length > initialTextLength)
{
free(text);
text = malloc(length);
if (text == NULL) return;
if (vsnprintf(text, length, fmt, ap) != length) goto end;
}
Lock(channel);
caml_putblock(channel, text, length);
caml_flush(channel);
Unlock(channel);
end:
free(text);
}
CAMLprim value caml_run_command(value caml_settings)
{
int res;
command_settings settings;
CAMLparam1(caml_settings);
settings.program =
caml_stat_strdup_to_os(String_val(Field(caml_settings, 0)));
settings.argv = cstringvect(Field(caml_settings, 1));
settings.envp = cstringvect(Field(caml_settings, 2));
settings.stdin_filename =
caml_stat_strdup_to_os(String_val(Field(caml_settings, 3)));
settings.stdout_filename =
caml_stat_strdup_to_os(String_val(Field(caml_settings, 4)));
settings.stderr_filename =
caml_stat_strdup_to_os(String_val(Field(caml_settings, 5)));
settings.append = Bool_val(Field(caml_settings, 6));
settings.timeout = Int_val(Field(caml_settings, 7));
settings.logger = logToChannel;
settings.loggerData = Channel(Field(caml_settings, 8));
res = run_command(&settings);
caml_stat_free(settings.program);
free_cstringvect(settings.argv);
free_cstringvect(settings.envp);
caml_stat_free(settings.stdin_filename);
caml_stat_free(settings.stdout_filename);
caml_stat_free(settings.stderr_filename);
CAMLreturn(Val_int(res));
}