diff --git a/src/core/handlers.c b/src/core/handlers.c index 96ff0be..3ca054c 100644 --- a/src/core/handlers.c +++ b/src/core/handlers.c @@ -50,7 +50,7 @@ return 1; } -GString* bot_exec_report(gchar * const reportScript, gchar * const csvfile) +GString* bot_exec_report(gchar * const reportScript, gchar * const csvfile, int days) { FILE *fp; size_t cap; @@ -58,7 +58,7 @@ GString *command = g_string_new(""); GString *output = g_string_new(""); - g_string_printf(command, "%s %s 10 it", reportScript, csvfile); // TODO variable number of expenses, daily ecc + g_string_printf(command, "%s %s 10 %d it", reportScript, days, csvfile); // TODO variable number of expenses, daily ecc fp = popen(command->str,"r"); while( getline(&buf, &cap, fp) != -1 ){ @@ -122,7 +122,7 @@ } break; case BOT_CMD_REPORT: - replytext = bot_exec_report("./python/report.sh", bot_get_filename()->str); + replytext = bot_exec_report("./python/report.sh", bot_get_filename()->str, bot_get_report().days); break; default: g_string_printf(replytext, "Non ho riconosciuto il comando: %s", intext->str); diff --git a/src/expenses/grammar.y b/src/expenses/grammar.y index 6a73067..c22b889 100644 --- a/src/expenses/grammar.y +++ b/src/expenses/grammar.y @@ -33,22 +33,24 @@ %union { int res; /* return value of C procedure */ - double val; /* For returning numbers. */ + double val; /* For returning tnbers. */ BotCommand cmd; BotReportTimespan tsp; + BotReport rp; GString *str; } %token TOK_INT %token TOK_DOUBLE %token TOK_CMD_IN TOK_CMD_OUT TOK_CMD_DEL TOK_CMD_REPORT TOK_CMD_REMINDER -%token TOK_TS_DAILY TOK_TS_WEEKLY TOK_TS_MONTHLY TOK_TS_YEARLY +%token TOK_TS_DAY TOK_TS_WEEK TOK_TS_MONTH TOK_TS_YEAR %token TOK_USER /* string for user */ %token TOK_STR /* string for notes */ %type update; %type value; -%type ts; %type msg; +%type days; +%type tn; %% input: /* empty */ @@ -70,11 +72,12 @@ bot_set_last_command($1); bot_set_last_reminder(abs($2)); } - | TOK_CMD_DEL TOK_INT { printf("delete: %d\n", $2); $$ = 0; } - | TOK_CMD_REPORT ts { printf("report: %d\n", $2); $$ = 0; } // TODO - | TOK_CMD_REPORT - { bot_set_last_command($1); - printf("standard report"); $$ = 0; + | TOK_CMD_DEL TOK_INT { printf("delete: %d\n", $2); $$ = 0; } // TODO + | TOK_CMD_REPORT days + { + bot_set_last_command($1); + bot_set_report($2); + printf("timestamp report: %d\n", $2.days); $$ = 0; } value: TOK_INT { $$ = (double)$1; } @@ -85,11 +88,17 @@ | TOK_CMD_OUT ; -ts: TOK_TS_DAILY - | TOK_TS_WEEKLY - | TOK_TS_MONTHLY - | TOK_TS_YEARLY +/* convert a timestamp into num. of days. No calendar taken into account */ +days: /* empty */ { $$ = (BotReport) { .days = 1 }; } + | TOK_TS_DAY tn { $$ = (BotReport) { .days = $2 }; } + | TOK_TS_WEEK tn { $$ = (BotReport) { .days = 7*$2 }; } + | TOK_TS_MONTH tn { $$ = (BotReport) { .days = 31*$2 }; } + | TOK_TS_YEAR tn { $$ = (BotReport) { .days = 365*$2 }; } ; + +tn: /* empty */ { $$ = 1; } + | TOK_INT { $$ = $1; } + ; %% /* Parse a string with Flex+Bison instead of a file / stdout */ diff --git a/src/expenses/lex.l b/src/expenses/lex.l index 8c71048..40b857b 100644 --- a/src/expenses/lex.l +++ b/src/expenses/lex.l @@ -1,4 +1,4 @@ -/* expenses.l: Expenses lexical analysis file +/* expenses.l: Expenses lexical anasis file * TODO free memory allocated with g_string_new AFTER the parser is done (see grammar.y) * TODO support italian-style floating point numbers e.g. 12,34 */ @@ -22,10 +22,10 @@ del|storno yylval.cmd=BOT_CMD_DELETE; return TOK_CMD_DEL; reminder|ricordami yylval.cmd=BOT_CMD_REMINDER; return TOK_CMD_REMINDER; report yylval.cmd=BOT_CMD_REPORT; return TOK_CMD_REPORT; -daily|giorno yylval.tsp=BOT_TS_DAILY; return TOK_TS_DAILY; -weekly|sett|settimana yylval.tsp=BOT_TS_WEEKLY; return TOK_TS_WEEKLY; -monthly|mese yylval.tsp=BOT_TS_MONTHLY; return TOK_TS_MONTHLY; -yearly|anno yylval.tsp=BOT_TS_YEARLY; return TOK_TS_YEARLY; +day|giorno yylval.tsp=BOT_TS_DAY; return TOK_TS_DAY; +week|sett|settimana yylval.tsp=BOT_TS_WEEK; return TOK_TS_WEEK; +month|mese yylval.tsp=BOT_TS_MONTH; return TOK_TS_MONTH; +year|anno yylval.tsp=BOT_TS_YEAR; return TOK_TS_YEAR; [a-zA-Z0-9_]+ yylval.str=g_string_new(yytext); return TOK_USER; /* user can't have special characters */ \"[^\n]+\" yylval.str=g_string_new(yytext); return TOK_STR; /* anything excepts '\n'; needs to be at the bottom of flex rules here */ [ \t]+ /* ignore whitespace */; diff --git a/src/expenses/task.c b/src/expenses/task.c index cf0bbec..ce0ecaa 100644 --- a/src/expenses/task.c +++ b/src/expenses/task.c @@ -10,6 +10,7 @@ /* protected globals */ static BotCommand g_lastCommand = BOT_CMD_NONE; +static BotReport g_report = { 1 }; static BotReminder *g_tmp_lastReminder = NULL; /* store a thread per user */ @@ -54,6 +55,16 @@ g_tmp_lastReminder = reminder; } +BotReport bot_get_report() +{ + return g_report; +} + +void bot_set_report(const BotReport cmd) +{ + g_report = cmd; +} + void free_bot_task (BotTask * data) { /* cancel and wait for the thread to finish */ diff --git a/src/expenses/task.h b/src/expenses/task.h index 4de7305..9e9bca8 100644 --- a/src/expenses/task.h +++ b/src/expenses/task.h @@ -16,6 +16,9 @@ BotCommand bot_get_last_command(); void bot_set_last_command(BotCommand cmd); +BotReport bot_get_report(); +void bot_set_report(BotReport cmd); + void bot_task_table_insert(gchar * const key, BotTask * const value); /* start a thread and update task table */ diff --git a/src/expenses/types.h b/src/expenses/types.h index ef1a773..bf103fb 100644 --- a/src/expenses/types.h +++ b/src/expenses/types.h @@ -17,10 +17,10 @@ } BotCommand; typedef enum BotReportTimespan { - BOT_TS_DAILY, - BOT_TS_WEEKLY, - BOT_TS_MONTHLY, - BOT_TS_YEARLY + BOT_TS_DAY, + BOT_TS_WEEK, + BOT_TS_MONTH, + BOT_TS_YEAR } BotReportTimespan; typedef struct BotExpenseEntry { @@ -37,6 +37,10 @@ unsigned int minutes; } BotReminder; +typedef struct BotReport { + int days; +} BotReport; + /* Parser definitions */ int yylex(); int yyparse();