summaryrefslogtreecommitdiff
path: root/src/app/main.c
blob: ce43b6179157062b77ad1b5db77f6a50fae5fe1d (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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

#include "mcp/server.h"
#include "storage/store.h"

static void
usage(FILE *stream)
{
  fwprintf(stream, L"Usage: tiny-convmem-mcp --data-dir <path>\n");
}

int
wmain(int argc, wchar_t **argv)
{
  wchar_t *absolute = NULL;
  DWORD required;
  tiny_store_t *store = NULL;
  tiny_store_status_t status;
  int result;
  if (argc == 2 && wcscmp(argv[1], L"--help") == 0)
    {
      usage(stdout);
      return 0;
    }
  if (argc != 3 || wcscmp(argv[1], L"--data-dir") != 0 || argv[2][0] == L'\0')
    {
      usage(stderr);
      return 2;
    }
  required = GetFullPathNameW(argv[2], 0, NULL, NULL);
  if (required == 0 || (absolute = malloc((size_t)required * sizeof(wchar_t))) == NULL
      || GetFullPathNameW(argv[2], required, absolute, NULL) == 0)
    {
      fprintf(stderr, "unable to resolve --data-dir\n");
      free(absolute);
      return 2;
    }
  status = tiny_store_open(&store, absolute);
  free(absolute);
  if (status != TINY_STORE_OK)
    {
      fprintf(stderr, "unable to open conversation store: %s\n", tiny_store_status_name(status));
      return status == TINY_STORE_BUSY ? 3 : 1;
    }
  result = tiny_convmem_server_run(stdin, stdout, store);
  tiny_store_close(store);
  if (result != 0)
    {
      fprintf(stderr, "tiny-convmem-mcp stopped with error code %d\n", result);
    }
  return result;
}