| 1 | /***********************************************************************
 | 
|---|
| 2 | 
 | 
|---|
| 3 |   $Id: menu.c 1673 2012-12-30 18:51:01Z gyoung $
 | 
|---|
| 4 | 
 | 
|---|
| 5 |   Custom menu support routines for FM/2
 | 
|---|
| 6 | 
 | 
|---|
| 7 |   Copyright (c) 1996-98 M. Kimes
 | 
|---|
| 8 |   Copyright (c) 2004, 2008 Steven H.Levine
 | 
|---|
| 9 | 
 | 
|---|
| 10 |   01 Aug 04 SHL Rework lstrip/rstrip usage
 | 
|---|
| 11 |   22 Jul 06 SHL Check more run time errors
 | 
|---|
| 12 |   29 Jul 06 SHL Use xfgets_bstripcr
 | 
|---|
| 13 |   20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
 | 
|---|
| 14 |   29 Feb 08 GKY Use xfree where appropriate
 | 
|---|
| 15 | 
 | 
|---|
| 16 | ***********************************************************************/
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <stdlib.h>
 | 
|---|
| 19 | #include <string.h>
 | 
|---|
| 20 | #include <share.h>
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #define INCL_WIN
 | 
|---|
| 23 | #define INCL_LONGLONG                   // dircnrs.h
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "fm3dll.h"
 | 
|---|
| 26 | #include "menu.h"
 | 
|---|
| 27 | #include "errutil.h"                    // Dos_Error...
 | 
|---|
| 28 | #include "delims.h"                     // skip_delim
 | 
|---|
| 29 | #include "wrappers.h"                   // xfgets_bstripcr
 | 
|---|
| 30 | #include "fortify.h"
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #pragma data_seg(DATA2)
 | 
|---|
| 33 | 
 | 
|---|
| 34 | static PSZ pszSrcFile = __FILE__;
 | 
|---|
| 35 | 
 | 
|---|
| 36 | MENU *menuhead = NULL;
 | 
|---|
| 37 | 
 | 
|---|
| 38 | INT tokenize(CHAR * str, INT max, CHAR ** tokens)
 | 
|---|
| 39 | {
 | 
|---|
| 40 |   INT x = 0;
 | 
|---|
| 41 |   CHAR *p;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   if (str && max && tokens) {
 | 
|---|
| 44 |     p = str;
 | 
|---|
| 45 |     for (;;) {
 | 
|---|
| 46 |       p = skip_delim(p, " \t");
 | 
|---|
| 47 |       if (!p)
 | 
|---|
| 48 |         break;
 | 
|---|
| 49 |       tokens[x++] = p;
 | 
|---|
| 50 |       if (x == max)
 | 
|---|
| 51 |         break;
 | 
|---|
| 52 |       p = to_delim(p, " \t");
 | 
|---|
| 53 |       if (!p)
 | 
|---|
| 54 |         break;
 | 
|---|
| 55 |       *p = 0;
 | 
|---|
| 56 |       p++;
 | 
|---|
| 57 |       if (!*p)
 | 
|---|
| 58 |         break;
 | 
|---|
| 59 |     }
 | 
|---|
| 60 |   }
 | 
|---|
| 61 |   return x;
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | VOID FreeMenuList(MENU * head)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   MENU *info, *next;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   info = head;
 | 
|---|
| 69 |   while (info) {
 | 
|---|
| 70 |     next = info->next;
 | 
|---|
| 71 |     xfree(info->text, pszSrcFile, __LINE__);
 | 
|---|
| 72 |     free(info);
 | 
|---|
| 73 |     info = next;
 | 
|---|
| 74 |   }
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | // fixme why do I exist since the only call passes no filename and FM3MENU.DAT doesn't exist. GKY 1-30-09
 | 
|---|
| 78 | BOOL AddToMenu(CHAR * filename, HWND hwndMenu)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   FILE *fp;
 | 
|---|
| 81 |   CHAR s[256];
 | 
|---|
| 82 |   CHAR *tokens[3];
 | 
|---|
| 83 |   INT lines = 0;
 | 
|---|
| 84 |   MENU *info, *last = NULL;
 | 
|---|
| 85 |   BOOL ret = FALSE;
 | 
|---|
| 86 |   CHAR *moder = "r";
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // fixme to complain?
 | 
|---|
| 89 |   if (!hwndMenu) {
 | 
|---|
| 90 |     Runtime_Error(pszSrcFile, __LINE__, NULL);
 | 
|---|
| 91 |     return ret;
 | 
|---|
| 92 |   }
 | 
|---|
| 93 |   if (!filename)
 | 
|---|
| 94 |     filename = "FM3MENU.DAT";
 | 
|---|
| 95 |   fp = xfsopen(filename, moder, SH_DENYWR, pszSrcFile, __LINE__, TRUE);
 | 
|---|
| 96 |   if (fp)  {
 | 
|---|
| 97 |     while (!feof(fp)) {
 | 
|---|
| 98 |       if (!xfgets_bstripcr(s, sizeof(s), fp, pszSrcFile, __LINE__))
 | 
|---|
| 99 |         break;
 | 
|---|
| 100 |       lines++;
 | 
|---|
| 101 |       if (!*s || *s == ';')
 | 
|---|
| 102 |         continue;
 | 
|---|
| 103 |       if (tokenize(s, 3, tokens) == 3 && (USHORT) atoi(tokens[1])) {
 | 
|---|
| 104 |         info = xmallocz(sizeof(MENU), pszSrcFile, __LINE__);
 | 
|---|
| 105 |         if (info) {
 | 
|---|
| 106 |           info->size = sizeof(MENU);
 | 
|---|
| 107 |           info->text = xstrdup(tokens[2], pszSrcFile, __LINE__);
 | 
|---|
| 108 |           if (!info->text)
 | 
|---|
| 109 |             free(info);
 | 
|---|
| 110 |           else {
 | 
|---|
| 111 |             if (!stricmp(tokens[0], "MENUITEM"))
 | 
|---|
| 112 |               info->cmd = atoi(tokens[1]);
 | 
|---|
| 113 |             else if (!stricmp(tokens[0], "SEPARATOR"))
 | 
|---|
| 114 |               info->type = SEPARATOR;
 | 
|---|
| 115 |             else {
 | 
|---|
| 116 |               // error!
 | 
|---|
| 117 |               xfree(info->text, pszSrcFile, __LINE__);
 | 
|---|
| 118 |               free(info);
 | 
|---|
| 119 |               info = NULL;
 | 
|---|
| 120 |             }
 | 
|---|
| 121 |             if (info) {
 | 
|---|
| 122 |               if (!menuhead)
 | 
|---|
| 123 |                 menuhead = info;
 | 
|---|
| 124 |               else
 | 
|---|
| 125 |                 last->next = info;
 | 
|---|
| 126 |               info->next = NULL;
 | 
|---|
| 127 |               last = info;
 | 
|---|
| 128 |             }
 | 
|---|
| 129 |           }
 | 
|---|
| 130 |         }
 | 
|---|
| 131 |       }
 | 
|---|
| 132 |     }
 | 
|---|
| 133 |     fclose(fp);
 | 
|---|
| 134 | 
 | 
|---|
| 135 |     if (menuhead) {
 | 
|---|
| 136 |       MENUITEM mi;
 | 
|---|
| 137 | 
 | 
|---|
| 138 |       memset(&mi, 0, sizeof(mi));
 | 
|---|
| 139 |       info = menuhead;
 | 
|---|
| 140 |       WinEnableWindow(hwndMenu, FALSE);
 | 
|---|
| 141 |       while (info) {
 | 
|---|
| 142 |         mi.iPosition = MIT_END;
 | 
|---|
| 143 |         mi.id = info->cmd;
 | 
|---|
| 144 |         mi.afStyle =
 | 
|---|
| 145 |           (info->type == SEPARATOR) ? MIS_BREAKSEPARATOR : MIS_TEXT;
 | 
|---|
| 146 |         if (WinSendMsg
 | 
|---|
| 147 |             (hwndMenu, MM_INSERTITEM, MPFROMP(&mi), MPFROMP(info->text)))
 | 
|---|
| 148 |           ret = TRUE;
 | 
|---|
| 149 |         info = info->next;
 | 
|---|
| 150 |       }
 | 
|---|
| 151 |       WinEnableWindow(hwndMenu, TRUE);
 | 
|---|
| 152 |       FreeMenuList(menuhead);
 | 
|---|
| 153 |       menuhead = NULL;
 | 
|---|
| 154 |     }
 | 
|---|
| 155 |   }
 | 
|---|
| 156 |   return ret;
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | #pragma alloc_text(MENU,tokenize,FreeMenuList,AddToMenu)
 | 
|---|