all repos — kuro @ 6fa747864efcb84863d09a9a937315e748a9f040

multiwindow text editor thing for plan9 with simple client VM and 9p interface

kuro.c (raw)

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
#include "dat.h"
#include "fns.h"

static char* mtpt;

int threadmaybackground(void) {
  return 1;
}

void threadmain(int argc, char **argv)
{
  /* we want to strip any path elements off the executable name */
  char exe_name[256] = {0};
  char* exe_base;

  uvlong node_id = 0;
  int client_fd = -1;
  int newwin = 0;

  char client_9p_str[256] = {0};

  strcpy(exe_name, argv[0]);
  exe_base = utfrrune(exe_name, '/');
  if (exe_base) {
    exe_base++;
  } else {
    exe_base = exe_name;
  }

  /* if we are the server, serve the filetree on 9p */
  
  if (strequ(exe_base, "kurosrvr")) {
    if (argc > 1) {
      start_9p(argv[1]);
    } else {
      sysfatal("usage: kurosrvr mtpt");
    }
  } else if (strequ(exe_base, "kuro")) {
    mtpt = getenv("KURO_MTPT");
    if (mtpt == nil) {
      sysfatal("KURO_MTPT not set");
    }

    kuro9p_set_mtpt(mtpt);
    print("KURO_MTPT=%s\n", mtpt);

    ARGBEGIN{
      case 'n':
        newwin=1;
        break;
      case 'i':
	    sscanf(ARGF(), "%d", &node_id);
        break;
      case 'p':
        sscanf(ARGF(), "%d", &client_fd);
        break;
    }ARGEND

    strcpy(client_9p_str, "new");
    if (argv[0]) {
      strcat(client_9p_str, " ");
      strcat(client_9p_str, argv[0]);
    }

	if (node_id > 0 && client_fd >= 0) {
      supervise_node(create_node(node_id, client_fd, newwin, argv[0]));
    } else {
      /* if node_id or client_id are missing, ask the server to fork us with them, and forward the filename */
      print("9pstr=%s\n", client_9p_str);
      kuro9p_write("/ctl", client_9p_str, strlen(client_9p_str));
    }
  } else {
    sysfatal("invoke as kurosrvr to start the background service or kuro to start an application window");
  }
}