changed skimsource so that it just returns if the given filename
authorMark Linton <linton@ucbvax.Berkeley.EDU>
Fri, 12 Feb 1982 15:54:28 +0000 (07:54 -0800)
committerMark Linton <linton@ucbvax.Berkeley.EDU>
Fri, 12 Feb 1982 15:54:28 +0000 (07:54 -0800)
is the same as the current source filename ... this way routines from the
outside needn't test this

SCCS-vsn: usr.bin/pascal/pdx/source/source.c 1.2

usr/src/usr.bin/pascal/pdx/source/source.c

index 8bf0a30..26a66ce 100644 (file)
@@ -1,28 +1,37 @@
 /* Copyright (c) 1982 Regents of the University of California */
 
 /* Copyright (c) 1982 Regents of the University of California */
 
-static char sccsid[] = "@(#)source.c 1.1 %G%";
+static char sccsid[] = "@(#)source.c 1.2 %G%";
 
 /*
 
 /*
- * source file management
+ * Source file management.
  */
 
 #include "defs.h"
 #include "source.h"
 
 /*
  */
 
 #include "defs.h"
 #include "source.h"
 
 /*
- * data structure for indexing source seek addresses by line number
+ * Seektab is the data structure used for indexing source
+ * seek addresses by line number.
  *
  * The constraints are:
  *
  *
  * The constraints are:
  *
- *     we want an array so indexing is fast and easy
- *     we don't want to waste space for small files
- *     we don't want an upper bound on # of lines in a file
- *     we don't know how many lines there are
+ *  we want an array so indexing is fast and easy
+ *  we don't want to waste space for small files
+ *  we don't want an upper bound on # of lines in a file
+ *  we don't know how many lines there are
  *
  * The solution is a "dirty" hash table.  We have NSLOTS pointers to
  * arrays of NLINESPERSLOT addresses.  To find the source address of
  * a particular line we find the slot, allocate space if necessary,
  * and then find its location within the pointed to array.
  *
  * The solution is a "dirty" hash table.  We have NSLOTS pointers to
  * arrays of NLINESPERSLOT addresses.  To find the source address of
  * a particular line we find the slot, allocate space if necessary,
  * and then find its location within the pointed to array.
+ *
+ * As a result, there is a limit of NSLOTS*NLINESPERSLOT lines per file
+ * but this is plenty high and still fairly inexpensive.
+ *
+ * This implementation maintains only one source file at any given
+ * so as to avoid consuming too much memory.  In an environment where
+ * memory is less constrained and one expects to be changing between
+ * files often enough, it would be reasonable to have multiple seek tables.
  */
 
 typedef int SEEKADDR;
  */
 
 typedef int SEEKADDR;
@@ -30,10 +39,10 @@ typedef int SEEKADDR;
 #define NSLOTS 20
 #define NLINESPERSLOT 500
 
 #define NSLOTS 20
 #define NLINESPERSLOT 500
 
-#define slotno(line)   ((line)/NLINESPERSLOT)
-#define index(line)    ((line)%NLINESPERSLOT)
-#define slot_alloc()   alloc(NLINESPERSLOT, SEEKADDR)
-#define srcaddr(line)  seektab[(line)/NLINESPERSLOT][(line)%NLINESPERSLOT]
+#define slotno(line)    ((line)/NLINESPERSLOT)
+#define index(line) ((line)%NLINESPERSLOT)
+#define slot_alloc()    alloc(NLINESPERSLOT, SEEKADDR)
+#define srcaddr(line)   seektab[(line)/NLINESPERSLOT][(line)%NLINESPERSLOT]
 
 LOCAL SEEKADDR *seektab[NSLOTS];
 
 
 LOCAL SEEKADDR *seektab[NSLOTS];
 
@@ -46,12 +55,12 @@ LOCAL FILE *srcfp;
 chkline(linenum)
 register LINENO linenum;
 {
 chkline(linenum)
 register LINENO linenum;
 {
-       if (linenum < 1) {
-               error("line number must be positive");
-       }
-       if (linenum > lastlinenum) {
-               error("not that many lines");
-       }
+    if (linenum < 1) {
+       error("line number must be positive");
+    }
+    if (linenum > lastlinenum) {
+       error("not that many lines");
+    }
 }
 
 /*
 }
 
 /*
@@ -61,24 +70,24 @@ register LINENO linenum;
 printlines(l1, l2)
 LINENO l1, l2;
 {
 printlines(l1, l2)
 LINENO l1, l2;
 {
-       register int c;
-       register LINENO i;
-       register FILE *fp;
-
-       chkline(l1);
-       chkline(l2);
-       if (l2 < l1) {
-               error("second line number less than first");
-       }
-       fp = srcfp;
-       fseek(fp, (long) srcaddr(l1), 0);
-       for (i = l1; i <= l2; i++) {
-               printf("%5d   ", i);
-               while ((c = getc(fp)) != '\n') {
-                       putchar(c);
-               }
-               putchar('\n');
+    register int c;
+    register LINENO i;
+    register FILE *fp;
+
+    chkline(l1);
+    chkline(l2);
+    if (l2 < l1) {
+       error("second line number less than first");
+    }
+    fp = srcfp;
+    fseek(fp, (long) srcaddr(l1), 0);
+    for (i = l1; i <= l2; i++) {
+       printf("%5d   ", i);
+       while ((c = getc(fp)) != '\n') {
+           putchar(c);
        }
        }
+       putchar('\n');
+    }
 }
 
 /*
 }
 
 /*
@@ -88,40 +97,40 @@ LINENO l1, l2;
 skimsource(file)
 char *file;
 {
 skimsource(file)
 char *file;
 {
-       register int c;
-       register LINENO count;
-       register FILE *fp;
-       register LINENO linenum;
-       register SEEKADDR lastaddr;
-       register int slot;
-
-       if (file == NIL) {
-               return;
-       }
-       if ((fp = fopen(file, "r")) == NULL) {
-               panic("can't open \"%s\"", file);
-       }
-       if (cursource != NIL) {
-               free_seektab();
-       }
-       cursource = file;
-       linenum = 0, count = 0, lastaddr = 0;
-       while ((c = getc(fp)) != EOF) {
-               count++;
-               if (c == '\n') {
-                       slot = slotno(++linenum);
-                       if (slot >= NSLOTS) {
-                               panic("skimsource: too many lines");
-                       }
-                       if (seektab[slot] == NIL) {
-                               seektab[slot] = slot_alloc();
-                       }
-                       seektab[slot][index(linenum)] = lastaddr;
-                       lastaddr = count;
-               }
+    register int c;
+    register LINENO count;
+    register FILE *fp;
+    register LINENO linenum;
+    register SEEKADDR lastaddr;
+    register int slot;
+
+    if (file == NIL || file == cursource) {
+       return;
+    }
+    if ((fp = fopen(file, "r")) == NULL) {
+       panic("can't open \"%s\"", file);
+    }
+    if (cursource != NIL) {
+       free_seektab();
+    }
+    cursource = file;
+    linenum = 0, count = 0, lastaddr = 0;
+    while ((c = getc(fp)) != EOF) {
+       count++;
+       if (c == '\n') {
+           slot = slotno(++linenum);
+           if (slot >= NSLOTS) {
+               panic("skimsource: too many lines");
+           }
+           if (seektab[slot] == NIL) {
+               seektab[slot] = slot_alloc();
+           }
+           seektab[slot][index(linenum)] = lastaddr;
+           lastaddr = count;
        }
        }
-       lastlinenum = linenum;
-       srcfp = fp;
+    }
+    lastlinenum = linenum;
+    srcfp = fp;
 }
 
 /*
 }
 
 /*
@@ -133,12 +142,12 @@ char *file;
 
 LOCAL free_seektab()
 {
 
 LOCAL free_seektab()
 {
-       register int slot;
+    register int slot;
 
 
-       for (slot = 0; slot < NSLOTS; slot++) {
-               if (seektab[slot] != NIL) {
-                       free(seektab[slot]);
-                       seektab[slot] = NIL;
-               }
+    for (slot = 0; slot < NSLOTS; slot++) {
+       if (seektab[slot] != NIL) {
+           free(seektab[slot]);
+           seektab[slot] = NIL;
        }
        }
+    }
 }
 }