Adiciona comando help.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 10 Dec 2016 13:24:14 +0000 (11:24 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 10 Dec 2016 13:24:14 +0000 (11:24 -0200)
O comando help lê arquivos texto e imprime seu conteúdo na saída padrão.
Assim, a documentação pode ser escrita em simples arquivos.

lib/Makefile.am
lib/help.c [new file with mode: 0644]
lib/help.h [new file with mode: 0644]
src/declara.c

index 1410b20..1d26720 100644 (file)
@@ -9,6 +9,7 @@ libreceita_la_SOURCES = declaracao.c declaracao.h \
        base.c base.h \
        list.c list.h \
        util.c util.h \
+       help.c help.h \
        contribuinte.c contribuinte.h \
        rendimento.c rendimento.h \
        isento.c isento.h \
diff --git a/lib/help.c b/lib/help.c
new file mode 100644 (file)
index 0000000..66573ba
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ *  Copyright (C) 2015-2016  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#define _GNU_SOURCE
+#include "sistema.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include "cmd.h"
+#include "util.h"
+
+static const char *helpdir = "help";
+
+static int run_help(struct declaracao *dec, char **args, int argc)
+{
+       int r;
+       char *filename;
+       char *basename;
+       if (argc == 1)
+               basename = "help";
+       else if (argc == 2)
+               basename = args[1];
+       else
+               return -EINVAL;
+       r = asprintf(&filename, "%s/%s.txt", helpdir, basename);
+       if (r < 0) {
+               return -errno;
+       }
+       printf("\n");
+       dumpfile(1, filename);
+       printf("\n");
+       free(filename);
+       return 0;
+}
+
+static struct cmd cmd_help = {
+       .name = "help",
+       .run = run_help,
+};
+
+static struct cmd cmd_ajuda = {
+       .name = "ajuda",
+       .run = run_help,
+};
+
+int help_cmd_init(void)
+{
+       cmd_add(&cmd_help);
+       cmd_add(&cmd_ajuda);
+       return 0;
+}
diff --git a/lib/help.h b/lib/help.h
new file mode 100644 (file)
index 0000000..109e653
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ *  Copyright (C) 2015-2016  Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _HELP_H
+#define _HELP_H
+
+int help_cmd_init(void);
+
+#endif
index 54e1abb..d54451c 100644 (file)
@@ -38,6 +38,7 @@
 #include "dependente.h"
 #include "calcula.h"
 #include "gera.h"
+#include "help.h"
 
 static int readline_support = 1;
 
@@ -134,6 +135,7 @@ int main(int argc, char **argv)
        calcula_cmd_init();
        gera_cmd_init();
        sistema_cmd_init();
+       help_cmd_init();
 
        if (argc > 1)
                filename = argv[1];