-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
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "cmd.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _BASE_H
-#define _BASE_H
-
-int base_cmd_init(void);
-
-#endif
aclocal
autoheader
autoconf
+libtoolize --copy
automake --add-missing --copy --force --foreign
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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 <errno.h>
-#include <stdio.h>
-#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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "cmd.h"
-#include "pmhash.h"
-#include "token.h"
-#include <errno.h>
-#include <stdlib.h>
-
-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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _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
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)
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _CONTRIBUINTE_H
-#define _CONTRIBUINTE_H
-
-#include <stdio.h>
-
-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
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#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 <filename>\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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "declaracao.h"
-#include <stdlib.h>
-#include <string.h>
-#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);
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _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
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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 <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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
--- /dev/null
+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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "cmd.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _BASE_H
+#define _BASE_H
+
+int base_cmd_init(void);
+
+#endif
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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 <errno.h>
+#include <stdio.h>
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "cmd.h"
+#include "pmhash.h"
+#include "token.h"
+#include <errno.h>
+#include <stdlib.h>
+
+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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _CONTRIBUINTE_H
+#define _CONTRIBUINTE_H
+
+#include <stdio.h>
+
+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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "declaracao.h"
+#include <stdlib.h>
+#include <string.h>
+#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);
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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 <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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
--- /dev/null
+/*
+ * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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 <stdlib.h>
+#include <string.h>
+
+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);
+}
--- /dev/null
+/*
+ * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "pagamento.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _PAGAMENTO_H
+#define _PAGAMENTO_H
+
+#include <stdio.h>
+#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
--- /dev/null
+/*
+ * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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 <stdlib.h>
+#include <string.h>
+
+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);
+}
--- /dev/null
+/*
+ * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "rendimento.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _RENDIMENTO_H
+#define _RENDIMENTO_H
+
+#include <stdio.h>
+#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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "token.h"
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+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);
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _TOKEN_H
+#define _TOKEN_H
+
+char ** tokens_new(char *line);
+void tokens_free(char **args);
+
+#endif
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "totais.h"
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _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
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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 <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+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;
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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
+++ /dev/null
-/*
- * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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 <stdlib.h>
-#include <string.h>
-
-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);
-}
+++ /dev/null
-/*
- * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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
+++ /dev/null
-/*
- * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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 <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "pagamento.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <stdio.h>
-#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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _PAGAMENTO_H
-#define _PAGAMENTO_H
-
-#include <stdio.h>
-#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
+++ /dev/null
-/*
- * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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 <stdlib.h>
-#include <string.h>
-
-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);
-}
+++ /dev/null
-/*
- * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "rendimento.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <stdio.h>
-#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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _RENDIMENTO_H
-#define _RENDIMENTO_H
-
-#include <stdio.h>
-#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
--- /dev/null
+bin_PROGRAMS = declara
+
+declara_SOURCES = declara.c
+
+declara_CFLAGS = -I$(top_srcdir)/lib
+declara_LDFLAGS = -static
+declara_LDADD = -L$(top_builddir)/lib -lreceita
--- /dev/null
+/*
+ * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#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 <filename>\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;
+}
--- /dev/null
+check_PROGRAMS = listtest
+listtest_SOURCES = listtest.c
+listtest_CFLAGS = -I$(top_srcdir)/lib
+listtest_LDADD = -L$(top_builddir)/lib -lreceita
+
+TESTS = $(check_PROGRAMS)
--- /dev/null
+/*
+ * Copyright (C) 2012-2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ * 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 <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+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;
+}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "token.h"
-#include <ctype.h>
-#include <stdlib.h>
-#include <string.h>
-
-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);
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _TOKEN_H
-#define _TOKEN_H
-
-char ** tokens_new(char *line);
-void tokens_free(char **args);
-
-#endif
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include "totais.h"
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef _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
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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 <stdlib.h>
-#include <errno.h>
-#include <string.h>
-
-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;
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
- *
- * 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