mast-server.c

来自「EM算法的改进」· C语言 代码 · 共 679 行 · 第 1/2 页

C
679
字号
/* * $Id: mast-server.c 1339 2006-09-21 19:46:28Z tbailey $ *  * $Log$ * Revision 1.6  2005/10/28 22:44:19  nadya * fix typo in "rm" * * Revision 1.5  2005/09/15 21:27:12  nadya * fix db path for uploaded files * * Revision 1.4  2005/09/13 21:11:36  nadya * rm exit line from mast/meme submit scripts * * Revision 1.3  2005/08/30 22:46:21  nadya * update calls to mailer * * Revision 1.2  2005/08/24 04:37:07  nadya * change script layout. Don't assume a given directory, * just use full path where needed for files and binaries. * * Revision 1.1.1.1  2005/07/29 17:16:51  nadya * Importing from meme-3.0.14, and adding configure/make * *//*	mast-server	Usage: mast-server <socket> <command>		<socket>  socket number to listen on		<command> command to execute; quote if spaces        If the file sent to the socket contains just the word "ping",        the program exits with success. 	6/02/96; created; tbaileyFormat for input file read from the socket:----------------------ADDRESS <return address> PROGRAM mastDESCRIPTION <1 line of text>MOTIFS <original name of motif file>DB_TYPE local | uploadedDATABASE <name of database file to search (../mast_databases/ will be prepended)>ALPHABET <alphabet used in database>SWITCHES <other switches>LOGINFO <information to log>BEGIN_MOTIFS<logodds matrices>[%BEGIN_DB<uploaded FASTA sequences>%]Error messages:---------------  10 : Error opening file  11 : Error reading from socket  12 : Error writing to file  13 : Error sending ack to client  14 : Error binding socket  15 : Error listening to socket  16 : Error creating socket  20 : Header field not found  21 : Invalid field name  22 : Illegal number of motifs*//*********************************************************************** * Include files ***********************************************************************/#include "meme-cs.h"#include <signal.h>#include <sys/wait.h>#include <sys/errno.h>/*********************************************************************** * Constants ***********************************************************************/#define MAXHEADERLINE 256#define NPROCS 1/*Error messages:---------------  10 : Error opening file  11 : Error reading from socket  12 : Error writing to file  13 : Error sending ack to client  14 : Error binding socket  15 : Error listening to socket  16 : Error creating socket  20 : Header field not found  21 : Invalid field name  22 : Illegal number of motifs*//*********************************************************************** * Include files ***********************************************************************/#include "meme-cs.h"#include <signal.h>#include <sys/wait.h>#include <sys/errno.h>/*********************************************************************** * Constants ***********************************************************************/#define MAXHEADERLINE 256#define NPROCS 1/* returned by system() */#ifdef sunsparc  int SYSTEM_OK = -1;#else  int SYSTEM_OK = 0;#endif/*********************************************************************** * Type definitions ***********************************************************************/typedef struct HEADER {  char address[MAXHEADERLINE];  char program[MAXHEADERLINE];  char description[MAXHEADERLINE];  char motif_file[MAXHEADERLINE];  char db_type[MAXHEADERLINE];  char database[MAXHEADERLINE];  char alphabet[MAXHEADERLINE];  char switches[MAXHEADERLINE];  char loginfo[MAXHEADERLINE];} HEADER;/*********************************************************************** * Globals  ***********************************************************************/int SOCKET_NUMBER;		/* socket to listen on */char *MAST;			/* name of mast executable *//*********************************************************************** * * void strlower * * Downcase a given string. * ***********************************************************************/void strlower(char *a_string){  int index;  for (index = 0; a_string[index] != '\0'; index++)    a_string[index] = tolower((int) a_string[index]);}/*********************************************************************** * * void read_header_item * * Read one header line from a socket into a HEADER data structure. * ***********************************************************************/void read_header_item(HEADER *a_header, int sock, char *fieldname){  int i;  char buffer[MAXHEADERLINE];  char *info;    /* Read the line into a local buffer. */  if (readline(sock, buffer, MAXHEADERLINE) == 0) {    fprintf(stderr, "Error reading from socket.\n");    fflush(stderr);    exit(11);  }  /* Check to see if this is a ping */  if (strcmp(buffer, "ping") == 0) {    exit(0);                            /* ping; exit with OK status */  }   /* Search for the field name in the line. */  if ((info = (char *)strstr(buffer, fieldname)) == NULL) {    fprintf(stderr, "Field %s not found in %s.\n", fieldname, buffer);    fflush(stderr);    exit(20);  }  /* Move past the end of the field name. */  info += strlen(fieldname);  if (*info != '\0') info++;  /* Get the first character of the field name. */  switch (fieldname[0]) {    case 'A' :       switch (fieldname[1]) {	case 'D' :  /* ADDRESS */	  strcpy(a_header->address, info);	  break;	case 'L' :  /* ALPHABET */	  strcpy(a_header->alphabet, info);	  break;      }      break;    case 'M' :  /* MOTIFS */      strcpy(a_header->motif_file, info);      break;    case 'P' :  /* PROGRAM */      strcpy(a_header->program, info);      break;    case 'D' :       switch (fieldname[1]) {	case 'E' :  /* DESCRIPTION */	  strcpy(a_header->description, info);	  break;	case 'A' :  /* DATABASE */	  strcpy(a_header->database, info);	  break;	case 'B' :  /* DB_TYPE */	  strcpy(a_header->db_type, info);	  break;      }      break;    case 'S' :  /* SWITCHES */      /* copy removing any nasty semi-colons to prevent security breach */      for (i=0; info[i]; i++) {         if (info[i] != ';') {          a_header->switches[i] = info[i];        } else {           a_header->switches[i] = ' ';        }      }      a_header->switches[i] = '\0';      break;    case 'L' :  /* LOGINFO */      strcpy(a_header->loginfo, info);      break;    case 'B' :  /* BEGIN_LO or BEGIN_MOTIFS */      /* Don't do anything except skip header line */      break;    default:       fprintf(stderr, "Invalid fieldname: %s\n", fieldname);      fflush(stderr);      exit(21);  }} /* read_header_item *//*********************************************************************** * * write_data_file * * Read in some lines from a socket and write them to a file. * ***********************************************************************/void write_data_file(int sock, char *filename, char terminator){  int filedescriptor, nread;  char achar;  /* Create the data file and open it in write-only mode. */  if ((filedescriptor = open(filename, 			     O_WRONLY | O_CREAT | O_TRUNC, 400)) == -1) {    fprintf(stderr, "Error creating file %s\n", filename);    fflush(stderr);    exit(10);  }        /* Read from the socket until line containing only terminator reached. */  for (nread = readn(sock, &achar, 1); nread == 1 && achar != terminator;       nread = readn(sock, &achar, 1)) {        /*printf("%c", achar);*/    /* Echo incoming data to the file. */    if (write(filedescriptor, &achar, 1) != 1) {      fprintf(stderr, "Error writing to file %s.\n", filename);      fflush(stderr);      exit(12);    }  }  /* Throw away newline after terminator. */  if (achar == terminator) {    nread = readn(sock, &achar, 1);  }       /* Close the file. */  close(filedescriptor);}/*********************************************************************** * * receive_file * * Read in a file from the socket. * ***********************************************************************/void receive_file(  int sock,   char *motif_filename,   char *db_filename,  HEADER *a_header){  /* Read in the header. */  read_header_item(a_header, sock, "ADDRESS");  read_header_item(a_header, sock, "PROGRAM");  read_header_item(a_header, sock, "DESCRIPTION");  read_header_item(a_header, sock, "MOTIFS");  read_header_item(a_header, sock, "DB_TYPE");  read_header_item(a_header, sock, "DATABASE");  read_header_item(a_header, sock, "ALPHABET");  read_header_item(a_header, sock, "SWITCHES");  read_header_item(a_header, sock, "LOGINFO");  read_header_item(a_header, sock, "BEGIN_MOTIFS");  /* Echo the motifs to a file. */  write_data_file(sock, motif_filename, '%');  if (strcmp(a_header->db_type, "uploaded") == 0) {    /* Echo the uploaded sequence data to a file. */    read_header_item(a_header, sock, "BEGIN_DB");    write_data_file(sock, db_filename, '%');  }}/*********************************************************************** * * send_ack * * Send a single-byte ack to the client. * ***********************************************************************/void send_ack(int sock){  char achar = '1';  if (writen(sock, &achar, 1) != 1) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?