From: Thadeu Lima de Souza Cascardo Date: Sun, 2 Aug 2015 03:28:26 +0000 (-0300) Subject: Separa código em diretórios. X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fdeclara.git;a=commitdiff_plain;h=891f35cb2686c8b6135b42621e4aab9ce6e64e53 Separa código em diretórios. src contém o código de programas, lib contém código utilizado pelos programas, na forma do libreceita, e test contém os testes a serem realizados. --- diff --git a/Makefile.am b/Makefile.am index b0bea0d..128d61e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,18 +1 @@ -bin_PROGRAMS = declara -declara_SOURCES = declara.c declaracao.c declaracao.h \ - token.c token.h \ - cmd.c cmd.h pmhash.c pmhash.h \ - totais.c totais.h \ - calcula.c calcula.h \ - gera.c gera.h \ - base.c base.h \ - list.c list.h \ - util.c util.h \ - contribuinte.c contribuinte.h \ - rendimento.c rendimento.h \ - pagamento.c pagamento.h - -check_PROGRAMS = listtest -listtest_SOURCES = listtest.c list.c list.h - -TESTS = $(check_PROGRAMS) +SUBDIRS = lib src test diff --git a/base.c b/base.c deleted file mode 100644 index b4bf17d..0000000 --- a/base.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 "cmd.h" -#include -#include -#include -#include -#include "rendimento.h" -#include "util.h" - -SET_INT(ano); -SET_STRING(cpf); -SET_STRING(nome); -SET_STRING(recibo); -SET_STRING(retifica); - -SET_STRING(banco); -SET_STRING(agencia); -SET_STRING(contacorrente); -SET_STRING(dvconta); - -static int run_resumo(struct declaracao *dec, char **args, int argc) -{ - if (dec->retifica) - printf("retificadora\n"); - if (dec->obrigatoria) - printf("obrigatoria\n"); - printf("pago: %lld.%02d\n", reais(dec->pago), centavos(dec->pago)); - printf("retido: %lld.%02d\n", reais(dec->retido), - centavos(dec->retido)); - printf("devido: %lld.%02d\n", reais(dec->devido), - centavos(dec->devido)); - if (dec->restituicao > 0) - printf("restituicao: %lld.%02d\n", reais(dec->restituicao), - centavos(dec->restituicao)); - if (dec->pagar > 0) - printf("a pagar: %lld.%02d\n", reais(dec->pagar), - centavos(dec->pagar)); - return 0; -} - -static void salva(struct declaracao *dec, FILE *f) -{ - fprintf(f, "ano %d\n", dec->ano); - if (dec->cpf) - fprintf(f, "cpf \"%s\"\n", dec->cpf); - if (dec->nome) - fprintf(f, "nome \"%s\"\n", dec->nome); - if (dec->recibo) - fprintf(f, "recibo \"%s\"\n", dec->recibo); - if (dec->retifica) - fprintf(f, "retifica \"%s\"\n", dec->retifica); - contribuinte_salva(dec, f); - rendimento_salva(dec, f); -} - -static int run_salva(struct declaracao *dec, char **args, int argc) -{ - FILE *f; - char *filename; - if (argc != 2) - return -EINVAL; - filename = args[1]; - f = fopen(filename, "w"); - if (!f) - return -errno; - salva(dec, f); - fclose(f); - return 0; -} - -static struct cmd cmd_salva = { - .name = "salva", - .run = run_salva, -}; - -static struct cmd cmd_resumo = { - .name = "resumo", - .run = run_resumo, -}; - -int base_cmd_init(void) -{ - cmd_add(&cmd_salva); - cmd_add(&cmd_resumo); - - cmd_add(&cmd_ano); - cmd_add(&cmd_cpf); - cmd_add(&cmd_recibo); - cmd_add(&cmd_retifica); - cmd_add(&cmd_nome); - - cmd_add(&cmd_banco); - cmd_add(&cmd_agencia); - cmd_add(&cmd_contacorrente); - cmd_add(&cmd_dvconta); - - return 0; -} diff --git a/base.h b/base.h deleted file mode 100644 index 32b2779..0000000 --- a/base.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 _BASE_H -#define _BASE_H - -int base_cmd_init(void); - -#endif diff --git a/bootstrap.sh b/bootstrap.sh index 594643a..27e2764 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -33,4 +33,5 @@ test -e $CONFIG_FILE || touch $CONFIG_FILE aclocal autoheader autoconf +libtoolize --copy automake --add-missing --copy --force --foreign diff --git a/calcula.c b/calcula.c deleted file mode 100644 index 7b22639..0000000 --- a/calcula.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * 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 "calcula.h" -#include -#include -#include "declaracao.h" -#include "cmd.h" -#include "rendimento.h" -#include "totais.h" - -/* Alguns totais precisam ser limitados. Portanto, um total de decuções - * precisa ser ajustado para tais limites. Esta função considerará tais - * limites no futuro. */ -static long long total_deducao(struct declaracao *dec) -{ - return totais_get(dec, "INSS") + totais_get(dec, "PAGAMENTOS"); -} - -static void total_pago(struct declaracao *dec) -{ - struct rendimento *rendimento; - int i; - dec->pago = dec->retido = 0; - for (i = 0; rendimento = list_get(dec->rendimento, i); i++) { - dec->pago += rendimento->imposto; - dec->retido += rendimento->imposto; - } -} - -struct taxtable { - long long base; - long long aliquota; - long long deducao; -}; - -static struct taxtable table2015[] = { - { 0, 0, 0, }, - { 2145324, 750, 160899, }, - { 3215148, 1500, 402035, }, - { 4286917, 2250, 723554, }, - { 5356572, 2750, 991383, }, - { 9999999999999LL, 0, 0, }, -}; - -static const long long simples2015 = 1588089; - -static const long long obrigatoriedade2015 = 2681655; - -static long long imposto(struct taxtable *tt, long long tr) -{ - int i; - for (i = 0; tr >= tt[i].base; i++); - i--; - return tr * tt[i].aliquota / 10000 - tt[i].deducao; -} - -static long long imposto_simples(struct declaracao *dec) -{ - struct taxtable *tt; - long long tr, td; - tt = table2015; - tr = totais_get(dec, "RENDPJ"); - if (tr / 5 < simples2015) - td = tr / 5; - else - td = simples2015; - totais_add(dec, "DESCONTO", td); - tr -= td; - totais_add(dec, "BASE", tr); - return imposto(tt, tr); -} - -static long long imposto_completa(struct declaracao *dec) -{ - struct taxtable *tt; - long long tr, td; - if (dec->ano != 2015) { - return -EINVAL; - } - tt = table2015; - tr = totais_get(dec, "RENDPJ"); - td = total_deducao(dec); - tr -= td; - return imposto(tt, tr); -} - -int calcula(struct declaracao *dec) -{ - long long i_simples, i_completa; - if (dec->ano != 2015) { - return -EINVAL; - } - if (totais_get(dec, "RENDPJ") > obrigatoriedade2015) - dec->obrigatoria = 1; - i_simples = imposto_simples(dec); - i_completa = imposto_completa(dec); - total_pago(dec); - if (i_simples > i_completa) { - dec->tipo = COMPLETA; - dec->devido = i_completa; - } else { - dec->tipo = SIMPLES; - dec->devido = i_simples; - } - if (dec->pago > dec->devido) - dec->restituicao = dec->pago - dec->devido; - else - dec->pagar = dec->devido - dec->pago; - return 0; -} - -static int run_calcula(struct declaracao *dec, char **args, int argc) -{ - totais_add(dec, "EXCLUSIVOS_SEM_13o", - totais_get(dec, "EXCLUSIVOS") - - totais_get(dec, "DECIMOTERCEIRO")); - return calcula(dec); -} - -static struct cmd cmd_calcula = { - .name = "calcula", - .run = run_calcula, -}; - -int calcula_cmd_init(void) -{ - cmd_add(&cmd_calcula); - return 0; -} diff --git a/calcula.h b/calcula.h deleted file mode 100644 index cc40110..0000000 --- a/calcula.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 _CALCULA_H -#define _CALCULA_H - -int calcula_cmd_init(void); - -#endif diff --git a/cmd.c b/cmd.c deleted file mode 100644 index ad186a2..0000000 --- a/cmd.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 "cmd.h" -#include "pmhash.h" -#include "token.h" -#include -#include - -struct pmhash *cmds; - -int cmd_init(void) -{ - cmds = pmhash_new(); - if (!cmds) - return -errno; - return 0; -} - -int cmd_add(struct cmd *cmd) -{ - return pmhash_add(&cmds, cmd->name, cmd); -} - -int cmd_run(struct declaracao *dec, char *line) -{ - char **args = tokens_new(line); - struct cmd *cmd; - int r; - int argc; - if (!args || !args[0]) - return -errno; - for (argc = 0; args[argc]; argc++); - cmd = pmhash_get(cmds, args[0]); - if (!cmd || !cmd->run) { - tokens_free(args); - return -EINVAL; - } - r = cmd->run(dec, args, argc); - tokens_free(args); - return r; -} diff --git a/cmd.h b/cmd.h deleted file mode 100644 index 24ff5e0..0000000 --- a/cmd.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 _CMD_H -#define _CMD_H - -#include "declaracao.h" - -struct cmd { - char *name; - int (*run) (struct declaracao *dec, char **args, int argc); -}; - -int cmd_init(void); -int cmd_add(struct cmd *cmd); -int cmd_run(struct declaracao *dec, char *line); - -#endif diff --git a/configure.ac b/configure.ac index 4b20a5f..2803970 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,8 @@ AC_INIT(declara,2015.0,libreceita@lists.libreplanetbr.org) AC_CONFIG_AUX_DIR(build-aux) AM_INIT_AUTOMAKE +LT_INIT AC_PROG_CC AC_PROG_INSTALL AC_CONFIG_HEADERS([config.h]) -AC_OUTPUT(Makefile) +AC_OUTPUT(Makefile lib/Makefile src/Makefile test/Makefile) diff --git a/contribuinte.c b/contribuinte.c deleted file mode 100644 index 8438b36..0000000 --- a/contribuinte.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 "cmd.h" -#include "declaracao.h" -#include "contribuinte.h" -#include "util.h" - -#define SET_INT_CONTRIBUINTE(attr) \ - SET_INT_(attr, attr, contribuinte.attr) - -#define SET_STRING_CONTRIBUINTE(attr) \ - SET_STRING_(attr, attr, contribuinte.attr) - -SET_STRING_CONTRIBUINTE(uf); -SET_INT_CONTRIBUINTE(cd_municipio); -SET_STRING_CONTRIBUINTE(municipio); -SET_STRING_CONTRIBUINTE(cep); -SET_STRING_CONTRIBUINTE(bairro); -SET_STRING_CONTRIBUINTE(tipo_logradouro); -SET_STRING_CONTRIBUINTE(logradouro); -SET_STRING_CONTRIBUINTE(numero); -SET_STRING_CONTRIBUINTE(complemento); -SET_STRING_CONTRIBUINTE(titulo_eleitor); -SET_STRING_CONTRIBUINTE(dn); -SET_STRING_CONTRIBUINTE(ddd); -SET_STRING_CONTRIBUINTE(telefone); -SET_INT_CONTRIBUINTE(natureza_ocupacao); -SET_INT_CONTRIBUINTE(ocupacao_principal); - -void contribuinte_salva(struct declaracao *dec, FILE *f) -{ -#define SALVA_STRING(attr) \ - if (dec->contribuinte.attr) \ - fprintf(f, #attr " \"%s\"\n", dec->contribuinte.attr); -#define SALVA_INT(attr) \ - fprintf(f, #attr " %d\n", dec->contribuinte.attr); - SALVA_STRING(uf); - SALVA_INT(cd_municipio); - SALVA_STRING(municipio); - SALVA_STRING(cep); - SALVA_STRING(bairro); - SALVA_STRING(tipo_logradouro); - SALVA_STRING(logradouro); - SALVA_STRING(numero); - SALVA_STRING(complemento); - SALVA_STRING(titulo_eleitor); - SALVA_STRING(dn); - SALVA_STRING(ddd); - SALVA_STRING(telefone); - SALVA_INT(natureza_ocupacao); - SALVA_INT(ocupacao_principal); -} - -int contribuinte_cmd_init(void) -{ - cmd_add(&cmd_uf); - cmd_add(&cmd_cd_municipio); - cmd_add(&cmd_municipio); - cmd_add(&cmd_cep); - cmd_add(&cmd_bairro); - cmd_add(&cmd_tipo_logradouro); - cmd_add(&cmd_logradouro); - cmd_add(&cmd_numero); - cmd_add(&cmd_complemento); - cmd_add(&cmd_titulo_eleitor); - cmd_add(&cmd_dn); - cmd_add(&cmd_ddd); - cmd_add(&cmd_telefone); - cmd_add(&cmd_natureza_ocupacao); - cmd_add(&cmd_ocupacao_principal); - return 0; -} diff --git a/contribuinte.h b/contribuinte.h deleted file mode 100644 index e7ccefc..0000000 --- a/contribuinte.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 _CONTRIBUINTE_H -#define _CONTRIBUINTE_H - -#include - -struct declaracao; - -struct contribuinte { - char *uf; - int cd_municipio; - char *municipio; - char *cep; - char *bairro; - char *tipo_logradouro; - char *logradouro; - char *numero; - char *complemento; - char *titulo_eleitor; - char *dn; - char *ddd; - char *telefone; - int natureza_ocupacao; - int ocupacao_principal; -}; - -void contribuinte_salva(struct declaracao *dec, FILE *f); - -int contribuinte_cmd_init(void); - -#endif diff --git a/declara.c b/declara.c deleted file mode 100644 index 8db644a..0000000 --- a/declara.c +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include - -#include "declaracao.h" -#include "cmd.h" -#include "base.h" -#include "contribuinte.h" -#include "rendimento.h" -#include "pagamento.h" -#include "calcula.h" -#include "gera.h" - -static int realprocess(struct declaracao *dec, int fd) -{ - char *line = NULL; - size_t lsz = 0; - FILE *f; - int r; - int n = 1; - f = fdopen(fd, "r"); - if (!f) - return -errno; - while ((r = getline(&line, &lsz, f)) > 0) { - r = cmd_run(dec, line); - if (r < 0) { - fprintf(stderr, "Não foi possível executar comando na linha %d: %s\n", - n, strerror(-r)); - } - n++; - } - free(line); - return r; -} - -static int process(char *filename) -{ - int r = 0; - int fd; - struct declaracao *dec; - dec = declaracao_new(-1); - if (!dec) - return -errno; - fd = open(filename, O_RDONLY); - if (fd < 0) { - r = -errno; - goto out_open; - } - realprocess(dec, fd); - close(fd); -out_open: - declaracao_free(dec); - return r; -} - -static void usage(void) -{ - fprintf(stderr, "declara \n"); - exit(1); -} - -int main(int argc, char **argv) -{ - char *filename; - int r; - if (argc < 2) - usage(); - - cmd_init(); - base_cmd_init(); - contribuinte_cmd_init(); - rendimento_cmd_init(); - pagamento_cmd_init(); - calcula_cmd_init(); - gera_cmd_init(); - - filename = argv[1]; - r = process(filename); - if (r) - return 1; - return 0; -} diff --git a/declaracao.c b/declaracao.c deleted file mode 100644 index 53ff1e4..0000000 --- a/declaracao.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 "declaracao.h" -#include -#include -#include "list.h" -#include "rendimento.h" -#include "pagamento.h" -#include "pmhash.h" - -struct declaracao * declaracao_new(int ano) -{ - struct declaracao *dec; - dec = malloc(sizeof(*dec)); - if (!dec) - return NULL; - memset(dec, 0, sizeof(*dec)); - dec->ano = ano; - dec->rendimento = list_new(); - if (!dec->rendimento) - goto out_rendimento; - dec->pagamentos = list_new(); - if (!dec->pagamentos) - goto out_pagamentos; - dec->totais = pmhash_new(); - if (!dec->totais) - goto out_totais; - dec->pago = 0; - dec->devido = 0; - dec->restituicao = 0; - dec->tipo = SIMPLES; - return dec; -out_totais: - list_free(dec->pagamentos, pagamento_free); -out_pagamentos: - list_free(dec->rendimento, rendimento_free); -out_rendimento: - free(dec); - return NULL; -} - -void declaracao_free(struct declaracao *dec) -{ - if (dec->cpf) - free(dec->cpf); - if (dec->nome) - free(dec->nome); - if (dec->recibo) - free(dec->recibo); - if (dec->retifica) - free(dec->retifica); - if (dec->banco) - free(dec->banco); - if (dec->agencia) - free(dec->agencia); - if (dec->contacorrente) - free(dec->contacorrente); - if (dec->dvconta) - free(dec->dvconta); - list_free(dec->rendimento, rendimento_free); - list_free(dec->pagamentos, pagamento_free); - pmhash_del(dec->totais); - free(dec); -} diff --git a/declaracao.h b/declaracao.h deleted file mode 100644 index 1694661..0000000 --- a/declaracao.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 _DECLARACAO_H -#define _DECLARACAO_H - -#include "list.h" -#include "contribuinte.h" - -enum { - SIMPLES, - COMPLETA, -}; - -struct declaracao { - int ano; - char *cpf; - char *nome; - struct list *rendimento; - struct list *pagamentos; - struct contribuinte contribuinte; - long long pago; - long long retido; - long long devido; - long long pagar; - long long restituicao; - int obrigatoria; - int tipo; - char *recibo; - char *retifica; - char *banco; - char *agencia; - char *contacorrente; - char *dvconta; - int linhas[100]; /* Número de linhas escritas de cada tipo. */ - struct pmhash *totais; -}; - -struct declaracao * declaracao_new(int ano); -void declaracao_free(struct declaracao *dec); - -#endif diff --git a/gera.c b/gera.c deleted file mode 100644 index bf11b39..0000000 --- a/gera.c +++ /dev/null @@ -1,499 +0,0 @@ -/* - * 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 "gera.h" -#include "declaracao.h" -#include "cmd.h" -#include "rendimento.h" -#include -#include -#include -#include -#include "totais.h" - -static void gera_header(struct declaracao *dec, FILE *f) -{ - fprintf(f, "IRPF "); - fprintf(f, "%04d", dec->ano); /* Exercício */ - fprintf(f, "%04d", dec->ano - 1); /* Ano base */ - fprintf(f, "2500"); /* Código */ - fprintf(f, "%s", dec->retifica ? "1" : "0"); /* Retificadora */ - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, " "); /* Filler */ - fprintf(f, "1"); /* Tipo NI: Pessoa Física (1) */ - fprintf(f, "100"); /* Versão IRPF */ - fprintf(f, "%-60.60s", dec->nome); - fprintf(f, "%s", dec->contribuinte.uf); - fprintf(f, "%s", "0000000000"); /* FIXME: hash */ - fprintf(f, "1"); /* TODO: certificavel */ - fprintf(f, "%s", dec->contribuinte.dn); - fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N"); - fprintf(f, "%s", dec->restituicao ? "2" : "0"); /* TODO: resultado imposto, 2 - restituicao */ - 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, "%-10.10s", ""); /* TODO: última declaração transmitida (pode ser vazio?) */ - fprintf(f, "%04d", dec->contribuinte.cd_municipio); - fprintf(f, " "); /* TODO: CPF conjuge */ - fprintf(f, "%d", dec->obrigatoria ? 1 : 0); /* obrigatoria entrega */ - fprintf(f, "%013lld", dec->devido); /* imposto devido, carne-leao, complementar dos dependentes */ - fprintf(f, "%-10.10s", dec->recibo ?: ""); /* recibo ex. anterior */ - /* Indicador de segurança: 0 se retificadora, 2 se recibo - * anterior, se não 1. */ - fprintf(f, "%d", dec->retifica ? 0 : (dec->recibo ? 2 : 1)); - /* Indicador imposto pago, TODO: carnê leão e Lei 11.033. */ - fprintf(f, "%02d", dec->retido ? 1 : 0); - /* Indicador imposto antecipado, TODO: carnê leão, Lei 11.033, - * outros. */ - fprintf(f, "%d", dec->retido ? 1 : 0); - fprintf(f, "0"); /* TODO: mudanca endereco */ - fprintf(f, "%s", dec->contribuinte.cep); - fprintf(f, "0"); /* TODO: debito 1a quota */ - fprintf(f, "%-3.3s", dec->banco); /* no banco */ - fprintf(f, "%-4.4s", dec->agencia); /* agencia */ - fprintf(f, " "); /* TODO: filler */ - fprintf(f, "00000000"); /* transito julgado, gravado apenas se espolio */ - fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */ - fprintf(f, " "); /* RRA1 */ - fprintf(f, "%-11.11s", ""); /* CPF RRA1 */ - fprintf(f, " "); /* RRA2 */ - fprintf(f, "%-11.11s", ""); /* CPF RRA2 */ - fprintf(f, " "); /* RRA3 */ - fprintf(f, "%-11.11s", ""); /* CPF RRA3 */ - fprintf(f, " "); /* RRA4 */ - fprintf(f, "%-11.11s", ""); /* CPF RRA4 */ - fprintf(f, "0000000000000"); /* TODO: Doacao ECA */ - fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* TODO: CNPJ maior */ - fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 1)); /* TODO: CNPJ maior 2 */ - fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 2)); /* TODO: CNPJ maior 3 */ - fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 3)); /* TODO: CNPJ maior 4 */ - fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */ - fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */ - fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */ - fprintf(f, "%-8.8s", ""); /* DN Dependente 2 */ - fprintf(f, "%-11.11s", ""); /* CPF Dependente 3 */ - fprintf(f, "%-8.8s", ""); /* DN Dependente 3 */ - fprintf(f, "%-11.11s", ""); /* CPF Dependente 4 */ - fprintf(f, "%-8.8s", ""); /* DN Dependente 4 */ - fprintf(f, "%-11.11s", ""); /* CPF Dependente 5 */ - fprintf(f, "%-8.8s", ""); /* DN Dependente 5 */ - fprintf(f, "%-11.11s", ""); /* CPF Dependente 6 */ - fprintf(f, "%-8.8s", ""); /* DN Dependente 6 */ - /* medico maior, TODO: outros códigos */ - fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 26, 0)); - /* medico maior 2, TODO: outros códigos */ - fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 26, 1)); - fprintf(f, "%-11.11s", ""); /* CPF pensao */ - fprintf(f, "%-11.11s", ""); /* CPF inventariante */ - 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, "%08d", 0); /* Data saída */ - fprintf(f, "%-11.11s", ""); /* CPF procurador */ - fprintf(f, "%03d", dec->obrigatoria); /* criterio obrigatoriedade */ - fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* Total tributavel */ - /* CNPJ previdencia */ - fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 36, 0)); - /* CNPJ previdencia 2 */ - fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 36, 1)); - /* Total isentos */ - fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); - /* Total exclusivo */ - fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); - /* Total pagamentos */ - fprintf(f, "%013lld", totais_get(dec, "PAGAMENTOS")); - fprintf(f, "%-13.13s", dec->contacorrente); /* numero conta */ - fprintf(f, "%-2.2s", dec->dvconta); /* DV conta */ - fprintf(f, "0"); /* TODO: revalidar DV conta */ - - /* Mudanças de 2015 */ - - fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); /* natureza ocupacao */ - fprintf(f, "%-11.11s", ""); /* CPF domestica */ - fprintf(f, "%-11.11s", ""); /* NI domestica */ - fprintf(f, "%-11.11s", ""); /* CPF domestica 2 */ - fprintf(f, "%-11.11s", ""); /* NI domestica 2 */ - fprintf(f, "%-11.11s", ""); /* CPF domestica 3 */ - fprintf(f, "%-11.11s", ""); /* NI domestica 3 */ - fprintf(f, "1"); /* Declaracao iniciada */ - fprintf(f, "1"); /* Utilizou PGD */ - fprintf(f, "0"); /* Utilizou APP */ - fprintf(f, "0"); /* Utilizou online */ - fprintf(f, "0"); /* Utilizou rascunho */ - fprintf(f, "0"); /* Utilizou pre preenchida */ - fprintf(f, "0"); /* Utilizou fontes */ - fprintf(f, "0"); /* Utilizou plano saude */ - fprintf(f, "0"); /* Utilizou recuperar */ - fprintf(f, "1"); /* Declaracao iniciada */ - - /* Fim das mudanças de 2015 */ - - fprintf(f, " "); /* Teste PGD, FILLER2 */ - fprintf(f, "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_contribuinte(struct declaracao *dec, FILE *f) -{ - fprintf(f, "16"); - fprintf(f, "%s", dec->cpf); - fprintf(f, "%-60.60s", dec->nome); - fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro); - fprintf(f, "%-40.40s", dec->contribuinte.logradouro); - fprintf(f, "%-6.6s", dec->contribuinte.numero); - fprintf(f, "%-21.21s", dec->contribuinte.complemento); - fprintf(f, "%-19.19s", dec->contribuinte.bairro); - fprintf(f, "%-9.9s", dec->contribuinte.cep); - fprintf(f, "%04d", dec->contribuinte.cd_municipio); - fprintf(f, "%-40.40s", dec->contribuinte.municipio); - fprintf(f, "%-2.2s", dec->contribuinte.uf); - fprintf(f, " "); /* TODO: código exterior */ - fprintf(f, "105"); /* TODO: código país */ - fprintf(f, "%-100.100s", ""); /* TODO: FILLER3 */ - fprintf(f, "%-4.4s", ""); /* TODO: FILLER1 */ - fprintf(f, "%-8.8s", ""); /* TODO: FILLER2 */ - fprintf(f, "%-4.4s", dec->contribuinte.ddd); - fprintf(f, "%-9.9s", dec->contribuinte.telefone); - fprintf(f, "%-8.8s", dec->contribuinte.dn); - fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor); - fprintf(f, "%03d", dec->contribuinte.ocupacao_principal); - fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); - fprintf(f, "0"); /* TODO: qnt quotas */ - fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N"); - fprintf(f, "%s", dec->retifica ? "S" : "N"); - fprintf(f, "S"); /* GERADO */ - fprintf(f, "N"); /* TODO: mudança de endereço */ - fprintf(f, "%-12.12s", dec->retifica ? : ""); /* Recibo declaração original */ - fprintf(f, "%-3.3s", dec->banco); /* banco */ - fprintf(f, "%-4.4s", dec->agencia); /* agência */ - fprintf(f, "N"); /* TODO: deficiência */ - fprintf(f, "0"); /* FILLER: pré-preenchida */ - fprintf(f, "%-13.13s", dec->contacorrente); /* conta */ - fprintf(f, "%-2.2s", dec->dvconta); /* DV conta */ - fprintf(f, "N"); /* TODO: débito automático */ - fprintf(f, "0"); /* TODO: débito primeira quota */ - /* CNPJ principal fonte */ - fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); - fprintf(f, "%-10.10s", dec->recibo ?: ""); /* Recibo última declaração ano anterior */ - fprintf(f, "A"); /* Declaração de Ajuste */ - fprintf(f, "%-11.11s", ""); /* TODO: CPF procurador */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_simples(struct declaracao *dec, FILE *f) -{ - fprintf(f, "17"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, "%013lld", 0); /* TODO: carnê leão */ - fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */ - /* rendimentos isentos */ - fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); - /* rendimentos tributação exclusiva, exceto décimo terceiro */ - fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS_SEM_13o")); - /* 13o. */ - fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO")); - fprintf(f, "%013lld", 0); /* TODO: retido na fonte (Lei 11.033) */ - fprintf(f, "%013lld", 0); /* TODO: 13o. dependentes */ - fprintf(f, "%013lld", 0); /* TODO: lucros dependentes */ - fprintf(f, "%013lld", 0); /* TODO: isentos dependentes */ - fprintf(f, "%013lld", 0); /* TODO: exclusivos dependentes */ - fprintf(f, "%-13.13s", ""); /* FILLER */ - fprintf(f, "%-13.13s", ""); /* FILLER */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependentes */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF ext. depend. */ - fprintf(f, "%013lld", 0); /* TODO: carnê-leão PF? */ - fprintf(f, "%013lld", 0); /* TODO: carnê-leão dependentes */ - fprintf(f, "%013lld", 0); /* TODO: dedução dependentes */ - fprintf(f, "%013lld", 0); /* TODO: previdência RRA */ - fprintf(f, "%013lld", 0); /* TODO: previdência RRA dependentes */ - fprintf(f, "%013lld", 0); /* TODO: pensão RRA */ - fprintf(f, "%013lld", 0); /* TODO: pensão RRA dependentes */ - fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */ - fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */ - fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_totais_simples(struct declaracao *dec, FILE *f) -{ - fprintf(f, "18"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* TODO: rendimentos tributáveis */ - /* desconto simplificado */ - fprintf(f, "%013lld", totais_get(dec, "DESCONTO")); - /* base de cálculo */ - fprintf(f, "%013lld", totais_get(dec, "BASE")); - fprintf(f, "%013lld", dec->devido); /* imposto devido */ - fprintf(f, "%013lld", dec->retido); /* imposto retido */ - fprintf(f, "%013lld", 0); /* TODO: imposto complementar */ - fprintf(f, "%013lld", 0); /* TODO: carnê-leão */ - fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */ - fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */ - fprintf(f, "%013lld", dec->pagar); /* imposto pagar */ - fprintf(f, "%d", 0); /* TODO: quotas */ - fprintf(f, "%013lld", 0); /* TODO: valor quota */ - fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); /* rendimentos isentos */ - fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); /* rendimentos tributação exclusiva */ - fprintf(f, "%013lld", 0); /* TODO: conjuge */ - fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */ - fprintf(f, "%013lld", 0); /* TODO: imposto retido dependente */ - fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */ - fprintf(f, "%013lld", totais_get(dec, "RENDPJTIT")); /* rendimento tributável PJ titular */ - fprintf(f, "%013lld", 0); /* TODO: total rural */ - fprintf(f, "%013lld", dec->retido); /* imposto retido titular */ - fprintf(f, "%013lld", totais_get(dec, "BENSANTERIOR")); /* total bens ano anterior */ - fprintf(f, "%013lld", totais_get(dec, "BENS")); /* total bens ano base */ - fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT")); /* rendimentos isentos titular */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos dependentes */ - fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSTIT")); /* TODO: tributação exclusiva titular */ - fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva dependentes */ - fprintf(f, "%013lld", 0); /* TODO: total não tributável */ - fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */ - fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */ - fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */ - fprintf(f, "%013lld", 0); /* TODO: isentos e não tributáveis, transportado */ - fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva, transportado */ - fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos renda variável */ - fprintf(f, "%013lld", 0); /* TODO: parcela isenta ganhos capital */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior titular */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior dependentes */ - fprintf(f, "%013lld", 0); /* TODO: doações campanha */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */ - fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */ - fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */ - fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */ - fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */ - fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */ - fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */ - fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */ - fprintf(f, "%013lld", dec->devido); /* imposto devido */ - fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */ - fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */ - fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */ - fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */ - fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_completa(struct declaracao *dec, FILE *f) -{ - fprintf(f, "19"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */ - /* FIXME: completar campos */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_totais_completa(struct declaracao *dec, FILE *f) -{ - fprintf(f, "20"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - /* FIXME: completar campos */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_trailler(struct declaracao *dec, FILE *f) -{ - int i; - fprintf(f, "T9"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, "%06d", dec->linhas[0]); - for (i = 16; i < 100; i++) - if (i == 44) - fprintf(f, "%-5.5s", "0"); /* FILLER */ - else if (i == 49 || i == 57 || i == 64 || i == 79) - ; - else - fprintf(f, "%05d", dec->linhas[i]); - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_reciboheader(struct declaracao *dec, FILE *f) -{ - fprintf(f, "HR"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, "%-3.3s", ""); /* FILLER */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_recibodetalhe(struct declaracao *dec, FILE *f) -{ - fprintf(f, "DR"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, "%-3.3s", ""); /* FILLER */ - fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N"); - fprintf(f, "%-60.60s", dec->nome); - fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro); - fprintf(f, "%-40.40s", dec->contribuinte.logradouro); - fprintf(f, "%-6.6s", dec->contribuinte.numero); - fprintf(f, "%-21.21s", dec->contribuinte.complemento); - fprintf(f, "%-19.19s", dec->contribuinte.bairro); - fprintf(f, "%-9.9s", dec->contribuinte.cep); - fprintf(f, "%04d", dec->contribuinte.cd_municipio); - fprintf(f, "%-40.40s", dec->contribuinte.municipio); - fprintf(f, "%-2.2s", dec->contribuinte.uf); - fprintf(f, "%-100.100s", ""); /* FILLER */ - fprintf(f, "%-4.4s", ""); /* FILLER */ - fprintf(f, "%-8.8s", ""); /* FILLER */ - fprintf(f, "%-4.4s", dec->contribuinte.ddd); - fprintf(f, "%-9.9s", dec->contribuinte.telefone); - fprintf(f, "%s", dec->retifica ? "S" : "N"); /* Retificadora */ - fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* Total tributavel */ - fprintf(f, "%013lld", dec->devido); /* Imposto devido */ - fprintf(f, "%013lld", dec->restituicao); /* Imposto a restituir */ - fprintf(f, "%013lld", dec->pagar); /* Imposto a pagar */ - fprintf(f, "%d", 0); /* TODO: quotas */ - fprintf(f, "%013lld", 0); /* TODO: valor quota */ - fprintf(f, "%-3.3s", dec->banco); /* numero banco */ - fprintf(f, "%-4.4s", dec->agencia); /* numero agencia */ - fprintf(f, " "); /* FILLER */ - fprintf(f, "%d", 0); /* TODO: debito primeira quota */ - fprintf(f, "%013lld", 0); /* TODO: imposto pago ganho de capital */ - fprintf(f, "%-13.13s", dec->contacorrente); /* numero conta */ - fprintf(f, "%-2.2s", dec->dvconta); /* DV conta */ - fprintf(f, "%013lld", 0); /* TODO: imposto alienacao moeda estrangeira em especie */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_recibotrailler(struct declaracao *dec, FILE *f) -{ - fprintf(f, "R9"); - fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ - fprintf(f, "%-3.3s", ""); /* FILLER */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: hash */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -static void gera_rendimento(struct declaracao *dec, FILE *f) -{ - struct rendimento *r; - r = list_get(dec->rendimento, dec->linhas[21]); - fprintf(f, "21"); - fprintf(f, "%s", dec->cpf); - fprintf(f, "%-14.14s", r->cnpj); - fprintf(f, "%-60.60s", r->nome); - fprintf(f, "%013lld", r->rendimento); - fprintf(f, "%013lld", r->previdencia); - fprintf(f, "%013lld", r->decimoterceiro); - fprintf(f, "%013lld", r->imposto); - fprintf(f, "%-8.8s", r->saida); - fprintf(f, "%013lld", r->imposto_13o); - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ - fprintf(f, "\r\n"); -} - -typedef void (gera_linha)(struct declaracao *dec, FILE *f); - -static int wrap(gera_linha fn, struct declaracao *dec, FILE *f) -{ - FILE *m; - char *buf = NULL; - size_t bsize; - int linha; - int r; - m = open_memstream(&buf, &bsize); - if (!m) { - return -errno; - } - fn(dec, m); - dec->linhas[0]++; - fclose(m); - r = sscanf(buf, "%2d", &linha); - if (r == 1 && linha >= 0 && linha < 100) { - dec->linhas[linha]++; - } - fwrite(buf, bsize, 1, f); - free(buf); - return 0; -} - -static int gera(struct declaracao *dec, char *filename) -{ - FILE *f; - int r; - int i; - struct rendimento *rendimento; - -#define W(fn, dec, f) \ - do { \ - r = wrap(fn, dec, f); \ - if (r < 0) \ - goto out; \ - } while (0); - - f = fopen(filename, "w"); - if (!f) - return -errno; - memset(dec->linhas, 0, sizeof(dec->linhas)); - W(gera_header, dec, f); - W(gera_contribuinte, dec, f); - if (dec->tipo == COMPLETA) { - W(gera_completa, dec, f); - W(gera_totais_completa, dec, f); - } else { - W(gera_simples, dec, f); - W(gera_totais_simples, dec, f); - } - for (i = 0; rendimento = list_get(dec->rendimento, i); i++) { - W(gera_rendimento, dec, f); - } - W(gera_trailler, dec, f); - W(gera_reciboheader, dec, f); - W(gera_recibodetalhe, dec, f); - W(gera_recibotrailler, dec, f); - fclose(f); - return 0; -out: - fclose(f); - return r; -} - -static int run_gera(struct declaracao *dec, char **args, int argc) -{ - if (argc != 2) - return -EINVAL; - return gera(dec, args[1]); -} - -static struct cmd cmd_gera = { - .name = "gera", - .run = run_gera, -}; - -int gera_cmd_init(void) -{ - cmd_add(&cmd_gera); - return 0; -} diff --git a/gera.h b/gera.h deleted file mode 100644 index f97ab5a..0000000 --- a/gera.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 _GERA_H -#define _GERA_H - -int gera_cmd_init(void); - -#endif diff --git a/lib/Makefile.am b/lib/Makefile.am new file mode 100644 index 0000000..a8f050d --- /dev/null +++ b/lib/Makefile.am @@ -0,0 +1,13 @@ +lib_LTLIBRARIES = libreceita.la +libreceita_la_SOURCES = declaracao.c declaracao.h \ + token.c token.h \ + cmd.c cmd.h pmhash.c pmhash.h \ + totais.c totais.h \ + calcula.c calcula.h \ + gera.c gera.h \ + base.c base.h \ + list.c list.h \ + util.c util.h \ + contribuinte.c contribuinte.h \ + rendimento.c rendimento.h \ + pagamento.c pagamento.h diff --git a/lib/base.c b/lib/base.c new file mode 100644 index 0000000..b4bf17d --- /dev/null +++ b/lib/base.c @@ -0,0 +1,115 @@ +/* + * 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 "cmd.h" +#include +#include +#include +#include +#include "rendimento.h" +#include "util.h" + +SET_INT(ano); +SET_STRING(cpf); +SET_STRING(nome); +SET_STRING(recibo); +SET_STRING(retifica); + +SET_STRING(banco); +SET_STRING(agencia); +SET_STRING(contacorrente); +SET_STRING(dvconta); + +static int run_resumo(struct declaracao *dec, char **args, int argc) +{ + if (dec->retifica) + printf("retificadora\n"); + if (dec->obrigatoria) + printf("obrigatoria\n"); + printf("pago: %lld.%02d\n", reais(dec->pago), centavos(dec->pago)); + printf("retido: %lld.%02d\n", reais(dec->retido), + centavos(dec->retido)); + printf("devido: %lld.%02d\n", reais(dec->devido), + centavos(dec->devido)); + if (dec->restituicao > 0) + printf("restituicao: %lld.%02d\n", reais(dec->restituicao), + centavos(dec->restituicao)); + if (dec->pagar > 0) + printf("a pagar: %lld.%02d\n", reais(dec->pagar), + centavos(dec->pagar)); + return 0; +} + +static void salva(struct declaracao *dec, FILE *f) +{ + fprintf(f, "ano %d\n", dec->ano); + if (dec->cpf) + fprintf(f, "cpf \"%s\"\n", dec->cpf); + if (dec->nome) + fprintf(f, "nome \"%s\"\n", dec->nome); + if (dec->recibo) + fprintf(f, "recibo \"%s\"\n", dec->recibo); + if (dec->retifica) + fprintf(f, "retifica \"%s\"\n", dec->retifica); + contribuinte_salva(dec, f); + rendimento_salva(dec, f); +} + +static int run_salva(struct declaracao *dec, char **args, int argc) +{ + FILE *f; + char *filename; + if (argc != 2) + return -EINVAL; + filename = args[1]; + f = fopen(filename, "w"); + if (!f) + return -errno; + salva(dec, f); + fclose(f); + return 0; +} + +static struct cmd cmd_salva = { + .name = "salva", + .run = run_salva, +}; + +static struct cmd cmd_resumo = { + .name = "resumo", + .run = run_resumo, +}; + +int base_cmd_init(void) +{ + cmd_add(&cmd_salva); + cmd_add(&cmd_resumo); + + cmd_add(&cmd_ano); + cmd_add(&cmd_cpf); + cmd_add(&cmd_recibo); + cmd_add(&cmd_retifica); + cmd_add(&cmd_nome); + + cmd_add(&cmd_banco); + cmd_add(&cmd_agencia); + cmd_add(&cmd_contacorrente); + cmd_add(&cmd_dvconta); + + return 0; +} diff --git a/lib/base.h b/lib/base.h new file mode 100644 index 0000000..32b2779 --- /dev/null +++ b/lib/base.h @@ -0,0 +1,24 @@ +/* + * 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 _BASE_H +#define _BASE_H + +int base_cmd_init(void); + +#endif diff --git a/lib/calcula.c b/lib/calcula.c new file mode 100644 index 0000000..7b22639 --- /dev/null +++ b/lib/calcula.c @@ -0,0 +1,145 @@ +/* + * 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 "calcula.h" +#include +#include +#include "declaracao.h" +#include "cmd.h" +#include "rendimento.h" +#include "totais.h" + +/* Alguns totais precisam ser limitados. Portanto, um total de decuções + * precisa ser ajustado para tais limites. Esta função considerará tais + * limites no futuro. */ +static long long total_deducao(struct declaracao *dec) +{ + return totais_get(dec, "INSS") + totais_get(dec, "PAGAMENTOS"); +} + +static void total_pago(struct declaracao *dec) +{ + struct rendimento *rendimento; + int i; + dec->pago = dec->retido = 0; + for (i = 0; rendimento = list_get(dec->rendimento, i); i++) { + dec->pago += rendimento->imposto; + dec->retido += rendimento->imposto; + } +} + +struct taxtable { + long long base; + long long aliquota; + long long deducao; +}; + +static struct taxtable table2015[] = { + { 0, 0, 0, }, + { 2145324, 750, 160899, }, + { 3215148, 1500, 402035, }, + { 4286917, 2250, 723554, }, + { 5356572, 2750, 991383, }, + { 9999999999999LL, 0, 0, }, +}; + +static const long long simples2015 = 1588089; + +static const long long obrigatoriedade2015 = 2681655; + +static long long imposto(struct taxtable *tt, long long tr) +{ + int i; + for (i = 0; tr >= tt[i].base; i++); + i--; + return tr * tt[i].aliquota / 10000 - tt[i].deducao; +} + +static long long imposto_simples(struct declaracao *dec) +{ + struct taxtable *tt; + long long tr, td; + tt = table2015; + tr = totais_get(dec, "RENDPJ"); + if (tr / 5 < simples2015) + td = tr / 5; + else + td = simples2015; + totais_add(dec, "DESCONTO", td); + tr -= td; + totais_add(dec, "BASE", tr); + return imposto(tt, tr); +} + +static long long imposto_completa(struct declaracao *dec) +{ + struct taxtable *tt; + long long tr, td; + if (dec->ano != 2015) { + return -EINVAL; + } + tt = table2015; + tr = totais_get(dec, "RENDPJ"); + td = total_deducao(dec); + tr -= td; + return imposto(tt, tr); +} + +int calcula(struct declaracao *dec) +{ + long long i_simples, i_completa; + if (dec->ano != 2015) { + return -EINVAL; + } + if (totais_get(dec, "RENDPJ") > obrigatoriedade2015) + dec->obrigatoria = 1; + i_simples = imposto_simples(dec); + i_completa = imposto_completa(dec); + total_pago(dec); + if (i_simples > i_completa) { + dec->tipo = COMPLETA; + dec->devido = i_completa; + } else { + dec->tipo = SIMPLES; + dec->devido = i_simples; + } + if (dec->pago > dec->devido) + dec->restituicao = dec->pago - dec->devido; + else + dec->pagar = dec->devido - dec->pago; + return 0; +} + +static int run_calcula(struct declaracao *dec, char **args, int argc) +{ + totais_add(dec, "EXCLUSIVOS_SEM_13o", + totais_get(dec, "EXCLUSIVOS") - + totais_get(dec, "DECIMOTERCEIRO")); + return calcula(dec); +} + +static struct cmd cmd_calcula = { + .name = "calcula", + .run = run_calcula, +}; + +int calcula_cmd_init(void) +{ + cmd_add(&cmd_calcula); + return 0; +} diff --git a/lib/calcula.h b/lib/calcula.h new file mode 100644 index 0000000..cc40110 --- /dev/null +++ b/lib/calcula.h @@ -0,0 +1,24 @@ +/* + * 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 _CALCULA_H +#define _CALCULA_H + +int calcula_cmd_init(void); + +#endif diff --git a/lib/cmd.c b/lib/cmd.c new file mode 100644 index 0000000..ad186a2 --- /dev/null +++ b/lib/cmd.c @@ -0,0 +1,57 @@ +/* + * 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 "cmd.h" +#include "pmhash.h" +#include "token.h" +#include +#include + +struct pmhash *cmds; + +int cmd_init(void) +{ + cmds = pmhash_new(); + if (!cmds) + return -errno; + return 0; +} + +int cmd_add(struct cmd *cmd) +{ + return pmhash_add(&cmds, cmd->name, cmd); +} + +int cmd_run(struct declaracao *dec, char *line) +{ + char **args = tokens_new(line); + struct cmd *cmd; + int r; + int argc; + if (!args || !args[0]) + return -errno; + for (argc = 0; args[argc]; argc++); + cmd = pmhash_get(cmds, args[0]); + if (!cmd || !cmd->run) { + tokens_free(args); + return -EINVAL; + } + r = cmd->run(dec, args, argc); + tokens_free(args); + return r; +} diff --git a/lib/cmd.h b/lib/cmd.h new file mode 100644 index 0000000..24ff5e0 --- /dev/null +++ b/lib/cmd.h @@ -0,0 +1,33 @@ +/* + * 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 _CMD_H +#define _CMD_H + +#include "declaracao.h" + +struct cmd { + char *name; + int (*run) (struct declaracao *dec, char **args, int argc); +}; + +int cmd_init(void); +int cmd_add(struct cmd *cmd); +int cmd_run(struct declaracao *dec, char *line); + +#endif diff --git a/lib/contribuinte.c b/lib/contribuinte.c new file mode 100644 index 0000000..8438b36 --- /dev/null +++ b/lib/contribuinte.c @@ -0,0 +1,88 @@ +/* + * 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 "cmd.h" +#include "declaracao.h" +#include "contribuinte.h" +#include "util.h" + +#define SET_INT_CONTRIBUINTE(attr) \ + SET_INT_(attr, attr, contribuinte.attr) + +#define SET_STRING_CONTRIBUINTE(attr) \ + SET_STRING_(attr, attr, contribuinte.attr) + +SET_STRING_CONTRIBUINTE(uf); +SET_INT_CONTRIBUINTE(cd_municipio); +SET_STRING_CONTRIBUINTE(municipio); +SET_STRING_CONTRIBUINTE(cep); +SET_STRING_CONTRIBUINTE(bairro); +SET_STRING_CONTRIBUINTE(tipo_logradouro); +SET_STRING_CONTRIBUINTE(logradouro); +SET_STRING_CONTRIBUINTE(numero); +SET_STRING_CONTRIBUINTE(complemento); +SET_STRING_CONTRIBUINTE(titulo_eleitor); +SET_STRING_CONTRIBUINTE(dn); +SET_STRING_CONTRIBUINTE(ddd); +SET_STRING_CONTRIBUINTE(telefone); +SET_INT_CONTRIBUINTE(natureza_ocupacao); +SET_INT_CONTRIBUINTE(ocupacao_principal); + +void contribuinte_salva(struct declaracao *dec, FILE *f) +{ +#define SALVA_STRING(attr) \ + if (dec->contribuinte.attr) \ + fprintf(f, #attr " \"%s\"\n", dec->contribuinte.attr); +#define SALVA_INT(attr) \ + fprintf(f, #attr " %d\n", dec->contribuinte.attr); + SALVA_STRING(uf); + SALVA_INT(cd_municipio); + SALVA_STRING(municipio); + SALVA_STRING(cep); + SALVA_STRING(bairro); + SALVA_STRING(tipo_logradouro); + SALVA_STRING(logradouro); + SALVA_STRING(numero); + SALVA_STRING(complemento); + SALVA_STRING(titulo_eleitor); + SALVA_STRING(dn); + SALVA_STRING(ddd); + SALVA_STRING(telefone); + SALVA_INT(natureza_ocupacao); + SALVA_INT(ocupacao_principal); +} + +int contribuinte_cmd_init(void) +{ + cmd_add(&cmd_uf); + cmd_add(&cmd_cd_municipio); + cmd_add(&cmd_municipio); + cmd_add(&cmd_cep); + cmd_add(&cmd_bairro); + cmd_add(&cmd_tipo_logradouro); + cmd_add(&cmd_logradouro); + cmd_add(&cmd_numero); + cmd_add(&cmd_complemento); + cmd_add(&cmd_titulo_eleitor); + cmd_add(&cmd_dn); + cmd_add(&cmd_ddd); + cmd_add(&cmd_telefone); + cmd_add(&cmd_natureza_ocupacao); + cmd_add(&cmd_ocupacao_principal); + return 0; +} diff --git a/lib/contribuinte.h b/lib/contribuinte.h new file mode 100644 index 0000000..e7ccefc --- /dev/null +++ b/lib/contribuinte.h @@ -0,0 +1,48 @@ +/* + * 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 _CONTRIBUINTE_H +#define _CONTRIBUINTE_H + +#include + +struct declaracao; + +struct contribuinte { + char *uf; + int cd_municipio; + char *municipio; + char *cep; + char *bairro; + char *tipo_logradouro; + char *logradouro; + char *numero; + char *complemento; + char *titulo_eleitor; + char *dn; + char *ddd; + char *telefone; + int natureza_ocupacao; + int ocupacao_principal; +}; + +void contribuinte_salva(struct declaracao *dec, FILE *f); + +int contribuinte_cmd_init(void); + +#endif diff --git a/lib/declaracao.c b/lib/declaracao.c new file mode 100644 index 0000000..53ff1e4 --- /dev/null +++ b/lib/declaracao.c @@ -0,0 +1,80 @@ +/* + * 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 "declaracao.h" +#include +#include +#include "list.h" +#include "rendimento.h" +#include "pagamento.h" +#include "pmhash.h" + +struct declaracao * declaracao_new(int ano) +{ + struct declaracao *dec; + dec = malloc(sizeof(*dec)); + if (!dec) + return NULL; + memset(dec, 0, sizeof(*dec)); + dec->ano = ano; + dec->rendimento = list_new(); + if (!dec->rendimento) + goto out_rendimento; + dec->pagamentos = list_new(); + if (!dec->pagamentos) + goto out_pagamentos; + dec->totais = pmhash_new(); + if (!dec->totais) + goto out_totais; + dec->pago = 0; + dec->devido = 0; + dec->restituicao = 0; + dec->tipo = SIMPLES; + return dec; +out_totais: + list_free(dec->pagamentos, pagamento_free); +out_pagamentos: + list_free(dec->rendimento, rendimento_free); +out_rendimento: + free(dec); + return NULL; +} + +void declaracao_free(struct declaracao *dec) +{ + if (dec->cpf) + free(dec->cpf); + if (dec->nome) + free(dec->nome); + if (dec->recibo) + free(dec->recibo); + if (dec->retifica) + free(dec->retifica); + if (dec->banco) + free(dec->banco); + if (dec->agencia) + free(dec->agencia); + if (dec->contacorrente) + free(dec->contacorrente); + if (dec->dvconta) + free(dec->dvconta); + list_free(dec->rendimento, rendimento_free); + list_free(dec->pagamentos, pagamento_free); + pmhash_del(dec->totais); + free(dec); +} diff --git a/lib/declaracao.h b/lib/declaracao.h new file mode 100644 index 0000000..1694661 --- /dev/null +++ b/lib/declaracao.h @@ -0,0 +1,57 @@ +/* + * 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 _DECLARACAO_H +#define _DECLARACAO_H + +#include "list.h" +#include "contribuinte.h" + +enum { + SIMPLES, + COMPLETA, +}; + +struct declaracao { + int ano; + char *cpf; + char *nome; + struct list *rendimento; + struct list *pagamentos; + struct contribuinte contribuinte; + long long pago; + long long retido; + long long devido; + long long pagar; + long long restituicao; + int obrigatoria; + int tipo; + char *recibo; + char *retifica; + char *banco; + char *agencia; + char *contacorrente; + char *dvconta; + int linhas[100]; /* Número de linhas escritas de cada tipo. */ + struct pmhash *totais; +}; + +struct declaracao * declaracao_new(int ano); +void declaracao_free(struct declaracao *dec); + +#endif diff --git a/lib/gera.c b/lib/gera.c new file mode 100644 index 0000000..bf11b39 --- /dev/null +++ b/lib/gera.c @@ -0,0 +1,499 @@ +/* + * 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 "gera.h" +#include "declaracao.h" +#include "cmd.h" +#include "rendimento.h" +#include +#include +#include +#include +#include "totais.h" + +static void gera_header(struct declaracao *dec, FILE *f) +{ + fprintf(f, "IRPF "); + fprintf(f, "%04d", dec->ano); /* Exercício */ + fprintf(f, "%04d", dec->ano - 1); /* Ano base */ + fprintf(f, "2500"); /* Código */ + fprintf(f, "%s", dec->retifica ? "1" : "0"); /* Retificadora */ + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, " "); /* Filler */ + fprintf(f, "1"); /* Tipo NI: Pessoa Física (1) */ + fprintf(f, "100"); /* Versão IRPF */ + fprintf(f, "%-60.60s", dec->nome); + fprintf(f, "%s", dec->contribuinte.uf); + fprintf(f, "%s", "0000000000"); /* FIXME: hash */ + fprintf(f, "1"); /* TODO: certificavel */ + fprintf(f, "%s", dec->contribuinte.dn); + fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N"); + fprintf(f, "%s", dec->restituicao ? "2" : "0"); /* TODO: resultado imposto, 2 - restituicao */ + 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, "%-10.10s", ""); /* TODO: última declaração transmitida (pode ser vazio?) */ + fprintf(f, "%04d", dec->contribuinte.cd_municipio); + fprintf(f, " "); /* TODO: CPF conjuge */ + fprintf(f, "%d", dec->obrigatoria ? 1 : 0); /* obrigatoria entrega */ + fprintf(f, "%013lld", dec->devido); /* imposto devido, carne-leao, complementar dos dependentes */ + fprintf(f, "%-10.10s", dec->recibo ?: ""); /* recibo ex. anterior */ + /* Indicador de segurança: 0 se retificadora, 2 se recibo + * anterior, se não 1. */ + fprintf(f, "%d", dec->retifica ? 0 : (dec->recibo ? 2 : 1)); + /* Indicador imposto pago, TODO: carnê leão e Lei 11.033. */ + fprintf(f, "%02d", dec->retido ? 1 : 0); + /* Indicador imposto antecipado, TODO: carnê leão, Lei 11.033, + * outros. */ + fprintf(f, "%d", dec->retido ? 1 : 0); + fprintf(f, "0"); /* TODO: mudanca endereco */ + fprintf(f, "%s", dec->contribuinte.cep); + fprintf(f, "0"); /* TODO: debito 1a quota */ + fprintf(f, "%-3.3s", dec->banco); /* no banco */ + fprintf(f, "%-4.4s", dec->agencia); /* agencia */ + fprintf(f, " "); /* TODO: filler */ + fprintf(f, "00000000"); /* transito julgado, gravado apenas se espolio */ + fprintf(f, "%013lld", dec->pagar); /* imposto a pagar */ + fprintf(f, " "); /* RRA1 */ + fprintf(f, "%-11.11s", ""); /* CPF RRA1 */ + fprintf(f, " "); /* RRA2 */ + fprintf(f, "%-11.11s", ""); /* CPF RRA2 */ + fprintf(f, " "); /* RRA3 */ + fprintf(f, "%-11.11s", ""); /* CPF RRA3 */ + fprintf(f, " "); /* RRA4 */ + fprintf(f, "%-11.11s", ""); /* CPF RRA4 */ + fprintf(f, "0000000000000"); /* TODO: Doacao ECA */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); /* TODO: CNPJ maior */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 1)); /* TODO: CNPJ maior 2 */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 2)); /* TODO: CNPJ maior 3 */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 3)); /* TODO: CNPJ maior 4 */ + fprintf(f, "%-11.11s", ""); /* CPF Dependente 1 */ + fprintf(f, "%-8.8s", ""); /* DN Dependente 1 */ + fprintf(f, "%-11.11s", ""); /* CPF Dependente 2 */ + fprintf(f, "%-8.8s", ""); /* DN Dependente 2 */ + fprintf(f, "%-11.11s", ""); /* CPF Dependente 3 */ + fprintf(f, "%-8.8s", ""); /* DN Dependente 3 */ + fprintf(f, "%-11.11s", ""); /* CPF Dependente 4 */ + fprintf(f, "%-8.8s", ""); /* DN Dependente 4 */ + fprintf(f, "%-11.11s", ""); /* CPF Dependente 5 */ + fprintf(f, "%-8.8s", ""); /* DN Dependente 5 */ + fprintf(f, "%-11.11s", ""); /* CPF Dependente 6 */ + fprintf(f, "%-8.8s", ""); /* DN Dependente 6 */ + /* medico maior, TODO: outros códigos */ + fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 26, 0)); + /* medico maior 2, TODO: outros códigos */ + fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 26, 1)); + fprintf(f, "%-11.11s", ""); /* CPF pensao */ + fprintf(f, "%-11.11s", ""); /* CPF inventariante */ + 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, "%08d", 0); /* Data saída */ + fprintf(f, "%-11.11s", ""); /* CPF procurador */ + fprintf(f, "%03d", dec->obrigatoria); /* criterio obrigatoriedade */ + fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* Total tributavel */ + /* CNPJ previdencia */ + fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 36, 0)); + /* CNPJ previdencia 2 */ + fprintf(f, "%-14.14s", pagamento_cnpj_ordenado(dec, 36, 1)); + /* Total isentos */ + fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); + /* Total exclusivo */ + fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); + /* Total pagamentos */ + fprintf(f, "%013lld", totais_get(dec, "PAGAMENTOS")); + fprintf(f, "%-13.13s", dec->contacorrente); /* numero conta */ + fprintf(f, "%-2.2s", dec->dvconta); /* DV conta */ + fprintf(f, "0"); /* TODO: revalidar DV conta */ + + /* Mudanças de 2015 */ + + fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); /* natureza ocupacao */ + fprintf(f, "%-11.11s", ""); /* CPF domestica */ + fprintf(f, "%-11.11s", ""); /* NI domestica */ + fprintf(f, "%-11.11s", ""); /* CPF domestica 2 */ + fprintf(f, "%-11.11s", ""); /* NI domestica 2 */ + fprintf(f, "%-11.11s", ""); /* CPF domestica 3 */ + fprintf(f, "%-11.11s", ""); /* NI domestica 3 */ + fprintf(f, "1"); /* Declaracao iniciada */ + fprintf(f, "1"); /* Utilizou PGD */ + fprintf(f, "0"); /* Utilizou APP */ + fprintf(f, "0"); /* Utilizou online */ + fprintf(f, "0"); /* Utilizou rascunho */ + fprintf(f, "0"); /* Utilizou pre preenchida */ + fprintf(f, "0"); /* Utilizou fontes */ + fprintf(f, "0"); /* Utilizou plano saude */ + fprintf(f, "0"); /* Utilizou recuperar */ + fprintf(f, "1"); /* Declaracao iniciada */ + + /* Fim das mudanças de 2015 */ + + fprintf(f, " "); /* Teste PGD, FILLER2 */ + fprintf(f, "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_contribuinte(struct declaracao *dec, FILE *f) +{ + fprintf(f, "16"); + fprintf(f, "%s", dec->cpf); + fprintf(f, "%-60.60s", dec->nome); + fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro); + fprintf(f, "%-40.40s", dec->contribuinte.logradouro); + fprintf(f, "%-6.6s", dec->contribuinte.numero); + fprintf(f, "%-21.21s", dec->contribuinte.complemento); + fprintf(f, "%-19.19s", dec->contribuinte.bairro); + fprintf(f, "%-9.9s", dec->contribuinte.cep); + fprintf(f, "%04d", dec->contribuinte.cd_municipio); + fprintf(f, "%-40.40s", dec->contribuinte.municipio); + fprintf(f, "%-2.2s", dec->contribuinte.uf); + fprintf(f, " "); /* TODO: código exterior */ + fprintf(f, "105"); /* TODO: código país */ + fprintf(f, "%-100.100s", ""); /* TODO: FILLER3 */ + fprintf(f, "%-4.4s", ""); /* TODO: FILLER1 */ + fprintf(f, "%-8.8s", ""); /* TODO: FILLER2 */ + fprintf(f, "%-4.4s", dec->contribuinte.ddd); + fprintf(f, "%-9.9s", dec->contribuinte.telefone); + fprintf(f, "%-8.8s", dec->contribuinte.dn); + fprintf(f, "%-13.13s", dec->contribuinte.titulo_eleitor); + fprintf(f, "%03d", dec->contribuinte.ocupacao_principal); + fprintf(f, "%02d", dec->contribuinte.natureza_ocupacao); + fprintf(f, "0"); /* TODO: qnt quotas */ + fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N"); + fprintf(f, "%s", dec->retifica ? "S" : "N"); + fprintf(f, "S"); /* GERADO */ + fprintf(f, "N"); /* TODO: mudança de endereço */ + fprintf(f, "%-12.12s", dec->retifica ? : ""); /* Recibo declaração original */ + fprintf(f, "%-3.3s", dec->banco); /* banco */ + fprintf(f, "%-4.4s", dec->agencia); /* agência */ + fprintf(f, "N"); /* TODO: deficiência */ + fprintf(f, "0"); /* FILLER: pré-preenchida */ + fprintf(f, "%-13.13s", dec->contacorrente); /* conta */ + fprintf(f, "%-2.2s", dec->dvconta); /* DV conta */ + fprintf(f, "N"); /* TODO: débito automático */ + fprintf(f, "0"); /* TODO: débito primeira quota */ + /* CNPJ principal fonte */ + fprintf(f, "%-14.14s", rendimento_cnpj_ordenado(dec, 0)); + fprintf(f, "%-10.10s", dec->recibo ?: ""); /* Recibo última declaração ano anterior */ + fprintf(f, "A"); /* Declaração de Ajuste */ + fprintf(f, "%-11.11s", ""); /* TODO: CPF procurador */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_simples(struct declaracao *dec, FILE *f) +{ + fprintf(f, "17"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, "%013lld", 0); /* TODO: carnê leão */ + fprintf(f, "%013lld", 0); /* TODO: lucros e dividendos */ + /* rendimentos isentos */ + fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); + /* rendimentos tributação exclusiva, exceto décimo terceiro */ + fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS_SEM_13o")); + /* 13o. */ + fprintf(f, "%013lld", totais_get(dec, "DECIMOTERCEIRO")); + fprintf(f, "%013lld", 0); /* TODO: retido na fonte (Lei 11.033) */ + fprintf(f, "%013lld", 0); /* TODO: 13o. dependentes */ + fprintf(f, "%013lld", 0); /* TODO: lucros dependentes */ + fprintf(f, "%013lld", 0); /* TODO: isentos dependentes */ + fprintf(f, "%013lld", 0); /* TODO: exclusivos dependentes */ + fprintf(f, "%-13.13s", ""); /* FILLER */ + fprintf(f, "%-13.13s", ""); /* FILLER */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF dependentes */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF ext. depend. */ + fprintf(f, "%013lld", 0); /* TODO: carnê-leão PF? */ + fprintf(f, "%013lld", 0); /* TODO: carnê-leão dependentes */ + fprintf(f, "%013lld", 0); /* TODO: dedução dependentes */ + fprintf(f, "%013lld", 0); /* TODO: previdência RRA */ + fprintf(f, "%013lld", 0); /* TODO: previdência RRA dependentes */ + fprintf(f, "%013lld", 0); /* TODO: pensão RRA */ + fprintf(f, "%013lld", 0); /* TODO: pensão RRA dependentes */ + fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */ + fprintf(f, "%013lld", 0); /* TODO: imposto devido sem rendimento exterior */ + fprintf(f, "%013lld", 0); /* TODO: limite imposto pago exterior */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_totais_simples(struct declaracao *dec, FILE *f) +{ + fprintf(f, "18"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* TODO: rendimentos tributáveis */ + /* desconto simplificado */ + fprintf(f, "%013lld", totais_get(dec, "DESCONTO")); + /* base de cálculo */ + fprintf(f, "%013lld", totais_get(dec, "BASE")); + fprintf(f, "%013lld", dec->devido); /* imposto devido */ + fprintf(f, "%013lld", dec->retido); /* imposto retido */ + fprintf(f, "%013lld", 0); /* TODO: imposto complementar */ + fprintf(f, "%013lld", 0); /* TODO: carnê-leão */ + fprintf(f, "%013lld", 0); /* TODO: imposto retido Lei 11.033 */ + fprintf(f, "%013lld", dec->restituicao); /* imposto a restituir */ + fprintf(f, "%013lld", dec->pagar); /* imposto pagar */ + fprintf(f, "%d", 0); /* TODO: quotas */ + fprintf(f, "%013lld", 0); /* TODO: valor quota */ + fprintf(f, "%013lld", totais_get(dec, "ISENTOS")); /* rendimentos isentos */ + fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOS")); /* rendimentos tributação exclusiva */ + fprintf(f, "%013lld", 0); /* TODO: conjuge */ + fprintf(f, "%013lld", 0); /* TODO: rendimento PJ dependente */ + fprintf(f, "%013lld", 0); /* TODO: imposto retido dependente */ + fprintf(f, "%013lld", 0); /* TODO: imposto ganhos de capital */ + fprintf(f, "%013lld", totais_get(dec, "RENDPJTIT")); /* rendimento tributável PJ titular */ + fprintf(f, "%013lld", 0); /* TODO: total rural */ + fprintf(f, "%013lld", dec->retido); /* imposto retido titular */ + fprintf(f, "%013lld", totais_get(dec, "BENSANTERIOR")); /* total bens ano anterior */ + fprintf(f, "%013lld", totais_get(dec, "BENS")); /* total bens ano base */ + fprintf(f, "%013lld", totais_get(dec, "ISENTOSTIT")); /* rendimentos isentos titular */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos isentos dependentes */ + fprintf(f, "%013lld", totais_get(dec, "EXCLUSIVOSTIT")); /* TODO: tributação exclusiva titular */ + fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva dependentes */ + fprintf(f, "%013lld", 0); /* TODO: total não tributável */ + fprintf(f, "%013lld", 0); /* TODO: total dívidas ano anterior */ + fprintf(f, "%013lld", 0); /* TODO: total dívidas ano base */ + fprintf(f, "%013lld", 0); /* TODO: retido fonte Lei 11.033 */ + fprintf(f, "%013lld", 0); /* TODO: isentos e não tributáveis, transportado */ + fprintf(f, "%013lld", 0); /* TODO: tributação exclusiva, transportado */ + fprintf(f, "%013lld", 0); /* TODO: ganhos líquidos renda variável */ + fprintf(f, "%013lld", 0); /* TODO: parcela isenta ganhos capital */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior titular */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exterior dependentes */ + fprintf(f, "%013lld", 0); /* TODO: doações campanha */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa titular */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos PF exigibilidade suspensa dependentes */ + fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa titular */ + fprintf(f, "%013lld", 0); /* TODO: depósitos judiciais exigibilidade suspensa dependentes */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA titular */ + fprintf(f, "%013lld", 0); /* TODO: IRF RRA titular */ + fprintf(f, "%013lld", 0); /* TODO: imposto RRA titular */ + fprintf(f, "%013lld", 0); /* TODO: rendimentos RRA dependentes */ + fprintf(f, "%013lld", 0); /* TODO: IRF RRA dependentes */ + fprintf(f, "%013lld", 0); /* TODO: imposto RRA dependentes */ + fprintf(f, "%013lld", dec->devido); /* imposto devido */ + fprintf(f, "%013lld", 0); /* TODO: imposto diferido ganho capital */ + fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital */ + fprintf(f, "%013lld", 0); /* TODO: imposto ganho líquido renda variável */ + fprintf(f, "%013lld", 0); /* TODO: imposto devido ganho capital moeda estrangeira */ + fprintf(f, "%013lld", 0); /* TODO: imposto pago exterior */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_completa(struct declaracao *dec, FILE *f) +{ + fprintf(f, "19"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, "%-14.14s", ""); /* TODO: CNPJ maior */ + /* FIXME: completar campos */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_totais_completa(struct declaracao *dec, FILE *f) +{ + fprintf(f, "20"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + /* FIXME: completar campos */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_trailler(struct declaracao *dec, FILE *f) +{ + int i; + fprintf(f, "T9"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, "%06d", dec->linhas[0]); + for (i = 16; i < 100; i++) + if (i == 44) + fprintf(f, "%-5.5s", "0"); /* FILLER */ + else if (i == 49 || i == 57 || i == 64 || i == 79) + ; + else + fprintf(f, "%05d", dec->linhas[i]); + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_reciboheader(struct declaracao *dec, FILE *f) +{ + fprintf(f, "HR"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, "%-3.3s", ""); /* FILLER */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_recibodetalhe(struct declaracao *dec, FILE *f) +{ + fprintf(f, "DR"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, "%-3.3s", ""); /* FILLER */ + fprintf(f, "%s", dec->tipo == COMPLETA ? "S" : "N"); + fprintf(f, "%-60.60s", dec->nome); + fprintf(f, "%-15.15s", dec->contribuinte.tipo_logradouro); + fprintf(f, "%-40.40s", dec->contribuinte.logradouro); + fprintf(f, "%-6.6s", dec->contribuinte.numero); + fprintf(f, "%-21.21s", dec->contribuinte.complemento); + fprintf(f, "%-19.19s", dec->contribuinte.bairro); + fprintf(f, "%-9.9s", dec->contribuinte.cep); + fprintf(f, "%04d", dec->contribuinte.cd_municipio); + fprintf(f, "%-40.40s", dec->contribuinte.municipio); + fprintf(f, "%-2.2s", dec->contribuinte.uf); + fprintf(f, "%-100.100s", ""); /* FILLER */ + fprintf(f, "%-4.4s", ""); /* FILLER */ + fprintf(f, "%-8.8s", ""); /* FILLER */ + fprintf(f, "%-4.4s", dec->contribuinte.ddd); + fprintf(f, "%-9.9s", dec->contribuinte.telefone); + fprintf(f, "%s", dec->retifica ? "S" : "N"); /* Retificadora */ + fprintf(f, "%013lld", totais_get(dec, "RENDPJ")); /* Total tributavel */ + fprintf(f, "%013lld", dec->devido); /* Imposto devido */ + fprintf(f, "%013lld", dec->restituicao); /* Imposto a restituir */ + fprintf(f, "%013lld", dec->pagar); /* Imposto a pagar */ + fprintf(f, "%d", 0); /* TODO: quotas */ + fprintf(f, "%013lld", 0); /* TODO: valor quota */ + fprintf(f, "%-3.3s", dec->banco); /* numero banco */ + fprintf(f, "%-4.4s", dec->agencia); /* numero agencia */ + fprintf(f, " "); /* FILLER */ + fprintf(f, "%d", 0); /* TODO: debito primeira quota */ + fprintf(f, "%013lld", 0); /* TODO: imposto pago ganho de capital */ + fprintf(f, "%-13.13s", dec->contacorrente); /* numero conta */ + fprintf(f, "%-2.2s", dec->dvconta); /* DV conta */ + fprintf(f, "%013lld", 0); /* TODO: imposto alienacao moeda estrangeira em especie */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_recibotrailler(struct declaracao *dec, FILE *f) +{ + fprintf(f, "R9"); + fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ + fprintf(f, "%-3.3s", ""); /* FILLER */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: hash */ + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +static void gera_rendimento(struct declaracao *dec, FILE *f) +{ + struct rendimento *r; + r = list_get(dec->rendimento, dec->linhas[21]); + fprintf(f, "21"); + fprintf(f, "%s", dec->cpf); + fprintf(f, "%-14.14s", r->cnpj); + fprintf(f, "%-60.60s", r->nome); + fprintf(f, "%013lld", r->rendimento); + fprintf(f, "%013lld", r->previdencia); + fprintf(f, "%013lld", r->decimoterceiro); + fprintf(f, "%013lld", r->imposto); + fprintf(f, "%-8.8s", r->saida); + fprintf(f, "%013lld", r->imposto_13o); + fprintf(f, "%-10.10s", "0000000000"); /* FIXME: controle */ + fprintf(f, "\r\n"); +} + +typedef void (gera_linha)(struct declaracao *dec, FILE *f); + +static int wrap(gera_linha fn, struct declaracao *dec, FILE *f) +{ + FILE *m; + char *buf = NULL; + size_t bsize; + int linha; + int r; + m = open_memstream(&buf, &bsize); + if (!m) { + return -errno; + } + fn(dec, m); + dec->linhas[0]++; + fclose(m); + r = sscanf(buf, "%2d", &linha); + if (r == 1 && linha >= 0 && linha < 100) { + dec->linhas[linha]++; + } + fwrite(buf, bsize, 1, f); + free(buf); + return 0; +} + +static int gera(struct declaracao *dec, char *filename) +{ + FILE *f; + int r; + int i; + struct rendimento *rendimento; + +#define W(fn, dec, f) \ + do { \ + r = wrap(fn, dec, f); \ + if (r < 0) \ + goto out; \ + } while (0); + + f = fopen(filename, "w"); + if (!f) + return -errno; + memset(dec->linhas, 0, sizeof(dec->linhas)); + W(gera_header, dec, f); + W(gera_contribuinte, dec, f); + if (dec->tipo == COMPLETA) { + W(gera_completa, dec, f); + W(gera_totais_completa, dec, f); + } else { + W(gera_simples, dec, f); + W(gera_totais_simples, dec, f); + } + for (i = 0; rendimento = list_get(dec->rendimento, i); i++) { + W(gera_rendimento, dec, f); + } + W(gera_trailler, dec, f); + W(gera_reciboheader, dec, f); + W(gera_recibodetalhe, dec, f); + W(gera_recibotrailler, dec, f); + fclose(f); + return 0; +out: + fclose(f); + return r; +} + +static int run_gera(struct declaracao *dec, char **args, int argc) +{ + if (argc != 2) + return -EINVAL; + return gera(dec, args[1]); +} + +static struct cmd cmd_gera = { + .name = "gera", + .run = run_gera, +}; + +int gera_cmd_init(void) +{ + cmd_add(&cmd_gera); + return 0; +} diff --git a/lib/gera.h b/lib/gera.h new file mode 100644 index 0000000..f97ab5a --- /dev/null +++ b/lib/gera.h @@ -0,0 +1,24 @@ +/* + * 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 _GERA_H +#define _GERA_H + +int gera_cmd_init(void); + +#endif diff --git a/lib/list.c b/lib/list.c new file mode 100644 index 0000000..87913c4 --- /dev/null +++ b/lib/list.c @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2012-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 "list.h" +#include +#include + +struct item { + void *val; +}; + +struct list { + size_t alen; + size_t len; + struct item items[]; +}; + +struct list * list_new(void) +{ + struct list *list; + size_t alen = 128; + list = malloc(sizeof(*list) + alen * sizeof(struct item)); + if (!list) + return NULL; + list->alen = alen; + list->len = 0; + memset(list->items, 0, alen * sizeof(struct item)); + return list; +} + +static struct list * list_expand(struct list **list) +{ + struct list *l = *list; + struct list *nlist; + size_t len = l->alen * sizeof(struct item); + size_t nlen = len * 2; + nlist = realloc(l, sizeof(*nlist) + nlen); + if (!nlist) + goto out; + *list = l = nlist; + memset(&l->items[l->len], 0, len); + l->alen = l->alen * 2; + return l; +out: + return NULL; +} + +int list_add(struct list **list, void *val) +{ + unsigned int i; + struct list *l = *list; + l->items[l->len].val = val; + l->len++; + if (l->len == l->alen) { + l = list_expand(list); + if (!l) + goto out; + } + return 0; +out: + l->items[l->len].val = NULL; + return -1; +} + +int list_insert(struct list **list, int pos, void *val) +{ + struct list *l = *list; + if (pos > l->len) + return -1; + if (l->len + 1 == l->alen) { + l = list_expand(list); + if (!l) + goto out; + } + if (pos < l->len) + memmove(&l->items[pos + 1], &l->items[pos], + (l->len - pos) * sizeof(struct item)); + l->items[pos].val = val; + l->len++; + return 0; +out: + return -1; +} + +int list_insert_ordered(struct list **list, void *val, sort_function_t *fn) +{ + struct list *l = *list; + int i; + for (i = 0; i < l->len; i++) + if (fn(l->items[i].val, val) >= 0) + return list_insert(list, i, val); + return list_add(list, val); +} + +void * list_get(struct list *list, int pos) +{ + unsigned int i; + if (pos >= list->len) + return NULL; + return list->items[pos].val; +} + +void list_free(struct list *list, free_function_t *ifree) +{ + int i; + if (ifree) + for (i = 0; i < list->len; i++) + ifree(list->items[i].val); + free(list); +} diff --git a/lib/list.h b/lib/list.h new file mode 100644 index 0000000..bf09f6f --- /dev/null +++ b/lib/list.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2012-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 _LIST_H +#define _LIST_H + +typedef void (free_function_t)(void *); +typedef int (sort_function_t)(void *, void *); +struct list; + +struct list * list_new(void); +int list_add(struct list **list, void *val); +int list_insert(struct list **list, int pos, void *val); +int list_insert_ordered(struct list **list, void *val, sort_function_t *fn); +void * list_get(struct list *list, int pos); +void list_free(struct list *list, free_function_t *ifree); + +#endif diff --git a/lib/pagamento.c b/lib/pagamento.c new file mode 100644 index 0000000..26cb3c0 --- /dev/null +++ b/lib/pagamento.c @@ -0,0 +1,130 @@ +/* + * 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 "pagamento.h" +#include +#include +#include +#include +#include +#include "cmd.h" +#include "list.h" +#include "util.h" +#include "totais.h" + +void pagamento_free(void *pointer) +{ + struct pagamento *pagamento = pointer; + if (pagamento->cnpj) + free(pagamento->cnpj); + if (pagamento->nome) + free(pagamento->nome); + free(pagamento); +} + +static int pagamento_cmp(void *p1, void *p2) +{ + struct pagamento *r1 = p1; + struct pagamento *r2 = p2; + /* O pagamento maior vem primeiro. */ + if (r1->pagamento > r2->pagamento) + return -1; + else if (r1->pagamento < r2->pagamento) + return 1; + return 0; +} + +static struct pagamento * pagamento_new(char **args) +{ + struct pagamento *pagamento; + int r = 0; + pagamento = malloc(sizeof(*pagamento)); + pagamento->cnpj = strdup(args[2]); + pagamento->nome = strdup(args[3]); + /* TODO: consertar set_int para funcionar como set_llong */ + r += set_int(args, 2, &pagamento->codigo); + r += set_llong(args[4], &pagamento->pagamento); + r += set_llong(args[5], &pagamento->reembolso); + if (!pagamento->cnpj || !pagamento->nome) { + pagamento_free(pagamento); + return NULL; + } + if (r < 0 || pagamento->codigo < 0 || + pagamento->pagamento < 0 || pagamento->reembolso < 0) { + pagamento_free(pagamento); + return NULL; + } + return pagamento; +} + +static int run_pagamento(struct declaracao *dec, char **args, int argc) +{ + struct pagamento *pagamento; + int r; + if (argc != 6) + return -EINVAL; + pagamento = pagamento_new(args); + if (!pagamento) + return -ENOMEM; + r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp); + if (r < 0) { + pagamento_free(pagamento); + return r; + } + r = totais_add(dec, "PAGAMENTOS", pagamento->pagamento); + r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento); + if (r) { + pagamento_free(pagamento); + return r; + } + return 0; +} + +void pagamento_salva(struct declaracao *dec, FILE *f) +{ + int i; + struct pagamento *j; + for (i = 0; j = list_get(dec->pagamentos, i); i++) + fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld\n", + j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso); +} + +static struct cmd cmd_pagamento = { + .name = "pagamento", + .run = run_pagamento, +}; + +int pagamento_cmd_init(void) +{ + cmd_add(&cmd_pagamento); + return 0; +} + +char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n) +{ + struct pagamento *pagamento; + int i; + int j = 0; + for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) { + if (pagamento->codigo == codigo && j++ == n) + break; + } + if (!pagamento) + return ""; + return pagamento->cnpj; +} diff --git a/lib/pagamento.h b/lib/pagamento.h new file mode 100644 index 0000000..235daca --- /dev/null +++ b/lib/pagamento.h @@ -0,0 +1,40 @@ +/* + * 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 _PAGAMENTO_H +#define _PAGAMENTO_H + +#include +#include "declaracao.h" + +struct pagamento { + int codigo; + char *cnpj; + char *nome; + long long pagamento; + long long reembolso; +}; + +void pagamento_salva(struct declaracao *dec, FILE *f); +void pagamento_free(void *pointer); + +int pagamento_cmd_init(void); + +char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n); + +#endif diff --git a/lib/pmhash.c b/lib/pmhash.c new file mode 100644 index 0000000..ca9cfca --- /dev/null +++ b/lib/pmhash.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2012-2013 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 "pmhash.h" +#include +#include + +struct item { + char *key; + void *val; +}; + +struct pmhash { + size_t len; + struct item items[]; +}; + +struct pmhash * pmhash_new(void) +{ + struct pmhash *pmhash; + size_t len = 128; + pmhash = malloc(sizeof(*pmhash) + len * sizeof(struct item)); + if (!pmhash) + return NULL; + pmhash->len = len; + memset(pmhash->items, 0, len * sizeof(struct item)); + return pmhash; +} + +int pmhash_add(struct pmhash **pmhash, char *key, void *val) +{ + unsigned int i; + struct pmhash *hash = *pmhash; + i = 0; +repeat: + for (; i < hash->len; i++) { + if (hash->items[i].key == NULL) { + hash->items[i].key = key; + hash->items[i].val = val; + break; + } + } + if (i == hash->len) { + struct pmhash *nhash; + size_t len = hash->len * sizeof(struct item); + size_t nlen = len * 2; + nhash = realloc(hash, sizeof(*nhash) + nlen); + if (!nhash) + goto out; + *pmhash = hash = nhash; + memset(&hash->items[hash->len], 0, len); + hash->len = hash->len * 2; + goto repeat; + } + return 0; +out: + return -1; +} + +void * pmhash_get(struct pmhash *pmhash, char *key) +{ + unsigned int i; + for (i = 0; i < pmhash->len; i++) { + if (pmhash->items[i].key == NULL) + return NULL; + if (!strcmp(pmhash->items[i].key, key)) + return pmhash->items[i].val; + } + return NULL; +} + +void pmhash_del(struct pmhash *pmhash) +{ + unsigned int i; + for (i = 0; i < pmhash->len; i++) { + if (pmhash->items[i].key == NULL) + break; + free(pmhash->items[i].key); + free(pmhash->items[i].val); + } + free(pmhash); +} diff --git a/lib/pmhash.h b/lib/pmhash.h new file mode 100644 index 0000000..d1c0206 --- /dev/null +++ b/lib/pmhash.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2012-2013 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 _PMHASH_H +#define _PMHASH_H + +struct pmhash; + +struct pmhash * pmhash_new(void); +int pmhash_add(struct pmhash **pmhash, char *key, void *val); +void * pmhash_get(struct pmhash *pmhash, char *key); +void pmhash_del(struct pmhash *pmhash); + +#endif diff --git a/lib/rendimento.c b/lib/rendimento.c new file mode 100644 index 0000000..508a6fb --- /dev/null +++ b/lib/rendimento.c @@ -0,0 +1,135 @@ +/* + * 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 "rendimento.h" +#include +#include +#include +#include +#include +#include "cmd.h" +#include "list.h" +#include "util.h" +#include "totais.h" + +void rendimento_free(void *pointer) +{ + struct rendimento *rendimento = pointer; + if (rendimento->cnpj) + free(rendimento->cnpj); + if (rendimento->nome) + free(rendimento->nome); + if (rendimento->saida) + free(rendimento->saida); + free(rendimento); +} + +static int rendimento_cmp(void *p1, void *p2) +{ + struct rendimento *r1 = p1; + struct rendimento *r2 = p2; + /* O rendimento maior vem primeiro. */ + if (r1->rendimento > r2->rendimento) + return -1; + else if (r1->rendimento < r2->rendimento) + return 1; + return 0; +} + +static struct rendimento * rendimento_new(char **args) +{ + struct rendimento *rendimento; + int r = 0; + rendimento = malloc(sizeof(*rendimento)); + rendimento->cnpj = strdup(args[1]); + rendimento->nome = strdup(args[2]); + rendimento->saida = strdup(args[8]); + r += set_llong(args[3], &rendimento->rendimento); + r += set_llong(args[4], &rendimento->previdencia); + r += set_llong(args[5], &rendimento->decimoterceiro); + r += set_llong(args[6], &rendimento->imposto); + r += set_llong(args[7], &rendimento->imposto_13o); + if (!rendimento->cnpj || !rendimento->nome || !rendimento->saida) { + rendimento_free(rendimento); + return NULL; + } + if (r < 0 || rendimento->rendimento < 0 || rendimento->previdencia < 0 || + rendimento->decimoterceiro < 0 || rendimento->imposto < 0 || + rendimento->imposto_13o < 0) { + rendimento_free(rendimento); + return NULL; + } + return rendimento; +} + +static int run_rendimento(struct declaracao *dec, char **args, int argc) +{ + struct rendimento *rendimento; + int r; + if (argc != 9) + return -EINVAL; + rendimento = rendimento_new(args); + if (!rendimento) + return -ENOMEM; + r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp); + if (r < 0) { + rendimento_free(rendimento); + return r; + } + r = totais_add(dec, "RENDPJ", rendimento->rendimento); + r += totais_add(dec, "RENDPJTIT", rendimento->rendimento); + r += totais_add(dec, "INSS", rendimento->previdencia); + r += totais_add(dec, "DECIMOTERCEIRO", rendimento->decimoterceiro); + r += totais_add(dec, "EXCLUSIVOS", rendimento->decimoterceiro); + r += totais_add(dec, "EXCLUSIVOSTIT", rendimento->decimoterceiro); + if (r) { + rendimento_free(rendimento); + return r; + } + return 0; +} + +void rendimento_salva(struct declaracao *dec, FILE *f) +{ + int i; + struct rendimento *j; + for (i = 0; j = list_get(dec->rendimento, i); i++) + fprintf(f, "rendimento \"%s\" \"%s\" %lld %lld %lld %lld %lld \"%s\"\n", + j->cnpj, j->nome, j->rendimento, j->previdencia, + j->decimoterceiro, j->imposto, j->imposto_13o, j->saida); +} + +static struct cmd cmd_rendimento = { + .name = "rendimento", + .run = run_rendimento, +}; + +int rendimento_cmd_init(void) +{ + cmd_add(&cmd_rendimento); + return 0; +} + +char * rendimento_cnpj_ordenado(struct declaracao *dec, int i) +{ + struct rendimento *rendimento; + rendimento = list_get(dec->rendimento, i); + if (!rendimento) + return ""; + return rendimento->cnpj; +} diff --git a/lib/rendimento.h b/lib/rendimento.h new file mode 100644 index 0000000..2f96a2d --- /dev/null +++ b/lib/rendimento.h @@ -0,0 +1,43 @@ +/* + * 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 _RENDIMENTO_H +#define _RENDIMENTO_H + +#include +#include "declaracao.h" + +struct rendimento { + char *cnpj; + char *nome; + long long rendimento; + long long previdencia; + long long decimoterceiro; + long long imposto; + long long imposto_13o; + char *saida; +}; + +void rendimento_salva(struct declaracao *dec, FILE *f); +void rendimento_free(void *pointer); + +int rendimento_cmd_init(void); + +char * rendimento_cnpj_ordenado(struct declaracao *dec, int i); + +#endif diff --git a/lib/token.c b/lib/token.c new file mode 100644 index 0000000..50b82de --- /dev/null +++ b/lib/token.c @@ -0,0 +1,130 @@ +/* + * 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 "token.h" +#include +#include +#include + +static char * token_unescape(char *start, char *end) +{ + int escape = 0; + char *s, *r, *n; + s = n = malloc(end - start); + if (!s) + return NULL; + for (r = start + 1; r < end; r++) { + if (escape) { + *s = *r; + s++; + escape = 0; + } else if (*r == '\\') { + escape = 1; + } else { + *s = *r; + s++; + } + } + *s = 0; + return n; +} + +static char * token_next(char *line, char **end) +{ + char *start; + char *lend; + char cend; + int escape = 0; + start = line; + while (isspace(*start)) + start++; + if (*start == '"') { + lend = start + 1; + while (*lend) { + cend = *lend; + lend++; + if (escape) + escape = 0; + else if (cend == '\\') + escape = 1; + else if (cend == '"') + break; + } + } else { + lend = start; + while (*lend && !isspace(*lend)) + lend++; + } + *end = lend; + return start; +} + +char ** tokens_new(char *line) +{ + char **args = NULL; + size_t alloc = 0; + size_t next = 0; + const int inc = 8; + int more = 0; + char *start; + char *end; + int i; + end = line; + do { + if (next <= alloc) { + char **nargs; + alloc += inc; + nargs = realloc(args, alloc * sizeof(char *)); + if (!nargs) { + for (i = 0; i < alloc - inc; i++) + if (args[i]) + free(args[i]); + free(args); + return NULL; + } + for (i = alloc - inc; i < alloc; i++) + nargs[i] = NULL; + args = nargs; + } + start = token_next(end, &end); + if (start == end) + break; + if (*start == '"') { + args[next] = token_unescape(start, end - 1); + } else { + args[next] = strndup(start, end - start); + } + if (!args[next]) { + for (i = 0; i < alloc; i++) + if (args[i]) + free(args[i]); + free(args); + return NULL; + } + next++; + } while (1); + return args; +} + +void tokens_free(char **args) +{ + char **arg; + for (arg = args; *arg; arg++) + free(*arg); + free(args); +} diff --git a/lib/token.h b/lib/token.h new file mode 100644 index 0000000..5050c26 --- /dev/null +++ b/lib/token.h @@ -0,0 +1,25 @@ +/* + * 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 _TOKEN_H +#define _TOKEN_H + +char ** tokens_new(char *line); +void tokens_free(char **args); + +#endif diff --git a/lib/totais.c b/lib/totais.c new file mode 100644 index 0000000..ca79125 --- /dev/null +++ b/lib/totais.c @@ -0,0 +1,59 @@ +/* + * 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 "totais.h" +#include +#include +#include +#include "pmhash.h" + +int totais_add(struct declaracao *dec, char *key, long long val) +{ + long long *p; + int r = -ENOMEM; + p = pmhash_get(dec->totais, key); + if (!p) { + p = malloc(sizeof(*p)); + if (!p) + goto out_p; + key = strdup(key); + if (!key) + goto out_key; + r = pmhash_add(&dec->totais, key, p); + if (r) + goto out_hash; + *p = 0; + } + *p += val; + return 0; +out_hash: + free(key); +out_key: + free(p); +out_p: + return r; +} + +long long totais_get(struct declaracao *dec, char *key) +{ + long long *p; + p = pmhash_get(dec->totais, key); + if (!p) + return 0; + return *p; +} diff --git a/lib/totais.h b/lib/totais.h new file mode 100644 index 0000000..45583f9 --- /dev/null +++ b/lib/totais.h @@ -0,0 +1,27 @@ +/* + * 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 _TOTAIS_H +#define _TOTAIS_H + +#include "declaracao.h" + +int totais_add(struct declaracao *dec, char *key, long long val); +long long totais_get(struct declaracao *dec, char *key); + +#endif diff --git a/lib/util.c b/lib/util.c new file mode 100644 index 0000000..90ca426 --- /dev/null +++ b/lib/util.c @@ -0,0 +1,58 @@ +/* + * 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 "util.h" +#include +#include +#include + +int set_llong(char *str, long long *val) +{ + char *end = NULL; + errno = 0; + *val = strtoll(str, &end, 0); + if (end && *end) + return -EINVAL; + if (errno == ERANGE) + return -ERANGE; + return 0; +} + +int set_int(char **args, int argc, int *val) +{ + char *end = NULL; + if (argc != 2) + return -EINVAL; + errno = 0; + *val = strtol(args[1], &end, 0); + if (end && *end) + return -EINVAL; + if (errno == ERANGE) + return -ERANGE; + return 0; +} + +int set_string(char **args, int argc, char **str) +{ + if (argc != 2) + return -EINVAL; + *str = strdup(args[1]); + if (!*str) + return -errno; + return 0; +} diff --git a/lib/util.h b/lib/util.h new file mode 100644 index 0000000..758d67b --- /dev/null +++ b/lib/util.h @@ -0,0 +1,74 @@ +/* + * 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 _UTIL_H +#define _UTIL_H + +int set_llong(char *str, long long *val); +int set_int(char **args, int argc, int *val); +int set_string(char **args, int argc, char **str); + +#define SET_INT_(suffix, command, attr) \ +static int run_##suffix(struct declaracao *dec, char **args, int argc) \ +{ \ + int val; \ + int r = set_int(args, argc, &val); \ + if (r) \ + return r; \ + dec->attr = val; \ + return 0; \ +} \ +static struct cmd cmd_##suffix = { \ + .name = #command, \ + .run = run_##suffix, \ +}; + +#define SET_INT(attr) SET_INT_(attr, attr, attr) + +#define SET_STRING_(suffix, command, attr) \ +static int run_##suffix(struct declaracao *dec, char **args, int argc) \ +{ \ + char *val; \ + int r = set_string(args, argc, &val); \ + if (r) \ + return r; \ + dec->attr = val; \ + return 0; \ +} \ +static struct cmd cmd_##suffix = { \ + .name = #command, \ + .run = run_##suffix, \ +} + +#define SET_STRING(attr) SET_STRING_(attr, attr, attr) + +static inline long long reais(long long val) +{ + return val / 100LL; +} + +/* Sempre retorna um valor positivo. */ +static inline int centavos(long long val) +{ + if (val > 0LL) + return (int) (val % 100LL); + else + return (int) (-val % 100LL); +} + +#endif diff --git a/list.c b/list.c deleted file mode 100644 index 87913c4..0000000 --- a/list.c +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (C) 2012-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 "list.h" -#include -#include - -struct item { - void *val; -}; - -struct list { - size_t alen; - size_t len; - struct item items[]; -}; - -struct list * list_new(void) -{ - struct list *list; - size_t alen = 128; - list = malloc(sizeof(*list) + alen * sizeof(struct item)); - if (!list) - return NULL; - list->alen = alen; - list->len = 0; - memset(list->items, 0, alen * sizeof(struct item)); - return list; -} - -static struct list * list_expand(struct list **list) -{ - struct list *l = *list; - struct list *nlist; - size_t len = l->alen * sizeof(struct item); - size_t nlen = len * 2; - nlist = realloc(l, sizeof(*nlist) + nlen); - if (!nlist) - goto out; - *list = l = nlist; - memset(&l->items[l->len], 0, len); - l->alen = l->alen * 2; - return l; -out: - return NULL; -} - -int list_add(struct list **list, void *val) -{ - unsigned int i; - struct list *l = *list; - l->items[l->len].val = val; - l->len++; - if (l->len == l->alen) { - l = list_expand(list); - if (!l) - goto out; - } - return 0; -out: - l->items[l->len].val = NULL; - return -1; -} - -int list_insert(struct list **list, int pos, void *val) -{ - struct list *l = *list; - if (pos > l->len) - return -1; - if (l->len + 1 == l->alen) { - l = list_expand(list); - if (!l) - goto out; - } - if (pos < l->len) - memmove(&l->items[pos + 1], &l->items[pos], - (l->len - pos) * sizeof(struct item)); - l->items[pos].val = val; - l->len++; - return 0; -out: - return -1; -} - -int list_insert_ordered(struct list **list, void *val, sort_function_t *fn) -{ - struct list *l = *list; - int i; - for (i = 0; i < l->len; i++) - if (fn(l->items[i].val, val) >= 0) - return list_insert(list, i, val); - return list_add(list, val); -} - -void * list_get(struct list *list, int pos) -{ - unsigned int i; - if (pos >= list->len) - return NULL; - return list->items[pos].val; -} - -void list_free(struct list *list, free_function_t *ifree) -{ - int i; - if (ifree) - for (i = 0; i < list->len; i++) - ifree(list->items[i].val); - free(list); -} diff --git a/list.h b/list.h deleted file mode 100644 index bf09f6f..0000000 --- a/list.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2012-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 _LIST_H -#define _LIST_H - -typedef void (free_function_t)(void *); -typedef int (sort_function_t)(void *, void *); -struct list; - -struct list * list_new(void); -int list_add(struct list **list, void *val); -int list_insert(struct list **list, int pos, void *val); -int list_insert_ordered(struct list **list, void *val, sort_function_t *fn); -void * list_get(struct list *list, int pos); -void list_free(struct list *list, free_function_t *ifree); - -#endif diff --git a/listtest.c b/listtest.c deleted file mode 100644 index d40b78b..0000000 --- a/listtest.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2012-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 -#include -#include -#include - -static char *names[] = { - "backwards", - "end to start", - "finish", - "english", -}; - -static char *sorting[] = { - "1", - "5", - "0", - "3", - "9", -}; - -static char *sorted[] = { - "0", - "1", - "3", - "5", - "9", -}; - -#define ALEN(x) (sizeof(x)/sizeof(x[0])) - -int main(int argc, char **argv) -{ - struct list *l; - int i; - int arraylen; - - arraylen = sizeof(names)/sizeof(names[0]); - - l = list_new(); - list_add(&l, "GNU"); - list_free(l, NULL); - printf("Single static list test pass\n"); - fflush(stdout); - - l = list_new(); - for (i = 0; i < arraylen; i++) { - list_add(&l, strdup(names[i])); - } - for (i = 0; i < arraylen; i++) { - if (strcmp(list_get(l, i), names[i])) - return 1; - } - list_free(l, free); - printf("Multiple add ordering list test pass\n"); - fflush(stdout); - - l = list_new(); - for (i = 0; i < arraylen; i++) { - list_insert(&l, 0, strdup(names[i])); - } - for (i = 0; i < arraylen; i++) { - if (strcmp(list_get(l, i), names[arraylen - i - 1])) - return 1; - } - list_free(l, free); - printf("Multiple insertion at start ordering list test pass\n"); - fflush(stdout); - - l = list_new(); - for (i = 0; i < arraylen; i++) { - list_insert(&l, i, strdup(names[i])); - } - for (i = 0; i < arraylen; i++) { - if (strcmp(list_get(l, i), names[i])) - return 1; - } - list_free(l, free); - printf("Multiple insertion at end ordering list test pass\n"); - fflush(stdout); - - l = list_new(); - for (i = 0; i < ALEN(sorting); i++) { - list_insert_ordered(&l, strdup(sorting[i]), - (sort_function_t *) strcmp); - } - for (i = 0; i < ALEN(sorted); i++) { - if (strcmp(list_get(l, i), sorted[i])) - return 1; - } - list_free(l, free); - printf("Ordered insertion list test pass\n"); - fflush(stdout); - - return 0; -} diff --git a/pagamento.c b/pagamento.c deleted file mode 100644 index 26cb3c0..0000000 --- a/pagamento.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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 "pagamento.h" -#include -#include -#include -#include -#include -#include "cmd.h" -#include "list.h" -#include "util.h" -#include "totais.h" - -void pagamento_free(void *pointer) -{ - struct pagamento *pagamento = pointer; - if (pagamento->cnpj) - free(pagamento->cnpj); - if (pagamento->nome) - free(pagamento->nome); - free(pagamento); -} - -static int pagamento_cmp(void *p1, void *p2) -{ - struct pagamento *r1 = p1; - struct pagamento *r2 = p2; - /* O pagamento maior vem primeiro. */ - if (r1->pagamento > r2->pagamento) - return -1; - else if (r1->pagamento < r2->pagamento) - return 1; - return 0; -} - -static struct pagamento * pagamento_new(char **args) -{ - struct pagamento *pagamento; - int r = 0; - pagamento = malloc(sizeof(*pagamento)); - pagamento->cnpj = strdup(args[2]); - pagamento->nome = strdup(args[3]); - /* TODO: consertar set_int para funcionar como set_llong */ - r += set_int(args, 2, &pagamento->codigo); - r += set_llong(args[4], &pagamento->pagamento); - r += set_llong(args[5], &pagamento->reembolso); - if (!pagamento->cnpj || !pagamento->nome) { - pagamento_free(pagamento); - return NULL; - } - if (r < 0 || pagamento->codigo < 0 || - pagamento->pagamento < 0 || pagamento->reembolso < 0) { - pagamento_free(pagamento); - return NULL; - } - return pagamento; -} - -static int run_pagamento(struct declaracao *dec, char **args, int argc) -{ - struct pagamento *pagamento; - int r; - if (argc != 6) - return -EINVAL; - pagamento = pagamento_new(args); - if (!pagamento) - return -ENOMEM; - r = list_insert_ordered(&dec->pagamentos, pagamento, pagamento_cmp); - if (r < 0) { - pagamento_free(pagamento); - return r; - } - r = totais_add(dec, "PAGAMENTOS", pagamento->pagamento); - r += totais_add(dec, "PAGAMENTOSTIT", pagamento->pagamento); - if (r) { - pagamento_free(pagamento); - return r; - } - return 0; -} - -void pagamento_salva(struct declaracao *dec, FILE *f) -{ - int i; - struct pagamento *j; - for (i = 0; j = list_get(dec->pagamentos, i); i++) - fprintf(f, "pagamento %d \"%s\" \"%s\" %lld %lld\n", - j->codigo, j->cnpj, j->nome, j->pagamento, j->reembolso); -} - -static struct cmd cmd_pagamento = { - .name = "pagamento", - .run = run_pagamento, -}; - -int pagamento_cmd_init(void) -{ - cmd_add(&cmd_pagamento); - return 0; -} - -char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n) -{ - struct pagamento *pagamento; - int i; - int j = 0; - for (i = 0; (pagamento = list_get(dec->pagamentos, i)); i++) { - if (pagamento->codigo == codigo && j++ == n) - break; - } - if (!pagamento) - return ""; - return pagamento->cnpj; -} diff --git a/pagamento.h b/pagamento.h deleted file mode 100644 index 235daca..0000000 --- a/pagamento.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 _PAGAMENTO_H -#define _PAGAMENTO_H - -#include -#include "declaracao.h" - -struct pagamento { - int codigo; - char *cnpj; - char *nome; - long long pagamento; - long long reembolso; -}; - -void pagamento_salva(struct declaracao *dec, FILE *f); -void pagamento_free(void *pointer); - -int pagamento_cmd_init(void); - -char * pagamento_cnpj_ordenado(struct declaracao *dec, int codigo, int n); - -#endif diff --git a/pmhash.c b/pmhash.c deleted file mode 100644 index ca9cfca..0000000 --- a/pmhash.c +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2012-2013 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 "pmhash.h" -#include -#include - -struct item { - char *key; - void *val; -}; - -struct pmhash { - size_t len; - struct item items[]; -}; - -struct pmhash * pmhash_new(void) -{ - struct pmhash *pmhash; - size_t len = 128; - pmhash = malloc(sizeof(*pmhash) + len * sizeof(struct item)); - if (!pmhash) - return NULL; - pmhash->len = len; - memset(pmhash->items, 0, len * sizeof(struct item)); - return pmhash; -} - -int pmhash_add(struct pmhash **pmhash, char *key, void *val) -{ - unsigned int i; - struct pmhash *hash = *pmhash; - i = 0; -repeat: - for (; i < hash->len; i++) { - if (hash->items[i].key == NULL) { - hash->items[i].key = key; - hash->items[i].val = val; - break; - } - } - if (i == hash->len) { - struct pmhash *nhash; - size_t len = hash->len * sizeof(struct item); - size_t nlen = len * 2; - nhash = realloc(hash, sizeof(*nhash) + nlen); - if (!nhash) - goto out; - *pmhash = hash = nhash; - memset(&hash->items[hash->len], 0, len); - hash->len = hash->len * 2; - goto repeat; - } - return 0; -out: - return -1; -} - -void * pmhash_get(struct pmhash *pmhash, char *key) -{ - unsigned int i; - for (i = 0; i < pmhash->len; i++) { - if (pmhash->items[i].key == NULL) - return NULL; - if (!strcmp(pmhash->items[i].key, key)) - return pmhash->items[i].val; - } - return NULL; -} - -void pmhash_del(struct pmhash *pmhash) -{ - unsigned int i; - for (i = 0; i < pmhash->len; i++) { - if (pmhash->items[i].key == NULL) - break; - free(pmhash->items[i].key); - free(pmhash->items[i].val); - } - free(pmhash); -} diff --git a/pmhash.h b/pmhash.h deleted file mode 100644 index d1c0206..0000000 --- a/pmhash.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2012-2013 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 _PMHASH_H -#define _PMHASH_H - -struct pmhash; - -struct pmhash * pmhash_new(void); -int pmhash_add(struct pmhash **pmhash, char *key, void *val); -void * pmhash_get(struct pmhash *pmhash, char *key); -void pmhash_del(struct pmhash *pmhash); - -#endif diff --git a/rendimento.c b/rendimento.c deleted file mode 100644 index 508a6fb..0000000 --- a/rendimento.c +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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 "rendimento.h" -#include -#include -#include -#include -#include -#include "cmd.h" -#include "list.h" -#include "util.h" -#include "totais.h" - -void rendimento_free(void *pointer) -{ - struct rendimento *rendimento = pointer; - if (rendimento->cnpj) - free(rendimento->cnpj); - if (rendimento->nome) - free(rendimento->nome); - if (rendimento->saida) - free(rendimento->saida); - free(rendimento); -} - -static int rendimento_cmp(void *p1, void *p2) -{ - struct rendimento *r1 = p1; - struct rendimento *r2 = p2; - /* O rendimento maior vem primeiro. */ - if (r1->rendimento > r2->rendimento) - return -1; - else if (r1->rendimento < r2->rendimento) - return 1; - return 0; -} - -static struct rendimento * rendimento_new(char **args) -{ - struct rendimento *rendimento; - int r = 0; - rendimento = malloc(sizeof(*rendimento)); - rendimento->cnpj = strdup(args[1]); - rendimento->nome = strdup(args[2]); - rendimento->saida = strdup(args[8]); - r += set_llong(args[3], &rendimento->rendimento); - r += set_llong(args[4], &rendimento->previdencia); - r += set_llong(args[5], &rendimento->decimoterceiro); - r += set_llong(args[6], &rendimento->imposto); - r += set_llong(args[7], &rendimento->imposto_13o); - if (!rendimento->cnpj || !rendimento->nome || !rendimento->saida) { - rendimento_free(rendimento); - return NULL; - } - if (r < 0 || rendimento->rendimento < 0 || rendimento->previdencia < 0 || - rendimento->decimoterceiro < 0 || rendimento->imposto < 0 || - rendimento->imposto_13o < 0) { - rendimento_free(rendimento); - return NULL; - } - return rendimento; -} - -static int run_rendimento(struct declaracao *dec, char **args, int argc) -{ - struct rendimento *rendimento; - int r; - if (argc != 9) - return -EINVAL; - rendimento = rendimento_new(args); - if (!rendimento) - return -ENOMEM; - r = list_insert_ordered(&dec->rendimento, rendimento, rendimento_cmp); - if (r < 0) { - rendimento_free(rendimento); - return r; - } - r = totais_add(dec, "RENDPJ", rendimento->rendimento); - r += totais_add(dec, "RENDPJTIT", rendimento->rendimento); - r += totais_add(dec, "INSS", rendimento->previdencia); - r += totais_add(dec, "DECIMOTERCEIRO", rendimento->decimoterceiro); - r += totais_add(dec, "EXCLUSIVOS", rendimento->decimoterceiro); - r += totais_add(dec, "EXCLUSIVOSTIT", rendimento->decimoterceiro); - if (r) { - rendimento_free(rendimento); - return r; - } - return 0; -} - -void rendimento_salva(struct declaracao *dec, FILE *f) -{ - int i; - struct rendimento *j; - for (i = 0; j = list_get(dec->rendimento, i); i++) - fprintf(f, "rendimento \"%s\" \"%s\" %lld %lld %lld %lld %lld \"%s\"\n", - j->cnpj, j->nome, j->rendimento, j->previdencia, - j->decimoterceiro, j->imposto, j->imposto_13o, j->saida); -} - -static struct cmd cmd_rendimento = { - .name = "rendimento", - .run = run_rendimento, -}; - -int rendimento_cmd_init(void) -{ - cmd_add(&cmd_rendimento); - return 0; -} - -char * rendimento_cnpj_ordenado(struct declaracao *dec, int i) -{ - struct rendimento *rendimento; - rendimento = list_get(dec->rendimento, i); - if (!rendimento) - return ""; - return rendimento->cnpj; -} diff --git a/rendimento.h b/rendimento.h deleted file mode 100644 index 2f96a2d..0000000 --- a/rendimento.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 _RENDIMENTO_H -#define _RENDIMENTO_H - -#include -#include "declaracao.h" - -struct rendimento { - char *cnpj; - char *nome; - long long rendimento; - long long previdencia; - long long decimoterceiro; - long long imposto; - long long imposto_13o; - char *saida; -}; - -void rendimento_salva(struct declaracao *dec, FILE *f); -void rendimento_free(void *pointer); - -int rendimento_cmd_init(void); - -char * rendimento_cnpj_ordenado(struct declaracao *dec, int i); - -#endif diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..3380b43 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,7 @@ +bin_PROGRAMS = declara + +declara_SOURCES = declara.c + +declara_CFLAGS = -I$(top_srcdir)/lib +declara_LDFLAGS = -static +declara_LDADD = -L$(top_builddir)/lib -lreceita diff --git a/src/declara.c b/src/declara.c new file mode 100644 index 0000000..8db644a --- /dev/null +++ b/src/declara.c @@ -0,0 +1,103 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#include "declaracao.h" +#include "cmd.h" +#include "base.h" +#include "contribuinte.h" +#include "rendimento.h" +#include "pagamento.h" +#include "calcula.h" +#include "gera.h" + +static int realprocess(struct declaracao *dec, int fd) +{ + char *line = NULL; + size_t lsz = 0; + FILE *f; + int r; + int n = 1; + f = fdopen(fd, "r"); + if (!f) + return -errno; + while ((r = getline(&line, &lsz, f)) > 0) { + r = cmd_run(dec, line); + if (r < 0) { + fprintf(stderr, "Não foi possível executar comando na linha %d: %s\n", + n, strerror(-r)); + } + n++; + } + free(line); + return r; +} + +static int process(char *filename) +{ + int r = 0; + int fd; + struct declaracao *dec; + dec = declaracao_new(-1); + if (!dec) + return -errno; + fd = open(filename, O_RDONLY); + if (fd < 0) { + r = -errno; + goto out_open; + } + realprocess(dec, fd); + close(fd); +out_open: + declaracao_free(dec); + return r; +} + +static void usage(void) +{ + fprintf(stderr, "declara \n"); + exit(1); +} + +int main(int argc, char **argv) +{ + char *filename; + int r; + if (argc < 2) + usage(); + + cmd_init(); + base_cmd_init(); + contribuinte_cmd_init(); + rendimento_cmd_init(); + pagamento_cmd_init(); + calcula_cmd_init(); + gera_cmd_init(); + + filename = argv[1]; + r = process(filename); + if (r) + return 1; + return 0; +} diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..6cf16d1 --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,6 @@ +check_PROGRAMS = listtest +listtest_SOURCES = listtest.c +listtest_CFLAGS = -I$(top_srcdir)/lib +listtest_LDADD = -L$(top_builddir)/lib -lreceita + +TESTS = $(check_PROGRAMS) diff --git a/test/listtest.c b/test/listtest.c new file mode 100644 index 0000000..d40b78b --- /dev/null +++ b/test/listtest.c @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2012-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 +#include +#include +#include + +static char *names[] = { + "backwards", + "end to start", + "finish", + "english", +}; + +static char *sorting[] = { + "1", + "5", + "0", + "3", + "9", +}; + +static char *sorted[] = { + "0", + "1", + "3", + "5", + "9", +}; + +#define ALEN(x) (sizeof(x)/sizeof(x[0])) + +int main(int argc, char **argv) +{ + struct list *l; + int i; + int arraylen; + + arraylen = sizeof(names)/sizeof(names[0]); + + l = list_new(); + list_add(&l, "GNU"); + list_free(l, NULL); + printf("Single static list test pass\n"); + fflush(stdout); + + l = list_new(); + for (i = 0; i < arraylen; i++) { + list_add(&l, strdup(names[i])); + } + for (i = 0; i < arraylen; i++) { + if (strcmp(list_get(l, i), names[i])) + return 1; + } + list_free(l, free); + printf("Multiple add ordering list test pass\n"); + fflush(stdout); + + l = list_new(); + for (i = 0; i < arraylen; i++) { + list_insert(&l, 0, strdup(names[i])); + } + for (i = 0; i < arraylen; i++) { + if (strcmp(list_get(l, i), names[arraylen - i - 1])) + return 1; + } + list_free(l, free); + printf("Multiple insertion at start ordering list test pass\n"); + fflush(stdout); + + l = list_new(); + for (i = 0; i < arraylen; i++) { + list_insert(&l, i, strdup(names[i])); + } + for (i = 0; i < arraylen; i++) { + if (strcmp(list_get(l, i), names[i])) + return 1; + } + list_free(l, free); + printf("Multiple insertion at end ordering list test pass\n"); + fflush(stdout); + + l = list_new(); + for (i = 0; i < ALEN(sorting); i++) { + list_insert_ordered(&l, strdup(sorting[i]), + (sort_function_t *) strcmp); + } + for (i = 0; i < ALEN(sorted); i++) { + if (strcmp(list_get(l, i), sorted[i])) + return 1; + } + list_free(l, free); + printf("Ordered insertion list test pass\n"); + fflush(stdout); + + return 0; +} diff --git a/token.c b/token.c deleted file mode 100644 index 50b82de..0000000 --- a/token.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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 "token.h" -#include -#include -#include - -static char * token_unescape(char *start, char *end) -{ - int escape = 0; - char *s, *r, *n; - s = n = malloc(end - start); - if (!s) - return NULL; - for (r = start + 1; r < end; r++) { - if (escape) { - *s = *r; - s++; - escape = 0; - } else if (*r == '\\') { - escape = 1; - } else { - *s = *r; - s++; - } - } - *s = 0; - return n; -} - -static char * token_next(char *line, char **end) -{ - char *start; - char *lend; - char cend; - int escape = 0; - start = line; - while (isspace(*start)) - start++; - if (*start == '"') { - lend = start + 1; - while (*lend) { - cend = *lend; - lend++; - if (escape) - escape = 0; - else if (cend == '\\') - escape = 1; - else if (cend == '"') - break; - } - } else { - lend = start; - while (*lend && !isspace(*lend)) - lend++; - } - *end = lend; - return start; -} - -char ** tokens_new(char *line) -{ - char **args = NULL; - size_t alloc = 0; - size_t next = 0; - const int inc = 8; - int more = 0; - char *start; - char *end; - int i; - end = line; - do { - if (next <= alloc) { - char **nargs; - alloc += inc; - nargs = realloc(args, alloc * sizeof(char *)); - if (!nargs) { - for (i = 0; i < alloc - inc; i++) - if (args[i]) - free(args[i]); - free(args); - return NULL; - } - for (i = alloc - inc; i < alloc; i++) - nargs[i] = NULL; - args = nargs; - } - start = token_next(end, &end); - if (start == end) - break; - if (*start == '"') { - args[next] = token_unescape(start, end - 1); - } else { - args[next] = strndup(start, end - start); - } - if (!args[next]) { - for (i = 0; i < alloc; i++) - if (args[i]) - free(args[i]); - free(args); - return NULL; - } - next++; - } while (1); - return args; -} - -void tokens_free(char **args) -{ - char **arg; - for (arg = args; *arg; arg++) - free(*arg); - free(args); -} diff --git a/token.h b/token.h deleted file mode 100644 index 5050c26..0000000 --- a/token.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 _TOKEN_H -#define _TOKEN_H - -char ** tokens_new(char *line); -void tokens_free(char **args); - -#endif diff --git a/totais.c b/totais.c deleted file mode 100644 index ca79125..0000000 --- a/totais.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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 "totais.h" -#include -#include -#include -#include "pmhash.h" - -int totais_add(struct declaracao *dec, char *key, long long val) -{ - long long *p; - int r = -ENOMEM; - p = pmhash_get(dec->totais, key); - if (!p) { - p = malloc(sizeof(*p)); - if (!p) - goto out_p; - key = strdup(key); - if (!key) - goto out_key; - r = pmhash_add(&dec->totais, key, p); - if (r) - goto out_hash; - *p = 0; - } - *p += val; - return 0; -out_hash: - free(key); -out_key: - free(p); -out_p: - return r; -} - -long long totais_get(struct declaracao *dec, char *key) -{ - long long *p; - p = pmhash_get(dec->totais, key); - if (!p) - return 0; - return *p; -} diff --git a/totais.h b/totais.h deleted file mode 100644 index 45583f9..0000000 --- a/totais.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 _TOTAIS_H -#define _TOTAIS_H - -#include "declaracao.h" - -int totais_add(struct declaracao *dec, char *key, long long val); -long long totais_get(struct declaracao *dec, char *key); - -#endif diff --git a/util.c b/util.c deleted file mode 100644 index 90ca426..0000000 --- a/util.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 "util.h" -#include -#include -#include - -int set_llong(char *str, long long *val) -{ - char *end = NULL; - errno = 0; - *val = strtoll(str, &end, 0); - if (end && *end) - return -EINVAL; - if (errno == ERANGE) - return -ERANGE; - return 0; -} - -int set_int(char **args, int argc, int *val) -{ - char *end = NULL; - if (argc != 2) - return -EINVAL; - errno = 0; - *val = strtol(args[1], &end, 0); - if (end && *end) - return -EINVAL; - if (errno == ERANGE) - return -ERANGE; - return 0; -} - -int set_string(char **args, int argc, char **str) -{ - if (argc != 2) - return -EINVAL; - *str = strdup(args[1]); - if (!*str) - return -errno; - return 0; -} diff --git a/util.h b/util.h deleted file mode 100644 index 758d67b..0000000 --- a/util.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 _UTIL_H -#define _UTIL_H - -int set_llong(char *str, long long *val); -int set_int(char **args, int argc, int *val); -int set_string(char **args, int argc, char **str); - -#define SET_INT_(suffix, command, attr) \ -static int run_##suffix(struct declaracao *dec, char **args, int argc) \ -{ \ - int val; \ - int r = set_int(args, argc, &val); \ - if (r) \ - return r; \ - dec->attr = val; \ - return 0; \ -} \ -static struct cmd cmd_##suffix = { \ - .name = #command, \ - .run = run_##suffix, \ -}; - -#define SET_INT(attr) SET_INT_(attr, attr, attr) - -#define SET_STRING_(suffix, command, attr) \ -static int run_##suffix(struct declaracao *dec, char **args, int argc) \ -{ \ - char *val; \ - int r = set_string(args, argc, &val); \ - if (r) \ - return r; \ - dec->attr = val; \ - return 0; \ -} \ -static struct cmd cmd_##suffix = { \ - .name = #command, \ - .run = run_##suffix, \ -} - -#define SET_STRING(attr) SET_STRING_(attr, attr, attr) - -static inline long long reais(long long val) -{ - return val / 100LL; -} - -/* Sempre retorna um valor positivo. */ -static inline int centavos(long long val) -{ - if (val > 0LL) - return (int) (val % 100LL); - else - return (int) (-val % 100LL); -} - -#endif