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

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: 
1.1.1.2 ! root      279:         case 'u':
        !           280:         case 'U':
        !           281:             i++;
        !           282:             if (i>=argc)
        !           283:             {
        !           284:                 WRITEF((VBuff,"Incomplete Param %s..Aborting\n",argv[i-1]));
        !           285:                 return 1;
        !           286:             }
        !           287:             UserName=argv[i];
        !           288:             break;
        !           289: 
        !           290: 
1.1       root      291:         case 'q':
                    292:         case 'Q':
                    293:             IsAdvertise=FALSE;
                    294:             ClientToServerFlag|=0x80000000;
                    295:             break;
                    296: 
                    297:         default:
                    298:             WRITEF((VBuff,"Unknown Parameter=%s %s\n",argv[i-1],argv[i]));
                    299:             break;
                    300: 
                    301:         }
                    302: 
                    303:     }
                    304: 
                    305:     //
                    306:     //Now Set various Parameters
                    307:     //
                    308: 
                    309:     //
                    310:     //Colors
                    311:     //
                    312: 
                    313:     SetColor(wAttrib);
                    314: 
                    315:     if (RunType==REMOTE_CLIENT)
                    316:     {
                    317:         BOOL done=FALSE;
                    318: 
                    319:         //
                    320:         // Set Client end defaults and start client
                    321:         //
                    322: 
                    323: 
                    324: 
                    325:         while(!done)
                    326:         {
                    327:             if (!bPromptForArgs ||
                    328:                 GetNextConnectInfo(&ServerName,&PipeName)
                    329:                )
                    330:             {
                    331:                 sprintf(sTitle,"Remote /C %s %s",ServerName,PipeName);
                    332:                 SetConsoleTitle(sTitle);
                    333: 
                    334:                 //
                    335:                 // Start Client (Client.C)
                    336:                 //
                    337:                 Client(ServerName,PipeName);
                    338:             }
                    339:             done=!bPromptForArgs;
                    340:         }
                    341:     }
                    342: 
                    343:     if (RunType==REMOTE_SERVER)
                    344:     {
                    345:                SetConsoleTitle(sTitle);
                    346: 
                    347:         //
                    348:         // Start Server (Server.C)
                    349:         //
                    350:         Server(ChildCmd,PipeName);
                    351:     }
                    352: 
                    353:     //
                    354:     //Reset Colors
                    355:     //
                    356:     SetColor(csbiOriginal.wAttributes);
                    357:     SetConsoleTitle(orgTitle);
                    358: 
                    359:     ExitProcess(0);
                    360: }
                    361: /*************************************************************/
                    362: VOID
                    363: ErrorExit(
                    364:     char* str
                    365:     )
                    366: {
                    367:     WRITEF((VBuff,"Error-%d:%s\n",GetLastError(),str));
                    368:     ExitProcess(1);
                    369: }
                    370: /*************************************************************/
                    371: DWORD
                    372: ReadFixBytes(
                    373:     HANDLE hRead,
                    374:     char*  Buffer,
                    375:     DWORD  ToRead,
                    376:     DWORD  TimeOut   //ignore for timebeing
                    377:     )
                    378: {
                    379:     DWORD xyzBytesRead=0;
                    380:     DWORD xyzBytesToRead=ToRead;
                    381:     char* xyzbuff=Buffer;
                    382: 
                    383:     while(xyzBytesToRead!=0)
                    384:     {
                    385:         if (!ReadFile(hRead,xyzbuff,xyzBytesToRead,&xyzBytesRead,NULL))
                    386:         {
                    387:             return(xyzBytesToRead);
                    388:         }
                    389: 
                    390:         xyzBytesToRead-=xyzBytesRead;
                    391:         xyzbuff+=xyzBytesRead;
                    392:     }
                    393:     return(0);
                    394: 
                    395: }
                    396: /*************************************************************/
                    397: VOID
                    398: DisplayClientHlp()
                    399: {
                    400:     WRITEF((VBuff,"\n   To Start the CLIENT end of REMOTE\n"));
                    401:     WRITEF((VBuff,"   ---------------------------------\n"));
1.1.1.2 ! root      402:     WRITEF((VBuff,"   Syntax : REMOTE /C <ComputerName> <Unique Id> [/L] [/F] [/B]\n"));
        !           403:     WRITEF((VBuff,"\n"));
        !           404: 
        !           405:     WRITEF((VBuff,"   <ComputerName> The name of computer on which server-end of remote\n"));
        !           406:     WRITEF((VBuff,"                  was run on.\n"));
        !           407: 
        !           408:     WRITEF((VBuff,"   <Unique Id>    The Unique Id of the remote server running on \n"));
        !           409:     WRITEF((VBuff,"                  the computer <ComputerName>.\n"));
        !           410: 
        !           411:     WRITEF((VBuff,"   [/L]           Number of Lines to Get.\n"));
        !           412:     WRITEF((VBuff,"   [/F]           Foreground color eg blue, red, etc.\n"));
        !           413:     WRITEF((VBuff,"   [/B]           Background color eg blue, lwhite,etc.\n"));
        !           414:     WRITEF((VBuff,"\n"));
        !           415:     WRITEF((VBuff,"   Example: REMOTE /C rajnathX86 imbroglio\n"));
        !           416:     WRITEF((VBuff,"\n"));
        !           417:     WRITEF((VBuff,"   This would connect to a server session on \\\\rajnathX86\n"));
        !           418:     WRITEF((VBuff,"   with id \"imbroglio\" if there was a\n"));
1.1       root      419:     WRITEF((VBuff,"            REMOTE /S <\"Cmd\"> imbroglio\n"));
1.1.1.2 ! root      420:     WRITEF((VBuff,"   started on the machine \\\\rajnathX86.\n\n"));
1.1       root      421:     WRITEF((VBuff,"   To Exit: %cQ (Leaves the Remote Server Running)\n",COMMANDCHAR));
                    422:     WRITEF((VBuff,"\n"));
                    423: }
                    424: /*************************************************************/
                    425: 
                    426: VOID
                    427: DisplayServerHlp()
                    428: {
                    429:     WRITEF((VBuff,"\n   To Start the SERVER end of REMOTE\n"));
                    430:     WRITEF((VBuff,"   ---------------------------------\n"));
1.1.1.2 ! root      431:     WRITEF((VBuff,"   Syntax : REMOTE /S <\"Cmd\"> <Unique Id> [/U [domainname\\]username] [/F] [/B]\n"));
        !           432:     WRITEF((VBuff,"\n"));
        !           433: 
        !           434:     WRITEF((VBuff,"   <\"Cmd\">        A Text-Mode program that you want to control\n"));
        !           435:     WRITEF((VBuff,"                  from another computer.\n"));
        !           436: 
        !           437:     WRITEF((VBuff,"   <Unique Id>    The Id that will uniquely identify this session\n"));
        !           438:     WRITEF((VBuff,"                  of remote server from other possible sessions\n"));
        !           439:     WRITEF((VBuff,"                  on this computer.\n"));
        !           440: 
        !           441:     WRITEF((VBuff,"   [/U]           User or Group  who can connect to this session.\n"));
        !           442:     WRITEF((VBuff,"                  If this is not specified then anyone can connect.\n"));
        !           443:     WRITEF((VBuff,"   [/F]           Foreground color eg blue, red, etc.\n"));
        !           444:     WRITEF((VBuff,"   [/B]           Background color eg blue, lwhite, etc.\n"));
        !           445: 
        !           446:     WRITEF((VBuff,"\n"));
        !           447:     WRITEF((VBuff,"   Examples: REMOTE /S \"cmd\" emerald /U administrators\n"));
        !           448:     WRITEF((VBuff,"             REMOTE /S \"i386kd -v\" imbroglio\n"));
        !           449:     WRITEF((VBuff,"\n"));
        !           450:     WRITEF((VBuff,"   If you have started the server-end of remote on computer \\\\%s\n",HostName));
        !           451:     WRITEF((VBuff,"   and you want to interact with \"i386kd -v\" from another computer,\n"));
        !           452:     WRITEF((VBuff,"   start the client-end of remote on the other computer by typing:\n"));
        !           453:     WRITEF((VBuff,"            REMOTE /C %s imbroglio\n",HostName));
        !           454:     WRITEF((VBuff,"   To connect to the session \"Cmd\", start:\n"));
        !           455:     WRITEF((VBuff,"            REMOTE /C %s emerald\n",HostName));
        !           456:     WRITEF((VBuff,"   Only people with admin access on \\\\%s can connect to this session.\n",HostName));
        !           457: 
        !           458:     WRITEF((VBuff,"\n\n   To Exit: %cK \n",COMMANDCHAR));
1.1       root      459:     WRITEF((VBuff,"\n"));
                    460: 
                    461: }
                    462: 
                    463: WORD
                    464: GetColorNum(
                    465:     char *color
                    466:     )
                    467: {
                    468:     int i;
                    469: 
                    470:     strlwr(color);
                    471:     for (i=0;i<16;i++)
                    472:     {
                    473:         if (strcmp(ColorList[i],color)==0)
                    474:         {
                    475:             return(i);
                    476:         }
                    477:     }
                    478:     return ((WORD)atoi(color));
                    479: }
                    480: 
                    481: VOID
                    482: SetColor(
                    483:     WORD attr
                    484:     )
                    485: {
                    486:        COORD  origin={0,0};
                    487:     DWORD  dwrite;
                    488:     FillConsoleOutputAttribute
                    489:     (
                    490:        MyOutHandle,attr,csbiOriginal.dwSize.
                    491:        X*csbiOriginal.dwSize.Y,origin,&dwrite
                    492:     );
                    493:     SetConsoleTextAttribute(MyOutHandle,attr);
                    494: }
                    495: 
                    496: BOOL
                    497: GetNextConnectInfo(
                    498:     char** SrvName,
                    499:     char** PipeName
                    500:     )
                    501: {
                    502:     static char szServerName[32];
                    503:     static char szPipeName[32];
                    504: 
                    505:     try
                    506:     {
                    507:         ZeroMemory(szServerName,32);
                    508:         ZeroMemory(szPipeName,32);
                    509:         SetConsoleTitle("Remote - Prompting for next Connection");
                    510:         WRITEF((VBuff,"Debugger machine (server): "));
                    511:         fflush(stdout);
                    512: 
                    513:         if (((*SrvName=gets(szServerName))==NULL)||
                    514:              (strlen(szServerName)==0))
                    515:         {
                    516:             return(FALSE);
                    517:         }
                    518: 
                    519:         if (szServerName[0] == COMMANDCHAR &&
                    520:             (szServerName[1] == 'q' || szServerName[1] == 'Q')
                    521:            )
                    522:         {
                    523:             return(FALSE);
                    524:         }
                    525: 
                    526:         WRITEF((VBuff,"Debuggee machine : "));
                    527:         fflush(stdout);
                    528: 
                    529:         if ((*PipeName=gets(szPipeName))==NULL)
                    530:         {
                    531:             return(FALSE);
                    532:         }
                    533:         if (szPipeName[0] == COMMANDCHAR &&
                    534:             (szPipeName[1] == 'q' || szPipeName[1] == 'Q')
                    535:            )
                    536:         {
                    537:             return(FALSE);
                    538:         }
                    539:         WRITEF((VBuff,"\n\n"));
                    540:     }
                    541: 
                    542:     except(EXCEPTION_EXECUTE_HANDLER)
                    543:     {
                    544:         return(FALSE);  // Ignore exceptions
                    545:     }
                    546:     return(TRUE);
                    547: }
                    548: 
                    549: 
                    550: /*************************************************************/
                    551: VOID
                    552: Errormsg(
                    553:     char* str
                    554:     )
                    555: {
                    556:     WRITEF((VBuff,"Error (%d) - %s\n",GetLastError(),str));
                    557: }
                    558: 
                    559: /*************************************************************/

unix.superglobalmegacorp.com

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