-
Notifications
You must be signed in to change notification settings - Fork 2
/
webster.c
222 lines (187 loc) · 4.61 KB
/
webster.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Pattern matching on words in a dictionary.
* Patterns are entered as command arguments and results are
* displayed in this window.
*
* Robert W. Baldwin, December 1984.
*/
#include <stdio.h>
#include "window.h"
#include "terminal.h"
#include "layout.h"
#include "specs.h"
#define DICTNAME "/usr/dict/words"
#define DICTVAR "DICTIONARY" /* Name of shell var. */
#define WEBHELP "Word match is invoked using the 'lookup' command"
#define TOPINDEX 2
#define BOTINDEX (WEBHEIGHT-2)
/* Forward declarations */
int wordsim();
int web_first();
void webgraphics();
/* Strings for outlining the window.
*/
char webhead[] = "| Word Match";
char webpane[] = "|";
char webbot[] = "`-------------------------";
/* Window for the list of matches. */
displine webaline[WEBHEIGHT]; /* Display lines for the history. */
displine *weblines[WEBHEIGHT+1]; /* Pointers to them plus NULL. */
twindow webster = {
WEBROW,WEBCOL, /* Origin. */
WEBHEIGHT,WEBWIDTH, /* Height and width. */
1,1, /* Initial (relative) cursor pos. */
NULL, /* No private data. */
web_first, /* Firstime = accept cursor pos. */
wl_noop, /* Lasttime = do nothing. */
wl_twdraw, /* Default draw routine. */
dokey, /* Default keystroke handler. */
arwktab, /* Basic arrow keystroke handler. */
weblines,
};
/* Make a window with a title and partial outline.
*/
gwindow *(iwebster())
{
int i;
displine *line;
webgraphics(webhead);
webgraphics(webpane);
webgraphics(webbot);
for (i = 0 ; i < WEBHEIGHT ; i++) {
line = &webaline[i];
line->worg_row = webster.worg_row + i;
line->worg_col = webster.worg_col;
line->wheight = 1;
line->wwidth = webster.wwidth;
line->wcur_row = 1;
line->wcur_col = 1;
line->wfirst = wl_noop;
line->wlast = wl_noop;
line->wredraw = wl_dldraw;
line->wkey = dokey;
line->wkeyprocs = arwktab;
line->dl_min_col = 2;
line->dl_max_col = webster.wwidth;
setadline(line, webpane);
webster.dlines[i] = line;
}
webster.dlines[i] = NULL;
line = &webaline[0];
setadline(line, webhead);
line = &webaline[i - 1];
setadline(line, webbot); /* Truncates to fit. */
return((gwindow *) &webster);
}
/* Convert a default graphic chars to fancy graphics chars
* in the given string.
*/
void webgraphics(str)
char *str;
{
for ( ; *str != 0; str++) {
switch (*str) {
default:
break;
case '|':
*str = SVERTBAR;
break;
case '-':
*str = SHORZBAR;
break;
case '`':
*str = SLLCORNER;
break;
}
}
}
/* Command to lookup word pattern in dictionary.
* The pattern contains letters and dots. The dots match any character.
*/
char *webmatch(args)
char *args;
{
char *w; /* Pattern and word pointers. */
char patbuf[MAXWIDTH+1];
char wordbuf[MAXWIDTH+1];
int i;
char *bufptr;
int row,col;
FILE *fd;
displine *line;
int nextline; /* Index of next line to put result. */
char *dictfile;
extern char *getenv();
row = rowcursor();
col = colcursor();
if ((i = sscanf(args,"%*[^:]: %s", patbuf)) != 1) {
sprintf(statmsg, "Error, got %d args not 1. From: %s", i,args);
return(statmsg); /* Beware: this returns a pointer to the stack. */
}
line = webster.dlines[TOPINDEX-1];
dlsetvar(line, patbuf);
for (nextline = TOPINDEX ; nextline <= BOTINDEX ; nextline++) {
line = webster.dlines[nextline];
dlsetvar(line, "");
}
wl_draw(&webster);
setcursor(row, col);
fflush(stdout);
dictfile = getenv(DICTVAR);
if (dictfile == NULL)
dictfile = DICTNAME;
if ((fd = fopen(dictfile, "r")) == NULL) {
sprintf(statmsg, "Could not open %s to read dictionary.",
dictfile);
return(statmsg);
}
for (nextline = TOPINDEX ; nextline <= BOTINDEX ; nextline++) {
line = webster.dlines[nextline];
while (TRUE) {
bufptr = fgets(wordbuf, MAXWIDTH+1, fd);
if (bufptr != wordbuf) {
wordbuf[0] = '\000';
goto alldone;
}
if (wordsim(patbuf, wordbuf)) break;
}
for (w=wordbuf ; *w != '\n' ; w++) {}
*w = '\000';
dlsetvar(line, wordbuf);
wl_draw(line);
setcursor(row, col);
fflush(stdout);
}
alldone:
fclose(fd);
return(NULL);
}
/* Return TRUE if the pattern matches the word
*/
int wordsim(pattern, word)
char *pattern;
char *word;
{
for ( ; *pattern != 0 && *word != 0 ; pattern++, word++) {
if (*word == '\n')
return(FALSE);
if (*pattern == '.')
continue;
if (*pattern != *word)
return(FALSE);
}
if (*pattern == 0 && *word == '\n')
return(TRUE); /* Same length */
return(FALSE);
}
/* Called when cursor enters the window.
* Clears the help message and accepts the cursor position.
*/
int web_first(w, row, col)
gwindow *w;
int row, col;
{
wl_setcur(w, row, col);
usrhelp(&user, WEBHELP);
return 0;
}