BSD 4_2 release
[unix-history] / usr / src / usr.bin / refer / refer1.c
index 5f9abf4..726a3b6 100644 (file)
-# include "signal.h"
-# include "refer..c"
-main(argc,argv)
-       char *argv[];
+#ifndef lint
+static char *sccsid = "@(#)refer1.c    4.1 (Berkeley) 5/6/83";
+#endif
+
+#include <signal.h>
+#include "refer..c"
+
+main(argc,argv)                /* process command-line arguments */
+char *argv[];
 {
 {
-char line[LLINE], *s;
-int nodeflt =0;
+       char line[BUFSIZ], *s;
+       int nodeflt = 0;
 
 
-signals();
-while (argv[1][0] == '-')
-       {
-       switch(argv[1][1])
-               {
+       signals();
+       while (argv[1][0] == '-') {
+               switch(argv[1][1]) {
                case 'e':
                case 'e':
-                       endpush++; break;
+                       endpush++; 
+                       break;
+               case 's':
+                       sort++;
+                       endpush = 1;
+                       if (argv[1][2])
+                               keystr = argv[1]+2;
+                       break;
                case 'l': 
                        labels++;
                        s = argv[1]+2;
                        nmlen = atoi(s);
                case 'l': 
                        labels++;
                        s = argv[1]+2;
                        nmlen = atoi(s);
-                       for ( ; *s; s++)
-                               if (*s == ',' || *s =='_')
+                       while (*s)
+                               if (*s++ == ',')
                                        break;
                                        break;
-                       if (*s==0) break;
-                       labblkflg = (*s++ == '_');
                        dtlen = atoi(s);
                        break;
                case 'k':
                        keywant = (argv[1][2] ? argv[1][2] : 'L');
                        labels++;
                        break;
                        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;
+               case 'n':
+                       nodeflt = 1;
                        break;
                case 'p':
                        break;
                case 'p':
-                       argc--; argv++;
+                       argc--; 
+                       argv++;
                        *search++ = argv[1];
                        *search++ = argv[1];
-                       if (search-rdata>NSERCH)
+                       if (search-rdata > NSERCH)
                                err("too many -p options (%d)", NSERCH);
                        break;
                                err("too many -p options (%d)", NSERCH);
                        break;
-               case 'n':
-                       nodeflt=1;
+               case 'a':
+                       authrev = atoi(argv[1]+2);
+                       if (authrev<=0)
+                               authrev = 1000;
                        break;
                        break;
-               case 'B':
-                       nobracket++; /* falls through */
                case 'b':
                        bare = (argv[1][2] == '1') ? 1 : 2;
                        break;
                case 'c':
                        smallcaps = argv[1]+2;
                        break;
                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++;
+               case 'f':
+                       refnum = atoi(argv[1]+2) - 1;
+                       break;
+               case 'B':
+                       biblio++;
+                       bare = 2;
+                       if (argv[1][2])
+                               convert = argv[1]+2;
+                       break;
+               case 'S':
+                       science++;
+                       labels = 1;
+                       break;
+               case 'P':
+                       postpunct++;
                        break;
                }
                        break;
                }
-       argc--; argv++;
+               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);
+       if (getenv("REFER") != NULL)
+               *search++ = getenv("REFER");
+       else if (nodeflt == 0)
+               *search++ = "/usr/dict/papers/Ind";
+       if (endpush) {
+               sprintf(tfile, "/tmp/rj%da", getpid());
+               fo = fopen(tfile, "w");
+               if (fo == NULL) {
+                       fo = ftemp;
+                       fprintf(stderr, "Can't open scratch file");
                }
                }
+               sep = 002; /* separate records without confusing sort..*/
        }
        }
-
-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;
-                       }
+       if (sort && !labels) {
+               sprintf(ofile, "/tmp/rj%db", getpid());
+               ftemp = fopen(ofile, "w");
+               if (ftemp == NULL) {
+                       fprintf(stderr, "Can't open scratch file\n");
+                       exit(1);
                }
                }
-       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);
+       }
+       do {
+               if (argc > 1) {
+                       fclose(in);
+                       Iline = 0;
+                       in = fopen(Ifile = argv[1], "r");
+                       argc--; 
+                       argv++;
+                       if (in == NULL) {
+                               err("Can't read %s", (void) Ifile);
+                               continue;
                        }
                        }
-               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);
+               while (input(line)) {
+                       Iline++;
+                       if (biblio && *line == '\n')
+                               doref(line);
+                       else if (biblio && Iline == 1 && *line == '%')
+                               doref(line);
+                       else if (!prefix(".[", line))
+                               output(line);
+                       else
+                               doref(line);
+               }
+       } while (argc > 1);
+
+       if (endpush && fo != NULL)
+               dumpold();
+       output("");
+       if (sort && !labels)
+               recopy(ofile);
+       clfgrep();
+       cleanup();
+       exit(0);
 }
 
 extern int intr();
 }
 
 extern int intr();
+
 signals()
 {
 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);
+       if (signal(SIGINT, SIG_IGN) != SIG_IGN)
+               signal(SIGINT, intr);
+       signal(SIGHUP, intr);
+       signal(SIGPIPE, intr);
+       signal(SIGTERM, intr);
 }
 
 intr()
 {
 }
 
 intr()
 {
-       int oldsig;
-signal(SIGINT, 1);
-cleanup();
-exit(1);
+       signal(SIGINT, SIG_IGN);
+       cleanup();
+       exit(1);
 }
 }
+
 cleanup()
 {
 cleanup()
 {
-if (tfile[0]) unlink(tfile);
-if (gfile[0]) unlink(gfile);
-if (ofile[0]) unlink(ofile);
-if (hidenam[0]) unlink(hidenam);
+       if (tfile[0])
+               unlink(tfile);
+       if (gfile[0])
+               unlink(gfile);
+       if (ofile[0])
+               unlink(ofile);
+       if (hidenam[0])
+               unlink(hidenam);
 }
 }