Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2002:
[Freeciv-Dev] Re: dynamic timeout (PR#1356)
Home

[Freeciv-Dev] Re: dynamic timeout (PR#1356)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: rf13@xxxxxxxxxxxxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx, bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: dynamic timeout (PR#1356)
From: Mike Kaufman <kaufman@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 7 Apr 2002 10:45:31 -0500

On Sun, Apr 07, 2002 at 02:50:40PM +0200, Raimar Falke wrote:
> > +***************************************************************/
> > +void get_args(char *str, char **args, int numargs)
> 
> Please pass in the maximum size.

I do not understand. str's maximum size? why? 
numargs should be <= the size of the args array. I don't see why we need
anything else. Please be explicit.

> 
> > +{
> > +  int i, len;
> > +  bool quoted = FALSE;
> 
> What about single quoted. See cut_comment.

These are not analogous situations. cut_comment merely looks for paired
sets before it gets to the octothorp. Here we've got to keep track of
everything. Besides, I want to be able to create players like:
create "ain't I cool" "don't mess with me" 
which won't work if apostrophes are special. (of course we could expect
the user to \', but now we expect the user to use double quotes, either
way the user has some responsibility)

what would be relatively easy would be to treat ' and " interchangably.
so "5' 'abc da' "4t" 'a s" returns 4 tokens. To actually treat ' and "
in a nested fashion would suck ass. I don't have the energy (ok, it
wouldn't be _that_ hard, but I still don't have the energy). I look forward 
to your implementation. Extend this one after it's in cvs.

> 
> > +  /* we do lookups on buf, but change str; this is to guard against
> > +   * idiot users doing things like '4" 5"2' and expecting 4,5,2 back */
> > +  char *buf, *bptr;
> > +  char *sptr = str;
> > +
> > +  assert(str != NULL);
> > +
> > +  remove_trailing_spaces(str);
> > +
> 
> > +  len = strlen(str) + 1;
> > +  buf = fc_malloc(len);
> > +  mystrlcpy(buf, str, len);
> 
> Replace with mystrdup.

good idea.

> 
> > +  bptr = buf;
> > +
> > +  /* NULL out the args */
> > +  for(i = 0; i < numargs; i++) {
> > +    args[i] = NULL;
> > +  }
> > +  i = 0;
> > +
> > +  while(*bptr != '\0' && i < numargs) {
> > +    /* skip intervening whitespace */
> > +    while(!quoted 
> > +          && (*bptr == ' ' || *bptr == '\t' || *bptr == ',' || *bptr == 
> > '"')) {
> > +      sptr++; bptr++;
> > +      if (*(bptr - 1) == '"') {
> > +        quoted = TRUE; /* starting a quote */
> > +        break;
> > +      }
> > +    }
> > +
> > +    args[i++] = sptr;
> > +
> > +    /* skip arg */
> > +    while(*bptr != '\0'
> > +          && ((*bptr != ' ' && *bptr != '\t' && *bptr != ',') || quoted)) {
> > +      if (*bptr == '"') {
> > +        quoted ^= 1; /* could be starting or ending */
> > +        break;
> > +      }
> > +      sptr++; bptr++;
> > +    }
> > +
> > +    if (*bptr == '\0') {
> > +      break;
> > +    } else {
> > +      *(sptr++) = '\0';
> > +      bptr++;
> > +    }
> > +  }
> > +
> > +  free(buf);
> > +}
> > +
> 
> > +/**************************************************************************
> > +  Set timeout options.
> > +**************************************************************************/
> > +static void timeout_command(struct connection *caller, char *str) 
> > +{
> > +  char buf[MAX_LEN_CONSOLE_LINE];
> > +  char *arg[4];
> > +  int i = 0, noargs = 0, val[4];
> > +  int *timeouts[4];
> > +
> > +  timeouts[0] = &game.timeoutint;
> > +  timeouts[1] = &game.timeoutintinc;
> > +  timeouts[2] = &game.timeoutinc;
> > +  timeouts[3] = &game.timeoutincmult;
> > +
> > +  sz_strlcpy(buf, str);
> > +  get_args(buf, arg, 4);
> > +
> > +  for(i = 0; i < 4; i++) {
> > +    if (arg[i] != NULL) { 
> > +      if(sscanf(arg[i], "%d", &val[i]) != 1) {
> > +        cmd_reply(CMD_TIMEOUT, caller, C_FAIL, _("Invalid argument %d."), 
> > i+1);
> > +      } else {
> 
> > +        *timeouts[i] = val[i];
> 
> Why do you first use val here and copy it later to timeouts?

'cause we may get garbage on the sscanf and we want to protect the
original value. But we should get rid of val[4] in favor of a tmp var inside 
the for loop.

-mike


[Prev in Thread] Current Thread [Next in Thread]