set_int e set_string eram utilizados com argumentos dados a um comando e
sempre esperavam dois argumentos apenas, o que fazia com que seu uso não
fosse legível a não ser pelas macros SET_INT_ e SET_STRING_.
Movendo a lógica de teste de argumentos para as macros, foi possível
simplificar set_int e set_string e tornar o uso de set_int em alguns
casos mais legível, bem como passar a utilizar set_string para verificar
erros de alocação de memória.
As macros SET_INT_ e SET_STRING_ foram também unificadas em SET_VAL_,
porém mantidas com os argumentos que aceitavam anteriormente, mas
fazendo uso desta última. SET_VAL_ foi melhorada para aceitar um texto
de ajuda e definir um texto de erro mais amigável ao usuário.
int r = 0;
bem = malloc(sizeof(*bem));
memset(bem, 0, sizeof(*bem));
int r = 0;
bem = malloc(sizeof(*bem));
memset(bem, 0, sizeof(*bem));
- bem->descricao = strdup(args[2]);
- /* TODO: consertar set_int para funcionar como set_llong */
- r += set_int(args, 2, &bem->codigo);
+ r += set_int(args[1], &bem->codigo);
+ r += set_string(args[2], &bem->descricao);
r += set_llong(args[3], &bem->valor_anterior);
r += set_llong(args[4], &bem->valor);
r += set_llong(args[3], &bem->valor_anterior);
r += set_llong(args[4], &bem->valor);
- if (!bem->descricao) {
- bem_free(bem);
- return NULL;
- }
if (r < 0 || bem->codigo < 0 ||
bem->valor_anterior < 0 || bem->valor < 0) {
bem_free(bem);
if (r < 0 || bem->codigo < 0 ||
bem->valor_anterior < 0 || bem->valor < 0) {
bem_free(bem);
- dec->conjuge.cpf = strdup(args[1]);
-
+ r += set_string(args[1], &dec->conjuge.cpf);
r += set_llong(args[2], &dec->conjuge.base);
r += set_llong(args[3], &dec->conjuge.imposto);
r += set_llong(args[4], &dec->conjuge.isento);
r += set_llong(args[5], &dec->conjuge.exclusivo);
r += set_llong(args[6], &dec->conjuge.rendpj_exigibilidade_suspensa);
r += set_llong(args[7], &dec->conjuge.total);
r += set_llong(args[2], &dec->conjuge.base);
r += set_llong(args[3], &dec->conjuge.imposto);
r += set_llong(args[4], &dec->conjuge.isento);
r += set_llong(args[5], &dec->conjuge.exclusivo);
r += set_llong(args[6], &dec->conjuge.rendpj_exigibilidade_suspensa);
r += set_llong(args[7], &dec->conjuge.total);
- r += set_int(args + 7, 2, &dec->conjuge.entregou);
+ r += set_int(args[8], &dec->conjuge.entregou);
- if (!dec->conjuge.cpf) {
- conjuge_free(dec);
- return -ENOMEM;
- }
if (r < 0 || dec->conjuge.base < 0 || dec->conjuge.imposto < 0 ||
dec->conjuge.isento < 0 || dec->conjuge.exclusivo < 0 ||
dec->conjuge.rendpj_exigibilidade_suspensa < 0 ||
if (r < 0 || dec->conjuge.base < 0 || dec->conjuge.imposto < 0 ||
dec->conjuge.isento < 0 || dec->conjuge.exclusivo < 0 ||
dec->conjuge.rendpj_exigibilidade_suspensa < 0 ||
struct dependente *dependente;
int r = 0;
dependente = malloc(sizeof(*dependente));
struct dependente *dependente;
int r = 0;
dependente = malloc(sizeof(*dependente));
- /* TODO: consertar set_int para funcionar como set_llong */
- r += set_int(args, 2, &dependente->codigo);
- dependente->nome = strdup(args[2]);
- dependente->dn = strdup(args[3]);
- dependente->cpf = strdup(args[4]);
- if (!dependente->nome || !dependente->dn || !dependente->cpf) {
- dependente_free(dependente);
- return NULL;
- }
+ r += set_int(args[1], &dependente->codigo);
+ r += set_string(args[2], &dependente->nome);
+ r += set_string(args[3], &dependente->dn);
+ r += set_string(args[4], &dependente->cpf);
if (r < 0 || dependente->codigo < 0) {
dependente_free(dependente);
return NULL;
if (r < 0 || dependente->codigo < 0) {
dependente_free(dependente);
return NULL;
struct isento *isento;
int r = 0;
isento = malloc(sizeof(*isento));
struct isento *isento;
int r = 0;
isento = malloc(sizeof(*isento));
- isento->cnpj = strdup(args[2]);
- isento->nome = strdup(args[3]);
- /* TODO: consertar set_int para funcionar como set_llong */
- r += set_int(args, 2, &isento->codigo);
+
+ r += set_int(args[1], &isento->codigo);
+ r += set_string(args[2], &isento->cnpj);
+ r += set_string(args[3], &isento->nome);
r += set_llong(args[4], &isento->valor);
r += set_llong(args[4], &isento->valor);
- if (!isento->cnpj || !isento->nome) {
- isento_free(isento);
- return NULL;
- }
if (r < 0 || isento->codigo < 0 ||
isento->valor < 0) {
isento_free(isento);
return NULL;
}
if (argc == 6) {
if (r < 0 || isento->codigo < 0 ||
isento->valor < 0) {
isento_free(isento);
return NULL;
}
if (argc == 6) {
- r = set_int(&args[4], 2, &isento->dependente);
+ r = set_int(args[5], &isento->dependente);
} else {
isento->dependente = 0;
}
} else {
isento->dependente = 0;
}
struct pagamento *pagamento;
int r = 0;
pagamento = malloc(sizeof(*pagamento));
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_int(args[1], &pagamento->codigo);
+ r += set_string(args[2], &pagamento->cnpj);
+ r += set_string(args[3], &pagamento->nome);
r += set_llong(args[4], &pagamento->pagamento);
r += set_llong(args[5], &pagamento->reembolso);
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;
}
if (argc == 7) {
if (r < 0 || pagamento->codigo < 0 ||
pagamento->pagamento < 0 || pagamento->reembolso < 0) {
pagamento_free(pagamento);
return NULL;
}
if (argc == 7) {
- r = set_int(&args[5], 2, &pagamento->dependente);
+ r = set_int(args[6], &pagamento->dependente);
} else {
pagamento->dependente = 0;
}
} else {
pagamento->dependente = 0;
}
#include <fcntl.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
-int set_llong(char *str, long long *val)
+int set_llong(char *arg, long long *val)
{
char *end = NULL;
errno = 0;
{
char *end = NULL;
errno = 0;
- *val = strtoll(str, &end, 0);
+ *val = strtoll(arg, &end, 0);
if (end && *end)
return -EINVAL;
if (errno == ERANGE)
if (end && *end)
return -EINVAL;
if (errno == ERANGE)
-int set_int(char **args, int argc, int *val)
+int set_int(char *arg, int *val)
- if (argc != 2)
- return -EINVAL;
- *val = strtol(args[1], &end, 0);
+ *val = strtol(arg, &end, 0);
if (end && *end)
return -EINVAL;
if (errno == ERANGE)
if (end && *end)
return -EINVAL;
if (errno == ERANGE)
-int set_string(char **args, int argc, char **str)
+int set_string(char *arg, char **str)
- if (argc != 2)
- return -EINVAL;
- *str = strdup(args[1]);
if (!*str)
return -errno;
return 0;
if (!*str)
return -errno;
return 0;
#ifndef _UTIL_H
#define _UTIL_H
#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) \
+int set_llong(char *arg, long long *val);
+int set_int(char *arg, int *val);
+int set_string(char *arg, char **str);
+
+#define SET_VAL_(suffix, command, attr, type, param, func, help_) \
static int run_##suffix(struct declaracao *dec, char **args, int argc) \
{ \
static int run_##suffix(struct declaracao *dec, char **args, int argc) \
{ \
- int val; \
- int r = set_int(args, argc, &val); \
+ type val; \
+ int r; \
+ if (argc != 2) { \
+ dec_set_error(dec, "Comando %s espera um parâmetro "#param".\n", args[0]); \
+ return -EINVAL; \
+ } \
+ r = func(args[1], &val); \
if (r) \
return r; \
dec->attr = val; \
if (r) \
return r; \
dec->attr = val; \
static struct cmd cmd_##suffix = { \
.name = #command, \
.run = run_##suffix, \
static struct cmd cmd_##suffix = { \
.name = #command, \
.run = run_##suffix, \
+#define SET_INT_(suffix, cmd, attr) SET_VAL_(suffix, cmd, attr, int, inteiro, set_int, NULL)
+#define SET_STRING_(suffix, cmd, attr) SET_VAL_(suffix, cmd, attr, char *, texto, set_string, NULL)
#define SET_INT(attr) SET_INT_(attr, attr, attr)
#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)
#define SET_STRING(attr) SET_STRING_(attr, attr, attr)
static inline long long reais(long long val)