IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 19, 2006, 10:26:59 AM (20 years ago)
Author:
eugene
Message:

unified the client/standalone command functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/multicommand.c

    r4689 r7938  
    11# include "opihi.h"
     2
     3static int server = 0;
     4
     5void multicommand_InitServer () {
     6
     7  char hostname[256], PASSWORD[256];
     8
     9  if (server != 0) {
     10    /* check if down? */
     11    fprintf (stderr, "error: server fd already defined\n");
     12    exit (1);
     13  }
     14
     15  /* find the defined server hostname */
     16  if (VarConfig ("PANTASKS_SERVER", "%s", hostname) == NULL) {
     17    gprint (GP_ERR, "pantasks server host undefined\n");
     18    exit (2);
     19  }
     20
     21  /* attempt to connect to the server */
     22  server = GetClientSocket (hostname);
     23
     24  /* here we can perform the security handshaking */
     25  VarConfig ("PASSWORD", "%s", PASSWORD);
     26  SendCommand (server, strlen(PASSWORD), PASSWORD);
     27 
     28  return;
     29}
    230
    331/* take input line and split into multiple lines
     
    634int multicommand (char *line) {
    735 
    8   int done, status;
     36  int done, status, verbose;
    937  char *p, *q, *tmpline, *outline;
     38  IOBuffer message;
     39
     40  /* if no server is defined, use 'verbose' mode for 'command' */
     41  verbose = (server == 0);
    1042
    1143  p = line;
     
    2153    stripwhite (tmpline);
    2254    if (*tmpline) {
    23       status = command (tmpline, &outline);
     55
     56      status = command (tmpline, &outline, verbose);
     57
     58      if (status == -1) {
     59        if (server) {
     60          // send the command to the server instead
     61          if (!SendMessage (server, outline)) {
     62            switch (errno) {
     63              case EPIPE:
     64                gprint (GP_ERR, "server connection has died\n");
     65                exit (1);
     66              default:
     67                gprint (GP_ERR, "I/O error sending server message\n");
     68                exit (2);
     69            }
     70          }
     71
     72          // receive the resulting stderr
     73          if (ExpectMessage (server, 2.0, &message)) {
     74            switch (errno) {
     75              case EPIPE:
     76                gprint (GP_ERR, "server connection has died\n");
     77                exit (1);
     78              default:
     79                gprint (GP_ERR, "I/O error sending server message\n");
     80                exit (2);
     81            }
     82          } else {
     83            gwrite (message.buffer, 1, message.Nbuffer, GP_ERR);
     84          }
     85
     86          // receive the resulting stdout
     87          if (ExpectMessage (server, 2.0, &message)) {
     88            switch (errno) {
     89              case EPIPE:
     90                gprint (GP_ERR, "server connection has died\n");
     91                exit (1);
     92              default:
     93                gprint (GP_ERR, "I/O error sending server message\n");
     94                exit (2);
     95            }
     96          } else {
     97            gwrite (message.buffer, 1, message.Nbuffer, GP_LOG);
     98          }
     99        } else {
     100          // if no server is defined, we treat an unknown command as an error
     101          status = FALSE;
     102        }       
     103      }
     104
    24105      if (outline != NULL) free (outline);
    25106      if (!status && auto_break) done = TRUE;
     
    31112
    32113/* should skip ; surrounded by "" */
     114
     115/* commands which are not found are sent to the server socket if
     116   defined. commands executed by the server return their stderr
     117   and stdout streams.  server commands should probably not block
     118   the server for very long or multiple clients will interfere
     119   with each other. 
     120*/
Note: See TracChangeset for help on using the changeset viewer.