From a1330946e9aeb8b1d6760ae77522f10ec28fd097 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 8 Aug 2015 19:54:27 -0300 Subject: [PATCH] =?utf8?q?Implementa=20comando=20sistema=20para=20configur?= =?utf8?q?a=C3=A7=C3=B5es.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit SO, versão do SO e da JVM e MAC address são campos do cabeçalho da declaração. O usuário deve optar por informar ou não esses dados. No momento, os dados são obtidos do arquivo de entrada ou valores padrões são utilizados. Seria interessante, no entanto, implementar outras opções, como obter os dados reais do sistema, utilizar valores de sistemas livres, valores aleatórios e valores conformantes com o que o software privativo utiliza. O padrão também deveria ser configurado em tempo de compilação. --- lib/Makefile.am | 1 + lib/declaracao.c | 2 + lib/declaracao.h | 2 + lib/gera.c | 9 ++-- lib/sistema.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++ lib/sistema.h | 44 +++++++++++++++++++ src/declara.c | 1 + 7 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 lib/sistema.c create mode 100644 lib/sistema.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 68b9f89..9efbb78 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,6 +3,7 @@ libreceita_la_SOURCES = declaracao.c declaracao.h \ token.c token.h \ cmd.c cmd.h pmhash.c pmhash.h \ totais.c totais.h \ + sistema.c sistema.h \ calcula.c calcula.h \ gera.c gera.h \ base.c base.h \ diff --git a/lib/declaracao.c b/lib/declaracao.c index 9d36937..e4afe91 100644 --- a/lib/declaracao.c +++ b/lib/declaracao.c @@ -20,6 +20,7 @@ #include #include #include "list.h" +#include "sistema.h" #include "rendimento.h" #include "isento.h" #include "pagamento.h" @@ -90,5 +91,6 @@ void declaracao_free(struct declaracao *dec) list_free(dec->pagamentos, pagamento_free); list_free(dec->bens, bem_free); pmhash_del(dec->totais); + sistema_free(dec); free(dec); } diff --git a/lib/declaracao.h b/lib/declaracao.h index bdd2a3a..ddaa40f 100644 --- a/lib/declaracao.h +++ b/lib/declaracao.h @@ -21,6 +21,7 @@ #include "list.h" #include "contribuinte.h" +#include "sistema.h" enum { SIMPLES, @@ -36,6 +37,7 @@ struct declaracao { struct list *pagamentos; struct list *bens; struct contribuinte contribuinte; + struct sistema sistema; long long pago; long long retido; long long devido; diff --git a/lib/gera.c b/lib/gera.c index 61496da..5bcce5d 100644 --- a/lib/gera.c +++ b/lib/gera.c @@ -29,6 +29,7 @@ #include "pagamento.h" #include "bem.h" #include "totais.h" +#include "sistema.h" static void gera_header(struct declaracao *dec, FILE *f) { @@ -51,9 +52,9 @@ static void gera_header(struct declaracao *dec, FILE *f) fprintf(f, "S"); /* TODO: gerada? */ fprintf(f, "%-10.10s", dec->retifica ?: ""); /* recibo retificada ex. atual */ fprintf(f, "2"); /* TODO: PGD */ - fprintf(f, "LINUX "); /* TODO: SO */ - fprintf(f, "3.16.0-"); /* TODO: versao SO */ - fprintf(f, "1.7.0_75 "); /* TODO: versao JVM */ + fprintf(f, "%-14.14s", sistema_get_so(dec)); + fprintf(f, "%-7.7s", sistema_get_so_versao(dec)); + fprintf(f, "%-9.9s", sistema_get_jvm_versao(dec)); fprintf(f, "%-10.10s", ""); /* TODO: última declaração transmitida (pode ser vazio?) */ fprintf(f, "%04d", dec->contribuinte.cd_municipio); fprintf(f, " "); /* TODO: CPF conjuge */ @@ -110,7 +111,7 @@ static void gera_header(struct declaracao *dec, FILE *f) fprintf(f, "%-40.40s", dec->contribuinte.municipio); fprintf(f, "%-60.60s", dec->nome); fprintf(f, "%-11.11s", ""); /* CPF empregada */ - fprintf(f, "000000000000"); /* FIXME: MAC */ + fprintf(f, "%-12.12s", sistema_get_mac(dec)); fprintf(f, "%08d", 0); /* Data saída */ fprintf(f, "%-11.11s", ""); /* CPF procurador */ fprintf(f, "%03d", dec->obrigatoria); /* criterio obrigatoriedade */ diff --git a/lib/sistema.c b/lib/sistema.c new file mode 100644 index 0000000..2d3e718 --- /dev/null +++ b/lib/sistema.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * 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. + */ + +#include "sistema.h" +#include +#include +#include +#include +#include +#include "cmd.h" + +void sistema_free(struct declaracao *dec) +{ + if (dec->sistema.so) + free(dec->sistema.so); + if (dec->sistema.so_versao) + free(dec->sistema.so_versao); + if (dec->sistema.jvm_versao) + free(dec->sistema.jvm_versao); + if (dec->sistema.mac) + free(dec->sistema.mac); +} + +static int run_sistema(struct declaracao *dec, char **args, int argc) +{ + struct rendimento *rendimento; + int r; + if (argc != 5) + return -EINVAL; + dec->sistema.so = strdup(args[1]); + dec->sistema.so_versao = strdup(args[2]); + dec->sistema.jvm_versao = strdup(args[3]); + dec->sistema.mac = strdup(args[4]); + if (!dec->sistema.so || !dec->sistema.so_versao || + !dec->sistema.jvm_versao || !dec->sistema.mac) + goto out; + return 0; +out: + sistema_free(dec); + return -ENOMEM; +} + +void sistema_salva(struct declaracao *dec, FILE *f) +{ + fprintf(f, "sistema \"%s\" \"%s\" \"%s\" \"%s\"\n", + dec->sistema.so, dec->sistema.so_versao, + dec->sistema.jvm_versao, dec->sistema.mac); +} + +static struct cmd cmd_sistema = { + .name = "sistema", + .run = run_sistema, +}; + +int sistema_cmd_init(void) +{ + cmd_add(&cmd_sistema); + return 0; +} + +#define SISTEMA_SO_DEFAULT "LINUX" +#define SISTEMA_SO_VERSAO_DEFAULT "3.16.0" +#define SISTEMA_JVM_VERSAO_DEFAULT "1.7.0" +#define SISTEMA_MAC_DEFAULT "000000000000" + +char * sistema_get_so(struct declaracao *dec) +{ + if (!dec->sistema.so) + return SISTEMA_SO_DEFAULT; + return dec->sistema.so; +} + +char * sistema_get_so_versao(struct declaracao *dec) +{ + if (!dec->sistema.so_versao) + return SISTEMA_SO_VERSAO_DEFAULT; + return dec->sistema.so_versao; +} + +char * sistema_get_jvm_versao(struct declaracao *dec) +{ + if (!dec->sistema.jvm_versao) + return SISTEMA_JVM_VERSAO_DEFAULT; + return dec->sistema.jvm_versao; +} + +char * sistema_get_mac(struct declaracao *dec) +{ + if (!dec->sistema.mac) + return SISTEMA_MAC_DEFAULT; + return dec->sistema.mac; +} diff --git a/lib/sistema.h b/lib/sistema.h new file mode 100644 index 0000000..41bee95 --- /dev/null +++ b/lib/sistema.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * + * 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 _SISTEMA_H +#define _SISTEMA_H + +#include + +struct declaracao; + +struct sistema { + char *so; + char *so_versao; + char *jvm_versao; + char *mac; +}; + +void sistema_free(struct declaracao *dec); + +void sistema_salva(struct declaracao *dec, FILE *f); + +int sistema_cmd_init(void); + +char * sistema_get_so(struct declaracao *dec); +char * sistema_get_so_versao(struct declaracao *dec); +char * sistema_get_jvm_versao(struct declaracao *dec); +char * sistema_get_mac(struct declaracao *dec); + +#endif diff --git a/src/declara.c b/src/declara.c index 130826a..7dc38d7 100644 --- a/src/declara.c +++ b/src/declara.c @@ -97,6 +97,7 @@ int main(int argc, char **argv) bem_cmd_init(); calcula_cmd_init(); gera_cmd_init(); + sistema_cmd_init(); filename = argv[1]; r = process(filename); -- 2.20.1