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: Mike Kaufman <kaufman@xxxxxxxxxxxxxxxxxxxxxx>
Cc: freeciv-dev@xxxxxxxxxxx, bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: dynamic timeout (PR#1356)
From: Raimar Falke <hawk@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 7 Apr 2002 14:50:40 +0200
Reply-to: rf13@xxxxxxxxxxxxxxxxxxxxxx

On Sat, Apr 06, 2002 at 05:12:59PM -0600, Mike Kaufman wrote:
> attached is version 4 of the patch.
> 
> new in this version:
> updated to cvs
> add default values to help text. (though this should be fixed when
>   there's better support for mulitple arguments commands/options)
> 
> added a new function get_args() in shared.c comment in the function
> header should explain most of it. It's a much better deal than strtok.
> 
> Per: I think that you should be able to use this to clean up that part of
> stdinhand that you despise.
> 
> Raimar, please take a look and commit.

>  /***************************************************************
> + parses the string *str strtok style 
> + i.e. "34 abc 54 87\0" returns str="34\0abc\054\087\0" and 
> + args[0]="34\0" args[1]="abc\0" args[2]="54\0" args[3]="87\0"
> + 
> + Argument separators are spaces, tabs, and commas. An argument can be 
> + in quotes, and if it is, it can have embedded record separators. For 
> example,
> + command "a name" hard "1,2,3,4,5" 99 returns four arguments.
> +
> + Like strtok, this function _does_ modify the string *str, so you should pass
> + in a copy if you want to save it. To call, declare an array of char pointers
> + for args and pass in the length the array as numargs. This should be the 
> + number of args you want. It will ignore extras. If a string has too few 
> + arguments, the args[] not used will be set to NULL. Example:
> +
> +   char str[MAX_LEN];
> +   char *args[numargs];
> +
> +   sz_strlcpy(str, buffer);
> +   get_args(str, args, numargs);
> +***************************************************************/
> +void get_args(char *str, char **args, int numargs)

Please pass in the maximum size.

> +{
> +  int i, len;
> +  bool quoted = FALSE;

What about single quoted. See cut_comment.

> +  /* 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.

> +  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?

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx       phone: +49 351 4415773     icq: 54420251
 pgp 2: id: 0F9D7955 len: 1024 fingerprint: 7760F933D5478009 4FA0C56F1DC2FB8E
 "On the eigth day, God started debugging"


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