Annotation of mstools/samples/sdktools/remote/remote.c, revision 1.1.1.1

1.1       root        1: /*++
                      2: 
                      3: Copyright (c) 1993 Microsoft Corporation
                      4: 
                      5: Module Name:
                      6: 
                      7:     Remote.c
                      8: 
                      9: Abstract:
                     10: 
                     11:     This module contains the main() entry point for Remote.
                     12:     Calls the Server or the Client depending on the first parameter.
                     13: 
                     14: 
                     15: Author:
                     16: 
                     17:     Rajivendra Nath (rajnath) 2-Jan-1993
                     18: 
                     19: Environment:
                     20: 
                     21:     Console App. User mode.
                     22: 
                     23: Revision History:
                     24: 
                     25: --*/
                     26: 
                     27: 
                     28: #include <windows.h>
                     29: #include <stdio.h>
                     30: #include <stdlib.h>
                     31: #include "Remote.h"
                     32: 
                     33: char   HostName[HOSTNAMELEN];
                     34: char*  ChildCmd;
                     35: char*  PipeName;
                     36: char*  ServerName;
                     37: HANDLE MyOutHandle;
                     38: 
                     39: BOOL   IsAdvertise=TRUE;
                     40: DWORD  ClientToServerFlag;
                     41: 
                     42: char* ColorList[]={"black" ,"blue" ,"green" ,"cyan" ,"red" ,"purple" ,"yellow" ,"white",
                     43:                    "lblack","lblue","lgreen","lcyan","lred","lpurple","lyellow","lwhite"};
                     44: 
                     45: WORD
                     46: GetColorNum(
                     47:     char* color
                     48:     );
                     49: 
                     50: VOID
                     51: SetColor(
                     52:     WORD attr
                     53:     );
                     54: 
                     55: BOOL
                     56: GetNextConnectInfo(
                     57:     char** SrvName,
                     58:     char** PipeName
                     59:     );
                     60: 
                     61: 
                     62: 
                     63: CONSOLE_SCREEN_BUFFER_INFO csbiOriginal;
                     64: 
                     65: main(
                     66:     int    argc,
                     67:     char** argv
                     68:     )
                     69: {
                     70:     WORD  RunType;              // Server or Client end of Remote
                     71:     DWORD len=HOSTNAMELEN-1;
                     72:     int   i;
                     73: 
                     74:     char  sTitle[100];          // New Title
                     75:     char  orgTitle[100];        // Old Title
                     76:     BOOL  bSetAttrib=FALSE;     // Change Console Attributes
                     77:     BOOL  bPromptForArgs=FALSE; // Is /P option
                     78:     WORD  wAttrib;              // Console Attributes
                     79: 
                     80:     GetComputerName((LPTSTR)HostName,&len);
                     81: 
                     82:     MyOutHandle=GetStdHandle(STD_OUTPUT_HANDLE);
                     83: 
                     84:     GetConsoleScreenBufferInfo(MyOutHandle,&csbiOriginal);
                     85: 
                     86: 
                     87: 
                     88: 
                     89: 
                     90:     //
                     91:     // Parameter Processing
                     92:     //
                     93:     // For Server:
                     94:     // Remote /S <Executable>  <PipeName> [Optional Params]
                     95:     //
                     96:     // For Client:
                     97:     // Remote /C <Server Name> <PipeName> [Optional Params]
                     98:     // or
                     99:     // Remote /P
                    100:     // This will loop continously prompting for different
                    101:     // Servers and Pipename
                    102: 
                    103: 
                    104:     if ((argc<2)||((argv[1][0]!='/')&&(argv[1][0]!='-')))
                    105:     {
                    106: 
                    107:         DisplayServerHlp();
                    108:         DisplayClientHlp();
                    109:         return(1);
                    110:     }
                    111: 
                    112:     switch(argv[1][1])
                    113:     {
                    114: 
                    115:     case 'c':
                    116:     case 'C':
                    117: 
                    118:         //
                    119:         // Is Client End of Remote
                    120:         //
                    121: 
                    122:         if ((argc<4)||((argv[1][0]!='/')&&(argv[1][0]!='-')))
                    123:         {
                    124: 
                    125:             DisplayServerHlp();
                    126:             DisplayClientHlp();
                    127:             return(1);
                    128:         }
                    129: 
                    130:         ServerName=argv[2];
                    131:         PipeName=argv[3];
                    132: 
                    133:         RunType=REMOTE_CLIENT;
                    134:         break;
                    135: 
                    136: 
                    137:     case 'p':
                    138:     case 'P':
                    139: 
                    140:         //
                    141:         // Is Client End of Remote
                    142:         //
                    143: 
                    144:         bPromptForArgs=TRUE;
                    145:         RunType=REMOTE_CLIENT;
                    146:         break;
                    147: 
                    148: 
                    149:     case 's':
                    150:     case 'S':
                    151:         //
                    152:         // Is Server End of Remote
                    153:         //
                    154:         if ((argc<4)||((argv[1][0]!='/')&&(argv[1][0]!='-')))
                    155:         {
                    156: 
                    157:             DisplayServerHlp();
                    158:             DisplayClientHlp();
                    159:             return(1);
                    160:         }
                    161: 
                    162:         ChildCmd=argv[2];
                    163:         PipeName=argv[3];
                    164: 
                    165:         RunType=REMOTE_SERVER;
                    166:         break;
                    167: 
                    168: 
                    169:     default:
                    170:         DisplayServerHlp();
                    171:         DisplayClientHlp();
                    172:         return(1);
                    173:     }
                    174: 
                    175:     //
                    176:     // Save Existing Values
                    177:     //
                    178: 
                    179:     //
                    180:     //Colors /f   <ForeGround> /b <BackGround>
                    181:     //
                    182: 
                    183:     wAttrib=csbiOriginal.wAttributes;
                    184: 
                    185:     //
                    186:     //Title  /T Title
                    187:     //
                    188: 
                    189:     GetConsoleTitle(orgTitle,sizeof(orgTitle));
                    190: 
                    191:     if (RunType==REMOTE_SERVER)
                    192:     {
                    193:        //
                    194:        // Base Name of Executable
                    195:        // For setting the title
                    196:        //
                    197: 
                    198:         char *tcmd=ChildCmd;
                    199: 
                    200:         while ((*tcmd!=' ')    &&(*tcmd!=0))   tcmd++;
                    201:         while ((tcmd!=ChildCmd)&&(*tcmd!='\\'))tcmd--;
                    202: 
                    203:         sprintf(sTitle,"%8.8s [Remote /C %s %s]",tcmd,HostName,PipeName);
                    204:     }
                    205: 
                    206:     //
                    207:     //Process Common (Optional) Parameters
                    208:     //
                    209: 
                    210:     for (i=4;i<argc;i++)
                    211:     {
                    212: 
                    213:         if ((argv[i][0]!='/')&&(argv[i][0]!='-'))
                    214:         {
                    215:             WRITEF((VBuff,"Invalid parameter %s:Ignoring\n",argv[i]));
                    216:             continue;
                    217:         }
                    218: 
                    219:         switch(argv[i][1])
                    220:         {
                    221:         case 'l':    // Only Valid for client End
                    222:         case 'L':    // Max Number of Lines to recieve from Server
                    223:             i++;
                    224:             if (i>=argc)
                    225:             {
                    226:                 WRITEF((VBuff,"Incomplete Param %s..Ignoring\n",argv[i-1]));
                    227:                 break;
                    228:             }
                    229:             LinesToSend=(DWORD)atoi(argv[i])+1;
                    230:             break;
                    231: 
                    232:         case 't':    // Title to be set instead of the default
                    233:         case 'T':
                    234:             i++;
                    235:             if (i>=argc)
                    236:             {
                    237:                 WRITEF((VBuff,"Incomplete Param %s..Ignoring\n",argv[i-1]));
                    238:                 break;
                    239:             }
                    240:             sprintf(sTitle,"%s",argv[i]);
                    241:             break;
                    242: 
                    243:         case 'b':    // Background color
                    244:         case 'B':
                    245:             i++;
                    246:             if (i>=argc)
                    247:             {
                    248:                 WRITEF((VBuff,"Incomplete Param %s..Ignoring\n",argv[i-1]));
                    249:                 break;
                    250:             }
                    251:             {
                    252:                 WORD col=GetColorNum(argv[i]);
                    253:                 if (col!=0xffff)
                    254:                 {
                    255:                     bSetAttrib=TRUE;
                    256:                     wAttrib=col<<4|(wAttrib&0x000f);
                    257:                 }
                    258:                 break;
                    259:             }
                    260: 
                    261:         case 'f':    // Foreground color
                    262:         case 'F':
                    263:             i++;
                    264:             if (i>=argc)
                    265:             {
                    266:                 WRITEF((VBuff,"Incomplete Param %s..Ignoring\n",argv[i-1]));
                    267:                 break;
                    268:             }
                    269:             {
                    270:                 WORD col=GetColorNum(argv[i]);
                    271:                 if (col!=0xffff)
                    272:                 {
                    273:                     bSetAttrib=TRUE;
                    274:                     wAttrib=col|(wAttrib&0x00f0);
                    275:                 }
                    276:                 break;
                    277:             }
                    278: 
                    279:         case 'q':
                    280:         case 'Q':
                    281:             IsAdvertise=FALSE;
                    282:             ClientToServerFlag|=0x80000000;
                    283:             break;
                    284: 
                    285:         default:
                    286:             WRITEF((VBuff,"Unknown Parameter=%s %s\n",argv[i-1],argv[i]));
                    287:             break;
                    288: 
                    289:         }
                    290: 
                    291:     }
                    292: 
                    293:     //
                    294:     //Now Set various Parameters
                    295:     //
                    296: 
                    297:     //
                    298:     //Colors
                    299:     //
                    300: 
                    301:     SetColor(wAttrib);
                    302: 
                    303:     if (RunType==REMOTE_CLIENT)
                    304:     {
                    305:         BOOL done=FALSE;
                    306: 
                    307:         //
                    308:         // Set Client end defaults and start client
                    309:         //
                    310: 
                    311: 
                    312: 
                    313:         while(!done)
                    314:         {
                    315:             if (!bPromptForArgs ||
                    316:                 GetNextConnectInfo(&ServerName,&PipeName)
                    317:                )
                    318:             {
                    319:                 sprintf(sTitle,"Remote /C %s %s",ServerName,PipeName);
                    320:                 SetConsoleTitle(sTitle);
                    321: 
                    322:                 //
                    323:                 // Start Client (Client.C)
                    324:                 //
                    325:                 Client(ServerName,PipeName);
                    326:             }
                    327:             done=!bPromptForArgs;
                    328:         }
                    329:     }
                    330: 
                    331:     if (RunType==REMOTE_SERVER)
                    332:     {
                    333:                SetConsoleTitle(sTitle);
                    334: 
                    335:         //
                    336:         // Start Server (Server.C)
                    337:         //
                    338:         Server(ChildCmd,PipeName);
                    339:     }
                    340: 
                    341:     //
                    342:     //Reset Colors
                    343:     //
                    344:     SetColor(csbiOriginal.wAttributes);
                    345:     SetConsoleTitle(orgTitle);
                    346: 
                    347:     ExitProcess(0);
                    348: }
                    349: /*************************************************************/
                    350: VOID
                    351: ErrorExit(
                    352:     char* str
                    353:     )
                    354: {
                    355:     WRITEF((VBuff,"Error-%d:%s\n",GetLastError(),str));
                    356:     ExitProcess(1);
                    357: }
                    358: /*************************************************************/
                    359: DWORD
                    360: ReadFixBytes(
                    361:     HANDLE hRead,
                    362:     char*  Buffer,
                    363:     DWORD  ToRead,
                    364:     DWORD  TimeOut   //ignore for timebeing
                    365:     )
                    366: {
                    367:     DWORD xyzBytesRead=0;
                    368:     DWORD xyzBytesToRead=ToRead;
                    369:     char* xyzbuff=Buffer;
                    370: 
                    371:     while(xyzBytesToRead!=0)
                    372:     {
                    373:         if (!ReadFile(hRead,xyzbuff,xyzBytesToRead,&xyzBytesRead,NULL))
                    374:         {
                    375:             return(xyzBytesToRead);
                    376:         }
                    377: 
                    378:         xyzBytesToRead-=xyzBytesRead;
                    379:         xyzbuff+=xyzBytesRead;
                    380:     }
                    381:     return(0);
                    382: 
                    383: }
                    384: /*************************************************************/
                    385: VOID
                    386: DisplayClientHlp()
                    387: {
                    388:     WRITEF((VBuff,"\n   To Start the CLIENT end of REMOTE\n"));
                    389:     WRITEF((VBuff,"   ---------------------------------\n"));
                    390:     WRITEF((VBuff,"   Syntax : REMOTE /C <ServerName> <Unique Id> [Param]\n"));
                    391:     WRITEF((VBuff,"   Example: REMOTE /C rajnathX86   imbroglio\n"));
                    392:     WRITEF((VBuff,"            This would connect to a server session on \n"));
                    393:     WRITEF((VBuff,"            rajnathX86 with id \"imbroglio\" if there was a\n"));
                    394:     WRITEF((VBuff,"            REMOTE /S <\"Cmd\"> imbroglio\n"));
                    395:     WRITEF((VBuff,"            started on the machine rajnathX86.\n\n"));
                    396:     WRITEF((VBuff,"   To Exit: %cQ (Leaves the Remote Server Running)\n",COMMANDCHAR));
                    397:     WRITEF((VBuff,"   [Param]: /L <# of Lines to Get>\n"));
                    398:     WRITEF((VBuff,"   [Param]: /F <Foreground color eg blue, lred..>\n"));
                    399:     WRITEF((VBuff,"   [Param]: /B <Background color eg cyan, lwhite..>\n"));
                    400:     WRITEF((VBuff,"\n"));
                    401: }
                    402: /*************************************************************/
                    403: 
                    404: VOID
                    405: DisplayServerHlp()
                    406: {
                    407:     WRITEF((VBuff,"\n   To Start the SERVER end of REMOTE\n"));
                    408:     WRITEF((VBuff,"   ---------------------------------\n"));
                    409:     WRITEF((VBuff,"   Syntax : REMOTE /S <\"Cmd\">     <Unique Id> [Param]\n"));
                    410:     WRITEF((VBuff,"   Example: REMOTE /S \"i386kd -v\" imbroglio\n"));
                    411:     WRITEF((VBuff,"            To interact with this \"Cmd\" \n"));
                    412:     WRITEF((VBuff,"            from some other machine\n"));
                    413:     WRITEF((VBuff,"            - start the client end by:\n"));
                    414:     WRITEF((VBuff,"            REMOTE /C %s imbroglio\n\n",HostName));
                    415:     WRITEF((VBuff,"   To Exit: %cK \n",COMMANDCHAR));
                    416:     WRITEF((VBuff,"   [Param]: /F <Foreground color eg yellow, black..>\n"));
                    417:     WRITEF((VBuff,"   [Param]: /B <Background color eg lblue, white..>\n"));
                    418:     WRITEF((VBuff,"\n"));
                    419: 
                    420: }
                    421: 
                    422: WORD
                    423: GetColorNum(
                    424:     char *color
                    425:     )
                    426: {
                    427:     int i;
                    428: 
                    429:     strlwr(color);
                    430:     for (i=0;i<16;i++)
                    431:     {
                    432:         if (strcmp(ColorList[i],color)==0)
                    433:         {
                    434:             return(i);
                    435:         }
                    436:     }
                    437:     return ((WORD)atoi(color));
                    438: }
                    439: 
                    440: VOID
                    441: SetColor(
                    442:     WORD attr
                    443:     )
                    444: {
                    445:        COORD  origin={0,0};
                    446:     DWORD  dwrite;
                    447:     FillConsoleOutputAttribute
                    448:     (
                    449:        MyOutHandle,attr,csbiOriginal.dwSize.
                    450:        X*csbiOriginal.dwSize.Y,origin,&dwrite
                    451:     );
                    452:     SetConsoleTextAttribute(MyOutHandle,attr);
                    453: }
                    454: 
                    455: BOOL
                    456: GetNextConnectInfo(
                    457:     char** SrvName,
                    458:     char** PipeName
                    459:     )
                    460: {
                    461:     static char szServerName[32];
                    462:     static char szPipeName[32];
                    463: 
                    464:     try
                    465:     {
                    466:         ZeroMemory(szServerName,32);
                    467:         ZeroMemory(szPipeName,32);
                    468:         SetConsoleTitle("Remote - Prompting for next Connection");
                    469:         WRITEF((VBuff,"Debugger machine (server): "));
                    470:         fflush(stdout);
                    471: 
                    472:         if (((*SrvName=gets(szServerName))==NULL)||
                    473:              (strlen(szServerName)==0))
                    474:         {
                    475:             return(FALSE);
                    476:         }
                    477: 
                    478:         if (szServerName[0] == COMMANDCHAR &&
                    479:             (szServerName[1] == 'q' || szServerName[1] == 'Q')
                    480:            )
                    481:         {
                    482:             return(FALSE);
                    483:         }
                    484: 
                    485:         WRITEF((VBuff,"Debuggee machine : "));
                    486:         fflush(stdout);
                    487: 
                    488:         if ((*PipeName=gets(szPipeName))==NULL)
                    489:         {
                    490:             return(FALSE);
                    491:         }
                    492:         if (szPipeName[0] == COMMANDCHAR &&
                    493:             (szPipeName[1] == 'q' || szPipeName[1] == 'Q')
                    494:            )
                    495:         {
                    496:             return(FALSE);
                    497:         }
                    498:         WRITEF((VBuff,"\n\n"));
                    499:     }
                    500: 
                    501:     except(EXCEPTION_EXECUTE_HANDLER)
                    502:     {
                    503:         return(FALSE);  // Ignore exceptions
                    504:     }
                    505:     return(TRUE);
                    506: }
                    507: 
                    508: 
                    509: /*************************************************************/
                    510: VOID
                    511: Errormsg(
                    512:     char* str
                    513:     )
                    514: {
                    515:     WRITEF((VBuff,"Error (%d) - %s\n",GetLastError(),str));
                    516: }
                    517: 
                    518: /*************************************************************/

unix.superglobalmegacorp.com

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