summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ndb/mkdb.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/ndb/mkdb.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/ndb/mkdb.c')
-rwxr-xr-xsys/src/cmd/ndb/mkdb.c203
1 files changed, 203 insertions, 0 deletions
diff --git a/sys/src/cmd/ndb/mkdb.c b/sys/src/cmd/ndb/mkdb.c
new file mode 100755
index 000000000..e19685a63
--- /dev/null
+++ b/sys/src/cmd/ndb/mkdb.c
@@ -0,0 +1,203 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <ctype.h>
+
+Biobuf in;
+Biobuf out;
+
+enum
+{
+ Empty,
+ Sys,
+ Dk,
+ Ip,
+ Domain,
+};
+
+int
+iscomment(char *name)
+{
+ return *name == '#';
+}
+
+/*
+ * is this a fully specified datakit name?
+ */
+int
+isdk(char *name)
+{
+ int slash;
+
+ slash = 0;
+ for(; *name; name++){
+ if(isalnum(*name))
+ continue;
+ if(*name == '/'){
+ slash = 1;
+ continue;
+ }
+ return 0;
+ }
+ return slash;
+}
+
+/*
+ * Is this an internet domain name?
+ */
+int
+isdomain(char *name)
+{
+ int dot = 0;
+ int alpha = 0;
+
+ for(; *name; name++){
+ if(isalpha(*name) || *name == '-'){
+ alpha = 1;
+ continue;
+ }
+ if(*name == '.'){
+ dot = 1;
+ continue;
+ }
+ if(isdigit(*name))
+ continue;
+ return 0;
+ }
+ return dot && alpha;
+}
+
+/*
+ * is this an ip address?
+ */
+int
+isip(char *name)
+{
+ int dot = 0;
+
+ for(; *name; name++){
+ if(*name == '.'){
+ dot = 1;
+ continue;
+ }
+ if(isdigit(*name))
+ continue;
+ return 0;
+ }
+ return dot;
+}
+
+char tup[64][64];
+int ttype[64];
+int ntup;
+
+void
+tprint(void)
+{
+ int i, tab;
+ char *p;
+
+ tab = 0;
+ for(i = 0; i < ntup; i++){
+ if(ttype[i] == Sys){
+ Bprint(&out, "sys = %s\n", tup[i]);
+ tab = 1;
+ ttype[i] = Empty;
+ break;
+ }
+ }
+ for(i = 0; i < ntup; i++){
+ if(ttype[i] == Empty)
+ continue;
+ if(tab)
+ Bprint(&out, "\t");
+ tab = 1;
+
+ switch(ttype[i]){
+ case Domain:
+ Bprint(&out, "dom=%s\n", tup[i]);
+ break;
+ case Ip:
+ Bprint(&out, "ip=%s\n", tup[i]);
+ break;
+ case Dk:
+ p = strrchr(tup[i], '/');
+ if(p){
+ p++;
+ if((*p == 'C' || *p == 'R')
+ && strncmp(tup[i], "nj/astro/", p-tup[i]) == 0)
+ Bprint(&out, "flavor=console ");
+ }
+ Bprint(&out, "dk=%s\n", tup[i]);
+ break;
+ case Sys:
+ Bprint(&out, "sys=%s\n", tup[i]);
+ break;
+ }
+ }
+}
+
+#define NFIELDS 64
+
+/*
+ * make a database file from a merged uucp/inet database
+ */
+void
+main(void)
+{
+ int n, i, j;
+ char *l;
+ char *fields[NFIELDS];
+ int ftype[NFIELDS];
+ int same, match;
+
+ Binit(&in, 0, OREAD);
+ Binit(&out, 1, OWRITE);
+ ntup = 0;
+ while(l = Brdline(&in, '\n')){
+ l[Blinelen(&in)-1] = 0;
+ n = getfields(l, fields, NFIELDS, 1, " \t");
+ same = 0;
+ for(i = 0; i < n; i++){
+ if(iscomment(fields[i])){
+ n = i;
+ break;
+ }
+ if(isdomain(fields[i])){
+ ftype[i] = Domain;
+ for(j = 0; j < ntup; j++)
+ if(ttype[j] == Domain && strcmp(fields[i], tup[j]) == 0){
+ same = 1;
+ ftype[i] = Empty;
+ break;
+ }
+ } else if(isip(fields[i]))
+ ftype[i] = Ip;
+ else if(isdk(fields[i]))
+ ftype[i] = Dk;
+ else
+ ftype[i] = Sys;
+ }
+ if(!same && ntup){
+ tprint();
+ ntup = 0;
+ }
+ for(i = 0; i < n; i++){
+ match = 0;
+ for(j = 0; j < ntup; j++){
+ if(ftype[i] == ttype[j] && strcmp(fields[i], tup[j]) == 0){
+ match = 1;
+ break;
+ }
+ }
+ if(!match){
+ ttype[ntup] = ftype[i];
+ strcpy(tup[ntup], fields[i]);
+ ntup++;
+ }
+ }
+ }
+ if(ntup)
+ tprint();
+ exits(0);
+}