home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libreg / tests / interp.c next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.3 KB  |  431 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. // Registry interpreter
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24.  
  25. #include "VerReg.h"
  26. #include "NSReg.h"
  27.  
  28. extern char *errstr(REGERR err);
  29. extern int DumpTree(void);
  30.  
  31. int gVerbose = 1;
  32. int gPretend = 0;
  33.  
  34. int error(char *func, int err)
  35. {
  36.     if (err == REGERR_OK)
  37.     {
  38.         if (gVerbose)
  39.             printf("%s Ok\n", func);
  40.     }
  41.     else
  42.     {
  43.         printf("%s returns %s\n", func, errstr(err));
  44.     }
  45.  
  46.     return err;
  47.  
  48. }    // error
  49.  
  50. static char  *GetNextWord(char *cmd, char *buf)
  51. {
  52.     // copies until ',' or eos, then skips spaces
  53.     if (!cmd || !buf)
  54.         return 0;
  55.     while (*cmd && *cmd != ',')
  56.         *buf++ = *cmd++;
  57.     *buf = '\0';
  58.     if (*cmd == ',')
  59.     {
  60.         cmd++;
  61.         while(*cmd && *cmd == ' ')
  62.             cmd++;
  63.     }
  64.     return cmd;
  65.  
  66. }    // GetNextWord
  67.  
  68. static int vr_ParseVersion(char *verstr, VERSION *result)
  69. {
  70.  
  71.     result->major = result->minor = result->release = result->build = 0;
  72.     result->major = atoi(verstr);
  73.     while (*verstr && *verstr != '.')
  74.         verstr++;
  75.     if (*verstr)
  76.     {
  77.         verstr++;
  78.         result->minor = atoi(verstr);
  79.         while (*verstr && *verstr != '.')
  80.             verstr++;
  81.         if (*verstr)
  82.         {
  83.             verstr++;
  84.             result->release = atoi(verstr);
  85.             while (*verstr && *verstr != '.')
  86.                 verstr++;
  87.             if (*verstr)
  88.             {
  89.                 verstr++;
  90.                 result->build = atoi(verstr);
  91.                 while (*verstr && *verstr != '.')
  92.                     verstr++;
  93.             }
  94.         }
  95.     }
  96.  
  97.     return REGERR_OK;
  98.  
  99. }    // ParseVersion
  100.  
  101. void parse(char *cmd, char *name, VERSION *ver, char *path)
  102. {
  103.     // expects 'cmd' points to: "<name>, <ver>, <path>"
  104.     char buf[256];
  105.     char *p;
  106.  
  107.     cmd = GetNextWord(cmd, buf);
  108.     strcpy(name, buf);
  109.  
  110.     p = cmd;                    // 'cmd' points to version
  111.     vr_ParseVersion(cmd, ver);
  112.     ver->check = gPretend ? 0xad : 0;
  113.     while (*p && *p != ',')        // skip to next ','
  114.         p++;
  115.     if (*p == ',')                // skip comma
  116.         p++;
  117.     while (*p && *p == ' ')        // skip white space
  118.         p++;
  119.  
  120.     strcpy(path, p);
  121.  
  122. }    // parse
  123.  
  124. void vVerbose(char *cmd)
  125. {
  126.  
  127.     if (stricmp(cmd, "ON") == 0)
  128.     {
  129.         gVerbose = 1;
  130.         printf("Verbose mode is now ON.\n");
  131.     }
  132.     else
  133.     {
  134.         gVerbose = 0;
  135.     }
  136.  
  137. }    // vVerbose
  138.  
  139. void vCreate(char *cmd)
  140. {
  141.  
  142.     // Syntax: Create [new,] 5.0b1
  143.     char buf[64];
  144.  
  145.     int flag = 0;
  146.     cmd = GetNextWord(cmd, buf);
  147.     if (stricmp(buf, "NEW,") == 0)
  148.     {
  149.         flag = CR_NEWREGISTRY;
  150.     }
  151.     error("VR_CreateRegistry", VR_CreateRegistry(flag, cmd));
  152.  
  153. }    // vCreate
  154.  
  155. void vDisplay(char *cmd)
  156. {
  157.  
  158.     DumpTree();
  159.  
  160. }    // vDisplay
  161.  
  162.  
  163. void vFind(char *cmd)
  164. {
  165.  
  166.     VERSION ver;
  167.     char path[MAXREGPATHLEN];
  168.  
  169.     if (error("VR_GetVersion", VR_GetVersion(cmd, &ver)) == REGERR_OK)
  170.     {
  171.         if (error("VR_GetPath", VR_GetPath(cmd, sizeof(path), path)) == REGERR_OK)
  172.         {
  173.             printf("%s found: ver=%d.%d.%d.%d, check=0x%04x, path=%s\n", 
  174.                 cmd, ver.major, ver.minor, ver.release, ver.build, ver.check,
  175.                 path);
  176.             return;
  177.         }
  178.     }
  179.  
  180.     printf("%s not found.\n", cmd);
  181.     return;
  182.  
  183. }    // vFind
  184.  
  185.  
  186. void vHelp(char *cmd)
  187. {
  188.  
  189.     puts("Enter a command:");
  190.     puts("\tD)isplay         - display the current contents of the Registry");
  191.     puts("\tI)nstall <name>, <version>, <path> - install a new component");
  192.     puts("\tU)pdate <name>, <version>, <path>  - update a component");
  193.     puts("\tF)ind <name>     - returns version and path");
  194.     puts("\tV)erify <name>   - verify component exists and checks out");
  195.     puts("\tC)reate <name>   - create a new instance of Navigator (e.g., \"4.0\")");
  196.     puts("\tR)emove <name>   - deletes a component from the Registry");
  197.     puts("\tT)est            - perform a simple test on the Registry");
  198.     puts("\tver(B)ose ON|off - turn verbose mode on or off");
  199.     puts("\tP)retend on|OFF  - pretend that test files exist");
  200.     puts("\tS)ave            - save the Registry to disk");
  201.     puts("\tpac(K) registry    - squeeze out unused space from the Registry");
  202.     puts("\tQ)uit            - end the program");
  203.  
  204. }    // vHelp
  205.  
  206.  
  207. void vInstall(char *cmd)
  208. {
  209.  
  210.     char name[MAXREGPATHLEN+1];
  211.     char path[MAXREGPATHLEN+1];
  212.     VERSION ver;
  213.  
  214.     parse(cmd, name, &ver, path);
  215.     error("VR_Install", VR_Install(name, path, &ver));
  216.  
  217. }    // vInstall
  218.  
  219. void vPack(char *cmd)
  220. {
  221.     error("VR_PackRegistry", VR_PackRegistry(0));
  222.  
  223. }    // vPack
  224.  
  225. void vPretend(char *cmd)
  226. {
  227.  
  228.     if (!cmd)
  229.     {
  230.         gPretend = !!gPretend;
  231.     }
  232.     else
  233.     {
  234.         if (stricmp(cmd, "ON") == 0)
  235.             gPretend = 1;
  236.         else
  237.             gPretend = 0;
  238.     }
  239.  
  240.     if (gVerbose)
  241.         printf("Pretend mode is %s\n", gPretend ? "ON" : "OFF");
  242.  
  243. }    // vPretend
  244.  
  245. void vRemove(char *cmd)
  246. {
  247.  
  248.     error("VR_Remove", VR_Remove(cmd));
  249.  
  250. }    // vRemove
  251.  
  252.  
  253. void vSave(char *cmd)
  254. {
  255.  
  256.     error("VR_Checkpoint", VR_Checkpoint());
  257.  
  258. }    // vSave
  259.  
  260.  
  261. void vTest(char *cmd)
  262. {
  263.  
  264.     VERSION ver;
  265.     ver.major = 4;
  266.     ver.minor = 0;
  267.     ver.release = 0;
  268.     ver.build = 237;
  269.     ver.check = gPretend ? 0xad : 0;
  270.  
  271.     if (error("VR_Install", VR_Install("Navigator/NS.exe", 
  272.         "c:\\Program Files\\Netscape\\Navigator\\Program\\NETSCAPE.EXE", &ver)))
  273.         return;
  274.     if (error("VR_Install", VR_Install("Navigator/Help", 
  275.         "c:\\Program Files\\Netscape\\Navigator\\Program\\NETSCAPE.HLP", &ver)))
  276.         return;
  277.     if (error("VR_Install", VR_Install("Navigator/NSPR", 
  278.         "c:\\Program Files\\Netscape\\Navigator\\Program\\NSPR32.DLL", &ver)))
  279.         return;
  280.     if (error("VR_Install", VR_Install("Navigator/Player", 
  281.         "c:\\Program Files\\Netscape\\Navigator\\Program\\NSPLAYER.EXE", &ver)))
  282.         return;
  283.     if (error("VR_Install", VR_Install("Navigator/NSJava", 
  284.         "c:\\Program Files\\Netscape\\Navigator\\Program\\NSJAVA32.DLL", &ver)))
  285.         return;
  286.     if (error("VR_Install", VR_Install("Web/Certificate.DB", 
  287.         "c:\\Program Files\\Netscape\\Navigator\\Program\\CERT.DB", &ver)))
  288.         return;
  289.     if (error("VR_Install", VR_Install("Web/CertificateNI.DB", 
  290.         "c:\\Program Files\\Netscape\\Navigator\\Program\\CERTNI.DB", &ver)))
  291.         return;
  292.     if (error("VR_Install", VR_Install("Web/Keys", 
  293.         "c:\\Program Files\\Netscape\\Navigator\\Program\\KEY.DB", &ver)))
  294.         return;
  295.     if (error("VR_Install", VR_Install("MailNews/Postal", 
  296.         "c:\\Program Files\\Netscape\\Navigator\\System\\POSTAL32.DLL", &ver)))
  297.         return;
  298.     if (error("VR_Install", VR_Install("MailNews/Folders/Inbox", 
  299.         "c:\\Program Files\\Netscape\\Navigator\\Mail\\INBOX.SNM", &ver)))
  300.         return;
  301.     if (error("VR_Install", VR_Install("MailNews/Folders/Sent", 
  302.         "c:\\Program Files\\Netscape\\Navigator\\Mail\\SENT.SNM", &ver)))
  303.         return;
  304.     if (error("VR_Install", VR_Install("MailNews/Folders/Trash", 
  305.         "c:\\Program Files\\Netscape\\Navigator\\Mail\\TRASH.SNM", &ver)))
  306.         return;
  307.     if (error("VR_Install", VR_Install("Components/NUL", 
  308.         "c:\\Program Files\\Netscape\\Navigator\\Program\\Plugins\\NPNUL32.DLL", &ver)))
  309.         return;
  310.     if (error("VR_Install", VR_Install("Components/PointCast", 
  311.         "c:\\Program Files\\Netscape\\Navigator\\Program\\Plugins\\NPPCN32.DLL", &ver)))
  312.         return;
  313.     if (error("VR_Install", VR_Install("Components/AWT", 
  314.         "c:\\Program Files\\Netscape\\Navigator\\Program\\Java\\bin\\AWT3220.DLL", &ver)))
  315.         return;
  316.     if (error("VR_Install", VR_Install("Components/MM", 
  317.         "c:\\Program Files\\Netscape\\Navigator\\Program\\Java\\bin\\MM3220.DLL", &ver)))
  318.         return;
  319.     if (error("VR_Install", VR_Install("Java/Classes.Zip", 
  320.         "c:\\Program Files\\Netscape\\Navigator\\Program\\Java\\classes\\MOZ2_0.ZIP", &ver)))
  321.         return;
  322.     if (error("VR_Install", VR_Install("Java/Classes Directory", 
  323.         "c:\\Program Files\\Netscape\\Navigator\\Program\\Java\\classes\\MOZ2_0", &ver)))
  324.         return;
  325.  
  326.  
  327. }    // vTest
  328.  
  329.  
  330. void vUpdate(char *cmd)
  331. {
  332.  
  333.     char name[MAXREGPATHLEN+1];
  334.     char path[MAXREGPATHLEN+1];
  335.     VERSION ver;
  336.  
  337.     parse(cmd, name, &ver, path);
  338.     error("VR_Update", VR_Update(name, path, &ver));
  339.  
  340. }    // vUpdate
  341.  
  342.  
  343. void vVerify(char *cmd)
  344. {
  345.  
  346.     error("VR_CheckEntry", VR_CheckEntry(0, cmd));
  347.  
  348. }    // vVerify
  349.  
  350.             
  351. void interp(void)
  352. {
  353.  
  354.     char line[256];
  355.     char *p;
  356.  
  357.     while(1)
  358.     {
  359.         putchar('>');
  360.         putchar(' ');
  361.         flushall();
  362.         gets(line);
  363.  
  364.         // p points to next word after verb on command line
  365.         p = line;
  366.         while (*p && *p!=' ')
  367.             p++;
  368.         if (!*p)
  369.             p = 0;
  370.         else
  371.         {
  372.             while(*p && *p==' ')
  373.                 p++;
  374.         }
  375.  
  376.         switch(toupper(line[0]))
  377.         {
  378.         case 'B':
  379.             vVerbose(p);
  380.             break;
  381.         case 'C':
  382.             vCreate(p);
  383.             break;
  384.         case 'D':
  385.             vDisplay(p);
  386.             break;
  387.         case 'F':
  388.             vFind(p);
  389.             break;
  390.         case 'H':
  391.         default:
  392.             vHelp(line);
  393.             break;
  394.         case 'I':
  395.             vInstall(p);
  396.             break;
  397.         case 'K':
  398.             vPack(p);
  399.             break;
  400.         case 'P':
  401.             vPretend(p);
  402.             break;
  403.         case 'R':
  404.             vRemove(p);
  405.             break;
  406.         case 'S':
  407.             vSave(p);
  408.             break;
  409.         case 'T':
  410.             vTest(p);
  411.             break;
  412.         case 'U':
  413.             vUpdate(p);
  414.             break;
  415.         case 'V':
  416.             vVerify(p);
  417.             break;
  418.         case 'Q':
  419.         case 'X':
  420.             vSave(0);
  421.             return;
  422.         }    // switch
  423.     }    // while
  424.  
  425.     assert(0);
  426.     return;    // shouldn't get here
  427.  
  428. }    // interp
  429.  
  430. // EOF: interp.c
  431.