Blame src/ArgV.cc

Packit 8f70b4
/*
Packit 8f70b4
 * lftp - file transfer program
Packit 8f70b4
 *
Packit 8f70b4
 * Copyright (c) 1996-2017 by Alexander V. Lukyanov (lav@yars.free.net)
Packit 8f70b4
 *
Packit 8f70b4
 * This program is free software; you can redistribute it and/or modify
Packit 8f70b4
 * it under the terms of the GNU General Public License as published by
Packit 8f70b4
 * the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
 * (at your option) any later version.
Packit 8f70b4
 *
Packit 8f70b4
 * This program is distributed in the hope that it will be useful,
Packit 8f70b4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
 * GNU General Public License for more details.
Packit 8f70b4
 *
Packit 8f70b4
 * You should have received a copy of the GNU General Public License
Packit 8f70b4
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8f70b4
 */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
#include "misc.h"
Packit 8f70b4
#include "ArgV.h"
Packit 8f70b4
Packit 8f70b4
ArgV::ArgV(const char *a0, const char *args_c)
Packit 8f70b4
{
Packit 8f70b4
   ind=0;
Packit 8f70b4
Packit 8f70b4
   Append(a0);
Packit 8f70b4
   char *args=alloca_strdup(args_c);
Packit 8f70b4
   for(char *t=strtok(args," "); t; t=strtok(NULL," "))
Packit 8f70b4
      Append(t);
Packit 8f70b4
}
Packit 8f70b4
ArgV::~ArgV()
Packit 8f70b4
{
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void ArgV::seek(int n)
Packit 8f70b4
{
Packit 8f70b4
   if(n>=Count())
Packit 8f70b4
      n=Count();
Packit 8f70b4
   ind=n;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
const char *ArgV::getnext()
Packit 8f70b4
{
Packit 8f70b4
   const char *s=String(++ind);
Packit 8f70b4
   if(!s)
Packit 8f70b4
      ind=Count(); // getcurr will return 0
Packit 8f70b4
   return s;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void ArgV::back()
Packit 8f70b4
{
Packit 8f70b4
   if(ind>0)
Packit 8f70b4
      ind--;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
xstring& ArgV::CombineTo(xstring& res,int start,int end) const
Packit 8f70b4
{
Packit 8f70b4
   res.nset("",0);
Packit 8f70b4
   if(!end)
Packit 8f70b4
      end=Count();
Packit 8f70b4
   if(start>=end)
Packit 8f70b4
      return res;
Packit 8f70b4
   for(;;)
Packit 8f70b4
   {
Packit 8f70b4
      res.append(getarg(start++));
Packit 8f70b4
      if(start>=end)
Packit 8f70b4
	 return(res);
Packit 8f70b4
      res.append(' ');
Packit 8f70b4
   }
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
xstring& ArgV::CombineShellQuotedTo(xstring& res,int start) const
Packit 8f70b4
{
Packit 8f70b4
   res.nset("",0);
Packit 8f70b4
   if(start>=Count())
Packit 8f70b4
      return res;
Packit 8f70b4
   for(;;)
Packit 8f70b4
   {
Packit 8f70b4
      for(const char *arg=String(start++); *arg; arg++)
Packit 8f70b4
      {
Packit 8f70b4
	 if (is_shell_special(*arg))
Packit 8f70b4
	    res.append('\\');
Packit 8f70b4
	 res.append(*arg);
Packit 8f70b4
      }
Packit 8f70b4
      if(start>=Count())
Packit 8f70b4
	 return(res);
Packit 8f70b4
      res.append(' ');
Packit 8f70b4
   }
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
int ArgV::getopt_long(const char *opts,const struct option *lopts,int *lind)
Packit 8f70b4
{
Packit 8f70b4
   optind=ind;
Packit 8f70b4
   int r=::getopt_long(Count(),SetNonConst(),opts,lopts,lind);
Packit 8f70b4
   ind=optind;
Packit 8f70b4
   return r;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
const char *ArgV::getopt_error_message(int e)
Packit 8f70b4
{
Packit 8f70b4
   if(optopt>=32 && optopt<127)
Packit 8f70b4
   {
Packit 8f70b4
      if(e==':')
Packit 8f70b4
	 return xstring::format("%s -- %c",_("option requires an argument"),optopt);
Packit 8f70b4
      else
Packit 8f70b4
	 return xstring::format("%s -- %c",_("invalid option"),optopt);
Packit 8f70b4
   }
Packit 8f70b4
   if(ind>1)
Packit 8f70b4
   {
Packit 8f70b4
      if(e==':')
Packit 8f70b4
	 return xstring::format(_("option `%s' requires an argument"),getarg(ind-1));
Packit 8f70b4
      else
Packit 8f70b4
	 return xstring::format(_("unrecognized option `%s'"),getarg(ind-1));
Packit 8f70b4
   }
Packit 8f70b4
   return _("invalid option");
Packit 8f70b4
}