[Refdb-cvs] CVS: refdb/src refdbc.c,1.72.2.10,1.72.2.11 refdbc.h,1.6.2.1,1.6.2.2
Status: Beta
Brought to you by:
mhoenicka
From: Markus H. <mho...@us...> - 2005-10-11 20:30:13
|
Update of /cvsroot/refdb/refdb/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6619/src Modified Files: Tag: Release_0_9_5_stable refdbc.c refdbc.h Log Message: added updatejo command Index: refdbc.c =================================================================== RCS file: /cvsroot/refdb/refdb/src/refdbc.c,v retrieving revision 1.72.2.10 retrieving revision 1.72.2.11 diff -u -U2 -r1.72.2.10 -r1.72.2.11 --- refdbc.c 14 Aug 2005 00:22:44 -0000 1.72.2.10 +++ refdbc.c 11 Oct 2005 20:29:35 -0000 1.72.2.11 @@ -99,4 +99,5 @@ { "updateref", com_updateref, "Update references in the database" }, { "verbose", com_verbose, "Toggle verbose mode" }, + { "updatejo", com_updatejo, "Update journal name synonyms" }, { "whichdb", com_whichdb, "Show info about current database" }, { (char *)NULL, (Function *)NULL, (char *)NULL } @@ -5295,4 +5296,313 @@ /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + com_updatejo(): update the journal name synonyms + + int com_updatejo 0 if successful, 1 if error + + char *arg the spec of the note which are to be viewed + + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +int com_updatejo (char* arg) { + char outbuffer[COMMAND_INBUF_LEN]; /* holds the command for the server */ + char* infile; + char inbuffer[COMMAND_INBUF_LEN] = ""; + char db[DBNAME_LENGTH] = ""; + char **inargv; /* tokens of the argument */ + char *newarg; + char *read_result; + char scrambled_passwd[PASSWD_LENGTH*3+1] = ""; + int inargc = 0; /* number of tokens of the argument */ + int inargcmax; /* maximum number of tokens */ + int result; + int i, j; + int n_opt; + int n_done; + int n_open_andnot = 0; + int n_limit_to_user = 0; /* indicates -P switch */ + int n_read_file = 0; /* indicates -f switch */ + int n_cmdlinerror = 0; + int numbyte; + int n_read_done = 0; +/* size_t byte_written = 0; */ + FILE *infilefp; + FILE* errstream; + struct lilimem sentinel; + struct liliform* ptr_current; + struct simplelistvals slvals; + + errstream = (n_cgi) ? stdout : stderr; + + slvals.outbuffer = outbuffer; + + strcpy(slvals.outbuffer, "updatejo "); + slvals.n_file_open = 0; + slvals.n_file_append = 0; + slvals.n_pipe = 0; + slvals.outfile = NULL; + slvals.outpipe = NULL; + + sentinel.ptr_mem = NULL; + sentinel.ptr_next = NULL; + sentinel.varname[0] = '\0'; + + strcpy(db, current_db); /* use default db if set */ + + /* 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 = malloc((size_t)inargcmax*sizeof(char*)); + if (inargv == NULL) { + return 1; + } + + if (insert_lilimem(&sentinel, (void**)&inargv, NULL)) { + return 1; + } + + /* the following is a temporary hack to allow cmdln_tokenize to work */ + newarg = malloc((size_t)(strlen(arg)+8)); + if (newarg == NULL) { + delete_all_lilimem(&sentinel); + return 1; + } + + if (insert_lilimem(&sentinel, (void**)&newarg, NULL)) { + delete_all_lilimem(&sentinel); + return 1; + } + + strcpy(newarg, "addlink "); + strcat(newarg, arg); + + result = cmdln_tokenize(&inargc, &inargv, inargcmax, newarg); + + + if (result == 1 || result == 2) { /* memory error */ + delete_all_lilimem(&sentinel); + 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]); */ + /* } */ + + /* 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:PqR:s:S:t:T:u:U:vVw:")) != -1) { + switch(n_opt) { + case 'c': + /* printf("-c %s\n", optarg); */ + slvals.outpipe = 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)) { + 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 'f': */ +/* 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("Updates the synonyms of a given journal.\nSyntax: updatejo [-c command] [-d database] [-h] [-o outfile] [-O outfile] :JO:|:JF:|:J1:|:J2:=journal :XY:=name [:XY:=name1...]\nThe first argument selects the journal by its short name, full name, user abbreviation1, or user abbreviation2, respectively\nXY is one of JO|JF|J1|J2 and sets the corresponding synonym to the supplied name\nOptions: -c command pipe the output through command\n -d database specify the database to work with\n -h prints this mini-help\n -o outfile save the output in outfile (overwrite)\n -O outfile append the output to outfile\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 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; + /* now all the options that main has already taken care of */ + case 'a': + case 'C': /* fall through */ + case 'e': + case 'E': + case 'f': + case 'F': + case 'g': + case 'G': + case 'i': + case 'k': + case 'l': + case 'L': + case 'p': + case 'P': + case 'q': + case 'R': + case 's': + case 'S': + case 't': + 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]); */ +/* } */ + + if (!*db) { + fprintf(errstream, "Don't know which database to use. Select one with selectdb or use the -d switch with addlink\n"); + delete_all_lilimem(&sentinel); + return 1; + } + + if (n_cmdlinerror) { + delete_all_lilimem(&sentinel); + return 1; + } + + if (connect_to_server(&slvals.n_sockfd, server_ip, port_address) != 0) { + delete_all_lilimem(&sentinel); + return 1; + } + + strcpy(scrambled_passwd, passwd); + + if (init_dialog(slvals.n_sockfd, scrambled_passwd, inbuffer)) { + delete_all_lilimem(&sentinel); + close(slvals.n_sockfd); + return 1; + } + + + 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) { + send_status(slvals.n_sockfd, 112, TERM_NO); + delete_all_lilimem(&sentinel); + close(slvals.n_sockfd); + return 1; + } + } + else { + if (optind < inargc) { + strcat(slvals.outbuffer, "\""); + /* printf("%d\n", optind); */ + for (i = optind; i < inargc; i++) { +/* printf("inargv[%d]: %s<<\n", i, inargv[i]); */ + /* 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 '=' */ + if (slvals.outbuffer[strlen(slvals.outbuffer)-1] != '=') { + strcat(slvals.outbuffer, inargv[i]); +/* strcat(slvals.outbuffer, " "); */ + } + else { + /* quote item */ + strcat(slvals.outbuffer, "\'"); + + /* in order to allow e.g. single quotes in journal names we + must escape the string properly. It will be unescaped on + the server side */ + escape_chars(slvals.outbuffer + strlen(slvals.outbuffer), inargv[i], strlen(inargv[i]), "'\""); + strcat(slvals.outbuffer, "\' "); + } + } + strcpy(&slvals.outbuffer[strlen(slvals.outbuffer)-1], "\""); /* remove trailing space */ + } + } + + /* ToDo: do we need this here? */ + /* make sure there's no newlines in the string; reuse read_result */ + for (read_result = slvals.outbuffer; *read_result; read_result++) { + if (*read_result == '\n' || *read_result == '\r') { + *read_result = ' '; + } + } + + /* assemble command string for refdbd */ + strcat(slvals.outbuffer, " -u "); + strcat(slvals.outbuffer, username); + if (*passwd) { + strcat(slvals.outbuffer, " -w "); + strcat(slvals.outbuffer, scrambled_passwd); + } + strcat(slvals.outbuffer, " -d "); + strcat(slvals.outbuffer, db); + + LOG_PRINT(LOG_DEBUG, slvals.outbuffer); + getsimplelist(&slvals, 1); + + close(slvals.n_sockfd); + + delete_all_lilimem(&sentinel); + + return 0; +} + +/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ postprocess_var(): checks and converts as necessary config variables Index: refdbc.h =================================================================== RCS file: /cvsroot/refdb/refdb/src/refdbc.h,v retrieving revision 1.6.2.1 retrieving revision 1.6.2.2 diff -u -U2 -r1.6.2.1 -r1.6.2.2 --- refdbc.h 15 Apr 2005 22:45:09 -0000 1.6.2.1 +++ refdbc.h 11 Oct 2005 20:29:38 -0000 1.6.2.2 @@ -43,4 +43,5 @@ int com_selectdb(char *arg); int com_whichdb(char *arg); +int com_updatejo (char* arg); /* other functions */ |