changeset 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 7f056c566569
children 9ce308598c0a
files mcabber/configure.ac mcabber/src/Makefile.am mcabber/src/commands.c mcabber/src/help.c mcabber/src/help.h
diffstat 5 files changed, 139 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/configure.ac	Wed May 24 23:30:31 2006 +0200
+++ b/mcabber/configure.ac	Thu May 25 18:20:52 2006 +0200
@@ -113,6 +113,13 @@
 
 AC_DEFINE(BUILD_JABBER, 1, [build with jabber support])
 
+AC_ARG_WITH(datadir,
+    [AC_HELP_STRING(--with-datadir, Read-only data (help files))],
+    datadir=$withval, datadir="")
+if test x"${datadir}" != x""; then
+    AC_DEFINE_UNQUOTED(DATA_DIR, "${datadir}", [Data files directory])
+fi
+
 AC_ARG_ENABLE(debug,
     [AC_HELP_STRING(--enable-debug, Add development compilation options)],
     debug=$enableval, debug="")
--- a/mcabber/src/Makefile.am	Wed May 24 23:30:31 2006 +0200
+++ b/mcabber/src/Makefile.am	Thu May 25 18:20:52 2006 +0200
@@ -4,7 +4,7 @@
 		  commands.c commands.h compl.c compl.h \
 		  hbuf.c hbuf.h screen.c screen.h logprint.h \
 		  settings.c settings.h hooks.c hooks.h \
-		  histolog.c histolog.h utils.c utils.h
+		  histolog.c histolog.h utils.c utils.h help.c help.h
 
 LDADD = $(GLIB_LIBS) -lncurses -lpanel \
 	../libjabber/liblibjabber.a ../connwrap/libconnwrap.a
--- a/mcabber/src/commands.c	Wed May 24 23:30:31 2006 +0200
+++ b/mcabber/src/commands.c	Thu May 25 18:20:52 2006 +0200
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "commands.h"
+#include "help.h"
 #include "jabglue.h"
 #include "roster.h"
 #include "screen.h"
@@ -58,6 +59,7 @@
 static void do_version(char *arg);
 static void do_request(char *arg);
 static void do_event(char *arg);
+static void do_help(char *arg);
 
 // Global variable for the commands list
 static GSList *Commands;
@@ -96,7 +98,7 @@
   cmd_add("disconnect", "Disconnect from server", 0, 0, &do_disconnect);
   cmd_add("event", "Process an event", COMPL_EVENTSID, COMPL_EVENTS, &do_event);
   cmd_add("group", "Change group display settings", COMPL_GROUP, 0, &do_group);
-  //cmd_add("help", "Display some help", COMPL_CMD, 0, NULL);
+  cmd_add("help", "Display some help", COMPL_CMD, 0, &do_help);
   cmd_add("info", "Show basic info on current buddy", 0, 0, &do_info);
   cmd_add("move", "Move the current buddy to another group", COMPL_GROUPNAME,
           0, &do_move);
@@ -2218,4 +2220,9 @@
   jb_disconnect();
 }
 
+static void do_help(char *arg)
+{
+  help_process(arg);
+}
+
 /* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcabber/src/help.c	Thu May 25 18:20:52 2006 +0200
@@ -0,0 +1,116 @@
+/*
+ * help.c       -- Help command
+ *
+ * Copyright (C) 2006 Mikael Berthe <bmikael@lists.lilotux.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <glib.h>
+
+#include "settings.h"
+#include "logprint.h"
+#include "utils.h"
+
+#define DEFAULT_LANG "en"
+
+//  get_lang()
+// Return the language code string (a 2-letters string).
+static const char *get_lang(void) {
+  static const char *lang_str = DEFAULT_LANG;
+#ifdef DATA_DIR
+  static char lang[3];
+  const char *opt_l;
+  opt_l = settings_opt_get("lang");
+  if (opt_l && strlen(opt_l) == 2 && isalpha(opt_l[0]) && isalpha(opt_l[1])) {
+    strncpy(lang, opt_l, sizeof(lang));
+    mc_strtolower(lang);
+    lang_str = lang;
+  }
+#endif /* DATA_DIR */
+  return lang_str;
+}
+
+//  help_process(string)
+// Display help about the "string" command.
+// If string is null, display general help.
+// Return 0 in case of success.
+int help_process(char *string)
+{
+#ifndef DATA_DIR
+  scr_LogPrint(LPRINT_NORMAL, "Help isn't available.");
+  return -1;
+#else
+  const char *lang;
+  FILE *fp;
+  char *helpfiles_dir, *filename;
+  char *data;
+  const int datasize = 4096;
+  char *p;
+
+  // Check string is ok
+  for (p = string; p && *p; p++) {
+    if (!isalnum(*p) && *p != '_' && *p != '-') {
+      scr_LogPrint(LPRINT_NORMAL, "Cannot find help (invalid keyword).");
+      return 1;
+    }
+  }
+
+  // Look for help file
+  lang = get_lang();
+  helpfiles_dir = g_strdup_printf("%s/mcabber/help", DATA_DIR);
+  if (string && *string) {
+    p = g_strdup(string);
+    mc_strtolower(p);
+    filename = g_strdup_printf("%s/%s/hlp_%s.txt", helpfiles_dir, lang, p);
+    g_free(p);
+  } else
+    filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, lang);
+
+  fp = fopen(filename, "r");
+  g_free(filename);
+  g_free(helpfiles_dir);
+
+  if (!fp) {
+    scr_LogPrint(LPRINT_NORMAL, "No help found.");
+    return -1;
+  }
+
+  data = g_new(char, datasize);
+  while (!feof(fp)) {
+    if (fgets(data, datasize, fp) == NULL) break;
+    // Strip trailing newline
+    for (p = data; *p; p++) ;
+    if (p > data)
+      p--;
+    if (*p == '\n' || *p == '\r')
+      *p = '\0';
+    // Displaty the help line
+    scr_LogPrint(LPRINT_NORMAL, "%s", data);
+  }
+  fclose(fp);
+  g_free(data);
+
+  return 0;
+#endif /* DATA_DIR */
+}
+
+
+/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcabber/src/help.h	Thu May 25 18:20:52 2006 +0200
@@ -0,0 +1,7 @@
+#ifndef __HELP_H__
+#define __HELP_H__ 1
+
+int help_process(char *string);
+
+#endif /* __HELP_H__ */
+