Thread: [Refdb-cvs] CVS: refdb/src refdbc.c,1.72.2.6,1.72.2.7
Status: Beta
Brought to you by:
mhoenicka
From: Markus H. <mho...@us...> - 2005-05-05 17:26:11
|
Update of /cvsroot/refdb/refdb/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20702 Modified Files: Tag: Release_0_9_5_stable refdbc.c Log Message: close pipe properly if server cannot open db; removed some cgi stuff Index: refdbc.c =================================================================== RCS file: /cvsroot/refdb/refdb/src/refdbc.c,v retrieving revision 1.72.2.6 retrieving revision 1.72.2.7 diff -u -U2 -r1.72.2.6 -r1.72.2.7 --- refdbc.c 19 Apr 2005 19:39:58 -0000 1.72.2.6 +++ refdbc.c 5 May 2005 17:25:58 -0000 1.72.2.7 @@ -1785,352 +1785,178 @@ strcpy(my_toencoding, toencoding); /* use default encoding if set */ - if (n_cgi) { -#ifdef REFDB_CGI - /* this code must handle the following actions: - 1) simple query - 2) advanced query - 3) edit reference */ - - if ((ptr_current = get_liliform((struct liliform*)arg, "scope")) != NULL) { - if (strcmp(ptr_current->value, "prl") == 0) { - n_limit_to_user = 1; - } - } + /* if (n_batchmode) { */ /* in batchmode, the command line is already tokenized */ + /* inargc = main_argc; */ + /* inargv = main_argv; */ + /* } */ + /* else { */ + /* parse the argument. first we cut the argument + into pieces with strtok, then we use getopt to interpret */ - ptr_current = (struct liliform*)arg; - while ((ptr_current = get_liliform(ptr_current, "morefields")) != NULL) { - if (strlen(format_string) >= MAX_FMT_LEN-2) { - break; /* the correct search page will never cause this, but some - malevolent person might alter the page to overflow this - buffer on purpose */ - } - strcat(format_string, ptr_current->value); - } + /* get a buffer to hold the tokens. Start with 10 tokens, + increase in steps of 10 as needed */ + inargc = 0; + inargcmax = 10; + inargv = (char**)malloc((size_t)inargcmax*sizeof(char*)); + if (inargv == NULL) { + return 1; + } - if ((ptr_current = get_nliliform((struct liliform*)arg, "editid", 6)) != NULL) { - /* we want to retrieve a reference with a given ID in RIS format */ - strcat(slvals.outbuffer, ":ID:="); - strcat(slvals.outbuffer, &((ptr_current->name)[6])); - strcpy(type_string, "cgiris"); - } - else if ((ptr_current = get_liliform((struct liliform*)arg, "advquery")) != NULL) { - /* this is a valid query string (hopefully) */ - strcat(slvals.outbuffer, "\""); - strcat(slvals.outbuffer, ptr_current->value); - strcat(slvals.outbuffer, "\""); - strcpy(type_string, "cgi"); - } - else { - /* we have to evaluate four fields in the form and connect them - appropriately */ + if (insert_lilimem(&sentinel, (void**)&inargv, NULL)) { + return 1; + } - /* the query string proper has to be enclosed in quotation marks */ - strcat(slvals.outbuffer, "\""); + /* the following is a temporary hack to allow cmdln_tokenize to work */ + newarg = (char*)malloc((size_t)(strlen(arg)+8)); + if (newarg == NULL) { + delete_all_lilimem(&sentinel); + return 1; + } - /* first field */ - ptr_current = get_liliform((struct liliform*)arg, "simpquery1field"); - if (ptr_current) { - if (add_field_contents(slvals.outbuffer, ptr_current, (struct liliform*)arg, "simpquery1field", "simpquery1")) { - goto simpdone; - } - - if (get_liliform((struct liliform*)arg, "simpquery2")) { - ptr_current = get_liliform((struct liliform*)arg, "simpquery1op"); - if (ptr_current) { - if (!strcmp(ptr_current->value, "not")) { - strcat(slvals.outbuffer, " AND NOT ("); - n_open_andnot = 1; - } - else if (!strcmp(ptr_current->value, "or")) { - strcat(slvals.outbuffer, " OR "); - } - else { /* default is author */ - strcat(slvals.outbuffer, " AND "); - } - } - else { /* use AND as default connector */ - strcat(slvals.outbuffer, " AND "); - } - } - else { - goto simpdone; - } - } - else { - goto simpqueryerror; - } + if (insert_lilimem(&sentinel, (void**)&newarg, NULL)) { + delete_all_lilimem(&sentinel); + return 1; + } - /* second field */ - ptr_current = get_liliform((struct liliform*)arg, "simpquery2field"); - if (ptr_current) { - if (add_field_contents(slvals.outbuffer, ptr_current, (struct liliform*)arg, "simpquery2field", "simpquery2")) { - goto simpqueryerror; - } - - if (n_open_andnot) { - strcat(slvals.outbuffer, ")"); - n_open_andnot = 0; - } - - if (get_liliform((struct liliform*)arg, "simpquery3")) { - ptr_current = get_liliform((struct liliform*)arg, "simpquery2op"); - if (ptr_current) { - if (!strcmp(ptr_current->value, "not")) { - strcat(slvals.outbuffer, " AND NOT ("); - n_open_andnot = 1; - } - else if (!strcmp(ptr_current->value, "or")) { - strcat(slvals.outbuffer, " OR "); - } - else { /* default is author */ - strcat(slvals.outbuffer, " AND "); - } - } - else { /* use AND as default connector */ - strcat(slvals.outbuffer, " AND "); - } - } - else { - goto simpdone; - } - } + strcpy(newarg, "getref "); + strcat(newarg, arg); - /* third field */ - ptr_current = get_liliform((struct liliform*)arg, "simpquery3field"); - if (ptr_current) { - if (add_field_contents(slvals.outbuffer, ptr_current, (struct liliform*)arg, "simpquery3field", "simpquery3")) { - goto simpqueryerror; - } - - if (n_open_andnot) { - strcat(slvals.outbuffer, ")"); - n_open_andnot = 0; - } - - if (get_liliform((struct liliform*)arg, "simpquery4")) { - ptr_current = get_liliform((struct liliform*)arg, "simpquery3op"); - if (ptr_current) { - if (!strcmp(ptr_current->value, "not")) { - strcat(slvals.outbuffer, " AND NOT ("); - n_open_andnot = 1; - } - else if (!strcmp(ptr_current->value, "or")) { - strcat(slvals.outbuffer, " OR "); - } - else { /* default is author */ - strcat(slvals.outbuffer, " AND "); - } - } - else { /* use AND as default connector */ - strcat(slvals.outbuffer, " AND "); - } - } - else { - goto simpdone; - } - } + result = cmdln_tokenize(&inargc, &inargv, inargcmax, newarg); - /* fourth field */ - ptr_current = get_liliform((struct liliform*)arg, "simpquery4field"); - if (ptr_current) { - if (add_field_contents(slvals.outbuffer, ptr_current, (struct liliform*)arg, "simpquery4field", "simpquery4")) { - goto simpqueryerror; - } - if (n_open_andnot) { - strcat(slvals.outbuffer, ")"); - n_open_andnot = 0; - } - } - else { - goto simpdone; - } - - simpqueryerror: - cgi_header(CGI_PLAIN); - fprintf(errstream, "could not make sense of the query input\n"); - return 1; - simpdone: - strcat(slvals.outbuffer, "\""); - strcpy(type_string, "cgi"); - } -#endif /* REFDB_CGI */ + if (result == 1 || result == 2) { /* memory error */ + delete_all_lilimem(&sentinel); + return 1; } - else { /* not cgi */ -/* if (n_batchmode) { */ /* in batchmode, the command line is already tokenized */ -/* inargc = main_argc; */ -/* inargv = main_argv; */ -/* } */ -/* else { */ - /* parse the argument. first we cut the argument - into pieces with strtok, then we use getopt to interpret */ + /* } */ - /* get a buffer to hold the tokens. Start with 10 tokens, - increase in steps of 10 as needed */ - inargc = 0; - inargcmax = 10; - inargv = (char**)malloc((size_t)inargcmax*sizeof(char*)); - if (inargv == NULL) { - return 1; - } + /* now we have the tokens nicely arranged in inargc */ + /* for (i = 0; i < inargc; i++) { */ + /* printf("inargv[%d]: %s\n", i, inargv[i]); */ + /* } */ - if (insert_lilimem(&sentinel, (void**)&inargv, NULL)) { - return 1; - } + /* get options */ + optind = 0; - /* the following is a temporary hack to allow cmdln_tokenize to work */ - newarg = (char*)malloc((size_t)(strlen(arg)+8)); - if (newarg == NULL) { + while ((n_opt = getopt(inargc, inargv, "ac:C:d:e:E:f:F:g:G:hi:kl:L:o:O:p:PqrR:s:S:t:T:u:U:vVw:")) != -1) { + switch(n_opt) { + case 'c': + /* printf("-c %s\n", optarg); */ + slvals.outpipe = (char*)malloc(strlen(optarg)+1); + if (slvals.outpipe == NULL) { delete_all_lilimem(&sentinel); - return 1; + return 0; } - - if (insert_lilimem(&sentinel, (void**)&newarg, NULL)) { + strcpy(slvals.outpipe, optarg); + if (insert_lilimem(&sentinel, (void**)&slvals.outpipe, NULL)) { delete_all_lilimem(&sentinel); return 1; } - strcpy(newarg, "getref "); - strcat(newarg, arg); - - result = cmdln_tokenize(&inargc, &inargv, inargcmax, newarg); - - - if (result == 1 || result == 2) { /* memory error */ - delete_all_lilimem(&sentinel); - return 1; + slvals.n_pipe = 1; + break; + case 'd': + /* printf("-d %s\n", optarg); */ + strncpy(db, optarg, _POSIX_PATH_MAX - 1); /* override preset db */ + db[_POSIX_PATH_MAX-1] = '\0'; /* terminate in case string got truncated */ + break; + case 'E': + strncpy(my_toencoding, optarg, PREFS_BUF_LEN); + my_toencoding[PREFS_BUF_LEN-1] = '\0'; + break; + case 'f': + /* printf("-f %s\n", optarg); */ + if (!strcmp(optarg, "stdin")) { + n_read_stdin = 1; } -/* } */ - - /* now we have the tokens nicely arranged in inargc */ -/* for (i = 0; i < inargc; i++) { */ -/* printf("inargv[%d]: %s\n", i, inargv[i]); */ -/* } */ - - /* get options */ - optind = 0; - - while ((n_opt = getopt(inargc, inargv, "ac:C:d:e:E:f:F:g:G:hi:kl:L:o:O:p:PqrR:s:S:t:T:u:U:vVw:")) != -1) { - switch(n_opt) { - case 'c': - /* printf("-c %s\n", optarg); */ - slvals.outpipe = (char*)malloc(strlen(optarg)+1); - if (slvals.outpipe == NULL) { - delete_all_lilimem(&sentinel); - return 0; - } - strcpy(slvals.outpipe, optarg); - if (insert_lilimem(&sentinel, (void**)&slvals.outpipe, NULL)) { + else { + infile = canonicalize_path(optarg); + if (insert_lilimem(&sentinel, (void**)&infile, NULL)) { delete_all_lilimem(&sentinel); return 1; } - - slvals.n_pipe = 1; - break; - case 'd': - /* printf("-d %s\n", optarg); */ - strncpy(db, optarg, _POSIX_PATH_MAX - 1); /* override preset db */ - db[_POSIX_PATH_MAX-1] = '\0'; /* terminate in case string got truncated */ - break; - case 'E': - strncpy(my_toencoding, optarg, PREFS_BUF_LEN); - my_toencoding[PREFS_BUF_LEN-1] = '\0'; - break; - case 'f': - /* printf("-f %s\n", optarg); */ - if (!strcmp(optarg, "stdin")) { - n_read_stdin = 1; - } - else { - infile = canonicalize_path(optarg); - if (insert_lilimem(&sentinel, (void**)&infile, NULL)) { - delete_all_lilimem(&sentinel); - return 1; - } - n_read_file = 1; - } - break; - case 'h': - printf("Displays the result of a database search.\nSyntax: getref [-c command] [-d database] [-E encoding] [-h] [-o outfile] [-O outfile][-P] [-R pdfroot] [-s format] [-S tag] [-t output-format] {search-string|-f infile}\nSearch-string: {:XY:{<|=|~|!=|!~|>}{string|regexp}} [AND|OR|AND NOT] [...]\nwhere XY specifies the field to search in\nOptions: -c command pipe the output through command\n -d database specify the database to work with\n -E encoding set the output character encoding\n -h prints this mini-help\n -o outfile save the output in outfile (overwrite)\n -O outfile append the output to outfile\n -P limit search to personal interest list\n -R use pdfroot as root for path of pdf files\n -s format specify fields for screen or style for DocBook output\n -S tag sort output by tag ID (default) or PY\n -t output-format display as format scrn, html, xhtml, db31, db31x, teix, ris, risx, or bibtex\n -f infile use the saved search line in file infile\n All other arguments are interpreted as the search string.\n"); + n_read_file = 1; + } + break; + case 'h': + printf("Displays the result of a database search.\nSyntax: getref [-c command] [-d database] [-E encoding] [-h] [-o outfile] [-O outfile][-P] [-R pdfroot] [-s format] [-S tag] [-t output-format] {search-string|-f infile}\nSearch-string: {:XY:{<|=|~|!=|!~|>}{string|regexp}} [AND|OR|AND NOT] [...]\nwhere XY specifies the field to search in\nOptions: -c command pipe the output through command\n -d database specify the database to work with\n -E encoding set the output character encoding\n -h prints this mini-help\n -o outfile save the output in outfile (overwrite)\n -O outfile append the output to outfile\n -P limit search to personal interest list\n -R use pdfroot as root for path of pdf files\n -s format specify fields for screen or style for DocBook output\n -S tag sort output by tag ID (default) or PY\n -t output-format display as format scrn, html, xhtml, db31, db31x, teix, ris, risx, or bibtex\n -f infile use the saved search line in file infile\n All other arguments are interpreted as the search string.\n"); + delete_all_lilimem(&sentinel); + return 0; + break; + case 'o': + /* printf("-o %s\n", optarg); */ + slvals.outfile = canonicalize_path(optarg); + if (insert_lilimem(&sentinel, (void**)&slvals.outfile, NULL)) { delete_all_lilimem(&sentinel); - return 0; - break; - case 'o': - /* printf("-o %s\n", optarg); */ - slvals.outfile = canonicalize_path(optarg); - if (insert_lilimem(&sentinel, (void**)&slvals.outfile, NULL)) { - delete_all_lilimem(&sentinel); - return 1; - } - slvals.n_file_open = 1; - break; - case 'O': - /* printf("-O %s\n", optarg); */ - slvals.outfile = canonicalize_path(optarg); - if (insert_lilimem(&sentinel, (void**)&slvals.outfile, NULL)) { - delete_all_lilimem(&sentinel); - return 1; - } - slvals.n_file_append = 1; - break; - case 'P': - /* printf("-p %s\n", optarg); */ - n_limit_to_user = 1; - break; - case 'R': - strcpy(pdf_root, optarg); - break; - case 's': - /* printf("-s %s\n", optarg); */ - strncpy(format_string, optarg, MAX_FMT_LEN - 1); - format_string[MAX_FMT_LEN-1] = '\0'; /* terminate in case string got truncated */ - break; - case 'S': - /* printf("-S %s\n", optarg); */ - strncpy(sort_string, optarg, MAX_FMT_LEN - 1); - sort_string[MAX_FMT_LEN-1] = '\0'; /* terminate in case string got truncated */ - break; - case 't': - /* printf("-t %s\n", optarg); */ - strncpy(type_string, optarg, 9); - type_string[9] = '\0'; /* terminate in case string got truncated */ - break; - /* now all the options that main has already taken care of */ - case 'a': - case 'C': /* fall through */ - case 'e': - case 'F': - case 'g': - case 'G': - case 'i': - case 'k': - case 'l': - case 'L': - case 'p': - case 'q': - case 'r': - case 'T': - case 'u': - case 'U': - case 'v': - case 'V': - case 'w': - break; - case ':': - fprintf(stderr, "missing option\n"); - n_cmdlinerror = 1; - break; - case '?': - fprintf(stderr, "unknown option\n"); - n_cmdlinerror = 1; - break; + return 1; } + slvals.n_file_open = 1; + break; + case 'O': + /* printf("-O %s\n", optarg); */ + slvals.outfile = canonicalize_path(optarg); + if (insert_lilimem(&sentinel, (void**)&slvals.outfile, NULL)) { + delete_all_lilimem(&sentinel); + return 1; + } + slvals.n_file_append = 1; + break; + case 'P': + /* printf("-p %s\n", optarg); */ + n_limit_to_user = 1; + break; + case 'R': + strcpy(pdf_root, optarg); + break; + case 's': + /* printf("-s %s\n", optarg); */ + strncpy(format_string, optarg, MAX_FMT_LEN - 1); + format_string[MAX_FMT_LEN-1] = '\0'; /* terminate in case string got truncated */ + break; + case 'S': + /* printf("-S %s\n", optarg); */ + strncpy(sort_string, optarg, MAX_FMT_LEN - 1); + sort_string[MAX_FMT_LEN-1] = '\0'; /* terminate in case string got truncated */ + break; + case 't': + /* printf("-t %s\n", optarg); */ + strncpy(type_string, optarg, 9); + type_string[9] = '\0'; /* terminate in case string got truncated */ + break; + /* now all the options that main has already taken care of */ + case 'a': + case 'C': /* fall through */ + case 'e': + case 'F': + case 'g': + case 'G': + case 'i': + case 'k': + case 'l': + case 'L': + case 'p': + case 'q': + case 'r': + case 'T': + case 'u': + case 'U': + case 'v': + case 'V': + case 'w': + break; + case ':': + fprintf(stderr, "missing option\n"); + n_cmdlinerror = 1; + break; + case '?': + fprintf(stderr, "unknown option\n"); + n_cmdlinerror = 1; + break; } } /* get arguments */ -/* for (i = optind; i < inargc; i++) { */ -/* printf("argument %s\n", inargv[i]); */ -/* } */ + /* for (i = optind; i < inargc; i++) { */ + /* printf("argument %s\n", inargv[i]); */ + /* } */ if (!*db) { @@ -2159,24 +1985,8 @@ } - if (!n_cgi) { - if (n_read_file || n_read_stdin) { - if (!n_read_stdin) { - infilefp = fopen(infile, "rb"); - if (infilefp == NULL) { - send_status(slvals.n_sockfd, 112, TERM_NO); - delete_all_lilimem(&sentinel); - close(slvals.n_sockfd); - return 1; - } - } - else { - infilefp = stdin; /* stdin is already open */ - } - - read_result = fgets(slvals.outbuffer+strlen(slvals.outbuffer), COMMAND_INBUF_LEN-strlen(slvals.outbuffer), infilefp); - if (!n_read_stdin) { /* don't close stdin */ - fclose(infilefp); - } - if (read_result == NULL) { + if (n_read_file || n_read_stdin) { + if (!n_read_stdin) { + infilefp = fopen(infile, "rb"); + if (infilefp == NULL) { send_status(slvals.n_sockfd, 112, TERM_NO); delete_all_lilimem(&sentinel); @@ -2186,31 +1996,45 @@ } else { - if (optind < inargc) { - int open_quote = 0; - strcat(slvals.outbuffer, "\""); - /* printf("%d\n", optind); */ - for (i = optind; i < inargc; i++) { - strcat(slvals.outbuffer, inargv[i]); - if (open_quote) { - strcat(slvals.outbuffer, "\'"); - open_quote--; - } - /* the tokenizer returns a quoted item as a separate token - even if there is no space between e.g. a '=' and the - quoted item. To rectify this, check whether the previous - item ended with a '=' or a '~' */ - if (slvals.outbuffer[strlen(slvals.outbuffer)-1] != '=' - && slvals.outbuffer[strlen(slvals.outbuffer)-1] != '~') { - strcat(slvals.outbuffer, " "); - } - else { - /* insert a quote after the '='. This will put back the - quote only for items that actually were quoted */ - strcat(slvals.outbuffer, "\'"); - open_quote++; - } + infilefp = stdin; /* stdin is already open */ + } + + read_result = fgets(slvals.outbuffer+strlen(slvals.outbuffer), COMMAND_INBUF_LEN-strlen(slvals.outbuffer), infilefp); + if (!n_read_stdin) { /* don't close stdin */ + fclose(infilefp); + } + if (read_result == NULL) { + send_status(slvals.n_sockfd, 112, TERM_NO); + delete_all_lilimem(&sentinel); + close(slvals.n_sockfd); + return 1; + } + } + else { + if (optind < inargc) { + int open_quote = 0; + strcat(slvals.outbuffer, "\""); + /* printf("%d\n", optind); */ + for (i = optind; i < inargc; i++) { + strcat(slvals.outbuffer, inargv[i]); + if (open_quote) { + strcat(slvals.outbuffer, "\'"); + open_quote--; + } + /* the tokenizer returns a quoted item as a separate token + even if there is no space between e.g. a '=' and the + quoted item. To rectify this, check whether the previous + item ended with a '=' or a '~' */ + if (slvals.outbuffer[strlen(slvals.outbuffer)-1] != '=' + && slvals.outbuffer[strlen(slvals.outbuffer)-1] != '~') { + strcat(slvals.outbuffer, " "); + } + else { + /* insert a quote after the '='. This will put back the + quote only for items that actually were quoted */ + strcat(slvals.outbuffer, "\'"); + open_quote++; } - strcpy(&slvals.outbuffer[strlen(slvals.outbuffer)-1], "\""); /* remove trailing space */ } + strcpy(&slvals.outbuffer[strlen(slvals.outbuffer)-1], "\""); /* remove trailing space */ } } @@ -2313,4 +2137,11 @@ fprintf(errstream, get_status_msg(cs_status)); fprintf(errstream, "\n"); + if (slvals.n_file_open || slvals.n_file_append) { + close_outfile(pagerfp); + } + else { + closepager(pagerfp); + } + n_broken_pipe = 0; return 1; } @@ -2368,5 +2199,5 @@ byte_written += fwrite(slvals.inbuffer, sizeof(char), numbyte-n_curr_trailing_z, pagerfp); } -/* printf("inbuffer went to:%s<<\nn_read_done is %d<<n_done is %d<<\n", slvals.inbuffer, n_read_done, n_done); */ + /* printf("inbuffer went to:%s<<\nn_read_done is %d<<n_done is %d<<\n", slvals.inbuffer, n_read_done, n_done); */ if (!n_read_done) { n_last_trailing_z = n_curr_trailing_z; @@ -2376,5 +2207,5 @@ /* read summary */ -/* send_status(slvals.n_sockfd, 0, TERM_NO); */ + /* send_status(slvals.n_sockfd, 0, TERM_NO); */ cs_status = read_status(slvals.n_sockfd); @@ -4981,4 +4812,11 @@ fprintf(errstream, get_status_msg(cs_status)); fprintf(errstream, "\n"); + if (slvals.n_file_open || slvals.n_file_append) { + close_outfile(pagerfp); + } + else { + closepager(pagerfp); + } + n_broken_pipe = 0; return 1; } |