1 const char actions_rcs[] = "$Id: actions.c,v 1.18 2001/11/07 00:06:06 steudten 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.18 2001/11/07 00:06:06 steudten
37 * Add line number in error output for lineparsing for
40 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
41 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
42 * threads to call select() simultaneously. So, it's time to do a real, live,
43 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
44 * (native). Both versions will work, but using __OS2__ offers multi-threading.
46 * Revision 1.16 2001/10/23 21:30:30 jongfoster
47 * Adding error-checking to selected functions.
49 * Revision 1.15 2001/10/14 21:58:22 jongfoster
50 * Adding support for the CGI-based editor:
51 * - Exported get_actions()
52 * - Added new function free_alias_list()
53 * - Added support for {{settings}} and {{description}} blocks
54 * in the actions file. They are currently ignored.
55 * - Added restriction to only one {{alias}} block which must appear
56 * first in the file, to simplify the editor's rewriting rules.
57 * - Note that load_actions_file() is no longer used by the CGI-based
58 * editor, but some of the other routines in this file are.
60 * Revision 1.14 2001/09/22 16:36:59 jongfoster
61 * Removing unused parameter fs from read_config_line()
63 * Revision 1.13 2001/09/16 15:47:37 jongfoster
64 * First version of CGI-based edit interface. This is very much a
65 * work-in-progress, and you can't actually use it to edit anything
66 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
69 * Revision 1.12 2001/09/16 13:21:27 jongfoster
70 * Changes to use new list functions.
72 * Revision 1.11 2001/09/14 00:17:32 jongfoster
73 * Tidying up memory allocation. New function init_action().
75 * Revision 1.10 2001/09/10 10:14:34 oes
76 * Removing unused variable
78 * Revision 1.9 2001/07/30 22:08:36 jongfoster
79 * Tidying up #defines:
80 * - All feature #defines are now of the form FEATURE_xxx
81 * - Permanently turned off WIN_GUI_EDIT
82 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
84 * Revision 1.8 2001/06/29 13:19:52 oes
85 * Removed logentry from cancelled commit
87 * Revision 1.7 2001/06/09 10:55:28 jongfoster
88 * Changing BUFSIZ ==> BUFFER_SIZE
90 * Revision 1.6 2001/06/07 23:04:34 jongfoster
91 * Made get_actions() static.
93 * Revision 1.5 2001/06/03 19:11:48 oes
94 * adapted to new enlist_unique arg format
96 * Revision 1.4 2001/06/01 20:03:42 jongfoster
97 * Better memory management - current_action->strings[] now
98 * contains copies of the strings, not the original.
100 * Revision 1.3 2001/06/01 18:49:17 jongfoster
101 * Replaced "list_share" with "list" - the tiny memory gain was not
102 * worth the extra complexity.
104 * Revision 1.2 2001/05/31 21:40:00 jongfoster
105 * Removing some commented out, obsolete blocks of code.
107 * Revision 1.1 2001/05/31 21:16:46 jongfoster
108 * Moved functions to process the action list into this new file.
111 *********************************************************************/
125 #include "miscutil.h"
128 #ifdef FEATURE_CGI_EDIT_ACTIONS
130 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
132 const char actions_h_rcs[] = ACTIONS_H_VERSION;
136 * We need the main list of options.
138 * First, we need a way to tell between boolean, string, and multi-string
139 * options. For string and multistring options, we also need to be
140 * able to tell the difference between a "+" and a "-". (For bools,
141 * the "+"/"-" information is encoded in "add" and "mask"). So we use
142 * an enumerated type (well, the preprocessor equivalent). Here are
145 #define AV_NONE 0 /* +opt -opt */
146 #define AV_ADD_STRING 1 /* +stropt{string} */
147 #define AV_REM_STRING 2 /* -stropt */
148 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
149 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
152 * We need a structure to hold the name, flag changes,
153 * type, and string index.
158 unsigned mask; /* a bit set to "0" = remove action */
159 unsigned add; /* a bit set to "1" = add action */
160 int takes_value; /* an AV_... constant */
161 int index; /* index into strings[] or multi[] */
165 * And with those building blocks in place, here's the array.
167 static const struct action_name action_names[] =
170 * Well actually there's no data here - it's in actionlist.h
171 * This keeps it together to make it easy to change.
173 * Here's the macros used to format it:
175 #define DEFINE_ACTION_MULTI(name,index) \
176 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
177 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
178 #define DEFINE_ACTION_STRING(name,flag,index) \
179 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
180 { "-" name, ~flag, 0, AV_REM_STRING, index },
181 #define DEFINE_ACTION_BOOL(name,flag) \
182 { "+" name, ACTION_MASK_ALL, flag }, \
183 { "-" name, ~flag, 0 },
184 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
186 #include "actionlist.h"
188 #undef DEFINE_ACTION_MULTI
189 #undef DEFINE_ACTION_STRING
190 #undef DEFINE_ACTION_BOOL
191 #undef DEFINE_ACTION_ALIAS
193 { NULL, 0, 0 } /* End marker */
197 /*********************************************************************
199 * Function : merge_actions
201 * Description : Merge two actions together.
202 * Similar to "cur_action += new_action".
205 * 1 : cur_action = Current actions, to modify.
206 * 2 : new_action = Action to add.
208 * Returns : JB_ERR_OK or JB_ERR_MEMORY
210 *********************************************************************/
211 jb_err merge_actions (struct action_spec *dest,
212 const struct action_spec *src)
217 dest->mask &= src->mask;
218 dest->add &= src->mask;
219 dest->add |= src->add;
221 for (i = 0; i < ACTION_STRING_COUNT; i++)
223 char * str = src->string[i];
226 freez(dest->string[i]);
227 dest->string[i] = strdup(str);
228 if (NULL == dest->string[i])
230 return JB_ERR_MEMORY;
235 for (i = 0; i < ACTION_MULTI_COUNT; i++)
237 if (src->multi_remove_all[i])
239 /* Remove everything from dest */
240 list_remove_all(dest->multi_remove[i]);
241 dest->multi_remove_all[i] = 1;
243 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
245 else if (dest->multi_remove_all[i])
248 * dest already removes everything, so we only need to worry
251 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
252 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
256 /* No "remove all"s to worry about. */
257 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
258 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
259 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
272 /*********************************************************************
274 * Function : copy_action
276 * Description : Copy an action_specs.
277 * Similar to "cur_action = new_action".
278 * Note that dest better not contain valid data
279 * - it's overwritten, not freed.
282 * 1 : dest = Destination of copy.
283 * 2 : src = Source for copy.
287 *********************************************************************/
288 jb_err copy_action (struct action_spec *dest,
289 const struct action_spec *src)
292 jb_err err = JB_ERR_OK;
294 memset(dest, '\0', sizeof(*dest));
296 dest->mask = src->mask;
297 dest->add = src->add;
299 for (i = 0; i < ACTION_STRING_COUNT; i++)
301 char * str = src->string[i];
307 return JB_ERR_MEMORY;
309 dest->string[i] = str;
313 for (i = 0; i < ACTION_MULTI_COUNT; i++)
315 dest->multi_remove_all[i] = src->multi_remove_all[i];
316 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
321 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
331 /*********************************************************************
333 * Function : free_action
335 * Description : Destroy an action_spec. Frees memory used by it,
336 * except for the memory used by the struct action_spec
340 * 1 : src = Source to free.
344 *********************************************************************/
345 void free_action (struct action_spec *src)
349 for (i = 0; i < ACTION_STRING_COUNT; i++)
351 freez(src->string[i]);
354 for (i = 0; i < ACTION_MULTI_COUNT; i++)
356 destroy_list(src->multi_remove[i]);
357 destroy_list(src->multi_add[i]);
360 memset(src, '\0', sizeof(*src));
364 /*********************************************************************
366 * Function : get_action_token
368 * Description : Parses a line for the first action.
369 * Modifies it's input array, doesn't allocate memory.
371 * *line=" +abc{def} -ghi "
378 * 1 : line = [in] The line containing the action.
379 * [out] Start of next action on line, or
380 * NULL if we reached the end of line before
381 * we found an action.
382 * 2 : name = [out] Start of action name, null
383 * terminated. NULL on EOL
384 * 3 : value = [out] Start of action value, null
385 * terminated. NULL if none or EOL.
387 * Returns : JB_ERR_OK => Ok
388 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
390 *********************************************************************/
391 jb_err get_action_token(char **line, char **name, char **value)
396 /* set default returns */
401 /* Eat any leading whitespace */
402 while ((*str == ' ') || (*str == '\t'))
414 /* null name, just value is prohibited */
421 while (((ch = *str) != '\0') &&
422 (ch != ' ') && (ch != '\t') && (ch != '{'))
426 /* error, '}' without '{' */
438 /* EOL - be careful not to run off buffer */
443 /* More to parse next time. */
452 str = strchr(str, '}');
470 /*********************************************************************
472 * Function : get_actions
474 * Description : Parses a list of actions.
477 * 1 : line = The string containing the actions.
478 * Will be written to by this function.
479 * 2 : aliases = Custom alias list, or NULL for none.
480 * 3 : cur_action = Where to store the action. Caller
483 * Returns : JB_ERR_OK => Ok
484 * JB_ERR_PARSE => Parse error (line was trashed anyway)
485 * nonzero => Out of memory (line was trashed anyway)
487 *********************************************************************/
488 jb_err get_actions(char *line,
489 struct action_alias * alias_list,
490 struct action_spec *cur_action)
493 init_action(cur_action);
494 cur_action->mask = ACTION_MASK_ALL;
498 char * option = NULL;
501 err = get_action_token(&line, &option, &value);
509 /* handle option in 'option' */
511 /* Check for standard action name */
512 const struct action_name * action = action_names;
514 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
518 if (action->name != NULL)
521 cur_action->mask &= action->mask;
522 cur_action->add &= action->mask;
523 cur_action->add |= action->add;
525 switch (action->takes_value)
528 /* ignore any option. */
532 /* add single string. */
534 if ((value == NULL) || (*value == '\0'))
538 /* FIXME: should validate option string here */
539 freez (cur_action->string[action->index]);
540 cur_action->string[action->index] = strdup(value);
541 if (NULL == cur_action->string[action->index])
543 return JB_ERR_MEMORY;
549 /* remove single string. */
551 freez (cur_action->string[action->index]);
556 /* append multi string. */
558 struct list * remove = cur_action->multi_remove[action->index];
559 struct list * add = cur_action->multi_add[action->index];
561 if ((value == NULL) || (*value == '\0'))
566 list_remove_item(remove, value);
567 err = enlist_unique(add, value, 0);
576 /* remove multi string. */
578 struct list * remove = cur_action->multi_remove[action->index];
579 struct list * add = cur_action->multi_add[action->index];
581 if ( (value == NULL) || (*value == '\0')
582 || ((*value == '*') && (value[1] == '\0')) )
585 * no option, or option == "*".
589 list_remove_all(remove);
590 list_remove_all(add);
591 cur_action->multi_remove_all[action->index] = 1;
595 /* Valid option - remove only 1 option */
597 if ( !cur_action->multi_remove_all[action->index] )
599 /* there isn't a catch-all in the remove list already */
600 err = enlist_unique(remove, value, 0);
606 list_remove_item(add, value);
611 /* Shouldn't get here unless there's memory corruption. */
618 /* try user aliases. */
619 const struct action_alias * alias = alias_list;
621 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
628 merge_actions(cur_action, alias->action);
632 /* Bad action name */
643 /*********************************************************************
645 * Function : actions_to_text
647 * Description : Converts a actionsfile entry from numeric form
648 * ("mask" and "add") to text.
651 * 1 : mask = As from struct url_actions
652 * 2 : add = As from struct url_actions
654 * Returns : A string. Caller must free it.
655 * NULL on out-of-memory error.
657 *********************************************************************/
658 char * actions_to_text(struct action_spec *action)
660 unsigned mask = action->mask;
661 unsigned add = action->add;
662 char * result = strdup("");
663 struct list_entry * lst;
665 /* sanity - prevents "-feature +feature" */
669 #define DEFINE_ACTION_BOOL(__name, __bit) \
670 if (!(mask & __bit)) \
672 string_append(&result, " -" __name); \
674 else if (add & __bit) \
676 string_append(&result, " +" __name); \
679 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
680 if (!(mask & __bit)) \
682 string_append(&result, " -" __name); \
684 else if (add & __bit) \
686 string_append(&result, " +" __name "{"); \
687 string_append(&result, action->string[__index]); \
688 string_append(&result, "}"); \
691 #define DEFINE_ACTION_MULTI(__name, __index) \
692 if (action->multi_remove_all[__index]) \
694 string_append(&result, " -" __name "{*}"); \
698 lst = action->multi_remove[__index]->first; \
701 string_append(&result, " -" __name "{"); \
702 string_append(&result, lst->str); \
703 string_append(&result, "}"); \
707 lst = action->multi_add[__index]->first; \
710 string_append(&result, " +" __name "{"); \
711 string_append(&result, lst->str); \
712 string_append(&result, "}"); \
716 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
718 #include "actionlist.h"
720 #undef DEFINE_ACTION_MULTI
721 #undef DEFINE_ACTION_STRING
722 #undef DEFINE_ACTION_BOOL
723 #undef DEFINE_ACTION_ALIAS
729 #ifdef FEATURE_CGI_EDIT_ACTIONS
730 /*********************************************************************
732 * Function : actions_to_html
734 * Description : Converts a actionsfile entry from numeric form
735 * ("mask" and "add") to a <br>-seperated HTML string.
738 * 1 : mask = As from struct url_actions
739 * 2 : add = As from struct url_actions
741 * Returns : A string. Caller must free it.
742 * NULL on out-of-memory error.
744 *********************************************************************/
745 char * actions_to_html(struct action_spec *action)
747 unsigned mask = action->mask;
748 unsigned add = action->add;
749 char * result = strdup("");
751 struct list_entry * lst;
753 /* sanity - prevents "-feature +feature" */
757 #define DEFINE_ACTION_BOOL(__name, __bit) \
758 if (!(mask & __bit)) \
760 string_append(&result, "\n<br>-" __name); \
762 else if (add & __bit) \
764 string_append(&result, "\n<br>+" __name); \
767 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
768 if (!(mask & __bit)) \
770 string_append(&result, "\n<br>-" __name); \
772 else if (add & __bit) \
774 string_append(&result, "\n<br>+" __name "{"); \
775 if (NULL == result) \
779 enc_str = html_encode(action->string[__index]);\
780 if (NULL == enc_str) \
785 string_append(&result, enc_str); \
787 string_append(&result, "}"); \
790 #define DEFINE_ACTION_MULTI(__name, __index) \
791 if (action->multi_remove_all[__index]) \
793 string_append(&result, "\n<br>-" __name "{*}"); \
797 lst = action->multi_remove[__index]->first; \
800 string_append(&result, "\n<br>-" __name "{");\
801 if (NULL == result) \
805 enc_str = html_encode(lst->str); \
806 if (NULL == enc_str) \
811 string_append(&result, enc_str); \
813 string_append(&result, "}"); \
817 lst = action->multi_add[__index]->first; \
820 string_append(&result, "\n<br>+" __name "{"); \
821 if (NULL == result) \
825 enc_str = html_encode(lst->str); \
826 if (NULL == enc_str) \
831 string_append(&result, enc_str); \
833 string_append(&result, "}"); \
837 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
839 #include "actionlist.h"
841 #undef DEFINE_ACTION_MULTI
842 #undef DEFINE_ACTION_STRING
843 #undef DEFINE_ACTION_BOOL
844 #undef DEFINE_ACTION_ALIAS
846 /* trim leading <br> */
847 if (result && *result)
850 result = strdup(result + 5);
856 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
859 /*********************************************************************
861 * Function : current_actions_to_text
863 * Description : Converts a actionsfile entry to text.
866 * 1 : action = Action
868 * Returns : A string. Caller must free it.
869 * NULL on out-of-memory error.
871 *********************************************************************/
872 char * current_action_to_text(struct current_action_spec *action)
874 unsigned flags = action->flags;
875 char * result = strdup("");
876 struct list_entry * lst;
878 #define DEFINE_ACTION_BOOL(__name, __bit) \
881 string_append(&result, " +" __name); \
885 string_append(&result, " -" __name); \
888 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
891 string_append(&result, " +" __name "{"); \
892 string_append(&result, action->string[__index]); \
893 string_append(&result, "}"); \
897 string_append(&result, " -" __name); \
900 #define DEFINE_ACTION_MULTI(__name, __index) \
901 lst = action->multi[__index]->first; \
904 string_append(&result, " -" __name); \
910 string_append(&result, " +" __name "{"); \
911 string_append(&result, lst->str); \
912 string_append(&result, "}"); \
917 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
919 #include "actionlist.h"
921 #undef DEFINE_ACTION_MULTI
922 #undef DEFINE_ACTION_STRING
923 #undef DEFINE_ACTION_BOOL
924 #undef DEFINE_ACTION_ALIAS
930 /*********************************************************************
932 * Function : init_current_action
934 * Description : Zero out an action.
937 * 1 : dest = An uninitialized current_action_spec.
941 *********************************************************************/
942 void init_current_action (struct current_action_spec *dest)
944 memset(dest, '\0', sizeof(*dest));
946 dest->flags = ACTION_MOST_COMPATIBLE;
950 /*********************************************************************
952 * Function : init_action
954 * Description : Zero out an action.
957 * 1 : dest = An uninitialized action_spec.
961 *********************************************************************/
962 void init_action (struct action_spec *dest)
964 memset(dest, '\0', sizeof(*dest));
968 /*********************************************************************
970 * Function : merge_current_action
972 * Description : Merge two actions together.
973 * Similar to "dest += src".
974 * Differences between this and merge_actions()
975 * is that this one doesn't allocate memory for
976 * strings (so "src" better be in memory for at least
977 * as long as "dest" is, and you'd better free
978 * "dest" using "free_current_action").
979 * Also, there is no mask or remove lists in dest.
980 * (If we're applying it to a URL, we don't need them)
983 * 1 : dest = Current actions, to modify.
984 * 2 : src = Action to add.
986 * Returns 0 : no error
989 *********************************************************************/
990 jb_err merge_current_action (struct current_action_spec *dest,
991 const struct action_spec *src)
994 jb_err err = JB_ERR_OK;
996 dest->flags &= src->mask;
997 dest->flags |= src->add;
999 for (i = 0; i < ACTION_STRING_COUNT; i++)
1001 char * str = src->string[i];
1007 return JB_ERR_MEMORY;
1009 freez(dest->string[i]);
1010 dest->string[i] = str;
1014 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1016 if (src->multi_remove_all[i])
1018 /* Remove everything from dest, then add src->multi_add */
1019 err = list_duplicate(dest->multi[i], src->multi_add[i]);
1027 list_remove_list(dest->multi[i], src->multi_remove[i]);
1028 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
1039 /*********************************************************************
1041 * Function : free_current_action
1043 * Description : Free memory used by a current_action_spec.
1044 * Does not free the current_action_spec itself.
1047 * 1 : src = Source to free.
1051 *********************************************************************/
1052 void free_current_action (struct current_action_spec *src)
1056 for (i = 0; i < ACTION_STRING_COUNT; i++)
1058 freez(src->string[i]);
1061 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1063 destroy_list(src->multi[i]);
1066 memset(src, '\0', sizeof(*src));
1070 /*********************************************************************
1072 * Function : unload_actions_file
1074 * Description : Unloads an actions module.
1077 * 1 : file_data = the data structure associated with the
1082 *********************************************************************/
1083 void unload_actions_file(void *file_data)
1085 struct url_actions * next;
1086 struct url_actions * cur = (struct url_actions *)file_data;
1091 free_action(cur->action);
1099 /*********************************************************************
1101 * Function : free_alias_list
1103 * Description : Free memory used by a list of aliases.
1106 * 1 : alias_list = Linked list to free.
1110 *********************************************************************/
1111 void free_alias_list(struct action_alias *alias_list)
1113 while (alias_list != NULL)
1115 struct action_alias * next = alias_list->next;
1116 alias_list->next = NULL;
1117 freez(alias_list->name);
1118 free_action(alias_list->action);
1125 /*********************************************************************
1127 * Function : load_actions_file
1129 * Description : Read and parse a action file and add to files
1133 * 1 : csp = Current client state (buffers, headers, etc...)
1135 * Returns : 0 => Ok, everything else is an error.
1137 *********************************************************************/
1138 int load_actions_file(struct client_state *csp)
1140 static struct file_list *current_actions_file = NULL;
1144 * Note: Keep these in the order they occur in the file, they are
1145 * sometimes tested with <=
1147 #define MODE_START_OF_FILE 1
1148 #define MODE_SETTINGS 2
1149 #define MODE_DESCRIPTION 3
1150 #define MODE_ALIAS 4
1151 #define MODE_ACTIONS 5
1153 int mode = MODE_START_OF_FILE;
1156 struct url_actions *last_perm;
1157 struct url_actions *perm;
1158 char buf[BUFFER_SIZE];
1159 struct file_list *fs;
1160 struct action_spec * cur_action = NULL;
1161 int cur_action_used = 0;
1162 struct action_alias * alias_list = NULL;
1163 unsigned long linenum = 0;
1165 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
1167 /* No need to load */
1170 csp->actions_list = current_actions_file;
1176 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1177 csp->config->actions_file);
1178 return 1; /* never get here */
1181 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1182 if (last_perm == NULL)
1184 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1185 csp->config->actions_file);
1186 return 1; /* never get here */
1189 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
1191 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1192 csp->config->actions_file);
1193 return 1; /* never get here */
1196 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1200 /* It's a header block */
1203 /* It's {{settings}} or {{alias}} */
1204 int len = strlen(buf);
1205 char * start = buf + 2;
1206 char * end = buf + len - 1;
1207 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1211 log_error(LOG_LEVEL_FATAL,
1212 "can't load actions file '%s': invalid line (%lu): %s",
1213 csp->config->actions_file, linenum, buf);
1214 return 1; /* never get here */
1217 /* Trim leading and trailing whitespace. */
1225 log_error(LOG_LEVEL_FATAL,
1226 "can't load actions file '%s': invalid line (%lu): {{ }}",
1227 csp->config->actions_file, linenum);
1228 return 1; /* never get here */
1232 * An actionsfile can optionally contain the following blocks.
1233 * They *MUST* be in this order, to simplify processing:
1239 * ...free text, format TBD, but no line may start with a '{'...
1244 * The actual actions must be *after* these special blocks.
1245 * None of these special blocks may be repeated.
1248 if (0 == strcmpic(start, "settings"))
1250 /* it's a {{settings}} block */
1251 if (mode >= MODE_SETTINGS)
1253 /* {{settings}} must be first thing in file and must only
1257 log_error(LOG_LEVEL_FATAL,
1258 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1259 csp->config->actions_file, linenum);
1261 mode = MODE_SETTINGS;
1263 else if (0 == strcmpic(start, "description"))
1265 /* it's a {{description}} block */
1266 if (mode >= MODE_DESCRIPTION)
1268 /* {{description}} is a singleton and only {{settings}} may proceed it
1271 log_error(LOG_LEVEL_FATAL,
1272 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1273 csp->config->actions_file, linenum);
1275 mode = MODE_DESCRIPTION;
1277 else if (0 == strcmpic(start, "alias"))
1279 /* it's an {{alias}} block */
1280 if (mode >= MODE_ALIAS)
1282 /* {{alias}} must be first thing in file, possibly after
1283 * {{settings}} and {{description}}
1285 * {{alias}} must only appear once.
1287 * Note that these are new restrictions introduced in
1288 * v2.9.10 in order to make actionsfile editing simpler.
1289 * (Otherwise, reordering actionsfile entries without
1290 * completely rewriting the file becomes non-trivial)
1293 log_error(LOG_LEVEL_FATAL,
1294 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1295 csp->config->actions_file, linenum);
1301 /* invalid {{something}} block */
1303 log_error(LOG_LEVEL_FATAL,
1304 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1305 csp->config->actions_file, linenum, start);
1306 return 1; /* never get here */
1311 /* It's an actions block */
1313 char actions_buf[BUFFER_SIZE];
1317 mode = MODE_ACTIONS;
1319 /* free old action */
1322 if (!cur_action_used)
1324 free_action(cur_action);
1329 cur_action_used = 0;
1330 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1331 if (cur_action == NULL)
1334 log_error(LOG_LEVEL_FATAL,
1335 "can't load actions file '%s': out of memory",
1336 csp->config->actions_file);
1337 return 1; /* never get here */
1339 init_action(cur_action);
1342 strcpy(actions_buf, buf + 1);
1344 /* check we have a trailing } and then trim it */
1345 end = actions_buf + strlen(actions_buf) - 1;
1350 log_error(LOG_LEVEL_FATAL,
1351 "can't load actions file '%s': invalid line (%lu): %s",
1352 csp->config->actions_file, linenum, buf);
1353 return 1; /* never get here */
1357 /* trim any whitespace immediately inside {} */
1360 if (get_actions(actions_buf, alias_list, cur_action))
1364 log_error(LOG_LEVEL_FATAL,
1365 "can't load actions file '%s': invalid line (%lu): %s",
1366 csp->config->actions_file, linenum, buf);
1367 return 1; /* never get here */
1371 else if (mode == MODE_SETTINGS)
1374 * Part of the {{settings}} block.
1375 * Ignore for now, but we may want to read & check permissions
1376 * when we go multi-user.
1379 else if (mode == MODE_DESCRIPTION)
1382 * Part of the {{description}} block.
1386 else if (mode == MODE_ALIAS)
1391 char actions_buf[BUFFER_SIZE];
1392 struct action_alias * new_alias;
1394 char * start = strchr(buf, '=');
1397 if ((start == NULL) || (start == buf))
1399 log_error(LOG_LEVEL_FATAL,
1400 "can't load actions file '%s': invalid alias line (%lu): %s",
1401 csp->config->actions_file, linenum, buf);
1402 return 1; /* never get here */
1405 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1408 log_error(LOG_LEVEL_FATAL,
1409 "can't load actions file '%s': out of memory!",
1410 csp->config->actions_file);
1411 return 1; /* never get here */
1414 /* Eat any the whitespace before the '=' */
1416 while ((*end == ' ') || (*end == '\t'))
1419 * we already know we must have at least 1 non-ws char
1420 * at start of buf - no need to check
1426 /* Eat any the whitespace after the '=' */
1428 while ((*start == ' ') || (*start == '\t'))
1434 log_error(LOG_LEVEL_FATAL,
1435 "can't load actions file '%s': invalid alias line (%lu): %s",
1436 csp->config->actions_file, linenum, buf);
1437 return 1; /* never get here */
1440 new_alias->name = strdup(buf);
1442 strcpy(actions_buf, start);
1444 if (get_actions(actions_buf, alias_list, new_alias->action))
1448 log_error(LOG_LEVEL_FATAL,
1449 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1450 csp->config->actions_file, linenum, buf, start);
1451 return 1; /* never get here */
1455 new_alias->next = alias_list;
1456 alias_list = new_alias;
1458 else if (mode == MODE_ACTIONS)
1460 /* it's a URL pattern */
1462 /* allocate a new node */
1463 if ((perm = zalloc(sizeof(*perm))) == NULL)
1466 log_error(LOG_LEVEL_FATAL,
1467 "can't load actions file '%s': out of memory!",
1468 csp->config->actions_file);
1469 return 1; /* never get here */
1473 copy_action (perm->action, cur_action);
1475 /* Save the URL pattern */
1476 if (create_url_spec(perm->url, buf))
1479 log_error(LOG_LEVEL_FATAL,
1480 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1481 csp->config->actions_file, linenum, buf);
1482 return 1; /* never get here */
1485 /* add it to the list */
1486 last_perm->next = perm;
1489 else if (mode == MODE_START_OF_FILE)
1491 /* oops - please have a {} line as 1st line in file. */
1493 log_error(LOG_LEVEL_FATAL,
1494 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1495 csp->config->actions_file, linenum, buf);
1496 return 1; /* never get here */
1500 /* How did we get here? This is impossible! */
1502 log_error(LOG_LEVEL_FATAL,
1503 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1504 csp->config->actions_file, mode);
1505 return 1; /* never get here */
1511 free_action(cur_action);
1513 free_alias_list(alias_list);
1515 /* the old one is now obsolete */
1516 if (current_actions_file)
1518 current_actions_file->unloader = unload_actions_file;
1521 fs->next = files->next;
1523 current_actions_file = fs;
1527 csp->actions_list = fs;