Back to index

lightning-sunbird  0.9+nobinonly
testxpi.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 "testxpi.h"
00041 
00042 //#define SSU_DEBUG
00043 
00044 HANDLE hXPIStubInst;
00045 
00046 /* Function to show the usage for this application */
00047 void ShowUsage(char *name)
00048 {
00049     char szBuf[MAX_BUF];
00050 
00051     wsprintf(szBuf, "Usage: %s <output sea name> <files [file1] [file2]...>\n", name);
00052     lstrcat(szBuf, "\n");
00053     lstrcat(szBuf, "    output sea name: name to use for the self-extracting executable\n");
00054     lstrcat(szBuf, "    files: one or more files to add to the self-extracing executable\n");
00055 }
00056 
00057 /* Function to print error message with/without error code */
00058 void PrintError(LPSTR szMsg, DWORD dwErrorCodeSH, int iExitCode)
00059 {
00060   DWORD dwErr;
00061   char  szErrorString[MAX_BUF];
00062 
00063   if(dwErrorCodeSH == ERROR_CODE_SHOW)
00064   {
00065     dwErr = GetLastError();
00066     wsprintf(szErrorString, "%d : %s", dwErr, szMsg);
00067   }
00068   else
00069     wsprintf(szErrorString, "%s", szMsg);
00070 
00071   printf("%s\n", szErrorString);
00072   printf("exit code: %d\n", iExitCode);
00073   if(iExitCode == 0)
00074     printf("\n** Success! **\n");
00075 
00076   DeInitializeXPIStub();
00077   exit(iExitCode);
00078 }
00079 
00080 /* Function to remove quotes from a string */
00081 void RemoveQuotes(LPSTR lpszSrc, LPSTR lpszDest, int iDestSize)
00082 {
00083   char *lpszBegin;
00084 
00085   if(lstrlen(lpszSrc) > iDestSize)
00086     return;
00087 
00088   if(*lpszSrc == '\"')
00089     lpszBegin = &lpszSrc[1];
00090   else
00091     lpszBegin = lpszSrc;
00092 
00093   lstrcpy(lpszDest, lpszBegin);
00094 
00095   if(lpszDest[lstrlen(lpszDest) - 1] == '\"')
00096     lpszDest[lstrlen(lpszDest) - 1] = '\0';
00097 }
00098 
00099 /* Function to remove the last backslash from a path string */
00100 void RemoveBackSlash(LPSTR szInput)
00101 {
00102   int   iCounter;
00103   DWORD dwInputLen;
00104 
00105   if(szInput != NULL)
00106   {
00107     dwInputLen = lstrlen(szInput);
00108 
00109     for(iCounter = dwInputLen -1; iCounter >= 0 ; iCounter--)
00110     {
00111       if(szInput[iCounter] == '\\')
00112         szInput[iCounter] = '\0';
00113       else
00114         break;
00115     }
00116   }
00117 }
00118 
00119 /* Function to append a backslash to a path string */
00120 void AppendBackSlash(LPSTR szInput, DWORD dwInputSize)
00121 {
00122   if(szInput != NULL)
00123   {
00124     if(szInput[strlen(szInput) - 1] != '\\')
00125     {
00126       if(((DWORD)lstrlen(szInput) + 1) < dwInputSize)
00127       {
00128         lstrcat(szInput, "\\");
00129       }
00130     }
00131   }
00132 }
00133 
00134 /* Function to parse a path string for one of three parts of a path:
00135  *   Filename only
00136  *   Path only
00137  *   drive only */
00138 void ParsePath(LPSTR szInput, LPSTR szOutput, DWORD dwOutputSize, DWORD dwType)
00139 {
00140   int   iCounter;
00141   DWORD dwCounter;
00142   DWORD dwInputLen;
00143   BOOL  bFound;
00144 
00145   if((szInput != NULL) && (szOutput != NULL))
00146   {
00147     bFound        = FALSE;
00148     dwInputLen    = lstrlen(szInput);
00149     ZeroMemory(szOutput, dwOutputSize);
00150 
00151     if(dwInputLen < dwOutputSize)
00152     {
00153       switch(dwType)
00154       {
00155         case PP_FILENAME_ONLY:
00156           for(iCounter = dwInputLen - 1; iCounter >= 0; iCounter--)
00157           {
00158             if(szInput[iCounter] == '\\')
00159             {
00160               lstrcpy(szOutput, &szInput[iCounter + 1]);
00161               bFound = TRUE;
00162               break;
00163             }
00164           }
00165           if(bFound == FALSE)
00166             lstrcpy(szOutput, szInput);
00167 
00168           break;
00169 
00170         case PP_PATH_ONLY:
00171           for(iCounter = dwInputLen - 1; iCounter >= 0; iCounter--)
00172           {
00173             if(szInput[iCounter] == '\\')
00174             {
00175               lstrcpy(szOutput, szInput);
00176               szOutput[iCounter + 1] = '\0';
00177               bFound = TRUE;
00178               break;
00179             }
00180           }
00181           if(bFound == FALSE)
00182             lstrcpy(szOutput, szInput);
00183 
00184           break;
00185 
00186         case PP_ROOT_ONLY:
00187           if(szInput[1] == ':')
00188           {
00189             szOutput[0] = szInput[0];
00190             szOutput[1] = szInput[1];
00191             AppendBackSlash(szOutput, dwOutputSize);
00192           }
00193           else if(szInput[1] == '\\')
00194           {
00195             int iFoundBackSlash = 0;
00196             for(dwCounter = 0; dwCounter < dwInputLen; dwCounter++)
00197             {
00198               if(szInput[dwCounter] == '\\')
00199               {
00200                 szOutput[dwCounter] = szInput[dwCounter];
00201                 ++iFoundBackSlash;
00202               }
00203 
00204               if(iFoundBackSlash == 3)
00205                 break;
00206             }
00207 
00208             if(iFoundBackSlash != 0)
00209               AppendBackSlash(szOutput, dwOutputSize);
00210           }
00211           break;
00212       }
00213     }
00214   }
00215 }
00216 
00217 /* Function to check to see if a file exists.
00218  * If it does, return it's attributes */
00219 long FileExists(LPSTR szFile)
00220 {
00221   DWORD rv;
00222 
00223   if((rv = GetFileAttributes(szFile)) == -1)
00224   {
00225     return(FALSE);
00226   }
00227   else
00228   {
00229     return(rv);
00230   }
00231 }
00232 
00233 /* Function to locate the first non space character in a string,
00234  * and return a pointer to it. */
00235 LPSTR GetFirstNonSpace(LPSTR lpszString)
00236 {
00237   int   i;
00238   int   iStrLength;
00239 
00240   iStrLength = lstrlen(lpszString);
00241 
00242   for(i = 0; i < iStrLength; i++)
00243   {
00244     if(!isspace(lpszString[i]))
00245       return(&lpszString[i]);
00246   }
00247 
00248   return(NULL);
00249 }
00250 
00251 /* Function to return the argument count given a command line input
00252  * format string */
00253 int GetArgC(LPSTR lpszCommandLine)
00254 {
00255   int   i;
00256   int   iArgCount;
00257   int   iStrLength;
00258   LPSTR lpszBeginStr;
00259   BOOL  bFoundQuote;
00260   BOOL  bFoundSpace;
00261 
00262   iArgCount    = 0;
00263   lpszBeginStr = GetFirstNonSpace(lpszCommandLine);
00264 
00265   if(lpszBeginStr == NULL)
00266     return(iArgCount);
00267 
00268   iStrLength   = lstrlen(lpszBeginStr);
00269   bFoundQuote  = FALSE;
00270   bFoundSpace  = TRUE;
00271 
00272   for(i = 0; i < iStrLength; i++)
00273   {
00274     if(lpszCommandLine[i] == '\"')
00275     {
00276       if(bFoundQuote == FALSE)
00277       {
00278         ++iArgCount;
00279         bFoundQuote = TRUE;
00280       }
00281       else
00282       {
00283         bFoundQuote = FALSE;
00284       }
00285     }
00286     else if(bFoundQuote == FALSE)
00287     {
00288       if(!isspace(lpszCommandLine[i]) && (bFoundSpace == TRUE))
00289       {
00290         ++iArgCount;
00291         bFoundSpace = FALSE;
00292       }
00293       else if(isspace(lpszCommandLine[i]))
00294       {
00295         bFoundSpace = TRUE;
00296       }
00297     }
00298   }
00299 
00300   return(iArgCount);
00301 }
00302 
00303 /* Function to return a specific argument parameter from a given command line input
00304  * format string. */
00305 LPSTR GetArgV(LPSTR lpszCommandLine, int iIndex, LPSTR lpszDest, int iDestSize)
00306 {
00307   int   i;
00308   int   j;
00309   int   iArgCount;
00310   int   iStrLength;
00311   LPSTR lpszBeginStr;
00312   LPSTR lpszDestTemp;
00313   BOOL  bFoundQuote;
00314   BOOL  bFoundSpace;
00315 
00316   iArgCount    = 0;
00317   lpszBeginStr = GetFirstNonSpace(lpszCommandLine);
00318 
00319   if(lpszBeginStr == NULL)
00320     return(NULL);
00321 
00322   lpszDestTemp = (char *)calloc(iDestSize, sizeof(char));
00323   if(lpszDestTemp == NULL)
00324     PrintError("Out of memory", ERROR_CODE_HIDE, 1);
00325 
00326   ZeroMemory(lpszDest, iDestSize);
00327   iStrLength    = lstrlen(lpszBeginStr);
00328   bFoundQuote   = FALSE;
00329   bFoundSpace   = TRUE;
00330   j             = 0;
00331 
00332   for(i = 0; i < iStrLength; i++)
00333   {
00334     if(lpszCommandLine[i] == '\"')
00335     {
00336       if(bFoundQuote == FALSE)
00337       {
00338         ++iArgCount;
00339         bFoundQuote = TRUE;
00340       }
00341       else
00342       {
00343         bFoundQuote = FALSE;
00344       }
00345     }
00346     else if(bFoundQuote == FALSE)
00347     {
00348       if(!isspace(lpszCommandLine[i]) && (bFoundSpace == TRUE))
00349       {
00350         ++iArgCount;
00351         bFoundSpace = FALSE;
00352       }
00353       else if(isspace(lpszCommandLine[i]))
00354       {
00355         bFoundSpace = TRUE;
00356       }
00357     }
00358 
00359     if((iIndex == (iArgCount - 1)) &&
00360       ((bFoundQuote == TRUE) || (bFoundSpace == FALSE) ||
00361       ((bFoundQuote == FALSE) && (lpszCommandLine[i] == '\"'))))
00362     {
00363       if(j < iDestSize)
00364       {
00365         lpszDestTemp[j] = lpszCommandLine[i];
00366         ++j;
00367       }
00368       else
00369       {
00370         lpszDestTemp[j] = '\0';
00371       }
00372     }
00373   }
00374 
00375   RemoveQuotes(lpszDestTemp, lpszDest, iDestSize);
00376   free(lpszDestTemp);
00377   return(lpszDest);
00378 }
00379 
00380 int main(void)
00381 {
00382   HRESULT           hrResult;
00383   char              szBuf[MAX_BUF];
00384   char              szAppName[MAX_BUF];
00385   char              szAppPath[MAX_BUF];
00386   char              *listArchive[] = {"e:\\cmonkey\\mozilla\\dist\\win32_d.obj\\bin\\test1.xpi",
00387                                       "e:\\cmonkey\\mozilla\\dist\\win32_d.obj\\bin\\test2.xpi",
00388                                       "\0"};
00389 
00390   if(GetModuleFileName(NULL, szBuf, sizeof(szBuf)) == 0L)
00391     PrintError("GetModuleFileName() failed", ERROR_CODE_SHOW, 1);
00392 
00393   ParsePath(szBuf, szAppPath, sizeof(szAppPath), PP_PATH_ONLY);
00394   ParsePath(szBuf, szAppName, sizeof(szAppName), PP_FILENAME_ONLY);
00395 
00396   hrResult = SmartUpdateJars(szAppName, szAppPath, listArchive);
00397 
00398   if(hrResult == 999)
00399     wsprintf(szBuf, "%s done\nReboot required\n", szAppName);
00400   else
00401     wsprintf(szBuf, "%s done\n", szAppName);
00402 
00403   PrintError(szBuf, ERROR_CODE_SHOW, hrResult);
00404   return(0);
00405 }
00406