File: learn.c

package info (click to toggle)
lie 2.2.2%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,000 kB
  • ctags: 1,801
  • sloc: ansic: 12,761; yacc: 395; makefile: 150; sh: 4
file content (209 lines) | stat: -rw-r--r-- 5,827 bytes parent folder | download | duplicates (3)
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
#include "lie.h"

#define NRECS 80 /* Just enough for LiE 2.0 */
#define NCOLS 3

static char *directory_table[REPORT_LEN];
static int directory_pt = 0;

extern char *getenv();

/* Learn reads the indexfile belonging to the learnfile,
   searching it for keywords containing the given name as
   substring. For each match, the corresponding part of the
   learn file is copied to the result string.
*/

static void add_directory_table(char* name)
{   int i = 0, j = directory_pt;
    if (directory_pt >= REPORT_LEN) error("Learn index too large.\n");
    while (i < directory_pt && strcmp(directory_table[i], name) < 0)
	i++;
    if (i < directory_pt && !strcmp(directory_table[i], name))
	return;
    while (j > i) {
	directory_table[j] = directory_table[j - 1];
	j--;
    }
    if (!(*name)) error("Null name %d\n",i);
    directory_table[i] = name;
    directory_pt++;
}

static void printdirectory(void)
{
    int i;
    for (i = 0; i < directory_pt - 1; i++) {
	if(strlen(directory_table[i]) > KEYWORDLEN)
	  error("%s too long.\n",directory_table[i]);
	if (i % NCOLS == 0) Printf("\n");
	Printf("%-24.24s ", directory_table[i]);
    }
    Printf("\n");
}

boolean substring(key,name) char* name,* key;
{ char* keypt;
  if (!*name) return false;
  keypt = strchr(key,name[0]);
  while (keypt && strncmp(keypt,name,strlen(name)))
    keypt = strchr(keypt+1,name[0]);
  return keypt!=NULL;
}

void build_directory(learn,nitems) learn_index_tp* learn; long nitems;
{
    long i=0;
    for (i=0;i<nitems;i++) {
	if (!is_operator(learn[i].keyword)){
	    add_directory_table(learn[i].keyword);
	}
    }
}

/*
static learn_index_tp learn[NRECS];
*/

boolean Learn(name) char* name;
{
    FILE *indexpt,*learnpt;
    static long nitems;
    entry i,totalsize=0;

    short foundlearn[NRECS]; /* indexes of learn which satisfy search
			      criterium */
    short foundpt = 0;
    char *result;
    char *resultpt;

    char tmpfile[L_tmpnam];

    char *t = name;

    static learn_index_tp  *learn; 
    if (!learn)
	learn = (learn_index_tp*)
	malloc((unsigned long) (sizeof(learn_index_tp) * NRECS));
    if (!learn) error("No memory available.\n");

/***************************************************************
*  open index file					       *
***************************************************************/

    if (!*learnfil || !*learnind)
    error("Learn command is not implemented.\n");
    indexpt = fopen(learnind,readmode);
    if (!indexpt) error("File %s cannot be opened.\n",learnind);

/***************************************************************
*  read index						       *
***************************************************************/

    if (!nitems)
	nitems = fread(learn,sizeof(learn_index_tp),NRECS,indexpt);
    fclose(indexpt);
    if (nitems >= NRECS) error("Indexfile too large.\n");
    if (*name=='\0' || strcmp(name,"index")==0) {
	if (directory_pt == 0) build_directory(learn,nitems);
	printdirectory();
	return true;
    }


/***************************************************************
*  Make uppercase                                              *
***************************************************************/

    while (*t) {
	*t = isupper(*t)? tolower(*t): *t;
	t++;
    }

/***************************************************************
*  Search						       *
***************************************************************/

    foundpt = 0;
    for (i=0;i<nitems;i++) {
	if (substring(learn[i].keyword,name)) foundlearn[foundpt++] = i;
    }
    if (!foundpt) /* error("Info about %s is not available.\n",name)*/
	return false;

    tmpnam(tmpfile);
    cur_out = fopen(tmpfile, "w");
    if (cur_out==NULL) cur_out = stdout;

/***************************************************************
*  Compute size output string				       *
***************************************************************/

    for (i=0; i<foundpt; i++) 
	totalsize += (long) learn[foundlearn[i]].size;
    result = (char*) malloc(totalsize+foundpt*(KEYWORDLEN+2)+sizeof(char));
    if (!result) error("No memory available.\n");

/***************************************************************
*  Fill text string					       *
***************************************************************/

    resultpt = result;
    learnpt = fopen(learnfil,readmode);
    if (!learnpt) error("File %s cannot be opened.\n",learnfil);
    for (i=0;i<foundpt;i++) {
    /*
	sprintf(resultpt,"%s:\n",
	learn[foundlearn[i]].keyword);
	resultpt = resultpt +
	(long) strlen(learn[foundlearn[i]].keyword)+ (long) 2;
    */
	fseek(learnpt,learn[foundlearn[i]].start,0);
	fread(resultpt,sizeof(char),
	learn[foundlearn[i]].size,learnpt);
	resultpt = resultpt + (long) learn[foundlearn[i]].size;
    }
    *resultpt= '\0';
    Printf("%s\n",result);
    free(result);
    fclose(learnpt);
    if (cur_out == stdout) return true;

    fclose(cur_out);
    invoke_prog(pager,tmpfile);
    remove(tmpfile);
    cur_out = stdout;
    return true;
}

entry exec_learn(char *name)
{
    FILE *indexpt;
    short nitems,i;
    char *t = name;

    learn_index_tp learn[NRECS];

    /* opening of the files */
    if (!*learnfil || !*learnind)
    error("Exec command is not implemented.\n");
    indexpt = fopen(learnind,readmode);
    if (!indexpt)
	error("File %s cannot be opened.\n",learnind);
    /* end open files */
    rewind(indexpt);
    nitems = fread(learn,sizeof(learn_index_tp),NRECS,indexpt);
    fclose(indexpt);
    if (nitems > NRECS) error("Indexfile too large.\n");
    i = 0;
    /* printf("learn:%s name: %s\n",learn[i].keyword,name); */
    /* Convert name to lowercase */
    while (*t) {
	*t = isupper(*t)? tolower(*t): *t;
	t++;
    }
    /* Lineair search */
    while(i < NRECS && strcmp(learn[i].keyword,name)) i++;
    if (i==NRECS) return -1; /* Not found */
    return learn[i].start;
}