]> git.pond.sub.org Git - empserver/blob - src/lib/commands/info.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / lib / commands / info.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  info.c: display an info page
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Mike Wise, 1997 - added apropos and case insensitivity
33  *     Doug Hay, 1998
34  *     Steve McClure, 1998-2000
35  */
36
37 #include <config.h>
38
39 #include <errno.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <sys/stat.h>
43 #include <time.h>
44 #if !defined(_WIN32)
45 #include <dirent.h>
46 #else
47 #include <windows.h>
48 #endif
49 #include "misc.h"
50 #include "player.h"
51 #include "commands.h"
52 #include "optlist.h"
53
54 static s_char *
55 lowerit(s_char *buf, int n, s_char *orig)
56 {                               /* converts a string to lower case */
57     /* lower case output buffer */
58     /* size of output buffer */
59     /* input strig */
60     int i;
61     s_char *tmp;
62     tmp = buf;
63     memset(buf, 0, n);
64     for (i = 0; i < n && *orig; i++) {
65         *tmp++ = tolower(*orig++);
66     }
67     return buf;
68 }
69
70 #if !defined(_WIN32)
71
72 int
73 info(void)
74 {
75     s_char buf[255];
76     FILE *fp;
77     s_char *name;
78     s_char *tmp_name;
79     struct stat statb;
80     struct dirent *dp;
81     s_char filename[1024];
82     s_char last[256];
83     DIR *info_dp;
84     int nmatch = 0;
85     int width = 0;
86     char sep;
87  
88     name = player->argp[1];
89     if (name) {
90         /*
91          * don't let sneaky people go outside the info directory
92          */
93         if (NULL != (tmp_name = strrchr(name, '/')))
94             name = tmp_name + 1;
95     }
96     if (!name || !*name)
97         name = "TOP";
98  
99     snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
100     fp = fopen(filename, "r");
101     if (fp == NULL) {
102         /* may be a "partial" request.  */
103         info_dp = opendir(infodir);
104         if (info_dp == 0) {
105             pr("Can't open info dir\n");
106             logerror("Can't open info dir \"%s\"\n", infodir);
107             return RET_SYS;
108         }
109
110         while ((dp = readdir(info_dp)) != 0) {
111             if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
112                 continue;
113             nmatch++;
114             if (nmatch == 1) {
115                 snprintf(last, sizeof(last), "%s", dp->d_name);
116             } else {
117                 if (nmatch == 2) {
118                     pr("`%s' is ambiguous.  The following topics match:\n%s",
119                         name, last);
120                     width = strlen(last);
121                 }
122                 width += 2 + strlen(dp->d_name);
123                 sep = ' ';
124                 if (width > 75) {
125                     sep = '\n';
126                     width = strlen(dp->d_name);
127                 }
128                 pr(",%c%s", sep, dp->d_name);
129             }
130         }
131         closedir(info_dp);
132         if (nmatch == 0) {
133             pr("Sorry, there is no info on %s\n", name);
134             return RET_FAIL;
135         } else if (nmatch > 1) {
136             pr(".\n");
137             return RET_FAIL;
138         }
139         snprintf(filename, sizeof(filename), "%s/%s", infodir,
140                  last);
141         fp = fopen(filename, "r");
142         if (fp == NULL) {
143             pr("Error reading info file for %s\n", name);
144             logerror("Cannot open for \"%s\" info file (%s)",
145                      filename, strerror(errno));
146             return RET_FAIL;
147         }
148     }
149     if (fstat(fileno(fp), &statb) < 0) {
150         pr("Error reading info file for %s\n", name);
151         logerror("Cannot fstat for \"%s\" info file (%s)",
152                  filename, strerror(errno));
153         fclose(fp);
154         return RET_SYS;
155     }
156     if ((statb.st_mode & S_IFREG) == 0) {
157         pr("Error reading info file for %s\n", name);
158         logerror("The info file \"%s\" is not regular file\n", filename);
159         fclose(fp);
160         return RET_SYS;
161     }
162     pr("Information on:  %s    Last modification date: %s",
163        name, ctime(&statb.st_mtime));
164     while (fgets(buf, sizeof(buf), fp) != 0)
165         pr("%s", buf);
166     (void)fclose(fp);
167     return RET_OK;
168 }
169
170 int
171 apro(void)
172 {
173     FILE *fp;
174     s_char *name, *lbp;
175     s_char *fbuf;
176     s_char *lbuf;
177     struct dirent *dp;
178     s_char filename[1024];
179     DIR *info_dp;
180     long nf, nhf, nl, nlhl, nhl, nll;
181     int alreadyhit;
182     int lhitlim;
183     struct stat statb;
184
185     if (player->argp[1] == 0 || !*player->argp[1]) {
186         pr("Apropos what?\n");
187         return RET_FAIL;
188     }
189
190     lhitlim = 100;
191     if (player->argp[2]) {
192         lhitlim = atoi(player->argp[2]);
193         if (lhitlim <= 0)
194             lhitlim = 100;
195     }
196
197     info_dp = opendir(infodir);
198     if (info_dp == NULL) {
199         pr("Can't open info dir \n");
200         logerror("Can't open info dir \"%s\"", infodir);
201         return RET_SYS;
202     }
203
204     fbuf = malloc(256);
205     lbuf = malloc(256);
206     lbp = malloc(256);
207
208     /*
209      *  lower case search string into lbp
210      */
211     name = player->argp[1];
212     lowerit(lbp, 256, name);
213
214     /*
215      *  search
216      */
217     nf = nhf = nl = nhl = 0;
218     while ((dp = readdir(info_dp)) != 0) {
219         snprintf(filename, sizeof(filename), "%s/%s", infodir,
220             dp->d_name);
221         fp = fopen(filename, "r");
222         alreadyhit = 0;
223         nll = nlhl = 0;
224         if (fp != NULL) {
225             if (fstat(fileno(fp), &statb) < 0) {
226                 logerror("Cannot stat for \"%s\" info file (%s)",
227                          filename, strerror(errno));
228                 fclose(fp);
229                 continue;
230             }
231             if ((statb.st_mode & S_IFREG) == 0) {
232                 logerror("The info file \"%s\" is not regular file\n",
233                          filename);
234                 fclose(fp);
235                 continue;
236             }
237             while (fgets(fbuf, 256, fp)) {
238                 lowerit(lbuf, 256, fbuf);
239                 if (strstr(lbuf, lbp)) {
240                     if (!alreadyhit) {
241                         pr("*** %s ***\n", dp->d_name);
242                         alreadyhit = 1;
243                         nhf++;
244                     }
245                     fbuf[74] = '\n';
246                     fbuf[75] = 0;
247                     pr("   %s", fbuf);
248                     nlhl++;
249                     /*
250                      * break if too many lines
251                      */
252                     if ((nhl + nlhl) > lhitlim)
253                         break;
254                 }
255                 nll++;
256             }
257             fclose(fp);
258         }
259         nhl += nlhl;
260         nl += nll;
261         nf++;
262         if (nhl > lhitlim)
263             break;
264     }
265     closedir(info_dp);
266
267     free(fbuf);
268     free(lbuf);
269     free(lbp);
270
271     if ((nhl) > lhitlim) {
272         pr("Limit of %d lines exceeded\n", lhitlim);
273     }
274     pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
275        name, nhf, nf, nhl, nl);
276     return RET_OK;
277 }
278
279 #else
280
281 int
282 info(void)
283 {
284     s_char buf[255];
285     FILE *fp;
286     s_char *name;
287     s_char *tmp_name;
288     s_char filename[1024];
289     s_char last[256];
290     int nmatch = 0;
291     int width = 0;
292     char sep;
293
294     name = player->argp[1];
295     if (name) {
296         /*
297          * don't let sneaky people go outside the info directory
298          */
299         if (NULL != (tmp_name = strrchr(name, '/')))
300             name = tmp_name + 1;
301         if (NULL != (tmp_name = strrchr(name, '\\')))
302             name = tmp_name + 1;
303         if (NULL != (tmp_name = strrchr(name, ':')))
304             name = tmp_name + 1;
305     }
306     if (!name || !*name)
307         name = "TOP";
308
309     _snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
310     fp = fopen(filename, "rb");
311     if (fp == NULL) {
312         /* may be a "partial" request.  */
313         HANDLE hDir;
314         WIN32_FIND_DATA fData;
315         strcat(filename, "*");
316         hDir = FindFirstFile(filename, &fData);
317         if (hDir == INVALID_HANDLE_VALUE) {
318             switch (GetLastError()) {
319             case ERROR_FILE_NOT_FOUND:
320                 pr("Sorry, there is no info on %s\n", name);
321                 return RET_FAIL;
322                 break;
323             case ERROR_PATH_NOT_FOUND:
324                 pr("Can't open info dir\n");
325                 logerror("Can't open info dir \"%s\"", infodir);
326                 break;
327             default:
328                 pr("Error reading info dir\n");
329                 logerror("Error (%lu) reading info dir(%s)\\file(%s)",
330                     GetLastError(), infodir, filename);
331             }
332             return RET_SYS;
333         }
334         do {
335             if ((fData.dwFileAttributes != (DWORD)-1) &&
336                 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
337                  (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
338                  (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
339                 (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
340                 nmatch++;
341                 if (nmatch == 1) {
342                     _snprintf(last, sizeof(last), "%s", fData.cFileName);
343                 } else {
344                     if (nmatch == 2) {
345                         pr("`%s' is ambiguous.  The following topics match:\n%s",
346                             name, last);
347                         width = strlen(last);
348                     }
349                     width += 2 + strlen(fData.cFileName);
350                     sep = ' ';
351                     if (width > 75) {
352                         sep = '\n';
353                         width = strlen(fData.cFileName);
354                     }
355                     pr(",%c%s", sep, fData.cFileName);
356                 }
357             }
358         } while (FindNextFile(hDir, &fData));
359         FindClose(hDir);
360         if (nmatch == 0) {
361             pr("Sorry, there is no info on %s\n", name);
362             return RET_FAIL;
363         } else if (nmatch > 1) {
364             pr(".\n");
365             return RET_FAIL;
366         }
367         _snprintf(filename, sizeof(filename), "%s/%s",
368                   infodir, last);
369         fp = fopen(filename, "rb");
370         if (fp == NULL) {
371             pr("Error reading info file for %s\n", name);
372             logerror("Cannot open for \"%s\" info file (%s)",
373                      filename, strerror(errno));
374             return RET_FAIL;
375         }
376     }
377     else {
378         DWORD fAttrib = GetFileAttributes(filename);
379         if ((fAttrib == (DWORD)-1) || /* INVALID_FILE_ATTRIBUTES */
380             ((fAttrib != FILE_ATTRIBUTE_NORMAL) &&
381              (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
382              (fAttrib != FILE_ATTRIBUTE_READONLY))) {
383             pr("Error reading info file for %s\n", name);
384             logerror("The info file \"%s\" is not regular file\n",
385                      filename);
386             fclose(fp);
387             return RET_SYS;
388         }
389     }
390
391     pr("Information on:  %s", name);
392     while (fgets(buf, sizeof(buf), fp) != 0)
393         pr("%s", buf);
394     (void)fclose(fp);
395     return RET_OK;
396 }
397
398 int
399 apro(void)
400 {
401     HANDLE hDir;
402     WIN32_FIND_DATA fData;
403     FILE *fp;
404     s_char *name, *lbp;
405     s_char *fbuf;
406     s_char *lbuf;
407     s_char filename[1024];
408     long nf, nhf, nl, nlhl, nhl, nll;
409     int alreadyhit;
410     int lhitlim;
411
412     if (player->argp[1] == 0 || !*player->argp[1]) {
413         pr("Apropos what?\n");
414         return RET_FAIL;
415     }
416
417     lhitlim = 100;
418     if (player->argp[2]) {
419         lhitlim = atoi(player->argp[2]);
420         if (lhitlim <= 0)
421             lhitlim = 100;
422     }
423
424     _snprintf(filename, sizeof(filename), "%s\\*",infodir);
425     hDir = FindFirstFile(filename, &fData);
426     if (hDir == INVALID_HANDLE_VALUE) {
427         if (GetLastError() == ERROR_PATH_NOT_FOUND) {
428             pr("Can't open info dir\n");
429             logerror("Can't open info dir \"%s\"", infodir);
430         } else {
431             pr("Error reading info dir\n");
432             logerror("Error (%lu) reading info dir(%s)\\file(%s)",
433                 GetLastError(), infodir, filename);
434         }
435         return RET_SYS;
436     }
437
438     fbuf = malloc(256);
439     lbuf = malloc(256);
440     lbp = malloc(256);
441
442     /*
443      *  lower case search string into lbp
444      */
445     name = player->argp[1];
446     lowerit(lbp, 256, name);
447
448     /*
449      *  search
450      */
451     nf = nhf = nl = nhl = 0;
452     do {
453         if ((fData.dwFileAttributes != (DWORD)-1) &&
454             ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
455              (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
456              (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
457             _snprintf(filename, sizeof(filename), "%s\\%s", infodir,
458                       fData.cFileName);
459             fp = fopen(filename, "rb");
460             alreadyhit = 0;
461             nll = nlhl = 0;
462             if (fp != NULL) {
463                 while (fgets(fbuf, 256, fp)) {
464                     lowerit(lbuf, 256, fbuf);
465                     if (strstr(lbuf, lbp)) {
466                         if (!alreadyhit) {
467                             pr("*** %s ***\n", fData.cFileName);
468                             alreadyhit = 1;
469                             nhf++;
470                         }
471                         fbuf[74] = '\n';
472                         fbuf[75] = 0;
473                         pr("   %s", fbuf);
474                         nlhl++;
475                         /*
476                          * break if too many lines
477                          */
478                         if ((nhl + nlhl) > lhitlim)
479                             break;
480                     }
481                     nll++;
482                 }
483                 fclose(fp);
484             }
485             nhl += nlhl;
486             nl += nll;
487             nf++;
488             if (nhl > lhitlim)
489                 break;
490         }
491     } while (FindNextFile(hDir, &fData));
492     FindClose(hDir);
493
494     free(fbuf);
495     free(lbuf);
496     free(lbp);
497
498     if ((nhl) > lhitlim) {
499         pr("Limit of %d lines exceeded\n", lhitlim);
500     }
501     pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
502        name, nhf, nf, nhl, nl);
503     return RET_OK;
504 }
505
506 #endif