PRIVATE int parseFeed(rss_feeds *feeds, const char* feedstr) { rss_feed* feed = NULL; int32_t result = SUCCESS; /* be optimistic */ simple_list option_list = NULL; NODE * current = NULL; suboption_t *opt_item = NULL; option_list = parseMultiOption(feedstr); current = option_list; while (current != NULL) { opt_item = (suboption_t*)current->data; if(opt_item != NULL) { if(!feed) { feed = feed_new(); assert(feed && "feed_new() failed!"); } if(!strncmp(opt_item->option, "url_pattern", 11)) { feed->url_pattern = trim(opt_item->value); } else if(!strncmp(opt_item->option, "url_replace", 11)) { feed->url_replace = trim(opt_item->value); } else if(!strncmp(opt_item->option, "url", 3)) { feed->url = trim(opt_item->value); } else if(!strncmp(opt_item->option, "cookies", 6)) { feed->cookies = trim(opt_item->value); } else if(!strncmp(opt_item->option, "id", 2)) { feed->id = trim(opt_item->value); } else { dbg_printf(P_ERROR, "Unknown suboption '%s'!", opt_item->option); } } else { assert(0 && "opt_item == NULL"); } current = current->next; } if(feed && feed->url) { /* Maybe the cookies are encoded within the URL */ if(feed->cookies == NULL) { parseCookiesFromURL(feed); } feed_add(feed, feeds); } else { dbg_printf(P_ERROR, "Invalid feed: '%s'", feedstr); feed_free(feed); result = FAILURE; } if(option_list != NULL) { freeList(&option_list, freeOptionItem); } return result; }
static boolean parseAnOption(struct hash *hash, char *arg, struct optionSpec *optionSpecs) /* Parse a single option argument and add to the hash, validating if * optionSpecs is not NULL. Return TRUE if it's arg is an option argument * FALSE if it's not. */ { char *name, *val; char *eqPtr = strchr(arg, '='); if (!((eqPtr != NULL) || (arg[0] == '-'))) return FALSE; /* not an option */ /* A dash by itself is not an option. It can mean * negative strand for some of the DNA oriented utilities. */ if (arg[0] == '-' && (arg[1] == 0 || isspace(arg[1]))) return FALSE; /* It's nice to be able to use url's in the command line, but they * may have = in them... */ if (startsWith("http://", arg) || startsWith("https://", arg) || startsWith("ftp://", arg)) return FALSE; name = arg; if (name[0] == '-') name++; if (eqPtr != NULL) { *eqPtr = '\0'; val = eqPtr+1; } else val = NULL; if (optionSpecs != NULL) validateOption(name, val, optionSpecs); if (val == NULL) val = "on"; if (optionSpecs == NULL) hashAdd(hash, name, val); else { struct optionSpec *spec = matchingOption(name, optionSpecs); if (spec != NULL && (spec->flags & OPTION_MULTI)) /* process multiple instances of option */ parseMultiOption(hash, name, val, spec); else hashAdd(hash, name, val); } if (eqPtr != NULL) *eqPtr = '='; return TRUE; }
PRIVATE int parseFilter(am_filters *filters, const char* filter_str) { am_filter filter = NULL; int32_t result = SUCCESS; /* be optimistic */ simple_list option_list = NULL; NODE * current = NULL; suboption_t *opt_item = NULL; char *tmpStr = NULL; option_list = parseMultiOption(filter_str); current = option_list; while (current != NULL) { opt_item = (suboption_t*)current->data; if(opt_item != NULL) { if(!filter) { filter = filter_new(); assert(filter && "filter_new() failed!"); } if(!strncmp(opt_item->option, "pattern", 7)) { filter->pattern = trim(opt_item->value); } else if(!strncmp(opt_item->option, "folder", 6)) { tmpStr = trim(opt_item->value); set_path(tmpStr, &filter->folder); am_free(tmpStr); } else if(!strncmp(opt_item->option, "feedid", 6)) { filter->feedID = trim(opt_item->value); } else { dbg_printf(P_ERROR, "Unknown suboption '%s'!", opt_item->option); } } else { assert(0 && "opt_item == NULL"); } current = current->next; } if(filter && filter->pattern) { filter_add(filter, filters); } else { dbg_printf(P_ERROR, "Invalid filter: '%s'", filter_str); filter_free(filter); result = FAILURE; } if(option_list != NULL) { freeList(&option_list, freeOptionItem); } return result; }
static boolean parseAnOption(struct hash *hash, char *arg, struct optionSpec *optionSpecs) /* Parse a single option argument and add to the hash, validating if * optionSpecs is not NULL. Return TRUE if it's arg is an option argument * FALSE if it's not. */ { char *name, *val; char *eqPtr = strchr(arg, '='); if (!((eqPtr != NULL) || (arg[0] == '-'))) return FALSE; /* not an option */ /* A dash by itself is not an option. It can mean * negative strand for some of the DNA oriented utilities. */ if (arg[0] == '-' && (arg[1] == 0 || isspace(arg[1]))) return FALSE; /* We treat this=that as an option only if the '=' happens before any non-alphanumeric * characters. This lets us have URLs and SQL statements in the command line even though * they can have equals in them. */ if (eqPtr != NULL) { char *s, c; for (s=arg; s < eqPtr; ++s) { c = *s; if (c != '_' && c != '-' && !isalnum(c)) return FALSE; } } name = arg; if (name[0] == '-') name++; if (eqPtr != NULL) { *eqPtr = '\0'; val = eqPtr+1; } else val = NULL; if (optionSpecs != NULL) validateOption(name, val, optionSpecs); if (val == NULL) val = "on"; if (optionSpecs == NULL) hashAdd(hash, name, val); else { struct optionSpec *spec = matchingOption(name, optionSpecs); if (spec != NULL && (spec->flags & OPTION_MULTI)) /* process multiple instances of option */ parseMultiOption(hash, name, val, spec); else hashAdd(hash, name, val); } if (eqPtr != NULL) *eqPtr = '='; return TRUE; }