home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / MISC / mtp.shar / mtpcmds.c < prev    next >
C/C++ Source or Header  |  2009-11-06  |  10KB  |  403 lines

  1. /* mtpcmds.c, Version 1.4, Created 8/9/91 */
  2. /* Subroutines called by mtp */
  3. /* Dr Alan M. McIvor, BP Sunbury Research Centre */
  4.  
  5. #include <stdio.h>
  6. #include <strings.h>
  7. #include <errno.h>
  8. #include <ctype.h>
  9. #include <malloc.h>
  10. #include <memory.h>
  11. #include "/chuckapps/os9/defs/module.h"
  12. #include "mtp.h"
  13.  
  14.  
  15. int command_index(usr_cmd)
  16.      char *usr_cmd;
  17. {
  18.   if (strcmp(usr_cmd, "quit") == 0)
  19.     return(BYE);
  20.   else if (strcmp(usr_cmd, "bye") == 0)
  21.     return(BYE);
  22.   else if (strcmp(usr_cmd, "put") == 0)
  23.     return(PUTMOD);
  24.   else if (strcmp(usr_cmd, "get") == 0)
  25.     return(GETMOD);
  26.   else if (strcmp(usr_cmd, "cd") == 0)
  27.     return(CHDIR);
  28.   else if (strcmp(usr_cmd, "pwd") == 0)
  29.     return(REPPWD);
  30.   else if (strcmp(usr_cmd, "ls") == 0)
  31.     return(REPLS);
  32.   else if (strcmp(usr_cmd, "uput") == 0)
  33.     return(UPUTMOD);
  34.   else
  35.     return(UNKNOWN);
  36. }
  37.  
  38.  
  39. /* command handlers - return -1 if error, 0 if caller should hangup normally
  40.    or >0 if caller should continue. */
  41.  
  42. int signal_bye(s)
  43.      int s;
  44. {
  45.   char c;
  46.  
  47.   /* send host a quit command */
  48.   c = QUIT;
  49.   if (write(s, &c, 1) == -1)
  50.     return(-1);
  51.   else
  52.     return(0);
  53. }
  54.  
  55.  
  56. int put_module(s, argument, control)
  57.      int s;            /* socket */
  58.      char *argument;
  59.      char control;        /* either PUT_MODULE or UPUT_MODULE */
  60. {
  61.   FILE *fp;
  62.   FILE *file_open();
  63.   struct modhcom os9modhead;
  64.   char *module, *ptr;
  65.   int i, j;
  66.   int handle_error();
  67.  
  68.   /* assume that the argument is one file name only and open this file */
  69.   if ((fp = file_open(argument)) == NULL)
  70.     {
  71.       fprintf(stderr, "put_module: Couldn't open module file %s\n", argument);
  72.       return(1);
  73.     }
  74.  
  75.   /* read module header from file */
  76.   if (fread((char *)&os9modhead, sizeof(struct modhcom), 1, fp) != 1)
  77.     {
  78.       fprintf(stderr, "put_module: Couldn't read module header from file %s\n",
  79.           argument);
  80.       fclose(fp);
  81.       return(1);
  82.     }
  83.  
  84.   /* check that the module sync bytes are correct */
  85.   if (os9modhead._msync != MODSYNC)
  86.     {
  87.       fprintf(stderr,
  88.           "put_module: File %s doesn't contain a valid module header\n",
  89.           argument);
  90.       fclose(fp);
  91.       return(1);
  92.     }
  93.   
  94.   /* read in the module */
  95.   if ((module = malloc(os9modhead._msize)) == NULL)
  96.     {
  97.       fprintf(stderr, "put_module: Error mallocing\n");
  98.       fclose(fp);
  99.       return(1);
  100.     }
  101.   memcpy(module, (char *)&os9modhead, sizeof(struct modhcom));
  102.   if (fread((module + sizeof(struct modhcom)), 1,
  103.         (os9modhead._msize - sizeof(struct modhcom)), fp) !=
  104.       (os9modhead._msize - sizeof(struct modhcom)))
  105.     {
  106.       fprintf(stderr, "put_module: Error reading file %s\n", argument);
  107.       fclose(fp);
  108.       return(1);
  109.     }
  110.   fclose(fp);
  111.  
  112. /*
  113.   printf("\t\t\tmodule name is %s\n", (module + os9modhead._mname));
  114.   printf("\t\t\tmodule size is %d\n", os9modhead._msize);
  115. */
  116.   /* send the server the data */
  117.   if (write(s, &control, 1) != 1) /* put mod code */
  118.     {
  119.       fprintf(stderr, "put_module: Error on socket I/O\n");
  120.       return(-1);
  121.     }
  122.   i = strlen((module + os9modhead._mname));
  123.   i = htonl(i);
  124.   if (write(s, &i, 4) != 4)    /* length of name */
  125.     {
  126.       fprintf(stderr, "put_module: Error on socket I/O\n");
  127.       return(-1);
  128.     }
  129.   i = strlen((module + os9modhead._mname));
  130.   if (write(s, (module + os9modhead._mname), i) != i) /*name */
  131.     {
  132.       fprintf(stderr, "put_module: Error on socket I/O\n");
  133.       return(-1);
  134.     }
  135.   i = htonl(os9modhead._msize);
  136.   if (write(s, &i, 4) != 4)    /* size of module */
  137.     {
  138.       fprintf(stderr, "put_module: Error on socket I/O\n");
  139.       return(-1);
  140.     }
  141.  
  142.   /* wait for acknowledge */
  143.   if (read(s, &control, 1) <= 0)
  144.     {
  145.       fprintf(stderr, "put_module: Error on socket I/O\n");
  146.       return(-1);
  147.     }
  148. /*  printf("Server acknowledged with code %c\n", control);*/
  149.   switch (control)
  150.     {
  151.       case ACKNOWLEDGE:
  152. /*        printf("Server acknowledged\n");*/
  153.     break;
  154.       case ERROR_CONTROL:
  155.     return(handle_error(s));
  156.       default:
  157.     fprintf(stderr, "put_module: Unknown control from server\n");
  158.     return(-1);
  159.     }
  160.  
  161. /*  printf("Writing module ...\n");*/
  162.   
  163.   /* send the module data */
  164.   if (write_block(s, module, os9modhead._msize) == -1)
  165.     {
  166.       fprintf(stderr, "put_module: Error on socket I/O\n");
  167.       return(-1);
  168.     }
  169. /*
  170.     else
  171.       printf("%d ", j);
  172.   printf("\n");
  173.  
  174.   printf("\t\t ... finished\n");
  175. */
  176.   free(module);
  177.  
  178.   /* wait for acknowledge */
  179.   if (read(s, &control, 1) <= 0)
  180.     {
  181.       fprintf(stderr, "put_module: Error on socket I/O\n");
  182.       return(-1);
  183.     }
  184.   switch (control)
  185.     {
  186.       case ACKNOWLEDGE:
  187. /*        printf("Server acknowledged\n");*/
  188.     break;
  189.       case ERROR_CONTROL:
  190.     fclose(fp);
  191.     return(handle_error(s));
  192.       default:
  193.     fprintf(stderr, "put_module: Unknown control from server\n");
  194.     return(-1);
  195.     }
  196.   
  197.   return(1);
  198. }
  199.  
  200.  
  201. int handle_error(socket)
  202.      int socket;
  203. {
  204.   /* return -1 if fatal error, 0 nonfatal but stop, >0 if continue */
  205.   int error_number;
  206.  
  207.   /* get error number from socket */
  208.   if (read(socket, (char *)&error_number, 4) == -1)
  209.     {
  210.       fprintf(stderr, "put_module: Error on socket I/O\n");
  211.       return(-1);
  212.     }
  213.   error_number = ntohl(error_number);
  214.  
  215.   /* process error */
  216.   switch(error_number)
  217.     {
  218.       case E_FATAL:
  219.         fprintf(stderr, "Server indicated FATAL error.\n");
  220.     return(-1);
  221.       case E_UNLOAD:
  222.     fprintf(stderr, "Server indicated UNLOAD error.\n");
  223.     return(E_UNLOAD);
  224.       case E_MEMALLOC:
  225.     fprintf(stderr, "Server indicated ALLOC error.\n");
  226.     return(E_MEMALLOC);
  227.       case E_FORK:
  228.     fprintf(stderr, "Server indicated FORK error.\n");
  229.     return(E_FORK);
  230.       case E_VALIDATE:
  231.     fprintf(stderr, "Server indicated validation error.\n");
  232.     return(E_VALIDATE);
  233.       case E_NOMODULE:
  234.     fprintf(stderr, "Server indicated no such module.\n");
  235.     return(E_NOMODULE);
  236.       default:
  237.     fprintf(stderr, "Server indicated unknown error %d\n", error_number);
  238.     return(-1);
  239.     }
  240. }
  241.  
  242.  
  243. int get_module(s, argument)
  244.      int s;            /* socket */
  245.      char *argument;
  246. {
  247.   FILE *fp;
  248.   char *module;
  249.   char control;
  250.   int i, j;
  251.   int handle_error();
  252.   char module_name[256];
  253.   char file_name[256];
  254.  
  255.   /* get module name and file name */
  256.   /* skip over white space */
  257.   for (i = 0; argument[i] != '\0'; i++)
  258.     if (!isspace(argument[i]))
  259.       break;
  260.   /* get module name */
  261.   for (j = 0; argument[i] != '\0'; i++)
  262.     if (isspace(argument[i]))
  263.       break;
  264.     else
  265.     module_name[j++] = argument[i];
  266.   module_name[j] = '\0';
  267.   /* skip over white space */
  268.   for (; argument[i] != '\0'; i++)
  269.     if (!isspace(argument[i]))
  270.       break;
  271.   /* get file name */
  272.   for (j = 0; argument[i] != '\0'; i++)
  273.     if (isspace(argument[i]))
  274.       break;
  275.     else
  276.     file_name[j++] = argument[i];
  277.   file_name[j] = '\0';
  278.   if (strlen(file_name) == 0)
  279.     strcpy(file_name, module_name);
  280.   
  281. /*  printf("Getting module %s into file %s\n", module_name, file_name);*/
  282.  
  283.   if (strlen(module_name) == 0)
  284.     {
  285.       fprintf(stderr, "? get mod-name [file-name]\n");
  286.       return(2);
  287.     }
  288.  
  289.   /* open the file to write the data into */
  290.   if ((fp = fopen(file_name, "w")) == NULL)
  291.     {
  292.       fprintf(stderr, "get_module: Couldn't open file %s\n", file_name);
  293.       return(3);
  294.     }
  295.   
  296.   /* send server GETMOD command, module name size and module name */
  297.   control = GET_MODULE;
  298.   if (write(s, &control, 1) != 1) /* get mod code */
  299.     {
  300.       fprintf(stderr, "get_module: Error on socket I/O\n");
  301.       return(-1);
  302.     }
  303.   i = strlen(module_name);
  304.   i = htonl(i);
  305.   if (write(s, &i, 4) != 4)    /* length of name */
  306.     {
  307.       fprintf(stderr, "get_module: Error on socket I/O\n");
  308.       return(-1);
  309.     }
  310.   i = strlen(module_name);
  311.   if (write(s, module_name, i) != i) /*name */
  312.     {
  313.       fprintf(stderr, "get_module: Error on socket I/O\n");
  314.       return(-1);
  315.     }
  316.  
  317.   /* wait for acknowledge */
  318.   if (read(s, &control, 1) <= 0)
  319.     {
  320.       fprintf(stderr, "get_module: Error on socket I/O\n");
  321.       return(-1);
  322.     }
  323.   switch (control)
  324.     {
  325.       case ACKNOWLEDGE:
  326. /*        printf("Server acknowledged\n");*/
  327.     break;
  328.       case ERROR_CONTROL:
  329.     fclose(fp);
  330.     return(handle_error(s));
  331.     break;
  332.       default:
  333.     fprintf(stderr, "get_module: Unknown control from server\n");
  334.     return(-1);
  335.     }
  336.  
  337.   if (read(s, &i, 4) != 4)    /* size of module */
  338.     {
  339.       fprintf(stderr, "get_module: Error on socket I/O\n");
  340.       return(-1);
  341.     }
  342.   i = ntohl(i);
  343. /*  printf("Module of size %d\n", i);*/
  344.   
  345.   /* read module */
  346.   module = (char *)malloc(i);
  347.   if (read_block(s, module, i) != i)
  348.     {
  349.       fprintf(stderr, "get_module: Error on socket I/O\n");
  350.       return(-1);
  351.     }
  352.  
  353.   /* write to file */
  354.   if (fwrite(module, 1, i, fp) != i)
  355.     {
  356.       fprintf(stderr, "get_module: Error writing module to file\n");
  357.       return(4);
  358.     }
  359.   free(module);
  360.  
  361.   return(1);
  362. }
  363.  
  364.  
  365. FILE *file_open(fname)
  366.      char *fname;
  367. {
  368.   FILE *fp;
  369.   char *mtppath;        /* paths to search for modules */
  370.   extern char *getenv();
  371.   char pathname[1024];
  372.   int i;
  373.   
  374.   if ((fp = fopen(fname, "r")) != NULL)
  375.     return(fp);
  376.  
  377.   /* so file not in directory, look in path for it */
  378.  
  379.   /* if no path defined return NULL */
  380.   if ((mtppath = getenv("MTPPATH")) == NULL)
  381.     return(NULL);
  382.  
  383. /*  printf("MTPPATH = %s\n");*/
  384.  
  385.   do
  386.     {
  387.       /* copy the next path out of MTPPATH:- paths being separated by : */
  388.       i = 0;
  389.       while ((*mtppath != ':') && (*mtppath != '\0'))
  390.     pathname[i++] = *mtppath++;
  391.       /* complete the full possible path name */
  392.       pathname[i] = '\0';
  393.       strcat(pathname, "/");
  394.       strcat(pathname, fname);
  395.       if ((fp = fopen(pathname, "r")) != NULL)
  396.     return(fp);
  397.       /* so not in that directory, repeat if further paths */
  398.     }
  399.   while (*mtppath++ != '\0');
  400.  
  401.   return(NULL);
  402. }
  403.