Listing 2 (rmenu4.c)

  1: /*************************************************************
  2:  * Program: RMENU Menu Interpreter
  3:  *  Module: rmenu4.c
  4:  *    System-dependent Utility functions
  5:  * Written by: Leor Zolman, 7/91
  6:  *************************************************************/
  7:
  8: #include "cmenu.h"
  9: #include "rcmenu.h"
 10:
 11: #if __STDC
 12: #pragma hdrstop
 13: #endif
 14:
 15: #if DOS
 16: #include <dir.h>
 17:
 18:                        /* To set an explicit Near Heap size,   */
 19: /* #define HEAPSIZ 20000   /* define HEAPSIZ in the MAKEFILE  */
 20:                        /* or un-comment this definition       */
 21:
 22: static struct {
 23:    int curdrive;
 24:    char curpath[MAX_PATH];
 25: } path_stack[MAX_PATH_STACK];
 26:
 27: static int path_stackp = 0;
 28:
 29: #ifdef HEAPSIZ
 30: extern unsigned _heaplen = HEAPSIZ;
 31: #endif
 32: #endif
 33:
 34:
 35: /***********************************************************
 36:  *                   CURSES FUNCTIONS                      *
 37:  ***********************************************************/
 38:
 39: /***********************************************************
 40:  * init_win():
 41:  * Initialize Curses system.
 42:  ***********************************************************/
 43:
 44: Void init_win()
 45: {
 46:                      /* initialize screen, and check for failure*/
 47:    if (initscr() == INIT_FAIL)
 48:    {
 49:       fprintf(stderr, "initscr failed!\n");
 50:       exit(1);
 51:    }
 52:
 53:    tty_curses();                    /* set tty modes for curses */
 54:    hlight_end();                    /* set colors under DOS     */
 55:
 56: #if DOS
 57:    strcpy(SysShell, getenv("COMSPEC"));
 58: #else
 59:    strcpy(SysShell, SHELL_ESC);
 60: #endif
 61:
 62:    return;
 63: }
 64:
 65:
 66: /******************************************************
 67:  * close_win():
 68:  * Terminate Curses system
 69:  ******************************************************/
 70:
 71: Void close_win()
 72: {
 73: #if DOS
 74:    attrset(A_COLOR(DOS_FCOLOR,DOS_BCOLOR));
 75: #endif
 76:    clear();
 77:    noraw();
 78:    refresh();                /* This is so cursor shows back up */
 79:    endwin();
 80: }
 81:
 82:
 83: /************************************************************
 84:  * tty_shell()
 85:  * Set tty mode for a shell call
 86:  ************************************************************/
 87:
 88: Void tty_shell()
 89: {
 90: #if XENIX || (UNIX && OLD_CURSES)
 91:       resetty();
 92: #else
 93:       attrset(A_COLOR(DOS_FCOLOR,DOS_BCOLOR));
 94:       clear();
 95:       refresh();
 96:       endwin();
 97: #endif
 98:    return;
 99: }
100:
101:
102: /************************************************************
103:  * tty_curses()
104:  * Set tty mode for curses
105:  ************************************************************/
106:
107: Void tty_curses()
108: {                                       /* set up tty modes    */
109:    raw();                               /* ignore ^C, etc.     */
110:    nonl();
111:    noecho();                            /* do not echo input   */
112:    keypad(stdscr, TRUE);                      /* enable keypad */
113: #if DOS
114:    attrset(A_COLOR(M_FCOLOR, M_BCOLOR));
115: #endif
116:    return;
117: }
118:
119:
120: /***********************************************************
121:  * hlight_on()
122:  * Set to standout mode (Reverse colors for DOS)
123:  ***********************************************************/
124:
125: Void hlight_on()
126: {
127: #if DOS
128:    attrset(A_COLOR(MREV_FCOLOR, MREV_BCOLOR));
129: #else
130:    standout();
131: #endif
132: }
133:
134:
135: /***********************************************************
136:  * hlight_end()
137:  * Turn off standout mode (Normal colors for DOS)
138:  ***********************************************************/
139:
140: Void hlight_end()
141: {
142: #if DOS
143:    attrset(A_COLOR(M_FCOLOR, M_BCOLOR));
144: #else
145:    standend();
146: #endif
147: }
148:
149:
150: /*************************************************************
151:  *             PATH MANAGEMENT FUNCTIONS                     *
152:  *************************************************************/
153:
154: /*************************************************************
155:  * push_path()
156:  * DOS only: save drive and path for later restoration, and
157:  *    select new drive based on new path
158:  *  UNIX: a no-op, since sub-shells cannot affect parent path
159:  *************************************************************/
160:
161: Void push_path()
162: {
163: #if DOS
164:    if (path_stackp == MAX_PATH_STACK)
165:       fatal("Maximum stack nesting reached");
166:    else
167:    {
168:       path_stack[path_stackp].curdrive = getdisk();
169:       getcwd(path_stack[path_stackp].curpath, MAX_PATH);
170:       path_stackp++;
171:    }
172: #endif
173:    return;
174: }
175:
176:
177: /**************************************************************
178:  * pop_path()
179:  * DOS only: restore drive and path from path stack
180:  *  UNIX: a no-op, since sub-shells cannot affect parent path
181:  **************************************************************/
182:
183: Void pop_path()
184: {
185: #if DOS
186:       path_stackp--;
187:       setdisk(path_stack[path_stackp].curdrive);
188:       chdir(path_stack[path_stackp].curpath);
189: #endif
190:    return;
191: }
192:
193:
194: /*************************************************************
195:  * trans_slash():
196:  * Translate forward slashes (/) into backslashes (\) in
197:  * DOS pathnames
198:  *************************************************************/
199:
200: static char *trans_slash(str)
201: char *str;
202: {
203:    char *cptr;
204:
205: #if DOS
206:    for (cptr = str; *cptr; cptr++)
207:       if (*cptr == '/')
208:          *cptr = '\\';
209: #endif
210:
211:    return str;
212: }
213:
214:
215: /*************************************************************
216:  * make_path():
217:  * Construct new path based on old default path, old_path,
218:  * and current incremental path, incr_path (which might be
219:  * absolute)
220:  *************************************************************/
221:
222: char *make_path(old_path, incr_path)
223: char *old_path, *incr_path;
224: {
225:    static char newpath[MAX_PATH];
226:
227:    trans_slash(old_path);
228:    trans_slash(incr_path);
229:
230:    if (*incr_path == '/' || *incr_path == '\\')/* If path abs-  */
231:       strcpy(newpath, incr_path);    /* olute, then it's full    */
232: #if DOS
233:    else if (incr path[1] == ':')     /* drive designator? if so, */
234:       strcpy(newpath, incr_path);    /* treat as ABSOLUTE path   */
235: #endif
236:    else
237:    {
238:       strcpy(newpath, old_path);     /* start with old path      */
239:       if (*incr_path && *newpath)    /* add path delimiter       */
240: #if DOS                              /*    (if necessary)        */
241:          strcat(newpath, "\\");
242: #else
243:          strcat(newpath, "/");
244: #endif
245:
246:       strcat(newpath, incr_path);    /* and append new path      */
247:    }
248:    return newpath;
249: }
250:
251:
252: /*************************************************************
253:  * make_cmd():
254:  * Construct a system command string based on a given path
255:  * and action string. If the path is non-null, generate a
256:  * leading "cd" command to change the current directory.
257:  * If running under DOS, then generate a drive selection
258:  * statement as well to change the current drive.
259:  *************************************************************/
260:
261: char *make_cmd(path, action)
262: char *path, *action;
263: {
264:    static char cmd_line[MAX_CMD];
265:    char strtemp[10];
266:
267:    trans_slash(path);
268:
269:    if (*path)                                /* select new path */
270:    {
271:       sprintf(cmd_line, "cd %s; ", path);
272: #if DOS
273:       if (path[1] == ':')                   /* select new drive */
274:       {
275:          sprintf(strtemp, "%c:; ", path[0]);
276:          strcat(cmd_line, strtemp);
277:       }
278: #endif
279:       strcat(cmd_line, action);                   /* action text */
280:    }
281:    else                             /* no new path: trivial case */
282:       sprintf(cmd_line, "%s", action);
283:
284:    return cmd_line;
285: }
286:
287:
288: /**************************************************************
289:  * system0(): Execute a command via a system() call.
290:  *
291:  * If UNIX, just pass the command string to a shell.
292:  *
293:  * if DOS, pass each sub-command (delimited by ;) to
294:  *       a command processor
295:  **************************************************************/
296:
297: #if UNIX || XENIX                     /* Unix version trivial */
298:
299: int system0(cmd)
300: char *cmd;
301: {
302:    return system(cmd);
303: }
304:
305: #endif                                /* end of Unix version  */
306:
307: #if DOS
308: int system0(cmd)
309: char *cmd;
310: {
311:    char subcmd[MAX_CMD];
312:    char savcmd[MAX_CMD], *cmdp;
313:    int i, retval;
314:    char *cp =  savcmd;
315:    char c, lastc;
316:
317:    strcpy(savcmd, cmd);
318:    while (*cp)                     /* if more subcommands left */
319:    {
320:       if (*cp == ';')
321:          cp++;
322:       cmdp = cp;                   /* ptr to start of command  */
323:       while (*cp && *cp !: ';')
324:          cp++;                     /* find end of subcommand   */
325:       lastc = *cp;                 /* save terminating char    */
326:       *cp = '\0';                  /* delimit subcommand       */
327:       retval = system(cmdp);          /* pass it to system()   */
328:       *cp = lastc;                 /* restore last terminator  */
329:    }
330:    return retval;
331: }
332: #endif                             /* end of DOS version       */
/* End of File */