annotate mcabber/src/help.c @ 1509:189ffdd944b4

Add a small Coding Style document
author Mikael Berthe <mikael@lilotux.net>
date Sun, 31 Aug 2008 15:21:49 +0200
parents 366ef500c522
children dcd5d4c75199
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
1 /*
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
2 * help.c -- Help command
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
3 *
1414
366ef500c522 Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
4 * Copyright (C) 2006-2008 Mikael Berthe <mikael@lilotux.net>
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
5 *
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or (at
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
9 * your option) any later version.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
10 *
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful, but
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
14 * General Public License for more details.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
15 *
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
17 * along with this program; if not, write to the Free Software
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
19 * USA
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
20 */
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
21
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
22 #include <stdio.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
23 #include <stdlib.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
24 #include <string.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
25 #include <ctype.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
26 #include <glib.h>
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
27
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
28 #include "settings.h"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
29 #include "logprint.h"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
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
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
32
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
33 #define DEFAULT_LANG "en"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
34
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
35 // get_lang()
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
36 // Return the language code string (a 2-letters string).
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
37 static const char *get_lang(void) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
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
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
40 static char lang[3];
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
41 const char *opt_l;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
42 opt_l = settings_opt_get("lang");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
43 if (opt_l && strlen(opt_l) == 2 && isalpha(opt_l[0]) && isalpha(opt_l[1])) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
44 strncpy(lang, opt_l, sizeof(lang));
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
45 mc_strtolower(lang);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
46 lang_str = lang;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
47 }
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
48 #endif /* DATA_DIR */
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
49 return lang_str;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
50 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
51
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
52 // help_process(string)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
53 // Display help about the "string" command.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
54 // If string is null, display general help.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
55 // Return 0 in case of success.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
56 int help_process(char *string)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
57 {
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
58 #ifndef DATA_DIR
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
59 scr_LogPrint(LPRINT_NORMAL, "Help isn't available.");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
60 return -1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
61 #else
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
62 const char *lang;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
63 FILE *fp;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
64 char *helpfiles_dir, *filename;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
65 char *data;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
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
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
68 char *p;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
69
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
70 // Check string is ok
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
71 for (p = string; p && *p; p++) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
72 if (!isalnum(*p) && *p != '_' && *p != '-') {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
73 scr_LogPrint(LPRINT_NORMAL, "Cannot find help (invalid keyword).");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
74 return 1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
75 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
76 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
77
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
78 // Look for help file
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
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
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
81 if (string && *string) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
82 p = g_strdup(string);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
83 mc_strtolower(p);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
84 filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, lang, p);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
85 g_free(p);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
86 } else
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
87 filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, lang);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
88
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
89 fp = fopen(filename, "r");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
90 g_free(filename);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
91 g_free(helpfiles_dir);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
92
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
93 if (!fp) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
94 scr_LogPrint(LPRINT_NORMAL, "No help found.");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
95 return -1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
96 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
97
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
98 data = g_new(char, datasize);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
99 while (!feof(fp)) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
100 if (fgets(data, datasize, fp) == NULL) break;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
101 // Strip trailing newline
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
102 for (p = data; *p; p++) ;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
103 if (p > data)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
104 p--;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
105 if (*p == '\n' || *p == '\r')
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
106 *p = '\0';
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
107 // Displaty the help line
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
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
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
110 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
111 fclose(fp);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
112 g_free(data);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
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
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
119 return 0;
882
c3c2f7a0c5d5 autoconf: use datadir, not datarootdir
Mikael Berthe <mikael@lilotux.net>
parents: 877
diff changeset
120 #endif /* DATA_DIR */
868
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
121 }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
122
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
123 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */