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