Back to index

lightning-sunbird  0.9+nobinonly
parser.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Sean Su <ssu@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "extern.h"
00041 #include "logkeys.h"
00042 #include "extra.h"
00043 #include "parser.h"
00044 #include "ifuncns.h"
00045 #include "dialogs.h"
00046 
00047 #define KEY_SHARED_DLLS "Software\\Microsoft\\Windows\\CurrentVersion\\SharedDlls"
00048 
00049 BOOL DeleteOrDelayUntilReboot(PSZ szFile)
00050 {
00051   FILE      *ofp;
00052   char      szWinDir[MAX_BUF];
00053   char      szWininitFile[MAX_BUF];
00054   BOOL      bDelayDelete = FALSE;
00055   BOOL      bWriteRenameSection;
00056 
00057   FileDelete(szFile);
00058   if(FileExists(szFile))
00059   {
00060     DosReplaceModule(szFile, NULL, NULL);
00061     FileDelete(szFile);
00062   }
00063 
00064   return TRUE;
00065 }
00066 
00067 void RemoveUninstaller(PSZ szUninstallFilename)
00068 {
00069   char      szUninstallFile[MAX_BUF];
00070 
00071   strcpy(szUninstallFile, ugUninstall.szLogPath);
00072   strcat(szUninstallFile, "\\");
00073   strcat(szUninstallFile, szUninstallFilename);
00074   DeleteOrDelayUntilReboot(szUninstallFile);
00075   DirectoryRemove(ugUninstall.szLogPath, FALSE);
00076 }
00077 
00078 sil *InitSilNodes(char *szInFile)
00079 {
00080   FILE      *ifp;
00081   char      szLineRead[MAX_BUF];
00082   sil       *silTemp;
00083   sil       *silHead;
00084   unsigned long long ullLineCount;
00085 
00086   if(FileExists(szInFile) == FALSE)
00087     return(NULL);
00088 
00089   ullLineCount = 0;
00090   silHead      = NULL;
00091   if((ifp = fopen(szInFile, "r")) == NULL)
00092     exit(1);
00093 
00094   while(fgets(szLineRead, MAX_BUF, ifp) != NULL)
00095   {
00096     silTemp = CreateSilNode();
00097 
00098     silTemp->ullLineNumber = ++ullLineCount;
00099     strcpy(silTemp->szLine, szLineRead);
00100     if(silHead == NULL)
00101     {
00102       silHead = silTemp;
00103     }
00104     else
00105     {
00106       SilNodeInsert(silHead, silTemp);
00107     }
00108 
00109     ProcessWindowsMessages();
00110   }
00111   fclose(ifp);
00112   return(silHead);
00113 }
00114 
00115 void DeInitSilNodes(sil **silHead)
00116 {
00117   sil   *silTemp;
00118   
00119   if(*silHead == NULL)
00120   {
00121     return;
00122   }
00123   else if(((*silHead)->Prev == NULL) || ((*silHead)->Prev == *silHead))
00124   {
00125     SilNodeDelete(*silHead);
00126     return;
00127   }
00128   else
00129   {
00130     silTemp = (*silHead)->Prev;
00131   }
00132 
00133   while(silTemp != *silHead)
00134   {
00135     SilNodeDelete(silTemp);
00136     silTemp = (*silHead)->Prev;
00137 
00138     ProcessWindowsMessages();
00139   }
00140   SilNodeDelete(silTemp);
00141 }
00142 
00143 void ParseForFile(PSZ szString, PSZ szKeyStr, PSZ szFile, ULONG ulShortFilenameBufSize)
00144 {
00145   int     iLen;
00146   PSZ     szFirstNonSpace;
00147   char    szBuf[MAX_BUF];
00148 
00149   if((szFirstNonSpace = GetFirstNonSpace(&(szString[strlen(szKeyStr)]))) != NULL)
00150   {
00151     iLen = strlen(szFirstNonSpace);
00152     if(szFirstNonSpace[iLen - 1] == '\n')
00153       szFirstNonSpace[iLen -1] = '\0';
00154 
00155     strcpy(szFile, szFirstNonSpace);
00156   }
00157 }
00158 
00159 void ParseForCopyFile(PSZ szString, PSZ szKeyStr, PSZ szFile, ULONG ulShortFilenameBufSize)
00160 {
00161   int     iLen;
00162   PSZ     szFirstNonSpace;
00163   PSZ     szSubStr = NULL;
00164   char    szBuf[MAX_BUF];
00165 
00166   if((szSubStr = strstr(szString, " to ")) != NULL)
00167   {
00168     if((szFirstNonSpace = GetFirstNonSpace(&(szSubStr[strlen(" to ")]))) != NULL)
00169     {
00170       iLen = strlen(szFirstNonSpace);
00171       if(szFirstNonSpace[iLen - 1] == '\n')
00172         szFirstNonSpace[iLen -1] = '\0';
00173 
00174     strcpy(szFile, szFirstNonSpace);
00175     }
00176   }
00177 }
00178 
00179 void ParseForOS2INIInfo(PSZ szString, PSZ szKeyStr, PSZ szApp, ULONG ulAppBufSize, PSZ szKey, ULONG ulKeyBufSize)
00180 {
00181   int     i;
00182   int     iLen;
00183   int     iBrackets;
00184   char    szStrCopy[MAX_BUF];
00185   PSZ     szFirstNonSpace;
00186   PSZ     szFirstBackSlash;
00187   BOOL    bFoundOpenBracket;
00188   BOOL    bFoundName;
00189 
00190   strcpy(szStrCopy, szString);
00191   if((szFirstNonSpace = GetFirstNonSpace(&(szStrCopy[strlen(szKeyStr)]))) != NULL)
00192   {
00193     iLen = strlen(szFirstNonSpace);
00194     if(szFirstNonSpace[iLen - 1] == '\n')
00195     {
00196       szFirstNonSpace[--iLen] = '\0';
00197     }
00198 
00199     iLen = strlen(szFirstNonSpace);
00200 
00201     iBrackets         = 0;
00202     bFoundName        = FALSE;
00203     bFoundOpenBracket = FALSE;
00204     for(i = iLen - 1; i >= 0; i--)
00205     {
00206       if(bFoundName == FALSE)
00207       {
00208         /* Find the Name created in the Windows registry key.
00209          * Since the Name can contain '[' and ']', we have to
00210          * parse for the brackets that denote the Name string in
00211          * szFirstNonSpace.  It parses from right to left.
00212          */
00213         if(szFirstNonSpace[i] == ']')
00214         {
00215           if(iBrackets == 0)
00216             szFirstNonSpace[i] = '\0';
00217 
00218           ++iBrackets;
00219         }
00220         else if(szFirstNonSpace[i] == '[')
00221         {
00222           bFoundOpenBracket = TRUE;
00223           --iBrackets;
00224         }
00225 
00226         if((bFoundOpenBracket) && (iBrackets == 0))
00227         {
00228           strcpy(szKey, &(szFirstNonSpace[i + 1]));
00229           bFoundName = TRUE;
00230         }
00231       }
00232       else
00233       {
00234         /* locate the first non space to the left of the last '[' */
00235         if(!isspace(szFirstNonSpace[i]))
00236         {
00237           szFirstNonSpace[i + 1] = '\0';
00238           strcpy(szApp, szFirstNonSpace);
00239           break;
00240         }
00241       }
00242     }
00243   }
00244 }
00245 
00246 ULONG Uninstall(sil* silInstallLogHead)
00247 {
00248   sil   *silInstallLogTemp;
00249   PSZ   szSubStr;
00250   char  szLCLine[MAX_BUF];
00251   char  szApp[MAX_BUF];
00252   char  szKey[MAX_BUF];
00253   char  szFile[MAX_BUF];
00254 
00255   if(silInstallLogHead != NULL)
00256   {
00257     silInstallLogTemp = silInstallLogHead;
00258     do
00259     {
00260       silInstallLogTemp = silInstallLogTemp->Prev;
00261       strcpy(szLCLine, silInstallLogTemp->szLine);
00262       strlwr(szLCLine);
00263 
00264       if(((szSubStr = strstr(szLCLine, KEY_INSTALLING)) != NULL) &&
00265                (strstr(szLCLine, KEY_DO_NOT_UNINSTALL) == NULL))
00266       {
00267         /* check for "Installing: " string and delete the file */
00268         ParseForFile(szSubStr, KEY_INSTALLING, szFile, sizeof(szFile));
00269         DeleteOrDelayUntilReboot(szFile);
00270       }
00271       else if(((szSubStr = strstr(szLCLine, KEY_REPLACING)) != NULL) &&
00272                (strstr(szLCLine, KEY_DO_NOT_UNINSTALL) == NULL))
00273       {
00274         /* check for "Replacing: " string and delete the file */
00275         ParseForFile(szSubStr, KEY_REPLACING, szFile, sizeof(szFile));
00276         DeleteOrDelayUntilReboot(szFile);
00277       }
00278       else if(((szSubStr = strstr(silInstallLogTemp->szLine, KEY_STORE_INI_ENTRY)) != NULL) &&
00279                (strstr(szLCLine, KEY_DO_NOT_UNINSTALL) == NULL))
00280       {
00281         /* check for "Store INI Entry: " string and get the app/key pair */
00282         ParseForOS2INIInfo(szSubStr, KEY_STORE_INI_ENTRY, szApp, sizeof(szKey), szKey, sizeof(szKey));
00283         PrfWriteProfileString(HINI_USER, szApp, szKey, NULL);
00284       }
00285       else if(((szSubStr = strstr(silInstallLogTemp->szLine, KEY_OS2_OBJECT)) != NULL) &&
00286                (strstr(szLCLine, KEY_DO_NOT_UNINSTALL) == NULL))
00287       {
00288         /* check for "OS2 object" string and delete the object */
00289         HOBJECT hObj = NULLHANDLE;
00290         ParseForFile(szSubStr, KEY_OS2_OBJECT, szFile, sizeof(szFile));
00291         hObj = WinQueryObject(szFile);
00292         if (hObj) {
00293            WinDestroyObject(hObj);
00294         }
00295       }
00296       else if(((szSubStr = strstr(szLCLine, KEY_CREATE_FOLDER)) != NULL) &&
00297                (strstr(szLCLine, KEY_DO_NOT_UNINSTALL) == NULL))
00298       {
00299         ParseForFile(szSubStr, KEY_CREATE_FOLDER, szFile, sizeof(szFile));
00300         DirectoryRemove(szFile, FALSE);
00301       }
00302       else if(((szSubStr = strstr(szLCLine, KEY_COPY_FILE)) != NULL) &&
00303                (strstr(szLCLine, KEY_DO_NOT_UNINSTALL) == NULL))
00304       {
00305         /* check for "copy file: " string and delete the file */
00306         ParseForCopyFile(szSubStr, KEY_COPY_FILE, szFile, sizeof(szFile));
00307         DeleteOrDelayUntilReboot(szFile);
00308       }
00309 
00310       ProcessWindowsMessages();
00311     } while(silInstallLogTemp != silInstallLogHead);
00312   }
00313 
00314   return(0);
00315 }
00316 
00317 ULONG GetLogFile(PSZ szTargetPath, PSZ szInFilename, PSZ szOutBuf, ULONG dwOutBufSize)
00318 {
00319   int             iFilenameOnlyLen;
00320   char            szSearchFilename[MAX_BUF];
00321   char            szSearchTargetFullFilename[MAX_BUF];
00322   char            szFilenameOnly[MAX_BUF];
00323   char            szFilenameExtensionOnly[MAX_BUF];
00324   char            szNumber[MAX_BUF];
00325   long            ulNumber;
00326   long            ulMaxNumber;
00327   PSZ             szDotPtr;
00328   HDIR            hFile;
00329   FILEFINDBUF3    fdFile;
00330   ULONG           ulFindCount;
00331   ULONG           ulAttrs;
00332   BOOL            bFound;
00333 
00334   if(FileExists(szTargetPath))
00335   {
00336     /* zero out the memory */
00337     memset(szOutBuf,                0, dwOutBufSize);
00338     memset(szSearchFilename,        0, sizeof(szSearchFilename));
00339     memset(szFilenameOnly,          0, sizeof(szFilenameOnly));
00340     memset(szFilenameExtensionOnly, 0, sizeof(szFilenameExtensionOnly));
00341 
00342     /* parse for the filename w/o extention and also only the extension */
00343     if((szDotPtr = strstr(szInFilename, ".")) != NULL)
00344     {
00345       *szDotPtr = '\0';
00346       strcpy(szSearchFilename, szInFilename);
00347       strcpy(szFilenameOnly, szInFilename);
00348       strcpy(szFilenameExtensionOnly, &szDotPtr[1]);
00349       *szDotPtr = '.';
00350     }
00351     else
00352     {
00353       strcpy(szFilenameOnly, szInFilename);
00354       strcpy(szSearchFilename, szInFilename);
00355     }
00356 
00357     /* create the wild arg filename to search for in the szTargetPath */
00358     strcat(szSearchFilename, "*.*");
00359     strcpy(szSearchTargetFullFilename, szTargetPath);
00360     AppendBackSlash(szSearchTargetFullFilename, sizeof(szSearchTargetFullFilename));
00361     strcat(szSearchTargetFullFilename, szSearchFilename);
00362 
00363     iFilenameOnlyLen = strlen(szFilenameOnly);
00364     ulNumber         = 0;
00365     ulMaxNumber      = -1;
00366 
00367     /* find the largest numbered filename in the szTargetPath */
00368     ulFindCount = 1;
00369     hFile = HDIR_CREATE;
00370     ulAttrs = FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED;
00371     if((DosFindFirst(szSearchTargetFullFilename, &hFile, ulAttrs, &fdFile, sizeof(fdFile), &ulFindCount, FIL_STANDARD)) != NO_ERROR)
00372       bFound = FALSE;
00373     else
00374       bFound = TRUE;
00375 
00376     while(bFound)
00377     {
00378        memset(szNumber, 0, sizeof(szNumber));
00379       if((stricmp(fdFile.achName, ".") != 0) && (stricmp(fdFile.achName, "..") != 0))
00380       {
00381         strcpy(szNumber, &fdFile.achName[iFilenameOnlyLen]);
00382         ulNumber = atoi(szNumber);
00383         if(ulNumber > ulMaxNumber)
00384           ulMaxNumber = ulNumber;
00385       }
00386 
00387       ulFindCount = 1;
00388       if (DosFindNext(hFile, &fdFile, sizeof(fdFile), &ulFindCount) == NO_ERROR) {
00389         bFound = TRUE;
00390       } else {
00391         bFound = FALSE;
00392       }
00393     }
00394 
00395     DosFindClose(hFile);
00396 
00397     strcpy(szOutBuf, szTargetPath);
00398     AppendBackSlash(szOutBuf, dwOutBufSize);
00399     strcat(szOutBuf, szFilenameOnly);
00400     _itoa(ulMaxNumber, szNumber, 10);
00401     strcat(szOutBuf, szNumber);
00402 
00403     if(*szFilenameExtensionOnly != '\0')
00404     {
00405       strcat(szOutBuf, ".");
00406       strcat(szOutBuf, szFilenameExtensionOnly);
00407     }
00408   }
00409   else
00410     return(0);
00411   return(FileExists(szOutBuf));
00412 }
00413