callbacks.c 952 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // callbacks.c - Sets alpm callbacks to Go functions.
  2. //
  3. // Copyright (c) 2013 The go-alpm Authors
  4. //
  5. // MIT Licensed. See LICENSE for details.
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdarg.h>
  9. #include <alpm.h>
  10. void logCallback(alpm_loglevel_t level, char *cstring);
  11. void questionCallback(alpm_question_t *question);
  12. void go_alpm_log_cb(alpm_loglevel_t level, const char *fmt, va_list arg) {
  13. char *s = malloc(128);
  14. if (s == NULL) return;
  15. int16_t length = vsnprintf(s, 128, fmt, arg);
  16. if (length > 128) {
  17. length = (length + 16) & ~0xf;
  18. s = realloc(s, length);
  19. }
  20. if (s != NULL) {
  21. logCallback(level, s);
  22. free(s);
  23. }
  24. }
  25. void go_alpm_question_cb(alpm_question_t *question) {
  26. questionCallback(question);
  27. }
  28. void go_alpm_set_logging(alpm_handle_t *handle) {
  29. alpm_option_set_logcb(handle, go_alpm_log_cb);
  30. }
  31. void go_alpm_set_question(alpm_handle_t *handle) {
  32. alpm_option_set_questioncb(handle, go_alpm_question_cb);
  33. }