1 const char actions_rcs[] = "$Id: actions.c,v 1.32.2.1 2002/05/26 12:13:16 roro Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/actions.c,v $
6 * Purpose : Declares functions to work with actions files
7 * Functions declared include: FIXME
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
37 * Change unsigned to unsigned long in actions_name struct. This closes
38 * SourceForge Bug #539284.
40 * Revision 1.32 2002/05/12 21:36:29 jongfoster
41 * Correcting function comments
43 * Revision 1.31 2002/05/06 07:56:50 oes
44 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
46 * Revision 1.30 2002/04/30 11:14:52 oes
47 * Made csp the first parameter in *action_to_html
49 * Revision 1.29 2002/04/26 19:30:54 jongfoster
50 * - current_action_to_html(): Adding help link for the "-" form of
52 * - Some actions had "<br>-", some "<br> -" (note the space).
53 * Standardizing on no space.
54 * - Greatly simplifying some of the code by using string_join()
57 * Revision 1.28 2002/04/26 12:53:15 oes
58 * - CGI AF editor now writes action lines split into
59 * single lines with line continuation
60 * - actions_to_html now embeds each action name in
62 * - current_action_to_text is now called current_action_to_html
63 * and acts like actions_to_html
65 * Revision 1.27 2002/04/24 02:10:31 oes
66 * - Jon's patch for multiple AFs:
67 * - split load_actions_file, add load_one_actions_file
68 * - make csp->actions_list files an array
69 * - remember file id with each action
70 * - Copy_action now frees dest action before copying
72 * Revision 1.26 2002/03/26 22:29:54 swa
73 * we have a new homepage!
75 * Revision 1.25 2002/03/24 13:25:43 swa
76 * name change related issues
78 * Revision 1.24 2002/03/16 23:54:06 jongfoster
79 * Adding graceful termination feature, to help look for memory leaks.
80 * If you enable this (which, by design, has to be done by hand
81 * editing config.h) and then go to http://i.j.b/die, then the program
82 * will exit cleanly after the *next* request. It should free all the
83 * memory that was used.
85 * Revision 1.23 2002/03/07 03:46:16 oes
86 * Fixed compiler warnings
88 * Revision 1.22 2002/01/21 00:27:02 jongfoster
89 * Allowing free_action(NULL).
90 * Moving the functions that #include actionlist.h to the end of the file,
91 * because the Visual C++ 97 debugger gets extremely confused if you try
92 * to debug any code that comes after them in the file.
94 * Revision 1.21 2002/01/17 20:54:44 jongfoster
95 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
97 * Revision 1.20 2001/11/22 21:56:49 jongfoster
98 * Making action_spec->flags into an unsigned long rather than just an
100 * Fixing a bug in the display of -add-header and -wafer
102 * Revision 1.19 2001/11/13 00:14:07 jongfoster
103 * Fixing stupid bug now I've figured out what || means.
104 * (It always returns 0 or 1, not one of it's paramaters.)
106 * Revision 1.18 2001/11/07 00:06:06 steudten
107 * Add line number in error output for lineparsing for
110 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
111 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
112 * threads to call select() simultaneously. So, it's time to do a real, live,
113 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
114 * (native). Both versions will work, but using __OS2__ offers multi-threading.
116 * Revision 1.16 2001/10/23 21:30:30 jongfoster
117 * Adding error-checking to selected functions.
119 * Revision 1.15 2001/10/14 21:58:22 jongfoster
120 * Adding support for the CGI-based editor:
121 * - Exported get_actions()
122 * - Added new function free_alias_list()
123 * - Added support for {{settings}} and {{description}} blocks
124 * in the actions file. They are currently ignored.
125 * - Added restriction to only one {{alias}} block which must appear
126 * first in the file, to simplify the editor's rewriting rules.
127 * - Note that load_actions_file() is no longer used by the CGI-based
128 * editor, but some of the other routines in this file are.
130 * Revision 1.14 2001/09/22 16:36:59 jongfoster
131 * Removing unused parameter fs from read_config_line()
133 * Revision 1.13 2001/09/16 15:47:37 jongfoster
134 * First version of CGI-based edit interface. This is very much a
135 * work-in-progress, and you can't actually use it to edit anything
136 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
137 * to have any effect.
139 * Revision 1.12 2001/09/16 13:21:27 jongfoster
140 * Changes to use new list functions.
142 * Revision 1.11 2001/09/14 00:17:32 jongfoster
143 * Tidying up memory allocation. New function init_action().
145 * Revision 1.10 2001/09/10 10:14:34 oes
146 * Removing unused variable
148 * Revision 1.9 2001/07/30 22:08:36 jongfoster
149 * Tidying up #defines:
150 * - All feature #defines are now of the form FEATURE_xxx
151 * - Permanently turned off WIN_GUI_EDIT
152 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
154 * Revision 1.8 2001/06/29 13:19:52 oes
155 * Removed logentry from cancelled commit
157 * Revision 1.7 2001/06/09 10:55:28 jongfoster
158 * Changing BUFSIZ ==> BUFFER_SIZE
160 * Revision 1.6 2001/06/07 23:04:34 jongfoster
161 * Made get_actions() static.
163 * Revision 1.5 2001/06/03 19:11:48 oes
164 * adapted to new enlist_unique arg format
166 * Revision 1.4 2001/06/01 20:03:42 jongfoster
167 * Better memory management - current_action->strings[] now
168 * contains copies of the strings, not the original.
170 * Revision 1.3 2001/06/01 18:49:17 jongfoster
171 * Replaced "list_share" with "list" - the tiny memory gain was not
172 * worth the extra complexity.
174 * Revision 1.2 2001/05/31 21:40:00 jongfoster
175 * Removing some commented out, obsolete blocks of code.
177 * Revision 1.1 2001/05/31 21:16:46 jongfoster
178 * Moved functions to process the action list into this new file.
181 *********************************************************************/
195 #include "miscutil.h"
199 #include "urlmatch.h"
202 const char actions_h_rcs[] = ACTIONS_H_VERSION;
206 * We need the main list of options.
208 * First, we need a way to tell between boolean, string, and multi-string
209 * options. For string and multistring options, we also need to be
210 * able to tell the difference between a "+" and a "-". (For bools,
211 * the "+"/"-" information is encoded in "add" and "mask"). So we use
212 * an enumerated type (well, the preprocessor equivalent). Here are
215 #define AV_NONE 0 /* +opt -opt */
216 #define AV_ADD_STRING 1 /* +stropt{string} */
217 #define AV_REM_STRING 2 /* -stropt */
218 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
219 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
222 * We need a structure to hold the name, flag changes,
223 * type, and string index.
228 unsigned long mask; /* a bit set to "0" = remove action */
229 unsigned long add; /* a bit set to "1" = add action */
230 int takes_value; /* an AV_... constant */
231 int index; /* index into strings[] or multi[] */
235 * And with those building blocks in place, here's the array.
237 static const struct action_name action_names[] =
240 * Well actually there's no data here - it's in actionlist.h
241 * This keeps it together to make it easy to change.
243 * Here's the macros used to format it:
245 #define DEFINE_ACTION_MULTI(name,index) \
246 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
247 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
248 #define DEFINE_ACTION_STRING(name,flag,index) \
249 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
250 { "-" name, ~flag, 0, AV_REM_STRING, index },
251 #define DEFINE_ACTION_BOOL(name,flag) \
252 { "+" name, ACTION_MASK_ALL, flag }, \
253 { "-" name, ~flag, 0 },
254 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
256 #include "actionlist.h"
258 #undef DEFINE_ACTION_MULTI
259 #undef DEFINE_ACTION_STRING
260 #undef DEFINE_ACTION_BOOL
261 #undef DEFINE_ACTION_ALIAS
263 { NULL, 0, 0 } /* End marker */
267 static int load_one_actions_file(struct client_state *csp, int fileid);
270 /*********************************************************************
272 * Function : merge_actions
274 * Description : Merge two actions together.
275 * Similar to "dest += src".
278 * 1 : dest = Actions to modify.
279 * 2 : src = Action to add.
281 * Returns : JB_ERR_OK or JB_ERR_MEMORY
283 *********************************************************************/
284 jb_err merge_actions (struct action_spec *dest,
285 const struct action_spec *src)
290 dest->mask &= src->mask;
291 dest->add &= src->mask;
292 dest->add |= src->add;
294 for (i = 0; i < ACTION_STRING_COUNT; i++)
296 char * str = src->string[i];
299 freez(dest->string[i]);
300 dest->string[i] = strdup(str);
301 if (NULL == dest->string[i])
303 return JB_ERR_MEMORY;
308 for (i = 0; i < ACTION_MULTI_COUNT; i++)
310 if (src->multi_remove_all[i])
312 /* Remove everything from dest */
313 list_remove_all(dest->multi_remove[i]);
314 dest->multi_remove_all[i] = 1;
316 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
318 else if (dest->multi_remove_all[i])
321 * dest already removes everything, so we only need to worry
324 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
325 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
329 /* No "remove all"s to worry about. */
330 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
331 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
332 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
345 /*********************************************************************
347 * Function : copy_action
349 * Description : Copy an action_specs.
350 * Similar to "dest = src".
353 * 1 : dest = Destination of copy.
354 * 2 : src = Source for copy.
358 *********************************************************************/
359 jb_err copy_action (struct action_spec *dest,
360 const struct action_spec *src)
363 jb_err err = JB_ERR_OK;
366 memset(dest, '\0', sizeof(*dest));
368 dest->mask = src->mask;
369 dest->add = src->add;
371 for (i = 0; i < ACTION_STRING_COUNT; i++)
373 char * str = src->string[i];
379 return JB_ERR_MEMORY;
381 dest->string[i] = str;
385 for (i = 0; i < ACTION_MULTI_COUNT; i++)
387 dest->multi_remove_all[i] = src->multi_remove_all[i];
388 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
393 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
403 /*********************************************************************
405 * Function : free_action
407 * Description : Destroy an action_spec. Frees memory used by it,
408 * except for the memory used by the struct action_spec
412 * 1 : src = Source to free.
416 *********************************************************************/
417 void free_action (struct action_spec *src)
426 for (i = 0; i < ACTION_STRING_COUNT; i++)
428 freez(src->string[i]);
431 for (i = 0; i < ACTION_MULTI_COUNT; i++)
433 destroy_list(src->multi_remove[i]);
434 destroy_list(src->multi_add[i]);
437 memset(src, '\0', sizeof(*src));
441 /*********************************************************************
443 * Function : get_action_token
445 * Description : Parses a line for the first action.
446 * Modifies it's input array, doesn't allocate memory.
448 * *line=" +abc{def} -ghi "
455 * 1 : line = [in] The line containing the action.
456 * [out] Start of next action on line, or
457 * NULL if we reached the end of line before
458 * we found an action.
459 * 2 : name = [out] Start of action name, null
460 * terminated. NULL on EOL
461 * 3 : value = [out] Start of action value, null
462 * terminated. NULL if none or EOL.
464 * Returns : JB_ERR_OK => Ok
465 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
467 *********************************************************************/
468 jb_err get_action_token(char **line, char **name, char **value)
473 /* set default returns */
478 /* Eat any leading whitespace */
479 while ((*str == ' ') || (*str == '\t'))
491 /* null name, just value is prohibited */
498 while (((ch = *str) != '\0') &&
499 (ch != ' ') && (ch != '\t') && (ch != '{'))
503 /* error, '}' without '{' */
515 /* EOL - be careful not to run off buffer */
520 /* More to parse next time. */
529 str = strchr(str, '}');
547 /*********************************************************************
549 * Function : get_actions
551 * Description : Parses a list of actions.
554 * 1 : line = The string containing the actions.
555 * Will be written to by this function.
556 * 2 : alias_list = Custom alias list, or NULL for none.
557 * 3 : cur_action = Where to store the action. Caller
560 * Returns : JB_ERR_OK => Ok
561 * JB_ERR_PARSE => Parse error (line was trashed anyway)
562 * nonzero => Out of memory (line was trashed anyway)
564 *********************************************************************/
565 jb_err get_actions(char *line,
566 struct action_alias * alias_list,
567 struct action_spec *cur_action)
570 init_action(cur_action);
571 cur_action->mask = ACTION_MASK_ALL;
575 char * option = NULL;
578 err = get_action_token(&line, &option, &value);
586 /* handle option in 'option' */
588 /* Check for standard action name */
589 const struct action_name * action = action_names;
591 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
595 if (action->name != NULL)
598 cur_action->mask &= action->mask;
599 cur_action->add &= action->mask;
600 cur_action->add |= action->add;
602 switch (action->takes_value)
605 /* ignore any option. */
609 /* add single string. */
611 if ((value == NULL) || (*value == '\0'))
615 /* FIXME: should validate option string here */
616 freez (cur_action->string[action->index]);
617 cur_action->string[action->index] = strdup(value);
618 if (NULL == cur_action->string[action->index])
620 return JB_ERR_MEMORY;
626 /* remove single string. */
628 freez (cur_action->string[action->index]);
633 /* append multi string. */
635 struct list * remove_p = cur_action->multi_remove[action->index];
636 struct list * add_p = cur_action->multi_add[action->index];
638 if ((value == NULL) || (*value == '\0'))
643 list_remove_item(remove_p, value);
644 err = enlist_unique(add_p, value, 0);
653 /* remove multi string. */
655 struct list * remove_p = cur_action->multi_remove[action->index];
656 struct list * add_p = cur_action->multi_add[action->index];
658 if ( (value == NULL) || (*value == '\0')
659 || ((*value == '*') && (value[1] == '\0')) )
662 * no option, or option == "*".
666 list_remove_all(remove_p);
667 list_remove_all(add_p);
668 cur_action->multi_remove_all[action->index] = 1;
672 /* Valid option - remove only 1 option */
674 if ( !cur_action->multi_remove_all[action->index] )
676 /* there isn't a catch-all in the remove list already */
677 err = enlist_unique(remove_p, value, 0);
683 list_remove_item(add_p, value);
688 /* Shouldn't get here unless there's memory corruption. */
695 /* try user aliases. */
696 const struct action_alias * alias = alias_list;
698 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
705 merge_actions(cur_action, alias->action);
709 /* Bad action name */
720 /*********************************************************************
722 * Function : init_current_action
724 * Description : Zero out an action.
727 * 1 : dest = An uninitialized current_action_spec.
731 *********************************************************************/
732 void init_current_action (struct current_action_spec *dest)
734 memset(dest, '\0', sizeof(*dest));
736 dest->flags = ACTION_MOST_COMPATIBLE;
740 /*********************************************************************
742 * Function : init_action
744 * Description : Zero out an action.
747 * 1 : dest = An uninitialized action_spec.
751 *********************************************************************/
752 void init_action (struct action_spec *dest)
754 memset(dest, '\0', sizeof(*dest));
758 /*********************************************************************
760 * Function : merge_current_action
762 * Description : Merge two actions together.
763 * Similar to "dest += src".
764 * Differences between this and merge_actions()
765 * is that this one doesn't allocate memory for
766 * strings (so "src" better be in memory for at least
767 * as long as "dest" is, and you'd better free
768 * "dest" using "free_current_action").
769 * Also, there is no mask or remove lists in dest.
770 * (If we're applying it to a URL, we don't need them)
773 * 1 : dest = Current actions, to modify.
774 * 2 : src = Action to add.
776 * Returns 0 : no error
777 * !=0 : error, probably JB_ERR_MEMORY.
779 *********************************************************************/
780 jb_err merge_current_action (struct current_action_spec *dest,
781 const struct action_spec *src)
784 jb_err err = JB_ERR_OK;
786 dest->flags &= src->mask;
787 dest->flags |= src->add;
789 for (i = 0; i < ACTION_STRING_COUNT; i++)
791 char * str = src->string[i];
797 return JB_ERR_MEMORY;
799 freez(dest->string[i]);
800 dest->string[i] = str;
804 for (i = 0; i < ACTION_MULTI_COUNT; i++)
806 if (src->multi_remove_all[i])
808 /* Remove everything from dest, then add src->multi_add */
809 err = list_duplicate(dest->multi[i], src->multi_add[i]);
817 list_remove_list(dest->multi[i], src->multi_remove[i]);
818 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
829 /*********************************************************************
831 * Function : free_current_action
833 * Description : Free memory used by a current_action_spec.
834 * Does not free the current_action_spec itself.
837 * 1 : src = Source to free.
841 *********************************************************************/
842 void free_current_action (struct current_action_spec *src)
846 for (i = 0; i < ACTION_STRING_COUNT; i++)
848 freez(src->string[i]);
851 for (i = 0; i < ACTION_MULTI_COUNT; i++)
853 destroy_list(src->multi[i]);
856 memset(src, '\0', sizeof(*src));
860 static struct file_list *current_actions_file[MAX_ACTION_FILES] = {
861 NULL, NULL, NULL, NULL, NULL,
862 NULL, NULL, NULL, NULL, NULL
866 #ifdef FEATURE_GRACEFUL_TERMINATION
867 /*********************************************************************
869 * Function : unload_current_actions_file
871 * Description : Unloads current actions file - reset to state at
872 * beginning of program.
878 *********************************************************************/
879 void unload_current_actions_file(void)
883 for (i = 0; i < MAX_ACTION_FILES; i++)
885 if (current_actions_file[i])
887 current_actions_file[i]->unloader = unload_actions_file;
888 current_actions_file[i] = NULL;
892 #endif /* FEATURE_GRACEFUL_TERMINATION */
895 /*********************************************************************
897 * Function : unload_actions_file
899 * Description : Unloads an actions module.
902 * 1 : file_data = the data structure associated with the
907 *********************************************************************/
908 void unload_actions_file(void *file_data)
910 struct url_actions * next;
911 struct url_actions * cur = (struct url_actions *)file_data;
915 free_url_spec(cur->url);
916 free_action(cur->action);
924 /*********************************************************************
926 * Function : free_alias_list
928 * Description : Free memory used by a list of aliases.
931 * 1 : alias_list = Linked list to free.
935 *********************************************************************/
936 void free_alias_list(struct action_alias *alias_list)
938 while (alias_list != NULL)
940 struct action_alias * next = alias_list->next;
941 alias_list->next = NULL;
942 freez(alias_list->name);
943 free_action(alias_list->action);
950 /*********************************************************************
952 * Function : load_actions_file
954 * Description : Read and parse all the action files and add to files
958 * 1 : csp = Current client state (buffers, headers, etc...)
960 * Returns : 0 => Ok, everything else is an error.
962 *********************************************************************/
963 int load_actions_file(struct client_state *csp)
968 for (i = 0; i < MAX_ACTION_FILES; i++)
970 if (csp->config->actions_file[i])
972 result = load_one_actions_file(csp, i);
978 else if (current_actions_file[i])
980 current_actions_file[i]->unloader = unload_actions_file;
981 current_actions_file[i] = NULL;
988 /*********************************************************************
990 * Function : load_one_actions_file
992 * Description : Read and parse a action file and add to files
996 * 1 : csp = Current client state (buffers, headers, etc...)
997 * 2 : fileid = File index to load.
999 * Returns : 0 => Ok, everything else is an error.
1001 *********************************************************************/
1002 static int load_one_actions_file(struct client_state *csp, int fileid)
1007 * Note: Keep these in the order they occur in the file, they are
1008 * sometimes tested with <=
1010 #define MODE_START_OF_FILE 1
1011 #define MODE_SETTINGS 2
1012 #define MODE_DESCRIPTION 3
1013 #define MODE_ALIAS 4
1014 #define MODE_ACTIONS 5
1016 int mode = MODE_START_OF_FILE;
1019 struct url_actions *last_perm;
1020 struct url_actions *perm;
1021 char buf[BUFFER_SIZE];
1022 struct file_list *fs;
1023 struct action_spec * cur_action = NULL;
1024 int cur_action_used = 0;
1025 struct action_alias * alias_list = NULL;
1026 unsigned long linenum = 0;
1028 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1030 /* No need to load */
1031 csp->actions_list[fileid] = current_actions_file[fileid];
1036 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1037 csp->config->actions_file[fileid]);
1038 return 1; /* never get here */
1041 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1042 if (last_perm == NULL)
1044 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1045 csp->config->actions_file[fileid]);
1046 return 1; /* never get here */
1049 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1051 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1052 csp->config->actions_file[fileid]);
1053 return 1; /* never get here */
1056 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1060 /* It's a header block */
1063 /* It's {{settings}} or {{alias}} */
1064 int len = strlen(buf);
1065 char * start = buf + 2;
1066 char * end = buf + len - 1;
1067 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1071 log_error(LOG_LEVEL_FATAL,
1072 "can't load actions file '%s': invalid line (%lu): %s",
1073 csp->config->actions_file[fileid], linenum, buf);
1074 return 1; /* never get here */
1077 /* Trim leading and trailing whitespace. */
1085 log_error(LOG_LEVEL_FATAL,
1086 "can't load actions file '%s': invalid line (%lu): {{ }}",
1087 csp->config->actions_file[fileid], linenum);
1088 return 1; /* never get here */
1092 * An actionsfile can optionally contain the following blocks.
1093 * They *MUST* be in this order, to simplify processing:
1099 * ...free text, format TBD, but no line may start with a '{'...
1104 * The actual actions must be *after* these special blocks.
1105 * None of these special blocks may be repeated.
1108 if (0 == strcmpic(start, "settings"))
1110 /* it's a {{settings}} block */
1111 if (mode >= MODE_SETTINGS)
1113 /* {{settings}} must be first thing in file and must only
1117 log_error(LOG_LEVEL_FATAL,
1118 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1119 csp->config->actions_file[fileid], linenum);
1121 mode = MODE_SETTINGS;
1123 else if (0 == strcmpic(start, "description"))
1125 /* it's a {{description}} block */
1126 if (mode >= MODE_DESCRIPTION)
1128 /* {{description}} is a singleton and only {{settings}} may proceed it
1131 log_error(LOG_LEVEL_FATAL,
1132 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1133 csp->config->actions_file[fileid], linenum);
1135 mode = MODE_DESCRIPTION;
1137 else if (0 == strcmpic(start, "alias"))
1139 /* it's an {{alias}} block */
1140 if (mode >= MODE_ALIAS)
1142 /* {{alias}} must be first thing in file, possibly after
1143 * {{settings}} and {{description}}
1145 * {{alias}} must only appear once.
1147 * Note that these are new restrictions introduced in
1148 * v2.9.10 in order to make actionsfile editing simpler.
1149 * (Otherwise, reordering actionsfile entries without
1150 * completely rewriting the file becomes non-trivial)
1153 log_error(LOG_LEVEL_FATAL,
1154 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1155 csp->config->actions_file[fileid], linenum);
1161 /* invalid {{something}} block */
1163 log_error(LOG_LEVEL_FATAL,
1164 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1165 csp->config->actions_file[fileid], linenum, start);
1166 return 1; /* never get here */
1171 /* It's an actions block */
1173 char actions_buf[BUFFER_SIZE];
1177 mode = MODE_ACTIONS;
1179 /* free old action */
1182 if (!cur_action_used)
1184 free_action(cur_action);
1189 cur_action_used = 0;
1190 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1191 if (cur_action == NULL)
1194 log_error(LOG_LEVEL_FATAL,
1195 "can't load actions file '%s': out of memory",
1196 csp->config->actions_file[fileid]);
1197 return 1; /* never get here */
1199 init_action(cur_action);
1202 strcpy(actions_buf, buf + 1);
1204 /* check we have a trailing } and then trim it */
1205 end = actions_buf + strlen(actions_buf) - 1;
1210 log_error(LOG_LEVEL_FATAL,
1211 "can't load actions file '%s': invalid line (%lu): %s",
1212 csp->config->actions_file[fileid], linenum, buf);
1213 return 1; /* never get here */
1217 /* trim any whitespace immediately inside {} */
1220 if (get_actions(actions_buf, alias_list, cur_action))
1224 log_error(LOG_LEVEL_FATAL,
1225 "can't load actions file '%s': invalid line (%lu): %s",
1226 csp->config->actions_file[fileid], linenum, buf);
1227 return 1; /* never get here */
1231 else if (mode == MODE_SETTINGS)
1234 * Part of the {{settings}} block.
1235 * Ignore for now, but we may want to read & check permissions
1236 * when we go multi-user.
1239 else if (mode == MODE_DESCRIPTION)
1242 * Part of the {{description}} block.
1246 else if (mode == MODE_ALIAS)
1251 char actions_buf[BUFFER_SIZE];
1252 struct action_alias * new_alias;
1254 char * start = strchr(buf, '=');
1257 if ((start == NULL) || (start == buf))
1259 log_error(LOG_LEVEL_FATAL,
1260 "can't load actions file '%s': invalid alias line (%lu): %s",
1261 csp->config->actions_file[fileid], linenum, buf);
1262 return 1; /* never get here */
1265 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1268 log_error(LOG_LEVEL_FATAL,
1269 "can't load actions file '%s': out of memory!",
1270 csp->config->actions_file[fileid]);
1271 return 1; /* never get here */
1274 /* Eat any the whitespace before the '=' */
1276 while ((*end == ' ') || (*end == '\t'))
1279 * we already know we must have at least 1 non-ws char
1280 * at start of buf - no need to check
1286 /* Eat any the whitespace after the '=' */
1288 while ((*start == ' ') || (*start == '\t'))
1294 log_error(LOG_LEVEL_FATAL,
1295 "can't load actions file '%s': invalid alias line (%lu): %s",
1296 csp->config->actions_file[fileid], linenum, buf);
1297 return 1; /* never get here */
1300 new_alias->name = strdup(buf);
1302 strcpy(actions_buf, start);
1304 if (get_actions(actions_buf, alias_list, new_alias->action))
1308 log_error(LOG_LEVEL_FATAL,
1309 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1310 csp->config->actions_file[fileid], linenum, buf, start);
1311 return 1; /* never get here */
1315 new_alias->next = alias_list;
1316 alias_list = new_alias;
1318 else if (mode == MODE_ACTIONS)
1320 /* it's a URL pattern */
1322 /* allocate a new node */
1323 if ((perm = zalloc(sizeof(*perm))) == NULL)
1326 log_error(LOG_LEVEL_FATAL,
1327 "can't load actions file '%s': out of memory!",
1328 csp->config->actions_file[fileid]);
1329 return 1; /* never get here */
1333 copy_action (perm->action, cur_action);
1335 /* Save the URL pattern */
1336 if (create_url_spec(perm->url, buf))
1339 log_error(LOG_LEVEL_FATAL,
1340 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1341 csp->config->actions_file[fileid], linenum, buf);
1342 return 1; /* never get here */
1345 /* add it to the list */
1346 last_perm->next = perm;
1349 else if (mode == MODE_START_OF_FILE)
1351 /* oops - please have a {} line as 1st line in file. */
1353 log_error(LOG_LEVEL_FATAL,
1354 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1355 csp->config->actions_file[fileid], linenum, buf);
1356 return 1; /* never get here */
1360 /* How did we get here? This is impossible! */
1362 log_error(LOG_LEVEL_FATAL,
1363 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1364 csp->config->actions_file[fileid], mode);
1365 return 1; /* never get here */
1371 free_action(cur_action);
1373 free_alias_list(alias_list);
1375 /* the old one is now obsolete */
1376 if (current_actions_file[fileid])
1378 current_actions_file[fileid]->unloader = unload_actions_file;
1381 fs->next = files->next;
1383 current_actions_file[fileid] = fs;
1385 csp->actions_list[fileid] = fs;
1392 /*********************************************************************
1394 * Function : actions_to_text
1396 * Description : Converts a actionsfile entry from the internal
1397 * structurt into a text line. The output is split
1398 * into one line for each action with line continuation.
1401 * 1 : action = The action to format.
1403 * Returns : A string. Caller must free it.
1404 * NULL on out-of-memory error.
1406 *********************************************************************/
1407 char * actions_to_text(struct action_spec *action)
1409 unsigned mask = action->mask;
1410 unsigned add = action->add;
1411 char * result = strdup("");
1412 struct list_entry * lst;
1414 /* sanity - prevents "-feature +feature" */
1418 #define DEFINE_ACTION_BOOL(__name, __bit) \
1419 if (!(mask & __bit)) \
1421 string_append(&result, " -" __name " \\\n"); \
1423 else if (add & __bit) \
1425 string_append(&result, " +" __name " \\\n"); \
1428 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1429 if (!(mask & __bit)) \
1431 string_append(&result, " -" __name " \\\n"); \
1433 else if (add & __bit) \
1435 string_append(&result, " +" __name "{"); \
1436 string_append(&result, action->string[__index]); \
1437 string_append(&result, "} \\\n"); \
1440 #define DEFINE_ACTION_MULTI(__name, __index) \
1441 if (action->multi_remove_all[__index]) \
1443 string_append(&result, " -" __name " \\\n"); \
1447 lst = action->multi_remove[__index]->first; \
1450 string_append(&result, " -" __name "{"); \
1451 string_append(&result, lst->str); \
1452 string_append(&result, "} \\\n"); \
1456 lst = action->multi_add[__index]->first; \
1459 string_append(&result, " +" __name "{"); \
1460 string_append(&result, lst->str); \
1461 string_append(&result, "} \\\n"); \
1465 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1467 #include "actionlist.h"
1469 #undef DEFINE_ACTION_MULTI
1470 #undef DEFINE_ACTION_STRING
1471 #undef DEFINE_ACTION_BOOL
1472 #undef DEFINE_ACTION_ALIAS
1478 /*********************************************************************
1480 * Function : actions_to_html
1482 * Description : Converts a actionsfile entry from numeric form
1483 * ("mask" and "add") to a <br>-seperated HTML string
1484 * in which each action is linked to its chapter in
1488 * 1 : csp = Client state (for config)
1489 * 2 : action = Action spec to be converted
1491 * Returns : A string. Caller must free it.
1492 * NULL on out-of-memory error.
1494 *********************************************************************/
1495 char * actions_to_html(struct client_state *csp,
1496 struct action_spec *action)
1498 unsigned mask = action->mask;
1499 unsigned add = action->add;
1500 char * result = strdup("");
1501 struct list_entry * lst;
1503 /* sanity - prevents "-feature +feature" */
1507 #define DEFINE_ACTION_BOOL(__name, __bit) \
1508 if (!(mask & __bit)) \
1510 string_append(&result, "\n<br>-"); \
1511 string_join(&result, add_help_link(__name, csp->config)); \
1513 else if (add & __bit) \
1515 string_append(&result, "\n<br>+"); \
1516 string_join(&result, add_help_link(__name, csp->config)); \
1519 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1520 if (!(mask & __bit)) \
1522 string_append(&result, "\n<br>-"); \
1523 string_join(&result, add_help_link(__name, csp->config)); \
1525 else if (add & __bit) \
1527 string_append(&result, "\n<br>+"); \
1528 string_join(&result, add_help_link(__name, csp->config)); \
1529 string_append(&result, "{"); \
1530 string_join(&result, html_encode(action->string[__index])); \
1531 string_append(&result, "}"); \
1534 #define DEFINE_ACTION_MULTI(__name, __index) \
1535 if (action->multi_remove_all[__index]) \
1537 string_append(&result, "\n<br>-"); \
1538 string_join(&result, add_help_link(__name, csp->config)); \
1542 lst = action->multi_remove[__index]->first; \
1545 string_append(&result, "\n<br>-"); \
1546 string_join(&result, add_help_link(__name, csp->config)); \
1547 string_append(&result, "{"); \
1548 string_join(&result, html_encode(lst->str)); \
1549 string_append(&result, "}"); \
1553 lst = action->multi_add[__index]->first; \
1556 string_append(&result, "\n<br>+"); \
1557 string_join(&result, add_help_link(__name, csp->config)); \
1558 string_append(&result, "{"); \
1559 string_join(&result, html_encode(lst->str)); \
1560 string_append(&result, "}"); \
1564 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1566 #include "actionlist.h"
1568 #undef DEFINE_ACTION_MULTI
1569 #undef DEFINE_ACTION_STRING
1570 #undef DEFINE_ACTION_BOOL
1571 #undef DEFINE_ACTION_ALIAS
1573 /* trim leading <br> */
1574 if (result && *result)
1577 result = strdup(result + 5);
1585 /*********************************************************************
1587 * Function : current_actions_to_html
1589 * Description : Converts a curren action spec to a <br> seperated HTML
1590 * text in which each action is linked to its chapter in
1594 * 1 : csp = Client state (for config)
1595 * 2 : action = Current action spec to be converted
1597 * Returns : A string. Caller must free it.
1598 * NULL on out-of-memory error.
1600 *********************************************************************/
1601 char *current_action_to_html(struct client_state *csp,
1602 struct current_action_spec *action)
1604 unsigned long flags = action->flags;
1605 char * result = strdup("");
1606 struct list_entry * lst;
1608 #define DEFINE_ACTION_BOOL(__name, __bit) \
1609 if (flags & __bit) \
1611 string_append(&result, "\n<br>+"); \
1612 string_join(&result, add_help_link(__name, csp->config)); \
1616 string_append(&result, "\n<br>-"); \
1617 string_join(&result, add_help_link(__name, csp->config)); \
1620 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1621 if (flags & __bit) \
1623 string_append(&result, "\n<br>+"); \
1624 string_join(&result, add_help_link(__name, csp->config)); \
1625 string_append(&result, "{"); \
1626 string_join(&result, html_encode(action->string[__index])); \
1627 string_append(&result, "}"); \
1631 string_append(&result, "\n<br>-"); \
1632 string_join(&result, add_help_link(__name, csp->config)); \
1635 #define DEFINE_ACTION_MULTI(__name, __index) \
1636 lst = action->multi[__index]->first; \
1639 string_append(&result, "\n<br>-"); \
1640 string_join(&result, add_help_link(__name, csp->config)); \
1646 string_append(&result, "\n<br>+"); \
1647 string_join(&result, add_help_link(__name, csp->config)); \
1648 string_append(&result, "{"); \
1649 string_join(&result, html_encode(lst->str)); \
1650 string_append(&result, "}"); \
1655 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1657 #include "actionlist.h"
1659 #undef DEFINE_ACTION_MULTI
1660 #undef DEFINE_ACTION_STRING
1661 #undef DEFINE_ACTION_BOOL
1662 #undef DEFINE_ACTION_ALIAS