1 const char actions_rcs[] = "$Id: actions.c,v 1.32.2.2 2002/11/20 14:36:55 oes 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.2 2002/11/20 14:36:55 oes
37 * Extended unload_current_actions_file() to multiple AFs.
38 * Thanks to Oliver Stoeneberg for the hint.
40 * Revision 1.32.2.1 2002/05/26 12:13:16 roro
41 * Change unsigned to unsigned long in actions_name struct. This closes
42 * SourceForge Bug #539284.
44 * Revision 1.32 2002/05/12 21:36:29 jongfoster
45 * Correcting function comments
47 * Revision 1.31 2002/05/06 07:56:50 oes
48 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
50 * Revision 1.30 2002/04/30 11:14:52 oes
51 * Made csp the first parameter in *action_to_html
53 * Revision 1.29 2002/04/26 19:30:54 jongfoster
54 * - current_action_to_html(): Adding help link for the "-" form of
56 * - Some actions had "<br>-", some "<br> -" (note the space).
57 * Standardizing on no space.
58 * - Greatly simplifying some of the code by using string_join()
61 * Revision 1.28 2002/04/26 12:53:15 oes
62 * - CGI AF editor now writes action lines split into
63 * single lines with line continuation
64 * - actions_to_html now embeds each action name in
66 * - current_action_to_text is now called current_action_to_html
67 * and acts like actions_to_html
69 * Revision 1.27 2002/04/24 02:10:31 oes
70 * - Jon's patch for multiple AFs:
71 * - split load_actions_file, add load_one_actions_file
72 * - make csp->actions_list files an array
73 * - remember file id with each action
74 * - Copy_action now frees dest action before copying
76 * Revision 1.26 2002/03/26 22:29:54 swa
77 * we have a new homepage!
79 * Revision 1.25 2002/03/24 13:25:43 swa
80 * name change related issues
82 * Revision 1.24 2002/03/16 23:54:06 jongfoster
83 * Adding graceful termination feature, to help look for memory leaks.
84 * If you enable this (which, by design, has to be done by hand
85 * editing config.h) and then go to http://i.j.b/die, then the program
86 * will exit cleanly after the *next* request. It should free all the
87 * memory that was used.
89 * Revision 1.23 2002/03/07 03:46:16 oes
90 * Fixed compiler warnings
92 * Revision 1.22 2002/01/21 00:27:02 jongfoster
93 * Allowing free_action(NULL).
94 * Moving the functions that #include actionlist.h to the end of the file,
95 * because the Visual C++ 97 debugger gets extremely confused if you try
96 * to debug any code that comes after them in the file.
98 * Revision 1.21 2002/01/17 20:54:44 jongfoster
99 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
101 * Revision 1.20 2001/11/22 21:56:49 jongfoster
102 * Making action_spec->flags into an unsigned long rather than just an
104 * Fixing a bug in the display of -add-header and -wafer
106 * Revision 1.19 2001/11/13 00:14:07 jongfoster
107 * Fixing stupid bug now I've figured out what || means.
108 * (It always returns 0 or 1, not one of it's paramaters.)
110 * Revision 1.18 2001/11/07 00:06:06 steudten
111 * Add line number in error output for lineparsing for
114 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
115 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
116 * threads to call select() simultaneously. So, it's time to do a real, live,
117 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
118 * (native). Both versions will work, but using __OS2__ offers multi-threading.
120 * Revision 1.16 2001/10/23 21:30:30 jongfoster
121 * Adding error-checking to selected functions.
123 * Revision 1.15 2001/10/14 21:58:22 jongfoster
124 * Adding support for the CGI-based editor:
125 * - Exported get_actions()
126 * - Added new function free_alias_list()
127 * - Added support for {{settings}} and {{description}} blocks
128 * in the actions file. They are currently ignored.
129 * - Added restriction to only one {{alias}} block which must appear
130 * first in the file, to simplify the editor's rewriting rules.
131 * - Note that load_actions_file() is no longer used by the CGI-based
132 * editor, but some of the other routines in this file are.
134 * Revision 1.14 2001/09/22 16:36:59 jongfoster
135 * Removing unused parameter fs from read_config_line()
137 * Revision 1.13 2001/09/16 15:47:37 jongfoster
138 * First version of CGI-based edit interface. This is very much a
139 * work-in-progress, and you can't actually use it to edit anything
140 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
141 * to have any effect.
143 * Revision 1.12 2001/09/16 13:21:27 jongfoster
144 * Changes to use new list functions.
146 * Revision 1.11 2001/09/14 00:17:32 jongfoster
147 * Tidying up memory allocation. New function init_action().
149 * Revision 1.10 2001/09/10 10:14:34 oes
150 * Removing unused variable
152 * Revision 1.9 2001/07/30 22:08:36 jongfoster
153 * Tidying up #defines:
154 * - All feature #defines are now of the form FEATURE_xxx
155 * - Permanently turned off WIN_GUI_EDIT
156 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
158 * Revision 1.8 2001/06/29 13:19:52 oes
159 * Removed logentry from cancelled commit
161 * Revision 1.7 2001/06/09 10:55:28 jongfoster
162 * Changing BUFSIZ ==> BUFFER_SIZE
164 * Revision 1.6 2001/06/07 23:04:34 jongfoster
165 * Made get_actions() static.
167 * Revision 1.5 2001/06/03 19:11:48 oes
168 * adapted to new enlist_unique arg format
170 * Revision 1.4 2001/06/01 20:03:42 jongfoster
171 * Better memory management - current_action->strings[] now
172 * contains copies of the strings, not the original.
174 * Revision 1.3 2001/06/01 18:49:17 jongfoster
175 * Replaced "list_share" with "list" - the tiny memory gain was not
176 * worth the extra complexity.
178 * Revision 1.2 2001/05/31 21:40:00 jongfoster
179 * Removing some commented out, obsolete blocks of code.
181 * Revision 1.1 2001/05/31 21:16:46 jongfoster
182 * Moved functions to process the action list into this new file.
185 *********************************************************************/
199 #include "miscutil.h"
203 #include "urlmatch.h"
206 const char actions_h_rcs[] = ACTIONS_H_VERSION;
210 * We need the main list of options.
212 * First, we need a way to tell between boolean, string, and multi-string
213 * options. For string and multistring options, we also need to be
214 * able to tell the difference between a "+" and a "-". (For bools,
215 * the "+"/"-" information is encoded in "add" and "mask"). So we use
216 * an enumerated type (well, the preprocessor equivalent). Here are
219 #define AV_NONE 0 /* +opt -opt */
220 #define AV_ADD_STRING 1 /* +stropt{string} */
221 #define AV_REM_STRING 2 /* -stropt */
222 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
223 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
226 * We need a structure to hold the name, flag changes,
227 * type, and string index.
232 unsigned long mask; /* a bit set to "0" = remove action */
233 unsigned long add; /* a bit set to "1" = add action */
234 int takes_value; /* an AV_... constant */
235 int index; /* index into strings[] or multi[] */
239 * And with those building blocks in place, here's the array.
241 static const struct action_name action_names[] =
244 * Well actually there's no data here - it's in actionlist.h
245 * This keeps it together to make it easy to change.
247 * Here's the macros used to format it:
249 #define DEFINE_ACTION_MULTI(name,index) \
250 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
251 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
252 #define DEFINE_ACTION_STRING(name,flag,index) \
253 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
254 { "-" name, ~flag, 0, AV_REM_STRING, index },
255 #define DEFINE_ACTION_BOOL(name,flag) \
256 { "+" name, ACTION_MASK_ALL, flag }, \
257 { "-" name, ~flag, 0 },
258 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
260 #include "actionlist.h"
262 #undef DEFINE_ACTION_MULTI
263 #undef DEFINE_ACTION_STRING
264 #undef DEFINE_ACTION_BOOL
265 #undef DEFINE_ACTION_ALIAS
267 { NULL, 0, 0 } /* End marker */
271 static int load_one_actions_file(struct client_state *csp, int fileid);
274 /*********************************************************************
276 * Function : merge_actions
278 * Description : Merge two actions together.
279 * Similar to "dest += src".
282 * 1 : dest = Actions to modify.
283 * 2 : src = Action to add.
285 * Returns : JB_ERR_OK or JB_ERR_MEMORY
287 *********************************************************************/
288 jb_err merge_actions (struct action_spec *dest,
289 const struct action_spec *src)
294 dest->mask &= src->mask;
295 dest->add &= src->mask;
296 dest->add |= src->add;
298 for (i = 0; i < ACTION_STRING_COUNT; i++)
300 char * str = src->string[i];
303 freez(dest->string[i]);
304 dest->string[i] = strdup(str);
305 if (NULL == dest->string[i])
307 return JB_ERR_MEMORY;
312 for (i = 0; i < ACTION_MULTI_COUNT; i++)
314 if (src->multi_remove_all[i])
316 /* Remove everything from dest */
317 list_remove_all(dest->multi_remove[i]);
318 dest->multi_remove_all[i] = 1;
320 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
322 else if (dest->multi_remove_all[i])
325 * dest already removes everything, so we only need to worry
328 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
329 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
333 /* No "remove all"s to worry about. */
334 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
335 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
336 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
349 /*********************************************************************
351 * Function : copy_action
353 * Description : Copy an action_specs.
354 * Similar to "dest = src".
357 * 1 : dest = Destination of copy.
358 * 2 : src = Source for copy.
362 *********************************************************************/
363 jb_err copy_action (struct action_spec *dest,
364 const struct action_spec *src)
367 jb_err err = JB_ERR_OK;
370 memset(dest, '\0', sizeof(*dest));
372 dest->mask = src->mask;
373 dest->add = src->add;
375 for (i = 0; i < ACTION_STRING_COUNT; i++)
377 char * str = src->string[i];
383 return JB_ERR_MEMORY;
385 dest->string[i] = str;
389 for (i = 0; i < ACTION_MULTI_COUNT; i++)
391 dest->multi_remove_all[i] = src->multi_remove_all[i];
392 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
397 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
407 /*********************************************************************
409 * Function : free_action
411 * Description : Destroy an action_spec. Frees memory used by it,
412 * except for the memory used by the struct action_spec
416 * 1 : src = Source to free.
420 *********************************************************************/
421 void free_action (struct action_spec *src)
430 for (i = 0; i < ACTION_STRING_COUNT; i++)
432 freez(src->string[i]);
435 for (i = 0; i < ACTION_MULTI_COUNT; i++)
437 destroy_list(src->multi_remove[i]);
438 destroy_list(src->multi_add[i]);
441 memset(src, '\0', sizeof(*src));
445 /*********************************************************************
447 * Function : get_action_token
449 * Description : Parses a line for the first action.
450 * Modifies it's input array, doesn't allocate memory.
452 * *line=" +abc{def} -ghi "
459 * 1 : line = [in] The line containing the action.
460 * [out] Start of next action on line, or
461 * NULL if we reached the end of line before
462 * we found an action.
463 * 2 : name = [out] Start of action name, null
464 * terminated. NULL on EOL
465 * 3 : value = [out] Start of action value, null
466 * terminated. NULL if none or EOL.
468 * Returns : JB_ERR_OK => Ok
469 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
471 *********************************************************************/
472 jb_err get_action_token(char **line, char **name, char **value)
477 /* set default returns */
482 /* Eat any leading whitespace */
483 while ((*str == ' ') || (*str == '\t'))
495 /* null name, just value is prohibited */
502 while (((ch = *str) != '\0') &&
503 (ch != ' ') && (ch != '\t') && (ch != '{'))
507 /* error, '}' without '{' */
519 /* EOL - be careful not to run off buffer */
524 /* More to parse next time. */
533 str = strchr(str, '}');
551 /*********************************************************************
553 * Function : get_actions
555 * Description : Parses a list of actions.
558 * 1 : line = The string containing the actions.
559 * Will be written to by this function.
560 * 2 : alias_list = Custom alias list, or NULL for none.
561 * 3 : cur_action = Where to store the action. Caller
564 * Returns : JB_ERR_OK => Ok
565 * JB_ERR_PARSE => Parse error (line was trashed anyway)
566 * nonzero => Out of memory (line was trashed anyway)
568 *********************************************************************/
569 jb_err get_actions(char *line,
570 struct action_alias * alias_list,
571 struct action_spec *cur_action)
574 init_action(cur_action);
575 cur_action->mask = ACTION_MASK_ALL;
579 char * option = NULL;
582 err = get_action_token(&line, &option, &value);
590 /* handle option in 'option' */
592 /* Check for standard action name */
593 const struct action_name * action = action_names;
595 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
599 if (action->name != NULL)
602 cur_action->mask &= action->mask;
603 cur_action->add &= action->mask;
604 cur_action->add |= action->add;
606 switch (action->takes_value)
609 /* ignore any option. */
613 /* add single string. */
615 if ((value == NULL) || (*value == '\0'))
619 /* FIXME: should validate option string here */
620 freez (cur_action->string[action->index]);
621 cur_action->string[action->index] = strdup(value);
622 if (NULL == cur_action->string[action->index])
624 return JB_ERR_MEMORY;
630 /* remove single string. */
632 freez (cur_action->string[action->index]);
637 /* append multi string. */
639 struct list * remove_p = cur_action->multi_remove[action->index];
640 struct list * add_p = cur_action->multi_add[action->index];
642 if ((value == NULL) || (*value == '\0'))
647 list_remove_item(remove_p, value);
648 err = enlist_unique(add_p, value, 0);
657 /* remove multi string. */
659 struct list * remove_p = cur_action->multi_remove[action->index];
660 struct list * add_p = cur_action->multi_add[action->index];
662 if ( (value == NULL) || (*value == '\0')
663 || ((*value == '*') && (value[1] == '\0')) )
666 * no option, or option == "*".
670 list_remove_all(remove_p);
671 list_remove_all(add_p);
672 cur_action->multi_remove_all[action->index] = 1;
676 /* Valid option - remove only 1 option */
678 if ( !cur_action->multi_remove_all[action->index] )
680 /* there isn't a catch-all in the remove list already */
681 err = enlist_unique(remove_p, value, 0);
687 list_remove_item(add_p, value);
692 /* Shouldn't get here unless there's memory corruption. */
699 /* try user aliases. */
700 const struct action_alias * alias = alias_list;
702 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
709 merge_actions(cur_action, alias->action);
713 /* Bad action name */
724 /*********************************************************************
726 * Function : init_current_action
728 * Description : Zero out an action.
731 * 1 : dest = An uninitialized current_action_spec.
735 *********************************************************************/
736 void init_current_action (struct current_action_spec *dest)
738 memset(dest, '\0', sizeof(*dest));
740 dest->flags = ACTION_MOST_COMPATIBLE;
744 /*********************************************************************
746 * Function : init_action
748 * Description : Zero out an action.
751 * 1 : dest = An uninitialized action_spec.
755 *********************************************************************/
756 void init_action (struct action_spec *dest)
758 memset(dest, '\0', sizeof(*dest));
762 /*********************************************************************
764 * Function : merge_current_action
766 * Description : Merge two actions together.
767 * Similar to "dest += src".
768 * Differences between this and merge_actions()
769 * is that this one doesn't allocate memory for
770 * strings (so "src" better be in memory for at least
771 * as long as "dest" is, and you'd better free
772 * "dest" using "free_current_action").
773 * Also, there is no mask or remove lists in dest.
774 * (If we're applying it to a URL, we don't need them)
777 * 1 : dest = Current actions, to modify.
778 * 2 : src = Action to add.
780 * Returns 0 : no error
781 * !=0 : error, probably JB_ERR_MEMORY.
783 *********************************************************************/
784 jb_err merge_current_action (struct current_action_spec *dest,
785 const struct action_spec *src)
788 jb_err err = JB_ERR_OK;
790 dest->flags &= src->mask;
791 dest->flags |= src->add;
793 for (i = 0; i < ACTION_STRING_COUNT; i++)
795 char * str = src->string[i];
801 return JB_ERR_MEMORY;
803 freez(dest->string[i]);
804 dest->string[i] = str;
808 for (i = 0; i < ACTION_MULTI_COUNT; i++)
810 if (src->multi_remove_all[i])
812 /* Remove everything from dest, then add src->multi_add */
813 err = list_duplicate(dest->multi[i], src->multi_add[i]);
821 list_remove_list(dest->multi[i], src->multi_remove[i]);
822 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
833 /*********************************************************************
835 * Function : free_current_action
837 * Description : Free memory used by a current_action_spec.
838 * Does not free the current_action_spec itself.
841 * 1 : src = Source to free.
845 *********************************************************************/
846 void free_current_action (struct current_action_spec *src)
850 for (i = 0; i < ACTION_STRING_COUNT; i++)
852 freez(src->string[i]);
855 for (i = 0; i < ACTION_MULTI_COUNT; i++)
857 destroy_list(src->multi[i]);
860 memset(src, '\0', sizeof(*src));
864 static struct file_list *current_actions_file[MAX_ACTION_FILES] = {
865 NULL, NULL, NULL, NULL, NULL,
866 NULL, NULL, NULL, NULL, NULL
870 #ifdef FEATURE_GRACEFUL_TERMINATION
871 /*********************************************************************
873 * Function : unload_current_actions_file
875 * Description : Unloads current actions file - reset to state at
876 * beginning of program.
882 *********************************************************************/
883 void unload_current_actions_file(void)
887 for (i = 0; i < MAX_ACTION_FILES; i++)
889 if (current_actions_file[i])
891 current_actions_file[i]->unloader = unload_actions_file;
892 current_actions_file[i] = NULL;
896 #endif /* FEATURE_GRACEFUL_TERMINATION */
899 /*********************************************************************
901 * Function : unload_actions_file
903 * Description : Unloads an actions module.
906 * 1 : file_data = the data structure associated with the
911 *********************************************************************/
912 void unload_actions_file(void *file_data)
914 struct url_actions * next;
915 struct url_actions * cur = (struct url_actions *)file_data;
919 free_url_spec(cur->url);
920 free_action(cur->action);
928 /*********************************************************************
930 * Function : free_alias_list
932 * Description : Free memory used by a list of aliases.
935 * 1 : alias_list = Linked list to free.
939 *********************************************************************/
940 void free_alias_list(struct action_alias *alias_list)
942 while (alias_list != NULL)
944 struct action_alias * next = alias_list->next;
945 alias_list->next = NULL;
946 freez(alias_list->name);
947 free_action(alias_list->action);
954 /*********************************************************************
956 * Function : load_actions_file
958 * Description : Read and parse all the action files and add to files
962 * 1 : csp = Current client state (buffers, headers, etc...)
964 * Returns : 0 => Ok, everything else is an error.
966 *********************************************************************/
967 int load_actions_file(struct client_state *csp)
972 for (i = 0; i < MAX_ACTION_FILES; i++)
974 if (csp->config->actions_file[i])
976 result = load_one_actions_file(csp, i);
982 else if (current_actions_file[i])
984 current_actions_file[i]->unloader = unload_actions_file;
985 current_actions_file[i] = NULL;
992 /*********************************************************************
994 * Function : load_one_actions_file
996 * Description : Read and parse a action file and add to files
1000 * 1 : csp = Current client state (buffers, headers, etc...)
1001 * 2 : fileid = File index to load.
1003 * Returns : 0 => Ok, everything else is an error.
1005 *********************************************************************/
1006 static int load_one_actions_file(struct client_state *csp, int fileid)
1011 * Note: Keep these in the order they occur in the file, they are
1012 * sometimes tested with <=
1014 #define MODE_START_OF_FILE 1
1015 #define MODE_SETTINGS 2
1016 #define MODE_DESCRIPTION 3
1017 #define MODE_ALIAS 4
1018 #define MODE_ACTIONS 5
1020 int mode = MODE_START_OF_FILE;
1023 struct url_actions *last_perm;
1024 struct url_actions *perm;
1025 char buf[BUFFER_SIZE];
1026 struct file_list *fs;
1027 struct action_spec * cur_action = NULL;
1028 int cur_action_used = 0;
1029 struct action_alias * alias_list = NULL;
1030 unsigned long linenum = 0;
1032 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1034 /* No need to load */
1035 csp->actions_list[fileid] = current_actions_file[fileid];
1040 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1041 csp->config->actions_file[fileid]);
1042 return 1; /* never get here */
1045 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1046 if (last_perm == NULL)
1048 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1049 csp->config->actions_file[fileid]);
1050 return 1; /* never get here */
1053 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1055 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1056 csp->config->actions_file[fileid]);
1057 return 1; /* never get here */
1060 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1064 /* It's a header block */
1067 /* It's {{settings}} or {{alias}} */
1068 int len = strlen(buf);
1069 char * start = buf + 2;
1070 char * end = buf + len - 1;
1071 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1075 log_error(LOG_LEVEL_FATAL,
1076 "can't load actions file '%s': invalid line (%lu): %s",
1077 csp->config->actions_file[fileid], linenum, buf);
1078 return 1; /* never get here */
1081 /* Trim leading and trailing whitespace. */
1089 log_error(LOG_LEVEL_FATAL,
1090 "can't load actions file '%s': invalid line (%lu): {{ }}",
1091 csp->config->actions_file[fileid], linenum);
1092 return 1; /* never get here */
1096 * An actionsfile can optionally contain the following blocks.
1097 * They *MUST* be in this order, to simplify processing:
1103 * ...free text, format TBD, but no line may start with a '{'...
1108 * The actual actions must be *after* these special blocks.
1109 * None of these special blocks may be repeated.
1112 if (0 == strcmpic(start, "settings"))
1114 /* it's a {{settings}} block */
1115 if (mode >= MODE_SETTINGS)
1117 /* {{settings}} must be first thing in file and must only
1121 log_error(LOG_LEVEL_FATAL,
1122 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1123 csp->config->actions_file[fileid], linenum);
1125 mode = MODE_SETTINGS;
1127 else if (0 == strcmpic(start, "description"))
1129 /* it's a {{description}} block */
1130 if (mode >= MODE_DESCRIPTION)
1132 /* {{description}} is a singleton and only {{settings}} may proceed it
1135 log_error(LOG_LEVEL_FATAL,
1136 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1137 csp->config->actions_file[fileid], linenum);
1139 mode = MODE_DESCRIPTION;
1141 else if (0 == strcmpic(start, "alias"))
1143 /* it's an {{alias}} block */
1144 if (mode >= MODE_ALIAS)
1146 /* {{alias}} must be first thing in file, possibly after
1147 * {{settings}} and {{description}}
1149 * {{alias}} must only appear once.
1151 * Note that these are new restrictions introduced in
1152 * v2.9.10 in order to make actionsfile editing simpler.
1153 * (Otherwise, reordering actionsfile entries without
1154 * completely rewriting the file becomes non-trivial)
1157 log_error(LOG_LEVEL_FATAL,
1158 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1159 csp->config->actions_file[fileid], linenum);
1165 /* invalid {{something}} block */
1167 log_error(LOG_LEVEL_FATAL,
1168 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1169 csp->config->actions_file[fileid], linenum, start);
1170 return 1; /* never get here */
1175 /* It's an actions block */
1177 char actions_buf[BUFFER_SIZE];
1181 mode = MODE_ACTIONS;
1183 /* free old action */
1186 if (!cur_action_used)
1188 free_action(cur_action);
1193 cur_action_used = 0;
1194 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1195 if (cur_action == NULL)
1198 log_error(LOG_LEVEL_FATAL,
1199 "can't load actions file '%s': out of memory",
1200 csp->config->actions_file[fileid]);
1201 return 1; /* never get here */
1203 init_action(cur_action);
1206 strcpy(actions_buf, buf + 1);
1208 /* check we have a trailing } and then trim it */
1209 end = actions_buf + strlen(actions_buf) - 1;
1214 log_error(LOG_LEVEL_FATAL,
1215 "can't load actions file '%s': invalid line (%lu): %s",
1216 csp->config->actions_file[fileid], linenum, buf);
1217 return 1; /* never get here */
1221 /* trim any whitespace immediately inside {} */
1224 if (get_actions(actions_buf, alias_list, cur_action))
1228 log_error(LOG_LEVEL_FATAL,
1229 "can't load actions file '%s': invalid line (%lu): %s",
1230 csp->config->actions_file[fileid], linenum, buf);
1231 return 1; /* never get here */
1235 else if (mode == MODE_SETTINGS)
1238 * Part of the {{settings}} block.
1239 * Ignore for now, but we may want to read & check permissions
1240 * when we go multi-user.
1243 else if (mode == MODE_DESCRIPTION)
1246 * Part of the {{description}} block.
1250 else if (mode == MODE_ALIAS)
1255 char actions_buf[BUFFER_SIZE];
1256 struct action_alias * new_alias;
1258 char * start = strchr(buf, '=');
1261 if ((start == NULL) || (start == buf))
1263 log_error(LOG_LEVEL_FATAL,
1264 "can't load actions file '%s': invalid alias line (%lu): %s",
1265 csp->config->actions_file[fileid], linenum, buf);
1266 return 1; /* never get here */
1269 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1272 log_error(LOG_LEVEL_FATAL,
1273 "can't load actions file '%s': out of memory!",
1274 csp->config->actions_file[fileid]);
1275 return 1; /* never get here */
1278 /* Eat any the whitespace before the '=' */
1280 while ((*end == ' ') || (*end == '\t'))
1283 * we already know we must have at least 1 non-ws char
1284 * at start of buf - no need to check
1290 /* Eat any the whitespace after the '=' */
1292 while ((*start == ' ') || (*start == '\t'))
1298 log_error(LOG_LEVEL_FATAL,
1299 "can't load actions file '%s': invalid alias line (%lu): %s",
1300 csp->config->actions_file[fileid], linenum, buf);
1301 return 1; /* never get here */
1304 new_alias->name = strdup(buf);
1306 strcpy(actions_buf, start);
1308 if (get_actions(actions_buf, alias_list, new_alias->action))
1312 log_error(LOG_LEVEL_FATAL,
1313 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1314 csp->config->actions_file[fileid], linenum, buf, start);
1315 return 1; /* never get here */
1319 new_alias->next = alias_list;
1320 alias_list = new_alias;
1322 else if (mode == MODE_ACTIONS)
1324 /* it's a URL pattern */
1326 /* allocate a new node */
1327 if ((perm = zalloc(sizeof(*perm))) == NULL)
1330 log_error(LOG_LEVEL_FATAL,
1331 "can't load actions file '%s': out of memory!",
1332 csp->config->actions_file[fileid]);
1333 return 1; /* never get here */
1337 copy_action (perm->action, cur_action);
1339 /* Save the URL pattern */
1340 if (create_url_spec(perm->url, buf))
1343 log_error(LOG_LEVEL_FATAL,
1344 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1345 csp->config->actions_file[fileid], linenum, buf);
1346 return 1; /* never get here */
1349 /* add it to the list */
1350 last_perm->next = perm;
1353 else if (mode == MODE_START_OF_FILE)
1355 /* oops - please have a {} line as 1st line in file. */
1357 log_error(LOG_LEVEL_FATAL,
1358 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1359 csp->config->actions_file[fileid], linenum, buf);
1360 return 1; /* never get here */
1364 /* How did we get here? This is impossible! */
1366 log_error(LOG_LEVEL_FATAL,
1367 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1368 csp->config->actions_file[fileid], mode);
1369 return 1; /* never get here */
1375 free_action(cur_action);
1378 free_alias_list(alias_list);
1380 /* the old one is now obsolete */
1381 if (current_actions_file[fileid])
1383 current_actions_file[fileid]->unloader = unload_actions_file;
1386 fs->next = files->next;
1388 current_actions_file[fileid] = fs;
1390 csp->actions_list[fileid] = fs;
1397 /*********************************************************************
1399 * Function : actions_to_text
1401 * Description : Converts a actionsfile entry from the internal
1402 * structurt into a text line. The output is split
1403 * into one line for each action with line continuation.
1406 * 1 : action = The action to format.
1408 * Returns : A string. Caller must free it.
1409 * NULL on out-of-memory error.
1411 *********************************************************************/
1412 char * actions_to_text(struct action_spec *action)
1414 unsigned mask = action->mask;
1415 unsigned add = action->add;
1416 char * result = strdup("");
1417 struct list_entry * lst;
1419 /* sanity - prevents "-feature +feature" */
1423 #define DEFINE_ACTION_BOOL(__name, __bit) \
1424 if (!(mask & __bit)) \
1426 string_append(&result, " -" __name " \\\n"); \
1428 else if (add & __bit) \
1430 string_append(&result, " +" __name " \\\n"); \
1433 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1434 if (!(mask & __bit)) \
1436 string_append(&result, " -" __name " \\\n"); \
1438 else if (add & __bit) \
1440 string_append(&result, " +" __name "{"); \
1441 string_append(&result, action->string[__index]); \
1442 string_append(&result, "} \\\n"); \
1445 #define DEFINE_ACTION_MULTI(__name, __index) \
1446 if (action->multi_remove_all[__index]) \
1448 string_append(&result, " -" __name " \\\n"); \
1452 lst = action->multi_remove[__index]->first; \
1455 string_append(&result, " -" __name "{"); \
1456 string_append(&result, lst->str); \
1457 string_append(&result, "} \\\n"); \
1461 lst = action->multi_add[__index]->first; \
1464 string_append(&result, " +" __name "{"); \
1465 string_append(&result, lst->str); \
1466 string_append(&result, "} \\\n"); \
1470 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1472 #include "actionlist.h"
1474 #undef DEFINE_ACTION_MULTI
1475 #undef DEFINE_ACTION_STRING
1476 #undef DEFINE_ACTION_BOOL
1477 #undef DEFINE_ACTION_ALIAS
1483 /*********************************************************************
1485 * Function : actions_to_html
1487 * Description : Converts a actionsfile entry from numeric form
1488 * ("mask" and "add") to a <br>-seperated HTML string
1489 * in which each action is linked to its chapter in
1493 * 1 : csp = Client state (for config)
1494 * 2 : action = Action spec to be converted
1496 * Returns : A string. Caller must free it.
1497 * NULL on out-of-memory error.
1499 *********************************************************************/
1500 char * actions_to_html(struct client_state *csp,
1501 struct action_spec *action)
1503 unsigned mask = action->mask;
1504 unsigned add = action->add;
1505 char * result = strdup("");
1506 struct list_entry * lst;
1508 /* sanity - prevents "-feature +feature" */
1512 #define DEFINE_ACTION_BOOL(__name, __bit) \
1513 if (!(mask & __bit)) \
1515 string_append(&result, "\n<br>-"); \
1516 string_join(&result, add_help_link(__name, csp->config)); \
1518 else if (add & __bit) \
1520 string_append(&result, "\n<br>+"); \
1521 string_join(&result, add_help_link(__name, csp->config)); \
1524 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1525 if (!(mask & __bit)) \
1527 string_append(&result, "\n<br>-"); \
1528 string_join(&result, add_help_link(__name, csp->config)); \
1530 else if (add & __bit) \
1532 string_append(&result, "\n<br>+"); \
1533 string_join(&result, add_help_link(__name, csp->config)); \
1534 string_append(&result, "{"); \
1535 string_join(&result, html_encode(action->string[__index])); \
1536 string_append(&result, "}"); \
1539 #define DEFINE_ACTION_MULTI(__name, __index) \
1540 if (action->multi_remove_all[__index]) \
1542 string_append(&result, "\n<br>-"); \
1543 string_join(&result, add_help_link(__name, csp->config)); \
1547 lst = action->multi_remove[__index]->first; \
1550 string_append(&result, "\n<br>-"); \
1551 string_join(&result, add_help_link(__name, csp->config)); \
1552 string_append(&result, "{"); \
1553 string_join(&result, html_encode(lst->str)); \
1554 string_append(&result, "}"); \
1558 lst = action->multi_add[__index]->first; \
1561 string_append(&result, "\n<br>+"); \
1562 string_join(&result, add_help_link(__name, csp->config)); \
1563 string_append(&result, "{"); \
1564 string_join(&result, html_encode(lst->str)); \
1565 string_append(&result, "}"); \
1569 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1571 #include "actionlist.h"
1573 #undef DEFINE_ACTION_MULTI
1574 #undef DEFINE_ACTION_STRING
1575 #undef DEFINE_ACTION_BOOL
1576 #undef DEFINE_ACTION_ALIAS
1578 /* trim leading <br> */
1579 if (result && *result)
1582 result = strdup(result + 5);
1590 /*********************************************************************
1592 * Function : current_actions_to_html
1594 * Description : Converts a curren action spec to a <br> seperated HTML
1595 * text in which each action is linked to its chapter in
1599 * 1 : csp = Client state (for config)
1600 * 2 : action = Current action spec to be converted
1602 * Returns : A string. Caller must free it.
1603 * NULL on out-of-memory error.
1605 *********************************************************************/
1606 char *current_action_to_html(struct client_state *csp,
1607 struct current_action_spec *action)
1609 unsigned long flags = action->flags;
1610 char * result = strdup("");
1611 struct list_entry * lst;
1613 #define DEFINE_ACTION_BOOL(__name, __bit) \
1614 if (flags & __bit) \
1616 string_append(&result, "\n<br>+"); \
1617 string_join(&result, add_help_link(__name, csp->config)); \
1621 string_append(&result, "\n<br>-"); \
1622 string_join(&result, add_help_link(__name, csp->config)); \
1625 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1626 if (flags & __bit) \
1628 string_append(&result, "\n<br>+"); \
1629 string_join(&result, add_help_link(__name, csp->config)); \
1630 string_append(&result, "{"); \
1631 string_join(&result, html_encode(action->string[__index])); \
1632 string_append(&result, "}"); \
1636 string_append(&result, "\n<br>-"); \
1637 string_join(&result, add_help_link(__name, csp->config)); \
1640 #define DEFINE_ACTION_MULTI(__name, __index) \
1641 lst = action->multi[__index]->first; \
1644 string_append(&result, "\n<br>-"); \
1645 string_join(&result, add_help_link(__name, csp->config)); \
1651 string_append(&result, "\n<br>+"); \
1652 string_join(&result, add_help_link(__name, csp->config)); \
1653 string_append(&result, "{"); \
1654 string_join(&result, html_encode(lst->str)); \
1655 string_append(&result, "}"); \
1660 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1662 #include "actionlist.h"
1664 #undef DEFINE_ACTION_MULTI
1665 #undef DEFINE_ACTION_STRING
1666 #undef DEFINE_ACTION_BOOL
1667 #undef DEFINE_ACTION_ALIAS