date and time created 83/08/05 13:34:37 by sam
[unix-history] / usr / src / old / dbx / source.c
index db5444d..9ef8946 100644 (file)
@@ -1,6 +1,6 @@
 /* Copyright (c) 1982 Regents of the University of California */
 
 /* Copyright (c) 1982 Regents of the University of California */
 
-static char sccsid[] = "@(#)source.c 1.2 %G%";
+static char sccsid[] = "@(#)source.c 1.9 %G%";
 
 /*
  * Source file management.
 
 /*
  * Source file management.
@@ -45,7 +45,7 @@ private String prevsource = nil;
  * and then find its location within the pointed to array.
  */
 
  * and then find its location within the pointed to array.
  */
 
-typedef int Seekaddr;
+typedef long Seekaddr;
 
 #define NSLOTS 20
 #define NLINESPERSLOT 500
 
 #define NSLOTS 20
 #define NLINESPERSLOT 500
@@ -101,7 +101,7 @@ Lineno l1, l2;
                    ub = lastlinenum;
                }
                f = srcfp;
                    ub = lastlinenum;
                }
                f = srcfp;
-               fseek(f, (long) srcaddr(lb), 0);
+               fseek(f, srcaddr(lb), 0);
                for (i = lb; i <= ub; i++) {
                    printf("%5d   ", i);
                    while ((c = getc(f)) != '\n') {
                for (i = lb; i <= ub; i++) {
                    printf("%5d   ", i);
                    while ((c = getc(f)) != '\n') {
@@ -115,6 +115,35 @@ Lineno l1, l2;
     }
 }
 
     }
 }
 
+/*
+ * Search the sourcepath for a file.
+ */
+
+static char fileNameBuf[1024];
+
+public String findsource(filename)
+String filename;
+{
+    register File f;
+    register String src, dir;
+
+    if (filename[0] == '/') {
+       src = filename;
+    } else {
+       src = nil;
+       foreach (String, dir, sourcepath)
+           sprintf(fileNameBuf, "%s/%s", dir, filename);
+           f = fopen(fileNameBuf, "r");
+           if (f != nil) {
+               fclose(f);
+               src = fileNameBuf;
+               break;
+           }
+       endfor
+    }
+    return src;
+}
+
 /*
  * Open a source file looking in the appropriate places.
  */
 /*
  * Open a source file looking in the appropriate places.
  */
@@ -122,18 +151,15 @@ Lineno l1, l2;
 public File opensource(filename)
 String filename;
 {
 public File opensource(filename)
 String filename;
 {
-    register String dir;
-    char buf[256];
+    String s;
     File f;
 
     File f;
 
-    f = nil;
-    foreach (String, dir, sourcepath)
-       sprintf(buf, "%s/%s", dir, filename);
-       f = fopen(buf, "r");
-       if (f != nil) {
-           break;
-       }
-    endfor
+    s = findsource(filename);
+    if (s == nil) {
+       f = nil;
+    } else {
+       f = fopen(s, "r");
+    }
     return f;
 }
 
     return f;
 }
 
@@ -158,7 +184,7 @@ String filename;
 private skimsource()
 {
     register int c;
 private skimsource()
 {
     register int c;
-    register Lineno count;
+    register Seekaddr count;
     register File f;
     register Lineno linenum;
     register Seekaddr lastaddr;
     register File f;
     register Lineno linenum;
     register Seekaddr lastaddr;
@@ -226,7 +252,9 @@ public getsrcpos()
     curline = srcline(pc);
     filename = srcfilename(pc);
     setsource(filename);
     curline = srcline(pc);
     filename = srcfilename(pc);
     setsource(filename);
-    cursrcline = curline;
+    if (curline != 0) {
+       cursrcline = curline;
+    }
 }
 
 /*
 }
 
 /*
@@ -240,3 +268,46 @@ public printsrcpos()
        printf(" in file \"%s\"", cursource);
     }
 }
        printf(" in file \"%s\"", cursource);
     }
 }
+
+#define DEF_EDITOR  "vi"
+
+/*
+ * Invoke an editor on the given file.  Which editor to use might change
+ * installation to installation.  For now, we use "vi".  In any event,
+ * the environment variable "EDITOR" overrides any default.
+ */
+
+public edit(filename)
+String filename;
+{
+    extern String getenv();
+    String ed, src, s;
+    Symbol f;
+    Address addr;
+    char lineno[10];
+
+    ed = getenv("EDITOR");
+    if (ed == nil) {
+       ed = DEF_EDITOR;
+    }
+    src = findsource((filename != nil) ? filename : cursource);
+    if (src == nil) {
+       f = which(identname(filename, true));
+       if (not isblock(f)) {
+           error("can't read \"%s\"", filename);
+       }
+       addr = firstline(f);
+       if (addr == NOADDR) {
+           error("no source for \"%s\"", filename);
+       }
+       src = srcfilename(addr);
+       s = findsource(src);
+       if (s != nil) {
+           src = s;
+       }
+       sprintf(lineno, "+%d", srcline(addr));
+    } else {
+       sprintf(lineno, "+1");
+    }
+    call(ed, stdin, stdout, lineno, src, nil);
+}