From 5e0d8ecba2153b5038e7ce797d0607df3bd3c78d Mon Sep 17 00:00:00 2001 From: mazze Date: Mon, 4 Jun 2012 19:12:17 +0000 Subject: [PATCH] Use poolmem and allocate memory dynamically. Bugfixes. git-svn-id: https://svn.aros.org/svn/aros/trunk/AROS@44943 fb15a70f-31f2-0310-bbcc-cdcc74a49acc --- workbench/c/R/gui.c | 18 +++--- workbench/c/R/main.c | 152 ++++++++++++++++++++++++++++++++++++++------------- workbench/c/R/r.h | 10 ++-- 3 files changed, 128 insertions(+), 52 deletions(-) diff --git a/workbench/c/R/gui.c b/workbench/c/R/gui.c index 54298c5249..68188d2a1f 100644 --- a/workbench/c/R/gui.c +++ b/workbench/c/R/gui.c @@ -62,13 +62,13 @@ AROS_UFH3S(void, pop_single_btn_func, ) { ULONG buflen = strlen(request->fr_Drawer) + strlen(request->fr_File) + 10; - TEXT *buffer = AllocVec(buflen, MEMF_ANY); + TEXT *buffer = AllocPooled(poolmem, buflen); if (buffer) { strcpy(buffer, request->fr_Drawer); AddPart(buffer, request->fr_File, buflen); SET(*str_object, MUIA_String_Contents, buffer); - FreeVec(buffer); + FreePooled(poolmem, buffer, buflen); } } @@ -104,7 +104,7 @@ AROS_UFH3S(void, pop_multi_btn_func, buflen += strlen(request->fr_Drawer) + strlen(request->fr_ArgList[i].wa_Name) + 5; } - TEXT *buffer = AllocVec(buflen, MEMF_ANY); + TEXT *buffer = AllocPooled(poolmem, buflen); if (buffer) { TEXT *ptr = buffer; @@ -113,13 +113,13 @@ AROS_UFH3S(void, pop_multi_btn_func, { *ptr++ = '\"'; strcpy(ptr, request->fr_Drawer); - AddPart(ptr, request->fr_File, buffer + buflen - ptr - 2); + AddPart(ptr, request->fr_ArgList[i].wa_Name, buffer + buflen - ptr - 2); ptr = buffer + strlen(buffer); strcpy(ptr, "\" "); ptr += 2; } SET(*str_object, MUIA_String_Contents, buffer); - FreeVec(buffer); + FreePooled(poolmem, buffer, buflen); } } @@ -175,7 +175,7 @@ BOOL create_gui(struct Req *req) if (req->cargs[i].s_flag || req->cargs[i].t_flag) // Switch { - new_obj = Label(req->cargs[i].argname); + new_obj = Label1(req->cargs[i].argname); if (new_obj) { chk_group_tags[chk_cnt].ti_Tag = Child; @@ -193,7 +193,7 @@ BOOL create_gui(struct Req *req) } else if (req->cargs[i].n_flag) // Numeric { - new_obj = Label(req->cargs[i].argname); + new_obj = Label2(req->cargs[i].argname); if (new_obj) { str_group_tags[str_cnt].ti_Tag = Child; @@ -216,7 +216,7 @@ BOOL create_gui(struct Req *req) else if (req->cargs[i].m_flag) // Multiple { Object *pop_btn; - new_obj = Label(req->cargs[i].argname); + new_obj = Label2(req->cargs[i].argname); if (new_obj) { str_group_tags[str_cnt].ti_Tag = Child; @@ -247,7 +247,7 @@ BOOL create_gui(struct Req *req) else { Object *pop_btn; - new_obj = Label(req->cargs[i].argname); + new_obj = Label2(req->cargs[i].argname); if (new_obj) { str_group_tags[str_cnt].ti_Tag = Child; diff --git a/workbench/c/R/main.c b/workbench/c/R/main.c index 6dc21debb7..d95804c02a 100644 --- a/workbench/c/R/main.c +++ b/workbench/c/R/main.c @@ -17,6 +17,9 @@ #include "r.h" #define ARG_TEMPLATE "FILENAME,PROFILE/K,NOGUI/S,ARGUMENTS/F" +#define CMD_TMPLATE_SIZE (2000) + +APTR poolmem; enum { @@ -40,15 +43,15 @@ static void clean_exit(struct Req *req, CONST_STRPTR s) if (req) { if (req->rda) FreeArgs(req->rda); - FreeVec(req); } cleanup_gui(); + DeletePool(poolmem); exit(retval); } static struct Req *alloc_req(void) { - return AllocVec(sizeof (struct Req), MEMF_ANY | MEMF_CLEAR); + return AllocPooled(poolmem, sizeof (struct Req)); } static BOOL handle_args(struct Req *req, int argc, char **argv) @@ -115,6 +118,9 @@ static BOOL check_exist(struct Req *req) { BOOL retval = FALSE; + if (req->filename == NULL) + return FALSE; + if (strchr(req->filename, ':')) // absolute path { if (is_file(req->filename)) @@ -162,8 +168,8 @@ static BOOL get_template(struct Req *req) TEXT out_file_name[30]; TEXT in_file_name[30]; - - TEXT cmd[255]; + TEXT *cmd = NULL; + ULONG cmd_len = 0; LONG i; LONG cmd_res = 0; @@ -173,6 +179,13 @@ static BOOL get_template(struct Req *req) goto cleanup; } + cmd_len = strlen(req->filename) + 20; + cmd = AllocPooled(poolmem, cmd_len); + if (cmd == NULL) + { + goto cleanup; + } + for (i = 0; i < 20 && output_fh == NULL; i++) { sprintf(out_file_name, "t:%08u.request.outfile", i); @@ -200,8 +213,8 @@ static BOOL get_template(struct Req *req) } // append "*>NIL: ?" to the command - strlcpy(cmd, req->filename, sizeof cmd); - strlcat(cmd, " *>NIL: ?", sizeof cmd); + strlcpy(cmd, req->filename, cmd_len); + strlcat(cmd, " *>NIL: ?", cmd_len); // shut up DOS error message struct Process *me = (struct Process*)FindTask(NULL); @@ -215,9 +228,15 @@ static BOOL get_template(struct Req *req) // restore window ptr me->pr_WindowPtr = oldwin; + req->cmd_template = AllocPooled(poolmem, CMD_TMPLATE_SIZE); // FIXME get mem size from file size + if (req->cmd_template == NULL) + { + goto cleanup; + } + // go to the beginning of the output file and read the template Seek(output_fh, 0, OFFSET_BEGINNING); - if (FGets(output_fh, req->cmd_template, sizeof req->cmd_template)) + if (FGets(output_fh, req->cmd_template, CMD_TMPLATE_SIZE)) { D(bug("[R] template read: %s\n", req->cmd_template)); retval = TRUE; @@ -235,19 +254,49 @@ cleanup: DeleteFile(out_file_name); } + FreePooled(poolmem, cmd, cmd_len); + return retval; } static BOOL parse_template(struct Req *req) { - TEXT *chr = req->cmd_template; + TEXT *chr; LONG len; + LONG arg; if (req->cmd_template[0] == '\0') return FALSE; - while (req->arg_cnt < MAX_ARG_CNT) + // count number of arguments + for + ( + req->arg_cnt = 1, chr = req->cmd_template; + *chr != '\0' && req->arg_cnt < 50; + chr++ + ) + { + if (*chr == ',') + { + req->arg_cnt++; + } + } + + D(bug("[R/parse_template args found %d\n", req->arg_cnt)); + + req->cargs = AllocPooled(poolmem, sizeof (struct CArg) * req->arg_cnt); + if (req->cargs == NULL) + { + return FALSE; + } + + for + ( + arg = 0, chr = req->cmd_template; + arg < req->arg_cnt; + chr++ + ) { // read name TEXT *name_start = chr; @@ -272,10 +321,16 @@ static BOOL parse_template(struct Req *req) if (len == 0) return FALSE; - if (len >= MAX_NAME_CNT - 1) - len = MAX_NAME_CNT - 1; + if (len >= 35) + len = 35; - memcpy(req->cargs[req->arg_cnt].argname, name_start, len); + req->cargs[arg].argname = AllocPooled(poolmem, len + 1); + if (req->cargs[arg].argname == NULL) + { + return FALSE; + } + memcpy(req->cargs[arg].argname, name_start, len); + req->cargs[arg].argname[len] = '\0'; // read modifiers while (*chr == '/') @@ -283,31 +338,31 @@ static BOOL parse_template(struct Req *req) switch (*(chr + 1)) { case 'A': - req->cargs[req->arg_cnt].a_flag = TRUE; + req->cargs[arg].a_flag = TRUE; chr++; break; case 'F': - req->cargs[req->arg_cnt].f_flag = TRUE; + req->cargs[arg].f_flag = TRUE; chr++; break; case 'K': - req->cargs[req->arg_cnt].k_flag = TRUE; + req->cargs[arg].k_flag = TRUE; chr++; break; case 'M': - req->cargs[req->arg_cnt].m_flag = TRUE; + req->cargs[arg].m_flag = TRUE; chr++; break; case 'N': - req->cargs[req->arg_cnt].n_flag = TRUE; + req->cargs[arg].n_flag = TRUE; chr++; break; case 'S': - req->cargs[req->arg_cnt].s_flag = TRUE; + req->cargs[arg].s_flag = TRUE; chr++; break; case 'T': - req->cargs[req->arg_cnt].t_flag = TRUE; + req->cargs[arg].t_flag = TRUE; chr++; break; default: @@ -316,10 +371,9 @@ static BOOL parse_template(struct Req *req) } chr++; } - req->arg_cnt++; + arg++; if (*chr != ',') break; - chr++; } return TRUE; } @@ -330,8 +384,25 @@ static void execute_command(struct Req *req) { ULONG i; CONST_STRPTR str; + TEXT *cmd; - strlcpy(req->cmd_template, req->filename, sizeof req->cmd_template); + ULONG cmd_size = strlen(req->filename) + 5; + for (i = 0; i < req->arg_cnt; i++) + { + cmd_size += strlen(req->cargs[i].argname) + 5; + if (!req->cargs[i].s_flag && !req->cargs[i].t_flag) + { + cmd_size += strlen(get_gui_string(&req->cargs[i])) + 5; + } + } + + cmd = AllocPooled(poolmem, cmd_size); + if (cmd == NULL) + { + return; + } + + strcpy(cmd, req->filename); for (i = 0; i < req->arg_cnt; i++) { @@ -339,8 +410,8 @@ static void execute_command(struct Req *req) { if (get_gui_bool(&req->cargs[i])) { - strlcat(req->cmd_template, " ", sizeof req->cmd_template); - strlcat(req->cmd_template, req->cargs[i].argname, sizeof req->cmd_template); + strcat(cmd, " "); + strcat(cmd, req->cargs[i].argname); } } else if (req->cargs[i].n_flag) @@ -348,10 +419,10 @@ static void execute_command(struct Req *req) str = get_gui_string(&req->cargs[i]); if (str[0] != '\0') { - strlcat(req->cmd_template, " ", sizeof req->cmd_template); - strlcat(req->cmd_template, req->cargs[i].argname, sizeof req->cmd_template); - strlcat(req->cmd_template, " ", sizeof req->cmd_template); - strlcat(req->cmd_template, str, sizeof req->cmd_template); + strcat(cmd, " "); + strcat(cmd, req->cargs[i].argname); + strcat(cmd, " "); + strcat(cmd, str); } } else @@ -361,31 +432,32 @@ static void execute_command(struct Req *req) if (str[0] != '\0') { // do we have a space character in the string? - // if yes: quote it - if (strchr(str, ' ') && str[0] != '\"') + // if yes: quote it. + // For /M the quotes are already set by the GUI + if (!req->cargs[i].m_flag && strchr(str, ' ') && str[0] != '\"') { quote = TRUE; } - strlcat(req->cmd_template, " ", sizeof req->cmd_template); - strlcat(req->cmd_template, req->cargs[i].argname, sizeof req->cmd_template); - strlcat(req->cmd_template, " ", sizeof req->cmd_template); + strcat(cmd, " "); + strcat(cmd, req->cargs[i].argname); + strcat(cmd, " "); if (quote) { - strlcat(req->cmd_template, "\"", sizeof req->cmd_template); + strcat(cmd, "\""); } - strlcat(req->cmd_template, str, sizeof req->cmd_template); + strcat(cmd, str); if (quote) { - strlcat(req->cmd_template, "\"", sizeof req->cmd_template); + strcat(cmd, "\""); } } } } - D(bug("[R] executing command %s\n", req->cmd_template)); + D(bug("[R] executing command %s\n", cmd)); LONG result = System ( - req->cmd_template, + cmd, NULL ); if (result) @@ -397,6 +469,10 @@ static void execute_command(struct Req *req) int main(int argc, char **argv) { + poolmem = CreatePool(MEMF_ANY | MEMF_CLEAR, 2000, 2000); + if (poolmem == NULL) + clean_exit(NULL, "r: Can't create poolmem\n"); + struct Req *req = alloc_req(); if (req == NULL) clean_exit(req, "r: Can't allocate struct Req\n"); diff --git a/workbench/c/R/r.h b/workbench/c/R/r.h index 8ac05e4595..4c84e1893f 100644 --- a/workbench/c/R/r.h +++ b/workbench/c/R/r.h @@ -9,12 +9,10 @@ #include #include -#define MAX_ARG_CNT (30) -#define MAX_NAME_CNT (30) struct CArg { - TEXT argname[MAX_NAME_CNT]; + TEXT *argname; Object *object; BOOL a_flag; BOOL f_flag; @@ -33,15 +31,17 @@ struct Req BOOL nogui; STRPTR arguments; - TEXT cmd_template[2000]; + TEXT *cmd_template; // template string from outfile ULONG arg_cnt; - struct CArg cargs[MAX_ARG_CNT]; // TODO: dynamic allocation + struct CArg *cargs; BOOL do_execute; // TRUE if Execute button was clicked }; +extern APTR poolmem; + BOOL create_gui(struct Req *req); BOOL handle_gui(struct Req *req); -- 2.11.4.GIT