ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

if.c
Go to the documentation of this file.
00001 /*
00002  *  IF.C - if internal batch command.
00003  *
00004  *
00005  *  History:
00006  *
00007  *    16 Jul 1998 (Hans B Pufal)
00008  *        started.
00009  *
00010  *    16 Jul 1998 (John P Price)
00011  *        Seperated commands into individual files.
00012  *
00013  *    27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
00014  *        added config.h include
00015  *
00016  *    07-Jan-1999 (Eric Kohl)
00017  *        Added help text ("if /?") and cleaned up.
00018  *
00019  *    21-Jan-1999 (Eric Kohl)
00020  *        Unicode and redirection ready!
00021  *
00022  *    01-Sep-1999 (Eric Kohl)
00023  *        Fixed help text.
00024  *
00025  *    17-Feb-2001 (ea)
00026  *        IF DEFINED variable command
00027  *
00028  *    28-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
00029  *        Remove all hardcode string to En.rc
00030  *
00031  */
00032 
00033 #include <precomp.h>
00034 
00035 static INT GenericCmp(INT (*StringCmp)(LPCTSTR, LPCTSTR),
00036                       LPCTSTR Left, LPCTSTR Right)
00037 {
00038     TCHAR *end;
00039     INT nLeft = _tcstol(Left, &end, 0);
00040     if (*end == _T('\0'))
00041     {
00042         INT nRight = _tcstol(Right, &end, 0);
00043         if (*end == _T('\0'))
00044         {
00045             /* both arguments are numeric */
00046             return (nLeft < nRight) ? -1 : (nLeft > nRight);
00047         }
00048     }
00049     return StringCmp(Left, Right);
00050 }
00051 
00052 INT cmd_if (LPTSTR param)
00053 {
00054     TRACE ("cmd_if: (\'%s\')\n", debugstr_aw(param));
00055 
00056     if (!_tcsncmp (param, _T("/?"), 2))
00057     {
00058         ConOutResPaging(TRUE,STRING_IF_HELP1);
00059         return 0;
00060     }
00061 
00062     error_syntax(param);
00063     return 1;
00064 }
00065 
00066 INT ExecuteIf(PARSED_COMMAND *Cmd)
00067 {
00068     INT result = FALSE; /* when set cause 'then' clause to be executed */
00069     LPTSTR param;
00070     LPTSTR Left = NULL, Right;
00071 
00072     if (Cmd->If.LeftArg)
00073     {
00074         Left = DoDelayedExpansion(Cmd->If.LeftArg);
00075         if (!Left)
00076             return 1;
00077     }
00078     Right = DoDelayedExpansion(Cmd->If.RightArg);
00079     if (!Right)
00080     {
00081         cmd_free(Left);
00082         return 1;
00083     }
00084 
00085     if (Cmd->If.Operator == IF_CMDEXTVERSION)
00086     {
00087         /* IF CMDEXTVERSION n: check if Command Extensions version
00088          * is greater or equal to n */
00089         DWORD n = _tcstoul(Right, &param, 10);
00090         if (*param != _T('\0'))
00091         {
00092             error_syntax(Right);
00093             cmd_free(Right);
00094             return 1;
00095         }
00096         result = (2 >= n);
00097     }
00098     else if (Cmd->If.Operator == IF_DEFINED)
00099     {
00100         /* IF DEFINED var: check if environment variable exists */
00101         result = (GetEnvVarOrSpecial(Right) != NULL);
00102     }
00103     else if (Cmd->If.Operator == IF_ERRORLEVEL)
00104     {
00105         /* IF ERRORLEVEL n: check if last exit code is greater or equal to n */
00106         INT n = _tcstol(Right, &param, 10);
00107         if (*param != _T('\0'))
00108         {
00109             error_syntax(Right);
00110             cmd_free(Right);
00111             return 1;
00112         }
00113         result = (nErrorLevel >= n);
00114     }
00115     else if (Cmd->If.Operator == IF_EXIST)
00116     {
00117         /* IF EXIST filename: check if file exists (wildcards allowed) */
00118         StripQuotes(Right);
00119 
00120         if (_tcschr(Right, _T('*')) || _tcschr(Right, _T('?')))
00121         {
00122             WIN32_FIND_DATA f;
00123             HANDLE hFind = FindFirstFile(Right, &f);
00124             if (hFind != INVALID_HANDLE_VALUE)
00125             {
00126                 result = TRUE;
00127                 FindClose(hFind);
00128             }
00129         }
00130         else
00131         {
00132             result = (GetFileAttributes(Right) != INVALID_FILE_ATTRIBUTES);
00133         }
00134     }
00135     else
00136     {
00137         /* Do case-insensitive string comparisons if /I specified */
00138         INT (*StringCmp)(LPCTSTR, LPCTSTR) =
00139             (Cmd->If.Flags & IFFLAG_IGNORECASE) ? _tcsicmp : _tcscmp;
00140 
00141         if (Cmd->If.Operator == IF_STRINGEQ)
00142         {
00143             /* IF str1 == str2 */
00144             result = StringCmp(Left, Right) == 0;
00145         }
00146         else
00147         {
00148             result = GenericCmp(StringCmp, Left, Right);
00149             switch (Cmd->If.Operator)
00150             {
00151             case IF_EQU: result = (result == 0); break;
00152             case IF_NEQ: result = (result != 0); break;
00153             case IF_LSS: result = (result < 0); break;
00154             case IF_LEQ: result = (result <= 0); break;
00155             case IF_GTR: result = (result > 0); break;
00156             case IF_GEQ: result = (result >= 0); break;
00157             }
00158         }
00159     }
00160 
00161     cmd_free(Left);
00162     cmd_free(Right);
00163 
00164     if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0))
00165     {
00166         /* full condition was true, do the command */
00167         return ExecuteCommand(Cmd->Subcommands);
00168     }
00169     else
00170     {
00171         /* full condition was false, do the "else" command if there is one */
00172         if (Cmd->Subcommands->Next)
00173             return ExecuteCommand(Cmd->Subcommands->Next);
00174         return 0;
00175     }
00176 }
00177 
00178 /* EOF */

Generated on Sun May 27 2012 04:18:12 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.