File: pcnltss.c

package info (click to toggle)
pact 980714-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 13,096 kB
  • ctags: 26,034
  • sloc: ansic: 109,076; lisp: 9,645; csh: 7,147; fortran: 1,050; makefile: 136; lex: 95; sh: 32
file content (311 lines) | stat: -rw-r--r-- 7,978 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * PCNLTSS.C - NLTSS operating system routines
 *
 * Source Version: 2.0
 * Software Release #92-0043
 *
 */

#include "cpyright.h"

#include "ppc.h"
#include "baselb.h"

#define NO_CONTROLLEE_TO_SEND   3
#define NO_CONTROLLEE_ATTACHED  2
#define RECEIVE_STATE           6
#define MESSAGE_RECEIVED        0

char
 **argv_cray;

static int
 _PC_nltss_release(PROCESS *pp),
 _PC_nltss_in_ready(PROCESS *pp),
 _PC_nltss_close(PROCESS *pp),
 _PC_nltss_status(PROCESS *pp),
 _PC_nltss_flush(PROCESS *pp),
 _PC_nltss_gets(char *bf, int len, PROCESS *pp),
 _PC_nltss_printf(PROCESS *pp, char *buffer);

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* FORK - fake a UNIX fork under NLTSS
 *      - return 1 if successful
 *      - and -1 otherwise
 */

fork()
   {int  flag, len, i;
    char bf[MAXLINE];
    static id = 2;

    PC_ERR_TRAP(-1);

    flag = IZNITIAL(argv_cray[0], &Zero_I, &Zero_I);
    if (flag != 0)
       PC_error("COULDN'T INITIALIZE PROCESS (%d) - FORK", flag);

/* build the message in bf */
    strcpy(bf, argv_cray[1]);
    for (i = 2; argv_cray[i] != '\0'; i++)
        {strcat(bf, " ");
         strcat(bf, argv_cray[i]);};

    len  = strlen(bf);
    flag = IZSNDME(bf, &len, &Zero_I);
    if (flag != 0)
       PC_error("COULDN'T SEND MESSAGE TO PROCESS (%d) - FORK", flag);

    return(id++);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* PC_MK_PROCESS - initialize and return a PROCESS */

PROCESS *PC_mk_process(argv, mode, type)
   char **argv, *mode;
   int type;
   {PROCESS *pp;

    argv_cray = argv;

    pp = FMAKE(PROCESS, "PC_MK_PROCESS:pp");

    pp->in_ring  = FMAKE_N(unsigned char, SIZE_BUF,
                   "PC_MK_PROCESS:in_ring");
    pp->ib_in    = 0;
    pp->ob_in    = 0;

    pp->type     = PC_LOCAL;
    pp->rcpu     = -1;
    pp->acpu     = -1;

    pp->line_mode = FALSE;
    pp->line_sep  = 0x1f;
    pp->blocking  = TRUE;

    if (mode[1] == 'b')
       {pp->vif = PD_open_vif(argv[0]);
        pp->vif->stream = (FILE *) pp;}

    else
       pp->vif = NULL;

/* assign procedures to service this process */
    pp->release    = _PC_nltss_release;
    pp->exec       = NULL;
    pp->close      = _PC_nltss_close;
    pp->statusp    = _PC_nltss_status;
    pp->flush      = _PC_nltss_flush;
    pp->read       = _PC_bin_read;
    pp->write      = _PC_bin_write;
    pp->printf     = _PC_nltss_printf;
    pp->gets       = _PC_nltss_gets;
    pp->in_ready   = _PC_nltss_in_ready;
    pp->setup      = NULL;

    if (type == PC_PARENT)
       _PC_manage_process(pp);

    return(pp);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_NLTSS_RELEASE - close the file descriptors and release the PROCESS */

static int _PC_nltss_release(pp)
   PROCESS *pp;
   {if (pp == NULL)
       return(FALSE);

    SFREE(pp->in_ring);
    SFREE(pp);

    return(TRUE);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_NLTSS_STATUS - return the status of the process */

static int _PC_nltss_status(pp)
   PROCESS *pp;
   {if (pp == NULL)
       return(EXITED);
    else
       return(pp->status & ~CHANGED);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_FGETS - get a string from the given file
 *           - if none are available or there is an return NULL
 *           - this is basically a non-blocking version of fgets
 */

char *_PC_fgets(bf, len, fp)
   char *bf;
   int len;
   FILE *fp;
   {int n;

#undef fgets

    if (fp == stdin)
       {if (IZGETMR(bf, &len, &_PC_unblock, &n))
           {*bf = '\0';
            bf  = NULL;}
        else
           bf[n] = '\0';}
    else
       return(fgets(bf, len, fp));

    return(bf);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_NLTSS_GETS - do basic read operation on NLTSS */

static int _PC_nltss_gets(bf, len, pp)
   char *bf;
   int len;
   PROCESS *pp;
   {int flag, nrcv, oneless;

    PC_ERR_TRAP(-1);

    if (pp->status != RUNNING)
       return(-1);

    oneless = len - 1;
    switch (flag = IZGETME(bf, &oneless, &Zero_I, &nrcv)) 
       {case MESSAGE_RECEIVED      :
             if (strncmp(bf, "\r\x0a all done\r\x0a", 13) != 0)
                {pp->status = RUNNING; 
                 PC_flush(pp);
                 bf[nrcv] = '\0';}

/* case of "all done" */
             else
                {pp->status = EXITED; 
                 bf = NULL;};
             break;

        case RECEIVE_STATE          :
             pp->status = RUNNING;
             bf = NULL;
             break;

        case NO_CONTROLLEE_ATTACHED :
             pp->status = STOPPED; 
             bf = NULL;
             break;

        default:
             PC_error("COULDN'T GET MESSAGE FROM PROCESS (%d) - _PC_NLTSS_GETS", 
                      flag);
             bf = NULL;
             break;};

    return(nrcv);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_NLTSS_IN_READY - return input ready status of process */

static _PC_nltss_in_ready(pp)
   PROCESS *pp;
   {return(_PC_unblock);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_NLTSS_CLOSE - kill the process in effect closing the file
 *                 - release the PROCESS (be careful about using pp
 *                 - after this!!) clean up the details too
 */

static int _PC_nltss_close(pp)
   PROCESS *pp;
   {int in, out, id, flag, dummy;

    PC_flush(pp);

    PC_ERR_TRAP(FALSE);

    if (pp == NULL)
       return(FALSE);

    in  = pp->in;
    out = pp->out;
    id  = pp->id;

    _PC_delete_process(pp);

/* kill the process itself */
    flag = IZDSCNEK(&dummy);
    if (flag != 0)
       PC_error("COULDN'T DISCONNECT PROCESS (%d) - _PC_NLTSS_CLOSE",
                flag);

/* close the communications pipe */
    pp->release(pp);

    return(TRUE);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_NLTSS_FLUSH - flush the input and output streams
 *                 - for the given process
 */

static int_PC_nltss_flush(pp)
   PROCESS *pp;
   {int len1, flag;
    char empty[1];

    if (pp == NULL)
       return(FALSE);

    PC_ERR_TRAP(FALSE);

/* send a blank message to controllee (obviously) */
    len1 = -1;
    flag = IZSNDME(empty, &len1, &Zero_I);
    if (flag == NO_CONTROLLEE_TO_SEND)
       PC_error("COULDN'T SEND BLANK MESSAGE TO PROCESS (%d) - PC_FLUSH", 
                flag);

    return(TRUE);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* _PC_NLTSS_PRINTF - do an fprintf style output to a process */

static int _PC_nltss_printf(pp, buffer)
   PROCESS *pp;
   char *buffer;
   {int len, flag;
  
    len = strlen(buffer);

    PC_ERR_TRAP(FALSE);

    buffer[len] = '\0';
    flag = IZSNDME(buffer, &len, &Zero_I);
    if (flag != 0)
       PC_error("ERROR WRITING TO PROCESS (%d) - PC_PRINTF", flag);

    return(TRUE);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/