perl / Git.xson commit Git.pm: Don't #define around die (65a4e98)
   1/* By carefully stacking #includes here (even if WE don't really need them)
   2 * we strive to make the thing actually compile. Git header files aren't very
   3 * nice. Perl headers are one of the signs of the coming apocalypse. */
   4#include <ctype.h>
   5/* Ok, it hasn't been so bad so far. */
   6
   7/* libgit interface */
   8#include "../cache.h"
   9#include "../exec_cmd.h"
  10
  11/* XS and Perl interface */
  12#include "EXTERN.h"
  13#include "perl.h"
  14#include "XSUB.h"
  15
  16
  17static char *
  18report_xs(const char *prefix, const char *err, va_list params)
  19{
  20        static char buf[4096];
  21        strcpy(buf, prefix);
  22        vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
  23        return buf;
  24}
  25
  26static void NORETURN
  27die_xs(const char *err, va_list params)
  28{
  29        char *str;
  30        str = report_xs("fatal: ", err, params);
  31        croak(str);
  32}
  33
  34static void
  35error_xs(const char *err, va_list params)
  36{
  37        char *str;
  38        str = report_xs("error: ", err, params);
  39        warn(str);
  40}
  41
  42
  43MODULE = Git            PACKAGE = Git
  44
  45PROTOTYPES: DISABLE
  46
  47
  48BOOT:
  49{
  50        set_error_routine(error_xs);
  51        set_die_routine(die_xs);
  52}
  53
  54
  55# /* TODO: xs_call_gate(). See Git.pm. */
  56
  57
  58char *
  59xs_version()
  60CODE:
  61{
  62        RETVAL = GIT_VERSION;
  63}
  64OUTPUT:
  65        RETVAL
  66
  67
  68char *
  69xs_exec_path()
  70CODE:
  71{
  72        RETVAL = (char *)git_exec_path();
  73}
  74OUTPUT:
  75        RETVAL
  76
  77
  78void
  79xs__execv_git_cmd(...)
  80CODE:
  81{
  82        const char **argv;
  83        int i;
  84
  85        argv = malloc(sizeof(const char *) * (items + 1));
  86        if (!argv)
  87                croak("malloc failed");
  88        for (i = 0; i < items; i++)
  89                argv[i] = strdup(SvPV_nolen(ST(i)));
  90        argv[i] = NULL;
  91
  92        execv_git_cmd(argv);
  93
  94        for (i = 0; i < items; i++)
  95                if (argv[i])
  96                        free((char *) argv[i]);
  97        free((char **) argv);
  98}
  99
 100char *
 101xs_hash_object_pipe(type, fd)
 102        char *type;
 103        int fd;
 104CODE:
 105{
 106        unsigned char sha1[20];
 107
 108        if (index_pipe(sha1, fd, type, 0))
 109                croak("Unable to hash given filehandle");
 110        RETVAL = sha1_to_hex(sha1);
 111}
 112OUTPUT:
 113        RETVAL
 114
 115char *
 116xs_hash_object_file(type, path)
 117        char *type;
 118        char *path;
 119CODE:
 120{
 121        unsigned char sha1[20];
 122        int fd = open(path, O_RDONLY);
 123        struct stat st;
 124
 125        if (fd < 0 ||
 126            fstat(fd, &st) < 0 ||
 127            index_fd(sha1, fd, &st, 0, type))
 128                croak("Unable to hash %s", path);
 129        close(fd);
 130
 131        RETVAL = sha1_to_hex(sha1);
 132}
 133OUTPUT:
 134        RETVAL