History

20070410-2023 look, issue 40

Charles.Forsyth committed 19 years ago

Commit c75b1eb1 parent 2ae337c5 files · +47 −44Browse files

Unified · Split

5 files
  1. CHANGES +3 −0
  2. appl/cmd/look.b +32 −41
  3. dis/look.dis +0 −0
  4. include/version.h +1 −1
  5. man/1/look +12 −3
CHANGES+3−0
@@ -1,3 +1,6 @@
1+20070410
2+ fix some porting errors in /appl/cmd/look.b
3+ document look's -r option in look(1)
14 20070407
25 /os/port/^(portdat.h lib.h portfns.h xalloc.c) compatiblity changes with plan 9
36 /os/pc ether drivers updated to match plan 9, and os/ip (and a few files in /os/port) also updated, including removing some unreachable code
appl/cmd/look.b+32−41
@@ -28,13 +28,11 @@ bout: ref Iobuf; · 27 unmodified lines
2828 debug := 0;
2929 fold, direc, exact, iflag, range: int;
3030 rev := 1; # -1 for reverse-ordered file, not implemented
31+compare: ref fn(a, b: string): int;
3132 tab := '\t';
32−nflag := 0;
3333 entry: string;
3434 word: string;
3535 key: string;
36−orig: string;
37−targ: string;
3836 latin_fold_tab := array[64] of {
3937 # Table to fold latin 1 characters to ASCII equivalents
4038 # based at Rune value 0xc0
@@ -64,10 +62,15 @@ init(nil: ref Draw->Context, args: list of string) · 23 unmodified lines
6462 bufio = load Bufio Bufio->PATH;
6563 arg := load Arg Arg->PATH;
6664
65+ lastkey: string;
66+
6767 arg->init(args);
68− arg->setusage(arg->progname()+" -[dfinx] [-r orig] [-t c] [string] [file]");
68+ arg->setusage("look -[dfinx] [-r lastkey] [-t c] [string] [file]");
69+ compare = acomp;
6970 while((c := arg->opt()) != 0)
7071 case c {
72+ 'D' =>
73+ debug = 1;
7174 'd' =>
7275 direc++;
7376 'f' =>
@@ -75,37 +78,37 @@ init(nil: ref Draw->Context, args: list of string) · 1 unmodified line
7578 'i' =>
7679 iflag++;
7780 'n' =>
78− nflag = 1;
81+ compare = ncomp;
7982 't' =>
8083 tab = (arg->earg())[0];
8184 'x' =>
8285 exact++;
8386 'r' =>
8487 range++;
85− orig = arg->earg();
86− targ = rcanon(orig);
88+ lastkey = rcanon(arg->earg());
8789 * =>
88− sys->fprint(sys->fildes(2), "%s: bad option %c\n", arg->progname(), c);
89− sys->fprint(sys->fildes(2), "usage: %s -[dfinx] [-t c] [-r limit] [string] [file]\n", arg->progname());
90− raise "fail:usage";
90+ arg->usage();
9191 }
9292 args = arg->argv();
9393 arg = nil;
9494
9595 bin := bufio->fopen(sys->fildes(0), Sys->OREAD);
9696 bout = bufio->fopen(sys->fildes(1), Sys->OWRITE);
97− if(!iflag)
97+ orig: string;
98+ if(!iflag){
9899 if(args != nil){
99100 orig = hd args;
100101 args = tl args;
101− key = rcanon(orig);
102102 }else
103103 iflag++;
104+ }
104105 if(args == nil){
105106 direc++;
106107 fold++;
107108 }else
108109 filename = hd args;
110+ if(!iflag)
111+ key = rcanon(orig);
109112 if(debug)
110113 sys->fprint(sys->fildes(2), "orig %s key %s %s\n", orig, key, filename);
111114 dfile = bufio->open(filename, Sys->OREAD);
@@ -114,7 +117,7 @@ init(nil: ref Draw->Context, args: list of string) · 2 unmodified lines
114117 raise "fail:no dictionary";
115118 }
116119 if(!iflag)
117− if(!locate() && !range && exact)
120+ if(!locate() && !range)
118121 raise "fail:not found";
119122 do{
120123 if(iflag){
@@ -126,36 +129,34 @@ init(nil: ref Draw->Context, args: list of string) · 5 unmodified lines
126129 continue;
127130 }
128131 if(range){
129− if(compare(key, word) <= 0 && compare(word, targ) <= 0)
132+ if(compare(key, word) <= 0 && compare(word, lastkey) <= 0)
130133 bout.puts(entry);
131− }else if(!exact || !compare(word, key))
134+ }else if(!exact || acomp(word, orig) == 0)
132135 bout.puts(entry);
136+ Matches:
133137 while((entry = dfile.gets('\n')) != nil){
134138 word = rcanon(entry);
135139 if(range)
136− n := compare(word, targ);
140+ n := compare(word, lastkey);
137141 else
138142 n = compare(key, word);
139143 if(debug)
140− sys->print("compare %d\n", n);
144+ sys->print("compare %d %q\n", n, word);
141145 case n {
142146 -2 =>
143− if(range){
144− bout.puts(entry);
145− continue;
146− }
147+ if(!range)
148+ break Matches;
149+ bout.puts(entry);
147150 -1 =>
148151 if(exact)
149− break;
150− if(!exact || !compare(word, key))
151− bout.puts(entry);
152− continue;
152+ break Matches;
153+ bout.puts(entry);
153154 0 =>
154− if(!exact || !compare(word, key))
155+ if(!exact || acomp(word, orig) == 0)
155156 bout.puts(entry);
156− continue;
157+ * =>
158+ break Matches;
157159 }
158− break;
159160 }
160161 }while(iflag);
161162 bout.flush();
@@ -208,26 +209,16 @@ Search: · 46 unmodified lines
208209 -2 =>
209210 return 0;
210211 -1 =>
211− if(exact)
212− return 0;
213− return 1;
212+ return !exact;
214213 0 =>
215214 return 1;
216215 1 or 2 =>
217− continue;
216+ ;
218217 }
219218 }
220219 return 0;
221220 }
222221
223−compare(s, t: string): int
224−{
225− if(nflag)
226− return ncomp(s, t);
227− else
228− return acomp(s, t);
229−}
230−
231222 #
232223 # acomp(s, t) returns:
233224 # -2 if s strictly precedes t
@@ -265,7 +256,7 @@ rcanon(s: string): string · 31 unmodified lines
265256 s = s[0: len s - 1];
266257 o := 0;
267258 for(i := 0; i < len s && (r := s[i]) != tab; i++){
268− if(16rc0 <= r && r <= 16rff && (mr := latin_fold_tab[r-16rc0]) != 0)
259+ if(islatin1(r) && (mr := latin_fold_tab[r-16rc0]) != 0)
269260 r = mr;
270261 if(direc)
271262 if(!(isalnum(r) || r == ' ' || r == '\t'))
dis/look.dis+0−0

Binary file not shown.

include/version.h+1−1
@@ -1 +1 @@
1−#define VERSION "Fourth Edition (20070407)"
1+#define VERSION "Fourth Edition (20070410)"
man/1/look+12−3
@@ -4,9 +4,12 @@ look \- find lines in a sorted list · 3 unmodified lines
44 .SH SYNOPSIS
55 .B look
66 [
7−.BI -dfnixt c
8−]
9−[
7+.BI -dfnix
8+] [
9+.BI -r " endkey"
10+] [
11+.BI -t c
12+] [
1013 .I string
1114 ]
1215 [
@@ -49,6 +52,12 @@ Upper case letters compare equal to lower case. · 36 unmodified lines
4952 Numeric comparison with initial string of digits, optional minus sign,
5053 and optional decimal point.
5154 .TP
55+.BI -r " endkey"
56+Limit the range of matching values, to include
57+the word
58+.I endkey
59+but no larger values.
60+.TP
5261 .BR -t [ \f2c\f1 ]
5362 Character
5463 .I c