~ubuntu-branches/ubuntu/maverick/texinfo/maverick

« back to all changes in this revision

Viewing changes to makeinfo/macro.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2005-10-28 15:10:30 UTC
  • mto: (2.1.1 dapper) (3.1.4 hardy)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20051028151030-9nsf2s2k2z3fktjt
Tags: upstream-4.8
ImportĀ upstreamĀ versionĀ 4.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* macro.c -- user-defined macros for Texinfo.
2
 
   $Id: macro.c,v 1.12 2002/03/02 15:05:21 karl Exp $
 
2
   $Id: macro.c,v 1.6 2004/04/11 17:56:47 karl Exp $
3
3
 
4
 
   Copyright (C) 1998, 99, 2002 Free Software Foundation, Inc.
 
4
   Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
5
5
 
6
6
   This program is free software; you can redistribute it and/or modify
7
7
   it under the terms of the GNU General Public License as published by
19
19
 
20
20
#include "system.h"
21
21
#include "cmds.h"
 
22
#include "files.h"
22
23
#include "macro.h"
23
24
#include "makeinfo.h"
24
25
#include "insertion.h"
54
55
 
55
56
/* Return the length of the array in ARRAY. */
56
57
int
57
 
array_len (array)
58
 
     char **array;
 
58
array_len (char **array)
59
59
{
60
60
  int i = 0;
61
61
 
66
66
}
67
67
 
68
68
void
69
 
free_array (array)
70
 
     char **array;
 
69
free_array (char **array)
71
70
{
72
71
  if (array)
73
72
    {
81
80
 
82
81
/* Return the macro definition of NAME or NULL if NAME is not defined. */
83
82
MACRO_DEF *
84
 
find_macro (name)
85
 
     char *name;
 
83
find_macro (char *name)
86
84
{
87
85
  int i;
88
86
  MACRO_DEF *def;
101
99
   and SOURCE_LINENO is the line number within that file.  If a macro already
102
100
   exists with NAME, then a warning is produced, and that previous
103
101
   definition is overwritten. */
104
 
void
105
 
add_macro (name, arglist, body, source_file, source_lineno, flags)
106
 
     char *name;
107
 
     char **arglist;
108
 
     char *body;
109
 
     char *source_file;
110
 
     int source_lineno, flags;
 
102
static void
 
103
add_macro (char *name, char **arglist, char *body, char *source_file,
 
104
    int source_lineno, int flags)
111
105
{
112
106
  MACRO_DEF *def;
113
107
 
163
157
 
164
158
 
165
159
char **
166
 
get_brace_args (quote_single)
167
 
     int quote_single;
 
160
get_brace_args (int quote_single)
168
161
{
169
162
  char **arglist, *word;
170
163
  int arglist_index, arglist_size;
243
236
  return arglist;
244
237
}
245
238
 
246
 
char **
247
 
get_macro_args (def)
248
 
    MACRO_DEF *def;
 
239
static char **
 
240
get_macro_args (MACRO_DEF *def)
249
241
{
250
242
  int i;
251
243
  char *word;
298
290
/* Substitute actual parameters for named parameters in body.
299
291
   The named parameters which appear in BODY must by surrounded
300
292
   reverse slashes, as in \foo\. */
301
 
char *
302
 
apply (named, actuals, body)
303
 
     char **named, **actuals, *body;
 
293
static char *
 
294
apply (char **named, char **actuals, char *body)
304
295
{
305
296
  int i;
306
297
  int new_body_index, new_body_size;
322
313
      else
323
314
        { /* Snarf parameter name, check against named parameters. */
324
315
          char *param;
325
 
          int param_start, which, len;
 
316
          int param_start, len;
326
317
 
327
318
          param_start = ++i;
328
319
          while (body[i] && body[i] != '\\')
336
327
          if (body[i]) /* move past \ */
337
328
            i++;
338
329
 
339
 
          /* Now check against named parameters. */
340
 
          for (which = 0; named && named[which]; which++)
341
 
            if (STREQ (named[which], param))
342
 
              break;
343
 
 
344
 
          if (named && named[which])
345
 
            {
346
 
              text = which < length_of_actuals ? actuals[which] : NULL;
347
 
              if (!text)
348
 
                text = "";
349
 
              len = strlen (text);
350
 
            }
351
 
          else
352
 
            { /* not a parameter, either it's \\ (if len==0) or an
353
 
                 error.  In either case, restore one \ at least.  */
354
 
              if (len) {
355
 
                warning (_("\\ in macro expansion followed by `%s' instead of \\ or parameter name"),
356
 
                         param); 
357
 
              }
 
330
          if (len == 0)
 
331
            { /* \\ always means \, even if macro has no args.  */
358
332
              len++;
359
333
              text = xmalloc (1 + len);
360
334
              sprintf (text, "\\%s", param);
361
335
            }
 
336
          else
 
337
            {
 
338
              int which;
 
339
              
 
340
              /* Check against named parameters. */
 
341
              for (which = 0; named && named[which]; which++)
 
342
                if (STREQ (named[which], param))
 
343
                  break;
 
344
 
 
345
              if (named && named[which])
 
346
                {
 
347
                  text = which < length_of_actuals ? actuals[which] : NULL;
 
348
                  if (!text)
 
349
                    text = "";
 
350
                  len = strlen (text);
 
351
                  text = xstrdup (text);  /* so we can free it */
 
352
                }
 
353
              else
 
354
                { /* not a parameter, so it's an error.  */
 
355
                  warning (_("\\ in macro expansion followed by `%s' instead of parameter name"),
 
356
                             param); 
 
357
                  len++;
 
358
                  text = xmalloc (1 + len);
 
359
                  sprintf (text, "\\%s", param);
 
360
                }
 
361
            }
362
362
 
363
363
          if (strlen (param) + 2 < len)
364
364
            {
371
371
          strcpy (new_body + new_body_index, text);
372
372
          new_body_index += len;
373
373
 
374
 
          if (!named || !named[which])
375
 
            free (text);
 
374
          free (text);
376
375
        }
377
376
    }
378
377
 
383
382
/* Expand macro passed in DEF, a pointer to a MACRO_DEF, and
384
383
   return its expansion as a string.  */
385
384
char *
386
 
expand_macro (def)
387
 
     MACRO_DEF *def;
 
385
expand_macro (MACRO_DEF *def)
388
386
{
389
387
  char **arglist;
390
388
  int num_args;
414
412
 
415
413
/* Execute the macro passed in DEF, a pointer to a MACRO_DEF.  */
416
414
void
417
 
execute_macro (def)
418
 
     MACRO_DEF *def;
 
415
execute_macro (MACRO_DEF *def)
419
416
{
420
417
  char *execution_string;
421
418
  int start_line = line_number, end_line;
436
433
      end_line = line_number;
437
434
      line_number = start_line;
438
435
 
439
 
      if (macro_expansion_output_stream && !executing_string && !me_inhibit_expansion)
 
436
      if (macro_expansion_output_stream
 
437
          && !executing_string && !me_inhibit_expansion)
440
438
        {
441
439
          remember_itext (input_text, input_text_offset);
442
440
          me_execute_string (execution_string);
454
452
   set the ME_RECURSE flag.  MACTYPE is either "macro" or "rmacro", and
455
453
   tells us what the matching @end should be.  */
456
454
static void
457
 
define_macro (mactype, recursive)
458
 
     char *mactype;
459
 
     int recursive;
 
455
define_macro (char *mactype, int recursive)
460
456
{
461
 
  int i;
462
 
  char *name, **arglist, *body, *line, *last_end;
463
 
  int body_size, body_index;
 
457
  int i, start;
 
458
  char *name, *line;
 
459
  char *last_end = NULL;
 
460
  char *body = NULL;
 
461
  char **arglist = NULL;
 
462
  int body_size = 0, body_index = 0;
464
463
  int depth = 1;
 
464
  int flags = 0;
465
465
  int defining_line = line_number;
466
 
  int flags = 0;
467
 
 
468
 
  arglist = NULL;
469
 
  body = NULL;
470
 
  body_size = 0;
471
 
  body_index = 0;
472
466
 
473
467
  if (macro_expansion_output_stream && !executing_string)
474
468
    me_append_before_this_command ();
477
471
 
478
472
  /* Get the name of the macro.  This is the set of characters which are
479
473
     not whitespace and are not `{' immediately following the @macro. */
 
474
  start = input_text_offset;
480
475
  {
481
 
    int start = input_text_offset;
482
476
    int len;
483
477
 
484
 
    for (i = start;
485
 
         (i < input_text_length) &&
486
 
         (input_text[i] != '{') &&
487
 
         (!cr_or_whitespace (input_text[i]));
488
 
         i++);
 
478
    for (i = start; i < input_text_length && input_text[i] != '{'
 
479
                    && !cr_or_whitespace (input_text[i]);
 
480
         i++) ;
489
481
 
490
482
    len = i - start;
491
483
    name = xmalloc (1 + len);
645
637
          depth--;
646
638
          last_end = "macro";
647
639
        }
648
 
      if (*line == COMMAND_PREFIX && strncmp (line + 1, "end rmacro", 9) == 0)
 
640
      if (*line == COMMAND_PREFIX && strncmp (line + 1, "end rmacro", 10) == 0)
649
641
        {
650
642
          depth--;
651
643
          last_end = "rmacro";
689
681
  add_macro (name, arglist, body, input_filename, defining_line, flags);
690
682
 
691
683
  if (macro_expansion_output_stream && !executing_string)
692
 
    remember_itext (input_text, input_text_offset);
 
684
    {
 
685
      /* Remember text for future expansions.  */
 
686
      remember_itext (input_text, input_text_offset);
 
687
 
 
688
      /* Bizarrely, output the @macro itself.  This is so texinfo.tex
 
689
         will have a chance to read it when texi2dvi calls makeinfo -E.
 
690
         The problem is that we don't really expand macros in all
 
691
         contexts; a @table's @item is one.  And a fix is not obvious to
 
692
         me, since it appears virtually identical to any other internal
 
693
         expansion.  Just setting a variable in cm_item caused other
 
694
         strange expansion problems.  */
 
695
      write_region_to_macro_output ("@", 0, 1);
 
696
      write_region_to_macro_output (mactype, 0, strlen (mactype));
 
697
      write_region_to_macro_output (" ", 0, 1);
 
698
      write_region_to_macro_output (input_text, start, input_text_offset);
 
699
    }
693
700
}
694
701
 
695
702
void 
696
 
cm_macro ()
 
703
cm_macro (void)
697
704
{
698
705
  define_macro ("macro", 0);
699
706
}
700
707
 
701
708
void 
702
 
cm_rmacro ()
 
709
cm_rmacro (void)
703
710
{
704
711
  define_macro ("rmacro", 1);
705
712
}
709
716
   returned. */
710
717
 
711
718
static MACRO_DEF *
712
 
delete_macro (name)
713
 
     char *name;
 
719
delete_macro (char *name)
714
720
{
715
721
  int i;
716
722
  MACRO_DEF *def;
729
735
}
730
736
 
731
737
void
732
 
cm_unmacro ()
 
738
cm_unmacro (void)
733
739
{
734
740
  int i;
735
741
  char *line, *name;
777
783
 
778
784
/* Set the value of POINTER's offset to OFFSET. */
779
785
ITEXT *
780
 
remember_itext (pointer, offset)
781
 
     char *pointer;
782
 
     int offset;
 
786
remember_itext (char *pointer, int offset)
783
787
{
784
788
  int i;
785
789
  ITEXT *itext = NULL;
833
837
 
834
838
/* Forget the input text associated with POINTER. */
835
839
void
836
 
forget_itext (pointer)
837
 
     char *pointer;
 
840
forget_itext (char *pointer)
838
841
{
839
842
  int i;
840
843
 
850
853
/* Append the text which appeared in input_text from the last offset to
851
854
   the character just before the command that we are currently executing. */
852
855
void
853
 
me_append_before_this_command ()
 
856
me_append_before_this_command (void)
854
857
{
855
858
  int i;
856
859
 
862
865
/* Similar to execute_string, but only takes a single string argument,
863
866
   and remembers the input text location, etc. */
864
867
void
865
 
me_execute_string (execution_string)
866
 
     char *execution_string;
 
868
me_execute_string (char *execution_string)
867
869
{
868
870
  int saved_escape_html = escape_html;
869
871
  int saved_in_paragraph = in_paragraph;
895
897
   when we need to produce macro-expanded output for input which
896
898
   leaves no traces in the Info output.  */
897
899
void
898
 
me_execute_string_keep_state (execution_string, append_string)
899
 
     char *execution_string, *append_string;
 
900
me_execute_string_keep_state (char *execution_string, char *append_string)
900
901
{
901
902
  int op_orig, opcol_orig, popen_orig;
902
903
  int fill_orig, newline_orig, indent_orig, meta_pos_orig;
926
927
/* Append the text which appears in input_text from the last offset to
927
928
   the current OFFSET. */
928
929
void
929
 
append_to_expansion_output (offset)
930
 
     int offset;
 
930
append_to_expansion_output (int offset)
931
931
{
932
932
  int i;
933
933
  ITEXT *itext = NULL;
951
951
 
952
952
/* Only write this input text iff it appears in our itext list. */
953
953
void
954
 
maybe_write_itext (pointer, offset)
955
 
     char *pointer;
956
 
     int offset;
 
954
maybe_write_itext (char *pointer, int offset)
957
955
{
958
956
  int i;
959
957
  ITEXT *itext = NULL;
973
971
}
974
972
 
975
973
void
976
 
write_region_to_macro_output (string, start, end)
977
 
     char *string;
978
 
     int start, end;
 
974
write_region_to_macro_output (char *string, int start, int end)
979
975
{
980
976
  if (macro_expansion_output_stream)
981
977
    fwrite (string + start, 1, end - start, macro_expansion_output_stream);
992
988
 
993
989
static alias_type *aliases; 
994
990
 
995
 
/* @alias */
 
991
/* @alias aname = cmdname */
 
992
 
996
993
void
997
 
cm_alias ()
 
994
cm_alias (void)
998
995
{
999
996
  alias_type *a = xmalloc (sizeof (alias_type));
1000
997
 
1001
998
  skip_whitespace ();
1002
 
  get_until_in_line (1, "=", &(a->alias));
 
999
  get_until_in_line (0, "=", &(a->alias));
1003
1000
  canon_white (a->alias);
1004
1001
 
1005
1002
  discard_until ("=");
1012
1009
 
1013
1010
/* Perform an alias expansion.  Called from read_command.  */
1014
1011
char *
1015
 
alias_expand (tok)
1016
 
     char *tok;
 
1012
alias_expand (char *tok)
1017
1013
{
1018
1014
  alias_type *findit = aliases;
1019
1015
 
1054
1050
 
1055
1051
/* @definfoenclose */
1056
1052
void
1057
 
cm_definfoenclose ()
 
1053
cm_definfoenclose (void)
1058
1054
{
1059
1055
  enclosure_type *e = xmalloc (sizeof (enclosure_type));
1060
1056
 
1073
1069
   return 1.  Else return 0.  */
1074
1070
 
1075
1071
int
1076
 
enclosure_command (tok)
1077
 
     char *tok;
 
1072
enclosure_command (char *tok)
1078
1073
{
1079
1074
  enclosure_type *findit = enclosures;
1080
1075
 
1096
1091
 
1097
1092
/* actually perform the enclosure expansion */
1098
1093
void
1099
 
enclosure_expand (arg, start, end)
1100
 
     int arg, start, end;
 
1094
enclosure_expand (int arg, int start, int end)
1101
1095
{
1102
1096
  if (arg == START)
1103
1097
    add_word (enclosure_stack->current->before);