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

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

unix.superglobalmegacorp.com

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