Esempio n. 1
0
int WINAPI WinMain(HINSTANCE instance,HINSTANCE last,LPSTR command,int state)
{
  char
    **argv;

  int
    argc,
    main(int,char **);

  argv=StringToArgv(command,&argc);
  return(main(argc,argv));
}
Esempio n. 2
0
static void
ParseArgs(char *argstring)
{
    int argc;
    char **argv;
    static int excluded = 0;
    int i;
    char *pch;
    
    /* make sure there ARE arguments */
    if (!(argstring && *argstring))
	return;

    /* break the string into normal arguments */
    StringToArgv(argstring,&argc,&argv);

    /* check the module args */
    for (i=1; i < argc; ++i) {
	float interval;
	if (ldebug > 1)
	    printf("Checking argv[%d]: '%s'\n", i, argv[i]);
	if (strcmp(argv[i],"-d") == 0) {
	    ++ldebug;
	} else if (sscanf(argv[i],"-i%f", &interval) == 1) {
	    age_interval = interval;
	    if (ldebug)
		printf("mod_traffic: setting age interval to %.3f seconds\n",
		       age_interval);
	} else if (strcmp(argv[i],"-G") == 0) {
	    doplot_active = TRUE;
	    doplot_idle = TRUE;
	    doplot_data = TRUE;
	    doplot_bytes = TRUE;
	    doplot_loss = TRUE;
	    doplot_long = TRUE;
	    doplot_open = TRUE;
	    doplot_halfopen = TRUE;
	    doplot_openclose = TRUE;
	    doplot_i_open = TRUE;
	    doplot_packets = TRUE;
	    doplot_pureacks = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating all graphs\n");
	} else if (strcmp(argv[i],"-A") == 0) {
	    doplot_active = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'active' graph into '%s'\n",
			PLOTTER_ACTIVE_FILENAME);
	} else if (strcmp(argv[i],"-B") == 0) {
	    doplot_bytes = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'bytes' graph into '%s'\n",
			PLOTTER_BYTES_FILENAME);
	} else if (strcmp(argv[i],"-H") == 0) {
	    doplot_halfopen = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'halfopen' graph into '%s'\n",
			PLOTTER_HALFOPEN_FILENAME);
	} else if (strcmp(argv[i],"-Q") == 0) {
	    doplot_idle = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'idle' graph into '%s'\n",
			PLOTTER_IDLE_FILENAME);
	} else if (strcmp(argv[i],"-K") == 0) {
	    doplot_pureacks = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'pureacks' graph into '%s'\n",
			PLOTTER_PUREACKS_FILENAME);
	} else if (strcmp(argv[i],"-L") == 0) {
	    doplot_loss = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'loss' graph into '%s'\n",
			PLOTTER_LOSS_FILENAME);
	} else if (strcmp(argv[i],"-T") == 0) {
	    doplot_data = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'total data' graph into '%s'\n",
			PLOTTER_DATA_FILENAME);
	} else if (strncmp(argv[i],"-D",2) == 0) {
	    doplot_long = TRUE;
	    if (strlen(argv[i]) > 2) {
		/* grab the number */
		longconn_duration = atoi(argv[i]+2);
		if (longconn_duration <= 0) {
		    fprintf(stderr,"bad time value for -LN '%s'\n",
			    argv[i]);
		    exit(-1);
		}

	    }
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'long duration' graph (%d secs) into '%s'\n",
			longconn_duration,
			PLOTTER_LONG_FILENAME);
	} else if (strcmp(argv[i],"-O") == 0) {
	    doplot_open = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'open' graph into '%s'\n",
			PLOTTER_OPEN_FILENAME);
	} else if (strcmp(argv[i],"-C") == 0) {
	    doplot_openclose = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'openclose' graph into '%s'\n",
			PLOTTER_OPENCLOSE_FILENAME);
	} else if (strcmp(argv[i],"-I") == 0) {
	    doplot_i_open = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'instantaneous openclose' graph into '%s'\n",
			PLOTTER_I_OPEN_FILENAME);
	} else if (strcmp(argv[i],"-P") == 0) {
	    doplot_packets = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'packets' graph into '%s'\n",
			PLOTTER_PACKETS_FILENAME);
	} else if (strncmp(argv[i],"-R",2) == 0) {
	    int nargs;
	    doplot_rtt = TRUE;
	    if (ldebug)
		fprintf(stderr,
			"mod_traffic: generating 'rtt' graph into '%s'\n",
			PLOTTER_RTT_FILENAME);
	    /* check for valid RTT range args */
	    nargs = sscanf(argv[i],"-R%d-%d", &rtt_minvalid,
			   &rtt_maxvalid);
	    switch (nargs) {
	      case 2: {		/* 2 args is min and max */
		  /* sanity check */
		  if (rtt_maxvalid <= rtt_minvalid) {
		      fprintf(stderr,
			      "mod_traffic: Out of order min-max range for -R '%s'\n",
			      argv[i]);
		      traffic_usage();
		      exit(-1);
		  }
		  break;
	      }
	      case 1: {		/* 1 args in min rtt */
		  /* sanity check */
		  if (rtt_maxvalid <= rtt_minvalid) {
		      fprintf(stderr,
			      "mod_traffic: Out of order min-max range for -R '%s'\n",
			      argv[i]);
		      traffic_usage();
		      exit(-1);
		  }
		  break;
	      }
	      case 0: 		/* no args, that's OK */
	      case -1: 		/* (means the same as 0) */
		break;
	      default:		/* illegal args  */
		fprintf(stderr,
			"mod_traffic: Invalid min-max range for -R '%s'\n",
			argv[i]);
		traffic_usage();
		exit(-1);
		break;
	    }
	} else if (strncmp(argv[i],"-p",2) == 0) {
	    pch = argv[i]+2;
	    while (pch && *pch) {
		char *pch_next;
		unsigned port1, port2;

		if ((pch_next = strchr(pch,',')) != NULL) {
		    *pch_next = '\00';
		    ++pch_next;
		}

		if (!excluded) {
		    ExcludePorts(1,NUM_PORTS-1);
		    excluded = 1;
		}

		if (sscanf(pch,"-%u-%u", &port1, &port2) == 2) {
		    ExcludePorts(port1,port2);
		} else if (sscanf(pch,"%u-%u", &port1, &port2) == 2) {
		    IncludePorts(port1,port2);
		} else if (sscanf(pch,"-%u", &port1) == 1) {
		    ExcludePorts(port1,port1);
		} else if (sscanf(pch,"%u", &port1) == 1) {
		    IncludePorts(port1,port1);
		} else {
		    fprintf(stderr,"mod_traffic: Invalid port specification string '%s'\n", pch);
		    traffic_usage();
		    exit(-1);
		}

		pch = pch_next;
	    }
	} else {
	    fprintf(stderr,"Traffic module: bad argument '%s'\n",
		    argv[i]);
	    exit(-1);
	}
    }
}
Esempio n. 3
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
+   I n v o k e P o s t s c r i p t D e l e g a t e                           %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  InvokePostscriptDelegate() executes the postscript interpreter with the
%  specified command.
%
%  The format of the InvokePostscriptDelegate method is:
%
%      MagickPassFail InvokePostscriptDelegate(const unsigned int verbose,
%        const char *command, ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o status:  Method InvokePostscriptDelegate returns MagickPass if the command
%      is successfully executed, otherwise MagickFail.
%
%    o verbose: A value other than zero displays the command prior to
%      executing it.
%
%    o command: The address of a character string containing the command to
%      execute.  The command is formulated through direct FormatString()
%      substitutions rather than using TranslateText.
%
%
*/
MagickExport MagickPassFail
InvokePostscriptDelegate(const unsigned int verbose,
			 const char *command,ExceptionInfo *exception)
{
  register long
    i;

  char
    **argv;

  int
    argc;

  int
    status;

#if defined(HasGS) || defined(MSWINDOWS)

  gs_main_instance
    *interpreter;

  int
    pexit_code;

#if defined(MSWINDOWS)
  const GhostscriptVectors
    *gs_func;

  gs_func=NTGhostscriptDLLVectors();
#elif defined(HasGS)
  GhostscriptVectors
    gs_func_struct;

  const GhostscriptVectors
    *gs_func;

  gs_func=(&gs_func_struct);
  gs_func_struct.exit=gsapi_exit;
  gs_func_struct.init_with_args=gsapi_init_with_args;
  gs_func_struct.new_instance=gsapi_new_instance;
  gs_func_struct.run_string=gsapi_run_string;
  gs_func_struct.delete_instance=gsapi_delete_instance;
#endif
  if (gs_func != (GhostscriptVectors *) NULL)
    {

      /*
	Allocate an interpreter.
      */
      interpreter = (gs_main_instance *) NULL;
      status=(gs_func->new_instance)(&interpreter,(void *) NULL);
      if (status < 0)
	{
	  ThrowException(exception,DelegateError,
			 FailedToAllocateGhostscriptInterpreter,command);
	  return(MagickFail);
	}
      /*
	Initialize interpreter with argument list.
      */
      argv=StringToArgv(command,&argc);
      if (argv == (char **) NULL)
	{
	  ThrowException(exception,DelegateError,FailedToAllocateArgumentList,
			 command);
	  return(MagickFail);
	}

      if (verbose)
	{
	  char
	    buffer[MaxTextExtent];

#if defined(MSWINDOWS)
	  (void) NTGhostscriptDLL(buffer,sizeof(buffer));
#else
	  (void) strlcpy(buffer,"[ghostscript library]",sizeof(buffer));
#endif
	  (void) fputs(buffer,stderr);
	  for (i=2 ; i < argc ; i++)
	    (void) fprintf(stderr," \"%s\"",argv[i]);
	  (void) fflush(stderr);
	}
      status=(gs_func->init_with_args)(interpreter,argc-1,argv+1);
      if (status == 0)
	{
	  status=(gs_func->run_string)
	    (interpreter,"systemdict /start get exec\n",0,&pexit_code);
	  if ((status == 0) || (status <= -100))
	    {
	      char
		reason[MaxTextExtent];

	      FormatString(reason,"Ghostscript returns status %d, exit code %d",
			   status,pexit_code);
	      (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",reason);
	      ThrowException(exception,DelegateError,PostscriptDelegateFailed,command);
	    }
	}
      /*
	Exit interpreter.
      */
      (gs_func->exit)(interpreter);
      /*
	Deallocate interpreter.
      */
      (gs_func->delete_instance)(interpreter);
      for (i=0; i < argc; i++)
	MagickFreeMemory(argv[i]);
      MagickFreeMemory(argv);
      if ((status == 0) || (status <= -100))
	return(MagickFail);
      return(MagickPass);
    }
#endif /* defined(HasGS) || defined(MSWINDOWS) */

  status=MagickFail;
#if defined(POSIX)
  {
    argv = StringToArgv(command,&argc);
    if (argv == (char **) NULL)
      {
	ThrowException(exception,DelegateError,
		       FailedToAllocateArgumentList,
		       command);
      }
    else
      {
	if (MagickSpawnVP(verbose,argv[1],argv+1) == 0)
	  status=MagickPass;
	for (i=0; i < argc; i++)
	  MagickFreeMemory(argv[i]);
	MagickFreeMemory(argv);
      }
  }
#else
  if (SystemCommand(verbose,command) == 0)
    status=MagickPass;
#endif
  return status;
}
Esempio n. 4
0
MagickExport unsigned int InvokeDelegate(ImageInfo *image_info,Image *image,
  const char *decode,const char *encode,ExceptionInfo *exception)
{
  char
    *command,
    **commands,
    filename[MaxTextExtent];

  const DelegateInfo
    *delegate_info;

  register long
    i;

  unsigned int
    status,
    temporary_image_filename;

  /*
    Get delegate.
  */
  assert(image_info != (ImageInfo *) NULL);
  assert(image_info->signature == MagickSignature);
  assert(image != (Image *) NULL);
  assert(image->signature == MagickSignature);
  temporary_image_filename=(*image->filename == '\0');
  if (temporary_image_filename)
    {
      /* Allocate a temporary filename if image is unnamed.  */
      if(!AcquireTemporaryFileName(image->filename))
        {
          (void) ThrowException(exception,FileOpenError,UnableToCreateTemporaryFile,image->filename);
          return(False);
        }
    }
  (void) strlcpy(filename,image->filename,MaxTextExtent);
  delegate_info=GetDelegateInfo(decode,encode,exception);
  if (delegate_info == (DelegateInfo *) NULL)
    {
      if (temporary_image_filename)
        (void) LiberateTemporaryFile(image->filename);
      (void) ThrowException(exception,DelegateError,NoTagFound,
        decode ? decode : encode);
      return(False);
    }

  if (*image_info->filename == '\0')
    {
      /* ReadImage will normally have already set image_info->filename
         to the name of a temporary file.  If not, then assign
         one. Setting image_info->temporary to True indicates that
         there is a temporary file to be removed later.  */
      if(!AcquireTemporaryFileName(image_info->filename))
        {
          if (temporary_image_filename)
            (void) LiberateTemporaryFile(image->filename);
          (void) ThrowException(exception,FileOpenError,UnableToCreateTemporaryFile,image_info->filename);
          return(False);
        }
      image_info->temporary=True;
    }

  if (delegate_info->mode != 0)
    if ((decode && (delegate_info->encode != (char *) NULL)) ||
        (encode && (delegate_info->decode != (char *) NULL)))
      {
        char
          decode_filename[MaxTextExtent],
          *magick;

        ImageInfo
          *clone_info;

        register Image
          *p;

        /*
          Delegate requires a particular image format.
        */

        if (!AcquireTemporaryFileName(image_info->unique))
        {
          if (temporary_image_filename)
            (void) LiberateTemporaryFile(image->filename);
          (void) ThrowException(exception,FileOpenError,UnableToCreateTemporaryFile,image_info->unique);
          return(False);
        }

        if (!AcquireTemporaryFileName(image_info->zero))
        {
          if (temporary_image_filename)
            (void) LiberateTemporaryFile(image->filename);
          (void) LiberateTemporaryFile(image_info->unique);
          (void) ThrowException(exception,FileOpenError,UnableToCreateTemporaryFile,image_info->zero);
          return(False);
        }
        /* Expand sprintf-style codes in delegate command to command string */
        magick=TranslateText(image_info,image,decode != (char *) NULL ?
          delegate_info->encode : delegate_info->decode);
        if (magick == (char *) NULL)
          {
            (void) LiberateTemporaryFile(image_info->unique);
            (void) LiberateTemporaryFile(image_info->zero);
            if (temporary_image_filename)
              (void) LiberateTemporaryFile(image->filename);
            (void) ThrowException(exception,DelegateError,DelegateFailed,
              decode ? decode : encode);
            return(False);
          }
        LocaleUpper(magick);
        clone_info=CloneImageInfo(image_info);
        (void) strlcpy((char *) clone_info->magick,magick,MaxTextExtent);
        (void) strlcpy(image->magick,magick,MaxTextExtent);
        MagickFreeMemory(magick);
        (void) strlcpy(decode_filename,image->filename,MaxTextExtent);
        FormatString(clone_info->filename,"%.1024s:",delegate_info->decode);
        (void) SetImageInfo(clone_info,True,exception);
        (void) strlcpy(clone_info->filename,image_info->filename,
          MaxTextExtent);
        for (p=image; p != (Image *) NULL; p=p->next)
        {
          FormatString(p->filename,"%.1024s:%.1024s",delegate_info->decode,
            decode_filename);
          status=WriteImage(clone_info,p);
          if (status == False)
            {
              (void) LiberateTemporaryFile(image_info->unique);
              (void) LiberateTemporaryFile(image_info->zero);
              if (temporary_image_filename)
                (void) LiberateTemporaryFile(image->filename);
              DestroyImageInfo(clone_info);
              (void) ThrowException(exception,DelegateError,DelegateFailed,
                decode ? decode : encode);
              return(False);
            }
          if (clone_info->adjoin)
            break;
        }
        (void) LiberateTemporaryFile(image_info->unique);
        (void) LiberateTemporaryFile(image_info->zero);
        DestroyImageInfo(clone_info);
      }
  /*
    Invoke delegate.
  */
  (void) strlcpy(image->filename,filename,MaxTextExtent);
  commands=StringToList(delegate_info->commands);
  if (commands == (char **) NULL)
    {
      if (temporary_image_filename)
        (void) LiberateTemporaryFile(image->filename);
      (void) ThrowException(exception,ResourceLimitError,MemoryAllocationFailed,decode ? decode : encode);
      return(False);
    }
  command=(char *) NULL;
  status=True;
  /* For each delegate command ... */
  for (i=0; commands[i] != (char *) NULL; i++)
  {
    status=True;
    /* Allocate convenience temporary files */
    if (!AcquireTemporaryFileName(image_info->unique))
    {
      (void) ThrowException(exception,FileOpenError,UnableToCreateTemporaryFile,image_info->unique);
      status=False;
      goto error_exit;
    }
    if (!AcquireTemporaryFileName(image_info->zero))
    {
      (void) ThrowException(exception,FileOpenError,UnableToCreateTemporaryFile,image_info->zero);
      (void) LiberateTemporaryFile(image_info->unique);
      status=False;
      goto error_exit;
    }
#if defined(POSIX)
    {
      MagickBool
        needs_shell;

      /*
        Check to see if command template must be executed via shell
        due to using constructs requiring multiple processes or I/O
        redirection.
      */
      needs_shell = MagickFalse;
      {
        char *
          p;

        p = commands[i];
        for (p = commands[i]; *p; p++)
          {
            if (('&' == *p) ||
                (';' == *p) ||
                ('<' == *p) ||
                ('>' == *p) ||
                ('|' == *p))
              {
                needs_shell = MagickTrue;
                break;
              }
          }
      }

      if (MagickFalse == needs_shell)
        {
          int
            arg_count,
            j;
          
          char
            **arg_array;
          
          /*
            Convert command template into an argument array.  Translate
            each argument array element individually in order to
            absolutely avoid any possibility that the number of arguments
            may be altered due to substituted data.
          */
          arg_array = StringToArgv(commands[i],&arg_count);
          for (j = 0; arg_array[j] != (const char*) NULL; j++)
            {
              if (strchr(arg_array[j], '%') != (const char*) NULL)
                {
                  char *expanded = TranslateText(image_info,image,arg_array[j]);
                  if (expanded != (char *) NULL)
                    {
                      MagickFreeMemory(arg_array[j]);
                      arg_array[j] = expanded;
                    }
                }
            }
          
          /*
            Execute delegate using our secure "spawn" facility.
          */
          status = MagickSpawnVP(image_info->verbose,arg_array[1],arg_array+1);
        }
      else
        {
          /*
            Expand sprintf-style codes in delegate command to command
            string, escaping replacement text appropriately
          */
          command=TranslateTextEx(image_info,image,commands[i],UnixShellTextEscape);
          if (command == (char *) NULL)
            break;
          /*
            Execute delegate using command shell.
          */
          status=SystemCommand(image_info->verbose,command);
        }
    }
#else
    {
      /*
        Expand sprintf-style codes in delegate command to command string
      */
      command=TranslateText(image_info,image,commands[i]);
      if (command == (char *) NULL)
        break;
      /*
        Execute delegate using command shell.
      */
      status=SystemCommand(image_info->verbose,command);
    }
#endif
    MagickFreeMemory(command);
    /* Liberate convenience temporary files */
    (void) LiberateTemporaryFile(image_info->unique);
    (void) LiberateTemporaryFile(image_info->zero);
    if (status != False)
      {
        (void) ThrowException(exception,DelegateError,DelegateFailed,
          commands[i]);
        goto error_exit;
      }
    MagickFreeMemory(commands[i]);
  }
  /*
    Free resources.
  */
 error_exit:
  if (temporary_image_filename)
    (void) LiberateTemporaryFile(image->filename);
  for ( ; commands[i] != (char *) NULL; i++)
    MagickFreeMemory(commands[i]);
  MagickFreeMemory(commands);
  return(status != False);
}