File: gnbkclient.c

package info (click to toggle)
acedb 4.9.39+dfsg.02-5
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 33,796 kB
  • sloc: ansic: 256,989; perl: 2,803; cpp: 2,534; csh: 1,712; python: 862; sh: 658; makefile: 300; awk: 249; lex: 225; yacc: 221
file content (151 lines) | stat: -rw-r--r-- 4,332 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*  File: gnbkclient.c
 *  Author: Lincoln Stein (Whitehead) and Jean Thierry-Mieg
 *  Copyright (C) L Stein and J Thierry-Mieg, 1996
 * -------------------------------------------------------------------
 * Acedb is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
 * -------------------------------------------------------------------
 * This file is part of the ACEDB genome database package, written by
 * 	Richard Durbin (MRC LMB, UK) rd@mrc-lmb.cam.ac.uk, and
 *	Jean Thierry-Mieg (CRBM du CNRS, France) mieg@kaa.cnrs-mop.fr
 *
 * Description:
 * Modified from netclient to handle communication
 * with a gnbk server
 * 
 * Exported functions:
 * HISTORY:
 * Last edited: Feb 25 21:36 1996 (mieg)
 * Created: Wed Nov 25 20:02:45 1992 (mieg)
 *-------------------------------------------------------------------
 */

 /* $Id: gnbkclient.c,v 1.5 2002/05/13 12:59:40 srk Exp $ */

#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#if !defined(DARWIN)
#include <malloc.h>
#endif
#include "regular.h"
#include "array.h"

#define OK 200
#define GOODBYE 201
#define DEBUG 301
#define COMMENT 302
#define REDIRECT 303
#define PARAMERROR 401
#define SYNTAXERROR 402
#define UNIMPLEMENTED 403
#define TIMEOUTERROR 501
#define COMMERROR 502
#define MEMERROR 503
#define FILEERROR 504

extern BOOL openServer(char *host, u_long rpc_port) ;
extern int askServer(char *request, char **answerPtr) ;

void writeStatus (int status,char* message) {
  fprintf(stdout,"%d %s\r\n",status,message);
  fflush(stdout);
}

/*************************************************************************/

void doQuery(char* query) 
{
  char* answer = 0 ;
  int retval = askServer(query,&answer) ;

  if (retval > 0) 
    printf ("Server error code %d",retval);
  else
    printf ("%s",answer) ;
  messfree(answer);
}

/*************************************************************************/
void printHelp () {
  char* helpText[] = {
    "- Commands:",
    "-    TYpe a genbank accession name",
    "End of HELP info",
    NULL
  };
  char** m = helpText;
  while (*m != NULL)
    writeStatus(COMMENT,*m++);
}

/*************************************************************************/
void main( int argc, char *argv[] )
{ int level = 0 ;
  char *host = "localhost";
  unsigned long port = 0x20000300 ;
  char *command ;

  /* Read command line parameters */
  if (argc < 2)
    {
      fprintf(stderr,"Usage: gnbkclient [-host host] [-port port_num]\n");
      exit (-1);
    }
 
  while (argc > 1) {
    argv++; argc--;
    if ( (argc > 1) && !strcmp("-host",*argv) ) {
      argv++; argc--;
      host = *argv;
    } 
    else if ( (argc > 1) && !strcmp("-port",*argv) ) {
      argv++; argc--;
      port = atoi(*argv);
    }
    else {
      fprintf(stderr,"Usage: gnbkclient [-host host] [-port port_num]\n");
      exit (-1);
    }
  }
  fprintf(stderr,"command line ok\n") ;
  if (!openServer(host, port))
    {
      writeStatus(COMMERROR,"cannot establish connection");
      exit (-1);
    }
  fprintf(stderr, "server ready\n") ;
  level = freesetfile (stdin, "") ;
  freespecial("\n") ;
  while (freecard(level))
    { if ((command = freeword()))
      { printf("Call server with : %s \n", command) ;
	if (!strcasecmp (command,"HELP"))
	  printHelp();
	else if (!strcasecmp (command,"QUIT"))
	  break ;
	else
	  doQuery(command);
	fflush (stdout) ;
      }
    }
  fprintf(stderr,"A bientot");
  fflush (stdout) ;
}

/*************************************************************************/
/*************************************************************************/