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