Annotation of os2sdk/demos/examples/session/session.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * This example demonstrates some of the session manager API
1.1.1.2 ! root        3:  *
        !             4:  * Created by Microsoft Corp. 1987
1.1       root        5:  */
                      6: 
1.1.1.2 ! root        7: #define INCL_DOSSESMGR
        !             8: 
        !             9: #include <os2def.h>    /* defines SELECTOROF and OFFSETOF */
1.1       root       10: #include <stdio.h>
                     11: #include <ctype.h>
1.1.1.2 ! root       12: #include <bse.h>
1.1       root       13: 
                     14: #define        TOTAL_COLMS     80      /* screen size in colms */
                     15: #define        TOTAL_ROWS      24      /* screen size in rows */
                     16: 
                     17: void Menu();
                     18: void StartChild();
                     19: void KillChild();
                     20: void ExitThisProc();
                     21: 
                     22: extern unsigned _aenvseg;
                     23: extern unsigned _acmdln;
                     24: 
                     25: 
1.1.1.2 ! root       26: USHORT KidSID;              /* Array of session IDs */
        !            27: USHORT KidPID;
1.1       root       28: short Instance;
                     29: char spbuf[80], pbuf[128], Inputs[10];
                     30: char Title[100] = "SESSION.EXE";
                     31: 
                     32: main(ac,av)
                     33:     int     ac;
                     34:     char    **av;
                     35: {
                     36:     int i;
                     37:     char **p;
                     38:     char far *progname;
                     39:     /*
                     40:      * Grab the full path name of this program (the one used by EXECPGM())
                     41:      * for use in creating child sessions.
                     42:      */
1.1.1.2 ! root       43:     SELECTOROF(progname) = _aenvseg;   /* This is standard C runtime stuff */
        !            44:     OFFSETOF(progname) = _acmdln-1;   /* The Path name comes just prior to */
1.1       root       45:                                    /* AV[0] - but remember this is in a */
                     46:     while(*--progname)             /* different segment than the rest of */
                     47:            ;                       /* the program, thus the copy (below) */
                     48: 
                     49:     for (i=0; *++progname; i++)     /* Copy a far string to a local buffer */
                     50:        pbuf[i] = *progname;
                     51:     pbuf[i] = 0;
                     52: 
                     53:     KidSID = -1;
                     54:     /*
                     55:      * Look for the first Numeric argument, convert it and break out.
                     56:      */
                     57:     Instance = 0;
                     58:     for (p = av+1; **p; p++) {
                     59:        if (isdigit(**p)) {
                     60:            Instance = atoi(*p);
                     61:            break;
                     62:        }
                     63:     }
                     64: 
                     65:     for (;;) {
                     66: 
                     67:        Menu();
                     68: 
                     69:        switch(getch()) {
                     70: 
                     71:            case 's':
                     72:                StartChild();
                     73:                break;
                     74:            case 'f':
                     75:                KillChild();
                     76:                break;
                     77:            case 'e':
                     78:                ExitThisProc();
                     79:                break;
                     80:            case 'm':
                     81:                break;
                     82:            default:
                     83:                putc('\007', stdout);
                     84:        }
                     85:     }
                     86: }
                     87: 
                     88: void
                     89: Menu()
                     90: {
                     91:     if( Instance)
                     92:        printf("\t%s Child process # %u\n\n", Title, Instance);
                     93:     else
                     94:        printf("\t%s\n\n", Title);
                     95:     printf("Select an option:\n\n");
                     96:     printf("\ts: Start child\n");
                     97:     printf("\tf: Stop child\n");
                     98:     printf("\te: Exit\n");
                     99:     printf("\tm: Menu\n\n");
                    100:     printf("? ");
                    101: }
                    102: 
                    103: void
                    104: StartChild()
                    105: {
                    106:     int rc;
1.1.1.2 ! root      107:     STARTDATA SDbuf;
        !           108:     USHORT SessionID, ProcessID;
1.1       root      109: 
                    110:     if (KidSID != -1) {
                    111:        printf("Sorry, only one child at a time.\n");
                    112:        return;
                    113:     }
                    114:     sprintf(spbuf, "Child Session # %u", Instance + 1 );
                    115:     sprintf(Inputs, "%u", Instance + 1);
                    116: 
                    117: 
                    118:     printf("Starting Child...\n");
                    119: 
1.1.1.2 ! root      120:     SDbuf.cb       = sizeof(SDbuf);
1.1       root      121:     SDbuf.Related   = 1;       /* Yes                                       */
                    122:     SDbuf.FgBg     = 0;        /* Start in the foreground                   */
                    123:     SDbuf.TraceOpt  = 0;       /* Run the child synchronously, why? I dunno */
                    124:     SDbuf.PgmTitle  = spbuf;   /* Title in session manager box              */
                    125:     SDbuf.PgmName   = pbuf;    /* Exec path (see main)                      */
                    126:     SDbuf.PgmInputs = Inputs;  /* Any command line arguments                */
1.1.1.2 ! root      127:     SDbuf.TermQ     = 0;       /* No notification queue                     */
1.1       root      128: 
1.1.1.2 ! root      129:     rc = DosStartSession(&SDbuf, &SessionID, &ProcessID);
1.1       root      130: 
                    131:     if (rc)
                    132:        printf("Failed, %d no more screens available\n", rc);
                    133:     else
                    134:     {
                    135:        printf("Success, screen group %u, PID %u\n", SessionID, ProcessID);
                    136:        KidSID = SessionID;
                    137:        KidPID = ProcessID;
                    138:     }
                    139: 
                    140: }
                    141: 
                    142: void
                    143: KillChild()
                    144: {
                    145:     short rc;
                    146: 
                    147:     if (KidSID == -1) {
                    148:        printf("Sorry, must start session before you can stop it.\n");
                    149:        return;
                    150:     }
1.1.1.2 ! root      151:     rc = DosStopSession(0, KidSID, 0l);
1.1       root      152: #ifdef DEBUG
                    153:     printf("Screen ID: %d\n", KidSID);
                    154: #endif
                    155:     if (rc)
                    156:        printf("Stop session failed, code %d\n", rc);
                    157:     KidSID = -1;
                    158: }
                    159: 
                    160: void
                    161: ExitThisProc()
                    162: {
                    163:     printf(" Exiting, gasp cough die...\n");
                    164:     exit(0);
                    165: }

unix.superglobalmegacorp.com

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