| 1 | #include "head.h" |
| 2 | |
| 3 | /* |
| 4 | * These procedures manage the source files examined by sdb. |
| 5 | */ |
| 6 | |
| 7 | /* Change the current source file to `name'. */ |
| 8 | finit(name) |
| 9 | char *name; { |
| 10 | register char *p, *q; |
| 11 | |
| 12 | if (fiobuf.fd) close(fiobuf.fd); |
| 13 | q = name; |
| 14 | for (p=fp; *q; *p++ = *q++) ; |
| 15 | *p = 0; |
| 16 | if ((fiobuf.fd = open(filework,0)) == -1) { |
| 17 | nolines = 1; |
| 18 | perror(filework); |
| 19 | return; |
| 20 | } |
| 21 | binit(&fiobuf); |
| 22 | cpstr(curfile, name); |
| 23 | for (p=fbuf;;p++) { |
| 24 | if (bread(&fiobuf,p,1) <= 0) { |
| 25 | nolines = 1; |
| 26 | printf("%s: No lines in file\n", name); |
| 27 | return; |
| 28 | } |
| 29 | if (*p == '\n') break; |
| 30 | } |
| 31 | fline = 1; |
| 32 | maxfline = 0; |
| 33 | nolines = 0; |
| 34 | } |
| 35 | |
| 36 | /* Make the next line current. */ |
| 37 | fnext() { |
| 38 | register char *p; |
| 39 | |
| 40 | if (nolines){ |
| 41 | return; |
| 42 | } |
| 43 | for(p=fbuf;;p++) { |
| 44 | if (bread(&fiobuf,p,1) <= 0) { |
| 45 | p--; |
| 46 | blseek(&fiobuf,0L,0); |
| 47 | fline = 0; |
| 48 | continue; |
| 49 | } |
| 50 | if (*p == '\n') break; |
| 51 | } |
| 52 | fline++; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Make the previous line current. */ |
| 57 | fprev() { |
| 58 | char c; |
| 59 | register int i; |
| 60 | |
| 61 | if (nolines){ |
| 62 | return; |
| 63 | } |
| 64 | for(i=0; i<3; i++) { |
| 65 | for (;;) { |
| 66 | if (bread(&fiobuf, &c+1, -1) <= 0) { |
| 67 | if (maxfline) blseek(&fiobuf,0L,2); |
| 68 | else { |
| 69 | blseek(&fiobuf,0L,0); |
| 70 | for(;;) { |
| 71 | if (bread(&fiobuf,&c,1)<=0) |
| 72 | break; |
| 73 | if (c == '\n') maxfline++; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if (c == '\n') break; |
| 78 | } |
| 79 | } |
| 80 | bread(&fiobuf, &c, 1); /* eat the '\n' */ |
| 81 | |
| 82 | fline -= 2; |
| 83 | if (fline < 0) fline = maxfline - 1; |
| 84 | |
| 85 | fnext(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* Print the current line. */ |
| 90 | fprint() { |
| 91 | register char *p; |
| 92 | |
| 93 | if (nolines){ |
| 94 | error("No lines in file"); |
| 95 | return; |
| 96 | } |
| 97 | printf("%d: ", fline); |
| 98 | p = fbuf; |
| 99 | while(putchar(*p++) != '\n') |
| 100 | ; |
| 101 | } |
| 102 | |
| 103 | /* Make line `num' current. */ |
| 104 | ffind(num) |
| 105 | register int num; { |
| 106 | register int i, ofline; |
| 107 | |
| 108 | if (nolines){ |
| 109 | return; |
| 110 | } |
| 111 | ofline = fline; |
| 112 | if (num>fline) |
| 113 | for (i=fline; i<num; i++) { |
| 114 | fnext(); |
| 115 | if (fline == 1) goto bad; |
| 116 | } |
| 117 | if (num<fline) |
| 118 | for (i=num; i<ofline; i++) { |
| 119 | fprev(); |
| 120 | } |
| 121 | |
| 122 | if (maxfline & num>maxfline) goto bad; |
| 123 | |
| 124 | return; |
| 125 | |
| 126 | bad: error("Not that many lines in file"); |
| 127 | ffind(ofline); |
| 128 | } |
| 129 | |
| 130 | /* Go back n lines. */ |
| 131 | fback(n) { |
| 132 | int i; |
| 133 | |
| 134 | if (nolines){ |
| 135 | return(0); |
| 136 | } |
| 137 | for (i=0; i<n; i++) { |
| 138 | if (fline == 1) return(i); |
| 139 | fprev(); |
| 140 | } |
| 141 | return(i); |
| 142 | } |
| 143 | |
| 144 | /* Go forwards n lines. */ |
| 145 | fforward(n) { |
| 146 | int i; |
| 147 | |
| 148 | if (nolines){ |
| 149 | return(0); |
| 150 | } |
| 151 | for (i=0; i<n; i++) { |
| 152 | fnext(); |
| 153 | if (fline == 1) { |
| 154 | fprev(); |
| 155 | return(i); |
| 156 | } |
| 157 | } |
| 158 | return(i); |
| 159 | } |
| 160 | |
| 161 | /* Print n lines. */ |
| 162 | fprintn(n) { |
| 163 | int i; |
| 164 | |
| 165 | if (nolines){ |
| 166 | error("No lines in file"); |
| 167 | return(0); |
| 168 | } |
| 169 | for (i=0; i<n; i++) { |
| 170 | fprint(); |
| 171 | fnext(); |
| 172 | if (fline == 1) break; |
| 173 | } |
| 174 | fprev(); |
| 175 | return(i); |
| 176 | } |