Changeset 985 for trunk/dll/assoc.c


Ignore:
Timestamp:
Mar 1, 2008, 2:37:14 AM (17 years ago)
Author:
Gregg Young
Message:

Update sizes dialog (ticket 44); Make max command line length user settable (ticket 199); use xfree for free in most cases (ticket 212); initial code to check for valid ini file (ticket 102); Some additional refactoring and structure rework; Some documentation updates;

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dll/assoc.c

    r959 r985  
    1616  20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
    1717  06 Jan 08 GKY Use NormalizeCmdLine to check program strings on entry
     18  29 Feb 08 GKY Changes to enable user settable command line length
     19  29 Feb 08 GKY Use xfree where appropriate
    1820
    1921**************************************************************************************/
     
    4042typedef struct
    4143{
    42   CHAR mask[CCHMAXPATH];
    43   CHAR cl[MAXCOMLINESTRG];
    44   CHAR sig[CCHMAXPATH];
    4544  LONG offset;
    4645  ULONG flags;
     46  PSZ pszCmdLine;
     47  CHAR mask[CCHMAXPATH];
     48  CHAR sig[CCHMAXPATH];
    4749}
    4850ASSOC;
     
    5153{
    5254  CHAR *mask;
    53   CHAR *cl;
     55  PSZ pszCmdLine;
    5456  CHAR *sig;
    5557  LONG offset;
     
    118120    next = info->next;
    119121    xfree(info->mask);
    120     xfree(info->cl);
     122    xfree(info->pszCmdLine);
    121123    xfree(info->sig);
    122124    xfree(info);
     
    130132  FILE *fp;
    131133  LINKASSOC *info;
    132   CHAR cl[1024];
     134  PSZ pszCmdLine;
    133135  CHAR mask[CCHMAXPATH + 24];
    134136  CHAR sig[CCHMAXPATH + 24];
     
    144146  strcat(mask, "ASSOC.DAT");
    145147  fp = _fsopen(mask, "r", SH_DENYWR);
     148  pszCmdLine = xmallocz(MaxComLineStrg, pszSrcFile, __LINE__);
     149  if (!pszCmdLine) {
     150    if (fp)
     151      fclose(fp); //already complained
     152  }
    146153  if (fp) {
    147154    while (!feof(fp)) {
     
    152159      if (!*mask || *mask == ';')
    153160        continue;
    154       if (!xfgets(cl, sizeof(cl), fp, pszSrcFile, __LINE__) ||
     161      if (!xfgets(pszCmdLine, MaxComLineStrg, fp, pszSrcFile, __LINE__) ||
    155162          !xfgets(sig, CCHMAXPATH + 24, fp, pszSrcFile, __LINE__) ||
    156163          !xfgets(offset, sizeof(offset), fp, pszSrcFile, __LINE__) ||
    157164          !xfgets(flags, sizeof(flags), fp, pszSrcFile, __LINE__))
    158165        break;                          /* error! */
    159       cl[1000] = 0;
    160       bstripcr(cl);
     166      pszCmdLine[MaxComLineStrg - 1] = 0;
     167      bstripcr(pszCmdLine);
    161168      sig[CCHMAXPATH] = 0;
    162169      bstripcr(sig);
     
    165172      flags[34] = 0;
    166173      bstripcr(flags);
    167       if (!*cl)
     174      if (!*pszCmdLine)
    168175        continue;
    169176      info = xmallocz(sizeof(LINKASSOC), pszSrcFile, __LINE__);
    170177      if (info) {
    171         info->cl = xstrdup(cl, pszSrcFile, __LINE__);
     178        info->pszCmdLine = xstrdup(pszCmdLine, pszSrcFile, __LINE__);
    172179        info->mask = xstrdup(mask, pszSrcFile, __LINE__);
    173180        if (*sig)
     
    175182        info->offset = atol(offset);
    176183        info->flags = atol(flags);
    177         if (!info->cl || !info->mask) {
    178           xfree(info->cl);
     184        if (!info->pszCmdLine || !info->mask) {
     185          xfree(info->pszCmdLine);
    179186          xfree(info->mask);
    180187          xfree(info);
     
    191198    }
    192199    fclose(fp);
     200  }
     201}
     202
     203VOID display_associations(HWND hwnd, ASSOC *temp, LINKASSOC *info)
     204{
     205  CHAR szEnviroment[2048];
     206  PSZ pszDisplayStr;
     207  SHORT x;
     208
     209  *szEnviroment = 0;
     210  WinQueryDlgItemText(hwnd, ASS_ENVIRON, 2048, szEnviroment);
     211  bstripcr(szEnviroment);
     212  if (*szEnviroment)
     213    PrfWriteProfileString(fmprof, FM3Str, temp->pszCmdLine, szEnviroment);
     214  pszDisplayStr = xmallocz((CCHMAXPATH * 2) + MaxComLineStrg + 6,
     215                           pszSrcFile, __LINE__);
     216  if (pszDisplayStr) {
     217    sprintf(pszDisplayStr, "%-12s \x1a %-24s %s%s%s", temp->mask,
     218            temp->pszCmdLine, (*temp->sig) ?
     219            "[" : NullStr, (*temp->sig) ? temp->sig : NullStr,
     220            (*temp->sig) ? "]" : NullStr);
     221    x = (SHORT) WinSendDlgItemMsg(hwnd,
     222                                  ASS_LISTBOX,
     223                                  LM_INSERTITEM,
     224                                  MPFROM2SHORT(LIT_END, 0), MPFROMP(pszDisplayStr));
     225    if (x >= 0) {
     226      WinSendDlgItemMsg(hwnd,
     227                        ASS_LISTBOX,
     228                        LM_SETITEMHANDLE,
     229                        MPFROMSHORT(x), MPFROMP(info));
     230      WinSendDlgItemMsg(hwnd,
     231                        ASS_LISTBOX,
     232                        LM_SELECTITEM,
     233                        MPFROMSHORT(x), MPFROMSHORT(TRUE));
     234    }
     235    xfree(pszDisplayStr);
    193236  }
    194237}
     
    232275    while (info) {
    233276      fprintf(fp,
    234               ";\n%0.*s\n%0.1000s\n%0.*s\n%lu\n%lu\n",
     277              ";\n%0.*s\n%0.4096s\n%0.*s\n%lu\n%lu\n",
    235278              CCHMAXPATH,
    236279              info->mask,
    237               info->cl,
     280              info->pszCmdLine,
    238281              CCHMAXPATH,
    239282              (info->sig) ? info->sig : NullStr, info->offset, info->flags);
     
    248291  LINKASSOC *info;
    249292
    250   if (addme && *addme->cl && *addme->mask) {
     293  if (addme && *addme->pszCmdLine && *addme->mask) {
    251294    info = asshead;
    252295    while (info) {
     
    260303      info = xmallocz(sizeof(LINKASSOC), pszSrcFile, __LINE__);
    261304      if (info) {
    262         info->cl = xstrdup(addme->cl, pszSrcFile, __LINE__);
     305        info->pszCmdLine = xstrdup(addme->pszCmdLine, pszSrcFile, __LINE__);
    263306        info->mask = xstrdup(addme->mask, pszSrcFile, __LINE__);
    264307        if (*addme->sig)
     
    268311        if (addme->flags)
    269312          info->flags = addme->flags;
    270         if (!info->cl || !info->mask) {
    271           xfree(info->cl);
     313        if (!info->pszCmdLine || !info->mask) {
     314          xfree(info->pszCmdLine);
    272315          xfree(info->mask);
    273316          xfree(info);
     
    315358            asstail = info->prev;
    316359        }
    317         xfree(info->cl);
     360        xfree(info->pszCmdLine);
    318361        xfree(info->mask);
    319362        xfree(info->sig);
     
    392435
    393436        if (fAmAV2) {
    394           if (stristr(info->cl, "AV2.EXE") ||
    395               stristr(info->cl, "AV2.CMD") || stristr(info->cl, "<>"))
     437          if (stristr(info->pszCmdLine, "AV2.EXE") ||
     438              stristr(info->pszCmdLine, "AV2.CMD") || stristr(info->pszCmdLine, "<>"))
    396439            return -1;
    397440        }
    398         if (!strcmp(info->cl, "<>")) {
     441        if (!strcmp(info->pszCmdLine, "<>")) {
    399442          OpenObject(datafile, Default, hwnd);
    400443          return 0;
     
    414457        flags &= (~DIEAFTER);
    415458        rc = ExecOnList(hwnd,
    416                         info->cl,
     459                        info->pszCmdLine,
    417460                        flags,
    418461                        NULL, list, GetPString(IDS_EXECASSOCTITLETEXT),
     
    467510  case UM_UNDO:
    468511    {
    469       CHAR s[1002 + CCHMAXPATH + 6];
     512      CHAR s[4096];
    470513
    471514      WinSendDlgItemMsg(hwnd, ASS_LISTBOX, LM_DELETEALL, MPVOID, MPVOID);
     
    475518                "%-12s \x1a %-24s %s%s%s",
    476519                info->mask,
    477                 info->cl,
     520                info->pszCmdLine,
    478521                (info->sig && *info->sig) ?
    479522                "[" : NullStr,
     
    518561          }
    519562          WinSetDlgItemText(hwnd, ASS_MASK, info->mask);
    520           WinSetDlgItemText(hwnd, ASS_CL, info->cl);
     563          WinSetDlgItemText(hwnd, ASS_CL, info->pszCmdLine);
    521564          WinSetDlgItemText(hwnd, ASS_SIG,
    522565                            (info->sig && *info->sig) ? info->sig : NullStr);
     
    545588            size = sizeof(env) - 1;
    546589            if (PrfQueryProfileData(fmprof,
    547                                     FM3Str, info->cl, env, &size) && *env)
     590                                    FM3Str, info->pszCmdLine, env, &size) && *env)
    548591              WinSetDlgItemText(hwnd, ASS_ENVIRON, env);
    549592            else
     
    629672        replace = FALSE;
    630673
     674        memset(&temp, 0, sizeof(ASSOC));
     675        temp.pszCmdLine = xmallocz(MaxComLineStrg, pszSrcFile, __LINE__);
     676        if (!temp.pszCmdLine)
     677          break; //already complained
     678
    631679        {
    632680          x = (SHORT) WinSendDlgItemMsg(hwnd,
     
    639687                                          MPFROMSHORT(0), MPFROMSHORT(TRUE));
    640688        }
    641         pszWorkBuf = xmalloc(MAXCOMLINESTRG, pszSrcFile, __LINE__);
    642         if (!pszWorkBuf)
     689        pszWorkBuf = xmalloc(MaxComLineStrg, pszSrcFile, __LINE__);
     690        if (!pszWorkBuf) {
     691          xfree(temp.pszCmdLine);
    643692          break; //already complained
    644         memset(&temp, 0, sizeof(ASSOC));
     693        }
    645694        WinQueryDlgItemText(hwnd, ASS_MASK, sizeof(temp.mask), temp.mask);
    646         WinQueryDlgItemText(hwnd, ASS_CL, sizeof(temp.cl), temp.cl);
    647         NormalizeCmdLine(pszWorkBuf, temp.cl);
    648         memcpy(temp.cl, pszWorkBuf, strlen(pszWorkBuf) + 1);
     695        WinQueryDlgItemText(hwnd, ASS_CL, MaxComLineStrg, temp.pszCmdLine);
     696        if (strcmp(temp.pszCmdLine, "<>")) {
     697          NormalizeCmdLine(pszWorkBuf, temp.pszCmdLine);
     698          memcpy(temp.pszCmdLine, pszWorkBuf, strlen(pszWorkBuf) + 1);
     699        }
    649700        xfree(pszWorkBuf);
    650701        WinQueryDlgItemText(hwnd, ASS_SIG, sizeof(temp.sig), temp.sig);
     
    655706        }
    656707        bstrip(temp.mask);
    657         bstrip(temp.cl);
     708        bstrip(temp.pszCmdLine);
    658709        if (WinQueryButtonCheckstate(hwnd, ASS_DEFAULT))
    659710          temp.flags = 0;
     
    674725        if (fCancelAction){
    675726          fCancelAction = FALSE;
     727          xfree(temp.pszCmdLine);
    676728          break;
    677729        }
     
    680732        if (!info)
    681733          WinDismissDlg(hwnd, 1);       /* Runtime_Error(pszSrcFile, __LINE__, "add_association"); */
    682         else {
    683 
    684           CHAR s[1002 + CCHMAXPATH + 6];
    685 
    686           *s = 0;
    687           WinQueryDlgItemText(hwnd, ASS_ENVIRON, 1000, s);
    688           bstripcr(s);
    689           if (*s)
    690             PrfWriteProfileString(fmprof, FM3Str, temp.cl, s);
    691           sprintf(s, "%-12s \x1a %-24s %s%s%s", temp.mask,
    692                   temp.cl, (*temp.sig) ?
    693                   "[" : NullStr, (*temp.sig) ? temp.sig : NullStr,
    694                   (*temp.sig) ? "]" : NullStr);
    695           x = (SHORT) WinSendDlgItemMsg(hwnd,
    696                                         ASS_LISTBOX,
    697                                         LM_INSERTITEM,
    698                                         MPFROM2SHORT(LIT_END, 0), MPFROMP(s));
    699           if (x >= 0) {
    700             WinSendDlgItemMsg(hwnd,
    701                               ASS_LISTBOX,
    702                               LM_SETITEMHANDLE,
    703                               MPFROMSHORT(x), MPFROMP(info));
    704             WinSendDlgItemMsg(hwnd,
    705                               ASS_LISTBOX,
    706                               LM_SELECTITEM,
    707                               MPFROMSHORT(x), MPFROMSHORT(TRUE));
    708           }
     734        else {
     735          display_associations(hwnd, &temp, info);
    709736          save_associations();
    710         }
     737        }
     738        xfree(temp.pszCmdLine);
    711739      }
    712740      WinDismissDlg(hwnd, 1);
     
    728756        CHAR dummy[34];
    729757        PSZ pszWorkBuf;
    730         replace = FALSE;
    731 
    732         pszWorkBuf = xmalloc(MAXCOMLINESTRG, pszSrcFile, __LINE__);
    733         if (!pszWorkBuf)
     758        replace = FALSE;
     759
     760        memset(&temp, 0, sizeof(ASSOC));
     761        temp.pszCmdLine = xmallocz(MaxComLineStrg, pszSrcFile, __LINE__);
     762        if (!temp.pszCmdLine)
    734763          break; //already complained
    735         memset(&temp, 0, sizeof(ASSOC));
    736         WinQueryDlgItemText(hwnd, ASS_MASK, sizeof(temp.mask), temp.mask);
    737         WinQueryDlgItemText(hwnd, ASS_CL, sizeof(temp.cl), temp.cl);
    738         NormalizeCmdLine(pszWorkBuf, temp.cl);
    739         memcpy(temp.cl, pszWorkBuf, strlen(pszWorkBuf) + 1);
     764        pszWorkBuf = xmalloc(MaxComLineStrg, pszSrcFile, __LINE__);
     765        if (!pszWorkBuf) {
     766          xfree(temp.pszCmdLine);
     767          break; //already complained
     768        }
     769        WinQueryDlgItemText(hwnd, ASS_MASK, sizeof(temp.mask), temp.mask);
     770        WinQueryDlgItemText(hwnd, ASS_CL, MaxComLineStrg, temp.pszCmdLine);
     771        if (strcmp(temp.pszCmdLine, "<>")) {
     772          NormalizeCmdLine(pszWorkBuf, temp.pszCmdLine);
     773          memcpy(temp.pszCmdLine, pszWorkBuf, strlen(pszWorkBuf) + 1);
     774        }
    740775        xfree(pszWorkBuf);
    741776        WinQueryDlgItemText(hwnd, ASS_SIG, sizeof(temp.sig), temp.sig);
     
    746781        }
    747782        bstrip(temp.mask);
    748         bstrip(temp.cl);
     783        bstrip(temp.pszCmdLine);
    749784        if (WinQueryButtonCheckstate(hwnd, ASS_DEFAULT))
    750785          temp.flags = 0;
     
    765800        if (fCancelAction){
    766801          fCancelAction = FALSE;
     802          xfree(temp.pszCmdLine);
    767803          break;
    768804        }
     
    771807        //Add will fail if mask is not changed
    772808        if (info) {
    773 
    774           CHAR s[1002 + CCHMAXPATH + 6];
    775 
    776           *s = 0;
    777           WinQueryDlgItemText(hwnd, ASS_ENVIRON, 1000, s);
    778           bstripcr(s);
    779           if (*s)
    780             PrfWriteProfileString(fmprof, FM3Str, temp.cl, s);
    781           sprintf(s, "%-12s \x1a %-24s %s%s%s", temp.mask,
    782                   temp.cl, (*temp.sig) ?
    783                   "[" : NullStr, (*temp.sig) ? temp.sig : NullStr,
    784                   (*temp.sig) ? "]" : NullStr);
    785           x = (SHORT) WinSendDlgItemMsg(hwnd,
    786                                         ASS_LISTBOX,
    787                                         LM_INSERTITEM,
    788                                         MPFROM2SHORT(LIT_END, 0), MPFROMP(s));
    789           if (x >= 0) {
    790             WinSendDlgItemMsg(hwnd,
    791                               ASS_LISTBOX,
    792                               LM_SETITEMHANDLE,
    793                               MPFROMSHORT(x), MPFROMP(info));
    794             WinSendDlgItemMsg(hwnd,
    795                               ASS_LISTBOX,
    796                               LM_SELECTITEM,
    797                               MPFROMSHORT(x), MPFROMSHORT(TRUE));
    798           }
     809          display_associations(hwnd, &temp, info);
    799810          save_associations();
    800811        }
     812        xfree(temp.pszCmdLine);
    801813      }
    802814      break;
     
    805817      {
    806818        ASSOC temp;
    807         CHAR dummy[34];
    808 
    809         memset(&temp, 0, sizeof(ASSOC));
     819        CHAR dummy[34];
     820
     821        memset(&temp, 0, sizeof(ASSOC));
     822        temp.pszCmdLine = xmallocz(MaxComLineStrg, pszSrcFile, __LINE__);
     823        if (!temp.pszCmdLine)
     824          break; //already complained
    810825        WinQueryDlgItemText(hwnd, ASS_MASK, sizeof(temp.mask), temp.mask);
    811826        WinQueryDlgItemText(hwnd, ASS_SIG, sizeof(temp.sig), temp.sig);
     
    817832        bstrip(temp.mask);
    818833        PrfWriteProfileData(fmprof, FM3Str, temp.mask, NULL, 0L);
    819         if (kill_association(&temp))
    820          /* Runtime_Error(pszSrcFile, __LINE__, "kill_association");
    821         else */{
     834        if (kill_association(&temp)) {
    822835          x = (SHORT) WinSendDlgItemMsg(hwnd,
    823836                                        ASS_LISTBOX,
     
    832845          }
    833846          save_associations();
    834         }
    835       }
     847        }
     848        xfree(temp.pszCmdLine);
     849      }
     850
    836851      break;
    837852    case ASS_REPLACE:
     
    841856        CHAR dummy[34];
    842857        PSZ pszWorkBuf;
    843         replace = TRUE;
    844 
     858        replace = TRUE;
     859
     860        memset(&temp, 0, sizeof(ASSOC));
     861        temp.pszCmdLine = xmallocz(MaxComLineStrg, pszSrcFile, __LINE__);
     862        if (!temp.pszCmdLine)
     863          break; //already complained
    845864        y = (SHORT) WinSendDlgItemMsg(hwnd,
    846865                                      ASS_LISTBOX,
    847866                                      LM_QUERYSELECTION,
    848867                                      MPFROMSHORT(LIT_CURSOR), MPVOID);
    849         pszWorkBuf = xmalloc(MAXCOMLINESTRG, pszSrcFile, __LINE__);
    850         if (!pszWorkBuf)
     868        pszWorkBuf = xmalloc(MaxComLineStrg, pszSrcFile, __LINE__);
     869        if (!pszWorkBuf) {
     870          xfree(temp.pszCmdLine);
    851871          break; //already complained
    852         memset(&temp, 0, sizeof(ASSOC));
     872        }
    853873        WinQueryDlgItemText(hwnd, ASS_MASK, sizeof(temp.mask), temp.mask);
    854         WinQueryDlgItemText(hwnd, ASS_CL, sizeof(temp.cl), temp.cl);
    855         NormalizeCmdLine(pszWorkBuf, temp.cl);
    856         memcpy(temp.cl, pszWorkBuf, strlen(pszWorkBuf) + 1);
     874        WinQueryDlgItemText(hwnd, ASS_CL, MaxComLineStrg, temp.pszCmdLine);
     875        if (strcmp(temp.pszCmdLine, "<>")) {
     876          NormalizeCmdLine(pszWorkBuf, temp.pszCmdLine);
     877          memcpy(temp.pszCmdLine, pszWorkBuf, strlen(pszWorkBuf) + 1);
     878        }
    857879        xfree(pszWorkBuf);
    858880        WinQueryDlgItemText(hwnd, ASS_SIG, sizeof(temp.sig), temp.sig);
     
    863885        }
    864886        bstrip(temp.mask);
    865         bstrip(temp.cl);
     887        bstrip(temp.pszCmdLine);
    866888        if (WinQueryButtonCheckstate(hwnd, ASS_DEFAULT))
    867889          temp.flags = 0;
     
    882904        if (fCancelAction){
    883905          fCancelAction = FALSE;
     906          xfree(temp.pszCmdLine);
    884907          break;
    885908        }
    886909        else
    887910          info = add_association(&temp);
    888         //Add will fail if mask is not changed
    889911        if (info) {
    890 
    891           CHAR s[1002 + CCHMAXPATH + 6];
    892 
    893           *s = 0;
    894           WinQueryDlgItemText(hwnd, ASS_ENVIRON, 1000, s);
    895           bstripcr(s);
    896           if (*s)
    897             PrfWriteProfileString(fmprof, FM3Str, temp.cl, s);
    898           sprintf(s, "%-12s \x1a %-24s %s%s%s", temp.mask,
    899                   temp.cl, (*temp.sig) ?
    900                   "[" : NullStr, (*temp.sig) ? temp.sig : NullStr,
    901                   (*temp.sig) ? "]" : NullStr);
    902           x = (SHORT) WinSendDlgItemMsg(hwnd,
    903                                         ASS_LISTBOX,
    904                                         LM_INSERTITEM,
    905                                         MPFROM2SHORT(LIT_END, 0), MPFROMP(s));
    906           if (x >= 0) {
    907             WinSendDlgItemMsg(hwnd,
    908                               ASS_LISTBOX,
    909                               LM_SETITEMHANDLE,
    910                               MPFROMSHORT(x), MPFROMP(info));
    911             WinSendDlgItemMsg(hwnd,
    912                               ASS_LISTBOX,
    913                               LM_SELECTITEM,
    914                               MPFROMSHORT(x), MPFROMSHORT(TRUE));
    915           }
     912          display_associations(hwnd, &temp, info);
    916913          save_associations();
    917         }
     914        }
     915        xfree(temp.pszCmdLine);
    918916      }
    919917      {
    920918        ASSOC temp;
    921         CHAR dummy[34];
    922 
     919        CHAR dummy[34];
     920
     921        temp.pszCmdLine = xmallocz(MaxComLineStrg, pszSrcFile, __LINE__);
     922        if (!temp.pszCmdLine)
     923          break; //already complained
    923924        WinSendDlgItemMsg(hwnd,
    924925                          ASS_LISTBOX,
    925926                          LM_SELECTITEM, MPFROMSHORT(y), MPFROMSHORT(TRUE));
    926         memset(&temp, 0, sizeof(ASSOC));
     927        memset(temp.sig, 0, sizeof(temp.sig));
     928        memset(temp.mask, 0, sizeof(temp.mask));
     929        temp.offset = 0;
    927930        WinQueryDlgItemText(hwnd, ASS_MASK, sizeof(temp.mask), temp.mask);
    928931        WinQueryDlgItemText(hwnd, ASS_SIG, sizeof(temp.sig), temp.sig);
     
    946949          }
    947950          save_associations();
    948         }
     951        }
     952        xfree(temp.pszCmdLine);
    949953      }
    950954      break;
     
    968972}
    969973
    970 #pragma alloc_text(ASSOC2,free_commands,load_associations,save_associations)
     974#pragma alloc_text(ASSOC2,free_commands,load_associations,save_associations,display_associations)
    971975#pragma alloc_text(ASSOC2,ExecAssociation,AssocTextProc)
    972976#pragma alloc_text(ASSOC,add_association,kill_association,AssocDlgProc,EditAssociations)
Note: See TracChangeset for help on using the changeset viewer.