int parse_int(char *s, ParsedInt *u) { int out_of_range = parse_int32(s, &(u->i)); if (!out_of_range) { u->t = I32; } else { /* parse a 64-bit integer */ out_of_range = parse_int64(s, &(u->l)); u->t = I64; } return out_of_range; }
void pgut_setopt(pgut_option *opt, const char *optarg, pgut_optsrc src) { const char *message; if (opt == NULL) { fprintf(stderr, "Try \"%s --help\" for more information.\n", PROGRAM_NAME); exit(EINVAL); } if (opt->source > src) { /* high prior value has been set already. */ return; } else if (src >= SOURCE_CMDLINE && opt->source >= src) { /* duplicated option in command line */ message = "specified only once"; } else { /* can be overwritten if non-command line source */ opt->source = src; switch (opt->type) { case 'b': case 'B': if (optarg == NULL) { *((bool *) opt->var) = (opt->type == 'b'); return; } else if (parse_bool(optarg, (bool *) opt->var)) { return; } message = "a boolean"; break; case 'f': ((pgut_optfn) opt->var)(opt, optarg); return; case 'i': if (parse_int32(optarg, opt->var)) return; message = "a 32bit signed integer"; break; case 'u': if (parse_uint32(optarg, opt->var)) return; message = "a 32bit unsigned integer"; break; case 'I': if (parse_int64(optarg, opt->var)) return; message = "a 64bit signed integer"; break; case 'U': if (parse_uint64(optarg, opt->var)) return; message = "a 64bit unsigned integer"; break; case 's': if (opt->source != SOURCE_DEFAULT) free(*(char **) opt->var); *(char **) opt->var = pgut_strdup(optarg); return; case 't': if (parse_time(optarg, opt->var)) return; message = "a time"; break; case 'y': case 'Y': if (optarg == NULL) { *(YesNo *) opt->var = (opt->type == 'y' ? YES : NO); return; } else { bool value; if (parse_bool(optarg, &value)) { *(YesNo *) opt->var = (value ? YES : NO); return; } } message = "a boolean"; break; default: ereport(ERROR, (errcode(EINVAL), errmsg("invalid option type: %c", opt->type))); return; /* keep compiler quiet */ } } if (isprint(opt->sname)) ereport(ERROR, (errcode(EINVAL), errmsg("option -%c, --%s should be %s: '%s'", opt->sname, opt->lname, message, optarg))); else ereport(ERROR, (errcode(EINVAL), errmsg("option --%s should be %s: '%s'", opt->lname, message, optarg))); }
static void http_player_control(struct shttpd_arg* arg) { HTTPAccess* access = (HTTPAccess*)arg->user_data; const char* requestURI; char queryValue[64]; int64_t offset = 0; int whence = SEEK_SET; int speed = 1; PlayUnit unit = FRAME_PLAY_UNIT; int forward = 1; int queryOk; int queryValueCount; int pause = 0; float factor; int64_t duration; int toggle = 1; int markType = 0; int64_t position = 0; unsigned int markTypeMask = 0; int vtrErrorLevel = 0; int selection = 0; requestURI = shttpd_get_env(arg, "REQUEST_URI"); if (strcmp("/player/control/play", requestURI) == 0) { mc_play(access->control); } else if (strcmp("/player/control/stop", requestURI) == 0) { mc_stop(access->control); } else if (strcmp("/player/control/pause", requestURI) == 0) { mc_pause(access->control); } else if (strcmp("/player/control/toggle-play-pause", requestURI) == 0) { mc_toggle_play_pause(access->control); } else if (strcmp("/player/control/seek", requestURI) == 0) { queryOk = 1; queryValueCount = 0; if (get_query_value(arg, "offset", queryValue, sizeof(queryValue))) { if (!parse_int64(queryValue, &offset)) { queryOk = 0; } else { queryValueCount++; } } if (get_query_value(arg, "whence", queryValue, sizeof(queryValue))) { queryValueCount++; if (strcmp("SEEK_SET", queryValue) == 0) { whence = SEEK_SET; } else if (strcmp("SEEK_CUR", queryValue) == 0) { whence = SEEK_CUR; } else if (strcmp("SEEK_END", queryValue) == 0) { whence = SEEK_END; } else { queryOk = 0; } } if (get_query_value(arg, "unit", queryValue, sizeof(queryValue))) { queryValueCount++; if (strcmp("FRAME_PLAY_UNIT", queryValue) == 0) { unit = FRAME_PLAY_UNIT; } else if (strcmp("PERCENTAGE_PLAY_UNIT", queryValue) == 0) { unit = PERCENTAGE_PLAY_UNIT; } else { queryOk = 0; } } if (get_query_value(arg, "pause", queryValue, sizeof(queryValue))) { if (strcmp("true", queryValue) == 0) { pause = 1; } } if (queryOk && queryValueCount == 3) { if (pause) { mc_pause(access->control); } mc_seek(access->control, offset, whence, unit); } } else if (strcmp("/player/control/play-speed", requestURI) == 0) { queryOk = 1; queryValueCount = 0; if (get_query_value(arg, "speed", queryValue, sizeof(queryValue))) { queryValueCount++; if ((speed = atoi(queryValue)) == 0) { queryOk = 0; } } if (get_query_value(arg, "unit", queryValue, sizeof(queryValue))) { queryValueCount++; if (strcmp("FRAME_PLAY_UNIT", queryValue) == 0) { unit = FRAME_PLAY_UNIT; } else if (strcmp("PERCENTAGE_PLAY_UNIT", queryValue) == 0) { unit = PERCENTAGE_PLAY_UNIT; } else { queryOk = 0; } } if (queryOk && queryValueCount == 2) { mc_play_speed(access->control, speed, unit); } } else if (strcmp("/player/control/play-speed-factor", requestURI) == 0) { queryOk = 1; queryValueCount = 0; factor = 0.0; if (get_query_value(arg, "factor", queryValue, sizeof(queryValue))) { queryValueCount++; factor = (float)(atof(queryValue)); if (factor == 0.0) { queryOk = 0; } } if (queryOk && queryValueCount == 1) { mc_play_speed_factor(access->control, factor); } } else if (strcmp("/player/control/step", requestURI) == 0) { queryOk = 1; queryValueCount = 0; if (get_query_value(arg, "forward", queryValue, sizeof(queryValue))) { queryValueCount++; if (strcmp("true", queryValue) == 0) { forward = 1; } else if (strcmp("false", queryValue) == 0) { forward = 0; } else { queryOk = 0; } } if (get_query_value(arg, "unit", queryValue, sizeof(queryValue))) { queryValueCount++; if (strcmp("FRAME_PLAY_UNIT", queryValue) == 0) { unit = FRAME_PLAY_UNIT; } else if (strcmp("PERCENTAGE_PLAY_UNIT", queryValue) == 0) { unit = PERCENTAGE_PLAY_UNIT; } else { queryOk = 0; } } if (queryOk && queryValueCount == 2) { mc_step(access->control, forward, unit); } } else if (strcmp("/player/control/home", requestURI) == 0) { if (get_query_value(arg, "pause", queryValue, sizeof(queryValue))) { if (strcmp("true", queryValue) == 0) { mc_pause(access->control); } } mc_seek(access->control, 0, SEEK_SET, FRAME_PLAY_UNIT); } else if (strcmp("/player/control/end", requestURI) == 0) { if (get_query_value(arg, "pause", queryValue, sizeof(queryValue))) { if (strcmp("true", queryValue) == 0) { mc_pause(access->control); } } mc_seek(access->control, 0, SEEK_END, FRAME_PLAY_UNIT); } else if (strcmp("/player/control/prev-mark", requestURI) == 0) { mc_seek_prev_mark(access->control); } else if (strcmp("/player/control/next-mark", requestURI) == 0) { mc_seek_next_mark(access->control); } else if (strcmp("/player/control/clip-mark", requestURI) == 0) { mc_seek_clip_mark(access->control); } else if (strcmp("/player/control/mark-position", requestURI) == 0) { queryOk = 1; queryValueCount = 0; if (get_query_value(arg, "position", queryValue, sizeof(queryValue))) { if (!parse_int64(queryValue, &position) || position < 0) { queryOk = 0; } else { queryValueCount++; } } if (get_query_value(arg, "type", queryValue, sizeof(queryValue))) { queryValueCount++; markType = atoi(queryValue); } if (get_query_value(arg, "toggle", queryValue, sizeof(queryValue))) { queryValueCount++; if (strcmp("true", queryValue) == 0) { toggle = 1; } else if (strcmp("false", queryValue) == 0) { toggle = 0; } else { queryOk = 0; } } if (queryOk && queryValueCount == 3) { mc_mark_position(access->control, position, markType, toggle); } } else if (strcmp("/player/control/clear-mark-position", requestURI) == 0) { queryOk = 1; queryValueCount = 0; if (get_query_value(arg, "position", queryValue, sizeof(queryValue))) { if (!parse_int64(queryValue, &position) || position < 0) { queryOk = 0; } else { queryValueCount++; } } if (get_query_value(arg, "type", queryValue, sizeof(queryValue))) { queryValueCount++; markTypeMask = atoi(queryValue); } if (queryOk && queryValueCount == 2) { mc_clear_mark_position(access->control, position, markTypeMask); } } else if (strcmp("/player/control/next-show-marks", requestURI) == 0) { queryOk = 1; selection = 0; if (get_query_value(arg, "sel", queryValue, sizeof(queryValue))) { if (!parse_int(queryValue, &selection)) { queryOk = 0; } } if (queryOk) { mc_next_show_marks(access->control, selection); } } else if (strcmp("/player/control/set-vtr-error-level", requestURI) == 0) { queryOk = 1; queryValueCount = 0; if (get_query_value(arg, "level", queryValue, sizeof(queryValue))) { if (!parse_int(queryValue, &vtrErrorLevel) || vtrErrorLevel < VTR_NO_ERROR_LEVEL || vtrErrorLevel > VTR_NO_GOOD_LEVEL) { queryOk = 0; } else { queryValueCount++; } } if (queryOk && queryValueCount == 1) { mc_set_vtr_error_level(access->control, (VTRErrorLevel)vtrErrorLevel); } } else if (strcmp("/player/control/next-vtr-error-level", requestURI) == 0) { mc_next_vtr_error_level(access->control); } else if (strcmp("/player/control/next-osd-screen", requestURI) == 0) { mc_next_osd_screen(access->control); } else if (strcmp("/player/control/next-osd-timecode", requestURI) == 0) { mc_next_osd_timecode(access->control); } else if (strcmp("/player/control/review", requestURI) == 0) { queryOk = 1; queryValueCount = 0; if (get_query_value(arg, "duration", queryValue, sizeof(queryValue))) { if (!parse_int64(queryValue, &duration) || duration <= 0) { queryOk = 0; } else { queryValueCount++; } } if (queryOk && queryValueCount == 1) { mc_review(access->control, duration); } } else if (strcmp("/player/control/next-marks-selection", requestURI) == 0) { mc_next_active_mark_selection(access->control); } shttpd_printf(arg, "HTTP/1.1 200 OK\r\n\r\n"); arg->flags |= SHTTPD_END_OF_OUTPUT; }
/* parses a 4-touple of the form {x, y, z, w} * where x, y, z, w are numbers */ static boolean parse_immediate_data(struct translate_ctx *ctx, unsigned type, union tgsi_immediate_data *values) { unsigned i; int ret; eat_opt_white( &ctx->cur ); if (*ctx->cur != '{') { report_error( ctx, "Expected `{'" ); return FALSE; } ctx->cur++; for (i = 0; i < 4; i++) { eat_opt_white( &ctx->cur ); if (i > 0) { if (*ctx->cur != ',') { report_error( ctx, "Expected `,'" ); return FALSE; } ctx->cur++; eat_opt_white( &ctx->cur ); } switch (type) { case TGSI_IMM_FLOAT64: ret = parse_double(&ctx->cur, &values[i].Uint, &values[i+1].Uint); i++; break; case TGSI_IMM_INT64: ret = parse_int64(&ctx->cur, &values[i].Uint, &values[i+1].Uint); i++; break; case TGSI_IMM_UINT64: ret = parse_uint64(&ctx->cur, &values[i].Uint, &values[i+1].Uint); i++; break; case TGSI_IMM_FLOAT32: ret = parse_float(&ctx->cur, &values[i].Float); break; case TGSI_IMM_UINT32: ret = parse_uint(&ctx->cur, &values[i].Uint); break; case TGSI_IMM_INT32: ret = parse_int(&ctx->cur, &values[i].Int); break; default: assert(0); ret = FALSE; break; } if (!ret) { report_error( ctx, "Expected immediate constant" ); return FALSE; } } eat_opt_white( &ctx->cur ); if (*ctx->cur != '}') { report_error( ctx, "Expected `}'" ); return FALSE; } ctx->cur++; return TRUE; }
static int key_value_parse_line_with_key(struct key_value_specification *k, char *line, void *base_address[]) { char *valuestr; int rc, len, klen; klen = strlen(k->key); len = strlen(line); if (len < klen + 1) return 1; if (strncmp(line, k->key, klen) != 0) return 1; if (line[klen] != ':') return 1; /* Skip leading spaces */ klen = klen + 1; while (line[klen] == ' ') klen++; /* At this point, we have a match on the key */ valuestr = &line[klen]; switch (k->type) { case KVS_STRING: rc = parse_string(valuestr, k, base_address); break; case KVS_INT64: rc = parse_int64(valuestr, k, base_address); break; case KVS_INT32: rc = parse_int32(valuestr, k, base_address); break; case KVS_INT16: rc = parse_int16(valuestr, k, base_address); break; case KVS_INT8: rc = parse_int8(valuestr, k, base_address); break; case KVS_UINT64: rc = parse_uint64(valuestr, k, base_address); break; case KVS_UINT32: rc = parse_uint32(valuestr, k, base_address); break; case KVS_UINT16: rc = parse_uint16(valuestr, k, base_address); break; case KVS_UINT8: rc = parse_uint8(valuestr, k, base_address); break; case KVS_DOUBLE: rc = parse_double(valuestr, k, base_address); break; case KVS_FLOAT: rc = parse_float(valuestr, k, base_address); break; default: fprintf(stderr, "%s:%d: unknown key type '%c' for key '%s'\n", __func__, __LINE__, k->type, k->key); rc = -1; break; } return rc; }