comparison mcabber/mcabber/help.c @ 1668:41c26b7d2890

Install mcabber headers * Change mcabber headers naming scheme * Move 'src/' -> 'mcabber/' * Add missing include <mcabber/config.h>'s * Create and install clean config.h version in 'include/' * Move "dirty" config.h version to 'mcabber/' * Add $(top_srcdir) to compiler include path * Update modules HOWTO
author Myhailo Danylenko <isbear@ukrpost.net>
date Mon, 18 Jan 2010 15:36:19 +0200
parents mcabber/src/help.c@cd81806b5947
children e489ead6574a
comparison
equal deleted inserted replaced
1667:8af0e0ad20ad 1668:41c26b7d2890
1 /*
2 * help.c -- Help command
3 *
4 * Copyright (C) 2006-2009 Mikael Berthe <mikael@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 #include "screen.h"
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;
39 #ifdef DATA_DIR
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 }
48 #endif /* DATA_DIR */
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 {
58 #ifndef DATA_DIR
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;
67 int linecount = 0;
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();
80 helpfiles_dir = g_strdup_printf("%s/mcabber/help", DATA_DIR);
81 p = NULL;
82
83 if (string && *string) {
84 p = g_strdup(string);
85 mc_strtolower(p);
86 filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, lang, p);
87 } else
88 filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, lang);
89
90 fp = fopen(filename, "r");
91
92 if (!(fp) && (g_strcmp0(lang, DEFAULT_LANG)) ) {
93 g_free(filename);
94 if (p)
95 filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, DEFAULT_LANG, p);
96 else
97 filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, DEFAULT_LANG);
98
99 fp = fopen(filename, "r");
100 }
101 g_free(p);
102 g_free(filename);
103 g_free(helpfiles_dir);
104
105 if (!fp) {
106 scr_LogPrint(LPRINT_NORMAL, "No help found.");
107 return -1;
108 }
109
110 data = g_new(char, datasize);
111 while (!feof(fp)) {
112 if (fgets(data, datasize, fp) == NULL) break;
113 // Strip trailing newline
114 for (p = data; *p; p++) ;
115 if (p > data)
116 p--;
117 if (*p == '\n' || *p == '\r')
118 *p = '\0';
119 // Displaty the help line
120 scr_LogPrint(LPRINT_NORMAL, "%s", data);
121 linecount++;
122 }
123 fclose(fp);
124 g_free(data);
125
126 if (linecount) {
127 scr_setmsgflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE);
128 update_roster = TRUE;
129 }
130
131 return 0;
132 #endif /* DATA_DIR */
133 }
134
135 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */