diff options
author | Michael Larson <mike@vyatta.com> | 2010-12-02 20:06:08 -0800 |
---|---|---|
committer | Michael Larson <mike@vyatta.com> | 2010-12-02 20:06:08 -0800 |
commit | fd10ae2d46668a0faa20d26b361d781e41c4f61e (patch) | |
tree | 17952541740ecaf7f41f4b867a3a69dcc07d3b8c /src | |
parent | 4e16053e43fbdff6d8d9bca7441df061d2087df8 (diff) | |
download | vyatta-cfg-fd10ae2d46668a0faa20d26b361d781e41c4f61e.tar.gz vyatta-cfg-fd10ae2d46668a0faa20d26b361d781e41c4f61e.zip |
update to errloc. changed reader so that stderr/stdout is printed as it is received. Also added new -x option to commit to force messages to be printed via the old format.
Diffstat (limited to 'src')
-rw-r--r-- | src/cli_new.c | 130 | ||||
-rw-r--r-- | src/cli_val.h | 2 | ||||
-rw-r--r-- | src/cli_val_engine.c | 1 | ||||
-rw-r--r-- | src/commit2.c | 47 | ||||
-rw-r--r-- | src/exe_action.c | 2 |
5 files changed, 135 insertions, 47 deletions
diff --git a/src/cli_new.c b/src/cli_new.c index f3cfa3a..821d21b 100644 --- a/src/cli_new.c +++ b/src/cli_new.c @@ -82,8 +82,8 @@ static valstruct validate_value_val; /* value being validated /* Local function declarations: */ static int check_comp(vtw_node *cur); -static boolean check_syn_func(vtw_node *cur,const char **outbuf, const char* func,int line); -#define check_syn(cur,out_buf) check_syn_func((cur),(out_buf),__FUNCTION__,__LINE__) +static boolean check_syn_func(vtw_node *cur,const char *prepend_msg,boolean format, const char* func,int line); +#define check_syn(cur,msg_buf,format) check_syn_func((cur),(msg_buf),(format),__FUNCTION__,__LINE__) void copy_path(vtw_path *to, vtw_path *from); static int eval_va(valstruct *res, vtw_node *node); static int expand_string(char *p); @@ -692,14 +692,14 @@ void vtw_sort(valstruct *valp, vtw_sorted *sortp) /* returns FALSE if execution returns non-null, returns TRUE if every excution returns NULL */ -boolean execute_list(vtw_node *cur, vtw_def *def, const char **outbuf) +boolean execute_list(vtw_node *cur, vtw_def *def, const char *prepend_msg,boolean format) { boolean ret; int status; set_in_exec(TRUE); status = char2val(def, get_at_string(), &validate_value_val); if (status) return FALSE; - ret = check_syn(cur,outbuf); + ret = check_syn(cur,prepend_msg,format); free_val(&validate_value_val); set_in_exec(FALSE); return ret; @@ -1229,7 +1229,7 @@ static int change_var_value(const char* var_reference,const char* value, int act return ret; } -int system_out(const char *command, const char **outbuf); +int system_out(const char *command, const char *prepend_msg, boolean format); /**************************************************** check_syn: @@ -1237,7 +1237,7 @@ int system_out(const char *command, const char **outbuf); returns TRUE if all checks are OK, returns FALSE if check fails. ****************************************************/ -static boolean check_syn_func(vtw_node *cur,const char **outbuf,const char* func,int line) +static boolean check_syn_func(vtw_node *cur,const char *prepend_msg,boolean format,const char* func,int line) { int status; int ret; @@ -1247,21 +1247,36 @@ static boolean check_syn_func(vtw_node *cur,const char **outbuf,const char* func case LIST_OP: ret = TRUE; if (is_in_commit() || !cur->vtw_node_aux) { - ret = check_syn(cur->vtw_node_left,outbuf); + ret = check_syn(cur->vtw_node_left,prepend_msg,format); } if (!ret || !cur->vtw_node_right) /* or no right operand */ return ret; - return check_syn(cur->vtw_node_right,outbuf); + return check_syn(cur->vtw_node_right,prepend_msg,format); case HELP_OP: - ret = check_syn(cur->vtw_node_left,outbuf); + ret = check_syn(cur->vtw_node_left,prepend_msg,format); if (ret <= 0){ if (expand_string(cur->vtw_node_right->vtw_node_string) == VTWERR_OK) { - if (outbuf == NULL) { - OUTPUT_USER("%s\n", exe_string); + //NEED TO PROCESS THIS ACCORDING TO ERROR LOC STRING... + + if (strstr(exe_string,"_errloc_:[") != NULL) { + if (format == FALSE) { + OUTPUT_USER("%s\n\n",exe_string+strlen("_errloc_:")); + } + else { + OUTPUT_USER("%s\n\n",exe_string); + } } else { - strcat((char*)*outbuf, exe_string); - } + //currently set to format option for GUI client. + if (prepend_msg != NULL) { + if (format == FALSE) { + OUTPUT_USER("[%s]\n%s\n\n",prepend_msg,exe_string); + } + else { + OUTPUT_USER("_errloc_:[%s]\n%s\n\n",prepend_msg,exe_string); + } + } + } } } return ret; @@ -1325,7 +1340,7 @@ static boolean check_syn_func(vtw_node *cur,const char **outbuf,const char* func set_at_string(save_at); return FALSE; } - ret = system_out(exe_string,outbuf); + ret = system_out(exe_string,prepend_msg,format); if (ret) { set_at_string(save_at); return FALSE; @@ -1339,7 +1354,7 @@ static boolean check_syn_func(vtw_node *cur,const char **outbuf,const char* func if (status != VTWERR_OK) { return FALSE; } - ret = system_out(exe_string,outbuf); + ret = system_out(exe_string,prepend_msg,format); return !ret; case PATTERN_OP: /* left to var, right to pattern */ @@ -1377,15 +1392,15 @@ static boolean check_syn_func(vtw_node *cur,const char **outbuf,const char* func } case OR_OP: - ret = check_syn(cur->vtw_node_left,outbuf) || - check_syn(cur->vtw_node_right,outbuf); + ret = check_syn(cur->vtw_node_left,prepend_msg,format) || + check_syn(cur->vtw_node_right,prepend_msg,format); return ret; case AND_OP: - ret = check_syn(cur->vtw_node_left,outbuf) && - check_syn(cur->vtw_node_right,outbuf); + ret = check_syn(cur->vtw_node_left,prepend_msg,format) && + check_syn(cur->vtw_node_right,prepend_msg,format); return ret; case NOT_OP: - ret = check_syn(cur->vtw_node_left,outbuf); + ret = check_syn(cur->vtw_node_left,prepend_msg,format); return !ret; case COND_OP: /* aux field specifies cond type (GT, GE, etc.)*/ @@ -2197,7 +2212,7 @@ boolean validate_value(vtw_def *def, char *cp) ret = TRUE; if (def->actions && def->actions[syntax_act].vtw_list_head){ in_validate_val = TRUE; - ret = check_syn(def->actions[syntax_act].vtw_list_head,(const char **)NULL); + ret = check_syn(def->actions[syntax_act].vtw_list_head,(const char *)NULL,FALSE); in_validate_val = FALSE; } validate_value_free_and_return: @@ -2524,7 +2539,7 @@ old_system_out(const char *command) if (out_stream && restore_output() == -1) { return -1; } - + ret = system(command); if (out_stream && redirect_output() == -1) { return -1; @@ -2535,10 +2550,10 @@ old_system_out(const char *command) } int -system_out(const char *cmd, const char **outbuf) +system_out(const char *cmd, const char *prepend_msg, boolean format) { // fprintf(out_stream,"system out\n"); - if (outbuf == NULL) { + if (prepend_msg == NULL) { return old_system_out(cmd); } if (cmd == NULL) { @@ -2546,7 +2561,8 @@ system_out(const char *cmd, const char **outbuf) } int cp[2]; // Child to parent pipe - if( pipe(cp) < 0) { return -1; + if( pipe(cp) < 0) { + return -1; } pid_t pid = fork(); @@ -2559,6 +2575,7 @@ system_out(const char *cmd, const char **outbuf) close(cp[0]); close(cp[1]); int ret = 0; + // fprintf(out_stream,"executing: %s\n",cmd); if (execl("/bin/sh","sh","-c",cmd,NULL) == -1) { ret = errno; } @@ -2568,15 +2585,68 @@ system_out(const char *cmd, const char **outbuf) else { //parent char buf[8192]; + char last_char = '\0'; memset(buf,'\0',8192); close(cp[1]); - int ct = 0, total = 0; - while ((ct = read(cp[0], &buf, 8192)) > 0 && (total < 8192)) { - strcat((char*)*outbuf,buf); - memset(buf,'\0',8192); - total += ct; + boolean first = TRUE; + boolean print = FALSE; + fd_set rfds; + struct timeval tv; + + FD_ZERO(&rfds); + FD_SET(cp[0], &rfds); + tv.tv_sec = 1; + tv.tv_usec = 0; + + while (select(FD_SETSIZE, &rfds, NULL, NULL, &tv) != -1) { + int err = 0; + if ((err = read(cp[0], &buf, 8191)) > 0) { + print = TRUE; + if (first == TRUE) { + if (strstr(buf,"_errloc_:[") != NULL) { + if (format == FALSE) { + fprintf(out_stream,"%s",buf+strlen("_errloc_:")); + } + else { + fprintf(out_stream,"%s",buf); + } + } + else { + //currently set to format option for GUI client. + if (prepend_msg != NULL) { + if (format == FALSE) { + fprintf(out_stream,"[%s]\n%s",prepend_msg,buf); + } + else { + fprintf(out_stream,"_errloc_:[%s]\n%s",prepend_msg,buf); + } + } + } + } + else { + fprintf(out_stream,"%s",buf); + } + first = FALSE; + last_char = buf[strlen(buf)-2]; + memset(buf,'\0',8192); + } + else if (err == -1 && errno == EAGAIN) { + //try again + } + else { + break; + } + FD_ZERO(&rfds); + FD_SET(cp[0], &rfds); + tv.tv_sec = 1; + tv.tv_usec = 0; + + fflush(NULL); } + if (print == TRUE && last_char != '\n') { + fprintf(out_stream,"\n"); + } //now wait on child to kick the bucket int status; wait(&status); diff --git a/src/cli_val.h b/src/cli_val.h index 536d964..7f1f512 100644 --- a/src/cli_val.h +++ b/src/cli_val.h @@ -116,7 +116,7 @@ extern void free_val(valstruct *val); extern void touch(void); extern int mkdir_p(const char *path); -extern boolean execute_list(vtw_node *cur, vtw_def *def, const char **outbuf); +extern boolean execute_list(vtw_node *cur, vtw_def *def, const char *outbuf,boolean format); extern void touch_dir(const char *dp); extern void touch_file(const char *name); diff --git a/src/cli_val_engine.c b/src/cli_val_engine.c index b481b0d..4f0690f 100644 --- a/src/cli_val_engine.c +++ b/src/cli_val_engine.c @@ -391,6 +391,7 @@ static clind_path_ref* clind_config_engine_apply_command(clind_path_ref cfg_path clind_path_ref tmpl_path, clind_cmd *cmd, int *result_len) { + clind_path_ref* ret=NULL; DPRINT("eng_apply_cmd cfg=[%s] tmpl=[%s] type=%d\n", diff --git a/src/commit2.c b/src/commit2.c index 0a91a9f..4401d8d 100644 --- a/src/commit2.c +++ b/src/commit2.c @@ -15,6 +15,7 @@ boolean g_coverage = FALSE; boolean g_dump_trans = FALSE; boolean g_dump_actions = FALSE; boolean g_print_error_location_all = FALSE; +boolean g_old_print_output = FALSE; #define g_num_actions 5 @@ -129,7 +130,7 @@ usage(void) printf("\t-f\t\tfull iteration over configuration on commit check\n"); printf("\t-b\t\tbreak on each priority node (debug mode)\n"); printf("\t-r\t\tdisable run hook script on finishing commit\n"); - printf("\t-x\t\texperimental expand print error location feature\n"); + printf("\t-x\t\t\disable new print feature\n"); printf("\t-h\t\thelp\n"); } @@ -156,7 +157,7 @@ main(int argc, char** argv) while ((ch = getopt(argc, argv, "xdpthsecoafbrC:")) != -1) { switch (ch) { case 'x': - g_print_error_location_all = TRUE; + g_old_print_output = TRUE; break; case 'd': g_debug = TRUE; @@ -659,11 +660,18 @@ process_func(GNode *node, gpointer data) setenv(ENV_ACTION_NAME,ENV_ACTION_ACTIVE,1); } - char *outbuf = NULL; if (g_dump_actions == FALSE) { - outbuf = malloc(8192); - outbuf[0] = '\0'; - status = execute_list(c->_def.actions[result->_action].vtw_list_head,&c->_def,(const char**)&outbuf); + //need to add g_print_error_location_all, and processed location + if (g_old_print_output == TRUE) { + status = execute_list(c->_def.actions[result->_action].vtw_list_head,&c->_def,NULL,g_print_error_location_all); + } + else { + char *p = process_script_path(d->_path); + status = execute_list(c->_def.actions[result->_action].vtw_list_head,&c->_def,p,g_print_error_location_all); + } + // status = execute_list(c->_def.actions[result->_action].vtw_list_head,&c->_def,NULL,g_print_error_location_all); + + /* if (strlen(outbuf) > 0) { if (strstr(outbuf,"_errloc_:[") != NULL) { if (g_print_error_location_all == FALSE) { //EXECUTE_LIST RETURNS FALSE ON FAILURE.... @@ -675,7 +683,6 @@ process_func(GNode *node, gpointer data) } else { //currently set to format option for GUI client. - char *p = process_script_path(d->_path); if (p != NULL) { if (g_print_error_location_all == FALSE) { //EXECUTE_LIST RETURNS FALSE ON FAILURE.... fprintf(out_stream,"[%s]\n%s\n",p,outbuf); @@ -686,6 +693,8 @@ process_func(GNode *node, gpointer data) } } } + */ + } else { char buf[MAX_LENGTH_DIR_PATH*sizeof(char)]; @@ -759,13 +768,17 @@ process_script_path(char* in) ptr = slash; } while ((slash = strchr(slash,'/')) != NULL); } + char *p = clind_unescape(path_buf); + char *ret = malloc(4096); + strcpy(ret,p); //copy back + free(p); if (strncmp(ptr,"value:",6) == 0) { if (strlen(ptr)-6 > 0) { - strncat(path_buf,ptr+6,strlen(ptr)-6); - strcat(path_buf," "); + strncat(ret,ptr+6,strlen(ptr)-6); + strcat(ret," "); } } - return clind_unescape(path_buf); + return ret; } @@ -1540,13 +1553,17 @@ validate_func(GNode *node, gpointer data) fprintf(out_stream,"[START] %lu:%lu, %s@%s",(unsigned long)t.tv_sec,(unsigned long)t.tv_usec,ActionNames[result->_action],d->_path); } - char *outbuf = malloc(8192); - outbuf[0] = '\0'; boolean status = 1; if (g_dump_actions == FALSE) { //set location env setenv(ENV_DATA_PATH,d->_path,1); - status = execute_list(c->_def.actions[result->_action].vtw_list_head,&c->_def,(const char**)&outbuf); + if (g_old_print_output == TRUE) { + status = execute_list(c->_def.actions[result->_action].vtw_list_head,&c->_def,NULL,g_print_error_location_all); + } + else { + char *p = process_script_path(d->_path); + status = execute_list(c->_def.actions[result->_action].vtw_list_head,&c->_def,p,g_print_error_location_all); + } unsetenv(ENV_DATA_PATH); } else { @@ -1574,6 +1591,7 @@ validate_func(GNode *node, gpointer data) } if (!status) { //EXECUTE_LIST RETURNS FALSE ON FAILURE.... + /* char *p = process_script_path(d->_path); if (p != NULL) { if (strlen(outbuf) > 0) { @@ -1585,6 +1603,7 @@ validate_func(GNode *node, gpointer data) } } } + */ syslog(LOG_ERR,"commit error for %s:[%s]\n",ActionNames[result->_action],d->_path); if (g_display_error_node) { fprintf(out_stream,"%s@_errloc_:[%s]\n",ActionNames[result->_action],d->_path); @@ -1594,10 +1613,8 @@ validate_func(GNode *node, gpointer data) printf("commit2::validate_func(): FAILURE: status: %d\n",status); syslog(LOG_DEBUG,"commit2::validate_func(): FAILURE: status: %d",status); } - free(outbuf); return result->_mode ? FALSE: TRUE; //WILL STOP AT THIS POINT if mode is not set for full syntax check } - free(outbuf); return FALSE; } diff --git a/src/exe_action.c b/src/exe_action.c index b650a4f..04ec835 100644 --- a/src/exe_action.c +++ b/src/exe_action.c @@ -100,7 +100,7 @@ main(int argc, char** argv) //BROKEN--NEEDS TO BE FIX BELOW FOR DPATH AND CPATH common_set_context(path,path); - status = execute_list(def.actions[act].vtw_list_head,&def,NULL); + status = execute_list(def.actions[act].vtw_list_head,&def,NULL,FALSE); if (status == FALSE) { printf("command failed! status: %d\n", status); } |