Changeset 5488:31c9e9727d42

Show
Ignore:
Timestamp:
2008-08-19 13:44:26 (4 months ago)
Author:
Aron Griffis <agriffis@…>
Branch:
HEAD
Message:

Handle -- correctly with or without -a, closes #3097

Prefix optstring with "+" to force POSIX behavior on GNU getopt, and thereby
prevent reordering argv. This allows us to correctly handle mixed addresses,
attachments, options and the double-dash to signal end-of-options.

Signed-off-by: Aron Griffis <agriffis@…>

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main.c

    r5449 r5488  
    546546  extern char *optarg; 
    547547  extern int optind; 
    548   int attach_sep = 0; 
     548  int double_dash = argc, nargc = 1; 
    549549 
    550550  /* sanity check against stupid administrators */ 
     
    574574  memset (QuadOptions, 0, sizeof (QuadOptions)); 
    575575 
    576   for (i = 1; i < argc; i++) 
    577     if (!strcmp(argv[i], "--")) 
     576  for (optind = 1; optind < double_dash; ) 
     577  { 
     578   /* We're getopt'ing POSIXLY, so we'll be here every time getopt() 
     579    * encounters a non-option.  That could be a file to attach  
     580    * (all non-options between -a and --) or it could be an address 
     581    * (which gets collapsed to the front of argv). 
     582    */ 
     583   for (; optind < argc; optind++) 
     584   { 
     585    if (argv[optind][0] == '-') 
    578586    { 
    579       attach_sep = i; 
    580       break; 
     587      if (argv[optind][1] == '-' && argv[optind][2] == '\0') 
     588        double_dash = optind; /* quit outer loop after getopt */ 
     589      break;                  /* drop through to getopt */ 
    581590    } 
    582591 
    583   while ((i = getopt (argc, argv, "A:a:b:F:f:c:Dd:e:H:s:i:hm:npQ:RvxyzZ")) != EOF) 
     592    /* non-option, either an attachment or address */ 
     593    if (attach) 
     594      attach = mutt_add_list (attach, argv[optind]); 
     595    else 
     596      argv[nargc++] = argv[optind]; 
     597   } 
     598 
     599   if ((i = getopt (argc, argv, "+A:a:b:F:f:c:Dd:e:H:s:i:hm:npQ:RvxyzZ")) != EOF) 
    584600    switch (i) 
    585601    { 
     
    685701        mutt_usage (); 
    686702    } 
     703  } 
     704 
     705  /* collapse remaining argv */ 
     706  while (optind < argc) 
     707    argv[nargc++] = argv[optind++]; 
     708  optind = 1; 
     709  argc = nargc; 
    687710 
    688711  switch (version) 
     
    752775    return rv; 
    753776  } 
    754  
    755   /* if an -a option is present, all non-option arguments before -- are considered attachments */ 
    756   if (attach) 
    757     for (; optind <= attach_sep; optind++) 
    758       attach = mutt_add_list (attach, argv[optind]); 
    759777 
    760778  if (newMagic)