home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_06 / 2n06063a < prev    next >
Text File  |  1991-05-01  |  3KB  |  137 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "StdWin.h"
  7.  
  8. static char CommandBuffer[120];
  9. #define MAXARGS 50
  10.  
  11. /* buffer for command line arg expansion */
  12. static char Buffer[MAXARGS][25];
  13.  
  14. /* CommandLine displays a one line edit control
  15.    dialog box and returns a facsimile of the command line */
  16.  
  17. int CommandLine(argv)
  18. char **argv;
  19. {
  20.  
  21.   FARPROC lpCommandLineDlg,lpCommandLineParseDlg;
  22.   BYTE *ptr;
  23.   HWND hListBox;
  24.   int matches,i,State = 0;
  25.   /* CommandBuffer needs two null terminators */
  26.   memset(CommandBuffer,0,sizeof(CommandBuffer));
  27.   /* call Windows to display the dialog */
  28.   lpCommandLineDlg = MakeProcInstance(CommandLineDlg,hInst);
  29.   i=DialogBox(hInst,"COMMAND",GetFocus(),lpCommandLineDlg);
  30.   FreeProcInstance(lpCommandLineDlg);
  31.   /* process the command line */
  32.   if(i == ID_CANCEL) return(0);
  33.   i = 1;
  34.  
  35.   /* set up argument pointers */
  36.   argv[0] = WindowTitle;
  37.   ptr = CommandBuffer;
  38.   Redirected = 0;
  39.   /* break the command buffer into strings */
  40.   while(*ptr)
  41.     {
  42.     /* find beginning */
  43.     while(*ptr && isspace(*ptr)) ptr++;
  44.     if(!*ptr) break;
  45.     argv[i] = ptr;
  46.     /* find end of arg */
  47.     while(*ptr && !isspace(*ptr)) ptr++;
  48.     *ptr = 0;
  49.     /* see if there are wild cards in this argument */
  50.     if((strchr(argv[i],'*') || strchr(argv[i],'?'))&&!State)
  51.         {
  52.         /* create a listbox window, don't show the window */
  53.         hListBox = CreateWindow("listbox",NULL,WS_CHILD,
  54.                 CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
  55.                 CW_USEDEFAULT,GetFocus(),1,hInst,NULL);
  56.         if(hListBox){
  57.             ShowWindow(hListBox,SW_HIDE);
  58.             /* ask the listbox to expand the wild card */
  59.             matches = SendMessage(hListBox,LB_DIR,0,
  60.                 (DWORD)((char far *)argv[i])) + 1;
  61.             /* get the matches */
  62.             matches = min(matches,MAXARGS-i);
  63.             while(matches){
  64.                 SendMessage(hListBox,LB_GETTEXT,--matches,
  65.                     (DWORD)((char far *)&Buffer[i-1][0]));
  66.                 argv[i] = Buffer[i-1];
  67.                 i++;
  68.                 }
  69.             i--;
  70.             }
  71.         DestroyWindow(hListBox);
  72.         }
  73.     if(State)       /* This argument is a filename */
  74.         {
  75.         hOutFile = OpenFile(argv[i],&OFStruct,OF_CREATE);
  76.         if(hOutFile == -1) hOutFile = 0;
  77.         if(State == 1 && hOutFile)   /* if redirected */
  78.             /* don't print to screen */
  79.             Redirected = 1;
  80.         State = 0;
  81.         /* don't pass these args to main */
  82.         i -= 2;
  83.         argv[i+1] = argv[i+2] = NULL;
  84.         }
  85.  
  86.     /* see if next argument is redirection or pipe */
  87.     if(!strcmp(argv[i],">"))
  88.         State = 1;
  89.     else if(!strcmp(argv[i],"|"))
  90.         State = 2;
  91.     else State = 0;
  92.  
  93.     ptr++;
  94.     i++;
  95.     }
  96.   return(i);
  97. }
  98.  
  99. /**********************************************************
  100.  
  101.     FUNCTION: CommandLineDlg(HWND, unsigned, WORD, LONG)
  102.  
  103. **********************************************************/
  104.  
  105. BOOL FAR PASCAL CommandLineDlg(hDlg,message,wParam,lParam)
  106. HWND hDlg;
  107. unsigned message;
  108. WORD wParam;
  109. LONG lParam;
  110. {
  111.  
  112.  
  113.     switch (message) {
  114.         case WM_INITDIALOG:
  115.             SetDlgItemText(hDlg,ID_TITLE,WindowTitle);
  116.             SetFocus(GetDlgItem(hDlg,ID_EDIT));
  117.             break;
  118.  
  119.         case WM_COMMAND:
  120.             switch (wParam) {
  121.                 case ID_OK:
  122.                     GetDlgItemText(hDlg,ID_EDIT,
  123.                         CommandBuffer,
  124.                         sizeof(CommandBuffer));
  125.  
  126.                 case ID_CANCEL:
  127.                     EndDialog(hDlg, wParam);
  128.                     break;
  129.  
  130.                 }
  131.         break;
  132.         }
  133.     return (FALSE);
  134. }
  135.  
  136.  
  137.