ES-2024 reverter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1002B

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "context.h"
  4. #include "filedata.h"
  5. #include "list.h"
  6. #include "logging.h"
  7. static LIST_HEAD(context_list);
  8. struct context * create_context(const char *filename)
  9. {
  10. struct context *ctx = malloc(sizeof(struct context));
  11. if (ctx == NULL) {
  12. log_print(LOG_WARN, "create_context(): out of memory");
  13. return NULL;
  14. }
  15. memset(ctx, 0, sizeof(struct context));
  16. ctx->lbuf = create_linebuffer(256);
  17. if (ctx->lbuf == NULL) {
  18. free(ctx);
  19. return NULL;
  20. }
  21. ctx->file = get_filedata(filename);
  22. if (ctx->file == NULL) {
  23. free(ctx->lbuf);
  24. free(ctx);
  25. return NULL;
  26. }
  27. list_add(&ctx->list, &context_list);
  28. return ctx;
  29. }
  30. int destroy_context(struct context *ctx)
  31. {
  32. list_del(&ctx->list);
  33. if (ctx->dev_close != NULL)
  34. ctx->dev_close(ctx);
  35. free(ctx->file);
  36. free(ctx->lbuf);
  37. free(ctx);
  38. return 0;
  39. }
  40. int context_close(void)
  41. {
  42. struct context *ctx, *tmp;
  43. list_for_each_entry_safe(ctx, tmp, &context_list, list)
  44. destroy_context(ctx);
  45. return 0;
  46. }