File: help.c

package info (click to toggle)
kbtin 1.0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,172 kB
  • sloc: ansic: 16,836; perl: 214; makefile: 133; sh: 92
file content (116 lines) | stat: -rw-r--r-- 3,300 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
113
114
115
116
/*****************************************************************/
/* 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"


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)
    {
        sprintf(filestring, "%s_help", tintin_exec);
        myfile = check_file(filestring);
    }
    if (!myfile)
    {
        sprintf(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==tintin_char)
        arg++;
    if (*arg)
    {
        sprintf(text, "~%s", arg);
        while (fgets(line, sizeof(line), myfile))
        {
            if (*line == '~')
            {
                if (*(line + 1) == '*')
                    break;
                if (is_abrev(text, line))
                {
                    while (fgets(line, sizeof(line), myfile))
                    {
                        if ((*line == '~')&&(*(line+1)=='~'))
                            goto end;
                        else
                        {
                            *(line + strlen(line) - 1) = '\0';
                            if (*line!='~')
                                tintin_printf(0, "%s", line);
                        }
                    }
                }
            }
        }
    }
    else
    {
        while (fgets(line, sizeof(line), myfile))
        {
            if ((*line == '~')&&(*(line+1)=='~'))
                goto end;
            else
            {
                *(line + strlen(line) - 1) = '\0';
                if (*line!='~')
                    tintin_printf(0, "%s", line);
            }
        }
    }
    tintin_printf(0, "#Sorry, no help on that word.");
end:
    fclose(myfile);
}