Check-in Lasker-2.2.3 tar ball from samba.org
[capablanca.git] / lasker-2.2.3 / src / help.c
1 /*
2    Copyright (C) Andrew Tridgell 2002
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8    
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13    
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 /*
19   support for the 'help' and 'ahelp' commands
20  */
21
22 #include "includes.h"
23
24 static const char *help_dir[NUM_LANGS] = {HELP_DIR, HELP_SPANISH, 
25                                           HELP_FRENCH, HELP_DANISH};
26
27
28
29 /*
30   help from a directory
31 */      
32 static int help_page(int p, const char *dir, const char *cmd)
33 {
34         int count;
35         char *filenames[1000];
36
37         /* try for help dir exact match */
38         if (psend_file(p, dir, cmd) == 0) {
39                 return COM_OK;
40         }
41         
42         /* search help dir for a partial match */
43         count = search_directory(dir, cmd, filenames, 1000);
44         if (count == 1) {
45                 psend_file(p, dir, filenames[0]);
46                 FREE(filenames[0]);
47                 return COM_OK;
48         }
49         
50         if (count > 0) {
51                 pprintf(p,"-- Matches: %u help topics --", count);
52                 display_directory(p, filenames, count);
53                 return COM_OK;
54         }
55
56         /* allow admins to use 'help' for admin pages */
57         if (dir != ADHELP_DIR && check_admin(p, ADMIN_ADMIN)) {
58                 return help_page(p, ADHELP_DIR, cmd);
59         }
60
61         pprintf(p,"No help found for topic '%s'\n", cmd);
62
63         return COM_OK;
64 }
65
66 /*
67   public help
68  */
69 int com_help(int p, param_list param)
70 {
71         struct player *pp = &player_globals.parray[p];
72         char *cmd;
73         int i, n;
74         const struct alias_type *list;
75
76         if (param[0].type == TYPE_STRING) {
77                 cmd = param[0].val.string;
78         } else {
79                 cmd = "help";
80         }
81
82         /* see if its a global alias */
83         list = alias_list_global();
84         for (i=0;list && list[i].comm_name;i++) {
85                 if (strcasecmp(cmd, list[i].comm_name) == 0) {
86                         pprintf(p,"'%s' is a global alias for '%s'\n",
87                                 cmd, list[i].alias);
88                         return COM_OK;
89                 }
90         }
91
92         /* or a personal alias */
93         list = alias_list_personal(p, &n);
94         for (i=0;i<n;i++) {
95                 if (strcasecmp(cmd, list[i].comm_name) == 0) {
96                         pprintf(p,"'%s' is a personal alias for '%s'\n",
97                                 cmd, list[i].alias);
98                         return COM_OK;
99                 }
100         }
101
102         return help_page(p, help_dir[pp->language], cmd);
103 }
104
105 /*
106   admin only help
107  */
108 int com_ahelp(int p, param_list param)
109 {
110         char *cmd;
111
112         if (param[0].type == TYPE_STRING) {
113                 cmd = param[0].val.string;
114         } else {
115                 cmd = "commands";
116         }
117
118         return help_page(p, ADHELP_DIR, cmd);
119 }
120