File: help.c

package info (click to toggle)
kbtin 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 2,300 kB
  • sloc: ansic: 18,241; perl: 165; sh: 83; makefile: 13
file content (112 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*****************************************************************/
/* functions for the #help command                               */
/*****************************************************************/
#include "tintin.h"
#include <fcntl.h>
#include "protos/globals.h"
#include "protos/print.h"
#include "protos/run.h"
#include "protos/utils.h"


#ifndef O_BINARY
# define O_BINARY 0
#endif

static FILE* check_file(const char *filestring)
{
#if COMPRESSED_HELP
    char sysfile[BUFFER_SIZE+8];
    int f;

    sprintf(sysfile, "%s%s", filestring, COMPRESSION_EXT);
    if ((f=open(sysfile, O_RDONLY|O_BINARY))==-1)
        return 0;
    return mypopen(UNCOMPRESS_CMD, false, f);
#else
    return (FILE *) fopen(filestring, "r");
#endif
}

void help_command(const char *arg, struct session *ses)
{
    FILE *myfile=NULL;
    char text[BUFFER_SIZE], line[BUFFER_SIZE], filestring[BUFFER_SIZE];

    if (strcmp(DEFAULT_FILE_DIR, "HOME"))
    {
        sprintf(filestring, "%s/KBtin_help", DEFAULT_FILE_DIR);
        myfile = check_file(filestring);
    }
#ifdef DATA_PATH
    if (!myfile)
    {
        sprintf(filestring, "%s/KBtin_help", DATA_PATH);
        myfile = check_file(filestring);
    }
#endif
    if (!myfile)
    {
        snprintf(filestring, sizeof filestring, "%s_help", tintin_exec);
        myfile = check_file(filestring);
    }
    if (!myfile)
    {
        snprintf(filestring, sizeof filestring, "%s/KBtin_help", getenv("HOME"));
        myfile = check_file(filestring);
    }
    if (!myfile)
    {
        tintin_eprintf(0, "#Help file not found - no help available.");
        tintin_eprintf(0, "#Locations checked:");
        if (strcmp(DEFAULT_FILE_DIR, "HOME"))
            tintin_eprintf(0, "#      %s/KBtin_help%s", DEFAULT_FILE_DIR,
                COMPRESSION_EXT);
#ifdef DATA_PATH
        tintin_eprintf(0, "#      %s/KBtin_help%s", DATA_PATH,
            COMPRESSION_EXT);
#endif
        tintin_eprintf(0, "#      %s_help%s", tintin_exec,
            COMPRESSION_EXT);
        tintin_eprintf(0, "#      %s/KBtin_help%s", getenv("HOME"),
            COMPRESSION_EXT);
        return;
    }

    if (!*arg)
    {
        while (fgets(line, sizeof(line), myfile))
        {
            if (line[0]=='~' && line[1]=='~')
                break;
            char *eol=strchr(line, '\n');
            if (eol)
                *eol=0;
            tintin_printf(0, "%s", line);
        }
        fclose(myfile);
        return;
    }

    if (*arg==tintin_char)
        arg++;
    sprintf(text, "~%s", arg);
    while (fgets(line, sizeof(line), myfile))
    {
        if (*line != '~' || !is_abrev(text, line))
            continue;
        while (fgets(line, sizeof(line), myfile))
        {
            if (line[0]=='~' && line[1]=='~')
                break;
            char *eol=strchr(line, '\n');
            if (eol)
                *eol=0;
            tintin_printf(0, "%s", line);
        }
        fclose(myfile);
        return;
    }
    tintin_printf(0, "#Sorry, no help on that word.");
    fclose(myfile);
}