comparison mcabber/src/help.c @ 868:cefdaeb42e67

Add online /help command We use help files stored in "$DATA_DIR/mcabber/help/$lang/", with $lang defaulting to "en". The DATA_DIR location can be set using configure --with-datadir="...".
author Mikael Berthe <mikael@lilotux.net>
date Thu, 25 May 2006 18:20:52 +0200
parents
children b38760d9ec2d
comparison
equal deleted inserted replaced
867:7f056c566569 868:cefdaeb42e67
1 /*
2 * help.c -- Help command
3 *
4 * Copyright (C) 2006 Mikael Berthe <bmikael@lists.lilotux.net>
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 (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
19 * USA
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <glib.h>
27
28 #include "settings.h"
29 #include "logprint.h"
30 #include "utils.h"
31
32 #define DEFAULT_LANG "en"
33
34 // get_lang()
35 // Return the language code string (a 2-letters string).
36 static const char *get_lang(void) {
37 static const char *lang_str = DEFAULT_LANG;
38 #ifdef DATA_DIR
39 static char lang[3];
40 const char *opt_l;
41 opt_l = settings_opt_get("lang");
42 if (opt_l && strlen(opt_l) == 2 && isalpha(opt_l[0]) && isalpha(opt_l[1])) {
43 strncpy(lang, opt_l, sizeof(lang));
44 mc_strtolower(lang);
45 lang_str = lang;
46 }
47 #endif /* DATA_DIR */
48 return lang_str;
49 }
50
51 // help_process(string)
52 // Display help about the "string" command.
53 // If string is null, display general help.
54 // Return 0 in case of success.
55 int help_process(char *string)
56 {
57 #ifndef DATA_DIR
58 scr_LogPrint(LPRINT_NORMAL, "Help isn't available.");
59 return -1;
60 #else
61 const char *lang;
62 FILE *fp;
63 char *helpfiles_dir, *filename;
64 char *data;
65 const int datasize = 4096;
66 char *p;
67
68 // Check string is ok
69 for (p = string; p && *p; p++) {
70 if (!isalnum(*p) && *p != '_' && *p != '-') {
71 scr_LogPrint(LPRINT_NORMAL, "Cannot find help (invalid keyword).");
72 return 1;
73 }
74 }
75
76 // Look for help file
77 lang = get_lang();
78 helpfiles_dir = g_strdup_printf("%s/mcabber/help", DATA_DIR);
79 if (string && *string) {
80 p = g_strdup(string);
81 mc_strtolower(p);
82 filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, lang, p);
83 g_free(p);
84 } else
85 filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, lang);
86
87 fp = fopen(filename, "r");
88 g_free(filename);
89 g_free(helpfiles_dir);
90
91 if (!fp) {
92 scr_LogPrint(LPRINT_NORMAL, "No help found.");
93 return -1;
94 }
95
96 data = g_new(char, datasize);
97 while (!feof(fp)) {
98 if (fgets(data, datasize, fp) == NULL) break;
99 // Strip trailing newline
100 for (p = data; *p; p++) ;
101 if (p > data)
102 p--;
103 if (*p == '\n' || *p == '\r')
104 *p = '\0';
105 // Displaty the help line
106 scr_LogPrint(LPRINT_NORMAL, "%s", data);
107 }
108 fclose(fp);
109 g_free(data);
110
111 return 0;
112 #endif /* DATA_DIR */
113 }
114
115
116 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */