1 const char actions_rcs[] = "$Id: actions.c,v 1.32 2002/05/12 21:36:29 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/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 2002/05/12 21:36:29 jongfoster
37 * Correcting function comments
39 * Revision 1.31 2002/05/06 07:56:50 oes
40 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
42 * Revision 1.30 2002/04/30 11:14:52 oes
43 * Made csp the first parameter in *action_to_html
45 * Revision 1.29 2002/04/26 19:30:54 jongfoster
46 * - current_action_to_html(): Adding help link for the "-" form of
48 * - Some actions had "<br>-", some "<br> -" (note the space).
49 * Standardizing on no space.
50 * - Greatly simplifying some of the code by using string_join()
53 * Revision 1.28 2002/04/26 12:53:15 oes
54 * - CGI AF editor now writes action lines split into
55 * single lines with line continuation
56 * - actions_to_html now embeds each action name in
58 * - current_action_to_text is now called current_action_to_html
59 * and acts like actions_to_html
61 * Revision 1.27 2002/04/24 02:10:31 oes
62 * - Jon's patch for multiple AFs:
63 * - split load_actions_file, add load_one_actions_file
64 * - make csp->actions_list files an array
65 * - remember file id with each action
66 * - Copy_action now frees dest action before copying
68 * Revision 1.26 2002/03/26 22:29:54 swa
69 * we have a new homepage!
71 * Revision 1.25 2002/03/24 13:25:43 swa
72 * name change related issues
74 * Revision 1.24 2002/03/16 23:54:06 jongfoster
75 * Adding graceful termination feature, to help look for memory leaks.
76 * If you enable this (which, by design, has to be done by hand
77 * editing config.h) and then go to http://i.j.b/die, then the program
78 * will exit cleanly after the *next* request. It should free all the
79 * memory that was used.
81 * Revision 1.23 2002/03/07 03:46:16 oes
82 * Fixed compiler warnings
84 * Revision 1.22 2002/01/21 00:27:02 jongfoster
85 * Allowing free_action(NULL).
86 * Moving the functions that #include actionlist.h to the end of the file,
87 * because the Visual C++ 97 debugger gets extremely confused if you try
88 * to debug any code that comes after them in the file.
90 * Revision 1.21 2002/01/17 20:54:44 jongfoster
91 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
93 * Revision 1.20 2001/11/22 21:56:49 jongfoster
94 * Making action_spec->flags into an unsigned long rather than just an
96 * Fixing a bug in the display of -add-header and -wafer
98 * Revision 1.19 2001/11/13 00:14:07 jongfoster
99 * Fixing stupid bug now I've figured out what || means.
100 * (It always returns 0 or 1, not one of it's paramaters.)
102 * Revision 1.18 2001/11/07 00:06:06 steudten
103 * Add line number in error output for lineparsing for
106 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
107 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
108 * threads to call select() simultaneously. So, it's time to do a real, live,
109 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
110 * (native). Both versions will work, but using __OS2__ offers multi-threading.
112 * Revision 1.16 2001/10/23 21:30:30 jongfoster
113 * Adding error-checking to selected functions.
115 * Revision 1.15 2001/10/14 21:58:22 jongfoster
116 * Adding support for the CGI-based editor:
117 * - Exported get_actions()
118 * - Added new function free_alias_list()
119 * - Added support for {{settings}} and {{description}} blocks
120 * in the actions file. They are currently ignored.
121 * - Added restriction to only one {{alias}} block which must appear
122 * first in the file, to simplify the editor's rewriting rules.
123 * - Note that load_actions_file() is no longer used by the CGI-based
124 * editor, but some of the other routines in this file are.
126 * Revision 1.14 2001/09/22 16:36:59 jongfoster
127 * Removing unused parameter fs from read_config_line()
129 * Revision 1.13 2001/09/16 15:47:37 jongfoster
130 * First version of CGI-based edit interface. This is very much a
131 * work-in-progress, and you can't actually use it to edit anything
132 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
133 * to have any effect.
135 * Revision 1.12 2001/09/16 13:21:27 jongfoster
136 * Changes to use new list functions.
138 * Revision 1.11 2001/09/14 00:17:32 jongfoster
139 * Tidying up memory allocation. New function init_action().
141 * Revision 1.10 2001/09/10 10:14:34 oes
142 * Removing unused variable
144 * Revision 1.9 2001/07/30 22:08:36 jongfoster
145 * Tidying up #defines:
146 * - All feature #defines are now of the form FEATURE_xxx
147 * - Permanently turned off WIN_GUI_EDIT
148 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
150 * Revision 1.8 2001/06/29 13:19:52 oes
151 * Removed logentry from cancelled commit
153 * Revision 1.7 2001/06/09 10:55:28 jongfoster
154 * Changing BUFSIZ ==> BUFFER_SIZE
156 * Revision 1.6 2001/06/07 23:04:34 jongfoster
157 * Made get_actions() static.
159 * Revision 1.5 2001/06/03 19:11:48 oes
160 * adapted to new enlist_unique arg format
162 * Revision 1.4 2001/06/01 20:03:42 jongfoster
163 * Better memory management - current_action->strings[] now
164 * contains copies of the strings, not the original.
166 * Revision 1.3 2001/06/01 18:49:17 jongfoster
167 * Replaced "list_share" with "list" - the tiny memory gain was not
168 * worth the extra complexity.
170 * Revision 1.2 2001/05/31 21:40:00 jongfoster
171 * Removing some commented out, obsolete blocks of code.
173 * Revision 1.1 2001/05/31 21:16:46 jongfoster
174 * Moved functions to process the action list into this new file.
177 *********************************************************************/
191 #include "miscutil.h"
195 #include "urlmatch.h"
198 const char actions_h_rcs[] = ACTIONS_H_VERSION;
202 * We need the main list of options.
204 * First, we need a way to tell between boolean, string, and multi-string
205 * options. For string and multistring options, we also need to be
206 * able to tell the difference between a "+" and a "-". (For bools,
207 * the "+"/"-" information is encoded in "add" and "mask"). So we use
208 * an enumerated type (well, the preprocessor equivalent). Here are
211 #define AV_NONE 0 /* +opt -opt */
212 #define AV_ADD_STRING 1 /* +stropt{string} */
213 #define AV_REM_STRING 2 /* -stropt */
214 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
215 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
218 * We need a structure to hold the name, flag changes,
219 * type, and string index.
224 unsigned long mask; /* a bit set to "0" = remove action */
225 unsigned long add; /* a bit set to "1" = add action */
226 int takes_value; /* an AV_... constant */
227 int index; /* index into strings[] or multi[] */
231 * And with those building blocks in place, here's the array.
233 static const struct action_name action_names[] =
236 * Well actually there's no data here - it's in actionlist.h
237 * This keeps it together to make it easy to change.
239 * Here's the macros used to format it:
241 #define DEFINE_ACTION_MULTI(name,index) \
242 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
243 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
244 #define DEFINE_ACTION_STRING(name,flag,index) \
245 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
246 { "-" name, ~flag, 0, AV_REM_STRING, index },
247 #define DEFINE_ACTION_BOOL(name,flag) \
248 { "+" name, ACTION_MASK_ALL, flag }, \
249 { "-" name, ~flag, 0 },
250 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
252 #include "actionlist.h"
254 #undef DEFINE_ACTION_MULTI
255 #undef DEFINE_ACTION_STRING
256 #undef DEFINE_ACTION_BOOL
257 #undef DEFINE_ACTION_ALIAS
259 { NULL, 0, 0 } /* End marker */
263 static int load_one_actions_file(struct client_state *csp, int fileid);
266 /*********************************************************************
268 * Function : merge_actions
270 * Description : Merge two actions together.
271 * Similar to "dest += src".
274 * 1 : dest = Actions to modify.
275 * 2 : src = Action to add.
277 * Returns : JB_ERR_OK or JB_ERR_MEMORY
279 *********************************************************************/
280 jb_err merge_actions (struct action_spec *dest,
281 const struct action_spec *src)
286 dest->mask &= src->mask;
287 dest->add &= src->mask;
288 dest->add |= src->add;
290 for (i = 0; i < ACTION_STRING_COUNT; i++)
292 char * str = src->string[i];
295 freez(dest->string[i]);
296 dest->string[i] = strdup(str);
297 if (NULL == dest->string[i])
299 return JB_ERR_MEMORY;
304 for (i = 0; i < ACTION_MULTI_COUNT; i++)
306 if (src->multi_remove_all[i])
308 /* Remove everything from dest */
309 list_remove_all(dest->multi_remove[i]);
310 dest->multi_remove_all[i] = 1;
312 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
314 else if (dest->multi_remove_all[i])
317 * dest already removes everything, so we only need to worry
320 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
321 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
325 /* No "remove all"s to worry about. */
326 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
327 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
328 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
341 /*********************************************************************
343 * Function : copy_action
345 * Description : Copy an action_specs.
346 * Similar to "dest = src".
349 * 1 : dest = Destination of copy.
350 * 2 : src = Source for copy.
354 *********************************************************************/
355 jb_err copy_action (struct action_spec *dest,
356 const struct action_spec *src)
359 jb_err err = JB_ERR_OK;
362 memset(dest, '\0', sizeof(*dest));
364 dest->mask = src->mask;
365 dest->add = src->add;
367 for (i = 0; i < ACTION_STRING_COUNT; i++)
369 char * str = src->string[i];
375 return JB_ERR_MEMORY;
377 dest->string[i] = str;
381 for (i = 0; i < ACTION_MULTI_COUNT; i++)
383 dest->multi_remove_all[i] = src->multi_remove_all[i];
384 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
389 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
399 /*********************************************************************
401 * Function : free_action
403 * Description : Destroy an action_spec. Frees memory used by it,
404 * except for the memory used by the struct action_spec
408 * 1 : src = Source to free.
412 *********************************************************************/
413 void free_action (struct action_spec *src)
422 for (i = 0; i < ACTION_STRING_COUNT; i++)
424 freez(src->string[i]);
427 for (i = 0; i < ACTION_MULTI_COUNT; i++)
429 destroy_list(src->multi_remove[i]);
430 destroy_list(src->multi_add[i]);
433 memset(src, '\0', sizeof(*src));
437 /*********************************************************************
439 * Function : get_action_token
441 * Description : Parses a line for the first action.
442 * Modifies it's input array, doesn't allocate memory.
444 * *line=" +abc{def} -ghi "
451 * 1 : line = [in] The line containing the action.
452 * [out] Start of next action on line, or
453 * NULL if we reached the end of line before
454 * we found an action.
455 * 2 : name = [out] Start of action name, null
456 * terminated. NULL on EOL
457 * 3 : value = [out] Start of action value, null
458 * terminated. NULL if none or EOL.
460 * Returns : JB_ERR_OK => Ok
461 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
463 *********************************************************************/
464 jb_err get_action_token(char **line, char **name, char **value)
469 /* set default returns */
474 /* Eat any leading whitespace */
475 while ((*str == ' ') || (*str == '\t'))
487 /* null name, just value is prohibited */
494 while (((ch = *str) != '\0') &&
495 (ch != ' ') && (ch != '\t') && (ch != '{'))
499 /* error, '}' without '{' */
511 /* EOL - be careful not to run off buffer */
516 /* More to parse next time. */
525 str = strchr(str, '}');
543 /*********************************************************************
545 * Function : get_actions
547 * Description : Parses a list of actions.
550 * 1 : line = The string containing the actions.
551 * Will be written to by this function.
552 * 2 : alias_list = Custom alias list, or NULL for none.
553 * 3 : cur_action = Where to store the action. Caller
556 * Returns : JB_ERR_OK => Ok
557 * JB_ERR_PARSE => Parse error (line was trashed anyway)
558 * nonzero => Out of memory (line was trashed anyway)
560 *********************************************************************/
561 jb_err get_actions(char *line,
562 struct action_alias * alias_list,
563 struct action_spec *cur_action)
566 init_action(cur_action);
567 cur_action->mask = ACTION_MASK_ALL;
571 char * option = NULL;
574 err = get_action_token(&line, &option, &value);
582 /* handle option in 'option' */
584 /* Check for standard action name */
585 const struct action_name * action = action_names;
587 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
591 if (action->name != NULL)
594 cur_action->mask &= action->mask;
595 cur_action->add &= action->mask;
596 cur_action->add |= action->add;
598 switch (action->takes_value)
601 /* ignore any option. */
605 /* add single string. */
607 if ((value == NULL) || (*value == '\0'))
611 /* FIXME: should validate option string here */
612 freez (cur_action->string[action->index]);
613 cur_action->string[action->index] = strdup(value);
614 if (NULL == cur_action->string[action->index])
616 return JB_ERR_MEMORY;
622 /* remove single string. */
624 freez (cur_action->string[action->index]);
629 /* append multi string. */
631 struct list * remove_p = cur_action->multi_remove[action->index];
632 struct list * add_p = cur_action->multi_add[action->index];
634 if ((value == NULL) || (*value == '\0'))
639 list_remove_item(remove_p, value);
640 err = enlist_unique(add_p, value, 0);
649 /* remove multi string. */
651 struct list * remove_p = cur_action->multi_remove[action->index];
652 struct list * add_p = cur_action->multi_add[action->index];
654 if ( (value == NULL) || (*value == '\0')
655 || ((*value == '*') && (value[1] == '\0')) )
658 * no option, or option == "*".
662 list_remove_all(remove_p);
663 list_remove_all(add_p);
664 cur_action->multi_remove_all[action->index] = 1;
668 /* Valid option - remove only 1 option */
670 if ( !cur_action->multi_remove_all[action->index] )
672 /* there isn't a catch-all in the remove list already */
673 err = enlist_unique(remove_p, value, 0);
679 list_remove_item(add_p, value);
684 /* Shouldn't get here unless there's memory corruption. */
691 /* try user aliases. */
692 const struct action_alias * alias = alias_list;
694 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
701 merge_actions(cur_action, alias->action);
705 /* Bad action name */
716 /*********************************************************************
718 * Function : init_current_action
720 * Description : Zero out an action.
723 * 1 : dest = An uninitialized current_action_spec.
727 *********************************************************************/
728 void init_current_action (struct current_action_spec *dest)
730 memset(dest, '\0', sizeof(*dest));
732 dest->flags = ACTION_MOST_COMPATIBLE;
736 /*********************************************************************
738 * Function : init_action
740 * Description : Zero out an action.
743 * 1 : dest = An uninitialized action_spec.
747 *********************************************************************/
748 void init_action (struct action_spec *dest)
750 memset(dest, '\0', sizeof(*dest));
754 /*********************************************************************
756 * Function : merge_current_action
758 * Description : Merge two actions together.
759 * Similar to "dest += src".
760 * Differences between this and merge_actions()
761 * is that this one doesn't allocate memory for
762 * strings (so "src" better be in memory for at least
763 * as long as "dest" is, and you'd better free
764 * "dest" using "free_current_action").
765 * Also, there is no mask or remove lists in dest.
766 * (If we're applying it to a URL, we don't need them)
769 * 1 : dest = Current actions, to modify.
770 * 2 : src = Action to add.
772 * Returns 0 : no error
773 * !=0 : error, probably JB_ERR_MEMORY.
775 *********************************************************************/
776 jb_err merge_current_action (struct current_action_spec *dest,
777 const struct action_spec *src)
780 jb_err err = JB_ERR_OK;
782 dest->flags &= src->mask;
783 dest->flags |= src->add;
785 for (i = 0; i < ACTION_STRING_COUNT; i++)
787 char * str = src->string[i];
793 return JB_ERR_MEMORY;
795 freez(dest->string[i]);
796 dest->string[i] = str;
800 for (i = 0; i < ACTION_MULTI_COUNT; i++)
802 if (src->multi_remove_all[i])
804 /* Remove everything from dest, then add src->multi_add */
805 err = list_duplicate(dest->multi[i], src->multi_add[i]);
813 list_remove_list(dest->multi[i], src->multi_remove[i]);
814 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
825 /*********************************************************************
827 * Function : free_current_action
829 * Description : Free memory used by a current_action_spec.
830 * Does not free the current_action_spec itself.
833 * 1 : src = Source to free.
837 *********************************************************************/
838 void free_current_action (struct current_action_spec *src)
842 for (i = 0; i < ACTION_STRING_COUNT; i++)
844 freez(src->string[i]);
847 for (i = 0; i < ACTION_MULTI_COUNT; i++)
849 destroy_list(src->multi[i]);
852 memset(src, '\0', sizeof(*src));
856 static struct file_list *current_actions_file[MAX_ACTION_FILES] = {
857 NULL, NULL, NULL, NULL, NULL,
858 NULL, NULL, NULL, NULL, NULL
862 #ifdef FEATURE_GRACEFUL_TERMINATION
863 /*********************************************************************
865 * Function : unload_current_actions_file
867 * Description : Unloads current actions file - reset to state at
868 * beginning of program.
874 *********************************************************************/
875 void unload_current_actions_file(void)
877 if (current_actions_file)
879 current_actions_file->unloader = unload_actions_file;
880 current_actions_file = NULL;
883 #endif /* FEATURE_GRACEFUL_TERMINATION */
886 /*********************************************************************
888 * Function : unload_actions_file
890 * Description : Unloads an actions module.
893 * 1 : file_data = the data structure associated with the
898 *********************************************************************/
899 void unload_actions_file(void *file_data)
901 struct url_actions * next;
902 struct url_actions * cur = (struct url_actions *)file_data;
906 free_url_spec(cur->url);
907 free_action(cur->action);
915 /*********************************************************************
917 * Function : free_alias_list
919 * Description : Free memory used by a list of aliases.
922 * 1 : alias_list = Linked list to free.
926 *********************************************************************/
927 void free_alias_list(struct action_alias *alias_list)
929 while (alias_list != NULL)
931 struct action_alias * next = alias_list->next;
932 alias_list->next = NULL;
933 freez(alias_list->name);
934 free_action(alias_list->action);
941 /*********************************************************************
943 * Function : load_actions_file
945 * Description : Read and parse all the action files and add to files
949 * 1 : csp = Current client state (buffers, headers, etc...)
951 * Returns : 0 => Ok, everything else is an error.
953 *********************************************************************/
954 int load_actions_file(struct client_state *csp)
959 for (i = 0; i < MAX_ACTION_FILES; i++)
961 if (csp->config->actions_file[i])
963 result = load_one_actions_file(csp, i);
969 else if (current_actions_file[i])
971 current_actions_file[i]->unloader = unload_actions_file;
972 current_actions_file[i] = NULL;
979 /*********************************************************************
981 * Function : load_one_actions_file
983 * Description : Read and parse a action file and add to files
987 * 1 : csp = Current client state (buffers, headers, etc...)
988 * 2 : fileid = File index to load.
990 * Returns : 0 => Ok, everything else is an error.
992 *********************************************************************/
993 static int load_one_actions_file(struct client_state *csp, int fileid)
998 * Note: Keep these in the order they occur in the file, they are
999 * sometimes tested with <=
1001 #define MODE_START_OF_FILE 1
1002 #define MODE_SETTINGS 2
1003 #define MODE_DESCRIPTION 3
1004 #define MODE_ALIAS 4
1005 #define MODE_ACTIONS 5
1007 int mode = MODE_START_OF_FILE;
1010 struct url_actions *last_perm;
1011 struct url_actions *perm;
1012 char buf[BUFFER_SIZE];
1013 struct file_list *fs;
1014 struct action_spec * cur_action = NULL;
1015 int cur_action_used = 0;
1016 struct action_alias * alias_list = NULL;
1017 unsigned long linenum = 0;
1019 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1021 /* No need to load */
1022 csp->actions_list[fileid] = current_actions_file[fileid];
1027 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1028 csp->config->actions_file[fileid]);
1029 return 1; /* never get here */
1032 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1033 if (last_perm == NULL)
1035 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1036 csp->config->actions_file[fileid]);
1037 return 1; /* never get here */
1040 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1042 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1043 csp->config->actions_file[fileid]);
1044 return 1; /* never get here */
1047 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1051 /* It's a header block */
1054 /* It's {{settings}} or {{alias}} */
1055 int len = strlen(buf);
1056 char * start = buf + 2;
1057 char * end = buf + len - 1;
1058 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1062 log_error(LOG_LEVEL_FATAL,
1063 "can't load actions file '%s': invalid line (%lu): %s",
1064 csp->config->actions_file[fileid], linenum, buf);
1065 return 1; /* never get here */
1068 /* Trim leading and trailing whitespace. */
1076 log_error(LOG_LEVEL_FATAL,
1077 "can't load actions file '%s': invalid line (%lu): {{ }}",
1078 csp->config->actions_file[fileid], linenum);
1079 return 1; /* never get here */
1083 * An actionsfile can optionally contain the following blocks.
1084 * They *MUST* be in this order, to simplify processing:
1090 * ...free text, format TBD, but no line may start with a '{'...
1095 * The actual actions must be *after* these special blocks.
1096 * None of these special blocks may be repeated.
1099 if (0 == strcmpic(start, "settings"))
1101 /* it's a {{settings}} block */
1102 if (mode >= MODE_SETTINGS)
1104 /* {{settings}} must be first thing in file and must only
1108 log_error(LOG_LEVEL_FATAL,
1109 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1110 csp->config->actions_file[fileid], linenum);
1112 mode = MODE_SETTINGS;
1114 else if (0 == strcmpic(start, "description"))
1116 /* it's a {{description}} block */
1117 if (mode >= MODE_DESCRIPTION)
1119 /* {{description}} is a singleton and only {{settings}} may proceed it
1122 log_error(LOG_LEVEL_FATAL,
1123 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1124 csp->config->actions_file[fileid], linenum);
1126 mode = MODE_DESCRIPTION;
1128 else if (0 == strcmpic(start, "alias"))
1130 /* it's an {{alias}} block */
1131 if (mode >= MODE_ALIAS)
1133 /* {{alias}} must be first thing in file, possibly after
1134 * {{settings}} and {{description}}
1136 * {{alias}} must only appear once.
1138 * Note that these are new restrictions introduced in
1139 * v2.9.10 in order to make actionsfile editing simpler.
1140 * (Otherwise, reordering actionsfile entries without
1141 * completely rewriting the file becomes non-trivial)
1144 log_error(LOG_LEVEL_FATAL,
1145 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1146 csp->config->actions_file[fileid], linenum);
1152 /* invalid {{something}} block */
1154 log_error(LOG_LEVEL_FATAL,
1155 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1156 csp->config->actions_file[fileid], linenum, start);
1157 return 1; /* never get here */
1162 /* It's an actions block */
1164 char actions_buf[BUFFER_SIZE];
1168 mode = MODE_ACTIONS;
1170 /* free old action */
1173 if (!cur_action_used)
1175 free_action(cur_action);
1180 cur_action_used = 0;
1181 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1182 if (cur_action == NULL)
1185 log_error(LOG_LEVEL_FATAL,
1186 "can't load actions file '%s': out of memory",
1187 csp->config->actions_file[fileid]);
1188 return 1; /* never get here */
1190 init_action(cur_action);
1193 strcpy(actions_buf, buf + 1);
1195 /* check we have a trailing } and then trim it */
1196 end = actions_buf + strlen(actions_buf) - 1;
1201 log_error(LOG_LEVEL_FATAL,
1202 "can't load actions file '%s': invalid line (%lu): %s",
1203 csp->config->actions_file[fileid], linenum, buf);
1204 return 1; /* never get here */
1208 /* trim any whitespace immediately inside {} */
1211 if (get_actions(actions_buf, alias_list, cur_action))
1215 log_error(LOG_LEVEL_FATAL,
1216 "can't load actions file '%s': invalid line (%lu): %s",
1217 csp->config->actions_file[fileid], linenum, buf);
1218 return 1; /* never get here */
1222 else if (mode == MODE_SETTINGS)
1225 * Part of the {{settings}} block.
1226 * Ignore for now, but we may want to read & check permissions
1227 * when we go multi-user.
1230 else if (mode == MODE_DESCRIPTION)
1233 * Part of the {{description}} block.
1237 else if (mode == MODE_ALIAS)
1242 char actions_buf[BUFFER_SIZE];
1243 struct action_alias * new_alias;
1245 char * start = strchr(buf, '=');
1248 if ((start == NULL) || (start == buf))
1250 log_error(LOG_LEVEL_FATAL,
1251 "can't load actions file '%s': invalid alias line (%lu): %s",
1252 csp->config->actions_file[fileid], linenum, buf);
1253 return 1; /* never get here */
1256 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1259 log_error(LOG_LEVEL_FATAL,
1260 "can't load actions file '%s': out of memory!",
1261 csp->config->actions_file[fileid]);
1262 return 1; /* never get here */
1265 /* Eat any the whitespace before the '=' */
1267 while ((*end == ' ') || (*end == '\t'))
1270 * we already know we must have at least 1 non-ws char
1271 * at start of buf - no need to check
1277 /* Eat any the whitespace after the '=' */
1279 while ((*start == ' ') || (*start == '\t'))
1285 log_error(LOG_LEVEL_FATAL,
1286 "can't load actions file '%s': invalid alias line (%lu): %s",
1287 csp->config->actions_file[fileid], linenum, buf);
1288 return 1; /* never get here */
1291 new_alias->name = strdup(buf);
1293 strcpy(actions_buf, start);
1295 if (get_actions(actions_buf, alias_list, new_alias->action))
1299 log_error(LOG_LEVEL_FATAL,
1300 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1301 csp->config->actions_file[fileid], linenum, buf, start);
1302 return 1; /* never get here */
1306 new_alias->next = alias_list;
1307 alias_list = new_alias;
1309 else if (mode == MODE_ACTIONS)
1311 /* it's a URL pattern */
1313 /* allocate a new node */
1314 if ((perm = zalloc(sizeof(*perm))) == NULL)
1317 log_error(LOG_LEVEL_FATAL,
1318 "can't load actions file '%s': out of memory!",
1319 csp->config->actions_file[fileid]);
1320 return 1; /* never get here */
1324 copy_action (perm->action, cur_action);
1326 /* Save the URL pattern */
1327 if (create_url_spec(perm->url, buf))
1330 log_error(LOG_LEVEL_FATAL,
1331 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1332 csp->config->actions_file[fileid], linenum, buf);
1333 return 1; /* never get here */
1336 /* add it to the list */
1337 last_perm->next = perm;
1340 else if (mode == MODE_START_OF_FILE)
1342 /* oops - please have a {} line as 1st line in file. */
1344 log_error(LOG_LEVEL_FATAL,
1345 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1346 csp->config->actions_file[fileid], linenum, buf);
1347 return 1; /* never get here */
1351 /* How did we get here? This is impossible! */
1353 log_error(LOG_LEVEL_FATAL,
1354 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1355 csp->config->actions_file[fileid], mode);
1356 return 1; /* never get here */
1362 free_action(cur_action);
1364 free_alias_list(alias_list);
1366 /* the old one is now obsolete */
1367 if (current_actions_file[fileid])
1369 current_actions_file[fileid]->unloader = unload_actions_file;
1372 fs->next = files->next;
1374 current_actions_file[fileid] = fs;
1376 csp->actions_list[fileid] = fs;
1383 /*********************************************************************
1385 * Function : actions_to_text
1387 * Description : Converts a actionsfile entry from the internal
1388 * structurt into a text line. The output is split
1389 * into one line for each action with line continuation.
1392 * 1 : action = The action to format.
1394 * Returns : A string. Caller must free it.
1395 * NULL on out-of-memory error.
1397 *********************************************************************/
1398 char * actions_to_text(struct action_spec *action)
1400 unsigned mask = action->mask;
1401 unsigned add = action->add;
1402 char * result = strdup("");
1403 struct list_entry * lst;
1405 /* sanity - prevents "-feature +feature" */
1409 #define DEFINE_ACTION_BOOL(__name, __bit) \
1410 if (!(mask & __bit)) \
1412 string_append(&result, " -" __name " \\\n"); \
1414 else if (add & __bit) \
1416 string_append(&result, " +" __name " \\\n"); \
1419 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1420 if (!(mask & __bit)) \
1422 string_append(&result, " -" __name " \\\n"); \
1424 else if (add & __bit) \
1426 string_append(&result, " +" __name "{"); \
1427 string_append(&result, action->string[__index]); \
1428 string_append(&result, "} \\\n"); \
1431 #define DEFINE_ACTION_MULTI(__name, __index) \
1432 if (action->multi_remove_all[__index]) \
1434 string_append(&result, " -" __name " \\\n"); \
1438 lst = action->multi_remove[__index]->first; \
1441 string_append(&result, " -" __name "{"); \
1442 string_append(&result, lst->str); \
1443 string_append(&result, "} \\\n"); \
1447 lst = action->multi_add[__index]->first; \
1450 string_append(&result, " +" __name "{"); \
1451 string_append(&result, lst->str); \
1452 string_append(&result, "} \\\n"); \
1456 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1458 #include "actionlist.h"
1460 #undef DEFINE_ACTION_MULTI
1461 #undef DEFINE_ACTION_STRING
1462 #undef DEFINE_ACTION_BOOL
1463 #undef DEFINE_ACTION_ALIAS
1469 /*********************************************************************
1471 * Function : actions_to_html
1473 * Description : Converts a actionsfile entry from numeric form
1474 * ("mask" and "add") to a <br>-seperated HTML string
1475 * in which each action is linked to its chapter in
1479 * 1 : csp = Client state (for config)
1480 * 2 : action = Action spec to be converted
1482 * Returns : A string. Caller must free it.
1483 * NULL on out-of-memory error.
1485 *********************************************************************/
1486 char * actions_to_html(struct client_state *csp,
1487 struct action_spec *action)
1489 unsigned mask = action->mask;
1490 unsigned add = action->add;
1491 char * result = strdup("");
1492 struct list_entry * lst;
1494 /* sanity - prevents "-feature +feature" */
1498 #define DEFINE_ACTION_BOOL(__name, __bit) \
1499 if (!(mask & __bit)) \
1501 string_append(&result, "\n<br>-"); \
1502 string_join(&result, add_help_link(__name, csp->config)); \
1504 else if (add & __bit) \
1506 string_append(&result, "\n<br>+"); \
1507 string_join(&result, add_help_link(__name, csp->config)); \
1510 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1511 if (!(mask & __bit)) \
1513 string_append(&result, "\n<br>-"); \
1514 string_join(&result, add_help_link(__name, csp->config)); \
1516 else if (add & __bit) \
1518 string_append(&result, "\n<br>+"); \
1519 string_join(&result, add_help_link(__name, csp->config)); \
1520 string_append(&result, "{"); \
1521 string_join(&result, html_encode(action->string[__index])); \
1522 string_append(&result, "}"); \
1525 #define DEFINE_ACTION_MULTI(__name, __index) \
1526 if (action->multi_remove_all[__index]) \
1528 string_append(&result, "\n<br>-"); \
1529 string_join(&result, add_help_link(__name, csp->config)); \
1533 lst = action->multi_remove[__index]->first; \
1536 string_append(&result, "\n<br>-"); \
1537 string_join(&result, add_help_link(__name, csp->config)); \
1538 string_append(&result, "{"); \
1539 string_join(&result, html_encode(lst->str)); \
1540 string_append(&result, "}"); \
1544 lst = action->multi_add[__index]->first; \
1547 string_append(&result, "\n<br>+"); \
1548 string_join(&result, add_help_link(__name, csp->config)); \
1549 string_append(&result, "{"); \
1550 string_join(&result, html_encode(lst->str)); \
1551 string_append(&result, "}"); \
1555 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1557 #include "actionlist.h"
1559 #undef DEFINE_ACTION_MULTI
1560 #undef DEFINE_ACTION_STRING
1561 #undef DEFINE_ACTION_BOOL
1562 #undef DEFINE_ACTION_ALIAS
1564 /* trim leading <br> */
1565 if (result && *result)
1568 result = strdup(result + 5);
1576 /*********************************************************************
1578 * Function : current_actions_to_html
1580 * Description : Converts a curren action spec to a <br> seperated HTML
1581 * text in which each action is linked to its chapter in
1585 * 1 : csp = Client state (for config)
1586 * 2 : action = Current action spec to be converted
1588 * Returns : A string. Caller must free it.
1589 * NULL on out-of-memory error.
1591 *********************************************************************/
1592 char *current_action_to_html(struct client_state *csp,
1593 struct current_action_spec *action)
1595 unsigned long flags = action->flags;
1596 char * result = strdup("");
1597 struct list_entry * lst;
1599 #define DEFINE_ACTION_BOOL(__name, __bit) \
1600 if (flags & __bit) \
1602 string_append(&result, "\n<br>+"); \
1603 string_join(&result, add_help_link(__name, csp->config)); \
1607 string_append(&result, "\n<br>-"); \
1608 string_join(&result, add_help_link(__name, csp->config)); \
1611 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1612 if (flags & __bit) \
1614 string_append(&result, "\n<br>+"); \
1615 string_join(&result, add_help_link(__name, csp->config)); \
1616 string_append(&result, "{"); \
1617 string_join(&result, html_encode(action->string[__index])); \
1618 string_append(&result, "}"); \
1622 string_append(&result, "\n<br>-"); \
1623 string_join(&result, add_help_link(__name, csp->config)); \
1626 #define DEFINE_ACTION_MULTI(__name, __index) \
1627 lst = action->multi[__index]->first; \
1630 string_append(&result, "\n<br>-"); \
1631 string_join(&result, add_help_link(__name, csp->config)); \
1637 string_append(&result, "\n<br>+"); \
1638 string_join(&result, add_help_link(__name, csp->config)); \
1639 string_append(&result, "{"); \
1640 string_join(&result, html_encode(lst->str)); \
1641 string_append(&result, "}"); \
1646 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1648 #include "actionlist.h"
1650 #undef DEFINE_ACTION_MULTI
1651 #undef DEFINE_ACTION_STRING
1652 #undef DEFINE_ACTION_BOOL
1653 #undef DEFINE_ACTION_ALIAS