/* PRM_VARLINESET - Parse and set entire line of variables/parameters ** Returns TRUE unless parsing error ** Mungs the input string! */ int prm_varlineset(char *s, struct prmvar_s *tab, FILE *of, FILE *ef) { #define NTOKS 100 char tokbuf[300+NTOKS]; /* Big work buffer */ char *tokarr[NTOKS]; /* Lots of tokens */ char **tkp; char *tcp = tokbuf; size_t tcnt = sizeof(tokbuf); size_t slen = strlen(s); int n; n = s_tokenize(tokarr, NTOKS-1, &tcp, &tcnt, &s, &slen); tokarr[n] = NULL; for (tkp = tokarr; *tkp; ++tkp) { if (!prm_varset(tkp, tab, of, ef)) { if (ef && tkp[1]) { /* Error? See if more left */ fprintf(ef, "Parsing aborted.\n"); } return FALSE; } } return TRUE; #undef NTOKS }
/******************************************************************** * buy: Checks commands' params and does the corresponding actions. ********************************************************************/ void buy(CommandType command, Player *player, WeaponType weaptable[]) { char input[MAX_INPUT+1], *tokens[2] = {NULL}; int i, num, cost; bool found; if (command.params[0] == NULL) { puts(_("What do you want to buy?")); return; } if (!strcmp(command.params[0], "bullets") || !strcmp(command.params[0], "drugs") || !strcmp(command.params[0], "weapon")) { if (command.params[1] == NULL) { puts(_("Please be more specific.")); return; } if (!strcmp(command.params[0], "weapon")) { for (i = 0; i < MAX_WEAPONS; i++) if (! strcmp(weaptable[i].name, command.params[1])) { found = true; break; } if (!found) { puts(_("There is no weapon with such name.")); return; } cost = weaptable[i].value; } else { num = strtol(command.params[1], NULL, 10); if (num <= 0) { printf(_("%s is not a valid positive integer.\n"), command.params[1]); return; } cost = !strcmp(command.params[0], "bullets") ? BULLET_COST:DRUG_COST; cost *= num; } printf(_("You need %d gold in order to buy this stuff.\n"), cost); if (player->gold < cost) { puts(_("Sorry, you don't have enough gold.")); return; } printf(_("Are you sure that you want to proceed? (y/n) ")); fgets(input, sizeof(input), stdin); s_tokenize(input, tokens, 2, " \n"); if (tokens[0] == NULL || tokens[1] != NULL || (strcmp(s_tolower(tokens[0]), "y") && strcmp(s_tolower(tokens[0]), "yes"))) { puts(_("canceled")); return; } if (!strcmp(command.params[0], "bullets")) { player->gold -= cost; player->bullets += num; } else if (!strcmp(command.params[0], "drugs")){ player->gold -= cost; player->health += num * DRUG_HEALTH; if (player->health > 100) player->health = 100; } else { player->gold -= cost; memcpy(&player->weapon, &weaptable[i], sizeof(WeaponType)); } puts(_("Done!")); return; } printf(_("You can't buy %s.\n"), command.params[0]); }