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