Ejemplo n.º 1
0
t_move_record *lookup_move(struct t_board *board, char *move_string) {

    t_chess_square from_square = name_to_index(move_string);
    t_chess_square to_square = name_to_index(move_string + 2);
    t_chess_piece piece = board->square[from_square];
    t_chess_piece captured = PIECETYPE(board->square[to_square]);
    t_chess_color color = COLOR(piece);
    t_chess_piece promote_to = 0;

    if (KING == PIECETYPE(piece)) {

        // Is it a Chess960 castling move?
        if ((captured != BLANK) && (COLOR(board->square[to_square]) == color)) {
            if (to_square > from_square) {
                return &xmove_list[2 * color];
            }
            else {
                return &xmove_list[2 * color + 1];
            }
        }
        else
            return move_directory[from_square][to_square][piece] + captured;
    }
    else if (PAWN == PIECETYPE(piece) && (RANK(to_square) % 7 == 0)) {
        if (move_string[4] == 'q')
            promote_to = QUEEN;
        else if (move_string[4] == 'b')
            promote_to = BISHOP;
        else if (move_string[4] == 'r')
            promote_to = ROOK;
        else if (move_string[4] == 'n')
            promote_to = KNIGHT;

        return move_directory[from_square][to_square][piece] + (4 * captured) + promote_to - 1;

    }
    else
        return move_directory[from_square][to_square][piece] + captured;
}
Ejemplo n.º 2
0
Archivo: join.c Proyecto: udhos/nexthop
static void join(const char *ifname, const char *mcast, const char* group, const char *addr, const char *port_str) {
  int ifindex;
  struct in_addr iface_addr;
  struct in_addr group_addr;
  struct in_addr bind_addr;
  int port;
  int result;
  int fd;
  struct sockaddr_in sock_addr;
  
  ifindex = name_to_index(ifname);
  if (ifindex < 0) {
    fprintf(stderr, "%s: could not find interface: %s\n",
	    prog_name, ifname);
    exit(1);
  }

  result = inet_pton(AF_INET, mcast, &iface_addr);
  if (result <= 0) {
    fprintf(stderr, "%s: bad interface address: %s\n",
	    prog_name, mcast);
    exit(1);
  }
  
  result = inet_pton(AF_INET, group, &group_addr);
  if (result <= 0) {
    fprintf(stderr, "%s: bad group address: %s\n",
	    prog_name, group);
    exit(1);
  }
  
  result = inet_pton(AF_INET, addr, &bind_addr);
  if (result <= 0) {
    fprintf(stderr, "%s: bad bind address: %s\n",
	    prog_name, addr);
    exit(1);
  }

  port = atoi(port_str);

  fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (fd < 0) {
    fprintf(stderr,
	    "%s: could not create socket: socket(): errno=%d: %s\n",
	    prog_name, errno, strerror(errno));
    exit(1);
  }

  {
    int enable = 1; /* boolean */
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &enable, sizeof enable)) {
      fprintf(stderr,
	      "%s: could not set SO_REUSEADDR: errno=%d: %s\n",
	      prog_name, errno, strerror(errno));
    }
  }

  {
    /* will request IP_PKTINFO info while reading the socket */
    int opt = 1;
    if (setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof opt)) {
      fprintf(stderr, "%s: could not set IP_PKTINFO: errno=%d: %s\n",
	      prog_name, errno, strerror(errno));
    }
  }

  if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, (void *) &iface_addr, sizeof iface_addr)) {
      fprintf(stderr, "%s: could not multicast interface: errno=%d: %s\n",
	      prog_name, errno, strerror(errno));
  }

  if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname))) {
      fprintf(stderr, "%s: could set SO_BINDTODEVICE: errno=%d: %s\n",
	      prog_name, errno, strerror(errno));
  }
  
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_addr   = bind_addr;
  sock_addr.sin_port   = htons(port);

  if (bind(fd, (struct sockaddr *)&sock_addr, sizeof(sock_addr))) {
    fprintf(stderr,
	    "%s: could not bind socket: errno=%d: %s\n",
	    prog_name, errno, strerror(errno));
    exit(1);
  }
  
  if (join_group(fd, ifindex, group_addr)) {
    fprintf(stderr,
	    "%s: could not join group: errno=%d: %s\n",
	    prog_name, errno, strerror(errno));
    exit(1);
  }

  printf("%s: joined, reading...\n", prog_name);

  read_loop(fd);

  close(fd);
}
Ejemplo n.º 3
0
int font::load(int *not_found, int head_only)
{
  char *path;
  FILE *fp;
  if ((fp = open_file(name, &path)) == NULL) {
    if (not_found)
      *not_found = 1;
    else
      error("can't find font file `%1'", name);
    return 0;
  }
  text_file t(fp, path);
  t.skip_comments = 1;
  t.silent = head_only;
  char *p;
  for (;;) {
    if (!t.next()) {
      t.error("missing charset command");
      return 0;
    }
    p = strtok(t.buf, WS);
    if (strcmp(p, "name") == 0) {
    }
    else if (strcmp(p, "spacewidth") == 0) {
      p = strtok(0, WS);
      int n;
      if (p == 0 || sscanf(p, "%d", &n) != 1 || n <= 0) {
	t.error("bad argument for spacewidth command");
	return 0;
      }
      space_width = n;
    }
    else if (strcmp(p, "slant") == 0) {
      p = strtok(0, WS);
      double n;
      if (p == 0 || sscanf(p, "%lf", &n) != 1 || n >= 90.0 || n <= -90.0) {
	t.error("bad argument for slant command", p);
	return 0;
      }
      slant = n;
    }
    else if (strcmp(p, "ligatures") == 0) {
      for (;;) {
	p = strtok(0, WS);
	if (p == 0 || strcmp(p, "0") == 0)
	  break;
	if (strcmp(p, "ff") == 0)
	  ligatures |= LIG_ff;
	else if (strcmp(p, "fi") == 0)
	  ligatures |= LIG_fi;
	else if (strcmp(p, "fl") == 0)
	  ligatures |= LIG_fl;
	else if (strcmp(p, "ffi") == 0)
	  ligatures |= LIG_ffi;
	else if (strcmp(p, "ffl") == 0)
	  ligatures |= LIG_ffl;
	else {
	  t.error("unrecognised ligature `%1'", p);
	  return 0;
	}
      }
    }
    else if (strcmp(p, "internalname") == 0) {
      p = strtok(0, WS);
      if (!p) {
	t.error("`internalname command requires argument");
	return 0;
      }
      internalname = new char[strlen(p) + 1];
      strcpy(internalname, p);
    }
    else if (strcmp(p, "special") == 0) {
      special = 1;
    }
    else if (strcmp(p, "kernpairs") != 0 && strcmp(p, "charset") != 0) {
      char *command = p;
      p = strtok(0, "\n");
      handle_unknown_font_command(command, trim_arg(p), t.path, t.lineno);
    }
    else
      break;
  }
  if (head_only)
    return 1;
  char *command = p;
  int had_charset = 0;
  t.skip_comments = 0;
  while (command) {
    if (strcmp(command, "kernpairs") == 0) {
      for (;;) {
	if (!t.next()) {
	  command = 0;
	  break;
	}
	char *c1 = strtok(t.buf, WS);
	if (c1 == 0)
	  continue;
	char *c2 = strtok(0, WS);
	if (c2 == 0) {
	  command = c1;
	  break;
	}
	p = strtok(0, WS);
	if (p == 0) {
	  t.error("missing kern amount");
	  return 0;
	}
	int n;
	if (sscanf(p, "%d", &n) != 1) {
	  t.error("bad kern amount `%1'", p);
	  return 0;
	}
	int i1 = name_to_index(c1);
	if (i1 < 0) {
	  t.error("invalid character `%1'", c1);
	  return 0;
	}
	int i2 = name_to_index(c2);
	if (i2 < 0) {
	  t.error("invalid character `%1'", c2);
	  return 0;
	}
	add_kern(i1, i2, n);
      }
    }
    else if (strcmp(command, "charset") == 0) {
      had_charset = 1;
      int last_index = -1;
      for (;;) {
	if (!t.next()) {
	  command = 0;
	  break;
	}
	char *nm = strtok(t.buf, WS);
	if (nm == 0)
	  continue;			// I dont think this should happen
	p = strtok(0, WS);
	if (p == 0) {
	  command = nm;
	  break;
	}
	if (p[0] == '"') {
	  if (last_index == -1) {
	    t.error("first charset entry is duplicate");
	    return 0;
	  }
	  if (strcmp(nm, "---") == 0) {
	    t.error("unnamed character cannot be duplicate");
	    return 0;
	  }
	  int idx = name_to_index(nm);
	  if (idx < 0) {
	    t.error("invalid character `%1'", nm);
	    return 0;
	  }
	  copy_entry(idx, last_index);
	}
	else {
	  font_char_metric metric;
	  metric.height = 0;
	  metric.depth = 0;
	  metric.pre_math_space = 0;
	  metric.italic_correction = 0;
	  metric.subscript_correction = 0;
	  int nparms = sscanf(p, "%d,%d,%d,%d,%d,%d",
			      &metric.width, &metric.height, &metric.depth,
			      &metric.italic_correction,
			      &metric.pre_math_space,
			      &metric.subscript_correction);
	  if (nparms < 1) {
	    t.error("bad width for `%1'", nm);
	    return 0;
	  }
	  p = strtok(0, WS);
	  if (p == 0) {
	    t.error("missing character type for `%1'", nm);
	    return 0;
	  }
	  int type;
	  if (sscanf(p, "%d", &type) != 1) {
	    t.error("bad character type for `%1'", nm);
	    return 0;
	  }
	  if (type < 0 || type > 255) {
	    t.error("character type `%1' out of range", type);
	    return 0;
	  }
	  metric.type = type;
	  p = strtok(0, WS);
	  if (p == 0) {
	    t.error("missing code for `%1'", nm);
	    return 0;
	  }
	  char *ptr;
	  metric.code = (int)strtol(p, &ptr, 0);
	  if (metric.code == 0 && ptr == p) {
	    t.error("bad code `%1' for character `%2'", p, nm);
	    return 0;
	  }
	  p = strtok(0, WS);
	  if ((p == NULL) || (strcmp(p, "--") == 0)) {
	    metric.special_device_coding = NULL;
	  }
	  else {
	    char *nam = new char[strlen(p) + 1];
	    strcpy(nam, p);
	    metric.special_device_coding = nam;
	  }
	  if (strcmp(nm, "---") == 0) {
	    last_index = number_to_index(metric.code);
	    add_entry(last_index, metric);
	  }
	  else {
	    last_index = name_to_index(nm);
	    if (last_index < 0) {
	      t.error("invalid character `%1'", nm);
	      return 0;
	    }
	    add_entry(last_index, metric);
	    copy_entry(number_to_index(metric.code), last_index);
	  }
	}
      }
      if (last_index == -1) {
	t.error("I didn't seem to find any characters");
	return 0;
      }
    }
    else {
      t.error("unrecognised command `%1' after `kernpairs' or `charset' command", command);
      return 0;
    }
  }
  if (!had_charset) {
    t.error("missing charset command");
    return 0;
  }
  if (space_width == 0)
    space_width = scale_round(unitwidth, res, 72*3*sizescale);
  compact();
  return 1;
}
/**
 * \brief Initialize extension override tables.
 *
 * This should be called one time early during first context initialization.
 */
void
_mesa_one_time_init_extension_overrides(void)
{
   const char *env_const = getenv("MESA_EXTENSION_OVERRIDE");
   char *env;
   char *ext;
   int len;
   size_t offset;

   atexit(free_unknown_extensions_strings);

   memset(&_mesa_extension_override_enables, 0, sizeof(struct gl_extensions));
   memset(&_mesa_extension_override_disables, 0, sizeof(struct gl_extensions));

   if (env_const == NULL) {
      return;
   }

   /* extra_exts: List of unrecognized extensions. */
   extra_extensions = calloc(ALIGN(strlen(env_const) + 2, 4), sizeof(char));

   /* Copy env_const because strtok() is destructive. */
   env = strdup(env_const);

   if (env == NULL ||
       extra_extensions == NULL) {
      free(env);
      free(extra_extensions);
      return;
   }

   for (ext = strtok(env, " "); ext != NULL; ext = strtok(NULL, " ")) {
      int enable;
      int i;
      bool recognized;
      switch (ext[0]) {
      case '+':
         enable = 1;
         ++ext;
         break;
      case '-':
         enable = 0;
         ++ext;
         break;
      default:
         enable = 1;
         break;
      }

      i = name_to_index(ext);
      offset = set_extension(&_mesa_extension_override_enables, i, enable);
      if (offset != 0 && (offset != o(dummy_true) || enable != GL_FALSE)) {
         ((GLboolean *) &_mesa_extension_override_disables)[offset] = !enable;
         recognized = true;
      } else {
         recognized = false;
      }

      if (i >= 0)
         disabled_extensions[i] = !enable;

      if (!recognized && enable) {
         strcat(extra_extensions, ext);
         strcat(extra_extensions, " ");
      }
   }

   free(env);

   /* Remove trailing space, and free if unused. */
   len = strlen(extra_extensions);
   if (len == 0) {
      free(extra_extensions);
      extra_extensions = NULL;
   } else if (extra_extensions[len - 1] == ' ') {
      extra_extensions[len - 1] = '\0';
   }
}