Adiciona comando help.
[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 static const char *helpdir = "help";
30
31 static int run_help(struct declaracao *dec, char **args, int argc)
32 {
33         int r;
34         char *filename;
35         char *basename;
36         if (argc == 1)
37                 basename = "help";
38         else if (argc == 2)
39                 basename = args[1];
40         else
41                 return -EINVAL;
42         r = asprintf(&filename, "%s/%s.txt", helpdir, basename);
43         if (r < 0) {
44                 return -errno;
45         }
46         printf("\n");
47         dumpfile(1, filename);
48         printf("\n");
49         free(filename);
50         return 0;
51 }
52
53 static struct cmd cmd_help = {
54         .name = "help",
55         .run = run_help,
56 };
57
58 static struct cmd cmd_ajuda = {
59         .name = "ajuda",
60         .run = run_help,
61 };
62
63 int help_cmd_init(void)
64 {
65         cmd_add(&cmd_help);
66         cmd_add(&cmd_ajuda);
67         return 0;
68 }