home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2MISC / CSDPMI3S.ZIP / SRC / CWSDPMI / CWSPARM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  6.8 KB  |  316 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #ifdef GO32
  6. #include <unistd.h>
  7. #else
  8. #include <io.h>
  9. #endif
  10.  
  11. #include "gotypes.h"
  12. #include "control.h"
  13.  
  14. char view_only = 0;
  15. int size_of_stubinfo = 0;
  16. char *client_stub_info;
  17. char buffer[30000];
  18. int f;
  19.  
  20. void find_info(char *filename)
  21. {
  22.   int i,size;
  23.   
  24.   f = open(filename, O_RDWR | O_BINARY);
  25.   if (f < 0) {
  26.     f = open(filename, O_RDONLY | O_BINARY);
  27.     if (f < 0) {
  28.       perror(filename);
  29.       exit(1);
  30.     }
  31.     if(!view_only) {
  32.       view_only = 1; 
  33.       printf("%s is read only, you can only view:\n",filename);
  34.     }
  35.   }
  36.  
  37.   size = read(f, buffer, sizeof(buffer));
  38.   if(size == sizeof(buffer)) {
  39.     printf("Buffer not large enough to hold %s (%d bytes).\n",filename,size);
  40.     exit(1);
  41.   }
  42.  
  43.   client_stub_info = NULL;
  44.   for(i=0; i<size && !client_stub_info; i++)
  45.     if(buffer[i] == 'C' && !strcmp(buffer+i+1,"WSPBLK")) {
  46.       client_stub_info = (buffer+i);
  47.       size_of_stubinfo = sizeof(CWSDPMI_pblk);
  48.     }
  49.  
  50.   if(!client_stub_info) {
  51.     printf("Parameter block magic not found in %s!\n",filename);
  52.     exit(2);
  53.   }
  54.  
  55.   lseek(f, i-1, SEEK_SET);    /* Ready to update */
  56.   return;
  57. }
  58.  
  59. void store_info(void)
  60. {
  61.   write(f, client_stub_info, size_of_stubinfo);
  62.   *(short *)(buffer+0x0c) += *(short *)(buffer+0x0a);
  63.   lseek(f, 0x0c, SEEK_SET);    /* Ready to update */
  64.   write(f, buffer+0x0c, 2);
  65. }
  66.  
  67. char *pose_question(char *question, char *default_answer)
  68. {
  69.   static char response[200];
  70.   printf("%s ? [%s] ", question, default_answer);
  71.   fflush(stdout);
  72.   gets(response);
  73.   if (response[0] == '\0')
  74.     return 0;
  75.   return response;
  76. }
  77.  
  78. typedef void (*PerFunc)(void *address_of_field, char *buffer);
  79.  
  80. void str_v2s(void *addr, char *buf, int len)
  81. {
  82.   if (*(char *)addr == 0)
  83.     strcpy(buf, "\"\"");
  84.   else
  85.   {
  86.     buf[len] = 0;
  87.     strncpy(buf, (char *)addr, len);
  88.   }
  89. }
  90.  
  91. void str_s2v(void *addr, char *buf, int len)
  92. {
  93.   if (strcmp(buf, "\"\"") == 0)
  94.     *(char *)addr = 0;
  95.   else
  96.   {
  97.     ((char *)addr)[len-1] = 0;
  98.     strncpy((char *)addr, buf, len);
  99.   }
  100. }
  101.  
  102. void str_v2s48(void *addr, char *buf)
  103. {
  104.   str_v2s(addr, buf, 48);
  105.   if (strcmp(buf, "\"\"") == 0)
  106.     strcpy(buf,"*Disabled*");
  107. }
  108.  
  109. void str_s2v48(void *addr, char *buf)
  110. {
  111.   str_s2v(addr, buf, 48);
  112. }
  113.  
  114. void num_v2s(void *addr, char *buf)
  115. {
  116.   unsigned v = *(unsigned short *)addr;
  117.   sprintf(buf, "%d", v);
  118. }
  119.  
  120. void num_s2v(void *addr, char *buf)
  121. {
  122.   unsigned r = 0;
  123.   sscanf(buf, "%i", &r);
  124.   *(unsigned short *)addr = r;
  125. }
  126.  
  127. void num_vpt2s(void *addr, char *buf)
  128. {
  129.   unsigned v = *(unsigned short *)addr;
  130.   if(v%256)
  131.     sprintf(buf, "%dKb", v*4);
  132.   else
  133.     sprintf(buf, "%dMb", v/256);
  134. }
  135.  
  136. void num_s2vpt(void *addr, char *buf)
  137. {
  138.   unsigned long r = 0;
  139.   char s = 0;
  140.   sscanf(buf, "%li%c", &r, &s);
  141.   switch (s)
  142.   {
  143.     case 'k':
  144.     case 'K':
  145.       r *= 1024L;
  146.       break;
  147.     case 'm':
  148.     case 'M':
  149.       r *= 1048576L;
  150.       break;
  151.   }
  152.   *(unsigned short *)addr = (r+4095L)/4096L;
  153. }
  154.  
  155. #define Ofs(n) ((int)&(((CWSDPMI_pblk *)0)->n))
  156.  
  157. struct {
  158.   char *short_name;
  159.   char *long_name;
  160.   int offset_of_field;
  161.   PerFunc val2string;
  162.   PerFunc string2val;
  163. } per_field[] = {
  164.   {
  165.     "swapfile",
  166.     "Full name of paging file (\"\" to disable)",
  167.     Ofs(swapname),
  168.     str_v2s48, str_s2v48
  169.   },
  170.   {
  171.     "pagetable",
  172.     "Number of page tables to initially allocate (0=auto)",
  173.     Ofs(pagedir),
  174.     num_v2s, num_s2v
  175.   },
  176.   {
  177.     "minappmem",
  178.     "Minimum application memory desired before 640K paging",
  179.     Ofs(minapp),
  180.     num_vpt2s, num_s2vpt
  181.   },
  182.   {
  183.     "savepara",
  184.     "Paragraphs of DOS memory to reserve when 640K paging",
  185.     Ofs(savepar),
  186.     num_v2s, num_s2v
  187.   },
  188.   {
  189.     "heap",
  190.     "Paragraphs of memory for extra CWSDPMI internal heap",
  191.     -2,
  192.     num_v2s, num_s2v
  193.   }
  194. /*  {
  195.     "flags",
  196.     "Value of run flags",
  197.     Ofs(flags),
  198.     num_v2s, num_s2v
  199.   },*/
  200. };
  201.  
  202. #define NUM_FIELDS (sizeof(per_field) / sizeof(per_field[0]))
  203.  
  204. #define HFORMAT "%-18s %s\n"
  205.  
  206. void give_help(void)
  207. {
  208.   int i;
  209.   fprintf(stderr, "Usage: cwsparam [-v] [-h] [cwsdpmi.exe] [field=value . . . ]\n");
  210.   fprintf(stderr, "-h = give help   -v = view info  field=value means set w/o prompt\n");
  211.   fprintf(stderr, HFORMAT, "-field-", "-description-");
  212.  
  213.   for (i=0; i < NUM_FIELDS; i++)
  214.     fprintf(stderr, HFORMAT, per_field[i].short_name, per_field[i].long_name);
  215.   exit(1);
  216. }
  217.  
  218. int main(int argc, char **argv)
  219. {
  220.   int i;
  221.   char need_to_save;
  222.  
  223.   if (argc > 1 && strcmp(argv[1], "-v") == 0)
  224.   {
  225.     view_only = 1;
  226.     argc--;
  227.     argv++;
  228.   }
  229.  
  230.   if (argc > 1 && strcmp(argv[1], "-h") == 0)
  231.     give_help();
  232.  
  233.   if (argc < 2 || strchr(argv[1],'=') != NULL)
  234.     find_info("cwsdpmi.exe");
  235.   else {
  236.     find_info(argv[1]);
  237.     argc--;
  238.     argv++;
  239.   }
  240.   /* This hack is for the heap which isn't in the stubinfo struct */
  241.   per_field[4].offset_of_field = (int)(buffer+0x0c - client_stub_info);
  242.   *(short *)(buffer+0x0c) -= *(short *)(buffer+0x0a);
  243.  
  244.   if (view_only)
  245.   {
  246.     char buf[100];
  247.     fprintf(stderr, HFORMAT, "-value-", "-field description-");
  248.     for (i=0; i<NUM_FIELDS; i++)
  249.     {
  250.       if (per_field[i].offset_of_field < size_of_stubinfo)
  251.       {
  252.         per_field[i].val2string(client_stub_info + per_field[i].offset_of_field, buf);
  253.         fprintf(stderr, HFORMAT, buf, per_field[i].long_name);
  254.       }
  255.     }
  256.     exit(0);
  257.   }
  258.  
  259.   if (argc > 1)
  260.   {
  261.     int field;
  262.     char got, got_any = 0;
  263.     char fname[100], fval[100];
  264.     for (i=1; i < argc; i++)
  265.     {
  266.       fname[0] = 0;
  267.       fval[0] = 0;
  268.       sscanf(argv[i], "%[^=]=%s", fname, fval);
  269.       got = 0;
  270.       for (field=0; field<NUM_FIELDS; field++)
  271.       {
  272.         if (strcmp(per_field[field].short_name, fname) == 0)
  273.         {
  274.           got = 1;
  275.           got_any = 1;
  276.           if (per_field[field].offset_of_field < size_of_stubinfo)
  277.           {
  278.             per_field[field].string2val(client_stub_info + per_field[field].offset_of_field, fval);
  279.           }
  280.           else
  281.             fprintf(stderr, "Warning: This image does not support field %s\n", fname);
  282.         }
  283.       }
  284.       if (!got)
  285.       {
  286.         fprintf(stderr, "Error: %s is not a valid field name.\n", fname);
  287.         give_help();
  288.       }
  289.     }
  290.     if (got_any)
  291.       store_info();
  292.     close(f);
  293.     return 0;
  294.   }
  295.  
  296.   need_to_save = 0;
  297.   for (i=0; i<NUM_FIELDS; i++)
  298.   {
  299.     char buf[100], *resp;
  300.     if (per_field[i].offset_of_field < size_of_stubinfo)
  301.     {
  302.       per_field[i].val2string(client_stub_info + per_field[i].offset_of_field, buf);
  303.       if ((resp = pose_question(per_field[i].long_name, buf)) != 0)
  304.       {
  305.         per_field[i].string2val(client_stub_info + per_field[i].offset_of_field, resp);
  306.         need_to_save = 1;
  307.       }
  308.     }
  309.   }
  310.   if (need_to_save)
  311.     store_info();
  312.   close(f);
  313.  
  314.   return 0;
  315. }
  316.