1 const char actions_rcs[] = "$Id: actions.c,v 1.16 2001/10/23 21:30:30 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 * IJBSWA team. http://ijbswa.sourceforge.net
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.16 2001/10/23 21:30:30 jongfoster
37 * Adding error-checking to selected functions.
39 * Revision 1.15 2001/10/14 21:58:22 jongfoster
40 * Adding support for the CGI-based editor:
41 * - Exported get_actions()
42 * - Added new function free_alias_list()
43 * - Added support for {{settings}} and {{description}} blocks
44 * in the actions file. They are currently ignored.
45 * - Added restriction to only one {{alias}} block which must appear
46 * first in the file, to simplify the editor's rewriting rules.
47 * - Note that load_actions_file() is no longer used by the CGI-based
48 * editor, but some of the other routines in this file are.
50 * Revision 1.14 2001/09/22 16:36:59 jongfoster
51 * Removing unused parameter fs from read_config_line()
53 * Revision 1.13 2001/09/16 15:47:37 jongfoster
54 * First version of CGI-based edit interface. This is very much a
55 * work-in-progress, and you can't actually use it to edit anything
56 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
59 * Revision 1.12 2001/09/16 13:21:27 jongfoster
60 * Changes to use new list functions.
62 * Revision 1.11 2001/09/14 00:17:32 jongfoster
63 * Tidying up memory allocation. New function init_action().
65 * Revision 1.10 2001/09/10 10:14:34 oes
66 * Removing unused variable
68 * Revision 1.9 2001/07/30 22:08:36 jongfoster
69 * Tidying up #defines:
70 * - All feature #defines are now of the form FEATURE_xxx
71 * - Permanently turned off WIN_GUI_EDIT
72 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
74 * Revision 1.8 2001/06/29 13:19:52 oes
75 * Removed logentry from cancelled commit
77 * Revision 1.7 2001/06/09 10:55:28 jongfoster
78 * Changing BUFSIZ ==> BUFFER_SIZE
80 * Revision 1.6 2001/06/07 23:04:34 jongfoster
81 * Made get_actions() static.
83 * Revision 1.5 2001/06/03 19:11:48 oes
84 * adapted to new enlist_unique arg format
86 * Revision 1.4 2001/06/01 20:03:42 jongfoster
87 * Better memory management - current_action->strings[] now
88 * contains copies of the strings, not the original.
90 * Revision 1.3 2001/06/01 18:49:17 jongfoster
91 * Replaced "list_share" with "list" - the tiny memory gain was not
92 * worth the extra complexity.
94 * Revision 1.2 2001/05/31 21:40:00 jongfoster
95 * Removing some commented out, obsolete blocks of code.
97 * Revision 1.1 2001/05/31 21:16:46 jongfoster
98 * Moved functions to process the action list into this new file.
101 *********************************************************************/
114 #include "miscutil.h"
117 #ifdef FEATURE_CGI_EDIT_ACTIONS
119 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
121 const char actions_h_rcs[] = ACTIONS_H_VERSION;
125 * We need the main list of options.
127 * First, we need a way to tell between boolean, string, and multi-string
128 * options. For string and multistring options, we also need to be
129 * able to tell the difference between a "+" and a "-". (For bools,
130 * the "+"/"-" information is encoded in "add" and "mask"). So we use
131 * an enumerated type (well, the preprocessor equivalent). Here are
134 #define AV_NONE 0 /* +opt -opt */
135 #define AV_ADD_STRING 1 /* +stropt{string} */
136 #define AV_REM_STRING 2 /* -stropt */
137 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
138 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
141 * We need a structure to hold the name, flag changes,
142 * type, and string index.
147 unsigned mask; /* a bit set to "0" = remove action */
148 unsigned add; /* a bit set to "1" = add action */
149 int takes_value; /* an AV_... constant */
150 int index; /* index into strings[] or multi[] */
154 * And with those building blocks in place, here's the array.
156 static const struct action_name action_names[] =
159 * Well actually there's no data here - it's in actionlist.h
160 * This keeps it together to make it easy to change.
162 * Here's the macros used to format it:
164 #define DEFINE_ACTION_MULTI(name,index) \
165 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
166 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
167 #define DEFINE_ACTION_STRING(name,flag,index) \
168 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
169 { "-" name, ~flag, 0, AV_REM_STRING, index },
170 #define DEFINE_ACTION_BOOL(name,flag) \
171 { "+" name, ACTION_MASK_ALL, flag }, \
172 { "-" name, ~flag, 0 },
173 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
175 #include "actionlist.h"
177 #undef DEFINE_ACTION_MULTI
178 #undef DEFINE_ACTION_STRING
179 #undef DEFINE_ACTION_BOOL
180 #undef DEFINE_ACTION_ALIAS
182 { NULL, 0, 0 } /* End marker */
186 /*********************************************************************
188 * Function : merge_actions
190 * Description : Merge two actions together.
191 * Similar to "cur_action += new_action".
194 * 1 : cur_action = Current actions, to modify.
195 * 2 : new_action = Action to add.
197 * Returns : JB_ERR_OK or JB_ERR_MEMORY
199 *********************************************************************/
200 jb_err merge_actions (struct action_spec *dest,
201 const struct action_spec *src)
206 dest->mask &= src->mask;
207 dest->add &= src->mask;
208 dest->add |= src->add;
210 for (i = 0; i < ACTION_STRING_COUNT; i++)
212 char * str = src->string[i];
215 freez(dest->string[i]);
216 dest->string[i] = strdup(str);
217 if (NULL == dest->string[i])
219 return JB_ERR_MEMORY;
224 for (i = 0; i < ACTION_MULTI_COUNT; i++)
226 if (src->multi_remove_all[i])
228 /* Remove everything from dest */
229 list_remove_all(dest->multi_remove[i]);
230 dest->multi_remove_all[i] = 1;
232 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
234 else if (dest->multi_remove_all[i])
237 * dest already removes everything, so we only need to worry
240 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
241 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
245 /* No "remove all"s to worry about. */
246 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
247 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
248 err = err || list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
261 /*********************************************************************
263 * Function : copy_action
265 * Description : Copy an action_specs.
266 * Similar to "cur_action = new_action".
267 * Note that dest better not contain valid data
268 * - it's overwritten, not freed.
271 * 1 : dest = Destination of copy.
272 * 2 : src = Source for copy.
276 *********************************************************************/
277 jb_err copy_action (struct action_spec *dest,
278 const struct action_spec *src)
281 jb_err err = JB_ERR_OK;
283 memset(dest, '\0', sizeof(*dest));
285 dest->mask = src->mask;
286 dest->add = src->add;
288 for (i = 0; i < ACTION_STRING_COUNT; i++)
290 char * str = src->string[i];
296 return JB_ERR_MEMORY;
298 dest->string[i] = str;
302 for (i = 0; i < ACTION_MULTI_COUNT; i++)
304 dest->multi_remove_all[i] = src->multi_remove_all[i];
305 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
306 err = err || list_duplicate(dest->multi_add[i], src->multi_add[i]);
316 /*********************************************************************
318 * Function : free_action
320 * Description : Destroy an action_spec. Frees memory used by it,
321 * except for the memory used by the struct action_spec
325 * 1 : src = Source to free.
329 *********************************************************************/
330 void free_action (struct action_spec *src)
334 for (i = 0; i < ACTION_STRING_COUNT; i++)
336 freez(src->string[i]);
339 for (i = 0; i < ACTION_MULTI_COUNT; i++)
341 destroy_list(src->multi_remove[i]);
342 destroy_list(src->multi_add[i]);
345 memset(src, '\0', sizeof(*src));
349 /*********************************************************************
351 * Function : get_action_token
353 * Description : Parses a line for the first action.
354 * Modifies it's input array, doesn't allocate memory.
356 * *line=" +abc{def} -ghi "
363 * 1 : line = [in] The line containing the action.
364 * [out] Start of next action on line, or
365 * NULL if we reached the end of line before
366 * we found an action.
367 * 2 : name = [out] Start of action name, null
368 * terminated. NULL on EOL
369 * 3 : value = [out] Start of action value, null
370 * terminated. NULL if none or EOL.
372 * Returns : JB_ERR_OK => Ok
373 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
375 *********************************************************************/
376 jb_err get_action_token(char **line, char **name, char **value)
381 /* set default returns */
386 /* Eat any leading whitespace */
387 while ((*str == ' ') || (*str == '\t'))
399 /* null name, just value is prohibited */
406 while (((ch = *str) != '\0') &&
407 (ch != ' ') && (ch != '\t') && (ch != '{'))
411 /* error, '}' without '{' */
423 /* EOL - be careful not to run off buffer */
428 /* More to parse next time. */
437 str = strchr(str, '}');
455 /*********************************************************************
457 * Function : get_actions
459 * Description : Parses a list of actions.
462 * 1 : line = The string containing the actions.
463 * Will be written to by this function.
464 * 2 : aliases = Custom alias list, or NULL for none.
465 * 3 : cur_action = Where to store the action. Caller
468 * Returns : JB_ERR_OK => Ok
469 * JB_ERR_PARSE => Parse error (line was trashed anyway)
470 * nonzero => Out of memory (line was trashed anyway)
472 *********************************************************************/
473 jb_err get_actions(char *line,
474 struct action_alias * alias_list,
475 struct action_spec *cur_action)
478 init_action(cur_action);
479 cur_action->mask = ACTION_MASK_ALL;
483 char * option = NULL;
486 err = get_action_token(&line, &option, &value);
494 /* handle option in 'option' */
496 /* Check for standard action name */
497 const struct action_name * action = action_names;
499 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
503 if (action->name != NULL)
506 cur_action->mask &= action->mask;
507 cur_action->add &= action->mask;
508 cur_action->add |= action->add;
510 switch (action->takes_value)
513 /* ignore any option. */
517 /* add single string. */
519 if ((value == NULL) || (*value == '\0'))
523 /* FIXME: should validate option string here */
524 freez (cur_action->string[action->index]);
525 cur_action->string[action->index] = strdup(value);
526 if (NULL == cur_action->string[action->index])
528 return JB_ERR_MEMORY;
534 /* remove single string. */
536 freez (cur_action->string[action->index]);
541 /* append multi string. */
543 struct list * remove = cur_action->multi_remove[action->index];
544 struct list * add = cur_action->multi_add[action->index];
546 if ((value == NULL) || (*value == '\0'))
551 list_remove_item(remove, value);
552 err = enlist_unique(add, value, 0);
561 /* remove multi string. */
563 struct list * remove = cur_action->multi_remove[action->index];
564 struct list * add = cur_action->multi_add[action->index];
566 if ( (value == NULL) || (*value == '\0')
567 || ((*value == '*') && (value[1] == '\0')) )
570 * no option, or option == "*".
574 list_remove_all(remove);
575 list_remove_all(add);
576 cur_action->multi_remove_all[action->index] = 1;
580 /* Valid option - remove only 1 option */
582 if ( !cur_action->multi_remove_all[action->index] )
584 /* there isn't a catch-all in the remove list already */
585 err = enlist_unique(remove, value, 0);
591 list_remove_item(add, value);
596 /* Shouldn't get here unless there's memory corruption. */
603 /* try user aliases. */
604 const struct action_alias * alias = alias_list;
606 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
613 merge_actions(cur_action, alias->action);
617 /* Bad action name */
628 /*********************************************************************
630 * Function : actions_to_text
632 * Description : Converts a actionsfile entry from numeric form
633 * ("mask" and "add") to text.
636 * 1 : mask = As from struct url_actions
637 * 2 : add = As from struct url_actions
639 * Returns : A string. Caller must free it.
640 * NULL on out-of-memory error.
642 *********************************************************************/
643 char * actions_to_text(struct action_spec *action)
645 unsigned mask = action->mask;
646 unsigned add = action->add;
647 char * result = strdup("");
648 struct list_entry * lst;
650 /* sanity - prevents "-feature +feature" */
654 #define DEFINE_ACTION_BOOL(__name, __bit) \
655 if (!(mask & __bit)) \
657 string_append(&result, " -" __name); \
659 else if (add & __bit) \
661 string_append(&result, " +" __name); \
664 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
665 if (!(mask & __bit)) \
667 string_append(&result, " -" __name); \
669 else if (add & __bit) \
671 string_append(&result, " +" __name "{"); \
672 string_append(&result, action->string[__index]); \
673 string_append(&result, "}"); \
676 #define DEFINE_ACTION_MULTI(__name, __index) \
677 if (action->multi_remove_all[__index]) \
679 string_append(&result, " -" __name "{*}"); \
683 lst = action->multi_remove[__index]->first; \
686 string_append(&result, " -" __name "{"); \
687 string_append(&result, lst->str); \
688 string_append(&result, "}"); \
692 lst = action->multi_add[__index]->first; \
695 string_append(&result, " +" __name "{"); \
696 string_append(&result, lst->str); \
697 string_append(&result, "}"); \
701 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
703 #include "actionlist.h"
705 #undef DEFINE_ACTION_MULTI
706 #undef DEFINE_ACTION_STRING
707 #undef DEFINE_ACTION_BOOL
708 #undef DEFINE_ACTION_ALIAS
714 #ifdef FEATURE_CGI_EDIT_ACTIONS
715 /*********************************************************************
717 * Function : actions_to_html
719 * Description : Converts a actionsfile entry from numeric form
720 * ("mask" and "add") to a <br>-seperated HTML string.
723 * 1 : mask = As from struct url_actions
724 * 2 : add = As from struct url_actions
726 * Returns : A string. Caller must free it.
727 * NULL on out-of-memory error.
729 *********************************************************************/
730 char * actions_to_html(struct action_spec *action)
732 unsigned mask = action->mask;
733 unsigned add = action->add;
734 char * result = strdup("");
736 struct list_entry * lst;
738 /* sanity - prevents "-feature +feature" */
742 #define DEFINE_ACTION_BOOL(__name, __bit) \
743 if (!(mask & __bit)) \
745 string_append(&result, "\n<br>-" __name); \
747 else if (add & __bit) \
749 string_append(&result, "\n<br>+" __name); \
752 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
753 if (!(mask & __bit)) \
755 string_append(&result, "\n<br>-" __name); \
757 else if (add & __bit) \
759 string_append(&result, "\n<br>+" __name "{"); \
760 if (NULL == result) \
764 enc_str = html_encode(action->string[__index]);\
765 if (NULL == enc_str) \
770 string_append(&result, enc_str); \
772 string_append(&result, "}"); \
775 #define DEFINE_ACTION_MULTI(__name, __index) \
776 if (action->multi_remove_all[__index]) \
778 string_append(&result, "\n<br>-" __name "{*}"); \
782 lst = action->multi_remove[__index]->first; \
785 string_append(&result, "\n<br>-" __name "{");\
786 if (NULL == result) \
790 enc_str = html_encode(lst->str); \
791 if (NULL == enc_str) \
796 string_append(&result, enc_str); \
798 string_append(&result, "}"); \
802 lst = action->multi_add[__index]->first; \
805 string_append(&result, "\n<br>+" __name "{"); \
806 if (NULL == result) \
810 enc_str = html_encode(lst->str); \
811 if (NULL == enc_str) \
816 string_append(&result, enc_str); \
818 string_append(&result, "}"); \
822 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
824 #include "actionlist.h"
826 #undef DEFINE_ACTION_MULTI
827 #undef DEFINE_ACTION_STRING
828 #undef DEFINE_ACTION_BOOL
829 #undef DEFINE_ACTION_ALIAS
831 /* trim leading <br> */
832 if (result && *result)
835 result = strdup(result + 5);
841 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
844 /*********************************************************************
846 * Function : current_actions_to_text
848 * Description : Converts a actionsfile entry to text.
851 * 1 : action = Action
853 * Returns : A string. Caller must free it.
854 * NULL on out-of-memory error.
856 *********************************************************************/
857 char * current_action_to_text(struct current_action_spec *action)
859 unsigned flags = action->flags;
860 char * result = strdup("");
861 struct list_entry * lst;
863 #define DEFINE_ACTION_BOOL(__name, __bit) \
866 string_append(&result, " +" __name); \
870 string_append(&result, " -" __name); \
873 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
876 string_append(&result, " +" __name "{"); \
877 string_append(&result, action->string[__index]); \
878 string_append(&result, "}"); \
882 string_append(&result, " -" __name); \
885 #define DEFINE_ACTION_MULTI(__name, __index) \
886 lst = action->multi[__index]->first; \
889 string_append(&result, " -" __name); \
895 string_append(&result, " +" __name "{"); \
896 string_append(&result, lst->str); \
897 string_append(&result, "}"); \
902 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
904 #include "actionlist.h"
906 #undef DEFINE_ACTION_MULTI
907 #undef DEFINE_ACTION_STRING
908 #undef DEFINE_ACTION_BOOL
909 #undef DEFINE_ACTION_ALIAS
915 /*********************************************************************
917 * Function : init_current_action
919 * Description : Zero out an action.
922 * 1 : dest = An uninitialized current_action_spec.
926 *********************************************************************/
927 void init_current_action (struct current_action_spec *dest)
929 memset(dest, '\0', sizeof(*dest));
931 dest->flags = ACTION_MOST_COMPATIBLE;
935 /*********************************************************************
937 * Function : init_action
939 * Description : Zero out an action.
942 * 1 : dest = An uninitialized action_spec.
946 *********************************************************************/
947 void init_action (struct action_spec *dest)
949 memset(dest, '\0', sizeof(*dest));
953 /*********************************************************************
955 * Function : merge_current_action
957 * Description : Merge two actions together.
958 * Similar to "dest += src".
959 * Differences between this and merge_actions()
960 * is that this one doesn't allocate memory for
961 * strings (so "src" better be in memory for at least
962 * as long as "dest" is, and you'd better free
963 * "dest" using "free_current_action").
964 * Also, there is no mask or remove lists in dest.
965 * (If we're applying it to a URL, we don't need them)
968 * 1 : dest = Current actions, to modify.
969 * 2 : src = Action to add.
971 * Returns 0 : no error
974 *********************************************************************/
975 jb_err merge_current_action (struct current_action_spec *dest,
976 const struct action_spec *src)
979 jb_err err = JB_ERR_OK;
981 dest->flags &= src->mask;
982 dest->flags |= src->add;
984 for (i = 0; i < ACTION_STRING_COUNT; i++)
986 char * str = src->string[i];
992 return JB_ERR_MEMORY;
994 freez(dest->string[i]);
995 dest->string[i] = str;
999 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1001 if (src->multi_remove_all[i])
1003 /* Remove everything from dest, then add src->multi_add */
1004 err = list_duplicate(dest->multi[i], src->multi_add[i]);
1012 list_remove_list(dest->multi[i], src->multi_remove[i]);
1013 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
1024 /*********************************************************************
1026 * Function : free_current_action
1028 * Description : Free memory used by a current_action_spec.
1029 * Does not free the current_action_spec itself.
1032 * 1 : src = Source to free.
1036 *********************************************************************/
1037 void free_current_action (struct current_action_spec *src)
1041 for (i = 0; i < ACTION_STRING_COUNT; i++)
1043 freez(src->string[i]);
1046 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1048 destroy_list(src->multi[i]);
1051 memset(src, '\0', sizeof(*src));
1055 /*********************************************************************
1057 * Function : unload_actions_file
1059 * Description : Unloads an actions module.
1062 * 1 : file_data = the data structure associated with the
1067 *********************************************************************/
1068 void unload_actions_file(void *file_data)
1070 struct url_actions * next;
1071 struct url_actions * cur = (struct url_actions *)file_data;
1076 free_action(cur->action);
1084 /*********************************************************************
1086 * Function : free_alias_list
1088 * Description : Free memory used by a list of aliases.
1091 * 1 : alias_list = Linked list to free.
1095 *********************************************************************/
1096 void free_alias_list(struct action_alias *alias_list)
1098 while (alias_list != NULL)
1100 struct action_alias * next = alias_list->next;
1101 alias_list->next = NULL;
1102 freez(alias_list->name);
1103 free_action(alias_list->action);
1110 /*********************************************************************
1112 * Function : load_actions_file
1114 * Description : Read and parse a action file and add to files
1118 * 1 : csp = Current client state (buffers, headers, etc...)
1120 * Returns : 0 => Ok, everything else is an error.
1122 *********************************************************************/
1123 int load_actions_file(struct client_state *csp)
1125 static struct file_list *current_actions_file = NULL;
1129 * Note: Keep these in the order they occur in the file, they are
1130 * sometimes tested with <=
1132 #define MODE_START_OF_FILE 1
1133 #define MODE_SETTINGS 2
1134 #define MODE_DESCRIPTION 3
1135 #define MODE_ALIAS 4
1136 #define MODE_ACTIONS 5
1138 int mode = MODE_START_OF_FILE;
1141 struct url_actions *last_perm;
1142 struct url_actions *perm;
1143 char buf[BUFFER_SIZE];
1144 struct file_list *fs;
1145 struct action_spec * cur_action = NULL;
1146 int cur_action_used = 0;
1147 struct action_alias * alias_list = NULL;
1149 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
1151 /* No need to load */
1154 csp->actions_list = current_actions_file;
1160 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1161 csp->config->actions_file);
1162 return 1; /* never get here */
1165 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1166 if (last_perm == NULL)
1168 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1169 csp->config->actions_file);
1170 return 1; /* never get here */
1173 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
1175 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1176 csp->config->actions_file);
1177 return 1; /* never get here */
1180 while (read_config_line(buf, sizeof(buf), fp) != NULL)
1184 /* It's a header block */
1187 /* It's {{settings}} or {{alias}} */
1188 int len = strlen(buf);
1189 char * start = buf + 2;
1190 char * end = buf + len - 1;
1191 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1195 log_error(LOG_LEVEL_FATAL,
1196 "can't load actions file '%s': invalid line: %s",
1197 csp->config->actions_file, buf);
1198 return 1; /* never get here */
1201 /* Trim leading and trailing whitespace. */
1209 log_error(LOG_LEVEL_FATAL,
1210 "can't load actions file '%s': invalid line: {{ }}",
1211 csp->config->actions_file);
1212 return 1; /* never get here */
1216 * An actionsfile can optionally contain the following blocks.
1217 * They *MUST* be in this order, to simplify processing:
1223 * ...free text, format TBD, but no line may start with a '{'...
1228 * The actual actions must be *after* these special blocks.
1229 * None of these special blocks may be repeated.
1232 if (0 == strcmpic(start, "settings"))
1234 /* it's a {{settings}} block */
1235 if (mode >= MODE_SETTINGS)
1237 /* {{settings}} must be first thing in file and must only
1241 log_error(LOG_LEVEL_FATAL,
1242 "can't load actions file '%s': {{settings}} must only appear once, and it must be before anything else.",
1243 csp->config->actions_file);
1245 mode = MODE_SETTINGS;
1247 else if (0 == strcmpic(start, "description"))
1249 /* it's a {{description}} block */
1250 if (mode >= MODE_DESCRIPTION)
1252 /* {{description}} is a singleton and only {{settings}} may proceed it
1255 log_error(LOG_LEVEL_FATAL,
1256 "can't load actions file '%s': {{description}} must only appear once, and only a {{settings}} block may be above it.",
1257 csp->config->actions_file);
1259 mode = MODE_DESCRIPTION;
1261 else if (0 == strcmpic(start, "alias"))
1263 /* it's an {{alias}} block */
1264 if (mode >= MODE_ALIAS)
1266 /* {{alias}} must be first thing in file, possibly after
1267 * {{settings}} and {{description}}
1269 * {{alias}} must only appear once.
1271 * Note that these are new restrictions introduced in
1272 * v2.9.10 in order to make actionsfile editing simpler.
1273 * (Otherwise, reordering actionsfile entries without
1274 * completely rewriting the file becomes non-trivial)
1277 log_error(LOG_LEVEL_FATAL,
1278 "can't load actions file '%s': {{alias}} must only appear once, and it must be before all actions.",
1279 csp->config->actions_file, start);
1285 /* invalid {{something}} block */
1287 log_error(LOG_LEVEL_FATAL,
1288 "can't load actions file '%s': invalid line: {{%s}}",
1289 csp->config->actions_file, start);
1290 return 1; /* never get here */
1295 /* It's an actions block */
1297 char actions_buf[BUFFER_SIZE];
1301 mode = MODE_ACTIONS;
1303 /* free old action */
1306 if (!cur_action_used)
1308 free_action(cur_action);
1313 cur_action_used = 0;
1314 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1315 if (cur_action == NULL)
1318 log_error(LOG_LEVEL_FATAL,
1319 "can't load actions file '%s': out of memory",
1320 csp->config->actions_file);
1321 return 1; /* never get here */
1323 init_action(cur_action);
1326 strcpy(actions_buf, buf + 1);
1328 /* check we have a trailing } and then trim it */
1329 end = actions_buf + strlen(actions_buf) - 1;
1334 log_error(LOG_LEVEL_FATAL,
1335 "can't load actions file '%s': invalid line: %s",
1336 csp->config->actions_file, buf);
1337 return 1; /* never get here */
1341 /* trim any whitespace immediately inside {} */
1344 if (get_actions(actions_buf, alias_list, cur_action))
1348 log_error(LOG_LEVEL_FATAL,
1349 "can't load actions file '%s': invalid line: %s",
1350 csp->config->actions_file, buf);
1351 return 1; /* never get here */
1355 else if (mode == MODE_SETTINGS)
1358 * Part of the {{settings}} block.
1359 * Ignore for now, but we may want to read & check permissions
1360 * when we go multi-user.
1363 else if (mode == MODE_DESCRIPTION)
1366 * Part of the {{description}} block.
1370 else if (mode == MODE_ALIAS)
1375 char actions_buf[BUFFER_SIZE];
1376 struct action_alias * new_alias;
1378 char * start = strchr(buf, '=');
1381 if ((start == NULL) || (start == buf))
1383 log_error(LOG_LEVEL_FATAL,
1384 "can't load actions file '%s': invalid alias line: %s",
1385 csp->config->actions_file, buf);
1386 return 1; /* never get here */
1389 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1392 log_error(LOG_LEVEL_FATAL,
1393 "can't load actions file '%s': out of memory!",
1394 csp->config->actions_file);
1395 return 1; /* never get here */
1398 /* Eat any the whitespace before the '=' */
1400 while ((*end == ' ') || (*end == '\t'))
1403 * we already know we must have at least 1 non-ws char
1404 * at start of buf - no need to check
1410 /* Eat any the whitespace after the '=' */
1412 while ((*start == ' ') || (*start == '\t'))
1418 log_error(LOG_LEVEL_FATAL,
1419 "can't load actions file '%s': invalid alias line: %s",
1420 csp->config->actions_file, buf);
1421 return 1; /* never get here */
1424 new_alias->name = strdup(buf);
1426 strcpy(actions_buf, start);
1428 if (get_actions(actions_buf, alias_list, new_alias->action))
1432 log_error(LOG_LEVEL_FATAL,
1433 "can't load actions file '%s': invalid alias line: %s = %s",
1434 csp->config->actions_file, buf, start);
1435 return 1; /* never get here */
1439 new_alias->next = alias_list;
1440 alias_list = new_alias;
1442 else if (mode == MODE_ACTIONS)
1444 /* it's a URL pattern */
1446 /* allocate a new node */
1447 if ((perm = zalloc(sizeof(*perm))) == NULL)
1450 log_error(LOG_LEVEL_FATAL,
1451 "can't load actions file '%s': out of memory!",
1452 csp->config->actions_file);
1453 return 1; /* never get here */
1457 copy_action (perm->action, cur_action);
1459 /* Save the URL pattern */
1460 if (create_url_spec(perm->url, buf))
1463 log_error(LOG_LEVEL_FATAL,
1464 "can't load actions file '%s': cannot create URL pattern from: %s",
1465 csp->config->actions_file, buf);
1466 return 1; /* never get here */
1469 /* add it to the list */
1470 last_perm->next = perm;
1473 else if (mode == MODE_START_OF_FILE)
1475 /* oops - please have a {} line as 1st line in file. */
1477 log_error(LOG_LEVEL_FATAL,
1478 "can't load actions file '%s': first line is invalid: %s",
1479 csp->config->actions_file, buf);
1480 return 1; /* never get here */
1484 /* How did we get here? This is impossible! */
1486 log_error(LOG_LEVEL_FATAL,
1487 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1488 csp->config->actions_file, mode);
1489 return 1; /* never get here */
1495 free_action(cur_action);
1497 free_alias_list(alias_list);
1499 /* the old one is now obsolete */
1500 if (current_actions_file)
1502 current_actions_file->unloader = unload_actions_file;
1505 fs->next = files->next;
1507 current_actions_file = fs;
1511 csp->actions_list = fs;