Changeset 920 for trunk/dll/pathutil.c


Ignore:
Timestamp:
Jan 13, 2008, 2:18:17 AM (18 years ago)
Author:
Gregg Young
Message:

Cleanup of NormalizeCmdLine moved to pathutil.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dll/pathutil.c

    r907 r920  
    1010
    1111  05 Jan 08 SHL Move from arccnrs.c and comp.c to here
     12  06 Jan 08 GKY Add NormalizeCmdLine to check program strings on entry
    1213
    1314***********************************************************************/
     
    1617
    1718#define INCL_WIN
     19#define INCL_DOS
    1820#define INCL_LONGLONG
    1921
    2022#include "pathutil.h"
    2123#include "fm3dll.h"                     // needs_quoting
     24#include "fm3str.h"
     25#include "errutil.h"                    // Dos_Error...
     26#include "strutil.h"                    // GetPString
    2227
    2328// #pragma data_seg(DATA1)
     
    96101}
    97102
     103/** NormalizeCmdLine
     104 * Checks a command line for common errors (missing quotes, missing extension,
     105 * no space between exe and args etc)
     106 * Command line passed as pszCmdLine_
     107 * A pointer to a buffer of the size MAXCOMLINESTRG should be supplied in
     108 * pszWorkBuf. This is where the quoted etc as necessary command
     109 * line string will be returned.
     110 */
     111
     112PCSZ NormalizeCmdLine(PSZ pszWorkBuf, PSZ pszCmdLine_)
     113{
     114  char szCmdLine[MAXCOMLINESTRG], szArgs[MAXCOMLINESTRG];
     115  char *offset = '\0', *offsetexe, *offsetcom, *offsetcmd, *offsetbtm, *offsetbat;
     116  APIRET ret;
     117  ULONG ulAppType;
     118  char *pszChar;
     119  FILEFINDBUF3 FindBuffer;
     120  ULONG ulResultBufLen = sizeof(FILEFINDBUF3);
     121  HDIR hdirFindHandle = HDIR_CREATE;
     122  ULONG ulFindCount = 1;
     123  PSZ pszNewCmdLine = pszWorkBuf;
     124
     125  bstrip(pszCmdLine_);
     126  strcpy(szCmdLine, pszCmdLine_);
     127  if (szCmdLine[0] != '\0') {
     128    offsetexe = strstr(strlwr(pszCmdLine_), ".exe");
     129    offsetcmd = strstr(strlwr(pszCmdLine_), ".cmd");
     130    offsetcom = strstr(strlwr(pszCmdLine_), ".com");
     131    offsetbtm = strstr(strlwr(pszCmdLine_), ".btm");
     132    offsetbat = strstr(strlwr(pszCmdLine_), ".bat");
     133    if (offsetexe)
     134      offset = offsetexe;
     135    else {
     136      if (offsetcom)
     137        offset = offsetcom;
     138      else {
     139        if (offsetcmd)
     140          offset = offsetcmd;
     141        else {
     142          if (offsetbtm)
     143            offset = offsetbtm;
     144          else {
     145            if (offsetbat)
     146              offset = offsetexe;
     147          }
     148        }
     149      }
     150    }
     151    if (offset) {
     152      szCmdLine[offset + 4 - pszCmdLine_] = '\0';
     153      strcpy(szArgs, &pszCmdLine_[offset + 4 - pszCmdLine_]);
     154      while (strchr(szCmdLine, '\"'))
     155           remove_first_occurence_of_character("\"", szCmdLine);
     156      if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
     157           !strstr(pszCmdLine_, "\\:")||
     158           strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
     159        remove_first_occurence_of_character("\"", szArgs);
     160      if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
     161        saymsg(MB_OK, HWND_DESKTOP,
     162               NullStr,
     163               GetPString(IDS_QUOTESINARGSTEXT),
     164               pszCmdLine_);
     165      if (!offsetexe) {
     166        ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
     167                           ulResultBufLen, &ulFindCount, FIL_STANDARD);
     168        if (ret) {
     169          pszChar = szCmdLine;
     170          while (pszChar) {
     171            if (*pszChar == ' ') {
     172              *pszChar = '\0';
     173              strcat(szCmdLine, ".exe");
     174              ret = DosQueryAppType(szCmdLine, &ulAppType);
     175              //printf("%d %s\n", ret, szCmdLine); fflush(stdout);
     176              if (!ret) {
     177                strcpy(szArgs, pszCmdLine_ + strlen(szCmdLine) - 3);
     178                break;
     179              }
     180            }
     181            strcpy(szCmdLine, pszCmdLine_);
     182            pszChar++;
     183          }
     184        }
     185      }
     186      else
     187        ret = DosQueryAppType(szCmdLine, &ulAppType);
     188      BldQuotedFileName(pszNewCmdLine, szCmdLine);
     189      //printf("%d A", ret); fflush(stdout);
     190      if (ret) {
     191        ret = saymsg(MB_YESNO,
     192                     HWND_DESKTOP,
     193                     NullStr,
     194                     GetPString(IDS_PROGRAMNOTFOUNDTEXT),
     195                     pszCmdLine_);
     196        if (ret == MBID_YES){
     197          pszNewCmdLine = pszCmdLine_;
     198      }
     199        else{
     200          fCancelAction = TRUE;
     201          pszNewCmdLine = pszCmdLine_;
     202        }
     203      }
     204      else{
     205        if (szArgs[0] != ' ')
     206          strcat(pszNewCmdLine, " ");
     207        strcat(pszNewCmdLine, szArgs);
     208      }
     209
     210    }
     211    else if (szCmdLine && (!strchr(szCmdLine, '.') ||
     212                         strrchr(szCmdLine, '.' ) < strrchr(szCmdLine, '\\'))) {
     213      if (!strchr(szCmdLine, ' ')) {
     214        while (strchr(szCmdLine, '\"'))
     215          remove_first_occurence_of_character("\"", szCmdLine);
     216        strcat(szCmdLine, ".exe");
     217        ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
     218                           ulResultBufLen, &ulFindCount, FIL_STANDARD);
     219        //printf("%d", ret); fflush(stdout);
     220      }
     221      else {
     222        pszChar = szCmdLine;
     223        while (pszChar) {
     224          while (strchr(szCmdLine, '\"'))
     225            remove_first_occurence_of_character("\"", szCmdLine);
     226          if (*pszChar == ' ') {
     227            *pszChar = '\0';
     228            strcat(szCmdLine, ".exe");
     229            ret = DosQueryAppType(szCmdLine, &ulAppType);
     230            //printf("%d %s\n", ret, szCmdLine); fflush(stdout);
     231            if (!ret) {
     232              break;
     233            }
     234          }
     235          strcpy(szCmdLine, pszCmdLine_);
     236          pszChar++;
     237        }
     238      }
     239      if (!ret){
     240        BldQuotedFileName(pszNewCmdLine, szCmdLine);
     241        strcpy(szArgs, pszCmdLine_ + strlen(szCmdLine) - 3);
     242        if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
     243             !strstr(pszCmdLine_, "\\:" ) ||
     244             strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
     245          remove_first_occurence_of_character("\"", szArgs);
     246        if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
     247          saymsg(MB_OK, HWND_DESKTOP,
     248                 NullStr,
     249                 GetPString(IDS_QUOTESINARGSTEXT),
     250                 pszCmdLine_);
     251        if (szArgs[0] != ' ')
     252          strcat(pszNewCmdLine, " ");
     253        strcat(pszNewCmdLine, szArgs);
     254      }
     255      else {
     256        ret = saymsg(MB_OK,
     257                     HWND_DESKTOP,
     258                     NullStr,
     259                     GetPString(IDS_PROGRAMNOTEXE2TEXT),
     260                     pszCmdLine_);
     261          fCancelAction = TRUE;
     262          pszNewCmdLine = pszCmdLine_;
     263      }
     264    }
     265    else {
     266      pszChar = strrchr(szCmdLine, '.');
     267      while (pszChar && *pszChar !=' ') {
     268        pszChar++;
     269      }
     270      *pszChar = '\0';
     271      strcpy (szArgs, pszCmdLine_ + strlen(szCmdLine));
     272      while (strchr(szCmdLine, '\"'))
     273        remove_first_occurence_of_character("\"", szCmdLine);
     274    if ((szArgs[0] == '\"' && szArgs[1] == ' ') ||
     275         !strstr(pszCmdLine_, "\\:")||
     276         strchr(szArgs, '\"') == strrchr(szArgs, '\"'))
     277      remove_first_occurence_of_character("\"", szArgs);
     278    if (strchr(szArgs, '\"') != strrchr(szArgs, '\"'))
     279      saymsg(MB_OK, HWND_DESKTOP,
     280             NullStr,
     281             GetPString(IDS_QUOTESINARGSTEXT),
     282             pszCmdLine_);
     283    ret = DosFindFirst(szCmdLine, &hdirFindHandle, FILE_NORMAL, &FindBuffer,
     284                         ulResultBufLen, &ulFindCount, FIL_STANDARD);
     285
     286    BldQuotedFileName(pszNewCmdLine, szCmdLine);
     287    //printf("%d %s ", ret, szCmdLine); fflush(stdout);
     288    if (ret) {
     289      ret = saymsg(MB_YESNO,
     290                   HWND_DESKTOP,
     291                   NullStr,
     292                   GetPString(IDS_PROGRAMNOTFOUNDTEXT),
     293                   pszCmdLine_);
     294      if (ret == MBID_YES) {
     295        pszWorkBuf = pszCmdLine_;
     296      }
     297      else {
     298        fCancelAction = TRUE;
     299        pszWorkBuf = pszCmdLine_;
     300      }
     301    }
     302    ret = saymsg(MB_YESNOCANCEL,
     303                   HWND_DESKTOP,
     304                   NullStr,
     305                   GetPString(IDS_PROGRAMNOTEXE3TEXT),
     306                   pszCmdLine_, pszNewCmdLine);
     307      if (ret == MBID_YES){
     308        if (szArgs[0] != ' ')
     309          strcat(pszNewCmdLine, " ");
     310        strcat(pszNewCmdLine, szArgs);
     311      }
     312      if (ret == MBID_CANCEL){
     313        fCancelAction = TRUE;
     314        pszNewCmdLine = pszCmdLine_;
     315      }
     316    }
     317  }
     318  return pszWorkBuf;
     319}
     320
    98321#pragma alloc_text(PATHUTIL,BldFullPathName)
    99322#pragma alloc_text(PATHUTIL,BldQuotedFileName)
    100323#pragma alloc_text(PATHUTIL,BldQuotedFullPathName)
     324#pragma alloc_text(PATHUTIL,NormalizeCmdLine)
Note: See TracChangeset for help on using the changeset viewer.