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