~ubuntu-branches/ubuntu/gutsy/m4/gutsy

« back to all changes in this revision

Viewing changes to src/m4.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2006-11-29 16:53:44 UTC
  • Revision ID: james.westby@ubuntu.com-20061129165344-0qimyyyhh135a7mf
Tags: 1.4.8-1
New upstream release. Lots of fixes, see the NEWS file for details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
struct macro_definition
66
66
{
67
67
  struct macro_definition *next;
68
 
  int code;                     /* D, U or t */
69
 
  const char *macro;
 
68
  int code;                     /* D, U, s, t, or '\1' */
 
69
  const char *arg;
70
70
};
71
71
typedef struct macro_definition macro_definition;
72
72
 
204
204
", stdout);
205
205
      printf ("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
206
206
    }
207
 
 
208
 
  if (close_stream (stdout) != 0)
209
 
    M4ERROR ((EXIT_FAILURE, errno, "write error"));
210
207
  exit (status);
211
208
}
212
209
 
260
257
   where we try to continue execution in the meantime.  */
261
258
int retcode;
262
259
 
 
260
/* Process a command line file NAME, and return true only if it was
 
261
   stdin.  */
 
262
static bool
 
263
process_file (const char *name)
 
264
{
 
265
  bool result = false;
 
266
  if (strcmp (name, "-") == 0)
 
267
    {
 
268
      /* If stdin is a terminal, we want to allow 'm4 - file -'
 
269
         to read input from stdin twice, like GNU cat.  Besides,
 
270
         there is no point closing stdin before wrapped text, to
 
271
         minimize bugs in syscmd called from wrapped text.  */
 
272
      push_file (stdin, "stdin", false);
 
273
      result = true;
 
274
    }
 
275
  else
 
276
    {
 
277
      char *full_name;
 
278
      FILE *fp = m4_path_search (name, &full_name);
 
279
      if (fp == NULL)
 
280
        {
 
281
          error (0, errno, "%s", name);
 
282
          /* Set the status to EXIT_FAILURE, even though we
 
283
             continue to process files after a missing file.  */
 
284
          retcode = EXIT_FAILURE;
 
285
          return false;
 
286
        }
 
287
      push_file (fp, full_name, true);
 
288
      free (full_name);
 
289
    }
 
290
  expand_input ();
 
291
  return result;
 
292
}
 
293
 
 
294
/* POSIX requires only -D, -U, and -s; and says that the first two
 
295
   must be recognized when interspersed with file names.  Traditional
 
296
   behavior also handles -s between files.  Starting OPTSTRING with
 
297
   '-' forces getopt_long to hand back file names as arguments to opt
 
298
   '\1', rather than reordering the command line.  */
263
299
#ifdef ENABLE_CHANGEWORD
264
 
#define OPTSTRING "B:D:EF:GH:I:L:N:PQR:S:T:U:W:d::eil:o:st:"
 
300
#define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:W:d::eil:o:st:"
265
301
#else
266
 
#define OPTSTRING "B:D:EF:GH:I:L:N:PQR:S:T:U:d::eil:o:st:"
 
302
#define OPTSTRING "-B:D:EF:GH:I:L:N:PQR:S:T:U:d::eil:o:st:"
267
303
#endif
268
304
 
269
305
int
271
307
{
272
308
  macro_definition *head;       /* head of deferred argument list */
273
309
  macro_definition *tail;
274
 
  macro_definition *new;
 
310
  macro_definition *defn;
275
311
  int optchar;                  /* option character */
276
312
 
277
313
  macro_definition *defines;
278
 
  FILE *fp;
279
 
  boolean read_stdin = FALSE;
280
 
  boolean interactive = FALSE;
 
314
  bool read_stdin = false;
 
315
  bool interactive = false;
 
316
  bool seen_file = false;
 
317
  const char *debugfile = NULL;
281
318
  const char *frozen_file_to_read = NULL;
282
319
  const char *frozen_file_to_write = NULL;
283
320
 
284
321
  program_name = argv[0];
285
322
  retcode = EXIT_SUCCESS;
 
323
  atexit (close_stdout);
286
324
 
287
325
  include_init ();
288
326
  debug_init ();
294
332
 
295
333
  head = tail = NULL;
296
334
 
297
 
  while (optchar = getopt_long (argc, (char **) argv, OPTSTRING,
298
 
                                long_options, NULL),
299
 
         optchar != EOF)
 
335
  while ((optchar = getopt_long (argc, (char **) argv, OPTSTRING,
 
336
                                 long_options, NULL)) != -1)
300
337
    switch (optchar)
301
338
      {
302
339
      default:
320
357
 
321
358
      case 'D':
322
359
      case 'U':
 
360
      case 's':
323
361
      case 't':
324
 
 
 
362
      case '\1':
325
363
        /* Arguments that cannot be handled until later are accumulated.  */
326
364
 
327
 
        new = (macro_definition *) xmalloc (sizeof (macro_definition));
328
 
        new->code = optchar;
329
 
        new->macro = optarg;
330
 
        new->next = NULL;
 
365
        defn = (macro_definition *) xmalloc (sizeof (macro_definition));
 
366
        defn->code = optchar;
 
367
        defn->arg = optarg;
 
368
        defn->next = NULL;
331
369
 
332
370
        if (head == NULL)
333
 
          head = new;
 
371
          head = defn;
334
372
        else
335
 
          tail->next = new;
336
 
        tail = new;
 
373
          tail->next = defn;
 
374
        tail = defn;
337
375
 
338
376
        break;
339
377
 
394
432
        error (0, 0, "Warning: `m4 -e' is deprecated, use `-i' instead");
395
433
        /* fall through */
396
434
      case 'i':
397
 
        interactive = TRUE;
 
435
        interactive = true;
398
436
        break;
399
437
 
400
438
      case 'l':
409
447
           or later is more widely established, as such a warning
410
448
           would interfere with all earlier versions of autoconf.  */
411
449
      case DEBUGFILE_OPTION:
412
 
        if (!debug_set_output (optarg))
413
 
          error (0, errno, "%s", optarg);
414
 
        break;
415
 
 
416
 
      case 's':
417
 
        sync_output = 1;
 
450
        /* Don't call debug_set_output here, as it has side effects.  */
 
451
        debugfile = optarg;
418
452
        break;
419
453
 
420
454
      case VERSION_OPTION:
421
 
         printf ("%s\n", PACKAGE_STRING);
422
 
         fputs ("\
 
455
        printf ("%s\n", PACKAGE_STRING);
 
456
        fputs ("\
423
457
Copyright (C) 2006 Free Software Foundation, Inc.\n\
424
458
This is free software; see the source for copying conditions.  There is NO\n\
425
459
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
426
460
\n\
427
461
Written by Rene' Seindal.\n\
428
462
", stdout);
429
 
 
430
 
         if (close_stream (stdout) != 0)
431
 
            M4ERROR ((EXIT_FAILURE, errno, "write error"));
432
 
         exit (EXIT_SUCCESS);
 
463
        exit (EXIT_SUCCESS);
433
464
        break;
434
465
 
435
466
      case HELP_OPTION:
439
470
 
440
471
  defines = head;
441
472
 
442
 
  /* Do the basic initialisations.  */
 
473
  /* Do the basic initializations.  */
 
474
  if (debugfile && !debug_set_output (debugfile))
 
475
    M4ERROR ((0, errno, "cannot set debug file `%s'", debugfile));
443
476
 
444
477
  input_init ();
445
478
  output_init ();
451
484
  else
452
485
    builtin_init ();
453
486
 
 
487
  /* Interactive mode means unbuffered output, and interrupts ignored.  */
 
488
 
 
489
  if (interactive)
 
490
    {
 
491
      signal (SIGINT, SIG_IGN);
 
492
      setbuf (stdout, (char *) NULL);
 
493
    }
 
494
 
454
495
  /* Handle deferred command line macro definitions.  Must come after
455
 
     initialisation of the symbol table.  */
 
496
     initialization of the symbol table.  */
456
497
 
457
498
  while (defines != NULL)
458
499
    {
459
500
      macro_definition *next;
460
 
      char *macro_value;
461
501
      symbol *sym;
462
502
 
463
503
      switch (defines->code)
464
504
        {
465
505
        case 'D':
466
 
          macro_value = strchr (defines->macro, '=');
467
 
          if (macro_value == NULL)
468
 
            macro_value = "";
469
 
          else
470
 
            *macro_value++ = '\0';
471
 
          define_user_macro (defines->macro, macro_value, SYMBOL_INSERT);
 
506
          {
 
507
            /* defines->arg is read-only, so we need a copy.  */
 
508
            char *macro_name = xstrdup (defines->arg);
 
509
            char *macro_value = strchr (macro_name, '=');
 
510
            if (macro_value)
 
511
              *macro_value++ = '\0';
 
512
            define_user_macro (macro_name, macro_value, SYMBOL_INSERT);
 
513
            free (macro_name);
 
514
          }
472
515
          break;
473
516
 
474
517
        case 'U':
475
 
          lookup_symbol (defines->macro, SYMBOL_DELETE);
 
518
          lookup_symbol (defines->arg, SYMBOL_DELETE);
476
519
          break;
477
520
 
478
521
        case 't':
479
 
          sym = lookup_symbol (defines->macro, SYMBOL_INSERT);
480
 
          SYMBOL_TRACED (sym) = TRUE;
 
522
          sym = lookup_symbol (defines->arg, SYMBOL_INSERT);
 
523
          SYMBOL_TRACED (sym) = true;
 
524
          break;
 
525
 
 
526
        case 's':
 
527
          sync_output = 1;
 
528
          break;
 
529
 
 
530
        case '\1':
 
531
          seen_file = true;
 
532
          if (process_file (defines->arg))
 
533
            read_stdin = true;
481
534
          break;
482
535
 
483
536
        default:
491
544
      defines = next;
492
545
    }
493
546
 
494
 
  /* Interactive mode means unbuffered output, and interrupts ignored.  */
495
 
 
496
 
  if (interactive)
497
 
    {
498
 
      signal (SIGINT, SIG_IGN);
499
 
      setbuf (stdout, (char *) NULL);
500
 
    }
501
 
 
502
 
  /* Handle the various input files.  Each file is pushed on the input,
 
547
  /* Handle remaining input files.  Each file is pushed on the input,
503
548
     and the input read.  Wrapup text is handled separately later.  */
504
549
 
505
 
  if (optind == argc)
506
 
    {
507
 
      /* No point closing stdin until after wrapped text is
508
 
         processed.  */
509
 
      push_file (stdin, "stdin", FALSE);
510
 
      read_stdin = TRUE;
511
 
      expand_input ();
512
 
    }
 
550
  if (optind == argc && !seen_file)
 
551
    read_stdin = process_file ("-");
513
552
  else
514
553
    for (; optind < argc; optind++)
515
 
      {
516
 
        if (strcmp (argv[optind], "-") == 0)
517
 
          {
518
 
            /* If stdin is a terminal, we want to allow 'm4 - file -'
519
 
               to read input from stdin twice, like GNU cat.  Besides,
520
 
               there is no point closing stdin before wrapped text, to
521
 
               minimize bugs in syscmd called from wrapped text.  */
522
 
            push_file (stdin, "stdin", FALSE);
523
 
            read_stdin = TRUE;
524
 
          }
525
 
        else
526
 
          {
527
 
            const char *name;
528
 
            fp = path_search (argv[optind], &name);
529
 
            if (fp == NULL)
530
 
              {
531
 
                error (0, errno, "%s", argv[optind]);
532
 
                /* Set the status to EXIT_FAILURE, even though we
533
 
                   continue to process files after a missing file.  */
534
 
                retcode = EXIT_FAILURE;
535
 
                continue;
536
 
              }
537
 
            push_file (fp, name, TRUE);
538
 
            free ((char *) name);
539
 
          }
540
 
        expand_input ();
541
 
      }
542
 
#undef NEXTARG
 
554
      if (process_file (defines->arg))
 
555
        read_stdin = true;
543
556
 
544
557
  /* Now handle wrapup text.  */
545
558
 
563
576
      make_diversion (0);
564
577
      undivert_all ();
565
578
    }
566
 
 
567
 
  if (close_stream (stdout) != 0)
568
 
    M4ERROR ((EXIT_FAILURE, errno, "write error"));
 
579
  output_exit ();
569
580
  exit (retcode);
570
581
}