home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / stg_v4.lzh / gdesc.c < prev    next >
C/C++ Source or Header  |  1994-11-11  |  7KB  |  366 lines

  1. #include <stdio.h>
  2. #include <module.h>
  3. #include ".../device.h"
  4.  
  5. #define ERR (-1)
  6.  
  7. #define T_DEC 1
  8. #define T_HEX 2
  9. #define T_STR 3
  10. #define T_YN 4
  11. #define T_MSG 0xFF
  12.  
  13. extern int errno;
  14.  
  15. char acName[BUFSIZ];
  16. char acBuf[BUFSIZ];
  17.  
  18. mod_dev *psDesc;
  19. mod_driver *psDriver;
  20. char *pcTemp;
  21. unsigned long lTemp;
  22. int iUseDef=0;
  23. int hFile;
  24.  
  25. mod_dev *modlink();
  26. mod_dev *modload();
  27. mod_dev *make_module();
  28.  
  29.  
  30. typedef unsigned char B;
  31. typedef unsigned short W;
  32. typedef unsigned long L;
  33.  
  34.  
  35. struct sConf
  36. {
  37.     B ct_type;
  38.     B ct_size;
  39.     W ct_offset;
  40.     L ct_default;
  41.     char ct_desc[24];
  42. } *psConf;
  43.  
  44. isdesc(s)
  45. char *s;
  46. {
  47.     while (*s && *s!='.') s++;
  48.     if (!*s) return(0);
  49.     s++;
  50.     while (*s && *s>='0' && *s<='9') s++;
  51.     if (*s) return(0);
  52.     return(1);
  53. }
  54.  
  55. long
  56. udec(s)
  57. char *s;
  58. {
  59.     long v=0;
  60.  
  61.     while (*s)
  62.     {
  63.         v*=10;
  64.         if (*s>='0' && *s<='9')
  65.             v+=*s-'0';
  66.         s++;
  67.     }
  68.     return(v);
  69. }
  70.  
  71. long
  72. uhex(s)
  73. char *s;
  74. {
  75.     long v=0;
  76.  
  77.     while (*s)
  78.     {
  79.         v<<=4;
  80.         if (*s>='0' && *s<='9')
  81.             v+=*s-'0';
  82.  
  83.         if (toupper(*s)>='A' && toupper(*s)<='F')
  84.             v+=toupper(*s)-'A'+10;
  85.         s++;
  86.     }
  87.     return(v);
  88. }
  89.  
  90. input(psConf)
  91. struct sConf *psConf;
  92. {
  93.     char *pField;
  94.     L value;
  95.  
  96.     if (!psConf->ct_type)
  97.         return(0);
  98.  
  99.     if (psConf->ct_type==T_MSG)
  100.     {
  101.         printf("%s\n",&psConf->ct_size);
  102.         return(0);
  103.     }
  104.  
  105.     if (psConf->ct_offset==0xFFFF)
  106.         pField=(char*)psDesc+0x30;
  107.     else if (psConf->ct_offset>0xFF00)
  108.         pField=(char*)psDesc+0x30+(~psConf->ct_offset&0x00FF);
  109.     else
  110.         pField=(char*)psDesc+psDesc->_mdevcon+psConf->ct_offset;
  111.  
  112.     if (!iUseDef) goto try_again;
  113.  
  114.     switch (psConf->ct_type&0x7F)
  115.     {
  116.     case T_STR: /* no string defaults */
  117.         break;
  118.     case T_DEC:
  119.     case T_HEX:
  120.     case T_YN:
  121.         switch (psConf->ct_size)
  122.         {
  123.         case 1:
  124.             *(B*)pField=psConf->ct_default&0x000000FF;
  125.             break;
  126.         case 2:
  127.             *(W*)pField=psConf->ct_default&0x0000FFFF;
  128.             break;
  129.         case 4:
  130.             *(L*)pField=psConf->ct_default;
  131.             break;
  132.         }
  133.     }
  134.  
  135. try_again:
  136.     switch (psConf->ct_type&0x7F)
  137.     {
  138.     case T_DEC:
  139.         switch (psConf->ct_size)
  140.         {
  141.         case 1:
  142.             sprintf(acBuf,"%8u",*(B*)pField);
  143.             break;
  144.         case 2:
  145.             sprintf(acBuf,"%8u",*(W*)pField);
  146.             break;
  147.         case 4:
  148.             sprintf(acBuf,"%8lu",*(L*)pField);
  149.             break;
  150.         }
  151.         break;
  152.     case T_HEX:
  153.         switch (psConf->ct_size)
  154.         {
  155.         case 1:
  156.             sprintf(acBuf,"      %2X",*(B*)pField);
  157.             break;
  158.         case 2:
  159.             sprintf(acBuf,"    %04X",*(W*)pField);
  160.             break;
  161.         case 4:
  162.             sprintf(acBuf,"%08lX",*(L*)pField);
  163.             break;
  164.         }
  165.         break;
  166.     case T_STR:
  167.         strncpy(acBuf,(char*)pField,psConf->ct_size);
  168.         *(acBuf+psConf->ct_size)=0;
  169.         while (strlen(acBuf)<8) strcat(acBuf," ");
  170.         break;
  171.     case T_YN:
  172.         if (*(B*)pField)
  173.             strcpy(acBuf,"Yes     ");
  174.         else
  175.             strcpy(acBuf,"No      ");
  176.         break;
  177.     default:
  178.         strcpy(acBuf,"ERROR");
  179.     }
  180.  
  181.     if (psConf->ct_type&0x80)
  182.     {
  183.         printf("%-24s            : %s\n",psConf->ct_desc,acBuf);
  184.         *acBuf=0;
  185.     }
  186.     else
  187.     {
  188.         printf("%-24s [%s] : ",psConf->ct_desc,acBuf);
  189.         if ((psConf->ct_type&0x7F)==2) printf("$");
  190.         gets(acBuf);
  191.     }
  192.  
  193.     if (*acBuf)
  194.     {
  195.         if ((psConf->ct_type&0x7F)==4)
  196.         {
  197.             *(B*)pField=*acBuf&1;
  198.         }
  199.         else if ((psConf->ct_type&0x7F)==3)
  200.         {
  201.             if (strlen(acBuf)>psConf->ct_size) goto try_again;
  202.             if (!strcmp(acBuf," ")) *acBuf=0;
  203.             strncpy((char*)pField,acBuf,psConf->ct_size);
  204.         }
  205.         else
  206.         {
  207.             switch (psConf->ct_type&0x7f)
  208.             {
  209.             case 1:
  210.                 value=udec(acBuf);
  211.                 break;
  212.             case 2:
  213.                 value=uhex(acBuf);
  214.                 break;
  215.             }
  216.             switch (psConf->ct_size)
  217.             {
  218.             case 1:
  219.                 if (value>0x000000FF) goto try_again;
  220.                 *(B*)pField=value;
  221.                 break;
  222.             case 2:
  223.                 if (value>0x0000FFFF) goto try_again;
  224.                 *(W*)pField=value;
  225.                 break;
  226.             case 4:
  227.                 *(L*)pField=value;
  228.                 break;
  229.             }
  230.         }
  231.     }
  232. }
  233.  
  234. main(argc,argv)
  235. char **argv;
  236. {
  237.     if (*++argv)
  238.     {
  239.         if (isdesc(*argv))
  240.         {
  241.             psDesc=(mod_dev*)modlink(*argv,0);
  242.             if ((int)psDesc==ERR)
  243.             {
  244.                 psDesc=(mod_dev*)modload(*argv,0);
  245.                 if ((int)psDesc==ERR)
  246.                     psDesc=0;
  247.             }
  248.         }
  249.         else
  250.         {
  251.             psDriver=(mod_driver*)modlink(*argv,0);
  252.             if ((int)psDriver==ERR)
  253.             {
  254.                 psDriver=(mod_driver*)modload(*argv,0);
  255.                 if ((int)psDriver==ERR)                
  256.                 {
  257.                     if (psDesc) munlink(psDesc);
  258.                     exit(errno);
  259.                 }
  260.             }
  261.         }
  262.     }
  263.  
  264.     if (psDesc)
  265.     {
  266.         pcTemp=(char *)psDesc+psDesc->_mpdev;
  267.         printf("Loading driver module: %s\n",pcTemp);
  268.         psDriver=(mod_driver*)modlink(pcTemp,0);
  269.         if ((int)psDriver==ERR)
  270.         {
  271.             psDriver=(mod_driver*)modload(pcTemp,0);
  272.             if ((int)psDriver==ERR)
  273.             {
  274.                 if (psDesc) munlink(psDesc);
  275.                 exit(errno);
  276.             }
  277.         }
  278.     }
  279.  
  280.     if (!psDriver)
  281.     {
  282.         printf("Enter driver module: ");
  283.         gets(acBuf);
  284.         psDriver=(mod_driver*)modlink(acBuf,0);
  285.         if ((int)psDriver==ERR)
  286.         {
  287.             psDriver=(mod_driver*)modload(acBuf,0);
  288.             if ((int)psDriver==ERR)
  289.             {
  290.                 if (psDesc) munlink(psDesc);
  291.                 exit(errno);
  292.             }
  293.         }
  294.     }
  295.     if (!psDesc)
  296.     {
  297. ta_desc:
  298.         if (*argv)
  299.         {
  300.             strcpy(acBuf,*argv);
  301.         }
  302.         else
  303.         {
  304.             printf("Enter descriptor module: ");
  305.             gets(acBuf);
  306.         }
  307.         if (!isdesc(acBuf))
  308.         {
  309.             printf("Descriptor module name must have numeric extension\n");
  310.             goto ta_desc;
  311.         }
  312.         psDesc=(mod_dev*)modlink(acBuf,0);
  313.         if ((int)psDesc==ERR)
  314.         {
  315.             psDesc=(mod_dev*)modload(acBuf,0);
  316.             if ((int)psDesc==ERR)
  317.             {
  318.                 psDesc=(mod_dev*)make_module(acBuf,0xA0,(MA_REENT<<8)+1,0x777,(MT_DATA<<8)+0,0);
  319.                 if ((int)psDesc==ERR)
  320.                 {
  321.                     if (psDriver) munlink(psDriver);
  322.                     exit(errno);
  323.                 }
  324.  
  325.                 psDesc->_mfmgr=0x68;
  326.                 strcpy((char*)psDesc+0x68,"Scf");
  327.                 psDesc->_mpdev=0x70;
  328.                 strcpy((char*)psDesc+0x70,(char*)psDriver+psDriver->_mh._mname);
  329.                 psDesc->_mdevcon=0x80;
  330.                 psDesc->_mport=0;
  331.                 iUseDef=1;
  332.                 printf("Using default settings\n");
  333.             }
  334.         }
  335.     }
  336.  
  337.     printf("\n");
  338.  
  339.     pcTemp=(char*)psDriver+psDriver->_mexec+0x0E;
  340.     psConf=(struct sConf*)( (char*)psDriver+*(W*)pcTemp );
  341.  
  342.     if (*(W*)pcTemp) while (psConf->ct_type==T_MSG) input(psConf++);
  343.  
  344.     if (*(W*)pcTemp && psConf->ct_type && psConf->ct_offset!=0xFFFF)
  345.         input("\x02\x04\xFF\xFF\x00\x00\x00\x00Device address");
  346.  
  347.     if (!*(W*)pcTemp) 
  348.         input("\x04\x01\x00\x00\x00\x00\x00\x00Network device?");
  349.  
  350.     if (*(W*)pcTemp) while (psConf->ct_type) input(psConf++);
  351.  
  352.     _setcrc(psDesc);
  353.  
  354.     strcpy((char*)psDesc+0x70,(char*)psDriver+psDriver->_mh._mname);
  355.  
  356.     hFile=creat((char*)psDesc+psDesc->_mh._mname,0x07);
  357.     if (hFile!=ERR)
  358.     {
  359.         write(hFile,psDesc,psDesc->_mh._msize);
  360.         close(hFile);
  361.     }
  362.  
  363.     munlink(psDesc);
  364.     munlink(psDriver);
  365. }
  366.