X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=actions.c;h=113024edd078c8cb2481caddd53949de17122c95;hp=3b01d36e9e28ac4e1f9cadb63098f647d74ab7f3;hb=f40a0abd128bc78c9ac705a8384e71818ae306b5;hpb=dbb3859ed876dbcd022021bfa05de7a6d0b586ed diff --git a/actions.c b/actions.c index 3b01d36e..113024ed 100644 --- a/actions.c +++ b/actions.c @@ -1,4 +1,4 @@ -const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster Exp $"; +const char actions_rcs[] = "$Id: actions.c,v 1.21 2002/01/17 20:54:44 jongfoster Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/actions.c,v $ @@ -10,10 +10,10 @@ const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster * IJBSWA team. http://ijbswa.sourceforge.net * * Based on the Internet Junkbuster originally written - * by and Copyright (C) 1997 Anonymous Coders and + * by and Copyright (C) 1997 Anonymous Coders and * Junkbusters Corporation. http://www.junkbusters.com * - * This program is free software; you can redistribute it + * 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 2 of the License, or (at @@ -33,6 +33,31 @@ const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster * * Revisions : * $Log: actions.c,v $ + * Revision 1.21 2002/01/17 20:54:44 jongfoster + * Renaming free_url to free_url_spec, since it frees a struct url_spec. + * + * Revision 1.20 2001/11/22 21:56:49 jongfoster + * Making action_spec->flags into an unsigned long rather than just an + * unsigned int. + * Fixing a bug in the display of -add-header and -wafer + * + * Revision 1.19 2001/11/13 00:14:07 jongfoster + * Fixing stupid bug now I've figured out what || means. + * (It always returns 0 or 1, not one of it's paramaters.) + * + * Revision 1.18 2001/11/07 00:06:06 steudten + * Add line number in error output for lineparsing for + * actionsfile. + * + * Revision 1.17 2001/10/25 03:40:47 david__schmidt + * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple + * threads to call select() simultaneously. So, it's time to do a real, live, + * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__ + * (native). Both versions will work, but using __OS2__ offers multi-threading. + * + * Revision 1.16 2001/10/23 21:30:30 jongfoster + * Adding error-checking to selected functions. + * * Revision 1.15 2001/10/14 21:58:22 jongfoster * Adding support for the CGI-based editor: * - Exported get_actions() @@ -103,6 +128,7 @@ const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster #include #include #include +#include #include "project.h" #include "jcc.h" @@ -114,6 +140,7 @@ const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 jongfoster #ifdef FEATURE_CGI_EDIT_ACTIONS #include "encode.h" #endif /* def FEATURE_CGI_EDIT_ACTIONS */ +#include "urlmatch.h" const char actions_h_rcs[] = ACTIONS_H_VERSION; @@ -132,10 +159,10 @@ const char actions_h_rcs[] = ACTIONS_H_VERSION; #define AV_ADD_STRING 1 /* +stropt{string} */ #define AV_REM_STRING 2 /* -stropt */ #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */ -#define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */ +#define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */ /* - * We need a structure to hold the name, flag changes, + * We need a structure to hold the name, flag changes, * type, and string index. */ struct action_name @@ -194,7 +221,7 @@ static const struct action_name action_names[] = * Returns : JB_ERR_OK or JB_ERR_MEMORY * *********************************************************************/ -jb_err merge_actions (struct action_spec *dest, +jb_err merge_actions (struct action_spec *dest, const struct action_spec *src) { int i; @@ -242,7 +269,7 @@ jb_err merge_actions (struct action_spec *dest, /* No "remove all"s to worry about. */ list_remove_list(dest->multi_add[i], src->multi_remove[i]); err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]); - err = err || list_append_list_unique(dest->multi_add[i], src->multi_add[i]); + if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]); } if (err) @@ -271,11 +298,11 @@ jb_err merge_actions (struct action_spec *dest, * Returns : N/A * *********************************************************************/ -jb_err copy_action (struct action_spec *dest, +jb_err copy_action (struct action_spec *dest, const struct action_spec *src) { int i; - jb_err err; + jb_err err = JB_ERR_OK; memset(dest, '\0', sizeof(*dest)); @@ -299,13 +326,18 @@ jb_err copy_action (struct action_spec *dest, for (i = 0; i < ACTION_MULTI_COUNT; i++) { dest->multi_remove_all[i] = src->multi_remove_all[i]; - err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]); - err = err || list_duplicate(dest->multi_add[i], src->multi_add[i]); + err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]); + if (err) + { + return err; + } + err = list_duplicate(dest->multi_add[i], src->multi_add[i]); if (err) { return err; } } + return err; } @@ -327,6 +359,11 @@ void free_action (struct action_spec *src) { int i; + if (src == NULL) + { + return; + } + for (i = 0; i < ACTION_STRING_COUNT; i++) { freez(src->string[i]); @@ -362,7 +399,7 @@ void free_action (struct action_spec *src) * we found an action. * 2 : name = [out] Start of action name, null * terminated. NULL on EOL - * 3 : value = [out] Start of action value, null + * 3 : value = [out] Start of action value, null * terminated. NULL if none or EOL. * * Returns : JB_ERR_OK => Ok @@ -399,7 +436,7 @@ jb_err get_action_token(char **line, char **name, char **value) *name = str; /* parse option */ - while (((ch = *str) != '\0') && + while (((ch = *str) != '\0') && (ch != ' ') && (ch != '\t') && (ch != '{')) { if (ch == '}') @@ -488,7 +525,7 @@ jb_err get_actions(char *line, if (option) { /* handle option in 'option' */ - + /* Check for standard action name */ const struct action_name * action = action_names; @@ -598,7 +635,7 @@ jb_err get_actions(char *line, { /* try user aliases. */ const struct action_alias * alias = alias_list; - + while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) ) { alias = alias->next; @@ -623,482 +660,197 @@ jb_err get_actions(char *line, /********************************************************************* * - * Function : actions_to_text + * Function : init_current_action * - * Description : Converts a actionsfile entry from numeric form - * ("mask" and "add") to text. + * Description : Zero out an action. * * Parameters : - * 1 : mask = As from struct url_actions - * 2 : add = As from struct url_actions + * 1 : dest = An uninitialized current_action_spec. * - * Returns : A string. Caller must free it. - * NULL on out-of-memory error. + * Returns : N/A * *********************************************************************/ -char * actions_to_text(struct action_spec *action) +void init_current_action (struct current_action_spec *dest) { - unsigned mask = action->mask; - unsigned add = action->add; - char * result = strdup(""); - struct list_entry * lst; + memset(dest, '\0', sizeof(*dest)); - /* sanity - prevents "-feature +feature" */ - mask |= add; + dest->flags = ACTION_MOST_COMPATIBLE; +} -#define DEFINE_ACTION_BOOL(__name, __bit) \ - if (!(mask & __bit)) \ - { \ - string_append(&result, " -" __name); \ - } \ - else if (add & __bit) \ - { \ - string_append(&result, " +" __name); \ - } +/********************************************************************* + * + * Function : init_action + * + * Description : Zero out an action. + * + * Parameters : + * 1 : dest = An uninitialized action_spec. + * + * Returns : N/A + * + *********************************************************************/ +void init_action (struct action_spec *dest) +{ + memset(dest, '\0', sizeof(*dest)); +} -#define DEFINE_ACTION_STRING(__name, __bit, __index) \ - if (!(mask & __bit)) \ - { \ - string_append(&result, " -" __name); \ - } \ - else if (add & __bit) \ - { \ - string_append(&result, " +" __name "{"); \ - string_append(&result, action->string[__index]); \ - string_append(&result, "}"); \ + +/********************************************************************* + * + * Function : merge_current_action + * + * Description : Merge two actions together. + * Similar to "dest += src". + * Differences between this and merge_actions() + * is that this one doesn't allocate memory for + * strings (so "src" better be in memory for at least + * as long as "dest" is, and you'd better free + * "dest" using "free_current_action"). + * Also, there is no mask or remove lists in dest. + * (If we're applying it to a URL, we don't need them) + * + * Parameters : + * 1 : dest = Current actions, to modify. + * 2 : src = Action to add. + * + * Returns 0 : no error + * !=0 : error, probably JB_ERR_MEMORY. + * + *********************************************************************/ +jb_err merge_current_action (struct current_action_spec *dest, + const struct action_spec *src) +{ + int i; + jb_err err = JB_ERR_OK; + + dest->flags &= src->mask; + dest->flags |= src->add; + + for (i = 0; i < ACTION_STRING_COUNT; i++) + { + char * str = src->string[i]; + if (str) + { + str = strdup(str); + if (!str) + { + return JB_ERR_MEMORY; + } + freez(dest->string[i]); + dest->string[i] = str; + } } -#define DEFINE_ACTION_MULTI(__name, __index) \ - if (action->multi_remove_all[__index]) \ - { \ - string_append(&result, " -" __name "{*}"); \ - } \ - else \ - { \ - lst = action->multi_remove[__index]->first; \ - while (lst) \ - { \ - string_append(&result, " -" __name "{"); \ - string_append(&result, lst->str); \ - string_append(&result, "}"); \ - lst = lst->next; \ - } \ - } \ - lst = action->multi_add[__index]->first; \ - while (lst) \ - { \ - string_append(&result, " +" __name "{"); \ - string_append(&result, lst->str); \ - string_append(&result, "}"); \ - lst = lst->next; \ + for (i = 0; i < ACTION_MULTI_COUNT; i++) + { + if (src->multi_remove_all[i]) + { + /* Remove everything from dest, then add src->multi_add */ + err = list_duplicate(dest->multi[i], src->multi_add[i]); + if (err) + { + return err; + } + } + else + { + list_remove_list(dest->multi[i], src->multi_remove[i]); + err = list_append_list_unique(dest->multi[i], src->multi_add[i]); + if (err) + { + return err; + } + } } + return err; +} -#define DEFINE_ACTION_ALIAS 0 /* No aliases for output */ -#include "actionlist.h" +/********************************************************************* + * + * Function : free_current_action + * + * Description : Free memory used by a current_action_spec. + * Does not free the current_action_spec itself. + * + * Parameters : + * 1 : src = Source to free. + * + * Returns : N/A + * + *********************************************************************/ +void free_current_action (struct current_action_spec *src) +{ + int i; -#undef DEFINE_ACTION_MULTI -#undef DEFINE_ACTION_STRING -#undef DEFINE_ACTION_BOOL -#undef DEFINE_ACTION_ALIAS + for (i = 0; i < ACTION_STRING_COUNT; i++) + { + freez(src->string[i]); + } - return result; + for (i = 0; i < ACTION_MULTI_COUNT; i++) + { + destroy_list(src->multi[i]); + } + + memset(src, '\0', sizeof(*src)); } -#ifdef FEATURE_CGI_EDIT_ACTIONS /********************************************************************* * - * Function : actions_to_html + * Function : unload_actions_file * - * Description : Converts a actionsfile entry from numeric form - * ("mask" and "add") to a
-seperated HTML string. + * Description : Unloads an actions module. * * Parameters : - * 1 : mask = As from struct url_actions - * 2 : add = As from struct url_actions + * 1 : file_data = the data structure associated with the + * actions file. * - * Returns : A string. Caller must free it. - * NULL on out-of-memory error. + * Returns : N/A * *********************************************************************/ -char * actions_to_html(struct action_spec *action) +void unload_actions_file(void *file_data) { - unsigned mask = action->mask; - unsigned add = action->add; - char * result = strdup(""); - char * enc_str; - struct list_entry * lst; + struct url_actions * next; + struct url_actions * cur = (struct url_actions *)file_data; + while (cur != NULL) + { + next = cur->next; + free_url_spec(cur->url); + free_action(cur->action); + freez(cur); + cur = next; + } - /* sanity - prevents "-feature +feature" */ - mask |= add; +} -#define DEFINE_ACTION_BOOL(__name, __bit) \ - if (!(mask & __bit)) \ - { \ - string_append(&result, "\n
-" __name); \ - } \ - else if (add & __bit) \ - { \ - string_append(&result, "\n
+" __name); \ +/********************************************************************* + * + * Function : free_alias_list + * + * Description : Free memory used by a list of aliases. + * + * Parameters : + * 1 : alias_list = Linked list to free. + * + * Returns : N/A + * + *********************************************************************/ +void free_alias_list(struct action_alias *alias_list) +{ + while (alias_list != NULL) + { + struct action_alias * next = alias_list->next; + alias_list->next = NULL; + freez(alias_list->name); + free_action(alias_list->action); + free(alias_list); + alias_list = next; } - -#define DEFINE_ACTION_STRING(__name, __bit, __index) \ - if (!(mask & __bit)) \ - { \ - string_append(&result, "\n
-" __name); \ - } \ - else if (add & __bit) \ - { \ - string_append(&result, "\n
+" __name "{"); \ - if (NULL == result) \ - { \ - return NULL; \ - } \ - enc_str = html_encode(action->string[__index]);\ - if (NULL == enc_str) \ - { \ - free(result); \ - return NULL; \ - } \ - string_append(&result, enc_str); \ - free(enc_str); \ - string_append(&result, "}"); \ - } - -#define DEFINE_ACTION_MULTI(__name, __index) \ - if (action->multi_remove_all[__index]) \ - { \ - string_append(&result, "\n
-" __name "{*}"); \ - } \ - else \ - { \ - lst = action->multi_remove[__index]->first; \ - while (lst) \ - { \ - string_append(&result, "\n
-" __name "{");\ - if (NULL == result) \ - { \ - return NULL; \ - } \ - enc_str = html_encode(lst->str); \ - if (NULL == enc_str) \ - { \ - free(result); \ - return NULL; \ - } \ - string_append(&result, enc_str); \ - free(enc_str); \ - string_append(&result, "}"); \ - lst = lst->next; \ - } \ - } \ - lst = action->multi_add[__index]->first; \ - while (lst) \ - { \ - string_append(&result, "\n
+" __name "{"); \ - if (NULL == result) \ - { \ - return NULL; \ - } \ - enc_str = html_encode(lst->str); \ - if (NULL == enc_str) \ - { \ - free(result); \ - return NULL; \ - } \ - string_append(&result, enc_str); \ - free(enc_str); \ - string_append(&result, "}"); \ - lst = lst->next; \ - } - -#define DEFINE_ACTION_ALIAS 0 /* No aliases for output */ - -#include "actionlist.h" - -#undef DEFINE_ACTION_MULTI -#undef DEFINE_ACTION_STRING -#undef DEFINE_ACTION_BOOL -#undef DEFINE_ACTION_ALIAS - - /* trim leading
*/ - if (result && *result) - { - char * s = result; - result = strdup(result + 5); - free(s); - } - - return result; -} -#endif /* def FEATURE_CGI_EDIT_ACTIONS */ - - -/********************************************************************* - * - * Function : current_actions_to_text - * - * Description : Converts a actionsfile entry to text. - * - * Parameters : - * 1 : action = Action - * - * Returns : A string. Caller must free it. - * NULL on out-of-memory error. - * - *********************************************************************/ -char * current_action_to_text(struct current_action_spec *action) -{ - unsigned flags = action->flags; - char * result = strdup(""); - struct list_entry * lst; - -#define DEFINE_ACTION_BOOL(__name, __bit) \ - if (flags & __bit) \ - { \ - string_append(&result, " +" __name); \ - } \ - else \ - { \ - string_append(&result, " -" __name); \ - } - -#define DEFINE_ACTION_STRING(__name, __bit, __index) \ - if (flags & __bit) \ - { \ - string_append(&result, " +" __name "{"); \ - string_append(&result, action->string[__index]); \ - string_append(&result, "}"); \ - } \ - else \ - { \ - string_append(&result, " -" __name); \ - } - -#define DEFINE_ACTION_MULTI(__name, __index) \ - lst = action->multi[__index]->first; \ - if (lst == NULL) \ - { \ - string_append(&result, " -" __name); \ - } \ - else \ - { \ - while (lst) \ - { \ - string_append(&result, " +" __name "{"); \ - string_append(&result, lst->str); \ - string_append(&result, "}"); \ - lst = lst->next; \ - } \ - } - -#define DEFINE_ACTION_ALIAS 0 /* No aliases for output */ - -#include "actionlist.h" - -#undef DEFINE_ACTION_MULTI -#undef DEFINE_ACTION_STRING -#undef DEFINE_ACTION_BOOL -#undef DEFINE_ACTION_ALIAS - - return result; -} - - -/********************************************************************* - * - * Function : init_current_action - * - * Description : Zero out an action. - * - * Parameters : - * 1 : dest = An uninitialized current_action_spec. - * - * Returns : N/A - * - *********************************************************************/ -void init_current_action (struct current_action_spec *dest) -{ - memset(dest, '\0', sizeof(*dest)); - - dest->flags = ACTION_MOST_COMPATIBLE; -} - - -/********************************************************************* - * - * Function : init_action - * - * Description : Zero out an action. - * - * Parameters : - * 1 : dest = An uninitialized action_spec. - * - * Returns : N/A - * - *********************************************************************/ -void init_action (struct action_spec *dest) -{ - memset(dest, '\0', sizeof(*dest)); -} - - -/********************************************************************* - * - * Function : merge_current_action - * - * Description : Merge two actions together. - * Similar to "dest += src". - * Differences between this and merge_actions() - * is that this one doesn't allocate memory for - * strings (so "src" better be in memory for at least - * as long as "dest" is, and you'd better free - * "dest" using "free_current_action"). - * Also, there is no mask or remove lists in dest. - * (If we're applying it to a URL, we don't need them) - * - * Parameters : - * 1 : dest = Current actions, to modify. - * 2 : src = Action to add. - * - * Returns : N/A - * - *********************************************************************/ -jb_err merge_current_action (struct current_action_spec *dest, - const struct action_spec *src) -{ - int i; - jb_err err; - - dest->flags &= src->mask; - dest->flags |= src->add; - - for (i = 0; i < ACTION_STRING_COUNT; i++) - { - char * str = src->string[i]; - if (str) - { - str = strdup(str); - if (!str) - { - return JB_ERR_MEMORY; - } - freez(dest->string[i]); - dest->string[i] = str; - } - } - - for (i = 0; i < ACTION_MULTI_COUNT; i++) - { - if (src->multi_remove_all[i]) - { - /* Remove everything from dest, then add src->multi_add */ - err = list_duplicate(dest->multi[i], src->multi_add[i]); - if (err) - { - return err; - } - } - else - { - list_remove_list(dest->multi[i], src->multi_remove[i]); - err = list_append_list_unique(dest->multi[i], src->multi_add[i]); - if (err) - { - return err; - } - } - } -} - - -/********************************************************************* - * - * Function : free_current_action - * - * Description : Free memory used by a current_action_spec. - * Does not free the current_action_spec itself. - * - * Parameters : - * 1 : src = Source to free. - * - * Returns : N/A - * - *********************************************************************/ -void free_current_action (struct current_action_spec *src) -{ - int i; - - for (i = 0; i < ACTION_STRING_COUNT; i++) - { - freez(src->string[i]); - } - - for (i = 0; i < ACTION_MULTI_COUNT; i++) - { - destroy_list(src->multi[i]); - } - - memset(src, '\0', sizeof(*src)); -} - - -/********************************************************************* - * - * Function : unload_actions_file - * - * Description : Unloads an actions module. - * - * Parameters : - * 1 : file_data = the data structure associated with the - * actions file. - * - * Returns : N/A - * - *********************************************************************/ -void unload_actions_file(void *file_data) -{ - struct url_actions * next; - struct url_actions * cur = (struct url_actions *)file_data; - while (cur != NULL) - { - next = cur->next; - free_url(cur->url); - free_action(cur->action); - freez(cur); - cur = next; - } - -} - - -/********************************************************************* - * - * Function : free_alias_list - * - * Description : Free memory used by a list of aliases. - * - * Parameters : - * 1 : alias_list = Linked list to free. - * - * Returns : N/A - * - *********************************************************************/ -void free_alias_list(struct action_alias *alias_list) -{ - while (alias_list != NULL) - { - struct action_alias * next = alias_list->next; - alias_list->next = NULL; - freez((char *)alias_list->name); - free_action(alias_list->action); - free(alias_list); - alias_list = next; - } -} +} /********************************************************************* @@ -1139,6 +891,7 @@ int load_actions_file(struct client_state *csp) struct action_spec * cur_action = NULL; int cur_action_used = 0; struct action_alias * alias_list = NULL; + unsigned long linenum = 0; if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs)) { @@ -1171,7 +924,7 @@ int load_actions_file(struct client_state *csp) return 1; /* never get here */ } - while (read_config_line(buf, sizeof(buf), fp) != NULL) + while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL) { if (*buf == '{') { @@ -1186,9 +939,9 @@ int load_actions_file(struct client_state *csp) { /* too short */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid line: %s", - csp->config->actions_file, buf); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid line (%lu): %s", + csp->config->actions_file, linenum, buf); return 1; /* never get here */ } @@ -1200,9 +953,9 @@ int load_actions_file(struct client_state *csp) { /* too short */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid line: {{ }}", - csp->config->actions_file); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid line (%lu): {{ }}", + csp->config->actions_file, linenum); return 1; /* never get here */ } @@ -1232,9 +985,9 @@ int load_actions_file(struct client_state *csp) * appear once. */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': {{settings}} must only appear once, and it must be before anything else.", - csp->config->actions_file); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.", + csp->config->actions_file, linenum); } mode = MODE_SETTINGS; } @@ -1246,9 +999,9 @@ int load_actions_file(struct client_state *csp) /* {{description}} is a singleton and only {{settings}} may proceed it */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': {{description}} must only appear once, and only a {{settings}} block may be above it.", - csp->config->actions_file); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.", + csp->config->actions_file, linenum); } mode = MODE_DESCRIPTION; } @@ -1268,9 +1021,9 @@ int load_actions_file(struct client_state *csp) * completely rewriting the file becomes non-trivial) */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': {{alias}} must only appear once, and it must be before all actions.", - csp->config->actions_file, start); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.", + csp->config->actions_file, linenum); } mode = MODE_ALIAS; } @@ -1278,9 +1031,9 @@ int load_actions_file(struct client_state *csp) { /* invalid {{something}} block */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid line: {{%s}}", - csp->config->actions_file, start); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid line (%lu): {{%s}}", + csp->config->actions_file, linenum, start); return 1; /* never get here */ } } @@ -1309,7 +1062,7 @@ int load_actions_file(struct client_state *csp) if (cur_action == NULL) { fclose(fp); - log_error(LOG_LEVEL_FATAL, + log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory", csp->config->actions_file); return 1; /* never get here */ @@ -1325,9 +1078,9 @@ int load_actions_file(struct client_state *csp) { /* No closing } */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid line: %s", - csp->config->actions_file, buf); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid line (%lu): %s", + csp->config->actions_file, linenum, buf); return 1; /* never get here */ } *end = '\0'; @@ -1339,9 +1092,9 @@ int load_actions_file(struct client_state *csp) { /* error */ fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid line: %s", - csp->config->actions_file, buf); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid line (%lu): %s", + csp->config->actions_file, linenum, buf); return 1; /* never get here */ } } @@ -1369,142 +1122,429 @@ int load_actions_file(struct client_state *csp) char actions_buf[BUFFER_SIZE]; struct action_alias * new_alias; - char * start = strchr(buf, '='); - char * end = start; + char * start = strchr(buf, '='); + char * end = start; + + if ((start == NULL) || (start == buf)) + { + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid alias line (%lu): %s", + csp->config->actions_file, linenum, buf); + return 1; /* never get here */ + } + + if ((new_alias = zalloc(sizeof(*new_alias))) == NULL) + { + fclose(fp); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': out of memory!", + csp->config->actions_file); + return 1; /* never get here */ + } + + /* Eat any the whitespace before the '=' */ + end--; + while ((*end == ' ') || (*end == '\t')) + { + /* + * we already know we must have at least 1 non-ws char + * at start of buf - no need to check + */ + end--; + } + end[1] = '\0'; + + /* Eat any the whitespace after the '=' */ + start++; + while ((*start == ' ') || (*start == '\t')) + { + start++; + } + if (*start == '\0') + { + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid alias line (%lu): %s", + csp->config->actions_file, linenum, buf); + return 1; /* never get here */ + } + + new_alias->name = strdup(buf); + + strcpy(actions_buf, start); + + if (get_actions(actions_buf, alias_list, new_alias->action)) + { + /* error */ + fclose(fp); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': invalid alias line (%lu): %s = %s", + csp->config->actions_file, linenum, buf, start); + return 1; /* never get here */ + } + + /* add to list */ + new_alias->next = alias_list; + alias_list = new_alias; + } + else if (mode == MODE_ACTIONS) + { + /* it's a URL pattern */ + + /* allocate a new node */ + if ((perm = zalloc(sizeof(*perm))) == NULL) + { + fclose(fp); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': out of memory!", + csp->config->actions_file); + return 1; /* never get here */ + } + + /* Save flags */ + copy_action (perm->action, cur_action); + + /* Save the URL pattern */ + if (create_url_spec(perm->url, buf)) + { + fclose(fp); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': line %lu: cannot create URL pattern from: %s", + csp->config->actions_file, linenum, buf); + return 1; /* never get here */ + } + + /* add it to the list */ + last_perm->next = perm; + last_perm = perm; + } + else if (mode == MODE_START_OF_FILE) + { + /* oops - please have a {} line as 1st line in file. */ + fclose(fp); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': first needed line (%lu) is invalid: %s", + csp->config->actions_file, linenum, buf); + return 1; /* never get here */ + } + else + { + /* How did we get here? This is impossible! */ + fclose(fp); + log_error(LOG_LEVEL_FATAL, + "can't load actions file '%s': INTERNAL ERROR - mode = %d", + csp->config->actions_file, mode); + return 1; /* never get here */ + } + } + + fclose(fp); + + free_action(cur_action); + + free_alias_list(alias_list); + + /* the old one is now obsolete */ + if (current_actions_file) + { + current_actions_file->unloader = unload_actions_file; + } + + fs->next = files->next; + files->next = fs; + current_actions_file = fs; + + if (csp) + { + csp->actions_list = fs; + } + + return(0); + +} + + +/********************************************************************* + * + * Function : actions_to_text + * + * Description : Converts a actionsfile entry from numeric form + * ("mask" and "add") to text. + * + * Parameters : + * 1 : mask = As from struct url_actions + * 2 : add = As from struct url_actions + * + * Returns : A string. Caller must free it. + * NULL on out-of-memory error. + * + *********************************************************************/ +char * actions_to_text(struct action_spec *action) +{ + unsigned mask = action->mask; + unsigned add = action->add; + char * result = strdup(""); + struct list_entry * lst; + + /* sanity - prevents "-feature +feature" */ + mask |= add; + + +#define DEFINE_ACTION_BOOL(__name, __bit) \ + if (!(mask & __bit)) \ + { \ + string_append(&result, " -" __name); \ + } \ + else if (add & __bit) \ + { \ + string_append(&result, " +" __name); \ + } + +#define DEFINE_ACTION_STRING(__name, __bit, __index) \ + if (!(mask & __bit)) \ + { \ + string_append(&result, " -" __name); \ + } \ + else if (add & __bit) \ + { \ + string_append(&result, " +" __name "{"); \ + string_append(&result, action->string[__index]); \ + string_append(&result, "}"); \ + } + +#define DEFINE_ACTION_MULTI(__name, __index) \ + if (action->multi_remove_all[__index]) \ + { \ + string_append(&result, " -" __name); \ + } \ + else \ + { \ + lst = action->multi_remove[__index]->first; \ + while (lst) \ + { \ + string_append(&result, " -" __name "{"); \ + string_append(&result, lst->str); \ + string_append(&result, "}"); \ + lst = lst->next; \ + } \ + } \ + lst = action->multi_add[__index]->first; \ + while (lst) \ + { \ + string_append(&result, " +" __name "{"); \ + string_append(&result, lst->str); \ + string_append(&result, "}"); \ + lst = lst->next; \ + } + +#define DEFINE_ACTION_ALIAS 0 /* No aliases for output */ + +#include "actionlist.h" + +#undef DEFINE_ACTION_MULTI +#undef DEFINE_ACTION_STRING +#undef DEFINE_ACTION_BOOL +#undef DEFINE_ACTION_ALIAS + + return result; +} + + +#ifdef FEATURE_CGI_EDIT_ACTIONS +/********************************************************************* + * + * Function : actions_to_html + * + * Description : Converts a actionsfile entry from numeric form + * ("mask" and "add") to a
-seperated HTML string. + * + * Parameters : + * 1 : mask = As from struct url_actions + * 2 : add = As from struct url_actions + * + * Returns : A string. Caller must free it. + * NULL on out-of-memory error. + * + *********************************************************************/ +char * actions_to_html(struct action_spec *action) +{ + unsigned mask = action->mask; + unsigned add = action->add; + char * result = strdup(""); + char * enc_str; + struct list_entry * lst; + + /* sanity - prevents "-feature +feature" */ + mask |= add; - if ((start == NULL) || (start == buf)) - { - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid alias line: %s", - csp->config->actions_file, buf); - return 1; /* never get here */ - } - if ((new_alias = zalloc(sizeof(*new_alias))) == NULL) - { - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': out of memory!", - csp->config->actions_file); - return 1; /* never get here */ - } +#define DEFINE_ACTION_BOOL(__name, __bit) \ + if (!(mask & __bit)) \ + { \ + string_append(&result, "\n
-" __name); \ + } \ + else if (add & __bit) \ + { \ + string_append(&result, "\n
+" __name); \ + } - /* Eat any the whitespace before the '=' */ - end--; - while ((*end == ' ') || (*end == '\t')) - { - /* - * we already know we must have at least 1 non-ws char - * at start of buf - no need to check - */ - end--; - } - end[1] = '\0'; +#define DEFINE_ACTION_STRING(__name, __bit, __index) \ + if (!(mask & __bit)) \ + { \ + string_append(&result, "\n
-" __name); \ + } \ + else if (add & __bit) \ + { \ + string_append(&result, "\n
+" __name "{"); \ + if (NULL == result) \ + { \ + return NULL; \ + } \ + enc_str = html_encode(action->string[__index]);\ + if (NULL == enc_str) \ + { \ + free(result); \ + return NULL; \ + } \ + string_append(&result, enc_str); \ + free(enc_str); \ + string_append(&result, "}"); \ + } - /* Eat any the whitespace after the '=' */ - start++; - while ((*start == ' ') || (*start == '\t')) - { - start++; - } - if (*start == '\0') - { - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid alias line: %s", - csp->config->actions_file, buf); - return 1; /* never get here */ - } +#define DEFINE_ACTION_MULTI(__name, __index) \ + if (action->multi_remove_all[__index]) \ + { \ + string_append(&result, "\n
-" __name); \ + } \ + else \ + { \ + lst = action->multi_remove[__index]->first; \ + while (lst) \ + { \ + string_append(&result, "\n
-" __name "{");\ + if (NULL == result) \ + { \ + return NULL; \ + } \ + enc_str = html_encode(lst->str); \ + if (NULL == enc_str) \ + { \ + free(result); \ + return NULL; \ + } \ + string_append(&result, enc_str); \ + free(enc_str); \ + string_append(&result, "}"); \ + lst = lst->next; \ + } \ + } \ + lst = action->multi_add[__index]->first; \ + while (lst) \ + { \ + string_append(&result, "\n
+" __name "{"); \ + if (NULL == result) \ + { \ + return NULL; \ + } \ + enc_str = html_encode(lst->str); \ + if (NULL == enc_str) \ + { \ + free(result); \ + return NULL; \ + } \ + string_append(&result, enc_str); \ + free(enc_str); \ + string_append(&result, "}"); \ + lst = lst->next; \ + } - new_alias->name = strdup(buf); +#define DEFINE_ACTION_ALIAS 0 /* No aliases for output */ - strcpy(actions_buf, start); +#include "actionlist.h" - if (get_actions(actions_buf, alias_list, new_alias->action)) - { - /* error */ - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': invalid alias line: %s = %s", - csp->config->actions_file, buf, start); - return 1; /* never get here */ - } - - /* add to list */ - new_alias->next = alias_list; - alias_list = new_alias; - } - else if (mode == MODE_ACTIONS) - { - /* it's a URL pattern */ +#undef DEFINE_ACTION_MULTI +#undef DEFINE_ACTION_STRING +#undef DEFINE_ACTION_BOOL +#undef DEFINE_ACTION_ALIAS - /* allocate a new node */ - if ((perm = zalloc(sizeof(*perm))) == NULL) - { - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': out of memory!", - csp->config->actions_file); - return 1; /* never get here */ - } + /* trim leading
*/ + if (result && *result) + { + char * s = result; + result = strdup(result + 5); + free(s); + } - /* Save flags */ - copy_action (perm->action, cur_action); + return result; +} +#endif /* def FEATURE_CGI_EDIT_ACTIONS */ - /* Save the URL pattern */ - if (create_url_spec(perm->url, buf)) - { - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': cannot create URL pattern from: %s", - csp->config->actions_file, buf); - return 1; /* never get here */ - } - /* add it to the list */ - last_perm->next = perm; - last_perm = perm; - } - else if (mode == MODE_START_OF_FILE) - { - /* oops - please have a {} line as 1st line in file. */ - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': first line is invalid: %s", - csp->config->actions_file, buf); - return 1; /* never get here */ - } - else - { - /* How did we get here? This is impossible! */ - fclose(fp); - log_error(LOG_LEVEL_FATAL, - "can't load actions file '%s': INTERNAL ERROR - mode = %d", - csp->config->actions_file, mode); - return 1; /* never get here */ - } - } +/********************************************************************* + * + * Function : current_actions_to_text + * + * Description : Converts a actionsfile entry to text. + * + * Parameters : + * 1 : action = Action + * + * Returns : A string. Caller must free it. + * NULL on out-of-memory error. + * + *********************************************************************/ +char * current_action_to_text(struct current_action_spec *action) +{ + unsigned long flags = action->flags; + char * result = strdup(""); + struct list_entry * lst; - fclose(fp); - - free_action(cur_action); +#define DEFINE_ACTION_BOOL(__name, __bit) \ + if (flags & __bit) \ + { \ + string_append(&result, " +" __name); \ + } \ + else \ + { \ + string_append(&result, " -" __name); \ + } - free_alias_list(alias_list); +#define DEFINE_ACTION_STRING(__name, __bit, __index) \ + if (flags & __bit) \ + { \ + string_append(&result, " +" __name "{"); \ + string_append(&result, action->string[__index]); \ + string_append(&result, "}"); \ + } \ + else \ + { \ + string_append(&result, " -" __name); \ + } - /* the old one is now obsolete */ - if (current_actions_file) - { - current_actions_file->unloader = unload_actions_file; +#define DEFINE_ACTION_MULTI(__name, __index) \ + lst = action->multi[__index]->first; \ + if (lst == NULL) \ + { \ + string_append(&result, " -" __name); \ + } \ + else \ + { \ + while (lst) \ + { \ + string_append(&result, " +" __name "{"); \ + string_append(&result, lst->str); \ + string_append(&result, "}"); \ + lst = lst->next; \ + } \ } - fs->next = files->next; - files->next = fs; - current_actions_file = fs; +#define DEFINE_ACTION_ALIAS 0 /* No aliases for output */ - if (csp) - { - csp->actions_list = fs; - } +#include "actionlist.h" - return(0); +#undef DEFINE_ACTION_MULTI +#undef DEFINE_ACTION_STRING +#undef DEFINE_ACTION_BOOL +#undef DEFINE_ACTION_ALIAS + return result; }