Annotation of mstools/samples/congui/console.c, revision 1.1

1.1     ! root        1: 
        !             2: /******************************************************************************\
        !             3: *       This is a part of the Microsoft Source Code Samples. 
        !             4: *       Copyright (C) 1993 Microsoft Corporation.
        !             5: *       All rights reserved. 
        !             6: *       This source code is only intended as a supplement to 
        !             7: *       Microsoft Development Tools and/or WinHelp documentation.
        !             8: *       See these sources for detailed information regarding the 
        !             9: *       Microsoft samples programs.
        !            10: \******************************************************************************/
        !            11: 
        !            12: // CONSOLE.C
        !            13: // ================================================================
        !            14: // This module contains the standard console functions for this
        !            15: // applicaiton. All of the 'graphical' functions are contained in
        !            16: // GUI.C
        !            17: 
        !            18: #include <stdio.h>
        !            19: #include <string.h>
        !            20: #include <ctype.h>
        !            21: #include <windows.h>
        !            22: #include "ConGUI.h"
        !            23: 
        !            24: void usage (void);
        !            25: static void DoArgs(int *pargc, char **pargv[]);
        !            26: void ListArgs (int argc, char **argv);
        !            27: 
        !            28: extern int GetDialogArgs (char ***pargv);
        !            29: extern int DoHelp (char *szHelpTopic);
        !            30: 
        !            31: BOOL bDoHelp = FALSE;
        !            32: BOOL bDoDialog = FALSE;
        !            33: BOOL bDoUsage = FALSE;
        !            34: 
        !            35: int main(int argc, char **argv)
        !            36: 
        !            37: {
        !            38:     char szAppName[80];
        !            39:     char **dargv;
        !            40: 
        !            41:     HANDLE hConsoleOutput;
        !            42:     CONSOLE_SCREEN_BUFFER_INFO csbi;
        !            43:     BOOL bLaunched;
        !            44: 
        !            45:     // Lets try a trick to determine if we were 'launched' as a seperate
        !            46:     // screen, or just running from the command line.
        !            47:     // We want to do this so that when we exit, we can prompt the user
        !            48:     // before we shut down if we were 'launched'. Otherwise, any data on
        !            49:     // the output window will be lost.
        !            50:     // We will do this by simply getting the current cursor position. It
        !            51:     // 'should' always be (0,0) on a launch, and something else if we were
        !            52:     // executed as a command from a console window. The only time I can see
        !            53:     // this as not working, is if the user executed a CLS and appended
        !            54:     // our program with the '&' character, as in:
        !            55:     //   C:\> CLS & ConGUI
        !            56:     // This will also result in a (0,0) cursor pos, but in this case, the
        !            57:     // user might also be wanting us to 'prompt' before closeing.
        !            58:     // We also need to handle the case of:
        !            59:     //   C:\> ConGUI > output.dat
        !            60: 
        !            61:     hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        !            62:     GetConsoleScreenBufferInfo(hConsoleOutput, &csbi);
        !            63:     bLaunched = ((csbi.dwCursorPosition.X==0) && (csbi.dwCursorPosition.Y==0));
        !            64:     if ((csbi.dwSize.X<=0) || (csbi.dwSize.Y <= 0)) bLaunched = FALSE;
        !            65: 
        !            66: //    printf ("Window Size: (%i, %i)\n",csbi.dwSize.X, csbi.dwSize.Y);
        !            67: //    printf ("Cursor Pos : (%i, %i)\n",csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y);
        !            68: 
        !            69: 
        !            70: 
        !            71:     if (argc <= 1) { // Nothing on the command line, give brief usage info
        !            72:         usage();
        !            73:         if (bLaunched) {
        !            74:             printf ("------------------------------------------\n");
        !            75:             printf ("Program Finished - Hit <Enter> to continue");
        !            76:             getchar();
        !            77:         }
        !            78:         return(0);
        !            79:     }
        !            80: 
        !            81:     // Get the application name off of the command line.
        !            82:     strcpy (szAppName, argv[0]); // this should always be there.
        !            83:     argc--;
        !            84:     argv++;
        !            85: 
        !            86:     // Get the rest of the arguments
        !            87:     DoArgs (&argc, &argv);
        !            88: 
        !            89: 
        !            90:     // And handle anything they told us to do
        !            91:     if (bDoUsage) {         // Simple Usage Info
        !            92:         usage();
        !            93:     } else if (bDoHelp) {   // Robust Help
        !            94:         DoHelp ("Contents");
        !            95:     } else if (bDoDialog) { // Dialog
        !            96:         dargv = (char **)GlobalAlloc (GPTR, 10*sizeof(char*));
        !            97:         argc = GetDialogArgs(&dargv);
        !            98:         if (argc > 0) {
        !            99:             ListArgs (argc, dargv);
        !           100:             DoArgs (&argc, &dargv);
        !           101:         } else {
        !           102:             printf ("\nUser Selected 'Cancel' in the dialog\n");
        !           103:         }
        !           104:     }
        !           105: 
        !           106:     if (bLaunched) {
        !           107:         printf ("------------------------------------------\n");
        !           108:         printf ("Program Finished - Hit <Enter> to continue");
        !           109:         getchar();
        !           110:     }
        !           111: 
        !           112:     return(0);
        !           113: }
        !           114: 
        !           115: 
        !           116: void usage(void)
        !           117: {
        !           118:     printf ("\nConGUI Usage information:\n");
        !           119:     printf ("-------------------------\n\n");
        !           120:     printf ("    -?       To print usage information\n");
        !           121:     printf ("    -help    To bring up WinHelp for this application\n");
        !           122:     printf ("    -dialog  To bring up a dialog to prompt for parameters\n");
        !           123: 
        !           124: }
        !           125: 
        !           126: void ListArgs (int argc, char **argv)
        !           127: {
        !           128:     printf ("\nCommand Line:\n    ConGUI ");
        !           129:     while (argc--) {
        !           130:         printf ("%s ", *argv);
        !           131:         argv++;
        !           132:     }
        !           133:     printf ("\n");
        !           134: }
        !           135: 
        !           136: 
        !           137: typedef struct tagToken {
        !           138:     int id;
        !           139:     char str[25];
        !           140: } Token;
        !           141: 
        !           142: Token params[] = {
        !           143:     { T_USAGE,   "?"},
        !           144:     { T_HELP,    "Help"},
        !           145:     { T_DIALOG,  "Dialog"},
        !           146:     { 0, 0}
        !           147: 
        !           148: };
        !           149: 
        !           150: 
        !           151: // Check the option entered on the command line against the tokens
        !           152: // we are expecting. The capital letters of the token represent the
        !           153: // minimum amount of the token that needs to be provided. The lower
        !           154: // case letters 'can' be provided, and if provided, must match.
        !           155: BOOL TokensMatch (char *token, char *mask)
        !           156: {
        !           157:     int lToken, lMask, index;
        !           158:     char *pchToken, *pchMask;
        !           159: 
        !           160:     lToken = strlen (token);
        !           161:     lMask = strlen (mask);
        !           162: 
        !           163:     if (lToken > lMask) return FALSE;
        !           164: 
        !           165:     pchToken = &token[0];
        !           166:     pchMask = &mask[0];
        !           167: 
        !           168:     index = 0;
        !           169:     while (*pchToken) {
        !           170:         if (toupper(*pchToken) != toupper(*pchMask)) return FALSE;
        !           171: 
        !           172:         pchToken++;
        !           173:         pchMask++;
        !           174:         index++;
        !           175:     }
        !           176: 
        !           177:     if (index == lMask) return TRUE;
        !           178: 
        !           179:     if (islower(*pchMask)) return TRUE;
        !           180: 
        !           181:     return FALSE;
        !           182: 
        !           183: }
        !           184: 
        !           185: // Return the id of the specified token. Return -1 if no match.
        !           186: int GetTokenID (char *token)
        !           187: {
        !           188:     int id = 0;
        !           189:     int index = 0;
        !           190: 
        !           191:     while (params[index].id) {
        !           192:         if (TokensMatch (token, params[index].str)) {
        !           193:             return params[index].id;
        !           194:         }
        !           195:         ++index;
        !           196:     }
        !           197: 
        !           198:     return -1;
        !           199: }
        !           200: 
        !           201: 
        !           202: // Parse the argument list, looking for valid keywords
        !           203: static void DoArgs(int *pargc, char **pargv[])
        !           204: {
        !           205:     int argc, id;
        !           206:     char **argv;
        !           207:     char *p;
        !           208: 
        !           209:     argc = *pargc;
        !           210:     argv = *pargv;
        !           211: 
        !           212:     p = argv[0];
        !           213: 
        !           214:     while (argc) {
        !           215: 
        !           216:         if (*p == '/' || (*p=='-' && *(p+1)!='\0')) {
        !           217:             id = GetTokenID (&(p[1]));
        !           218:             switch (id) {
        !           219:                 case -1 :
        !           220:                     //printf ("Invalid Token: %s\n", &(p[1]));
        !           221:                     break;
        !           222: 
        !           223:                 case T_USAGE:
        !           224:                     bDoUsage = TRUE;
        !           225:                     break;
        !           226: 
        !           227:                 case T_HELP:
        !           228:                     bDoHelp = TRUE;
        !           229:                     break;
        !           230: 
        !           231:                 case T_DIALOG:
        !           232:                     bDoDialog = TRUE;
        !           233:                     break;
        !           234: 
        !           235:                 default:
        !           236:                     printf ("Found Token: %s\n", &(p[1]));
        !           237:                     break;
        !           238:             }
        !           239:         } else {
        !           240:             printf ("Unexpected keyword : %s\n", p);
        !           241: 
        !           242:         }
        !           243: 
        !           244:         argc--;
        !           245:         argv++;
        !           246:         p = argv[0];
        !           247:     }
        !           248: 
        !           249:     *pargv = argv;
        !           250:     *pargc = argc;
        !           251: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.