author | Mikael Berthe <mikael@lilotux.net> |
Fri, 29 Feb 2008 14:55:55 +0100 | |
changeset 1443 | 0623d694a77f |
parent 1414 | 366ef500c522 |
child 1599 | dcd5d4c75199 |
permissions | -rw-r--r-- |
868 | 1 |
/* |
2 |
* help.c -- Help command |
|
3 |
* |
|
1414 | 4 |
* Copyright (C) 2006-2008 Mikael Berthe <mikael@lilotux.net> |
868 | 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" |
|
892
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
31 |
#include "screen.h" |
868 | 32 |
|
33 |
#define DEFAULT_LANG "en" |
|
34 |
||
35 |
// get_lang() |
|
36 |
// Return the language code string (a 2-letters string). |
|
37 |
static const char *get_lang(void) { |
|
38 |
static const char *lang_str = DEFAULT_LANG; |
|
882
c3c2f7a0c5d5
autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents:
877
diff
changeset
|
39 |
#ifdef DATA_DIR |
868 | 40 |
static char lang[3]; |
41 |
const char *opt_l; |
|
42 |
opt_l = settings_opt_get("lang"); |
|
43 |
if (opt_l && strlen(opt_l) == 2 && isalpha(opt_l[0]) && isalpha(opt_l[1])) { |
|
44 |
strncpy(lang, opt_l, sizeof(lang)); |
|
45 |
mc_strtolower(lang); |
|
46 |
lang_str = lang; |
|
47 |
} |
|
882
c3c2f7a0c5d5
autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents:
877
diff
changeset
|
48 |
#endif /* DATA_DIR */ |
868 | 49 |
return lang_str; |
50 |
} |
|
51 |
||
52 |
// help_process(string) |
|
53 |
// Display help about the "string" command. |
|
54 |
// If string is null, display general help. |
|
55 |
// Return 0 in case of success. |
|
56 |
int help_process(char *string) |
|
57 |
{ |
|
882
c3c2f7a0c5d5
autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents:
877
diff
changeset
|
58 |
#ifndef DATA_DIR |
868 | 59 |
scr_LogPrint(LPRINT_NORMAL, "Help isn't available."); |
60 |
return -1; |
|
61 |
#else |
|
62 |
const char *lang; |
|
63 |
FILE *fp; |
|
64 |
char *helpfiles_dir, *filename; |
|
65 |
char *data; |
|
66 |
const int datasize = 4096; |
|
892
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
67 |
int linecount = 0; |
868 | 68 |
char *p; |
69 |
||
70 |
// Check string is ok |
|
71 |
for (p = string; p && *p; p++) { |
|
72 |
if (!isalnum(*p) && *p != '_' && *p != '-') { |
|
73 |
scr_LogPrint(LPRINT_NORMAL, "Cannot find help (invalid keyword)."); |
|
74 |
return 1; |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
// Look for help file |
|
79 |
lang = get_lang(); |
|
882
c3c2f7a0c5d5
autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents:
877
diff
changeset
|
80 |
helpfiles_dir = g_strdup_printf("%s/mcabber/help", DATA_DIR); |
868 | 81 |
if (string && *string) { |
82 |
p = g_strdup(string); |
|
83 |
mc_strtolower(p); |
|
84 |
filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, lang, p); |
|
85 |
g_free(p); |
|
86 |
} else |
|
87 |
filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, lang); |
|
88 |
||
89 |
fp = fopen(filename, "r"); |
|
90 |
g_free(filename); |
|
91 |
g_free(helpfiles_dir); |
|
92 |
||
93 |
if (!fp) { |
|
94 |
scr_LogPrint(LPRINT_NORMAL, "No help found."); |
|
95 |
return -1; |
|
96 |
} |
|
97 |
||
98 |
data = g_new(char, datasize); |
|
99 |
while (!feof(fp)) { |
|
100 |
if (fgets(data, datasize, fp) == NULL) break; |
|
101 |
// Strip trailing newline |
|
102 |
for (p = data; *p; p++) ; |
|
103 |
if (p > data) |
|
104 |
p--; |
|
105 |
if (*p == '\n' || *p == '\r') |
|
106 |
*p = '\0'; |
|
107 |
// Displaty the help line |
|
108 |
scr_LogPrint(LPRINT_NORMAL, "%s", data); |
|
892
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
109 |
linecount++; |
868 | 110 |
} |
111 |
fclose(fp); |
|
112 |
g_free(data); |
|
113 |
||
892
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
114 |
if (linecount) { |
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
115 |
scr_setmsgflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE); |
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
116 |
update_roster = TRUE; |
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
117 |
} |
94bb9e40e40b
Set the pending message flag on the status buffer when using /help
Mikael Berthe <mikael@lilotux.net>
parents:
882
diff
changeset
|
118 |
|
868 | 119 |
return 0; |
882
c3c2f7a0c5d5
autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents:
877
diff
changeset
|
120 |
#endif /* DATA_DIR */ |
868 | 121 |
} |
122 |
||
123 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |