Replaces TABs by spaces in C source.
[privoxy.git] / actions.c
1 const char actions_rcs[] = "$Id: actions.c,v 1.32.2.6 2006/01/29 23:10:56 david__schmidt Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/Attic/actions.c,v $
5  *
6  * Purpose     :  Declares functions to work with actions files
7  *                Functions declared include: FIXME
8  *
9  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
10  *                Privoxy team. http://www.privoxy.org/
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
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.
21  *
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.
27  *
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.
33  *
34  * Revisions   :
35  *    $Log: actions.c,v $
36  *    Revision 1.32.2.6  2006/01/29 23:10:56  david__schmidt
37  *    Multiple filter file support
38  *
39  *    Revision 1.32.2.5  2005/06/09 01:18:41  david__schmidt
40  *    Tweaks to conditionally include pthread.h if FEATURE_PTHREAD is enabled -
41  *    this becomes important when jcc.h gets included later down the line.
42  *
43  *    Revision 1.32.2.4  2003/12/03 10:33:11  oes
44  *    - Implemented Privoxy version requirement through
45  *      for-privoxy-version= statement in {{settings}}
46  *      block
47  *    - Fix for unchecked out-of-memory condition
48  *
49  *    Revision 1.32.2.3  2003/02/28 12:52:10  oes
50  *    Fixed memory leak reported by Dan Price in Bug #694713
51  *
52  *    Revision 1.32.2.2  2002/11/20 14:36:55  oes
53  *    Extended unload_current_actions_file() to multiple AFs.
54  *    Thanks to Oliver Stoeneberg for the hint.
55  *
56  *    Revision 1.32.2.1  2002/05/26 12:13:16  roro
57  *    Change unsigned to unsigned long in actions_name struct.  This closes
58  *    SourceForge Bug #539284.
59  *
60  *    Revision 1.32  2002/05/12 21:36:29  jongfoster
61  *    Correcting function comments
62  *
63  *    Revision 1.31  2002/05/06 07:56:50  oes
64  *    Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
65  *
66  *    Revision 1.30  2002/04/30 11:14:52  oes
67  *    Made csp the first parameter in *action_to_html
68  *
69  *    Revision 1.29  2002/04/26 19:30:54  jongfoster
70  *    - current_action_to_html(): Adding help link for the "-" form of
71  *      one-string actions.
72  *    - Some actions had "<br>-", some "<br> -" (note the space).
73  *      Standardizing on no space.
74  *    - Greatly simplifying some of the code by using string_join()
75  *      where appropriate.
76  *
77  *    Revision 1.28  2002/04/26 12:53:15  oes
78  *     - CGI AF editor now writes action lines split into
79  *       single lines with line continuation
80  *     - actions_to_html now embeds each action name in
81  *       link to chapter
82  *     - current_action_to_text is now called current_action_to_html
83  *       and acts like actions_to_html
84  *
85  *    Revision 1.27  2002/04/24 02:10:31  oes
86  *     - Jon's patch for multiple AFs:
87  *       - split load_actions_file, add load_one_actions_file
88  *       - make csp->actions_list files an array
89  *       - remember file id with each action
90  *     - Copy_action now frees dest action before copying
91  *
92  *    Revision 1.26  2002/03/26 22:29:54  swa
93  *    we have a new homepage!
94  *
95  *    Revision 1.25  2002/03/24 13:25:43  swa
96  *    name change related issues
97  *
98  *    Revision 1.24  2002/03/16 23:54:06  jongfoster
99  *    Adding graceful termination feature, to help look for memory leaks.
100  *    If you enable this (which, by design, has to be done by hand
101  *    editing config.h) and then go to http://i.j.b/die, then the program
102  *    will exit cleanly after the *next* request.  It should free all the
103  *    memory that was used.
104  *
105  *    Revision 1.23  2002/03/07 03:46:16  oes
106  *    Fixed compiler warnings
107  *
108  *    Revision 1.22  2002/01/21 00:27:02  jongfoster
109  *    Allowing free_action(NULL).
110  *    Moving the functions that #include actionlist.h to the end of the file,
111  *    because the Visual C++ 97 debugger gets extremely confused if you try
112  *    to debug any code that comes after them in the file.
113  *
114  *    Revision 1.21  2002/01/17 20:54:44  jongfoster
115  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
116  *
117  *    Revision 1.20  2001/11/22 21:56:49  jongfoster
118  *    Making action_spec->flags into an unsigned long rather than just an
119  *    unsigned int.
120  *    Fixing a bug in the display of -add-header and -wafer
121  *
122  *    Revision 1.19  2001/11/13 00:14:07  jongfoster
123  *    Fixing stupid bug now I've figured out what || means.
124  *    (It always returns 0 or 1, not one of it's paramaters.)
125  *
126  *    Revision 1.18  2001/11/07 00:06:06  steudten
127  *    Add line number in error output for lineparsing for
128  *    actionsfile.
129  *
130  *    Revision 1.17  2001/10/25 03:40:47  david__schmidt
131  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
132  *    threads to call select() simultaneously.  So, it's time to do a real, live,
133  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
134  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
135  *
136  *    Revision 1.16  2001/10/23 21:30:30  jongfoster
137  *    Adding error-checking to selected functions.
138  *
139  *    Revision 1.15  2001/10/14 21:58:22  jongfoster
140  *    Adding support for the CGI-based editor:
141  *    - Exported get_actions()
142  *    - Added new function free_alias_list()
143  *    - Added support for {{settings}} and {{description}} blocks
144  *      in the actions file.  They are currently ignored.
145  *    - Added restriction to only one {{alias}} block which must appear
146  *      first in the file, to simplify the editor's rewriting rules.
147  *    - Note that load_actions_file() is no longer used by the CGI-based
148  *      editor, but some of the other routines in this file are.
149  *
150  *    Revision 1.14  2001/09/22 16:36:59  jongfoster
151  *    Removing unused parameter fs from read_config_line()
152  *
153  *    Revision 1.13  2001/09/16 15:47:37  jongfoster
154  *    First version of CGI-based edit interface.  This is very much a
155  *    work-in-progress, and you can't actually use it to edit anything
156  *    yet.  You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
157  *    to have any effect.
158  *
159  *    Revision 1.12  2001/09/16 13:21:27  jongfoster
160  *    Changes to use new list functions.
161  *
162  *    Revision 1.11  2001/09/14 00:17:32  jongfoster
163  *    Tidying up memory allocation. New function init_action().
164  *
165  *    Revision 1.10  2001/09/10 10:14:34  oes
166  *    Removing unused variable
167  *
168  *    Revision 1.9  2001/07/30 22:08:36  jongfoster
169  *    Tidying up #defines:
170  *    - All feature #defines are now of the form FEATURE_xxx
171  *    - Permanently turned off WIN_GUI_EDIT
172  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
173  *
174  *    Revision 1.8  2001/06/29 13:19:52  oes
175  *    Removed logentry from cancelled commit
176  *
177  *    Revision 1.7  2001/06/09 10:55:28  jongfoster
178  *    Changing BUFSIZ ==> BUFFER_SIZE
179  *
180  *    Revision 1.6  2001/06/07 23:04:34  jongfoster
181  *    Made get_actions() static.
182  *
183  *    Revision 1.5  2001/06/03 19:11:48  oes
184  *    adapted to new enlist_unique arg format
185  *
186  *    Revision 1.4  2001/06/01 20:03:42  jongfoster
187  *    Better memory management - current_action->strings[] now
188  *    contains copies of the strings, not the original.
189  *
190  *    Revision 1.3  2001/06/01 18:49:17  jongfoster
191  *    Replaced "list_share" with "list" - the tiny memory gain was not
192  *    worth the extra complexity.
193  *
194  *    Revision 1.2  2001/05/31 21:40:00  jongfoster
195  *    Removing some commented out, obsolete blocks of code.
196  *
197  *    Revision 1.1  2001/05/31 21:16:46  jongfoster
198  *    Moved functions to process the action list into this new file.
199  *
200  *
201  *********************************************************************/
202 \f
203
204 #include "config.h"
205
206 #include <stdio.h>
207 #include <string.h>
208 #include <assert.h>
209 #include <stdlib.h>
210
211 #ifdef FEATURE_PTHREAD
212 #include <pthread.h>
213 #endif
214
215 #include "project.h"
216 #include "jcc.h"
217 #include "list.h"
218 #include "actions.h"
219 #include "miscutil.h"
220 #include "errlog.h"
221 #include "loaders.h"
222 #include "encode.h"
223 #include "urlmatch.h"
224 #include "cgi.h"
225 #include "ssplit.h"
226
227 const char actions_h_rcs[] = ACTIONS_H_VERSION;
228
229
230 /*
231  * We need the main list of options.
232  *
233  * First, we need a way to tell between boolean, string, and multi-string
234  * options.  For string and multistring options, we also need to be
235  * able to tell the difference between a "+" and a "-".  (For bools,
236  * the "+"/"-" information is encoded in "add" and "mask").  So we use
237  * an enumerated type (well, the preprocessor equivalent).  Here are
238  * the values:
239  */
240 #define AV_NONE       0 /* +opt -opt */
241 #define AV_ADD_STRING 1 /* +stropt{string} */
242 #define AV_REM_STRING 2 /* -stropt */
243 #define AV_ADD_MULTI  3 /* +multiopt{string} +multiopt{string2} */
244 #define AV_REM_MULTI  4 /* -multiopt{string} -multiopt          */
245
246 /*
247  * We need a structure to hold the name, flag changes,
248  * type, and string index.
249  */
250 struct action_name
251 {
252    const char * name;
253    unsigned long mask;   /* a bit set to "0" = remove action */
254    unsigned long add;    /* a bit set to "1" = add action */
255    int takes_value;      /* an AV_... constant */
256    int index;            /* index into strings[] or multi[] */
257 };
258
259 /*
260  * And with those building blocks in place, here's the array.
261  */
262 static const struct action_name action_names[] =
263 {
264    /*
265     * Well actually there's no data here - it's in actionlist.h
266     * This keeps it together to make it easy to change.
267     *
268     * Here's the macros used to format it:
269     */
270 #define DEFINE_ACTION_MULTI(name,index)                   \
271    { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
272    { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
273 #define DEFINE_ACTION_STRING(name,flag,index)                 \
274    { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
275    { "-" name, ~flag, 0, AV_REM_STRING, index },
276 #define DEFINE_ACTION_BOOL(name,flag)   \
277    { "+" name, ACTION_MASK_ALL, flag }, \
278    { "-" name, ~flag, 0 },
279 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
280
281 #include "actionlist.h"
282
283 #undef DEFINE_ACTION_MULTI
284 #undef DEFINE_ACTION_STRING
285 #undef DEFINE_ACTION_BOOL
286 #undef DEFINE_ACTION_ALIAS
287
288    { NULL, 0, 0 } /* End marker */
289 };
290
291
292 static int load_one_actions_file(struct client_state *csp, int fileid);
293
294
295 /*********************************************************************
296  *
297  * Function    :  merge_actions
298  *
299  * Description :  Merge two actions together.
300  *                Similar to "dest += src".
301  *
302  * Parameters  :
303  *          1  :  dest = Actions to modify.
304  *          2  :  src = Action to add.
305  *
306  * Returns     :  JB_ERR_OK or JB_ERR_MEMORY
307  *
308  *********************************************************************/
309 jb_err merge_actions (struct action_spec *dest,
310                       const struct action_spec *src)
311 {
312    int i;
313    jb_err err;
314
315    dest->mask &= src->mask;
316    dest->add  &= src->mask;
317    dest->add  |= src->add;
318
319    for (i = 0; i < ACTION_STRING_COUNT; i++)
320    {
321       char * str = src->string[i];
322       if (str)
323       {
324          freez(dest->string[i]);
325          dest->string[i] = strdup(str);
326          if (NULL == dest->string[i])
327          {
328             return JB_ERR_MEMORY;
329          }
330       }
331    }
332
333    for (i = 0; i < ACTION_MULTI_COUNT; i++)
334    {
335       if (src->multi_remove_all[i])
336       {
337          /* Remove everything from dest */
338          list_remove_all(dest->multi_remove[i]);
339          dest->multi_remove_all[i] = 1;
340
341          err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
342       }
343       else if (dest->multi_remove_all[i])
344       {
345          /*
346           * dest already removes everything, so we only need to worry
347           * about what we add.
348           */
349          list_remove_list(dest->multi_add[i], src->multi_remove[i]);
350          err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
351       }
352       else
353       {
354          /* No "remove all"s to worry about. */
355          list_remove_list(dest->multi_add[i], src->multi_remove[i]);
356          err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
357          if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
358       }
359
360       if (err)
361       {
362          return err;
363       }
364    }
365
366    return JB_ERR_OK;
367 }
368
369
370 /*********************************************************************
371  *
372  * Function    :  copy_action
373  *
374  * Description :  Copy an action_specs.
375  *                Similar to "dest = src".
376  *
377  * Parameters  :
378  *          1  :  dest = Destination of copy.
379  *          2  :  src = Source for copy.
380  *
381  * Returns     :  N/A
382  *
383  *********************************************************************/
384 jb_err copy_action (struct action_spec *dest,
385                     const struct action_spec *src)
386 {
387    int i;
388    jb_err err = JB_ERR_OK;
389
390    free_action(dest);
391    memset(dest, '\0', sizeof(*dest));
392
393    dest->mask = src->mask;
394    dest->add  = src->add;
395
396    for (i = 0; i < ACTION_STRING_COUNT; i++)
397    {
398       char * str = src->string[i];
399       if (str)
400       {
401          str = strdup(str);
402          if (!str)
403          {
404             return JB_ERR_MEMORY;
405          }
406          dest->string[i] = str;
407       }
408    }
409
410    for (i = 0; i < ACTION_MULTI_COUNT; i++)
411    {
412       dest->multi_remove_all[i] = src->multi_remove_all[i];
413       err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
414       if (err)
415       {
416          return err;
417       }
418       err = list_duplicate(dest->multi_add[i],    src->multi_add[i]);
419       if (err)
420       {
421          return err;
422       }
423    }
424    return err;
425 }
426
427
428 /*********************************************************************
429  *
430  * Function    :  free_action
431  *
432  * Description :  Destroy an action_spec.  Frees memory used by it,
433  *                except for the memory used by the struct action_spec
434  *                itself.
435  *
436  * Parameters  :
437  *          1  :  src = Source to free.
438  *
439  * Returns     :  N/A
440  *
441  *********************************************************************/
442 void free_action (struct action_spec *src)
443 {
444    int i;
445
446    if (src == NULL)
447    {
448       return;
449    }
450
451    for (i = 0; i < ACTION_STRING_COUNT; i++)
452    {
453       freez(src->string[i]);
454    }
455
456    for (i = 0; i < ACTION_MULTI_COUNT; i++)
457    {
458       destroy_list(src->multi_remove[i]);
459       destroy_list(src->multi_add[i]);
460    }
461
462    memset(src, '\0', sizeof(*src));
463 }
464
465
466 /*********************************************************************
467  *
468  * Function    :  get_action_token
469  *
470  * Description :  Parses a line for the first action.
471  *                Modifies it's input array, doesn't allocate memory.
472  *                e.g. given:
473  *                *line="  +abc{def}  -ghi "
474  *                Returns:
475  *                *line="  -ghi "
476  *                *name="+abc"
477  *                *value="def"
478  *
479  * Parameters  :
480  *          1  :  line = [in] The line containing the action.
481  *                       [out] Start of next action on line, or
482  *                       NULL if we reached the end of line before
483  *                       we found an action.
484  *          2  :  name = [out] Start of action name, null
485  *                       terminated.  NULL on EOL
486  *          3  :  value = [out] Start of action value, null
487  *                        terminated.  NULL if none or EOL.
488  *
489  * Returns     :  JB_ERR_OK => Ok
490  *                JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
491  *
492  *********************************************************************/
493 jb_err get_action_token(char **line, char **name, char **value)
494 {
495    char * str = *line;
496    char ch;
497
498    /* set default returns */
499    *line = NULL;
500    *name = NULL;
501    *value = NULL;
502
503    /* Eat any leading whitespace */
504    while ((*str == ' ') || (*str == '\t'))
505    {
506       str++;
507    }
508
509    if (*str == '\0')
510    {
511       return 0;
512    }
513
514    if (*str == '{')
515    {
516       /* null name, just value is prohibited */
517       return JB_ERR_PARSE;
518    }
519
520    *name = str;
521
522    /* parse option */
523    while (((ch = *str) != '\0') &&
524           (ch != ' ') && (ch != '\t') && (ch != '{'))
525    {
526       if (ch == '}')
527       {
528          /* error, '}' without '{' */
529          return JB_ERR_PARSE;
530       }
531       str++;
532    }
533    *str = '\0';
534
535    if (ch != '{')
536    {
537       /* no value */
538       if (ch == '\0')
539       {
540          /* EOL - be careful not to run off buffer */
541          *line = str;
542       }
543       else
544       {
545          /* More to parse next time. */
546          *line = str + 1;
547       }
548       return JB_ERR_OK;
549    }
550
551    str++;
552    *value = str;
553
554    str = strchr(str, '}');
555    if (str == NULL)
556    {
557       /* error */
558       *value = NULL;
559       return JB_ERR_PARSE;
560    }
561
562    /* got value */
563    *str = '\0';
564    *line = str + 1;
565
566    chomp(*value);
567
568    return JB_ERR_OK;
569 }
570
571
572 /*********************************************************************
573  *
574  * Function    :  get_actions
575  *
576  * Description :  Parses a list of actions.
577  *
578  * Parameters  :
579  *          1  :  line = The string containing the actions.
580  *                       Will be written to by this function.
581  *          2  :  alias_list = Custom alias list, or NULL for none.
582  *          3  :  cur_action = Where to store the action.  Caller
583  *                             allocates memory.
584  *
585  * Returns     :  JB_ERR_OK => Ok
586  *                JB_ERR_PARSE => Parse error (line was trashed anyway)
587  *                nonzero => Out of memory (line was trashed anyway)
588  *
589  *********************************************************************/
590 jb_err get_actions(char *line,
591                    struct action_alias * alias_list,
592                    struct action_spec *cur_action)
593 {
594    jb_err err;
595    init_action(cur_action);
596    cur_action->mask = ACTION_MASK_ALL;
597
598    while (line)
599    {
600       char * option = NULL;
601       char * value = NULL;
602
603       err = get_action_token(&line, &option, &value);
604       if (err)
605       {
606          return err;
607       }
608
609       if (option)
610       {
611          /* handle option in 'option' */
612
613          /* Check for standard action name */
614          const struct action_name * action = action_names;
615
616          while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
617          {
618             action++;
619          }
620          if (action->name != NULL)
621          {
622             /* Found it */
623             cur_action->mask &= action->mask;
624             cur_action->add  &= action->mask;
625             cur_action->add  |= action->add;
626
627             switch (action->takes_value)
628             {
629             case AV_NONE:
630                /* ignore any option. */
631                break;
632             case AV_ADD_STRING:
633                {
634                   /* add single string. */
635
636                   if ((value == NULL) || (*value == '\0'))
637                   {
638                      return JB_ERR_PARSE;
639                   }
640                   /* FIXME: should validate option string here */
641                   freez (cur_action->string[action->index]);
642                   cur_action->string[action->index] = strdup(value);
643                   if (NULL == cur_action->string[action->index])
644                   {
645                      return JB_ERR_MEMORY;
646                   }
647                   break;
648                }
649             case AV_REM_STRING:
650                {
651                   /* remove single string. */
652
653                   freez (cur_action->string[action->index]);
654                   break;
655                }
656             case AV_ADD_MULTI:
657                {
658                   /* append multi string. */
659
660                   struct list * remove_p = cur_action->multi_remove[action->index];
661                   struct list * add_p    = cur_action->multi_add[action->index];
662
663                   if ((value == NULL) || (*value == '\0'))
664                   {
665                      return JB_ERR_PARSE;
666                   }
667
668                   list_remove_item(remove_p, value);
669                   err = enlist_unique(add_p, value, 0);
670                   if (err)
671                   {
672                      return err;
673                   }
674                   break;
675                }
676             case AV_REM_MULTI:
677                {
678                   /* remove multi string. */
679
680                   struct list * remove_p = cur_action->multi_remove[action->index];
681                   struct list * add_p    = cur_action->multi_add[action->index];
682
683                   if ( (value == NULL) || (*value == '\0')
684                      || ((*value == '*') && (value[1] == '\0')) )
685                   {
686                      /*
687                       * no option, or option == "*".
688                       *
689                       * Remove *ALL*.
690                       */
691                      list_remove_all(remove_p);
692                      list_remove_all(add_p);
693                      cur_action->multi_remove_all[action->index] = 1;
694                   }
695                   else
696                   {
697                      /* Valid option - remove only 1 option */
698
699                      if ( !cur_action->multi_remove_all[action->index] )
700                      {
701                         /* there isn't a catch-all in the remove list already */
702                         err = enlist_unique(remove_p, value, 0);
703                         if (err)
704                         {
705                            return err;
706                         }
707                      }
708                      list_remove_item(add_p, value);
709                   }
710                   break;
711                }
712             default:
713                /* Shouldn't get here unless there's memory corruption. */
714                assert(0);
715                return JB_ERR_PARSE;
716             }
717          }
718          else
719          {
720             /* try user aliases. */
721             const struct action_alias * alias = alias_list;
722
723             while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
724             {
725                alias = alias->next;
726             }
727             if (alias != NULL)
728             {
729                /* Found it */
730                merge_actions(cur_action, alias->action);
731             }
732             else
733             {
734                /* Bad action name */
735                return JB_ERR_PARSE;
736             }
737          }
738       }
739    }
740
741    return JB_ERR_OK;
742 }
743
744
745 /*********************************************************************
746  *
747  * Function    :  init_current_action
748  *
749  * Description :  Zero out an action.
750  *
751  * Parameters  :
752  *          1  :  dest = An uninitialized current_action_spec.
753  *
754  * Returns     :  N/A
755  *
756  *********************************************************************/
757 void init_current_action (struct current_action_spec *dest)
758 {
759    memset(dest, '\0', sizeof(*dest));
760
761    dest->flags = ACTION_MOST_COMPATIBLE;
762 }
763
764
765 /*********************************************************************
766  *
767  * Function    :  init_action
768  *
769  * Description :  Zero out an action.
770  *
771  * Parameters  :
772  *          1  :  dest = An uninitialized action_spec.
773  *
774  * Returns     :  N/A
775  *
776  *********************************************************************/
777 void init_action (struct action_spec *dest)
778 {
779    memset(dest, '\0', sizeof(*dest));
780 }
781
782
783 /*********************************************************************
784  *
785  * Function    :  merge_current_action
786  *
787  * Description :  Merge two actions together.
788  *                Similar to "dest += src".
789  *                Differences between this and merge_actions()
790  *                is that this one doesn't allocate memory for
791  *                strings (so "src" better be in memory for at least
792  *                as long as "dest" is, and you'd better free
793  *                "dest" using "free_current_action").
794  *                Also, there is no  mask or remove lists in dest.
795  *                (If we're applying it to a URL, we don't need them)
796  *
797  * Parameters  :
798  *          1  :  dest = Current actions, to modify.
799  *          2  :  src = Action to add.
800  *
801  * Returns  0  :  no error
802  *        !=0  :  error, probably JB_ERR_MEMORY.
803  *
804  *********************************************************************/
805 jb_err merge_current_action (struct current_action_spec *dest,
806                              const struct action_spec *src)
807 {
808    int i;
809    jb_err err = JB_ERR_OK;
810
811    dest->flags  &= src->mask;
812    dest->flags  |= src->add;
813
814    for (i = 0; i < ACTION_STRING_COUNT; i++)
815    {
816       char * str = src->string[i];
817       if (str)
818       {
819          str = strdup(str);
820          if (!str)
821          {
822             return JB_ERR_MEMORY;
823          }
824          freez(dest->string[i]);
825          dest->string[i] = str;
826       }
827    }
828
829    for (i = 0; i < ACTION_MULTI_COUNT; i++)
830    {
831       if (src->multi_remove_all[i])
832       {
833          /* Remove everything from dest, then add src->multi_add */
834          err = list_duplicate(dest->multi[i], src->multi_add[i]);
835          if (err)
836          {
837             return err;
838          }
839       }
840       else
841       {
842          list_remove_list(dest->multi[i], src->multi_remove[i]);
843          err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
844          if (err)
845          {
846             return err;
847          }
848       }
849    }
850    return err;
851 }
852
853
854 /*********************************************************************
855  *
856  * Function    :  free_current_action
857  *
858  * Description :  Free memory used by a current_action_spec.
859  *                Does not free the current_action_spec itself.
860  *
861  * Parameters  :
862  *          1  :  src = Source to free.
863  *
864  * Returns     :  N/A
865  *
866  *********************************************************************/
867 void free_current_action (struct current_action_spec *src)
868 {
869    int i;
870
871    for (i = 0; i < ACTION_STRING_COUNT; i++)
872    {
873       freez(src->string[i]);
874    }
875
876    for (i = 0; i < ACTION_MULTI_COUNT; i++)
877    {
878       destroy_list(src->multi[i]);
879    }
880
881    memset(src, '\0', sizeof(*src));
882 }
883
884
885 static struct file_list *current_actions_file[MAX_AF_FILES]  = {
886    NULL, NULL, NULL, NULL, NULL,
887    NULL, NULL, NULL, NULL, NULL
888 };
889
890
891 #ifdef FEATURE_GRACEFUL_TERMINATION
892 /*********************************************************************
893  *
894  * Function    :  unload_current_actions_file
895  *
896  * Description :  Unloads current actions file - reset to state at
897  *                beginning of program.
898  *
899  * Parameters  :  None
900  *
901  * Returns     :  N/A
902  *
903  *********************************************************************/
904 void unload_current_actions_file(void)
905 {
906    int i;
907
908    for (i = 0; i < MAX_AF_FILES; i++)
909    {
910       if (current_actions_file[i])
911       {
912          current_actions_file[i]->unloader = unload_actions_file;
913          current_actions_file[i] = NULL;
914       }
915    }
916 }
917 #endif /* FEATURE_GRACEFUL_TERMINATION */
918
919
920 /*********************************************************************
921  *
922  * Function    :  unload_actions_file
923  *
924  * Description :  Unloads an actions module.
925  *
926  * Parameters  :
927  *          1  :  file_data = the data structure associated with the
928  *                            actions file.
929  *
930  * Returns     :  N/A
931  *
932  *********************************************************************/
933 void unload_actions_file(void *file_data)
934 {
935    struct url_actions * next;
936    struct url_actions * cur = (struct url_actions *)file_data;
937    while (cur != NULL)
938    {
939       next = cur->next;
940       free_url_spec(cur->url);
941       free_action(cur->action);
942       freez(cur);
943       cur = next;
944    }
945
946 }
947
948
949 /*********************************************************************
950  *
951  * Function    :  free_alias_list
952  *
953  * Description :  Free memory used by a list of aliases.
954  *
955  * Parameters  :
956  *          1  :  alias_list = Linked list to free.
957  *
958  * Returns     :  N/A
959  *
960  *********************************************************************/
961 void free_alias_list(struct action_alias *alias_list)
962 {
963    while (alias_list != NULL)
964    {
965       struct action_alias * next = alias_list->next;
966       alias_list->next = NULL;
967       freez(alias_list->name);
968       free_action(alias_list->action);
969       free(alias_list);
970       alias_list = next;
971    }
972 }
973
974
975 /*********************************************************************
976  *
977  * Function    :  load_actions_file
978  *
979  * Description :  Read and parse all the action files and add to files
980  *                list.
981  *
982  * Parameters  :
983  *          1  :  csp = Current client state (buffers, headers, etc...)
984  *
985  * Returns     :  0 => Ok, everything else is an error.
986  *
987  *********************************************************************/
988 int load_actions_file(struct client_state *csp)
989 {
990    int i;
991    int result;
992
993    for (i = 0; i < MAX_AF_FILES; i++)
994    {
995       if (csp->config->actions_file[i])
996       {
997          result = load_one_actions_file(csp, i);
998          if (result)
999          {
1000             return result;
1001          }
1002       }
1003       else if (current_actions_file[i])
1004       {
1005          current_actions_file[i]->unloader = unload_actions_file;
1006          current_actions_file[i] = NULL;
1007       }
1008    }
1009
1010    return 0;
1011 }
1012
1013 /*********************************************************************
1014  *
1015  * Function    :  load_one_actions_file
1016  *
1017  * Description :  Read and parse a action file and add to files
1018  *                list.
1019  *
1020  * Parameters  :
1021  *          1  :  csp = Current client state (buffers, headers, etc...)
1022  *          2  :  fileid = File index to load.
1023  *
1024  * Returns     :  0 => Ok, everything else is an error.
1025  *
1026  *********************************************************************/
1027 static int load_one_actions_file(struct client_state *csp, int fileid)
1028 {
1029
1030    /*
1031     * Parser mode.
1032     * Note: Keep these in the order they occur in the file, they are
1033     * sometimes tested with <=
1034     */
1035 #define MODE_START_OF_FILE 1
1036 #define MODE_SETTINGS      2
1037 #define MODE_DESCRIPTION   3
1038 #define MODE_ALIAS         4
1039 #define MODE_ACTIONS       5
1040
1041    int mode = MODE_START_OF_FILE;
1042
1043    FILE *fp;
1044    struct url_actions *last_perm;
1045    struct url_actions *perm;
1046    char  buf[BUFFER_SIZE];
1047    struct file_list *fs;
1048    struct action_spec * cur_action = NULL;
1049    int cur_action_used = 0;
1050    struct action_alias * alias_list = NULL;
1051    unsigned long linenum = 0;
1052
1053    if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1054    {
1055       /* No need to load */
1056       csp->actions_list[fileid] = current_actions_file[fileid];
1057       return 0;
1058    }
1059    if (!fs)
1060    {
1061       log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1062                 csp->config->actions_file[fileid]);
1063       return 1; /* never get here */
1064    }
1065
1066    fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1067    if (last_perm == NULL)
1068    {
1069       log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1070                 csp->config->actions_file[fileid]);
1071       return 1; /* never get here */
1072    }
1073
1074    if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1075    {
1076       log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1077                 csp->config->actions_file[fileid]);
1078       return 1; /* never get here */
1079    }
1080
1081    while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1082    {
1083       if (*buf == '{')
1084       {
1085          /* It's a header block */
1086          if (buf[1] == '{')
1087          {
1088             /* It's {{settings}} or {{alias}} */
1089             int len = strlen(buf);
1090             char * start = buf + 2;
1091             char * end = buf + len - 1;
1092             if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1093             {
1094                /* too short */
1095                fclose(fp);
1096                log_error(LOG_LEVEL_FATAL,
1097                   "can't load actions file '%s': invalid line (%lu): %s", 
1098                   csp->config->actions_file[fileid], linenum, buf);
1099                return 1; /* never get here */
1100             }
1101
1102             /* Trim leading and trailing whitespace. */
1103             end[1] = '\0';
1104             chomp(start);
1105
1106             if (*start == '\0')
1107             {
1108                /* too short */
1109                fclose(fp);
1110                log_error(LOG_LEVEL_FATAL,
1111                   "can't load actions file '%s': invalid line (%lu): {{ }}",
1112                   csp->config->actions_file[fileid], linenum);
1113                return 1; /* never get here */
1114             }
1115
1116             /*
1117              * An actionsfile can optionally contain the following blocks.
1118              * They *MUST* be in this order, to simplify processing:
1119              *
1120              * {{settings}}
1121              * name=value...
1122              *
1123              * {{description}}
1124              * ...free text, format TBD, but no line may start with a '{'...
1125              *
1126              * {{alias}}
1127              * name=actions...
1128              *
1129              * The actual actions must be *after* these special blocks.
1130              * None of these special blocks may be repeated.
1131              *
1132              */
1133             if (0 == strcmpic(start, "settings"))
1134             {
1135                /* it's a {{settings}} block */
1136                if (mode >= MODE_SETTINGS)
1137                {
1138                   /* {{settings}} must be first thing in file and must only
1139                    * appear once.
1140                    */
1141                   fclose(fp);
1142                   log_error(LOG_LEVEL_FATAL,
1143                      "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1144                      csp->config->actions_file[fileid], linenum);
1145                }
1146                mode = MODE_SETTINGS;
1147             }
1148             else if (0 == strcmpic(start, "description"))
1149             {
1150                /* it's a {{description}} block */
1151                if (mode >= MODE_DESCRIPTION)
1152                {
1153                   /* {{description}} is a singleton and only {{settings}} may proceed it
1154                    */
1155                   fclose(fp);
1156                   log_error(LOG_LEVEL_FATAL,
1157                      "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1158                      csp->config->actions_file[fileid], linenum);
1159                }
1160                mode = MODE_DESCRIPTION;
1161             }
1162             else if (0 == strcmpic(start, "alias"))
1163             {
1164                /* it's an {{alias}} block */
1165                if (mode >= MODE_ALIAS)
1166                {
1167                   /* {{alias}} must be first thing in file, possibly after
1168                    * {{settings}} and {{description}}
1169                    *
1170                    * {{alias}} must only appear once.
1171                    *
1172                    * Note that these are new restrictions introduced in
1173                    * v2.9.10 in order to make actionsfile editing simpler.
1174                    * (Otherwise, reordering actionsfile entries without
1175                    * completely rewriting the file becomes non-trivial)
1176                    */
1177                   fclose(fp);
1178                   log_error(LOG_LEVEL_FATAL,
1179                      "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1180                      csp->config->actions_file[fileid], linenum);
1181                }
1182                mode = MODE_ALIAS;
1183             }
1184             else
1185             {
1186                /* invalid {{something}} block */
1187                fclose(fp);
1188                log_error(LOG_LEVEL_FATAL,
1189                   "can't load actions file '%s': invalid line (%lu): {{%s}}",
1190                   csp->config->actions_file[fileid], linenum, start);
1191                return 1; /* never get here */
1192             }
1193          }
1194          else
1195          {
1196             /* It's an actions block */
1197
1198             char  actions_buf[BUFFER_SIZE];
1199             char * end;
1200
1201             /* set mode */
1202             mode    = MODE_ACTIONS;
1203
1204             /* free old action */
1205             if (cur_action)
1206             {
1207                if (!cur_action_used)
1208                {
1209                   free_action(cur_action);
1210                   free(cur_action);
1211                }
1212                cur_action = NULL;
1213             }
1214             cur_action_used = 0;
1215             cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1216             if (cur_action == NULL)
1217             {
1218                fclose(fp);
1219                log_error(LOG_LEVEL_FATAL,
1220                   "can't load actions file '%s': out of memory",
1221                   csp->config->actions_file[fileid]);
1222                return 1; /* never get here */
1223             }
1224             init_action(cur_action);
1225
1226             /* trim { */
1227             strcpy(actions_buf, buf + 1);
1228
1229             /* check we have a trailing } and then trim it */
1230             end = actions_buf + strlen(actions_buf) - 1;
1231             if (*end != '}')
1232             {
1233                /* No closing } */
1234                fclose(fp);
1235                log_error(LOG_LEVEL_FATAL,
1236                   "can't load actions file '%s': invalid line (%lu): %s",
1237                   csp->config->actions_file[fileid], linenum, buf);
1238                return 1; /* never get here */
1239             }
1240             *end = '\0';
1241
1242             /* trim any whitespace immediately inside {} */
1243             chomp(actions_buf);
1244
1245             if (get_actions(actions_buf, alias_list, cur_action))
1246             {
1247                /* error */
1248                fclose(fp);
1249                log_error(LOG_LEVEL_FATAL,
1250                   "can't load actions file '%s': invalid line (%lu): %s",
1251                   csp->config->actions_file[fileid], linenum, buf);
1252                return 1; /* never get here */
1253             }
1254          }
1255       }
1256       else if (mode == MODE_SETTINGS)
1257       {
1258          /*
1259           * Part of the {{settings}} block.
1260           * For now only serves to check if the file's minimum Privoxy
1261           * version requirement is met, but we may want to read & check
1262           * permissions when we go multi-user.
1263           */
1264          if (!strncmp(buf, "for-privoxy-version=", 20))
1265          {
1266             char *version_string, *fields[3];
1267             int num_fields;
1268
1269             if ((version_string = strdup(buf + 20)) == NULL)
1270             {
1271                fclose(fp);
1272                log_error(LOG_LEVEL_FATAL,
1273                          "can't load actions file '%s': out of memory!",
1274                          csp->config->actions_file[fileid]);
1275                return 1; /* never get here */
1276             }
1277             
1278             num_fields = ssplit(version_string, ".", fields, 3, TRUE, FALSE);
1279
1280             if (num_fields < 1 || atoi(fields[0]) == 0)
1281             {
1282                log_error(LOG_LEVEL_ERROR,
1283                  "While loading actions file '%s': invalid line (%lu): %s",
1284                   csp->config->actions_file[fileid], linenum, buf);
1285             }
1286             else if (                      atoi(fields[0]) > VERSION_MAJOR
1287                      || (num_fields > 1 && atoi(fields[1]) > VERSION_MINOR)
1288                      || (num_fields > 2 && atoi(fields[2]) > VERSION_POINT))
1289             {
1290                fclose(fp);
1291                log_error(LOG_LEVEL_FATAL,
1292                          "Actions file '%s', line %lu requires newer Privoxy version: %s",
1293                          csp->config->actions_file[fileid], linenum, buf );
1294                return 1; /* never get here */
1295             }
1296             free(version_string);
1297          }
1298       }
1299       else if (mode == MODE_DESCRIPTION)
1300       {
1301          /*
1302           * Part of the {{description}} block.
1303           * Ignore for now.
1304           */
1305       }
1306       else if (mode == MODE_ALIAS)
1307       {
1308          /*
1309           * define an alias
1310           */
1311          char  actions_buf[BUFFER_SIZE];
1312          struct action_alias * new_alias;
1313
1314          char * start = strchr(buf, '=');
1315          char * end = start;
1316
1317          if ((start == NULL) || (start == buf))
1318          {
1319             log_error(LOG_LEVEL_FATAL,
1320                "can't load actions file '%s': invalid alias line (%lu): %s",
1321                csp->config->actions_file[fileid], linenum, buf);
1322             return 1; /* never get here */
1323          }
1324
1325          if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1326          {
1327             fclose(fp);
1328             log_error(LOG_LEVEL_FATAL,
1329                "can't load actions file '%s': out of memory!",
1330                csp->config->actions_file[fileid]);
1331             return 1; /* never get here */
1332          }
1333
1334          /* Eat any the whitespace before the '=' */
1335          end--;
1336          while ((*end == ' ') || (*end == '\t'))
1337          {
1338             /*
1339              * we already know we must have at least 1 non-ws char
1340              * at start of buf - no need to check
1341              */
1342             end--;
1343          }
1344          end[1] = '\0';
1345
1346          /* Eat any the whitespace after the '=' */
1347          start++;
1348          while ((*start == ' ') || (*start == '\t'))
1349          {
1350             start++;
1351          }
1352          if (*start == '\0')
1353          {
1354             log_error(LOG_LEVEL_FATAL,
1355                "can't load actions file '%s': invalid alias line (%lu): %s",
1356                csp->config->actions_file[fileid], linenum, buf);
1357             return 1; /* never get here */
1358          }
1359
1360          if ((new_alias->name = strdup(buf)) == NULL)
1361          {
1362             fclose(fp);
1363             log_error(LOG_LEVEL_FATAL,
1364                "can't load actions file '%s': out of memory!",
1365                csp->config->actions_file[fileid]);
1366             return 1; /* never get here */
1367          }
1368
1369          strcpy(actions_buf, start);
1370
1371          if (get_actions(actions_buf, alias_list, new_alias->action))
1372          {
1373             /* error */
1374             fclose(fp);
1375             log_error(LOG_LEVEL_FATAL,
1376                "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1377                csp->config->actions_file[fileid], linenum, buf, start);
1378             return 1; /* never get here */
1379          }
1380
1381          /* add to list */
1382          new_alias->next = alias_list;
1383          alias_list = new_alias;
1384       }
1385       else if (mode == MODE_ACTIONS)
1386       {
1387          /* it's a URL pattern */
1388
1389          /* allocate a new node */
1390          if ((perm = zalloc(sizeof(*perm))) == NULL)
1391          {
1392             fclose(fp);
1393             log_error(LOG_LEVEL_FATAL,
1394                "can't load actions file '%s': out of memory!",
1395                csp->config->actions_file[fileid]);
1396             return 1; /* never get here */
1397          }
1398
1399          /* Save flags */
1400          copy_action (perm->action, cur_action);
1401
1402          /* Save the URL pattern */
1403          if (create_url_spec(perm->url, buf))
1404          {
1405             fclose(fp);
1406             log_error(LOG_LEVEL_FATAL,
1407                "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1408                csp->config->actions_file[fileid], linenum, buf);
1409             return 1; /* never get here */
1410          }
1411
1412          /* add it to the list */
1413          last_perm->next = perm;
1414          last_perm = perm;
1415       }
1416       else if (mode == MODE_START_OF_FILE)
1417       {
1418          /* oops - please have a {} line as 1st line in file. */
1419          fclose(fp);
1420          log_error(LOG_LEVEL_FATAL,
1421             "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1422             csp->config->actions_file[fileid], linenum, buf);
1423          return 1; /* never get here */
1424       }
1425       else
1426       {
1427          /* How did we get here? This is impossible! */
1428          fclose(fp);
1429          log_error(LOG_LEVEL_FATAL,
1430             "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1431             csp->config->actions_file[fileid], mode);
1432          return 1; /* never get here */
1433       }
1434    }
1435
1436    fclose(fp);
1437
1438    free_action(cur_action);
1439    freez(cur_action);
1440
1441    free_alias_list(alias_list);
1442
1443    /* the old one is now obsolete */
1444    if (current_actions_file[fileid])
1445    {
1446       current_actions_file[fileid]->unloader = unload_actions_file;
1447    }
1448
1449    fs->next    = files->next;
1450    files->next = fs;
1451    current_actions_file[fileid] = fs;
1452
1453    csp->actions_list[fileid] = fs;
1454
1455    return(0);
1456
1457 }
1458
1459
1460 /*********************************************************************
1461  *
1462  * Function    :  actions_to_text
1463  *
1464  * Description :  Converts a actionsfile entry from the internal
1465  *                structurt into a text line.  The output is split
1466  *                into one line for each action with line continuation. 
1467  *
1468  * Parameters  :
1469  *          1  :  action = The action to format.
1470  *
1471  * Returns     :  A string.  Caller must free it.
1472  *                NULL on out-of-memory error.
1473  *
1474  *********************************************************************/
1475 char * actions_to_text(struct action_spec *action)
1476 {
1477    unsigned mask = action->mask;
1478    unsigned add  = action->add;
1479    char * result = strdup("");
1480    struct list_entry * lst;
1481
1482    /* sanity - prevents "-feature +feature" */
1483    mask |= add;
1484
1485
1486 #define DEFINE_ACTION_BOOL(__name, __bit)          \
1487    if (!(mask & __bit))                            \
1488    {                                               \
1489       string_append(&result, " -" __name " \\\n"); \
1490    }                                               \
1491    else if (add & __bit)                           \
1492    {                                               \
1493       string_append(&result, " +" __name " \\\n"); \
1494    }
1495
1496 #define DEFINE_ACTION_STRING(__name, __bit, __index)   \
1497    if (!(mask & __bit))                                \
1498    {                                                   \
1499       string_append(&result, " -" __name " \\\n");     \
1500    }                                                   \
1501    else if (add & __bit)                               \
1502    {                                                   \
1503       string_append(&result, " +" __name "{");         \
1504       string_append(&result, action->string[__index]); \
1505       string_append(&result, "} \\\n");                \
1506    }
1507
1508 #define DEFINE_ACTION_MULTI(__name, __index)         \
1509    if (action->multi_remove_all[__index])            \
1510    {                                                 \
1511       string_append(&result, " -" __name " \\\n");   \
1512    }                                                 \
1513    else                                              \
1514    {                                                 \
1515       lst = action->multi_remove[__index]->first;    \
1516       while (lst)                                    \
1517       {                                              \
1518          string_append(&result, " -" __name "{");    \
1519          string_append(&result, lst->str);           \
1520          string_append(&result, "} \\\n");           \
1521          lst = lst->next;                            \
1522       }                                              \
1523    }                                                 \
1524    lst = action->multi_add[__index]->first;          \
1525    while (lst)                                       \
1526    {                                                 \
1527       string_append(&result, " +" __name "{");       \
1528       string_append(&result, lst->str);              \
1529       string_append(&result, "} \\\n");              \
1530       lst = lst->next;                               \
1531    }
1532
1533 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1534
1535 #include "actionlist.h"
1536
1537 #undef DEFINE_ACTION_MULTI
1538 #undef DEFINE_ACTION_STRING
1539 #undef DEFINE_ACTION_BOOL
1540 #undef DEFINE_ACTION_ALIAS
1541
1542    return result;
1543 }
1544
1545
1546 /*********************************************************************
1547  *
1548  * Function    :  actions_to_html
1549  *
1550  * Description :  Converts a actionsfile entry from numeric form
1551  *                ("mask" and "add") to a <br>-seperated HTML string
1552  *                in which each action is linked to its chapter in
1553  *                the user manual.
1554  *
1555  * Parameters  :
1556  *          1  :  csp    = Client state (for config)
1557  *          2  :  action = Action spec to be converted
1558  *
1559  * Returns     :  A string.  Caller must free it.
1560  *                NULL on out-of-memory error.
1561  *
1562  *********************************************************************/
1563 char * actions_to_html(struct client_state *csp,
1564                        struct action_spec *action)
1565 {
1566    unsigned mask = action->mask;
1567    unsigned add  = action->add;
1568    char * result = strdup("");
1569    struct list_entry * lst;
1570
1571    /* sanity - prevents "-feature +feature" */
1572    mask |= add;
1573
1574
1575 #define DEFINE_ACTION_BOOL(__name, __bit)       \
1576    if (!(mask & __bit))                         \
1577    {                                            \
1578       string_append(&result, "\n<br>-");        \
1579       string_join(&result, add_help_link(__name, csp->config)); \
1580    }                                            \
1581    else if (add & __bit)                        \
1582    {                                            \
1583       string_append(&result, "\n<br>+");        \
1584       string_join(&result, add_help_link(__name, csp->config)); \
1585    }
1586
1587 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1588    if (!(mask & __bit))                              \
1589    {                                                 \
1590       string_append(&result, "\n<br>-");             \
1591       string_join(&result, add_help_link(__name, csp->config)); \
1592    }                                                 \
1593    else if (add & __bit)                             \
1594    {                                                 \
1595       string_append(&result, "\n<br>+");             \
1596       string_join(&result, add_help_link(__name, csp->config)); \
1597       string_append(&result, "{");                   \
1598       string_join(&result, html_encode(action->string[__index])); \
1599       string_append(&result, "}");                   \
1600    }
1601
1602 #define DEFINE_ACTION_MULTI(__name, __index)          \
1603    if (action->multi_remove_all[__index])             \
1604    {                                                  \
1605       string_append(&result, "\n<br>-");              \
1606       string_join(&result, add_help_link(__name, csp->config)); \
1607    }                                                  \
1608    else                                               \
1609    {                                                  \
1610       lst = action->multi_remove[__index]->first;     \
1611       while (lst)                                     \
1612       {                                               \
1613          string_append(&result, "\n<br>-");           \
1614          string_join(&result, add_help_link(__name, csp->config)); \
1615          string_append(&result, "{");                 \
1616          string_join(&result, html_encode(lst->str)); \
1617          string_append(&result, "}");                 \
1618          lst = lst->next;                             \
1619       }                                               \
1620    }                                                  \
1621    lst = action->multi_add[__index]->first;           \
1622    while (lst)                                        \
1623    {                                                  \
1624       string_append(&result, "\n<br>+");              \
1625       string_join(&result, add_help_link(__name, csp->config)); \
1626       string_append(&result, "{");                    \
1627       string_join(&result, html_encode(lst->str));    \
1628       string_append(&result, "}");                    \
1629       lst = lst->next;                                \
1630    }
1631
1632 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1633
1634 #include "actionlist.h"
1635
1636 #undef DEFINE_ACTION_MULTI
1637 #undef DEFINE_ACTION_STRING
1638 #undef DEFINE_ACTION_BOOL
1639 #undef DEFINE_ACTION_ALIAS
1640
1641    /* trim leading <br> */
1642    if (result && *result)
1643    {
1644       char * s = result;
1645       result = strdup(result + 5);
1646       free(s);
1647    }
1648
1649    return result;
1650 }
1651
1652
1653 /*********************************************************************
1654  *
1655  * Function    :  current_actions_to_html
1656  *
1657  * Description :  Converts a curren action spec to a <br> seperated HTML
1658  *                text in which each action is linked to its chapter in
1659  *                the user manual.
1660  *
1661  * Parameters  :
1662  *          1  :  csp    = Client state (for config) 
1663  *          2  :  action = Current action spec to be converted
1664  *
1665  * Returns     :  A string.  Caller must free it.
1666  *                NULL on out-of-memory error.
1667  *
1668  *********************************************************************/
1669 char *current_action_to_html(struct client_state *csp,
1670                              struct current_action_spec *action)
1671 {
1672    unsigned long flags  = action->flags;
1673    char * result = strdup("");
1674    struct list_entry * lst;
1675
1676 #define DEFINE_ACTION_BOOL(__name, __bit)  \
1677    if (flags & __bit)                      \
1678    {                                       \
1679       string_append(&result, "\n<br>+");   \
1680       string_join(&result, add_help_link(__name, csp->config)); \
1681    }                                       \
1682    else                                    \
1683    {                                       \
1684       string_append(&result, "\n<br>-");   \
1685       string_join(&result, add_help_link(__name, csp->config)); \
1686    }
1687
1688 #define DEFINE_ACTION_STRING(__name, __bit, __index)   \
1689    if (flags & __bit)                                  \
1690    {                                                   \
1691       string_append(&result, "\n<br>+");               \
1692       string_join(&result, add_help_link(__name, csp->config)); \
1693       string_append(&result, "{");                     \
1694       string_join(&result, html_encode(action->string[__index])); \
1695       string_append(&result, "}");                     \
1696    }                                                   \
1697    else                                                \
1698    {                                                   \
1699       string_append(&result, "\n<br>-");               \
1700       string_join(&result, add_help_link(__name, csp->config)); \
1701    }
1702
1703 #define DEFINE_ACTION_MULTI(__name, __index)           \
1704    lst = action->multi[__index]->first;                \
1705    if (lst == NULL)                                    \
1706    {                                                   \
1707       string_append(&result, "\n<br>-");              \
1708       string_join(&result, add_help_link(__name, csp->config)); \
1709    }                                                   \
1710    else                                                \
1711    {                                                   \
1712       while (lst)                                      \
1713       {                                                \
1714          string_append(&result, "\n<br>+");            \
1715          string_join(&result, add_help_link(__name, csp->config)); \
1716          string_append(&result, "{");                  \
1717          string_join(&result, html_encode(lst->str));  \
1718          string_append(&result, "}");                  \
1719          lst = lst->next;                              \
1720       }                                                \
1721    }
1722
1723 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1724
1725 #include "actionlist.h"
1726
1727 #undef DEFINE_ACTION_MULTI
1728 #undef DEFINE_ACTION_STRING
1729 #undef DEFINE_ACTION_BOOL
1730 #undef DEFINE_ACTION_ALIAS
1731
1732    return result;
1733 }