ref: f31bebf7e813616b83b255d665bf1283abef5250
author: sirjofri <sirjofri@sirjofri.de>
date: Sat Aug 30 16:54:36 EDT 2025
adds first functionality
--- /dev/null
+++ b/dat.h
@@ -1,0 +1,40 @@
+typedef struct Tuple Tuple;
+typedef struct Block Block;
+typedef struct Sys Sys;
+typedef struct Ipnet Ipnet;
+typedef struct Pos Pos;
+
+extern char *netdir;
+extern char *systuples[];
+extern int nsystuples;
+
+extern int pagewidth;
+extern int pageheight;
+
+struct Pos {+ int x;
+ int y;
+};
+
+struct Tuple {+ char *key;
+ char *value;
+ int ipnet; /* set by ipnet */
+};
+
+struct Block {+ Tuple *tuples;
+ int ntuples;
+
+ Pos p;
+ int width;
+ int height;
+};
+
+struct Sys {+ Block;
+};
+
+struct Ipnet {+ Block;
+};
--- /dev/null
+++ b/draw.c
@@ -1,0 +1,112 @@
+#include <u.h>
+#include <libc.h>
+#include "dat.h"
+#include "fns.h"
+
+static int lineheight = 10;
+static int charwidth = 7;
+
+static int boxoffsetx = 2;
+static int boxoffsety = 2;
+
+void
+dheader()
+{+ print(
+ "%%!PS\n"
+ "%%%%Creator: netgraph\n"
+ "%%%%Origin: 0 0\n"
+ "%%%%BoundingBox: 0 0 %d %d\n"
+ "%%%%Pages: 1\n"
+ "/Courier 10 selectfont\n",
+ pagewidth, pageheight
+ );
+}
+
+void
+dfooter()
+{+ print("showpage\n%%%%EOF\n");+}
+
+static void
+drawtext(Pos p, int off, char *fmt, ...)
+{+ va_list arg;
+
+ print("%d %d moveto\n(", p.x, p.y - (off * lineheight));+ va_start(arg, fmt);
+ vfprint(1, fmt, arg);
+ va_end(arg);
+ print(") show\n");+}
+
+static void
+drawbox(Block *b)
+{+ Pos p1, p2;
+
+ p1 = b->p;
+ p1.y = pageheight - p1.y;
+ p2 = p1;
+ p1.x -= boxoffsetx;
+ p1.y += boxoffsety;
+
+ p2.x += boxoffsetx + b->width;
+ p2.y -= boxoffsety + b->height;
+
+ print("newpath\n"+ "%d %d moveto\n"
+ "%d %d lineto\n"
+ "%d %d lineto\n"
+ "%d %d lineto\n"
+ "%d %d lineto\n"
+ "1 setlinewidth\n"
+ "stroke\n",
+ p1.x, p1.y,
+ p2.x, p1.y,
+ p2.x, p2.y,
+ p1.x, p2.y,
+ p1.x, p1.y);
+}
+
+static void
+drawtuple(Tuple *t, void *aux)
+{+ Pos *p = aux;
+
+ drawtext(*p, 1, "%s: %s", t->key, t->value);
+ p->y -= lineheight;
+}
+
+void
+dblock(Block *b)
+{+ Pos np;
+ Pos p = b->p;
+ p.y = pageheight - p.y;
+
+ np = p;
+ fortuple(b, drawtuple, &np);
+ drawbox(b);
+}
+
+static void
+tuplesize(Tuple *t, void *aux)
+{+ Block *b = aux;
+ int w;
+
+ w = strlen(t->key) + strlen(t->value) + 3;
+ if (w > b->width)
+ b->width = w;
+}
+
+void
+setblocksize(Block *b)
+{+ b->width = 0;
+ b->height = lineheight * b->ntuples;
+ fortuple(b, tuplesize, b);
+ b->width *= charwidth;
+}
--- /dev/null
+++ b/fns.h
@@ -1,0 +1,23 @@
+/* draw functions */
+void dheader(void);
+void dfooter(void);
+
+void dblock(Block*);
+void setblocksize(Block*);
+
+/* tuple functions */
+Tuple *findtuple(Block*, char*);
+void addtuple(Block*, char*, char*, int);
+void fortuple(Block*, void (*f)(Tuple*,void*), void*);
+
+/* sys functions */
+int issysarg(char*);
+int gensys(char*,char*);
+Sys *findsys(char*,char*);
+void forsys(void (*f)(Sys*,void*), void*);
+int numsystems(void);
+
+/* ipnet functions */
+int isnetarg(char*);
+int genipnet(char*,char*);
+void fornet(void (*f)(Ipnet*,void*), void*);
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,11 @@
+</$objtype/mkfile
+
+TARG=netgraph
+OFILES=\
+ netgraph.$O\
+ draw.$O\
+ sys.$O\
+ ipnet.$O\
+ tuple.$O\
+
+</sys/src/cmd/mkone
--- /dev/null
+++ b/netgraph.c
@@ -1,0 +1,127 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <ndb.h>
+#include "dat.h"
+#include "fns.h"
+
+static void
+usage(void)
+{+ fprint(2, "usage: %s [-s systuples]\n", argv0);
+ exits("usage");+}
+
+#define MAXSYSTP 24
+
+char *netdir = nil;
+
+char *systuples[MAXSYSTP];
+int nsystuples = 0;
+
+/* A3 format: 842 x 1191 */
+int pagewidth = 1191;
+int pageheight = 842;
+
+static void
+procrequest(char *key, char *val)
+{+ if (issysarg(key)) {+ gensys(key, val);
+ return;
+ }
+}
+
+static void
+procinput(char *s)
+{+ char *c;
+
+ c = strchr(s, '=');
+ if (!(c && c[0] && c[1]))
+ return;
+
+ c[0] = 0;
+ c++;
+
+ procrequest(s, c);
+}
+
+void
+dumptuples(Tuple *t, void*)
+{+ fprint(2, " %s=%s\n", t->key, t->value);
+}
+
+void
+dumpsys(Sys *s, void*)
+{+ fprint(2, "sys: %d %d\n", s->p.x, s->p.y);
+ fortuple(s, dumptuples, nil);
+}
+
+void
+calcsyslayout(Sys *s, void *aux)
+{+ int n = numsystems();
+ int *ip = aux;
+ int i = *ip;
+ float step = 2*PI / n;
+
+ setblocksize(s);
+
+ float pagex = pagewidth/2.;
+ float pagey = pageheight/2.;
+ float spread = 0.5;
+
+ s->p.x = cos(step * i) * pagex * spread + pagex - s->width/2;
+ s->p.y = sin(step * i) * pagey * spread + pagey - s->height/2;
+
+ (*ip)++;
+}
+
+void
+drawsystems(Sys *s, void*)
+{+ dblock(s);
+}
+
+void
+main(int argc, char **argv)
+{+ Biobuf *bin;
+ char *stp = "sys,ether,ip,dom,auth,authdom";
+ char *s;
+ int i;
+
+ ARGBEGIN{+ case 'h':
+ usage();
+ case 's':
+ stp = EARGF(usage());
+ break;
+ case 'x':
+ netdir = EARGF(usage());
+ break;
+ }ARGEND;
+
+ nsystuples = getfields(stp, systuples, MAXSYSTP, 1, ",");
+
+ bin = Bfdopen(0, OREAD);
+ if (!bin)
+ sysfatal("%r");+
+ while (s = Brdstr(bin, '\n', 1)) {+ procinput(s);
+ free(s);
+ }
+
+ Bterm(bin);
+
+
+ dheader();
+ i = 0;
+ forsys(calcsyslayout, &i);
+ forsys(drawsystems, nil);
+ dfooter();
+}
--- /dev/null
+++ b/sys.c
@@ -1,0 +1,114 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <ndb.h>
+#include "dat.h"
+#include "fns.h"
+
+static char *sysargs[] = {+ "sys", "ip", "dom",
+};
+
+#define MAXSYS 100
+static Sys systems[MAXSYS];
+static int nsystems = 0;
+
+int
+numsystems()
+{+ return nsystems;
+}
+
+static Sys*
+gensystem(void)
+{+ Sys *s;
+ s = &systems[nsystems];
+ nsystems++;
+ memset(s, 0, sizeof(Sys));
+ return s;
+}
+
+Sys*
+findsys(char *key, char *value)
+{+ Sys *s;
+ Tuple *t;
+ for (int i = 0; i < nsystems; i++) {+ s = &systems[i];
+ t = findtuple(s, key);
+ if (t && strcmp(t->value, value) == 0)
+ return s;
+ }
+ return nil;
+}
+
+void
+forsys(void (*f)(Sys*,void*), void *aux)
+{+ for (int i = 0; i < nsystems; i++)
+ f(&systems[i], aux);
+}
+
+int
+issysarg(char *key)
+{+ int n = sizeof(sysargs) / sizeof(*sysargs);
+ for (int i = 0; i < n; i++) {+ if (strcmp(sysargs[i], key) == 0)
+ return 1;
+ }
+ return 0;
+}
+
+static void
+fillsysvalue(Sys *s, char *key, char *ids, char *idv)
+{+ char *val;
+ Ndbtuple *t;
+
+ val = csgetvalue(netdir, ids, idv, key, nil);
+ if (val) {+ addtuple(s, key, val, 0);
+ return;
+ }
+ t = csipinfo(netdir, ids, idv, &key, 1);
+ if (t) {+ if (t->val) val = strdup(t->val);
+ ndbfree(t);
+ }
+ if (!val)
+ return;
+
+ addtuple(s, key, val, 1);
+
+ if (gensys("sys", val))+ return;
+ if (gensys("ip", val))+ return;
+}
+
+int
+gensys(char *key, char *value)
+{+ char *valid;
+ Sys *sys;
+
+ valid = csgetvalue(netdir, key, value, key, nil);
+ if (!valid)
+ return 0;
+ sys = findsys(key, value);
+ if (sys)
+ return 1;
+ sys = gensystem();
+
+ for (int i = 0; i < nsystuples; i++) {+ if (strcmp(systuples[i], key) == 0) {+ addtuple(sys, systuples[i], strdup(value), 0);
+ continue;
+ }
+ fillsysvalue(sys, systuples[i], key, value);
+ }
+
+ return 1;
+}
--- /dev/null
+++ b/tuple.c
@@ -1,0 +1,41 @@
+#include <u.h>
+#include <libc.h>
+#include "dat.h"
+#include "fns.h"
+
+Tuple*
+findtuple(Block *b, char *key)
+{+ Tuple *t;
+ for (int i = 0; i < b->ntuples; i++) {+ t = &b->tuples[i];
+ if (t->key && strcmp(t->key, key) == 0)
+ return t;
+ }
+ return nil;
+}
+
+void
+addtuple(Block *b, char *key, char *value, int ipnet)
+{+ Tuple *t;
+
+ if (!b->tuples) {+ b->ntuples = 1;
+ b->tuples = mallocz(sizeof(Tuple) * nsystuples, 1);
+ t = b->tuples;
+ } else {+ t = &b->tuples[b->ntuples];
+ b->ntuples++;
+ }
+ t->key = key;
+ t->value = value;
+ t->ipnet = ipnet;
+}
+
+void
+fortuple(Block *b, void (*f)(Tuple*,void*), void *aux)
+{+ for (int i = 0; i < b->ntuples; i++)
+ f(&b->tuples[i], aux);
+}
--
⑨