Busca os arquivos de ajuda no diretório instalado.
[cascardo/declara.git] / lib / help.c
1 /*
2  *  Copyright (C) 2015-2016  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #define _GNU_SOURCE
20 #include "sistema.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include "cmd.h"
27 #include "util.h"
28
29 #ifndef DOCDIR
30 #define DOCDIR "help"
31 #endif
32
33 static const char *helpdir = DOCDIR;
34
35 static int run_help(struct declaracao *dec, char **args, int argc)
36 {
37         int r;
38         char *filename;
39         char *basename;
40         if (argc == 1)
41                 basename = "help";
42         else if (argc == 2)
43                 basename = args[1];
44         else
45                 return -EINVAL;
46         r = asprintf(&filename, "%s/%s.txt", helpdir, basename);
47         if (r < 0) {
48                 return -errno;
49         }
50         printf("\n");
51         dumpfile(1, filename);
52         printf("\n");
53         free(filename);
54         return 0;
55 }
56
57 static struct cmd cmd_help = {
58         .name = "help",
59         .run = run_help,
60 };
61
62 static struct cmd cmd_ajuda = {
63         .name = "ajuda",
64         .run = run_help,
65 };
66
67 int help_cmd_init(void)
68 {
69         cmd_add(&cmd_help);
70         cmd_add(&cmd_ajuda);
71         return 0;
72 }