mcabber/src/help.c
author Mikael Berthe <mikael@lilotux.net>
Thu, 25 May 2006 18:20:52 +0200
changeset 868 cefdaeb42e67
child 877 b38760d9ec2d
permissions -rw-r--r--
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="...".
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
 *
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
 * Copyright (C) 2006 Mikael Berthe <bmikael@lists.lilotux.net>
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"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
#define DEFAULT_LANG "en"
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
//  get_lang()
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
// Return the language code string (a 2-letters string).
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
static const char *get_lang(void) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
  static const char *lang_str = DEFAULT_LANG;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
#ifdef DATA_DIR
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
  static char lang[3];
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
  const char *opt_l;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
  opt_l = settings_opt_get("lang");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
  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
    43
    strncpy(lang, opt_l, sizeof(lang));
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
    mc_strtolower(lang);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
    lang_str = lang;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
  }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
#endif /* DATA_DIR */
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
  return lang_str;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
}
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
//  help_process(string)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
// Display help about the "string" command.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
// If string is null, display general help.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
// Return 0 in case of success.
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
int help_process(char *string)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
{
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
#ifndef DATA_DIR
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
  scr_LogPrint(LPRINT_NORMAL, "Help isn't available.");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
  return -1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
#else
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
  const char *lang;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
  FILE *fp;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
  char *helpfiles_dir, *filename;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
  char *data;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
  const int datasize = 4096;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
  char *p;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
  // Check string is ok
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
  for (p = string; p && *p; p++) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
    if (!isalnum(*p) && *p != '_' && *p != '-') {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
      scr_LogPrint(LPRINT_NORMAL, "Cannot find help (invalid keyword).");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
      return 1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
    }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
  }
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
  // Look for help file
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
  lang = get_lang();
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
  helpfiles_dir = g_strdup_printf("%s/mcabber/help", DATA_DIR);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
  if (string && *string) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
    p = g_strdup(string);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
    mc_strtolower(p);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
    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
    83
    g_free(p);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
  } else
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
    filename = g_strdup_printf("%s/%s/hlp.txt", helpfiles_dir, lang);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
  fp = fopen(filename, "r");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
  g_free(filename);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
  g_free(helpfiles_dir);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
  if (!fp) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
    scr_LogPrint(LPRINT_NORMAL, "No help found.");
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
    return -1;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
  }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
  data = g_new(char, datasize);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
  while (!feof(fp)) {
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
    if (fgets(data, datasize, fp) == NULL) break;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
    // Strip trailing newline
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
    for (p = data; *p; p++) ;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
    if (p > data)
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
      p--;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
    if (*p == '\n' || *p == '\r')
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
      *p = '\0';
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
    // Displaty the help line
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
    scr_LogPrint(LPRINT_NORMAL, "%s", data);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
  }
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
  fclose(fp);
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
  g_free(data);
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
  return 0;
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
#endif /* DATA_DIR */
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
}
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
cefdaeb42e67 Add online /help command
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */