summaryrefslogtreecommitdiff
path: root/driver/utils/cwrapper.c
blob: cf763a275d7390b9505fd876f64af1e060f5dd1e (plain)
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

/* gcc on mingw is hardcoded to use /mingw (which is c:/mingw) to
   find various files. If this is a different version of mingw to the
   one that we have in the GHC tree then things can go wrong. We
   therefore need to add various -B flags to the gcc commandline,
   so that it uses our in-tree mingw. Hence this wrapper. */

#include "cwrapper.h"
#include <errno.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

void die(const char *fmt, ...) {
    va_list argp;

    va_start(argp, fmt);
    vfprintf(stderr, fmt, argp);
    va_end(argp);
    exit(1);
}

char *mkString(const char *fmt, ...) {
    char *p;
    int i, j;
    va_list argp;

    va_start(argp, fmt);
    i = vsnprintf(NULL, 0, fmt, argp);
    va_end(argp);

    if (i < 0) {
        die("snprintf 0 failed: errno %d: %s\n", errno, strerror(errno));
    }

    p = malloc(i + 1);
    if (p == NULL) {
        die("malloc failed: errno %d: %s\n", errno, strerror(errno));
    }

    va_start(argp, fmt);
    j = vsnprintf(p, i + 1, fmt, argp);
    va_end(argp);
    if (i < 0) {
        die("snprintf with %d failed: errno %d: %s\n",
            i + 1, errno, strerror(errno));
    }

    return p;
}

char *quote(char *str) {
    char *quotedStr;
    char *p;

    quotedStr = malloc(2 * strlen(str) + 2 + 1);
    if (quotedStr == NULL) {
        die("malloc failed: errno %d: %s\n", errno, strerror(errno));
    }
    p = quotedStr;
    *p++ = '"';
    while (*str) {
        if (*str == '"') {
            *p++ = '\\';
        }
        *p++ = *str++;
    }
    *p++ = '"';
    *p = '\0';

    return quotedStr;
}

__attribute__((noreturn)) int run(char *exePath, int numArgs1, char **args1, int numArgs2, char **args2) {
    char **p;
    char **newArgv;
    int i, ret;

    newArgv = malloc(sizeof(char *) * (1 + numArgs1 + numArgs2 + 1));
    if (newArgv == NULL) {
        die("malloc failed: errno %d: %s\n", errno, strerror(errno));
    }
    p = newArgv;
    *p++ = quote(exePath);
    for (i = 0; i < numArgs1; i++) {
        *p++ = quote(args1[i]);
    }
    for (i = 0; i < numArgs2; i++) {
        *p++ = quote(args2[i]);
    }
    *p = NULL;
    ret = spawnv(_P_WAIT, exePath, (const char* const*)newArgv);
    if (errno) {
        die("spawnv failed: errno %d: %s\n", errno, strerror(errno));
    }
    exit(ret);
}