Adiciona ajuda curta para ajuda.
[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 help_cmds(struct declaracao *dec)
36 {
37         void *iter = NULL;
38         struct cmd *cmd;
39         printf("\n");
40         while ((cmd = cmd_next(&iter)) != NULL) {
41                 printf("%s - ", cmd->name);
42                 if (cmd->help)
43                         printf("%s", cmd->help);
44                 printf("\n");
45         }
46         printf("\n");
47 }
48
49 static int run_help(struct declaracao *dec, char **args, int argc)
50 {
51         int r;
52         char *filename;
53         char *basename;
54         if (argc == 1)
55                 return help_cmds(dec);
56         else if (argc == 2)
57                 basename = args[1];
58         else
59                 return -EINVAL;
60         r = asprintf(&filename, "%s/%s.txt", helpdir, basename);
61         if (r < 0) {
62                 return -errno;
63         }
64         printf("\n");
65         dumpfile(1, filename);
66         printf("\n");
67         free(filename);
68         return 0;
69 }
70
71 static struct cmd cmd_help = {
72         .name = "help",
73         .run = run_help,
74         .help = "mostra ajuda de comandos",
75 };
76
77 static struct cmd cmd_ajuda = {
78         .name = "ajuda",
79         .run = run_help,
80         .help = "mostra ajuda de comandos",
81 };
82
83 int help_cmd_init(void)
84 {
85         cmd_add(&cmd_help);
86         cmd_add(&cmd_ajuda);
87         return 0;
88 }