BSD 4_1c_2 development
authorCSRG <csrg@ucbvax.Berkeley.EDU>
Thu, 2 Dec 1982 04:49:53 +0000 (20:49 -0800)
committerCSRG <csrg@ucbvax.Berkeley.EDU>
Thu, 2 Dec 1982 04:49:53 +0000 (20:49 -0800)
Work on file usr/src/usr.bin/refer/deliv1.c
Work on file usr/src/usr.bin/refer/deliv2.c
Work on file usr/src/usr.bin/refer/glue1.c
Work on file usr/src/usr.bin/refer/glue4.c
Work on file usr/src/usr.bin/refer/glue3.c
Work on file usr/src/usr.bin/refer/glue2.c
Work on file usr/src/usr.bin/refer/glue5.c
Work on file usr/src/usr.bin/refer/hunt2.c
Work on file usr/src/usr.bin/refer/hunt1.c
Work on file usr/src/usr.bin/refer/hunt5.c
Work on file usr/src/usr.bin/refer/hunt3.c
Work on file usr/src/usr.bin/refer/hunt6.c
Work on file usr/src/usr.bin/refer/hunt7.c
Work on file usr/src/usr.bin/refer/hunt9.c
Work on file usr/src/usr.bin/refer/hunt8.c
Work on file usr/src/usr.bin/refer/inv1.c
Work on file usr/src/usr.bin/refer/inv2.c
Work on file usr/src/usr.bin/refer/inv3.c
Work on file usr/src/usr.bin/refer/inv6.c
Work on file usr/src/usr.bin/refer/inv5.c
Work on file usr/src/usr.bin/refer/makefile
Work on file usr/src/usr.bin/refer/mkey1.c
Work on file usr/src/usr.bin/refer/mkey2.c
Work on file usr/src/usr.bin/refer/mkey3.c
Work on file usr/src/usr.bin/refer/refer..c
Work on file usr/src/usr.bin/refer/refer0.c
Work on file usr/src/usr.bin/refer/refer1.c
Work on file usr/src/usr.bin/refer/refer2.c
Work on file usr/src/usr.bin/refer/refer3.c
Work on file usr/src/usr.bin/refer/refer4.c
Work on file usr/src/usr.bin/refer/refer5.c
Work on file usr/src/usr.bin/refer/refer6.c
Work on file usr/src/usr.bin/refer/refer7.c
Work on file usr/src/usr.bin/refer/refer8.c
Work on file usr/src/usr.bin/refer/shell.c
Work on file usr/src/usr.bin/refer/tick.c
Work on file usr/src/usr.bin/refer/signal.h

Synthesized-from: CSRG/cd1/4.1c.2

37 files changed:
usr/src/usr.bin/refer/deliv1.c [new file with mode: 0644]
usr/src/usr.bin/refer/deliv2.c [new file with mode: 0644]
usr/src/usr.bin/refer/glue1.c [new file with mode: 0644]
usr/src/usr.bin/refer/glue2.c [new file with mode: 0644]
usr/src/usr.bin/refer/glue3.c [new file with mode: 0644]
usr/src/usr.bin/refer/glue4.c [new file with mode: 0644]
usr/src/usr.bin/refer/glue5.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt1.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt2.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt3.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt5.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt6.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt7.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt8.c [new file with mode: 0644]
usr/src/usr.bin/refer/hunt9.c [new file with mode: 0644]
usr/src/usr.bin/refer/inv1.c [new file with mode: 0644]
usr/src/usr.bin/refer/inv2.c [new file with mode: 0644]
usr/src/usr.bin/refer/inv3.c [new file with mode: 0644]
usr/src/usr.bin/refer/inv5.c [new file with mode: 0644]
usr/src/usr.bin/refer/inv6.c [new file with mode: 0644]
usr/src/usr.bin/refer/makefile [new file with mode: 0644]
usr/src/usr.bin/refer/mkey1.c [new file with mode: 0644]
usr/src/usr.bin/refer/mkey2.c [new file with mode: 0644]
usr/src/usr.bin/refer/mkey3.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer..c [new file with mode: 0644]
usr/src/usr.bin/refer/refer0.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer1.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer2.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer3.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer4.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer5.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer6.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer7.c [new file with mode: 0644]
usr/src/usr.bin/refer/refer8.c [new file with mode: 0644]
usr/src/usr.bin/refer/shell.c [new file with mode: 0644]
usr/src/usr.bin/refer/signal.h [new file with mode: 0644]
usr/src/usr.bin/refer/tick.c [new file with mode: 0644]

diff --git a/usr/src/usr.bin/refer/deliv1.c b/usr/src/usr.bin/refer/deliv1.c
new file mode 100644 (file)
index 0000000..ae4a57b
--- /dev/null
@@ -0,0 +1,45 @@
+# include "stdio.h"
+main(argc,argv)
+       char *argv[];
+{
+/* goes from file:begin,l to actual characters */
+char line[750], *p, name[100];
+FILE *fa NULL;
+long lp;
+int len;
+
+if (argc>1 && argv[1] && argv[1][0])
+       chdir (argv[1]);
+name[0]="";
+while (gets(line))
+       {
+       if (line[0]=='$' && line[1]=='$') 
+               {
+               chdir(line+2);
+               continue;
+               }
+       for(p=line; *p!= ':'; p++)
+               ;
+       *p++ = 0;
+       sscanf(p, "%ld,%d", &lp, &len);
+       if (p==line)
+               fa = stdin;
+       else
+       if (strcmp (name, line) != 0)
+               {
+               if (fa != NULL)
+                       fclose(fa);
+               fa = fopen(line, "r");
+               if (fa == NULL)
+                       err("Can't open %s", line);
+               strcpy(name, line);
+               }
+       if (fa != NULL)
+               {
+               fseek (fa, lp, 0);
+               fread (line, 1, len, fa);
+               line[len] = 0;
+               fputs(line, stdout);
+               }
+       }
+}
diff --git a/usr/src/usr.bin/refer/deliv2.c b/usr/src/usr.bin/refer/deliv2.c
new file mode 100644 (file)
index 0000000..2405991
--- /dev/null
@@ -0,0 +1,47 @@
+# include "stdio.h"
+hash (s)
+       char *s;
+{
+int c, n;
+for(n=0; c= *s; s++)
+       n += (c*n+ c << (n%4));
+return(n>0 ? n : -n);
+}
+err (s, a)
+       char *s;
+{
+fprintf(stderr, "Error: ");
+fprintf(stderr, s, a);
+putc('\n', stderr);
+exit(1);
+}
+prefix(t, s)
+       char *t, *s;
+{
+int c, d;
+while ( (c= *t++) == *s++)
+       if (c==0) return(1);
+return(c==0 ? 1: 0);
+}
+char *
+mindex(s, c)
+       char *s;
+{
+register char *p;
+for( p=s; *p; p++)
+       if (*p ==c)
+               return(p);
+return(0);
+}
+zalloc(m,n)
+{
+       int t;
+# if D1
+fprintf(stderr, "calling calloc for %d*%d bytes\n",m,n);
+# endif
+t = calloc(m,n);
+# if D1
+fprintf(stderr, "calloc returned %o\n", t);
+# endif
+return(t);
+}
diff --git a/usr/src/usr.bin/refer/glue1.c b/usr/src/usr.bin/refer/glue1.c
new file mode 100644 (file)
index 0000000..3e5f1ca
--- /dev/null
@@ -0,0 +1,260 @@
+# include "stdio.h"
+# define unopen(fil) {if (fil!=NULL) {fclose(fil); fil=NULL;}}
+extern char refdir[];
+int lmaster = 1000;
+int reached = 0;
+FILE *fd  = 0;
+long *zalloc();
+char *todir();
+FILE *iopen();
+int *hfreq, hfrflg;
+int colevel = 0;
+static union {unsigned *a; long *b;} master; /* initializes master to NULL */
+int iflong;
+extern char *fgnames[], **fgnamp;
+int prfreqs = 0;
+int typeindex  = 0;
+char usedir[100];
+static int full = 1000;
+static int tags  = 0;
+char *sinput, *soutput, *tagout;
+long indexdate  = 0, gdate();
+int soutlen = 1000;
+int taglen  = 1000;
+
+huntmain(argc,argv)
+       char *argv[];
+{
+/* read query from stdin, expect name of indexes in argv[1] */
+static FILE *fa, *fb, *fc;
+char indexname[100], *qitem[100], *rprog = 0;
+char grepquery[200];
+static char oldname[30] ;
+static int nhash  = 0;
+static int maxhash  = 0;
+int falseflg = 0, nitem, nfound, frtbl;
+static long *hpt  = 0;
+# if D1
+fprintf(stderr, "in glue1 argc %d argv %o %o\n", argc, argv[0],argv[1]);
+# endif
+savedir();
+while (argv[1][0] == '-')
+       {
+# if D1
+fprintf(stderr, "argv.1 is %s\n",argv[1]);
+# endif
+       switch(argv[1][1])
+               {
+               case 'a': /* all output, incl. false drops */
+                       falseflg = 1; break;
+               case 'r':
+                       argc--; argv++;
+                       rprog = argv[1];
+                       break;
+               case 'F': /* put out full text */
+                       full = setfrom(argv[1][2]);
+                       break;
+               case 'T': /* put out tags */
+                       tags = setfrom(argv[1][2]);
+                       break;
+               case 'i': /* input in argument string */
+                       argc--; argv++;
+                       sinput = argv[1];
+                       break;
+               case 's': /*text output to string */
+               case 'o':
+                       argc--; argv++;
+                       soutput = argv[1];
+                       if (argv[2]<16000)
+                               {
+                               soutlen = (int) argv[2];
+                               argc--; argv++;
+                               }
+                       break;
+               case 't': /*tag output to string */
+                       argc--; argv++;
+                       tagout = argv[1];
+                       if (argv[2]<16000)
+                               {
+                               taglen = (int)argv[2];
+                               argc--; argv++;
+                               }
+                       break;
+               case 'l': /* specify length of lists */
+                       argc--; argv++;
+                       lmaster = atoi(argv[1]);
+# if D1
+                       fprintf(stderr, "lmaster now %d\n",lmaster);
+# endif
+                       break;
+               case 'C': 
+                       argc--; argv++;
+                       colevel = atoi(argv[1]);
+                       break;
+               }
+       argc--; argv++;
+       }
+strcpy (indexname, todir(argv[1]));
+# if D1
+fprintf(stderr, "in huntmain indexname %s typeindex %d\n", indexname, typeindex);
+# endif
+if (typeindex == 0 || strcmp (oldname, indexname) !=0)
+       {
+       strcpy (oldname, indexname);
+       unopen(fa); unopen(fb); unopen(fc);
+
+       if (ckexist(indexname, ".ib"))
+               {
+# if D1
+               fprintf(stderr, "found old index\n");
+# endif
+               fa = iopen(indexname, ".ia");
+               fb = iopen(indexname, ".ib");
+               fc = iopen(indexname, ".ic");
+               typeindex =1;
+# if D1
+               fprintf(stderr, "opened f's as %o %o %o\n",fa,fb,fc);
+# endif
+               indexdate = gdate(fb);
+               fread (&nhash, sizeof(nhash), 1, fa);
+               fread (&iflong, sizeof(iflong), 1, fa);
+               if (nhash > maxhash)
+                       {
+                       if (hpt)
+                               free (hpt, maxhash, sizeof(*hpt));
+                       hpt=0;
+                       if (hfreq)
+                               free(hfreq, maxhash, sizeof(*hfreq));
+                       hfreq=0;
+                       maxhash=nhash;
+# if D1
+                       fprintf(stderr, "Freed if needed maxhash %d\n",maxhash);
+# endif
+                       }
+               if (hpt==0)
+               hpt = zalloc(nhash, sizeof(*hpt));
+# if D1
+               fprintf(stderr, "hpt now %o\n",hpt);
+# endif
+               if (hpt == NULL)
+                       err ("No space for hash list (%d)", nhash);
+               fread( hpt, sizeof(*hpt), nhash, fa);
+               if (hfreq==0)
+               hfreq=zalloc(nhash, sizeof(*hfreq));
+               if (hfreq==NULL)
+                       err ("No space for hash frequencies (%d)", nhash);
+               frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa);
+               hfrflg = (frtbl == nhash);
+# if D1
+               fprintf(stderr,"Read pointer files\n");
+# endif
+               if (master.a == NULL)
+                       if (iflong)
+                               master.b = zalloc(lmaster, sizeof(long));
+                       else
+                               master.a = zalloc(lmaster, sizeof(int));
+               if (master.a == NULL)
+                       err ("no space for answer list",0);
+               }
+       else
+       if (makefgrep(indexname))
+               typeindex=2;
+       else
+               {
+               err ("No files %s\n",indexname);
+               exit(1);
+               }
+       }
+
+# if D1
+fprintf(stderr, "typeindex now %d\n",typeindex);
+# endif
+tagout[0]=0;
+if (typeindex==2)
+       {
+       grepcall(sinput, tagout, indexname);
+# if D1
+       fprintf(stderr, " back from grepcall\n");
+# endif
+       restodir();
+       return;
+       }
+nitem = getq(qitem);
+# if D1
+fprintf(stderr, "approaching doquery fb %o\n", fb);
+# endif
+nfound = doquery(hpt, nhash, fb, nitem, qitem, master);
+# ifdef D1
+       fprintf(stderr, "return from doquery with nfound %d\n", nfound);
+# endif
+if (falseflg == 0)
+       nfound = baddrop(master, nfound, fc, nitem, qitem, rprog, full);
+# ifdef D1
+       fprintf(stderr, "after baddrop with nfound %d\n",nfound);
+       fprintf(stderr, "tagout is /%s/, sout /%s/\n",tagout, soutput);
+# endif
+if (tags)
+       result (master, nfound >tags ? tags : nfound, fc);
+# if D1
+fprintf(stderr, "done with huntmain\n");
+fprintf(stderr, "tagout is /%s/\n", tagout);
+fprintf(stderr, "string out is /%s/\n", soutput);
+# endif
+if (fgnamp>fgnames)
+       {
+       char **fgp;
+       int k;
+# if D1
+       fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
+# endif
+       grepquery[0]=0;
+       for(k=0; k<nitem; k++)
+               {
+               strcat(grepquery, " ");
+               strcat(grepquery, qitem[k]);
+               }
+       for(fgp=fgnames; fgp<fgnamp; fgp++)
+               {
+# if D1
+               fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery);
+# endif
+               makefgrep(*fgp);
+               grepcall(grepquery, tagout, *fgp);
+# if D1
+               fprintf(stderr, "tagout now /%s/\n", tagout);
+# endif
+               }
+       }
+restodir();
+}
+
+char *
+todir(t)
+       char *t;
+{
+       char *s;
+       s=t;
+       while (*s) s++;
+       while (s>=t && *s != '/') s--;
+       if (s<t) return(t);
+*s++ = 0;
+t = (*t ? t : "/");
+chdir (t);
+strcpy (usedir,t);
+return(s);
+}
+setfrom(c)
+{
+switch(c)
+       {
+       case 'y': case '\0':
+       default:
+               return(1000);
+       case '1':
+       case '2': case '3': case '4': case '5':
+       case '6': case '7': case '8': case '9':
+               return(c-'0');
+       case 'n': case '0':
+               return(0);
+       }
+}
diff --git a/usr/src/usr.bin/refer/glue2.c b/usr/src/usr.bin/refer/glue2.c
new file mode 100644 (file)
index 0000000..0225278
--- /dev/null
@@ -0,0 +1,11 @@
+ char refdir[50];
+savedir()
+{
+if (refdir[0]==0)
+       corout ("", refdir, "/bin/pwd", "", 50);
+trimnl(refdir);
+}
+restodir()
+{
+chdir(refdir);
+}
diff --git a/usr/src/usr.bin/refer/glue3.c b/usr/src/usr.bin/refer/glue3.c
new file mode 100644 (file)
index 0000000..d98a334
--- /dev/null
@@ -0,0 +1,69 @@
+# include "refer..c"
+corout(in, out, rprog, arg, outlen)
+       char *in, *out, *rprog;
+{
+# define move(x, y) close(y); dup(x); close(x);
+int pipev[2], fr1, fr2, fw1, fw2, n;
+
+if (strcmp (rprog, "hunt") ==0)
+       return(callhunt(in, out, arg, outlen));
+if (strcmp (rprog, "deliv")==0)
+       return(dodeliv(in, out, arg, outlen));
+pipe (pipev); fr1= pipev[0]; fw1 = pipev[1];
+pipe (pipev); fr2= pipev[0]; fw2 = pipev[1];
+if (fork()==0)
+       {
+       close (fw1); close (fr2);
+       move (fr1, 0);
+       move (fw2, 1);
+       if (rprog[0]!= '/')
+               chdir("/usr/lib/refer");
+       execl(rprog, "deliv", arg, 0);
+       err ("Can't run %s", rprog);
+       }
+close(fw2); close(fr1);
+write (fw1, in , strlen(in));
+close(fw1);
+wait(0);
+n = read (fr2, out, outlen);
+out[n]=0;
+close(fr2);
+}
+callhunt(in, out, arg, outlen)
+       char *in, *out, *arg;
+{
+# define ALEN 50
+char *argv[20], abuff[ALEN];
+extern int typeindex;
+int argc;
+extern char one[];
+extern int onelen;
+argv[0] = "hunt";
+argv[1] = "-i";
+argv[2] = in;
+argv[3] = "-t";
+argv[4] = out;
+argv[5] = (char *)outlen;
+argv[6] = "-T";
+argv[7] = "-F1";
+argv[8] = "-o";
+argv[9] = one;
+argv[10] = (char *)onelen;
+argv[11] = abuff; strcpy (abuff,arg);
+if (strlen(abuff) > ALEN)
+       err("abuff not big enough %d", strlen(abuff));
+argc = 6;
+huntmain (argc,argv);
+return(0);
+}
+dodeliv(in, out, arg, outlen)
+       char *in, *out, *arg;
+{
+# if D1
+fprintf(stderr, "in dodeliv, arg /%s/\n", arg?arg:"");
+# endif
+if (arg && arg[0])
+       chdir(arg);
+findline(in, out, outlen, 0L);
+restodir();
+}
diff --git a/usr/src/usr.bin/refer/glue4.c b/usr/src/usr.bin/refer/glue4.c
new file mode 100644 (file)
index 0000000..3a38c64
--- /dev/null
@@ -0,0 +1,96 @@
+# include "stdio.h"
+# include "ctype.h"
+extern char gfile[];
+
+grepcall (in, out, arg)
+       char *in, *out, *arg;
+{
+char line[200], *s, argig[100], *cv[50];
+char *inp, inb[500];
+FILE *qf, *gf;
+int c, oldc = 0, alph = 0, nv = 0;
+int sv0, sv1;
+strcpy (argig, arg); strcat(argig, ".ig");
+strcpy (inp=inb, in);
+if (gfile[0]==0)
+       sprintf(gfile, "/tmp/rj%dg", getpid());
+# if D1
+fprintf(stderr, "in grepcall, gfile %s in %o out %o\n", gfile,in,out);
+# endif
+for(cv[nv++] = "fgrep"; c = *inp; inp++)
+       {
+       if (c== ' ')
+               c = *inp = 0;
+       else if (isupper(c))
+               *inp = tolower(c);
+       alph = (c==0) ? 0 : alph+1;
+       if (alph == 1)
+               cv[nv++] = inp;
+       if (alph > 6)
+               *inp = 0;
+       oldc=c;
+       }
+# if D1
+fprintf(stderr, "%d args set up\n", nv);
+# endif
+       {
+       sv0 = dup(0);
+       close(0);
+       if (open (argig, 0) != 0)
+               err("Can't read fgrep index %s", argig);
+       sv1 = dup(1);
+       close(1);
+       if (creat(gfile, 0666) != 1)
+               err("Can't write fgrep output %s", gfile);
+       fgrep(nv, cv);
+# if D1
+fprintf(stderr, "fgrep returned, output is..\n");
+# endif
+       close (0); dup(sv0); close(sv0);
+       close (1); dup(sv1); close(sv1);
+       }
+
+# if D1
+fprintf(stderr, "back from fgrep\n");
+# endif
+gf = fopen(gfile, "r");
+if (gf==NULL)
+       err("can't read fgrep output %s", gfile);
+while (fgets(line, 100, gf) == line)
+       {
+       line[100]=0;
+# if D1
+fprintf(stderr, "read line as //%s//\n",line);
+# endif
+       for(s=line; *s && (*s != '\t'); s++);
+       if (*s == '\t')
+               {
+               *s++ = '\n';
+               *s++ = 0;
+               }
+       if (line[0])
+               strcat(out, line);
+# if D1
+fprintf(stderr, "out now /%s/\n",out);
+# endif
+       while (*s) s++;
+# if D1
+fprintf(stderr, "line %o s %o s-1 %o\n",line,s,s[-1]);
+# endif
+       if (s[-1]!= '\n')
+               while (!feof(gf) && getc(gf)!= '\n') ;
+       }
+fclose(gf);
+# if D1
+       fprintf(stderr, "back from reading %, out %s\n",out);
+# else
+unlink (gfile);
+# endif
+return(0);
+}
+
+clfgrep()
+{
+if (gfile[0])
+       unlink(gfile);
+}
diff --git a/usr/src/usr.bin/refer/glue5.c b/usr/src/usr.bin/refer/glue5.c
new file mode 100644 (file)
index 0000000..7e37508
--- /dev/null
@@ -0,0 +1,364 @@
+# include "stdio.h"
+# include "ctype.h"
+/*
+ * fgrep -- print all lines containing any of a set of keywords
+ *
+ *     status returns:
+ *             0 - ok, and some matches
+ *             1 - ok, but no matches
+ *             2 - some error
+ */
+#define        MAXSIZ 700
+#define QSIZE 400
+struct words {
+       char    inp;
+       char    out;
+       struct  words *nst;
+       struct  words *link;
+       struct  words *fail;
+} 
+*www, *smax, *q;
+
+struct words *zalloc();
+char   buf[1024];
+int    nsucc;
+int    need;
+char   *instr;
+int    inct;
+int    rflag;
+int    xargc;
+char   **xargv;
+int    numwords;
+int    nfound;
+static int flag  = 0;
+
+
+fgrep(argc, argv)
+char **argv;
+{
+       nsucc = need = inct = rflag = numwords = nfound = 0;
+       instr = 0;
+       flag = 0;
+       if (www==0)
+               www = zalloc(MAXSIZ, sizeof (*www));
+       if (www==NULL)
+               err("Can't get space for machines", 0);
+       for (q=www; q<www+MAXSIZ; q++)
+               {
+               q->inp = q->out = 0;  q->nst = q->link = q->fail =0;
+               }
+       xargc = argc-1;
+       xargv = argv+1;
+       while (xargc>0 && xargv[0][0]=='-')
+               {
+               switch(xargv[0][1])
+                       {
+                       case 'r': /* return value only */
+                               rflag++;
+                               break;
+                       case 'n': /* number of answers needed */
+                               need = (int) xargv[1];
+                               xargv++; xargc--;
+                               break;
+                       case 'i':
+                               instr = xargv[1];
+                               inct = (int) xargv[2]+2;
+# if D2
+fprintf(stderr,"inct %d xargv.2. %o %d\n",inct, xargv[2],xargv[2]);
+# endif
+                               xargv += 2; xargc -= 2;
+                               break;
+                       }
+               xargv++; xargc--;
+               }
+       if (xargc<=0)
+               {
+               write (2, "bad fgrep call\n", 15);
+               exit(2);
+               }
+# if D1
+       fprintf(stderr, "before cgoto\n");
+# endif
+       cgotofn();
+# if D1
+       fprintf(stderr, "before cfail\n");
+# endif
+       cfail();
+# if D1
+       fprintf(stderr, "before execute instr %.20s\n", instr? instr: "");
+       fprintf(stderr, "end of string %d %c %c %c\n", inct, instr[inct-3],
+               instr[inct-2], instr[inct-1]);
+# endif
+       execute();
+# if D1
+       fprintf(stderr, "returning nsucc %d\n", nsucc);
+       fprintf(stderr, "fgrep done www %o\n",www);
+# endif
+       return(nsucc == 0);
+}
+
+execute()
+{
+       register char *p;
+       register struct words * c;
+       register ch;
+       register ccount;
+       int f;
+       char *nlp;
+       f=0;
+       ccount = instr ? inct : 0;
+       nfound=0;
+       p = instr ? instr : buf;
+       if (need == 0) need = numwords;
+       nlp = p;
+       c = www;
+# if D2
+fprintf(stderr, "in execute ccount %d inct %d\n",ccount, inct );
+# endif
+       for (;;) {
+# if D3
+fprintf(stderr, "down ccount\n");
+# endif
+               if (--ccount <= 0) {
+# if D2
+fprintf(stderr, "ex loop ccount %d instr %o\n",ccount, instr);
+# endif
+                       if (instr) break;
+                       if (p == &buf[1024]) p = buf;
+                       if (p > &buf[512]) {
+                               if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
+                       }
+                       else if ((ccount = read(f, p, 512)) <= 0) break;
+# if D2
+fprintf(stderr, " normal read %d bytres\n", ccount);
+{char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
+fprintf(stderr, xx, p);
+}
+# endif
+               }
+nstate:
+               ch = *p;
+# if D2
+fprintf(stderr, "roaming along in ex ch %c c %o\n",ch,c);
+# endif
+               if (isupper(ch)) ch |= 040;
+               if (c->inp == ch) {
+                       c = c->nst;
+               }
+               else if (c->link != 0) {
+                       c = c->link;
+                       goto nstate;
+               }
+               else {
+                       c = c->fail;
+                       if (c==0) {
+                               c = www;
+istate:
+                               if (c->inp == ch) {
+                                       c = c->nst;
+                               }
+                               else if (c->link != 0) {
+                                       c = c->link;
+                                       goto istate;
+                               }
+                       }
+                       else goto nstate;
+               }
+               if (c->out && new (c)) {
+# if D2
+fprintf(stderr, " found: nfound %d need %d\n",nfound,need);
+# endif
+                       if (++nfound >= need)
+                       {
+# if D1
+fprintf(stderr, "found, p %o nlp %o ccount %d buf %o buf[1024] %o\n",p,nlp,ccount,buf,buf+1024);
+# endif
+                               if (instr==0)
+                               while (*p++ != '\n') {
+# if D3
+fprintf(stderr, "down ccount2\n");
+# endif
+                                       if (--ccount <= 0) {
+                                               if (p == &buf[1024]) p = buf;
+                                               if (p > &buf[512]) {
+                                                       if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
+                                               }
+                                               else if ((ccount = read(f, p, 512)) <= 0) break;
+# if D2
+fprintf(stderr, " read %d bytes\n",ccount);
+{ char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
+fprintf(stderr, xx, p);
+}
+# endif
+                                       }
+                               }
+                               nsucc = 1;
+                               if (rflag==0)
+                                       {
+# if D2
+fprintf(stderr, "p %o nlp %o buf %o\n",p,nlp,buf);
+if (p>nlp)
+{write (2, "XX\n", 3); write (2, nlp, p-nlp); write (2, "XX\n", 3);}
+# endif
+                                       if (p > nlp) write(1, nlp, p-nlp);
+                                       else {
+                                               write(1, nlp, &buf[1024] - nlp);
+                                               write(1, buf, p-&buf[0]);
+                                               }
+                                       if (p[-1]!= '\n') write (1, "\n", 1);
+                                       }
+                               if (instr==0)
+                                       {
+                                       nlp = p;
+                                       c = www;
+                                       nfound=0; 
+                                       }
+                       }
+                       else
+                               ccount++;
+                       continue;
+               }
+# if D2
+fprintf(stderr, "nr end loop p %o\n",p);
+# endif
+               if (instr)
+                       p++;
+               else
+               if (*p++ == '\n')
+               {
+                       nlp = p;
+                       c = www;
+                       nfound=0;
+               }
+       }
+       if (instr==0)
+               close(f);
+}
+
+cgotofn() {
+       register c;
+       register struct words * s;
+       s = smax = www;
+nword: 
+       for(;;) {
+# if D1
+       fprintf(stderr, " in for loop c now %o %c\n",c, c>' ' ? c : ' ');
+# endif
+               if ((c = gch())==0) return;
+               else if (c == '\n') {
+                       s->out = 1;
+                       s = www;
+               }
+               else {
+loop:  
+                       if (s->inp == c) {
+                               s = s->nst;
+                               continue;
+                       }
+                       if (s->inp == 0) goto enter;
+                       if (s->link == 0) {
+                               if (smax >= &www[MAXSIZ - 1]) overflo();
+                               s->link = ++smax;
+                               s = smax;
+                               goto enter;
+                       }
+                       s = s->link;
+                       goto loop;
+               }
+       }
+
+enter:
+       do {
+               s->inp = c;
+               if (smax >= &www[MAXSIZ - 1]) overflo();
+               s->nst = ++smax;
+               s = smax;
+       } 
+       while ((c = gch()) != '\n');
+       smax->out = 1;
+       s = www;
+       numwords++;
+       goto nword;
+
+}
+
+gch()
+{
+       static char *s;
+       if (flag==0)
+       {
+               flag=1;
+               s = *xargv++;
+# if D1
+       fprintf(stderr, "next arg is %s xargc %d\n",s,xargc);
+# endif
+               if (xargc-- <=0) return(0);
+       }
+       if (*s) return(*s++);
+       for(flag=0; flag<1024; flag++)
+               buf[flag]=0;
+       flag=0;
+       return('\n');
+}
+
+overflo() {
+       write(2,"wordlist too large\n", 19);
+       exit(2);
+}
+cfail() {
+       struct words *queue[QSIZE];
+       struct words **front, **rear;
+       struct words *state;
+       register char c;
+       register struct words * s;
+       s = www;
+       front = rear = queue;
+init:  
+       if ((s->inp) != 0) {
+               *rear++ = s->nst;
+               if (rear >= &queue[QSIZE - 1]) overflo();
+       }
+       if ((s = s->link) != 0) {
+               goto init;
+       }
+
+       while (rear!=front) {
+               s = *front;
+               if (front == &queue[QSIZE-1])
+                       front = queue;
+               else front++;
+cloop: 
+               if ((c = s->inp) != 0) {
+                       *rear = (q = s->nst);
+                       if (front < rear)
+                               if (rear >= &queue[QSIZE-1])
+                                       if (front == queue) overflo();
+                                       else rear = queue;
+                       else rear++;
+                       else
+                               if (++rear == front) overflo();
+                       state = s->fail;
+floop: 
+                       if (state == 0) state = www;
+                       if (state->inp == c) {
+                               q->fail = state->nst;
+                               if ((state->nst)->out == 1) q->out = 1;
+                               continue;
+                       }
+                       else if ((state = state->link) != 0)
+                               goto floop;
+               }
+               if ((s = s->link) != 0)
+                       goto cloop;
+       }
+}
+static int seen[50];
+new (x)
+{
+       int i;
+       for(i=0; i<nfound; i++)
+               if (seen[i]==x)
+                       return(0);
+       seen[i]=x;
+       return(1);
+}
diff --git a/usr/src/usr.bin/refer/hunt1.c b/usr/src/usr.bin/refer/hunt1.c
new file mode 100644 (file)
index 0000000..5f0c75e
--- /dev/null
@@ -0,0 +1,238 @@
+# include "stdio.h"
+# include "assert.h"
+extern char refdir[];
+extern int keepold;
+extern char *fgnames[];
+extern char **fgnamp;
+FILE *fd =NULL;
+int lmaster =500;
+int *hfreq, hfrflg;
+int colevel =0;
+int measure=0;
+int soutlen =1000;
+int reached =0;
+int iflong =0;
+int prfreqs =0;
+char usedir[100];
+char * calloc(); char * todir();
+char gfile[50];
+static int full =1000;
+static int tags =0;
+char *sinput, *soutput, *tagout;
+long indexdate =0, gdate();
+
+main(argc,argv)
+       char *argv[];
+{
+/* read query from stdin, expect name of indexes in argv[1] */
+static FILE *fa, *fb, *fc;
+char nma[100], nmb[100], nmc[100], *qitem[100], *rprog = NULL;
+char nmd[100], grepquery[256];
+static char oldname[30] ;
+static int was =0;
+/* these pointers are unions of pointer to int and pointer to long */
+long *hpt;
+unsigned *master =0;
+int falseflg, nhash, nitem, nfound, frtbl, kk;
+
+       /* special wart for refpart: default is tags only */
+
+while (argv[1][0] == '-')
+       {
+       switch(argv[1][1])
+               {
+               case 'a': /* all output, incl. false drops */
+                       falseflg = 1; break;
+               case 'r':
+                       argc--; argv++;
+                       rprog = argv[1];
+                       break;
+               case 'F': /* put out full text */
+                       full = setfrom(argv[1][2]);
+                       break;
+               case 'T': /* put out tags */
+                       tags = setfrom(argv[1][2]);
+                       break;
+               case 'i': /* input in argument string */
+                       argc--; argv++;
+                       sinput = argv[1];
+                       break;
+               case 's': /*text output to string */
+               case 'o':
+                       argc--; argv++;
+                       soutput = argv[1];
+                       if (argv[2]<16000)
+                               {
+                               soutlen = (int)argv[2];
+                               argc--; argv++;
+                               }
+                       break;
+               case 't': /*tag output to string */
+                       argc--; argv++;
+                       tagout = argv[1];
+                       break;
+               case 'l': /* length of internal lists */
+                       argc--; argv++;
+                       lmaster = atoi(argv[1]);
+                       break;
+               case 'g': /* suppress fgrep search on old files */
+                       keepold = 0;
+                       break;
+               case 'C': /* coordination level */
+                       colevel = atoi(argv[1]+2);
+# if D1
+fprintf(stderr, "colevel set to %d\n",colevel);
+# endif
+                       break;
+               case 'P': /* print term freqs */
+                       prfreqs=1; break;
+               case 'm':
+                       measure=1; break;
+               }
+       argc--; argv++;
+       }
+strcpy (nma, todir(argv[1]));
+if (was == 0 || strcmp (oldname, nma) !=0)
+       {
+       strcpy (oldname,nma);
+       strcpy (nmb, nma); strcpy (nmc, nmb); strcpy(nmd,nma);
+       strcat (nma, ".ia");
+       strcat (nmb, ".ib");
+       strcat (nmc, ".ic");
+       strcat (nmd, ".id");
+       if (was)
+               {fclose(fa); fclose(fb); fclose(fc);}
+
+       fa = fopen(nma, "r");
+       if (fa==NULL)
+               {
+               strcpy(*fgnamp++ = calloc(strlen(oldname)+2,1), oldname);
+               fb=NULL;
+               goto search;
+               }
+       fb = fopen(nmb, "r");
+       fc = fopen(nmc, "r");
+       was =1;
+       if (fb== NULL || fc ==NULL)
+               {
+               err("Index incomplete %s", nmb);
+               exit(1);
+               }
+       indexdate = gdate(fb);
+       fd = fopen(nmd, "r");
+       }
+fseek (fa, 0L, 0);
+fread (&nhash, sizeof(nhash), 1, fa);
+fread (&iflong, sizeof(iflong), 1, fa);
+if(master==0)
+master = calloc (lmaster, iflong? 4: 2);
+hpt = calloc(nhash, sizeof(*hpt));
+kk=fread( hpt, sizeof(*hpt), nhash, fa);
+# if D1
+fprintf(stderr,"read %d hashes, iflong %d, nhash %d\n", kk, iflong, nhash);
+# endif
+_assert (kk==nhash);
+hfreq = calloc(nhash, sizeof(*hfreq));
+_assert (hfreq != NULL);
+frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa);
+hfrflg = (frtbl == nhash);
+# if D1
+fprintf(stderr, "read freqs %d\n", frtbl);
+# endif
+
+search:
+while (1)
+       {
+       nitem = getq(qitem);
+       if (measure) tick();
+       if (nitem==0) continue;
+       if (nitem < 0) break;
+       if (tagout) tagout[0]=0;
+       if (fb!=NULL)
+       {
+       nfound = doquery(hpt, nhash, fb, nitem, qitem, master);
+# if D1
+       fprintf(stderr,"after doquery nfound %d\n", nfound);
+# endif
+       fgnamp=fgnames;
+       if (falseflg == 0)
+               nfound = baddrop(master, nfound, fc, nitem, qitem, rprog, full);
+# if D1
+       fprintf(stderr,"after baddrop nfound %d\n", nfound);
+# endif
+       }
+       if (fgnamp>fgnames)
+               {
+               char **fgp, tgbuff[100];
+               int k;
+# if D1
+               fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
+# endif
+               grepquery[0]=0;
+               for(k=0; k<nitem; k++)
+                       {
+                       strcat(grepquery, " ");
+                       strcat(grepquery, qitem[k]);
+                       }
+# if D1
+               fprintf(stderr, "grepquery %s\n",grepquery);
+# endif
+               for(fgp=fgnames; fgp<fgnamp; fgp++)
+                       {
+# if D1
+                       fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery);
+# endif
+                       makefgrep(*fgp);
+# if D1
+                       fprintf(stderr, "grepmade\n");
+# endif
+                       if (tagout==0)
+                               tagout=tgbuff;
+                       grepcall(grepquery, tagout, *fgp);
+# if D1
+                       fprintf(stderr, "tagout now /%s/\n", tagout);
+# endif
+                       if (full)
+                               {
+                               char bout[1000];
+                               findline(tagout, bout, 1000);
+                               fputs(bout,stdout);
+                               }
+                       }
+               }
+       if (tags)
+               result (master, nfound >tags ? tags: nfound, fc);
+       if (measure) tock();
+       }
+}
+
+char *
+todir(t)
+       char *t;
+{
+       char *s;
+       s=t;
+       while (*s) s++;
+       while (s>=t && *s != '/') s--;
+       if (s<t) return(t);
+*s++ = 0;
+t = (*t ? t : "/");
+chdir (t);
+strcpy (usedir,t);
+return(s);
+}
+setfrom(c)
+{
+switch(c)
+       {
+       case 'y': case '\0':
+       default:
+               return(1000);
+       case '1':
+       case '2': case '3': case '4': case '5':
+       case '6': case '7': case '8': case '9':
+               return(c-'0');
+       case 'n': case '0':
+               return(0);
+       }
+}
diff --git a/usr/src/usr.bin/refer/hunt2.c b/usr/src/usr.bin/refer/hunt2.c
new file mode 100644 (file)
index 0000000..8c88415
--- /dev/null
@@ -0,0 +1,276 @@
+# include "refer..c"
+static int *coord = 0;
+int *zalloc();
+int hh[50]; extern int *hfreq, hfrflg, hcomp(), hexch();
+extern int prfreqs;
+
+doquery(hpt, nhash, fb, nitem, qitem, master)
+       union ptr {unsigned *a; long *b;} master;
+       long *hpt;
+       FILE *fb;
+       char *qitem[];
+{
+long k;
+union ptr prevdrop;
+int nf = 0, best = 0, nterm = 0, i, g, j;
+int * prevcoord;
+long lp;
+extern int lmaster, colevel, reached;
+long getl(); unsigned getw(); extern int iflong;
+
+# if D1
+fprintf(stderr, "entering doquery nitem %d\n",nitem);
+fprintf(stderr, "first few hashes are %ld %ld %ld %ld %ld\n", hpt[0],hpt[1],hpt[2],hpt[3],hpt[4]);
+fprintf(stderr, "and frequencies are  %d %d %d %d %d\n",hfreq[0],hfreq[1],hfreq[2],hfreq[3],hfreq[4]);
+# endif
+_assert (lmaster>0);
+if (coord==0)
+       coord = zalloc(lmaster, sizeof(lmaster));
+if (colevel>0)
+       {
+       prevdrop.a=zalloc(lmaster,iflong?4:2);
+       prevcoord = zalloc(lmaster, sizeof(lmaster));
+       }
+else
+       {
+       prevdrop.a=master.a;
+       prevcoord=coord;
+       }
+# if D1
+fprintf(stderr, "nitem %d\n",nitem);
+# endif
+for(i=0; i<nitem; i++)
+       {
+       hh[i] = hash(qitem[i])%nhash;
+# if D1
+       fprintf(stderr,"query wd X%sX has hash %d\n", qitem[i], hh[i]);
+# endif
+       }
+# if D1
+fprintf(stderr, "past that loop nhash %d hpt is %lo\n", nhash, hpt);
+# endif
+if (prfreqs)
+       for(i=0; i<nitem; i++)
+               fprintf(stderr,"item %s hash %d hfreq %d\n",qitem[i], hh[i], hfreq[hh[i]]);
+/* if possible, sort query into decreasing frequency of hashes */
+if (hfrflg)
+       shell (nitem, hcomp, hexch);
+# if D1
+for(i=0; i<nitem; i++)
+ fprintf(stderr, "item hash %d frq %d\n", hh[i], hfreq[hh[i]]);
+# endif
+       lp = hpt [hh[0]];
+# if D1
+fprintf(stderr,"first item hash %d lp %ld 0%lo\n", hh[0],lp,lp);
+# endif
+_assert (fb!=NULL);
+_assert (fseek(fb,lp,0)==NULL);
+for(i=0; i<lmaster; i++)
+       {
+       if (iflong)
+               master.b[i] = getl(fb);
+       else
+               master.a[i] = getw(fb);
+       coord[i]=1;
+# if D2
+       if (iflong)
+       fprintf(stderr,"master has %ld\n",(master.b[i]));
+       else
+       fprintf(stderr,"master has %d\n",(master.a[i]));
+# endif
+       _assert (i<lmaster);
+       if (iflong)
+               {
+               if (master.b[i] == -1L) break;
+               }
+       else
+               {
+               if (master.a[i] == -1) break;
+               }
+       }
+nf= i;
+for(nterm=1; nterm<nitem; nterm++)
+       {
+# ifdef D1
+       fprintf(stderr, "item %d, hash %d\n", nterm, hh[nterm]);
+# endif
+       if (colevel>0)
+               {
+               for(j=0; j<nf; j++)
+                       {
+                       if (iflong)
+                       prevdrop.b[j] = master.b[j];
+                       else
+                       prevdrop.a[j] = master.a[j];
+                       prevcoord[j] = coord[j];
+                       }
+               }
+       lp = hpt[hh[nterm]];
+       _assert (fseek(fb, lp, 0)==0);
+# if D1
+       fprintf(stderr,"item %d hash %d seek to %ld\n",nterm,hh[nterm],lp);
+# endif
+       g=j=0;
+       while (1)
+               {
+               if (iflong)
+                       k = getl(fb);
+               else
+                       k = getw(fb);
+               if (k== -1) break;
+# if D2
+               fprintf(stderr,"next term finds %ld\n",k);
+# endif
+# if D3
+               if (iflong)
+               fprintf(stderr, "bfwh j %d nf %d master %ld k %ld\n",j,nf,prevdrop.b[j],(long)(k));
+               else
+               fprintf(stderr, "bfwh j %d nf %d master %ld k %ld\n",j,nf,prevdrop.a[j],(long)(k));
+# endif
+               while (j<nf && (iflong?prevdrop.b[j]:prevdrop.a[j])<k)
+                       {
+# if D3
+                       if (iflong)
+                       fprintf(stderr, "j %d nf %d prevdrop %ld prevcoord %d colevel %d nterm %d k %ld\n",
+                       j,nf,prevdrop.b[j], prevcoord[j], colevel, nterm, (long)(k));
+                       else
+                       fprintf(stderr, "j %d nf %d prevdrop %ld prevcoord %d colevel %d nterm %d k %ld\n",
+                       j,nf,prevdrop.a[j], prevcoord[j], colevel, nterm, (long)(k));
+# endif
+                       if (prevcoord[j] + colevel <= nterm)
+                               j++;
+                       else
+                               {
+                               _assert (g<lmaster);
+                               if (iflong)
+                               master.b[g] = prevdrop.b[j];
+                               else
+                               master.a[g] = prevdrop.a[j];
+                               coord[g++] = prevcoord[j++];
+# if D1
+if (iflong)
+fprintf(stderr, " not skip g %d doc %d coord %d note %d\n",g,master.b[g-1], coord[g-1],master.b[j-1]);
+else
+fprintf(stderr, " not skip g %d doc %ld coord %d nterm %d\n",g,master.a[g-1], coord[g-1],nterm);
+# endif
+                               continue;
+                               }
+                       }
+               if (colevel==0 && j>=nf) break;
+               if (j<nf && (iflong? prevdrop.b[j]: prevdrop.a[j]) == k)
+                       {
+                       if (iflong)
+                       master.b[g]=k;
+                       else
+                       master.a[g]=k;
+                       coord[g++] = prevcoord[j++]+1;
+# if D1
+if (iflong)
+                       fprintf(stderr, " at g %d item %ld coord %d note %ld\n",g,master.b[g-1],coord[g-1],master.b[j-1]);
+else
+                       fprintf(stderr, " at g %d item %d coord %d note %d\n",g,master.a[g-1],coord[g-1],master.a[j-1]);
+# endif
+                       }
+               else
+               if (colevel >= nterm)
+                       {
+                       if (iflong)
+                       master.b[g]=k;
+                       else
+                       master.a[g]=k;
+                       coord[g++] = 1;
+                       }
+               }
+# if D1
+fprintf(stderr,"now have %d items\n",g);
+# endif
+       if (colevel>0)
+       for ( ; j<nf; j++)
+               if ((iflong ? ((long *)prevcoord)[j] :
+                       ((unsigned *)prevcoord)[j])+colevel > nterm)
+                       {
+                       _assert(g<lmaster);
+                       if (iflong)
+                       master.b[g] = prevdrop.b[j];
+                       else
+                       master.a[g] = prevdrop.a[j];
+                       coord[g++] = prevcoord[j];
+# if D3
+if(iflong)
+fprintf(stderr, "copied over %ld coord %d\n",master.b[g-1], coord[g-1]);
+else
+fprintf(stderr, "copied over %d coord %d\n",master.a[g-1], coord[g-1]);
+# endif
+                       }
+       nf = g;
+       }
+if (colevel>0)
+       {
+       best=0;
+       for(j=0; j<nf; j++)
+               if (coord[j]>best) best = coord[j];
+# if D1
+       fprintf(stderr, "colevel %d best %d\n", colevel, best);
+# endif
+       reached = best;
+       for(g=j=0; j<nf; j++)
+               if (coord[j]==best)
+                       {
+                       if (iflong)
+                       master.b[g++] = master.b[j];
+                       else
+                       master.a[g++] = master.a[j];
+                       }
+       nf=g;
+# if D1
+       fprintf(stderr, "yet got %d\n",nf);
+# endif
+       }
+# ifdef D1
+       fprintf(stderr, " returning with %d\n",nf);
+# endif
+if (colevel)
+       {
+       free(prevdrop.a, lmaster, iflong?4:2);
+       free(prevcoord, lmaster, sizeof (lmaster));
+       }
+# if D3
+for(g=0;g<nf;g++)
+if(iflong)
+fprintf(stderr,":%ld\n",master.b[g]);
+else
+fprintf(stderr,":%d\n",master.a[g]);
+# endif
+return(nf);
+}
+long
+getl(fb)
+       FILE *fb;
+{
+int x[2];
+long *lp;
+x[0] = getw(fb);
+x[1] = getw(fb);
+lp= x;
+return(*lp);
+}
+putl(ll, f)
+       long ll;
+       FILE *f;
+{
+int *x;
+x = &ll;
+putw(x[0], f);
+putw(x[1], f);
+}
+hcomp( n1, n2)
+{
+return (hfreq[hh[n1]]<=hfreq[hh[n2]]);
+}
+hexch( n1, n2 )
+{
+int t;
+t = hh[n1];
+hh[n1] = hh[n2];
+hh[n2] = t;
+}
diff --git a/usr/src/usr.bin/refer/hunt3.c b/usr/src/usr.bin/refer/hunt3.c
new file mode 100644 (file)
index 0000000..839714c
--- /dev/null
@@ -0,0 +1,59 @@
+# include "refer..c"
+getq(v)
+       char *v[];
+{
+# define BSIZ 250
+static char buff[BSIZ];
+static int eof = 0;
+extern char *sinput;
+char *p;
+int c, n = 0, las = 0;
+if (eof) return(-1);
+p = buff;
+while ( (c = (sinput ? *sinput++ : getchar()) ) > 0)
+       {
+       if (c== '\n')
+               break;
+       if (isalpha(c) || isdigit(c))
+               {
+               if (las==0)
+                       {
+                       v[n++] = p;
+                       las=1;
+                       }
+               if (las++ <= 6)
+                       *p++ = c;
+               }
+       else
+               {
+               if (las>0)
+                       *p++ = 0;
+               las=0;
+               }
+       }
+*p=0;
+assert(p<buff+BSIZ);
+if (sinput==0 && c<= 0) eof=1;
+n = keycomp(v,n);
+# if D1
+fprintf(stderr, "no. keys %d\n",n);
+for(c=0; c<n; c++)
+ fprintf(stderr, "keys X%sX\n", v[c]);
+# endif
+return(n);
+}
+keycomp(v,n) /* compress keys */
+       char *v[];
+{
+int i, j, k;
+for(i=j=0; i<n; i++)
+       {
+       for(k=0; k<j; k++)
+               if (strcmp(v[i], v[k])==0)
+                       break;
+       if (k<j) /* found it */
+               continue;
+       v[j++] = v[i];
+       }
+return(j);
+}
diff --git a/usr/src/usr.bin/refer/hunt5.c b/usr/src/usr.bin/refer/hunt5.c
new file mode 100644 (file)
index 0000000..19557a5
--- /dev/null
@@ -0,0 +1,50 @@
+# include "stdio.h"
+extern char *soutput, *tagout, usedir[];
+
+result(master, nf, fc)
+       union ptr {unsigned *a; long *b;} *master;
+       FILE *fc;
+{
+int i, c;
+char *s;
+long lp;
+extern int iflong;
+char res[100];
+
+for(i=0; i<nf; i++)
+       {
+       lp = iflong ? master.b[i] : master.a[i];
+       fseek(fc,lp, 0);
+       fgets(res, 100, fc);
+       for(s=res; c = *s; s++)
+               if (c== ';')
+                       {
+                       *s=0;
+                       break;
+                       }
+       if (tagout !=0)
+               {
+               if (res[0]=='/' || usedir[0]==0)
+                       sprintf(tagout, "%s", res);
+               else
+               sprintf(tagout, "%s/%s", usedir, res);
+               while (*tagout) tagout++;
+               }
+       else
+               {
+               if (res[0]!='/' || usedir[0]==0)
+                       printf("%s/", usedir);
+               printf("%s\n", res);
+               }
+       }
+}
+
+# include "sys/types.h"
+# include "sys/stat.h"
+long gdate(f)
+       FILE *f;
+{
+struct stat sb;
+fstat (f->_file, &sb);
+return  (sb . st_mtime);
+}
diff --git a/usr/src/usr.bin/refer/hunt6.c b/usr/src/usr.bin/refer/hunt6.c
new file mode 100644 (file)
index 0000000..51c5a5d
--- /dev/null
@@ -0,0 +1,110 @@
+# include "stdio.h"
+# include "assert.h"
+# define TXTLEN 1000
+char *outbuf= 0;
+extern char *soutput;
+extern int soutlen, iflong;
+extern long indexdate;
+baddrop(master, nf, fc, nitem, qitem, rprog, full)
+       union ptr {unsigned *a; long *b; } master;
+       FILE *fc;
+       char *qitem[], *rprog;
+{
+/* checks list of drops for real bad drops;
+   uses "deliv" to find items. */
+int i, g, j, need, got, na, len;
+long lp;
+char res[100], *ar[50], output[TXTLEN];
+extern int colevel, reached;
+# if D1
+if (iflong)
+fprintf(stderr,"in baddrop, nf %d master %ld %ld %ld\n", nf, master.b[0], master.b[1], master.b[2]);
+else
+fprintf(stderr,"in baddrop, nf %d master %d %d %d\n", nf, master.a[0], master.a[1], master.a[2]);
+# endif
+for (i=g=0; i<nf; i++)
+       {
+       lp = iflong ? master.b[i] : master.a[i];
+# if D1
+       if (iflong)
+       fprintf(stderr, "i %d master %lo lp %lo\n", i, master.b[i], lp);
+       else
+       fprintf(stderr, "i %d master %o lp %lo\n", i, master.a[i], lp);
+# endif
+       fseek (fc, lp, 0);
+       fgets( res, 100, fc);
+# if D1
+       fprintf(stderr, "tag %s", res);
+# endif
+       if (!auxil(res,output))
+               {
+               char *s; int c;
+# if D1
+               fprintf(stderr, "not auxil try rprog %c\n", rprog? 'y': 'n');
+# endif
+               for(s=res; c= *s; s++)
+                       if (c == ';' || c == '\n')
+                               {*s=0; break;}
+               len = rprog ?
+                       corout (res, output, rprog, 0, TXTLEN) :
+                       findline (res, output, TXTLEN, indexdate);
+               }
+# if D1
+       _assert (len <TXTLEN);
+       fprintf(stderr,"item %d of %d, tag %s len %d output\n%s\n..\n",i,nf,res, len,output);
+# endif
+       if (len==0)
+               continue;
+       need = colevel ? reached : nitem;
+       na=0;
+       ar[na++] = "fgrep";
+       ar[na++] = "-r";
+       ar[na++] = "-n";
+       ar[na++] = (char *) need;
+       ar[na++] = "-i";
+       ar[na++] = output;
+       ar[na++] = (char *) len;
+       for(j=0; j<nitem; j++)
+               ar[na++] = qitem[j];
+# ifdef D1
+       fprintf(stderr, "calling fgrep len %d ar[4] %s %o %d \n",len,ar[4],ar[5],ar[6]);
+# endif
+       if (fgrep(na, ar)==0)
+               {
+# ifdef D1
+       fprintf(stderr, "fgrep found it\n");
+# endif
+               if (iflong)
+                       master.b[g++] = master.b[i];
+               else
+                       master.a[g++] = master.a[i];
+               if (full >= g)
+                       if (soutput==0)
+                               fputs(output, stdout);
+                       else
+                               strcpy (soutput, output);
+               }
+# ifdef D1
+       fprintf(stderr, "after fgrep\n");
+# endif
+       }
+return(g);
+}
+auxil( res, output)
+       char *res, *output;
+{
+extern FILE *fd;
+long lp, c; int len;
+if (fd==0)return(0);
+while (c = *res++) 
+       {
+       if (c == ';')
+               {
+               sscanf(res, "%ld,%d", &lp, &len);
+               fseek (fd, lp, 0);
+               fgets(output, len, fd);
+               return(1);
+               }
+       }
+return(0);
+}
diff --git a/usr/src/usr.bin/refer/hunt7.c b/usr/src/usr.bin/refer/hunt7.c
new file mode 100644 (file)
index 0000000..0fb8b47
--- /dev/null
@@ -0,0 +1,108 @@
+# include "stdio.h"
+# define SAME 0
+# include "assert.h"
+# define FGCT 10
+# define FGSIZE 150
+char *calloc();
+int keepold = 1; /* keep old things for fgrep search */
+char fgspace[FGSIZE];
+char *fgp = fgspace;
+char *fgnames[FGCT];
+char **fgnamp = fgnames;
+findline(argin, out, outlen, indexdate)
+       long indexdate;
+       char *argin, *out;
+{
+static char name[100] ="";
+char *p, **ftp, *in;
+extern long gdate();
+static FILE *fa= NULL;
+long lp, llen;
+int len, k, nofil;
+
+# if D1
+       fprintf(stderr, "findline: %s\n", argin);
+# endif
+       in = calloc(strlen(argin)+1,1);
+       assert(in!=0);
+       strcpy(in, argin);
+       if (mindex(in, '!'))
+               return(remote(in, out));
+       nofil = in[0]==0;
+       for(p=in; *p && *p != ':' && *p != ';'; p++)
+               ;
+       if (*p) *p++=0;
+       else p=in;
+       k = sscanf(p, "%ld,%ld", &lp, &llen);
+# ifdef D1
+       fprintf(stderr, "p %s k %d lp %ld llen %ld\n",p,k,lp,llen);
+# endif
+       if (k<2)
+               {
+               lp = 0;
+               llen=outlen;
+               }
+# ifdef D1
+       fprintf(stderr, "lp %ld llen %ld\n",lp, llen);
+# endif
+# ifdef D1
+fprintf(stderr, "fa now %o, p %o in %o %s\n",fa, p,in,in);
+# endif
+       if (nofil)
+{
+# if D1
+fprintf(stderr, "set fa to stdin\n");
+# endif
+               fa = stdin;
+}
+       else
+       if (strcmp (name, in) != 0 || 1)
+               {
+# if D1
+fprintf(stderr, "old: %s new %s not equal\n",name,in);
+# endif
+               if (fa != NULL)
+                       fa = freopen(in, "r", fa);
+               else
+                       fa = fopen(in, "r");
+# if D1
+               if (fa==NULL) fprintf(stderr, "failed to (re)open *%s*\n",in);
+# endif
+               if (fa == NULL)
+                       return(0);
+                       /* err("Can't open %s", in); */
+               strcpy(name, in);
+               if (gdate(fa) > indexdate && indexdate != 0)
+                       {
+                       if (keepold)
+                               {
+                               for(ftp=fgnames; ftp<fgnamp; ftp++)
+                                       if (strcmp(*ftp, name)==SAME)
+                                               return(0);
+                               strcpy (*fgnamp++ = fgp, name);
+                               assert(fgnamp<fgnames+FGCT);
+                               while (*fgp && *fgp!=':')
+                                       fgp++;
+                               *fgp++ = 0;
+                               assert (fgp<fgspace+FGSIZE);
+                               return(0);
+                               }
+                       fprintf(stderr, "Warning: index predates file '%s'\n", name);
+                       }
+               }
+# if D1
+       else
+               fprintf(stderr, "old %s new %s same fa %o\n", name,in,fa);
+# endif
+       if (fa != NULL)
+               {
+               fseek (fa, lp, 0);
+               len = (llen >= outlen) ? outlen-1 : llen;
+               len = fread (out, 1, len, fa);
+               out[len] = 0;
+# ifdef D1
+       fprintf(stderr, "length as read is %d\n",len);
+# endif
+               }
+       return(len);
+}
diff --git a/usr/src/usr.bin/refer/hunt8.c b/usr/src/usr.bin/refer/hunt8.c
new file mode 100644 (file)
index 0000000..434c6d8
--- /dev/null
@@ -0,0 +1,74 @@
+# include "stdio.h"
+# include "assert.h"
+# define unopen(fil) {if (fil!=NULL) {fclose(fil); fil=NULL;}}
+extern long indexdate, gdate();
+FILE *iopen();
+runbib (s)
+       char *s;
+{
+/* make a file suitable for fgrep */
+char tmp[200];
+sprintf(tmp, "/usr/lib/refer/mkey %s >%s.ig", s,s);
+system(tmp);
+}
+makefgrep(indexname)
+       char *indexname;
+{
+       FILE *fa =NULL, *fb =NULL;
+       if (ckexist(indexname, ".ig"))
+               {
+               /* existing gfrep -type index */
+# if D1
+               fprintf(stderr, "found fgrep\n");
+# endif
+               fa = iopen(indexname, ".ig");
+               fb = iopen(indexname, "");
+               if (gdate(fb)>gdate(fa))
+                       {
+                       if (fa!=NULL)
+                               fclose(fa);
+                       runbib(indexname);
+                       fa= iopen(indexname, ".ig");
+                       }
+               indexdate = gdate(fa);
+               unopen(fa); unopen(fb);
+               }
+       else
+       if (ckexist(indexname, ""))
+               {
+               /* make fgrep */
+# if D1
+                       fprintf(stderr, "make fgrep\n");
+# endif
+               runbib(indexname);
+               time(&indexdate);
+               unopen(fb);
+               }
+       else /* failure */
+               return(0);
+return(1); /* success */
+}
+ckexist(s, t)
+       char *s, *t;
+{
+char fnam[100];
+strcpy (fnam, s);
+strcat (fnam, t);
+return (access(fnam, 04) != -1);
+}
+FILE *
+iopen (s, t)
+       char *s, *t;
+{
+char fnam[100];
+FILE *f;
+strcpy (fnam, s);
+strcat (fnam, t);
+f = fopen (fnam, "r");
+if (f == NULL)
+       {
+       err("Missing expected file %s", fnam);
+       exit(1);
+       }
+return(f);
+}
diff --git a/usr/src/usr.bin/refer/hunt9.c b/usr/src/usr.bin/refer/hunt9.c
new file mode 100644 (file)
index 0000000..73b615f
--- /dev/null
@@ -0,0 +1,6 @@
+remote(in, out)
+       char *in, *out;
+{
+/* "in" is a long distance file name: get it */
+;
+}
diff --git a/usr/src/usr.bin/refer/inv1.c b/usr/src/usr.bin/refer/inv1.c
new file mode 100644 (file)
index 0000000..234a8f3
--- /dev/null
@@ -0,0 +1,157 @@
+# include "stdio.h"
+# include "assert.h"
+
+main(argc, argv)
+       char *argv[];
+{
+/* make inverted file indexes.  Reads a stream from mkey which
+   gives record pointer items and keys.  Generates a set of files
+      a. NHASH pointers to file b.
+      b. lists of record numbers.
+      c. record pointer items.
+
+   these files are named xxx.ia, xxx.ib, xxx.ic;
+   where xxx is taken from arg1.
+   If the files exist they are updated.
+*/
+
+FILE *fa, *fb, *fc, *fta, *ftb, *ftc, *fd;
+int nhash 256;
+int appflg 1;
+int keepkey 0, pipein 0;
+char nma[100], nmb[100], nmc[100], com[100], nmd[100];
+char tmpa[20], tmpb[20], tmpc[20];
+char *remove NULL;
+int chatty 0, docs, hashes, fp[2], fr, fw, pfork, pwait, status;
+int i,j,k;
+long keys;
+int iflong =0;
+char *sortdir;
+
+sortdir = (access("/crp/tmp", 06)==0) ? "/crp/tmp" : "/usr/tmp";
+while (argv[1][0] == '-')
+       {
+       switch(argv[1][1])
+               {
+               case 'h': /* size of hash table */
+                       nhash = atoi (argv[1]+2); break;
+               case 'n': /* new, don't append */
+                       appflg=0; break;
+               case 'a': /* append to old file */
+                       appflg=1; break;
+               case 'v': /* verbose output */
+                       chatty=1; break;
+               case 'd': /* keep keys on file .id for check on searching */
+                       keepkey=1; break;
+               case 'p': /* pipe into sort (saves space, costs time)*/
+                       pipein = 1; break;
+               case 'i': /* input is on file, not stdin */
+                       close(0);
+                       if (open(argv[2], 0) != 0)
+                               err("Can't read input %s", argv[2]);
+                       if (argv[1][2]=='u') /* unlink */
+                               remove = argv[2];
+                       argc--; argv++;
+                       break;
+               }
+       argc--;
+       argv++;
+       }
+
+strcpy (nma, argc >= 2 ? argv[1] : "Index");
+strcpy (nmb, nma);
+strcpy (nmc, nma);
+strcpy (nmd, nma);
+strcat (nma, ".ia");
+strcat (nmb, ".ib");
+strcat (nmc, ".ic");
+strcat (nmd, ".id");
+
+sprintf(tmpa, "junk%di", getpid());
+if (pipein)
+       {
+       pipe(fp); fr=fp[0]; fw=fp[1];
+       if ( (pfork=fork()) == 0)
+               {
+               close(fw);
+               close(0);
+               _assert(dup(fr)==0);
+               close(fr);
+               execl("/bin/sort", "sort", "-T", sortdir, "-o", tmpa, 0);
+               execl("/usr/bin/sort", "sort", "-T", sortdir, "-o", tmpa, 0);
+               _assert(0);
+               }
+       _assert(pfork!= -1);
+       close(fr);
+               fta = fopen("/dev/null", "w");
+               close(fta->_file);
+               fta->_file = fw;
+       }
+else /* use tmp file */
+       {
+       fta = fopen(tmpa, "w");
+       _assert (fta != NULL);
+       }
+fb = 0;
+if (appflg )
+       {
+       if (fb = fopen(nmb, "r"))
+               {
+               sprintf(tmpb, "junk%dj", getpid());
+               ftb = fopen(tmpb, "w");
+               if (ftb==NULL)
+                       err("Can't get scratch file %s",tmpb);
+               nhash = recopy(ftb, fb, fopen(nma, "r"));
+               fclose(ftb);
+               }
+       else
+               appflg=0;
+       }
+fc = fopen(nmc,  appflg ? "a" : "w");
+if (keepkey)
+fd = keepkey ? fopen(nmd, "w") : 0;
+docs = newkeys(fta, stdin, fc, nhash, fd, &iflong);
+fclose(stdin);
+if (remove != NULL)
+       unlink(remove);
+fclose(fta);
+if (pipein)
+       {
+       pwait = wait(&status);
+       printf("pfork %o pwait %o status %d\n",pfork,pwait,status);
+       _assert(pwait==pfork);
+       _assert(status==0);
+       }
+else
+       {
+       sprintf(com, "sort -T %s %s -o %s", sortdir, tmpa, tmpa);
+       system(com);
+       }
+
+if (appflg)
+       {
+       sprintf(tmpc, "junk%dk", getpid());
+       sprintf(com, "mv %s %s", tmpa, tmpc);
+       system(com);
+       sprintf(com, "sort -T %s  -m %s %s -o %s", sortdir,
+               tmpb, tmpc, tmpa);
+       system(com);
+       }
+fta = fopen(tmpa, "r");
+fa = fopen(nma, "w");
+fb = fopen(nmb, "w");
+whash(fta, fa, fb, nhash, iflong, &keys, &hashes);
+fclose(fta);
+# ifndef D1
+unlink(tmpa);
+# endif
+if (appflg)
+       {
+       unlink(tmpb);
+       unlink(tmpc);
+       }
+if (chatty)
+       
+       printf ("%ld key occurrences,  %d hashes, %d docs\n",
+               keys, hashes, docs);
+}
diff --git a/usr/src/usr.bin/refer/inv2.c b/usr/src/usr.bin/refer/inv2.c
new file mode 100644 (file)
index 0000000..3c366ea
--- /dev/null
@@ -0,0 +1,89 @@
+# include "stdio.h"
+# include "assert.h"
+newkeys (outf, inf, recf, nhash, fd, iflong)
+       FILE *outf, *inf, *recf, *fd;
+       int *iflong;
+{
+/* reads key lines from inf; hashes and writes on outf; writes orig
+   key on recf, records pointer on outf too.
+   format of outf is : hash code space record pointer
+*/
+
+# define LINESIZ 1250
+long lp, ftell();
+long ld 0; int ll 0, lt 0;
+char line[LINESIZ];
+char key[30], bkeys[40];
+char *p, *s;
+char *keyv[500];
+int i, nk, ndoc 0, more 0, c;
+
+lp = ftell (recf);
+while (fgets(line, LINESIZ, inf))
+       {
+       p = line;
+       while (*p != '\t') p++;
+       *p++ =0;
+       fputs(line, recf);
+       if (fd)
+               {
+               sprintf(bkeys, ";%ld", ld);
+               ll = strlen(p);
+               lt = strlen(bkeys);
+               fputs(bkeys, recf);
+               sprintf(bkeys, ",%d", ll);
+               lt += strlen(bkeys);
+               fputs(bkeys, recf);
+               ld += ll;
+               fputs(p, fd);
+               }
+       putc('\n',recf);
+       for(s=p; *s; s++);
+       if (*--s == '\n')
+               {
+               more=0;
+               *s=0;
+               }
+       else
+               more=1;
+       _assert (fd==0 || more==0);
+       nk = getargs(p, keyv);
+       if (more)
+               nk--;
+       for(i=0; i<nk; i++)
+               fprintf(outf,"%04d %06ld\n",hash(keyv[i])%nhash, lp);
+# if D1
+       for(i=0; i<nk; i++)
+               printf("key %s hash %d\n",keyv[i],hash(keyv[i])%nhash);
+# endif
+       if (more) /* allow more than LINESIZ keys */
+               {
+               strcpy(key, keyv[nk]);
+               for(s=key; *s; s++);
+               while ( (c=getc(inf)) != '\n')
+                       {
+                       if (c != ' ')
+                               {
+                               *s++ = c;
+                               continue;
+                               }
+                       *s=0;
+                       if (s>key)
+                               fprintf(outf, "%04d %06ld\n",hash(key)%nhash, lp);
+                       s = key;
+                       }
+               }
+       lp += (strlen(line)+lt+1);
+       ndoc++;
+       }
+*iflong = (lp>=65536L);
+fclose(recf);
+return(ndoc);
+}
+trimnl(p)
+       char *p;
+{
+while (*p) p++;
+p--;
+if (*p == '\n') *p=0;
+}
diff --git a/usr/src/usr.bin/refer/inv3.c b/usr/src/usr.bin/refer/inv3.c
new file mode 100644 (file)
index 0000000..0448f4e
--- /dev/null
@@ -0,0 +1,16 @@
+getargs(s, arps)
+       char *s, *arps[];
+{
+       int i;
+i = 0;
+while (1)
+       {
+       arps[i++]=s;
+       while (*s != 0 && *s!=' '&& *s != '\t')s++;
+       if (*s==0) break;
+       *s++ =0;
+       while (*s==' ' || *s=='\t')s++;
+       if (*s==0)break;
+       }
+return(i);
+}
diff --git a/usr/src/usr.bin/refer/inv5.c b/usr/src/usr.bin/refer/inv5.c
new file mode 100644 (file)
index 0000000..ddb3893
--- /dev/null
@@ -0,0 +1,50 @@
+# include "stdio.h"
+
+recopy (ft, fb, fa, nhash)
+       FILE *ft, *fb, *fa;
+{
+/* copy fb (old hash items/pointers) to ft (new ones) */
+int n, i, iflong;
+long getl();
+int getw();
+int *hpt_s;
+int (*getfun)();
+long *hpt_l;
+long k, lp;
+if (fa==NULL)
+       {
+       err("No old pointers",0);
+       return;
+       }
+fread(&n, sizeof(n), 1, fa);
+fread(&iflong, sizeof(iflong), 1, fa);
+if (iflong)
+       {
+       hpt_l =  calloc(sizeof(*hpt_l), n+1);
+       n =fread(hpt_l, sizeof(*hpt_l), n, fa);
+       }
+else
+       {
+       hpt_s =  calloc(sizeof(*hpt_s), n+1);
+       n =fread(hpt_s, sizeof(*hpt_s), n, fa);
+       }
+if (n!= nhash)
+       fprintf(stderr, "Changing hash value to old %d\n",n);
+fclose(fa);
+if (iflong)
+       getfun = &getl;
+else
+       getfun = &getw;
+for(i=0; i<n; i++)
+       {
+       if (iflong)
+               lp = hpt_l[i];
+       else
+               lp = hpt_s[i];
+       fseek(fb, lp, 0);
+       while ( (k= (*getfun)(fb) ) != -1)
+               fprintf(ft, "%04d %06ld\n",i,k);
+       }
+fclose(fb);
+return(n);
+}
diff --git a/usr/src/usr.bin/refer/inv6.c b/usr/src/usr.bin/refer/inv6.c
new file mode 100644 (file)
index 0000000..1d48b28
--- /dev/null
@@ -0,0 +1,81 @@
+# include "stdio.h"
+# include "assert.h"
+whash(ft, fa, fb, nhash, iflong, ptotct, phused)
+       FILE *fa, *fb, *ft;
+       int nhash, *phused;
+       long *ptotct;
+{
+char line[100];
+int hash 0, hused 0;
+long totct 0L;
+int ct 0;
+long point;
+long opoint -1;
+int m;
+int k; long lp;
+long *hpt;
+int *hfreq NULL;
+
+hpt = calloc (nhash+1, sizeof(*hpt));
+_assert (hpt != NULL);
+hfreq = calloc (nhash, sizeof(*hfreq));
+_assert (hfreq !=NULL);
+hpt[0] = 0;
+lp= 0;
+while (fgets(line, 100, ft))
+       {
+       totct++;
+       sscanf(line, "%d %ld", &k, &point);
+       if (hash < k)
+               {
+               hused++;
+               if (iflong) putl(-1L, fb); else putw(-1, fb);
+               hfreq[hash]=ct;
+               while (hash<k)
+                       {
+                       hpt[++hash] = lp;
+                       hfreq[hash] = 0;
+                       }
+               hpt[hash] = lp += iflong? sizeof(long) : sizeof(int);
+               opoint= -1;
+               ct=0;
+               }
+       if (point!=opoint)
+               {
+               if (iflong)
+                       putl(opoint=point, fb);
+               else
+                       putw( (int)(opoint=point), fb);
+               lp += iflong? sizeof(long) : sizeof(int);
+               ct++;
+               }
+       }
+if (iflong) putl(-1L, fb); else putw(-1,fb);
+while (hash<nhash)
+       hpt[++hash]=lp;
+fwrite(&nhash, sizeof(nhash), 1, fa);
+fwrite(&iflong, sizeof(iflong), 1, fa);
+fwrite(hpt, sizeof(*hpt), nhash, fa);
+fwrite (hfreq, sizeof(*hfreq), nhash, fa);
+*ptotct = totct;
+*phused = hused;
+}
+putl(ll, f)
+       long ll;
+       FILE *f;
+{
+int *x;
+x = &ll;
+putw(x[0], f);
+putw(x[1], f);
+}
+long getl(f)
+       FILE *f;
+{
+int x[2];
+int *ll;
+x[0] = getw(f);
+x[1] = getw(f);
+ll = x;
+return (*ll);
+}
diff --git a/usr/src/usr.bin/refer/makefile b/usr/src/usr.bin/refer/makefile
new file mode 100644 (file)
index 0000000..5a255e1
--- /dev/null
@@ -0,0 +1,57 @@
+CFLAGS = -O -s -I.
+all: refer mkey inv hunt
+mkey: mkey1.o mkey2.o mkey3.o deliv2.o
+       cc -s mkey?.o deliv2.o -o mkey
+inv: inv1.o inv2.o inv3.o inv5.o inv6.o deliv2.o
+       cc -s inv?.o deliv2.o -o inv
+hunt: hunt1.o hunt2.o hunt3.o hunt5.o hunt6.o hunt7.o glue5.o
+hunt: refer3.o hunt9.o shell.o deliv2.o hunt8.o glue4.o tick.o
+       cc -s -i hunt?.o refer3.o glue5.o glue4.o shell.o tick.o deliv2.o -o hunt
+deliv: deliv1.o deliv2.o
+       cc -s deliv?.o  -o deliv
+refpart: refer0.o refer1.o refer2.o refer3.o refer4.o refer5.o
+refpart: refer6.o refer7.o refer8.o deliv2.o glue4.o
+refpart: glue5.o mkey3.o
+       cc -s refer?.o deliv2.o glue4.o glue5.o mkey3.o  -o refpart
+refer0.o: refer..c
+refer1.o: refer..c
+refer2.o: refer..c
+refer3.o: refer..c
+refer4.o: refer..c
+refer6.o: refer..c
+refer7.o: refer..c
+refer: glue1.o refer1.o refer2.o refer4.o refer5.o refer6.o mkey3.o
+refer: refer7.o refer8.o hunt2.o hunt3.o deliv2.o hunt5.o hunt6.o hunt8.o
+refer: glue3.o hunt7.o hunt9.o glue2.o glue4.o glue5.o refer0.o shell.o
+       cc -s -i glue?.o refer[01245678].o hunt[2356789].o mkey3.o shell.o deliv2.o -o refer
+drefer: glue1.o refer1.o refer2.o refer4.o refer5.o refer6.o mkey3.o
+drefer: refer7.o refer8.o hunt2.o hunt3.o deliv2.o hunt5.o hunt6.o hunt8.o
+drefer: glue3.o hunt7.o hunt9.o glue2.o glue4.o glue5.o refer0.o shell.o
+       cc -i glue?.o refer[01245678].o hunt[2356789].o mkey3.o shell.o deliv2.o /usr/lib/dballoc.o -o refer
+refer-test:
+       /usr/bin/refer test >junk1a
+       refer test >junk1b
+       if cmp -s junk1a junk1b; then echo ok; else echo bad; fi
+       /usr/bin/refer -e test >junk1a
+       refer -e test >junk1b
+       if cmp -s junk1a junk1b; then echo ok; else echo bad; fi
+       /usr/bin/refer -s test >junk1a
+       refer -s test >junk1b
+       if cmp -s junk1a junk1b; then echo ok; else echo bad; fi
+       /usr/bin/refer -s -l test >junk1a
+       refer -s -l test >junk1b
+       if cmp -s junk1a junk1b; then echo ok; else echo bad; fi
+       rm junk1a junk1b
+refer-install:
+       cp /usr/bin/refer orefer
+       cp refer /usr/bin/refer
+whatabout: what1.o what2.o what3.o what4.o shell.o mkey3.o
+       cc what?.o shell.o mkey3.o  -o whatabout
+extra-all: mkey inv hunt deliv refpart refer
+install:
+       mv mkey $(DESTDIR)/usr/lib/refer/mkey
+       mv hunt $(DESTDIR)/usr/lib/refer/hunt
+       mv inv $(DESTDIR)/usr/lib/refer/inv
+       mv refer $(DESTDIR)/usr/bin/refer
+clean:
+       rm -f *.o refer hunt inv mkey deliver refpart
diff --git a/usr/src/usr.bin/refer/mkey1.c b/usr/src/usr.bin/refer/mkey1.c
new file mode 100644 (file)
index 0000000..29a7efd
--- /dev/null
@@ -0,0 +1,87 @@
+# include "stdio.h"
+extern char *comname; /* "/usr/lib/eign" */
+int wholefile = 0;
+int keycount = 100;
+int labels = 1;
+int minlen = 3;
+extern int comcount;
+char *iglist "XYZ#";
+
+main (argc,argv)
+       char *argv[];
+{
+/* this program expects as its arguments a list of
+   files and generates a set of lines of the form
+     filename:byte-add,length (tab) key1 key2 key3
+   where the byte addresses give the position within
+   the file and the keys are the strings off the lines
+   which are alphabetic, first six characters only.
+*/
+
+int i;
+char *name, qn[200];
+char *inlist 0;
+
+FILE *f, *ff;
+
+while (argc>1 && argv[1][0] == '-')
+       {
+       switch(argv[1][1])
+               {
+               case 'c':
+                       comname = argv[2];
+                       argv++; argc--;
+                       break;
+               case 'w':
+                       wholefile = 1;  break;
+               case 'f':
+                       inlist = argv[2];
+                       argv++; argc--;
+                       break;
+               case 'i':
+                       iglist = argv[2];
+                       argv++; argc--;
+                       break;
+               case 'l':
+                       minlen = atoi(argv[1]+2);
+                       if (minlen<=0) minlen=3;
+                       break;
+               case 'n': /* number of common words to use */
+                       comcount = atoi(argv[1]+2);
+                       break;
+               case 'k': /* number  of keys per file max */
+                       keycount = atoi(argv[1]+2);
+                       break;
+               case 's': /* suppress labels, search only */
+                       labels = 0;
+                       break;
+               }
+       argc--; argv++;
+       }
+if (inlist)
+       {
+       ff = fopen(inlist, "r");
+       while (fgets(qn, 200, ff))
+               {
+               trimnl(qn);
+               f = fopen (qn, "r");
+               if (f!=NULL)
+                       dofile(f, qn);
+               else
+                       fprintf(stderr, "Can't read %s\n",qn);
+               }
+       }
+else
+if (argc<=1)
+       dofile(stdin, "");
+else
+for(i=1; i<argc; i++)
+       {
+       f = fopen(name=argv[i], "r");
+       if (f==NULL)
+               err("No file %s",name);
+       else
+               dofile(f, name);
+       }
+exit(0);
+}
diff --git a/usr/src/usr.bin/refer/mkey2.c b/usr/src/usr.bin/refer/mkey2.c
new file mode 100644 (file)
index 0000000..fd12b80
--- /dev/null
@@ -0,0 +1,135 @@
+# include "stdio.h"
+# include "ctype.h"
+
+       static int eof 0;
+       static long lp, lim;
+       static int alph, used, prevc;
+       static char *p, key[20];
+       extern int minlen, keycount, labels, wholefile;
+       extern char *iglist;
+
+dofile(f, name)
+       FILE *f;
+       char *name;
+{
+
+/* read file f & spit out keys & ptrs */
+# define MAXLINE 500
+char line[MAXLINE], *s;
+int c;
+long grec();
+alph=used=prevc=eof=0;
+
+lp=0;
+if (wholefile==0)
+while (lim = grec(line,f))
+       {
+# if D1
+fprintf(stderr, "line: /%s",line);
+# endif
+       used=alph=0;
+       p = key;
+       for(s=line; (c= *s) && (used<keycount); s++)
+               chkey(c, name);
+       lp += lim;
+       if (used) putchar('\n');
+       }
+else
+       {
+       p=key;
+       used=alph=0;
+       while ( (c=getc(f)) != EOF && used<keycount)
+               chkey (c, name);
+       if (used) putchar('\n');
+       }
+fclose(f);
+}
+
+outkey( ky, lead, trail)
+       char *ky;
+{
+       int n;
+n = strlen(ky);
+if (n<minlen) return (0);
+if (n<3)
+       {
+       if (trail == '.') return(0);
+       if (mindex(".%,!#$%&'();+:*", lead)!=0) return(0);
+       }
+if (isdigit(ky[0]))
+       if (ky[0] != '1' || ky[1] != '9' || n!= 4) return(0);
+if (common(ky))
+       return(0);
+return(1);
+}
+long grec (s, f)
+       char *s;
+       FILE *f;
+{
+       char tm[200];
+       int curtype 0;
+       long len 0L, tlen 0L;
+       extern int wholefile;
+       if (eof) return(0);
+       *s = 0;
+       while (fgets(tm, 200, f))
+               {
+               tlen += strlen(tm);
+               if (tm[0] == '%' || tm[0] == '.')
+                       curtype = tm[1];
+               if (tlen < MAXLINE && mindex(iglist,curtype)==0)
+                               strcat(s, tm);
+               len = tlen;
+               if (wholefile==0 && tm[0] == '\n' && len>0)
+                       return(len);
+               if (wholefile>0 && len >= MAXLINE)
+                       {
+                       fseek (f, 0L, 2);
+                       return(ftell(f));
+                       }
+               }
+       eof=1;
+       return(s[0] ? len : 0L);
+}
+trimnl(ln)
+       char *ln;
+{
+register char *p ln;
+while (*p) p++;
+p--;
+if (*p == '\n') *p=0;
+return(ln);
+}
+chkey (c, name)
+       {
+       if ((isalpha(c) || isdigit(c)) && prevc != '\\')
+               {
+               if (alph++ < 6)
+                       *p++ = c;
+               }
+       else
+               {
+               *p = 0;
+               for(p=key; *p; p++)
+                       *p |= 040;
+               if (outkey(p=key,prevc,c))
+                       {
+                       if (used==0)
+                           {
+                           if (labels)
+                               {
+                               if (wholefile==0)
+                                       printf("%s:%ld,%ld\t", name, lp, lim);
+                               else
+                                       printf("%s\t", name);
+                           }
+                               }
+                       else
+                               putchar(' ');
+                       fputs(key, stdout);
+                       used++;
+                       }
+               prevc=c;
+               alph=0;
+               }
+       }
diff --git a/usr/src/usr.bin/refer/mkey3.c b/usr/src/usr.bin/refer/mkey3.c
new file mode 100644 (file)
index 0000000..0787400
--- /dev/null
@@ -0,0 +1,50 @@
+# include "stdio.h"
+char *comname = "/usr/lib/eign";
+static int cgate = 0;
+extern char *comname;
+# define COMNUM 500
+# define COMTSIZE 997
+int comcount = 100;
+static char cbuf[COMNUM*9];
+static char *cwds[COMTSIZE];
+static char *cbp;
+
+common (s)
+       char *s;
+{
+if (cgate==0) cominit();
+return (c_look(s, 1));
+}
+cominit()
+{
+int i;
+FILE *f;
+cgate=1;
+f = fopen(comname, "r");
+if (f==NULL) return;
+cbp=cbuf;
+for(i=0; i<comcount; i++)
+       {
+       if (fgets(cbp, 15, f)==NULL)
+               break;
+       trimnl(cbp);
+       c_look (cbp, 0);
+       while (*cbp++);
+       }
+fclose(f);
+}
+c_look (s, fl)
+       char *s;
+{
+int h;
+h = hash(s) % (COMTSIZE);
+while (cwds[h] != 0)
+       {
+       if (strcmp(s, cwds[h])==0)
+               return(1);
+       h = (h+1) % (COMTSIZE);
+       }
+if (fl==0)
+       cwds[h] = s;
+return(0);
+}
diff --git a/usr/src/usr.bin/refer/refer..c b/usr/src/usr.bin/refer/refer..c
new file mode 100644 (file)
index 0000000..b41365a
--- /dev/null
@@ -0,0 +1,33 @@
+# include "stdio.h"
+# include "ctype.h"
+# include "assert.h"
+extern FILE *in;
+extern int endpush, labels, sort, bare, keywant, nobracket;
+extern int newr[];
+extern char *smallcaps;
+extern char comname;
+extern char *keystr;
+extern int authrev;
+extern int nocomma;
+extern int nmlen, dtlen;
+extern int labblkflg;
+extern char *rdata[], **search;
+extern int refnum;
+extern char *reftable[];
+extern char *rtp, reftext[];
+extern int sep;
+extern char tfile[];
+extern char gfile[];
+extern char ofile[];
+extern char hidenam[];
+extern char *Ifile; extern int Iline;
+extern FILE *fo, *ftemp;
+# define FLAG 003
+# define NRFTXT 6000
+# define NTFILE 20
+# define NRFTBL 400
+# define LLINE 512
+# define QLEN 300
+# define ANSLEN 1000
+# define TAGLEN 400
+# define NSERCH 20
diff --git a/usr/src/usr.bin/refer/refer0.c b/usr/src/usr.bin/refer/refer0.c
new file mode 100644 (file)
index 0000000..8db8118
--- /dev/null
@@ -0,0 +1,30 @@
+# include "stdio.h"
+# include "refer..c"
+FILE *in = stdin;
+int endpush  = 0;
+int labels = 0;
+int keywant = 0;
+int sort = 0;
+int bare = 0;
+int nobracket=0;
+int authrev = 0;
+int nocomma=0;
+char *smallcaps = "";
+char *keystr = "AD";
+int nmlen = 0, dtlen = 0;
+int labblkflg=0;
+char *rdata[NSERCH];
+char **search = rdata;
+int refnum = 0;
+char reftext[NRFTXT];
+char *reftable[NRFTBL];
+char *rtp = reftext;
+int sep = '\n';
+char tfile[NTFILE];
+FILE *fo = stdout;
+FILE *ftemp = stdout;
+char ofile[NTFILE];
+char gfile[NTFILE];
+char hidenam[NTFILE];
+char *Ifile = "standard input";
+int Iline  =0;
diff --git a/usr/src/usr.bin/refer/refer1.c b/usr/src/usr.bin/refer/refer1.c
new file mode 100644 (file)
index 0000000..5f9abf4
--- /dev/null
@@ -0,0 +1,170 @@
+# include "signal.h"
+# include "refer..c"
+main(argc,argv)
+       char *argv[];
+{
+char line[LLINE], *s;
+int nodeflt =0;
+
+signals();
+while (argv[1][0] == '-')
+       {
+       switch(argv[1][1])
+               {
+               case 'e':
+                       endpush++; break;
+               case 'l': 
+                       labels++;
+                       s = argv[1]+2;
+                       nmlen = atoi(s);
+                       for ( ; *s; s++)
+                               if (*s == ',' || *s =='_')
+                                       break;
+                       if (*s==0) break;
+                       labblkflg = (*s++ == '_');
+                       dtlen = atoi(s);
+                       break;
+               case 'k':
+                       keywant = (argv[1][2] ? argv[1][2] : 'L');
+                       labels++;
+                       break;
+               case 's':
+                       sort++;
+                       if (argv[1][2])
+                               keystr= argv[1]+2;
+                       break;
+               case 'p':
+                       argc--; argv++;
+                       *search++ = argv[1];
+                       if (search-rdata>NSERCH)
+                               err("too many -p options (%d)", NSERCH);
+                       break;
+               case 'n':
+                       nodeflt=1;
+                       break;
+               case 'B':
+                       nobracket++; /* falls through */
+               case 'b':
+                       bare = (argv[1][2] == '1') ? 1 : 2;
+                       break;
+               case 'c':
+                       smallcaps = argv[1]+2;
+                       break;
+               case 'a':
+                       authrev = atoi (argv[1]+2);
+                       if (authrev<=0)
+                               authrev = 1000;
+                       for(s=argv[1]+2; isdigit(*s); s++)
+                               ;
+                       if (*s==',') nocomma++;
+                       break;
+               }
+       argc--; argv++;
+       }
+if (nodeflt==0)
+       *search++ = "/usr/dict/papers/Ind";
+
+if (sort)
+       endpush=1;
+
+
+if (endpush)
+       sprintf(tfile, "/tmp/rj%da", getpid());
+if (sort)
+       {
+       sprintf(ofile,"/tmp/rj%db", getpid());
+       ftemp = fopen(ofile, "w");
+       if (ftemp==NULL)
+               {
+               fprintf(stderr, "Can't open scratch file\n");
+               exit(1);
+               }
+       }
+
+do
+       {
+       if (argc>1)
+               {
+               fclose(in);
+               Iline=0;
+               in = fopen(Ifile=argv[1], "r");
+               argc--; argv++;
+               if (in==NULL)
+                       {
+                       err("Can't read %s", Ifile);
+                       continue;
+                       }
+               }
+       while (input(line))
+               {
+               Iline++;
+# ifdef D1
+               fprintf(stderr, "line %.20s\n",line);
+# endif
+               if (prefix(".[", line) || (nobracket && line[0]!='\n'))
+                       {
+                       if (endpush && (fo==NULL || fo == stdout))
+                               {
+                               fo = fopen(tfile, "w");
+# if D1
+                               fprintf(stderr, "opened %s as %o\n",tfile,fo);
+# endif
+                               if (fo==NULL)
+                                       {
+                                       fprintf(stderr,"Can't open scratch file");
+                                       exit(1);
+                                       }
+                               sep = 002; /* separate records without confusing sort..*/
+                               }
+                       doref(line);
+                       }
+               else
+                       output(line);
+# if D1
+               fprintf(stderr, "past output/doref\n");
+# endif
+               }
+       }
+       while (argc>1);
+# if D1
+fprintf(stderr, "before dumpold, endpush %d fo %o\n",endpush, fo);
+# endif
+if (endpush && (fo!=NULL && fo != stdout))
+       dumpold();
+output("");
+fflush (ftemp);
+if (sort)
+       recopy(ofile);
+clfgrep();
+# ifndef D1
+cleanup();
+# endif
+exit(0);
+}
+
+extern int intr();
+signals()
+{
+       SIG_TYP oldint;
+oldint = signal(SIGINT, &intr);
+if (oldint== (SIG_TYP)1)
+       signal (SIGINT, 1);
+signal (SIGHUP, &intr);
+signal (SIGPIPE, &intr);
+signal (SIGTERM, &intr);
+}
+
+intr()
+{
+       int oldsig;
+signal(SIGINT, 1);
+cleanup();
+exit(1);
+}
+cleanup()
+{
+if (tfile[0]) unlink(tfile);
+if (gfile[0]) unlink(gfile);
+if (ofile[0]) unlink(ofile);
+if (hidenam[0]) unlink(hidenam);
+}
diff --git a/usr/src/usr.bin/refer/refer2.c b/usr/src/usr.bin/refer/refer2.c
new file mode 100644 (file)
index 0000000..d1da206
--- /dev/null
@@ -0,0 +1,213 @@
+# include "refer..c"
+extern FILE *in;
+# define NFLD 30
+# define TLEN 800
+char one[ANSLEN];
+int onelen = ANSLEN;
+static char dr [100] = "";
+doref(firline)
+       char *firline;
+{
+char buff[QLEN], dbuff[3*QLEN], answer[ANSLEN], temp[TLEN];
+char line[LLINE];
+char *p, **sr, *flds[NFLD], *r;
+int nf, nr, alph, query = 0, chp, digs;
+
+/* get query */
+buff[0] = dbuff[0] = 0;
+if (nobracket)
+       {
+       strcpy( control(firline[0]) ? dbuff : buff , firline);
+       firline= "  ";
+       }
+while (input(line))
+       {
+       if (prefix(".]", line))
+               break;
+       if (nobracket && line[0]=='\n')
+               break;
+       if (control(line[0])) query=1;
+       strcat (query  ? dbuff: buff, line);
+       if (strlen(buff)>QLEN)
+               err("buff too big (%d)", strlen(buff));
+       assert (strlen(dbuff) <3*QLEN);
+       }
+if (strcmp (buff, "$LIST$\n")==0)
+       {
+# if D1
+fprintf(stderr, "dump sorted list\n");
+# endif
+       assert ( dbuff[0]==0);
+       dumpold();
+       return;
+       }
+answer[0] = 0;
+# ifdef D1
+       fprintf(stderr, "query is %s\n",buff);
+# endif
+for( p=buff; *p; p++)
+       {
+       if (isupper(*p)) *p |= 040;
+       }
+alph = digs =0;
+for(p=buff; *p; p++)
+       {
+       if (isalpha(chp = *p)) alph++;
+       else
+       if (isdigit(*p)) digs++;
+       else
+               {
+               *p=0;
+               if ( (alph+digs<3) || common(p-alph))
+                       {
+                       r = p-alph;
+                       while (r < p)
+                               *r++ = ' ';
+                       }
+               if ( alph==0 && digs >0)
+                       {
+                       r = p-digs;
+# if D1
+fprintf(stderr, "number, %d long, text is %s\n",digs,r);
+# endif
+                       if (digs != 4 || (atoi(r)/100 != 19))
+                               {
+                               while (r<p)
+                                       *r++ = ' ';
+                               }
+                       }
+               if (chp== '\\')
+                       *p++ = ' ';
+               *p=' ';
+               alph = digs = 0;
+               }
+       }
+# ifdef D1
+       fprintf(stderr, "query translated to %s\n", buff);
+# endif
+one[0]=0;
+if (buff[0]) /* do not search if no query */
+for( sr= rdata; sr < search; sr++)
+       {
+# ifdef D1
+       fprintf(stderr, "now searching %s\n", *sr);
+# endif
+       temp[0]=0;
+       corout (buff, temp, "hunt", *sr, TLEN);
+       assert (strlen(temp)<TLEN);
+       if (strlen(temp)+strlen(answer)>LLINE)
+               err("Accumulated answers too large",0);
+       strcat (answer, temp);
+       if (strlen(answer)>LLINE)
+               err("answer too long (%d)", strlen(answer));
+       if (newline(answer) > 0) break;
+       }
+# if D1
+fprintf(stderr, "answer:\n%s****\n", answer);
+# endif
+assert (strlen(one)<ANSLEN);
+assert (strlen(answer)<ANSLEN);
+if (buff[0])
+switch (newline(answer))
+       {
+       case 0:
+               fprintf (stderr, "No such paper %s\n", buff);
+               return;
+       default:
+               fprintf(stderr, "too many hits for '%s'\n", trimnl(buff));
+               choices(answer);
+               p = buff;
+               while (*p != '\n') p++;
+               *++p=0;
+       case 1:
+# ifdef D1
+       fprintf(stderr, "found one\n");
+# endif
+               if (endpush)
+                       if (nr = chkdup(answer))
+                               {
+                               if (bare<2)
+                                       putsig (0, flds, nr, firline, line);
+                               return;
+                               }
+# if D1
+               fprintf(stderr, "one[0] was %o\n",one[0]);
+# endif
+               if (one[0]==0)
+                       corout (answer, one, "deliv", dr, QLEN);
+# if D1
+               fprintf(stderr, "one now %o\n",one[0]);
+# endif
+               break;
+       }
+assert (strlen(buff)<QLEN);
+assert (strlen(one)<ANSLEN);
+nf = tabs(flds, one);
+nf += tabs(flds+nf, dbuff);
+# if D1
+fprintf(stderr, "one %.20s dbuff %.20s nf %d\n",one,dbuff, nf);
+# endif
+assert (nf < NFLD);
+refnum++;
+if (sort) putkey (nf, flds, refnum, keystr);
+if (bare<2)
+       putsig (nf, flds, refnum, firline, line);
+else
+       flout();
+# if D1
+fprintf(stderr, "put signal\n");
+# endif
+putref (nf, flds);
+# if D1
+fprintf(stderr, "put ref\n");
+# endif
+}
+newline(s)
+       char *s;
+{
+int k =0, c;
+while (c = *s++)
+       if (c == '\n')
+               k++;
+return(k);
+}
+choices( buff )
+       char *buff;
+{
+char ob[LLINE], *p, *r, *q, *t;
+int nl;
+for(r=p= buff; *p; p++)
+       {
+       if (*p == '\n')
+               {
+               *p++ = 0;
+                       {
+                       corout (r, ob, "deliv", dr, LLINE);
+                       nl = 1;
+                       for( q=ob; *q; q++)
+                               {
+                               if (nl && (q[0] == '.' || q[0] == '%') && q[1] == 'T')
+                                       {
+                                       q += 3;
+                                       for (t=q; *t && *t != '\n'; t++);
+                                       *t = 0;
+                                       fprintf(stderr, "%.70s\n", q);
+                                       q=0; break;
+                                       }
+                               nl = *q == '\n';
+                               }
+                       if (q)
+                               fprintf(stderr, "??? at %s\n",r);
+                       }
+               r=p;
+               }
+       if (*p==0) break; /* the p++ might go past the end */
+       }
+}
+
+control(c)
+       {
+       if (c=='.') return(1);
+       if (c=='%') return(1);
+       return(0);
+       }
diff --git a/usr/src/usr.bin/refer/refer3.c b/usr/src/usr.bin/refer/refer3.c
new file mode 100644 (file)
index 0000000..ca95793
--- /dev/null
@@ -0,0 +1,26 @@
+# include "refer..c"
+corout(in, out, rprog, arg, outlen)
+       char *in, *out, *rprog;
+{
+# define move(x, y) close(y); dup(x); close(x);
+int pipev[2], fr1, fr2, fw1, fw2, n;
+
+pipe (pipev); fr1= pipev[0]; fw1 = pipev[1];
+pipe (pipev); fr2= pipev[0]; fw2 = pipev[1];
+if (fork()==0)
+       {
+       close (fw1); close (fr2);
+       move (fr1, 0);
+       move (fw2, 1);
+       execl(rprog, "deliv", arg, 0);
+       err ("Can't run %s", rprog);
+       }
+close(fw2); close(fr1);
+write (fw1, in , strlen(in));
+close(fw1);
+wait(0);
+n = read (fr2, out, outlen);
+out[n]=0;
+close(fr2);
+return(n);
+}
diff --git a/usr/src/usr.bin/refer/refer4.c b/usr/src/usr.bin/refer/refer4.c
new file mode 100644 (file)
index 0000000..42ceb51
--- /dev/null
@@ -0,0 +1,82 @@
+# include "refer..c"
+static gate= 0;
+static char buff[LLINE];
+char *trimnl();
+output (s)
+       char *s;
+{
+if (gate)
+       fputs(buff,ftemp);
+else
+       gate=1;
+strcpy(buff,s);
+if (strlen(buff)>LLINE)
+       err("one buff too big (%d)!", LLINE);
+}
+append(s)
+       char *s;
+{
+char *p, *r; int lch;
+trimnl(buff);
+for (p=buff; *p; p++)
+       ;
+lch = *--p;
+/*
+ *     Extended by Robert Henry, UC Berkeley, 14 May 81
+ *     to know about lines ending with 
+ *     the set [?!)"-}:], as well as the original [.,]
+ *     all of which can occur in normal text.
+ *
+ *     This prevents the punctuation from being seperated from
+ *     the buried reference
+ */
+#define CASES \
+       case '.': case ',':\
+       case '?': case '!':\
+       case ')': case '"':\
+       case '-': case '}':\
+       case ':'
+
+switch (lch)
+       {
+       CASES:          /* rrh */
+               *p=0;
+               r="\\*(<";
+               while (*r) *p++= *r++;
+               *p++ = lch;
+               *p=0;
+       }
+strcat(buff,s);
+switch(lch)
+       {
+       CASES:          /* rrh */
+               for(p=buff; *p; p++)
+                       ;
+               if (*--p=='\n')*p=0;
+               r = "\\*(>";
+               while (*r) *p++ = *r++;
+               *p++ = lch;
+               *p++ = '\n';
+               *p=0;
+       }
+if (strlen(buff)>LLINE)
+       err("output buff too long (%d)", LLINE);
+}
+
+flout()
+{
+if (gate)
+       fputs(buff,ftemp);
+gate=0;
+}
+
+char *
+trimnl(ln)
+       char *ln;
+{
+register char *p = ln;
+while (*p) p++;
+p--;
+if (*p == '\n') *p=0;
+return(ln);
+}
diff --git a/usr/src/usr.bin/refer/refer5.c b/usr/src/usr.bin/refer/refer5.c
new file mode 100644 (file)
index 0000000..16a9810
--- /dev/null
@@ -0,0 +1,358 @@
+# include "refer..c"
+# define SAME 0
+# define NFLAB 3000
+# define NLABC 1000
+static char sig[400];
+
+static char bflab[NFLAB];
+static char *labtab[NLABC];
+char *lookat(), *artskp();
+static char *lbp = bflab;
+static char labc[NLABC];
+static char stbuff[50];
+static int  prevsig;
+
+putsig (nf, flds, nref, nstline, endline)
+       char *flds[], *nstline, *endline;
+{
+/* choose signal style */
+char t[200], t1[200], t2[200], format[10], *sd;
+char *fpar();
+int another = 0;
+int addon, addlet;
+char *stline, *pr;
+static FILE *fhide = 0;
+if (labels)
+       {
+       if (nf==0) /* old */
+               sprintf(t, "%s%c", labtab[nref], labc[nref]);
+       else
+               {
+               *t=0;
+               if (keywant)
+                       {
+                       pr = fpar(nf, flds, t1, keywant, 1, 0);
+                       if (pr) strcpy(t, pr);
+                       }
+               if (t[0]==0)
+                       {
+                       if (labblkflg)
+                       sprintf(format, nmlen>0 ? "%%.%ds %%s" : "%%s %%s", nmlen);
+                       else
+                       sprintf(format, nmlen>0 ? "%%.%ds%%s" : "%%s%%s", nmlen);
+# if D1
+                       fprintf(stderr, "format is /%s/\n", format);
+# endif
+                       /* format is %s%s for default labels or %.3s%s eg if wanted */
+                       sd = fpar(nf, flds, t2, 'D', 1, 0);
+                       if (dtlen>0)
+                               {
+                               char *sdb;
+                               for(sdb=sd; *sd; sd++);
+                               sd = sd-dtlen;
+                               if (sd<sdb) sd=sdb;
+                               }
+                       sprintf(t, format, fpar(nf, flds, t1, 'A', 1, 0), sd);
+# if D1
+                       fprintf(stderr, "tag is /%s/\n",t);
+# endif
+                       }
+               if (keywant)
+                       {
+                       addon=0;
+                       for(sd=t; *sd; sd++);
+                       if (*--sd == '-')
+                               {
+                               addon=1;
+                               *sd=0;
+                               }
+                       }
+               addlet = keylet(t, nref);
+               if (!keywant || addon)
+                       addch( t, addlet);
+               }
+       if (sort)
+               sprintf(t, "%c%d%c", FLAG, nref, FLAG);
+       }
+else 
+       {
+       if (sort)
+               sprintf(t, "%c%d%c", FLAG, nref, FLAG);
+       else
+               sprintf(t, "%d", nref);
+       }
+another = prefix (".[", sd=lookat());
+if (another && (strcmp(".[\n", sd) != SAME))
+       fprintf(stderr, "File %s, line %d- punctuation ignored from: %s", Ifile, Iline, sd);
+strcat (sig, t);
+# if D1
+fprintf(stderr, "sig is now %s leng %d\n",sig,strlen(sig));
+# endif
+trimnl(nstline);
+trimnl(endline);
+stline=stbuff;
+if (prevsig==0)
+       {
+       strcpy (stline, nstline);
+       prevsig=1;
+       }
+if (stline[2] || endline[2])
+       {
+       stline += 2;
+       endline += 2;
+       }
+else
+       {
+       stline  = "\\*([.";
+       endline = "\\*(.]";
+       }
+# if D1
+fprintf(stderr, "bare %d fhide %o fo %o another %d\n",bare, fhide, fo, another);
+# endif
+if (bare==0)
+       {
+       if (another==0)
+               {
+               sprintf(t1, "%s%s\%s\n", stline, sig, endline);
+               append(t1);
+               flout();
+               sig[0]=0;
+               prevsig=0;
+               if (fo != NULL && fo == fhide)
+                       {
+                       int ch;
+# if D1
+fprintf(stderr, "more hiding\n");
+# endif
+                       fclose(fhide); fhide= fopen(hidenam, "r");
+                       fo= ftemp;
+                       while ((ch = getc(fhide)) != EOF)
+                               putc(ch, fo);
+                       fclose(fhide);
+                       unlink(hidenam);
+# if D1
+fprintf(stderr, "past this stuff\n");
+# endif
+                       }
+               }
+       else
+               {
+               strcat (sig, (labels ? ", " : ",\\|"));
+               /* hide if need be */
+               if (fo == ftemp)
+                       {
+                       sprintf(hidenam, "/tmp/rj%dc", getpid());
+# if D1
+fprintf(stderr, "hiding in %s\n", hidenam);
+# endif
+                       fhide= fopen(hidenam, "w");
+                       if (fhide==NULL) err("Can't get scratch file %s", hidenam);
+                       fo = fhide;
+                       }
+               }
+       }
+if (bare<2)
+       if (nf>0) fprintf(fo,".ds [F %s%c",t,sep);
+if (bare>0)
+       flout();
+# if D1
+fprintf(stderr, "sig is now %s\n",sig);
+# endif
+}
+char *
+fpar (nf, flds, out, c, seq, prepend)
+       char *flds[], *out;
+{
+char *p, *s;
+int i, fnd = 0;
+for(i=0; i<nf; i++)
+       if (control(flds[i][0]) &&flds[i][1]==c && ++fnd >= seq)
+               {
+               if (c== 'L')
+                       {
+                       p = flds[i]+3;
+                       strcpy(out, p);
+                       return(out);
+                       }
+               if (c!= 'A' && c != 'D') /* if not author, date use first word */
+                       {
+                       p=flds[i]+3;
+                       p = artskp (p);
+                       mycpy2(out, p, 20);
+                       return(out);
+                       }
+               if (c=='A' && lfirst(p=flds[i]+3)) /* author in style Jones, A. */
+                       {
+                       for(s=out; *p!=','; p++)
+                               *s++ = *p;
+                       *s++ = 0;
+                       if (prepend)
+                               {
+                               while (isspace(*p))p++;
+                               *s++ = *p;
+                               *s=0;
+                               }
+                       return(out);
+                       }
+               for(s=p= flds[i]+2; *p; p++);
+               while (p>s && *p != ' ') p--;
+               /* special wart for authors */
+               if (c=='A' && (p[-1] == ',' || p[1] =='('))
+                       {
+                       p--;
+                       while (p>s && *p != ' ') p--;
+                       mycpy (out, p+1);
+                       }
+               else
+                       strcpy (out, p+1);
+               if (c=='A' && prepend)
+                       initadd(out, flds[i]+2, p);
+               return(out);
+               }
+return(0);
+}
+putkey(nf, flds, nref, keystr)
+       char *flds[], *keystr;
+{
+char t1[50], *sf;
+int ctype, i, count;
+fprintf(fo, ".\\\"");
+if (nf <= 0)
+       fprintf(fo, "%s%c%c", labtab[nref], labc[nref], sep);
+else
+       {
+       while (ctype= *keystr++)
+               {
+               count = atoi(keystr);
+               if (*keystr=='+') count=999;
+               if (count<=0) count=1;
+               for(i=1; i<=count; i++)
+                       {
+                       sf= fpar(nf, flds, t1, ctype, i, 1);
+                       if (sf==0)
+                               break;
+                       sf = artskp(sf);
+                       fprintf(fo, "%s%c", sf, '-');
+                       }
+               }
+       fprintf(fo, "%d%c", nref, sep);
+       }
+}
+keylet(t, nref)
+       char *t;
+{
+int i;
+int x = 'a'-1;
+for(i=1; i<nref;i++)
+       {
+       if (strcmp(labtab[i], t) == 0)
+               x = labc[i];
+       }
+strcpy(labtab[nref]=lbp, t);
+while (*lbp++);
+if (lbp-bflab >NFLAB)
+       err("bflab overflow (%d)", NFLAB);
+if (nref >NLABC)
+       err ("nref in labc overflow (%d)", NLABC);
+# ifdef D1
+fprintf(stderr, "lbp up to %d of 2000\n", lbp-bflab);
+# endif
+return (labc[nref] = x+1);
+}
+mycpy(s,t)
+       char *s, *t;
+{
+while (*t && *t != ',' && *t != ' ')
+       *s++ = *t++;
+*s=0;
+}
+mycpy2 (s, t, n)
+       char *s, *t;
+{
+int c;
+while (n-- && (c= *t++)>0)
+       {
+       if (c==' ')c= '-';
+       *s++ = c;
+       }
+*s=0;
+}
+initadd(to, from, stop)
+       char *to, *from, *stop;
+{
+       int c, nalph= 1;
+while (*to) to++;
+while (from<stop)
+       {
+       c = *from++;
+       if (!isalpha(c))
+               {
+               if (nalph)
+                       *to++ = '.';
+               nalph=0;
+               continue;
+               }
+       if (nalph++ ==0)
+               *to++ = c;
+       }
+*to=0;
+}
+
+static char *articles[] ={"the ", "an ", "a ", 0};
+char *
+artskp(s)
+       char *s;
+{
+/* skips over initial "a ", "an ", or "the " in s */
+       char **p, *r1, *r2;
+for(p=articles; *p; p++)
+       {
+       r2 = s;
+       for (r1= *p; ((*r1 ^ *r2) & ~040 ) == 0; r1++)
+               r2++;
+       if (*r1==0 && *r2 != 0)
+               return(r2);
+       }
+return(s);
+}
+expkey (or, nr, fwrite)
+       FILE *fwrite;
+{
+int uniq, less, i;
+char *s;
+# if D1
+fprintf(stderr, "old %d key %s: '%c' new %d\n",or, labtab[or],labc[or],nr);
+# endif
+/* is this unique? how many are before it ? */
+uniq=1; less= 'a';
+s = labtab[or];
+for(i=0; i <= refnum; i++)
+       {
+       if (i==or) continue;
+       if (strcmp(labtab[i], s)!=SAME)
+               continue;
+       uniq=0;
+       if (newr[i] != 0 && newr[i] < nr)
+               less++;
+       }
+if (uniq)
+       fprintf(fwrite, "%s", s);
+else
+       fprintf(fwrite,"%s%c", s, less);
+}
+lfirst(s)
+       char *s;
+{
+/* decides if s is name of format Jones, A */
+char *index(), *p;
+p = index(s, ',');
+if (p==NULL) return(0);
+while (isspace(*++p)) ;
+if (strncmp(p, "Jr", 2)==0)
+       return(0);
+if (strncmp(p, "II", 2)==0)
+       return(0);
+if (isupper(*p))
+       return(1);
+return(0);
+}
diff --git a/usr/src/usr.bin/refer/refer6.c b/usr/src/usr.bin/refer/refer6.c
new file mode 100644 (file)
index 0000000..250396c
--- /dev/null
@@ -0,0 +1,235 @@
+# include "refer..c"
+char *class();
+putref (n, tvec)
+       char *tvec[];
+{
+char *s, *tx;
+char *caps(), *revauth();
+char buf1[LLINE], buf2[50];
+int nauth = 0, i, lastype = 0, cch, macro = 0, la;
+int lauth =0, ltitle =0, lother =0;
+
+# if D1
+fprintf(stderr, "in putref, fo is %o\n",fo);
+# endif
+fprintf(fo, ".]-%c", sep);
+# define dsde (macro? "de" : "ds")
+# define ifnl (macro? sep : ' ')
+for (i=0; i<n; i++)
+       {
+       s = tvec[i];
+       if (*s==0) continue;
+       if (control(s[0]) )
+               {
+               if (lastype && macro)
+                       fprintf(fo, "..%c", sep);
+               if (control(s[1]))
+                       {
+                       cch = s[2];
+                       tx = s+3;
+                       macro=1;
+                       }
+               else
+                       {
+                       cch = s[1];
+                       tx = s+2;
+                       macro=0;
+                       }
+               }
+       else
+               {
+               cch = lastype;
+               tx=s;
+               }
+# if D1
+fprintf(stderr, "smallcaps %s cch %c\n",smallcaps, cch);
+# endif
+       if (mindex (smallcaps, cch))
+               tx = caps(tx, buf1);
+# if D1
+fprintf(stderr, " s %o tx %o %s\n",s,tx,tx);
+# endif
+       if (!control(s[0])) /* append to previous item */
+               {
+               if (lastype!=0)
+                       {
+                       if (macro)
+                               fprintf(fo, "%s%c", tx, sep);
+                       else
+                               fprintf(fo, ".as [%c \" %s%c",lastype,tx,sep);
+                       if (lastype == 'T')
+                               ltitle = (mindex(".;,?", last(tx))!=0);
+                       if (lastype == 'A')
+                               lauth = last(tx) == '.';
+                       if (lastype == 'O')
+                               lother = (mindex(".;,?", last(tx)) != 0);
+                       }
+               continue;
+               }
+       if (mindex("XYZ[]", cch)) /* skip these */
+               {
+               lastype=0;
+               continue;
+               }
+       else
+       if (cch == 'A')
+               {
+               if (nauth < authrev)
+                       tx = revauth(tx, buf2);
+               if (nauth++ ==0)
+                       if (macro)
+                               fprintf(fo, ".de [%c%c%s%c",cch,sep,tx,sep);
+                       else
+                               fprintf(fo, ".ds [%c%s%c", cch,tx,sep);
+               else
+                       {
+                       la = (tvec[i+1][1]!='A');
+                       fprintf(fo, ".as [A \"");
+                       if (la == 0 || nauth != 2)
+                               fprintf(fo, ",");
+                       if (la)
+                       fprintf(fo,"%s", 
+                               mindex(smallcaps, 'A')? " \\s-2AND\\s+2" : " and");
+                       fprintf(fo, "%s%c", tx, sep);
+                       }
+               lauth = last(tx)=='.';
+               }
+       else
+               if (macro)
+                       fprintf(fo, ".de [%c%c%s%c",cch,sep, tx, sep);
+               else
+                       fprintf(fo, ".ds [%c%s%c",cch,tx, sep);
+       if (cch=='P')
+               fprintf(fo, ".nr [P %d%c", mindex(s, '-')!=0, sep);
+       lastype = cch;
+       if (cch == 'T')
+               ltitle = (mindex(".;,?", last(tx)) != 0);
+       if (cch == 'O')
+               lother = (mindex(".;,?", last(tx)) != 0);
+       }
+if (lastype && macro)
+       fprintf(fo, "..%c", sep);
+fprintf(fo, ".nr [T %d%c", ltitle, sep);
+fprintf(fo, ".nr [A %d%c", lauth, sep);
+fprintf(fo, ".nr [O %d%c", lother, sep);
+fprintf (fo, ".][ %s%c", class(n, tvec), '\n');
+}
+tabs (sv, line)
+       char *sv[], *line;
+{
+char *p;
+int n = 0;
+sv[n++] = line;
+for( p= line; *p; p++)
+       {
+       if (*p == '\n')
+               {
+               *p=0;
+               sv[n++] = p+1;
+               }
+       }
+return(n-1);
+}
+char *
+class (nt, tv)
+       char *tv[];
+{
+if (hastype (nt, tv, 'J'))
+       return("1 journal-article");
+if (hastype (nt, tv, 'B'))
+       return("3 article-in-book");
+if (hastype (nt, tv, 'R'))
+       return ("4 tech-report");
+if (hastype (nt, tv, 'G'))
+       return ("4 tech-report");
+if (hastype (nt, tv, 'I'))
+       return("2 book");
+if (hastype (nt, tv,'M'))
+       return ("5 bell-tm");
+return("0 other");
+}
+hastype (nt, tv, c)
+       char *tv[];
+{
+int i;
+for(i=0; i<nt; i++)
+       if ( control(tv[i][0]) && tv[i][1]==c )
+               return(1);
+return(0);
+}
+char *
+caps(a, b)
+       char *a, *b;
+{
+char *p;
+int c, alph, this;
+p=b;
+alph = 0;
+while (c = *a++)
+       {
+       this = isalpha(c);
+       if (this && alph==1)
+               {
+               *b++ = '\\';
+               *b++ = 's';
+               *b++ = '-';
+               *b++ = '2';
+               }
+       if (!this && alph>1)
+               {
+               *b++ = '\\';
+               *b++ = 's';
+               *b++ = '+';
+               *b++ = '2';
+               }
+       if (this)
+               c &= (~040);
+       *b++ = c;
+       alph = this ? alph+1 : 0;
+       }
+if (alph>1)
+       {
+       *b++ = '\\';
+       *b++ = 's';
+       *b++ = '+';
+       *b++ = '2';
+       }
+*b=0;
+return (p);
+}
+char *
+revauth(s, b)
+       char *s, *b;
+{
+char *init, *name, *jr, *p, *bcop;
+bcop = b;
+init=name=s;
+while (*name)name++;
+jr=name;
+while (name>init && *name!= ' ')
+       name--;
+if (name[-1] == ',' || name[-1]== '(' )
+       {
+       jr = --name;
+       while (name>init && *name != ' ')
+               name--;
+       }
+p=name;
+while (p<jr)
+       *b++ = *p++;
+if (init<name && nocomma==0)
+       *b++ = ',';
+while (init<name)
+       *b++ = *init++;
+if (*jr)jr++;
+while(*jr)
+       *b++ = *jr++;
+*b++ = 0;
+return(bcop);
+}
+last(s)
+       char *s;
+{
+while (*s) s++;
+return(*--s);
+}
diff --git a/usr/src/usr.bin/refer/refer7.c b/usr/src/usr.bin/refer/refer7.c
new file mode 100644 (file)
index 0000000..5708f5f
--- /dev/null
@@ -0,0 +1,144 @@
+# include "refer..c"
+# define NRS 500
+int newr[NRS];
+chkdup ( tag )
+       char *tag;
+{
+int i;
+for(i=1; i<=refnum; i++)
+       {
+       if (strcmp(reftable[i], tag)==0)
+               return(i);
+       }
+reftable[refnum+1] = rtp;
+if (refnum>=NRFTBL)
+       err("too many references (%d) for table", refnum);
+strcpy (rtp, tag);
+while (*rtp++);
+if (rtp > reftext + NRFTXT)
+       err("reftext too big (%d)", rtp-reftext);
+return(0);
+}
+dumpold()
+{
+FILE *fi;
+int c, g1 = 0, nr = 1;
+if (!endpush) return;
+fclose(fo);
+# ifdef D1
+fprintf(stderr, "closing %o\n",fo);
+# endif
+fo=NULL;
+# if D1
+fprintf(stderr, "dumpold for %s ftemp is %o stdout %o\n", tfile, ftemp, stdout);
+# endif
+if (sort)
+       {
+       char comm[100];
+       sprintf(comm, "sort -fd %s -o %s", tfile, tfile);
+       system(comm);
+       }
+fi = fopen(tfile, "r");
+# if D1
+fprintf(stderr, "reopened %s as %o\n",tfile,fi);
+# endif
+if (fi==NULL) return;
+flout();
+fprintf(ftemp,".]<\n");
+while ( (c = getc(fi)) >=0)
+       {
+       if (c=='\n')
+               {
+               nr++;
+               g1 = 0;
+               }
+       if (c==sep)
+               c = '\n';
+       if (c== FLAG)
+               {
+               /* make old-new ref number table */
+               char tb[20];
+               int irr;
+               char *s = tb;
+               while ( (c=getc(fi)) != FLAG)
+                       *s++ = c;
+               *s=0;
+               if (g1++ == 0)
+                       {
+                       irr = atoi(tb);
+                       if (irr>=NRS)
+                               err("too many references to renumber",0);
+                       newr[irr]=nr;
+                       }
+# if D1
+               fprintf(stderr, "nr %d assigned to atoi(tb) %d\n",nr,irr);
+# endif
+               if (!labels)
+                       fprintf(ftemp,"%d", nr);
+               else
+                       expkey (irr, nr, ftemp);
+               continue;
+               }
+       putc(c, ftemp);
+       }
+fclose(fi);
+# ifndef D1
+unlink(tfile);
+# endif
+fprintf(ftemp, ".]>\n");
+}
+
+recopy (fnam)
+       char *fnam;
+{
+int c;
+fclose(ftemp);
+# if D1
+fprintf(stderr, "recopy, fnam %s\n",fnam);
+# endif
+
+ftemp = fopen(fnam, "r");
+if (ftemp==NULL)
+       {
+       fprintf(stderr, "Can't reopen %s\n", fnam);
+       exit(1);
+       }
+# if D1
+fprintf(stderr, "open ok ftemp %o\n",ftemp);
+# endif
+while ( 1)
+       {
+       c = fgetc(ftemp);
+# if D1
+fprintf(stderr, "read char %o /%c/\n",c,c);
+# endif
+       if (c == FLAG)
+               {
+               char tb[10];
+               char *s = tb;
+               int tbx;
+               while ( (c = getc(ftemp)) != FLAG && c != EOF)
+                       *s++ = c;
+               *s=0;
+               tbx = atoi(tb);
+# if D1
+               fprintf(stderr, "prelim number is %d\n",tbx);
+# endif
+               if (!labels)
+                       printf("%d", newr[tbx]);
+               else
+                       expkey (tbx, newr[tbx], stdout);
+               continue;
+               }
+       if (c==EOF) break;
+       putchar(c);
+       }
+# if D1
+fprintf(stderr, "finished reading file \n");
+# endif
+fclose(ftemp);
+unlink(fnam);
+# if D1
+fprintf(stderr, "le\ 1aving recopy\n");
+# endif
+}
diff --git a/usr/src/usr.bin/refer/refer8.c b/usr/src/usr.bin/refer/refer8.c
new file mode 100644 (file)
index 0000000..5066873
--- /dev/null
@@ -0,0 +1,33 @@
+# include "refer..c"
+static char ahead[1000];
+static int peeked = 0;
+static char * noteof = (char *)1;
+char *
+input (s)
+       char *s;
+{
+if (peeked)
+       {
+       peeked=0;
+       if (noteof==0) return(0);
+       strcpy (s, ahead);
+       return(s);
+       }
+return(fgets(s, 1000, in));
+}
+char *
+lookat()
+{
+       char *input();
+if (peeked) return(ahead);
+noteof = input(ahead);
+peeked=1;
+return(noteof);
+}
+addch(s, c)
+       char *s;
+{
+while (*s) s++;
+*s++ = c;
+*s = 0;
+}
diff --git a/usr/src/usr.bin/refer/shell.c b/usr/src/usr.bin/refer/shell.c
new file mode 100644 (file)
index 0000000..ed18064
--- /dev/null
@@ -0,0 +1,24 @@
+shell (n, comp, exch)
+       int (*comp)(), (*exch)();
+/* SORTS UP.  IF THERE ARE NO EXCHANGES (IEX=0) ON A SWEEP
+  THE COMPARISON GAP (IGAP) IS HALVED FOR THE NEXT SWEEP */
+{
+       int igap, iplusg, iex, i, imax;
+       igap=n;
+while (igap > 1)
+       {
+       igap /= 2;
+       imax = n-igap;
+       do
+               {
+               iex=0;
+               for(i=0; i<imax; i++)
+                       {
+                       iplusg = i + igap;
+                       if ((*comp) (i, iplusg) ) continue;
+                       (*exch) (i, iplusg);
+                       iex=1;
+                       }
+               } while (iex>0);
+       }
+}
diff --git a/usr/src/usr.bin/refer/signal.h b/usr/src/usr.bin/refer/signal.h
new file mode 100644 (file)
index 0000000..584a6b8
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef        NSIG
+#define NSIG   32
+
+#define        SIGHUP  1       /* hangup */
+#define        SIGINT  2       /* interrupt */
+#define        SIGQUIT 3       /* quit */
+#define        SIGILL  4       /* illegal instruction (not reset when caught) */
+#define        SIGTRAP 5       /* trace trap (not reset when caught) */
+#define        SIGIOT  6       /* IOT instruction */
+#define        SIGEMT  7       /* EMT instruction */
+#define        SIGFPE  8       /* floating point exception */
+#define                K_INT_OVF_T 0x1         /* integer overflow */
+#define                K_INT_DIV_T 0x2         /* integer divide by zero */
+#define                K_FLT_OVF_T 0x3         /* floating overflow */
+#define                K_FLT_DIV_T 0x4         /* floating/decimal divide by zero */
+#define                K_FLT_UND_T 0x5         /* floating underflow */
+#define                K_DEC_OVF_T 0x6         /* decimal overflow */
+#define                K_SUB_RNG_T 0x7         /* subscript out of range */
+#define                K_FLT_OVF_F 0x8         /* floating overflow fault */
+#define                K_FLT_DIV_F 0x9         /* divide by zero floating fault */
+#define                K_FLT_UND_F 0xa         /* floating underflow fault */
+#define        SIGKILL 9       /* kill (cannot be caught or ignored) */
+#define        SIGBUS  10      /* bus error */
+#define        SIGSEGV 11      /* segmentation violation */
+#define        SIGSYS  12      /* bad argument to system call */
+#define        SIGPIPE 13      /* write on a pipe with no one to read it */
+#define        SIGALRM 14      /* alarm clock */
+#define        SIGTERM 15      /* software termination signal from kill */
+
+#define        SIGSTOP 17      /* sendable stop signal not from tty */
+#define        SIGTSTP 18      /* stop signal from tty */
+#define        SIGCONT 19      /* continue a stopped process */
+#define        SIGCHLD 20      /* to parent on child stop or exit */
+#define        SIGTTIN 21      /* to readers pgrp upon background tty read */
+#define        SIGTTOU 22      /* like TTIN for output if (tp->t_local&LTOSTOP) */
+#define SIGTINT        23      /* to pgrp on every input character if LINTRUP */
+#define        SIGXCPU 24      /* exceeded CPU time limit */
+#define        SIGXFSZ 25      /* exceeded file size limit */
+
+#ifndef KERNEL
+int    (*signal())();
+#endif
+typedef int    (*SIG_TYP)();
+
+#define        BADSIG          (int (*)())-1
+#define        SIG_DFL         (int (*)())0
+#define        SIG_IGN         (int (*)())1
+#ifdef KERNEL
+#define        SIG_CATCH       (int (*)())2
+#endif
+#define        SIG_HOLD        (int (*)())3
+
+#define        SIGISDEFER(x)   (((int)(x) & 1) != 0)
+#define        SIGUNDEFER(x)   (int (*)())((int)(x) &~ 1)
+#define        DEFERSIG(x)     (int (*)())((int)(x) | 1)
+
+#define        SIGNUMMASK      0377            /* to extract pure signal number */
+#define        SIGDOPAUSE      0400            /* do pause after setting action */
+#define        SIGDORTI        01000           /* do ret+rti after setting action */
+#endif
diff --git a/usr/src/usr.bin/refer/tick.c b/usr/src/usr.bin/refer/tick.c
new file mode 100644 (file)
index 0000000..7bd2c97
--- /dev/null
@@ -0,0 +1,35 @@
+/* time programs */
+# include "stdio.h"
+# include "sys/types.h"
+# include "sys/timeb.h"
+struct tbuffer {
+       long    proc_user_time;
+       long    proc_system_time;
+       long    child_user_time;
+       long    child_system_time;
+};
+static long start, user, system;
+tick()
+{
+       struct tbuffer tx;
+       struct timeb tp;
+       times (&tx);
+       ftime (&tp);
+       user =  tx.proc_user_time;
+       system= tx.proc_system_time;
+       start = tp.time*1000+tp.millitm;
+}
+tock()
+{
+       struct tbuffer tx;
+       struct timeb tp;
+       float lap, use, sys;
+       if (start==0) return;
+       times (&tx);
+       ftime (&tp);
+       lap = (tp.time*1000+tp.millitm-start)/1000.;
+       use = (tx.proc_user_time - user)/60.;
+       sys = (tx.proc_system_time - system)/60.;
+       printf("Elapsed %.2f CPU %.2f (user %.2f, sys %.2f)\n",
+               lap, use+sys, use, sys);
+}