diff --git a/src/distroquery.c b/src/distroquery.c index 544b56d..5b8367c 100644 --- a/src/distroquery.c +++ b/src/distroquery.c @@ -72,17 +72,68 @@ #include "functions.h" static struct configTag *firstconfigtag = NULL, *configtag = NULL; -char *repository_tag = NULL; char *query; +char *query_package; +char *query_repository; +char *query_arch; struct configTag *query_repositories[100]; int search_milestone1 = 0, search_milestone2 = 1, search_devel = 1; int search_arm = 0, search_x86_64 = 0, search_i586 = 1, search_sources = 0; -void printInputForm(char *data) { +/* Converts a hex character to its integer value */ +char from_hex(char ch) { + return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; +} + +/* Converts an integer value to its hex character*/ +char to_hex(char code) { + static char hex[] = "0123456789abcdef"; + return hex[code & 15]; +} + +/* Returns a url-encoded version of str */ +/* IMPORTANT: be sure to free() the returned string after use */ +char *url_encode(char *str) { + char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf; + while (*pstr) { + if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') + *pbuf++ = *pstr; + else if (*pstr == ' ') + *pbuf++ = '+'; + else + *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); + pstr++; + } + *pbuf = '\0'; + return buf; +} + +/* Returns a url-decoded version of str */ +/* IMPORTANT: be sure to free() the returned string after use */ +char *url_decode(char *str) { + char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf; + while (*pstr) { + if (*pstr == '%') { + if (pstr[1] && pstr[2]) { + *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); + pstr += 2; + } + } else if (*pstr == '+') { + *pbuf++ = ' '; + } else { + *pbuf++ = *pstr; + } + pstr++; + } + *pbuf = '\0'; + return buf; +} + +void printInputForm() { struct configTag *ct = firstconfigtag; - const char ajax_call[] = "ajax_getvalues(" + const char ajax_call[] = "distroquery_request(" // "'repository='+getElementById('repository').value" "'query='+getElementById('query').value+" "'&search_devel='+getElementById('search_devel').checked+" @@ -119,7 +170,7 @@ void printInputForm(char *data) { printf("]]>"); } -void printQueryData(char* repository_tag, char* query) { +void printQueryData() { int a, i, j, k, numresults = 0; char dbname[PATH_MAX]; @@ -127,6 +178,9 @@ void printQueryData(char* repository_tag, char* query) { char sql[PATH_MAX]; printf("repository_dir); @@ -136,7 +190,7 @@ void printQueryData(char* repository_tag, char* query) { fprintf(stderr, "ERROR: unable to open sqlite3 db %s; aborting.\n", dbname); } } - + for (i = 0; query_repositories[i] != NULL; i++) { for (a = 0; a < ARCHS_MAX && query_repositories[i]->arch[a]; a++) { if ((search_i586 && !strcmp(query_repositories[i]->arch[a],"i586")) || @@ -147,18 +201,46 @@ void printQueryData(char* repository_tag, char* query) { (sqlite3_prepare_v2((sqlite3*)query_repositories[i]->db, sql, strlen(sql), &statement, NULL) == SQLITE_OK)) { while (sqlite3_step(statement) == SQLITE_ROW) { numresults++; - printf("

%s
 " + printf("


%s
 " "
%s

" - "%s %s-%s
%s
Group: %s

" - "
%s

", + "%s %s-%s
%s
Group: %s", query_repositories[i]->arch[a], query_repositories[i]->tag, sqlite3_column_text(statement,1), sqlite3_column_text(statement,4), sqlite3_column_text(statement,5), sqlite3_column_text(statement,6), - sqlite3_column_text(statement,7), + sqlite3_column_text(statement,7)); + + printf("

%s
", sqlite3_column_text(statement,8)); + + /* install */ + printf("
" + "
" + "" + "
", + sqlite3_column_text(statement,1), + sqlite3_column_text(statement,6), + sqlite3_column_text(statement,1)); + + /* download */ + printf("
" + " Download
", + query_repositories[i]->download_prefix, + query_repositories[i]->download_dir, + sqlite3_column_text(statement,1), + sqlite3_column_text(statement,4), + sqlite3_column_text(statement,5)); + /* details */ + printf("
" + " Details
", + query_repositories[i]->tag, + sqlite3_column_text(statement,1), + query_repositories[i]->arch[a]); + + printf("

"); } } } @@ -201,6 +283,78 @@ void printQueryData(char* repository_tag, char* query) { } } +void printPackageData() { + + int a, i, j, k, numresults = 0; + char dbname[PATH_MAX]; + sqlite3_stmt *statement; + char sql[PATH_MAX]; + struct configTag* ct = findRepositoryByTag(query_repository); + + printf("repository_dir); + if (sqlite3_open_v2(dbname, (sqlite3**)&ct->db, SQLITE_OPEN_READONLY, NULL)) { + if (ct->db) sqlite3_close(ct->db); + ct->db = NULL; + fprintf(stderr, "ERROR: unable to open sqlite3 db %s; aborting.\n", dbname); + return; + } + + snprintf(sql, PATH_MAX, "SELECT * FROM packages_%s WHERE name = '%s'", query_arch, query_package); + if (ct->db && + (sqlite3_prepare_v2((sqlite3*)ct->db, sql, strlen(sql), &statement, NULL) == SQLITE_OK)) { + while (sqlite3_step(statement) == SQLITE_ROW) { + numresults++; + printf("

%s
 " + "
%s

" + "%s %s-%s
%s
Group: %s" + "

%s

", + query_repositories[i]->arch[a], + query_repositories[i]->tag, + sqlite3_column_text(statement,1), + sqlite3_column_text(statement,4), + sqlite3_column_text(statement,5), + sqlite3_column_text(statement,6), + sqlite3_column_text(statement,7), + sqlite3_column_text(statement,8)); + } + } + printf("\n"); + +/* if (search_sources) { + snprintf(sql, PATH_MAX, "SELECT * FROM sources WHERE name LIKE '%%%s%%' OR summary LIKE '%%%s%%' OR description LIKE '%%%s%%'", query, query, query); + for (i = 0; query_repositories[i] != NULL; i++) { + if (query_repositories[i]->db && + (sqlite3_prepare_v2((sqlite3*)query_repositories[i]->db, sql, strlen(sql), &statement, NULL) == SQLITE_OK)) { + while (sqlite3_step(statement) == SQLITE_ROW) { + numresults++; + printf("

Source
 " + "
%s

" + "%s %s-%s
%s
Group: %s
License: %s
URL: %s

" + "
%s

", + query_repositories[i]->tag, + sqlite3_column_text(statement,1), + sqlite3_column_text(statement,4), + sqlite3_column_text(statement,5), + sqlite3_column_text(statement,6), + sqlite3_column_text(statement,8), + sqlite3_column_text(statement,11), + sqlite3_column_text(statement,10), + sqlite3_column_text(statement,9)); + } + } + printf("\n"); + } + } + + printf("

%d result(s) found.", numresults);*/ + printf("]]>
"); + + sqlite3_close(ct->db); + ct->db = NULL; +} + void parse_request_variables(char *data) { char *vartok, *valuetok; struct configTag *ct = firstconfigtag; @@ -211,23 +365,27 @@ void parse_request_variables(char *data) { if (data) data = NULL; if (!vartok || (vartok[0] == '\0') ) break; valuetok = (char *) strtok(NULL, "&"); - if (strstr(vartok, "repository")) { - repository_tag = valuetok; - } else if (strstr(vartok, "query")) { - query = valuetok; - } else if (strstr(vartok, "search_milestone2")) { + if (!strcmp(vartok, "repository")) { + query_repository = valuetok; + } else if (!strcmp(vartok, "package")) { + query_package = valuetok; + } else if (!strcmp(vartok, "arch")) { + query_arch = valuetok; + } else if (!strcmp(vartok, "query")) { + query = url_decode(valuetok); + } else if (!strcmp(vartok, "search_milestone2")) { search_milestone2 = (strstr(valuetok, "false") != valuetok); - } else if (strstr(vartok, "search_milestone1")) { + } else if (!strcmp(vartok, "search_milestone1")) { search_milestone1 = (strstr(valuetok, "false") != valuetok); - } else if (strstr(vartok, "search_devel")) { + } else if (!strcmp(vartok, "search_devel")) { search_devel = (strstr(valuetok, "false") != valuetok); - } else if (strstr(vartok, "search_i586")) { + } else if (!strcmp(vartok, "search_i586")) { search_i586 = (strstr(valuetok, "false") != valuetok); - } else if (strstr(vartok, "search_x86_64")) { + } else if (!strcmp(vartok, "search_x86_64")) { search_x86_64 = (strstr(valuetok, "false") != valuetok); - } else if (strstr(vartok, "search_arm")) { + } else if (!strcmp(vartok, "search_arm")) { search_arm = (strstr(valuetok, "false") != valuetok); - } else if (strstr(vartok, "search_sources")) { + } else if (!strcmp(vartok, "search_sources")) { search_sources = (strstr(valuetok, "false") != valuetok); } } @@ -263,14 +421,15 @@ main(int argc, char *argv[]) parse_request_variables(data); } - if (query && strlen(query)) { - printQueryData(repository_tag, query); - printf("\n"); - exit(0); + if (query_package && strlen(query_package)) { + printPackageData(); + } else if (query && strlen(query)) { + printQueryData(); + } else { + printInputForm(); } - printInputForm(data); - printf("\n"); exit(0); } +