~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to test/os/testtape.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* @(#)testtape.c       19.1 (ES0-DMD) 02/25/03 14:33:06 */
 
2
/*===========================================================================
 
3
  Copyright (C) 1995 European Southern Observatory (ESO)
 
4
 
 
5
  This program is free software; you can redistribute it and/or 
 
6
  modify it under the terms of the GNU General Public License as 
 
7
  published by the Free Software Foundation; either version 2 of 
 
8
  the License, or (at your option) any later version.
 
9
 
 
10
  This program is distributed in the hope that it will be useful,
 
11
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
  GNU General Public License for more details.
 
14
 
 
15
  You should have received a copy of the GNU General Public 
 
16
  License along with this program; if not, write to the Free 
 
17
  Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, 
 
18
  MA 02139, USA.
 
19
 
 
20
  Corresponding concerning ESO-MIDAS should be addressed as follows:
 
21
        Internet e-mail: midas@eso.org
 
22
        Postal address: European Southern Observatory
 
23
                        Data Management Division 
 
24
                        Karl-Schwarzschild-Strasse 2
 
25
                        D 85748 Garching bei Muenchen 
 
26
                        GERMANY
 
27
===========================================================================*/
 
28
 
 
29
#include <stdio.h>
 
30
#include <setjmp.h>
 
31
#include <signal.h>
 
32
#include <osparms.h>
 
33
#include <osudef.h>
 
34
 
 
35
char *osmsg();
 
36
 
 
37
int opentape(), closetape(), rewtape(), rstatape(), skiptape(), skbctape();
 
38
int wfmtape(), readtape(), writetape(), skipeom(), quit(), nothing();
 
39
int forwblk(), backblk(), infodev();
 
40
int returnhere();
 
41
 
 
42
 
 
43
        /* Definition of Functions */
 
44
 
 
45
#define ioinfo(f,b,fn,bn)       (ops[U_INFO])(f,b,fn,bn)
 
46
#define ioopen(f,m,d)           (ops[U_OPEN])(f,m,d)
 
47
#define ioclose(f)              (ops[U_CLOSE])(f)
 
48
#define ioread(f,b,l)           (ops[U_READ])(f,b,l) 
 
49
#define iowrite(f,b,l)          (ops[U_WRITE])(f,b,l) 
 
50
#define iorew(f)                (ops[U_REWIND])(f) 
 
51
#define ioeom(f)                (ops[U_EOM])(f) 
 
52
#define ioweof(f)               (ops[U_WEOF])(f)
 
53
#define iofsf(f,n)              (ops[U_FMF])(f,n)       
 
54
#define iobsf(f,n)              (ops[U_FMB])(f,n)       
 
55
#define iofsr(f,n)              (ops[U_BMF])(f,n)       
 
56
#define iobsr(f,n)              (ops[U_BMB])(f,n)       
 
57
#define iosread(f,s,ss,b,l)     (ops[U_SREAD])(f,s,ss,b,l) 
 
58
#define ioswrite(f,s,ss,b,l)    (ops[U_SWRITE])(f,s,ss,b,l)     
 
59
 
 
60
 
 
61
        /* This structure, created for each device, allows to know
 
62
                the current position on any device
 
63
         */
 
64
typedef int (*FCT_PTR)();               /* Pointer to function */
 
65
FCT_PTR ops[U_MAX+1];                   /* Available Operations */
 
66
#define NULL_PTR(x)     (x *)0
 
67
 
 
68
#define READ_WRITE 2    /* as in osparms.h */
 
69
 
 
70
static char name[80] = "ip1:/dev/nrst1"; 
 
71
static char class[20] = "remote"; 
 
72
 
 
73
static int fd=0xffff;
 
74
static int mode=0xffff;
 
75
static jmp_buf env;
 
76
 
 
77
struct msg_cmd {
 
78
        char *msg;
 
79
        int (*cmd)();
 
80
        } msg_cmd[] = {
 
81
        "\n0-   *****  MENU *****\n",   nothing,
 
82
        "1-     Open a tape device\n",  opentape,
 
83
        "2-     Close a tape device\n", closetape,
 
84
        "3-     Rewind the tape\n",     rewtape,
 
85
        "4-     Skip forward tape marks\n",     skiptape,
 
86
        "5-     Skip backward tape marks\n",    skbctape,
 
87
        "6-     Write a tape mark\n",   wfmtape,
 
88
        "7-     Read a buffer\n",       readtape,
 
89
        "8-     Write a buffer\n",      writetape,
 
90
        "9-     Skip forward block\n",  forwblk,
 
91
        "10-    Skip backward block\n", backblk,
 
92
        "11-    Retrieve device info\n",        infodev,
 
93
        "12-    Skip end of media\n",   skipeom,
 
94
        "13-    Exit\n",                quit,
 
95
        (char *)0,                      nothing
 
96
        };
 
97
 
 
98
main()
 
99
{
 
100
        struct msg_cmd *m_c;
 
101
        int opt;
 
102
        char buff[80];
 
103
        setjmp(env);
 
104
        signal(SIGINT,returnhere);
 
105
        while(1) {
 
106
                m_c = msg_cmd;
 
107
                while(m_c->msg != (char *)0)
 
108
                        printf(m_c++->msg);
 
109
                printf("\nOption: ");
 
110
                if ((opt=atoi(gets(buff))) >= (sizeof(msg_cmd)/sizeof(struct msg_cmd))) 
 
111
                        printf("Bad option\n");
 
112
                else
 
113
                        (*msg_cmd[opt].cmd)();
 
114
/*              printf("\nPress RETURN to continue"); */
 
115
/*              gets(buff); */
 
116
                }
 
117
}
 
118
 
 
119
opentape()
 
120
{
 
121
        char aname[80], aclass[20], input[80];
 
122
        char *pclass;
 
123
        int pfd;
 
124
 
 
125
        printf("\nOPEN TAPE\n");
 
126
 
 
127
        printf("Enter name of device (%s): ",name);
 
128
        gets(aname);
 
129
 
 
130
        if (strlen(aname) != 0)
 
131
                strcpy(name,aname);
 
132
 
 
133
        printf("Enter open mode (2:READ_WRITE): ");
 
134
        mode=atoi(gets(input));
 
135
        if (strlen(input) == 0)
 
136
                mode=READ_WRITE;
 
137
 
 
138
        printf("Enter class for this device (%s): ",class);
 
139
        gets(aclass);
 
140
        pclass=class;
 
141
 
 
142
        if (strlen(aclass) != 0)
 
143
                strcpy(class,aclass);
 
144
 
 
145
        if (getclass() < 0) {
 
146
                printf("Error finding class %s, %s\n",
 
147
                        class, osmsg());
 
148
                return;
 
149
                }
 
150
 
 
151
        pfd = ioopen(name,mode,0);
 
152
 
 
153
        if (pfd == -1) {
 
154
                printf("Error opening %s, %s\n",
 
155
                        name,osmsg());
 
156
                return;
 
157
                }
 
158
 
 
159
        fd=pfd;
 
160
        printf("OPEN %s succeded.\n",name);
 
161
}
 
162
 
 
163
closetape()
 
164
{
 
165
        printf("\nCLOSE TAPE\n");
 
166
        if (ioclose(fd) == -1) {
 
167
                printf("Error closing device, %s\n",osmsg());
 
168
                return;
 
169
                }
 
170
        printf("CLOSE succeded\n");
 
171
}
 
172
 
 
173
rewtape()
 
174
{
 
175
        printf("\nREWIND TAPE\n");
 
176
        if (iorew(fd) == -1) {
 
177
                printf("Error rewinding device, %s\n",
 
178
                        osmsg());
 
179
                return;
 
180
                }
 
181
        printf("REWIND succeded.\n");
 
182
}
 
183
 
 
184
skiptape()
 
185
{
 
186
        char name[80];
 
187
        int ntm;
 
188
        int ret;
 
189
        printf("Enter number of tape marks to be skipped (1): ");
 
190
 
 
191
        gets(name);
 
192
        if ( strlen(name) == 0)
 
193
                ntm=1;
 
194
        else
 
195
                ntm = atoi(name);
 
196
 
 
197
        printf("\nSKIP FORWARD %d TAPE MARKS\n",ntm);
 
198
        if ((ret = iofsf(fd,ntm)) == -1) {
 
199
                printf("Error skipping %d tape marks, %s\n",
 
200
                        ntm,osmsg());
 
201
                return;
 
202
                }
 
203
        printf("SKIP succeded\n");
 
204
}
 
205
 
 
206
skbctape()
 
207
{
 
208
        char name[80];
 
209
        int ntm;
 
210
        printf("Enter number of tape marks to be skipped (1): ");
 
211
 
 
212
        gets(name);
 
213
        if ( strlen(name) == 0)
 
214
                ntm=1;
 
215
        else
 
216
                ntm = atoi(name);
 
217
 
 
218
        printf("\nSKIP BACKWARD %d TAPE MARKS\n",ntm);
 
219
        if (iobsf(fd,ntm) == -1) {
 
220
                printf("Error skipping %d tape marks, %s\n",
 
221
                        ntm,osmsg());
 
222
                return;
 
223
                }
 
224
        printf("SKIP succeded\n");
 
225
}
 
226
 
 
227
wfmtape()
 
228
{
 
229
        printf("\nWRITE A TAPE MARK\n");
 
230
        if (ioweof(fd) == -1) {
 
231
                printf("Error writting a tape mark, %s\n", osmsg());
 
232
                return;
 
233
                }
 
234
        printf("WRITE A TAPE MARK succeded.\n");
 
235
}
 
236
 
 
237
writetape()
 
238
{
 
239
        char *malloc();
 
240
        char *pbuf;
 
241
        char name[80];
 
242
        int n_bytes;
 
243
        char pattern;
 
244
        int i;
 
245
 
 
246
        printf("\nWRITE A BUFFER\n");
 
247
        printf("Enter number of bytes to be written (1024): ");
 
248
        n_bytes = atoi(gets(name));
 
249
        if ( strlen(name) == 0)
 
250
                n_bytes=1024;
 
251
 
 
252
        if ((pbuf = malloc(n_bytes)) == (char *)0 ) { 
 
253
                printf("Max. memory allocation: %d bytes\n",n_bytes);
 
254
                return;
 
255
                }
 
256
 
 
257
        printf("Buffer allocated\n");
 
258
        printf("Enter pattern for buffer ('A'): ");
 
259
        if (strlen(gets(name)) == 0)
 
260
                pattern='A';
 
261
        else
 
262
                pattern=name[0];
 
263
 
 
264
        printf("Pattern=%c\n",pattern);
 
265
 
 
266
        for (i=0; i<n_bytes; i++)
 
267
                pbuf[i]=pattern;
 
268
 
 
269
/*      for(i=0; i<n_bytes; i++) 
 
270
/*              printf("%c ",pbuf[i]);
 
271
/*      getchar();
 
272
*/
 
273
        if (iowrite(fd,pbuf,n_bytes) == -1) {
 
274
                printf("Error writting %d bytes, %s\n",
 
275
                        n_bytes,osmsg());
 
276
                return;
 
277
                }
 
278
        printf("WRITE %d bytes succeded.\n",n_bytes);
 
279
        free(pbuf);
 
280
}
 
281
 
 
282
readtape()
 
283
{
 
284
        char *malloc();
 
285
        char *pbuf;
 
286
        char name[80];
 
287
        int n_bytes, length;
 
288
        char pattern;
 
289
        int i;
 
290
 
 
291
        printf("\nREAD A BUFFER\n");
 
292
        printf("Enter number of bytes to be read (1024): ");
 
293
        n_bytes = atoi(gets(name));
 
294
        if ( strlen(name) == 0)
 
295
                n_bytes=1024;
 
296
 
 
297
/*      if ((pbuf = malloc(8192)) == (char *)0 ) {  */
 
298
        if ((pbuf = malloc(n_bytes)) == (char *)0 ) {  
 
299
                printf("Max. memory allocation: %d bytes\n",n_bytes);
 
300
                return;
 
301
                }
 
302
 
 
303
        printf("Buffer allocated\n");
 
304
 
 
305
/*      for (i=0; i<n_bytes; i++)
 
306
/*              pbuf[i]='0';
 
307
/*      for(i=0; i<n_bytes; i++) 
 
308
/*              printf("%c ",pbuf[i]);
 
309
/*      getchar();
 
310
*/
 
311
 
 
312
        if ((length = ioread(fd,pbuf,n_bytes)) == -1) {
 
313
                printf("Error reading %d bytes, %s\n",
 
314
                        n_bytes,osmsg());
 
315
                return;
 
316
                }
 
317
 
 
318
/*      for(i=0; i<length; i++) 
 
319
/*              printf("%c ",pbuf[i]);
 
320
*/
 
321
 
 
322
        printf("\nREAD %d bytes succeded.\n",length);
 
323
        if (length > 0) {
 
324
                printf("Pattern=%c\n",pbuf[0]);
 
325
                pattern=pbuf[0];
 
326
                for(i=0; i<length; i++) 
 
327
                        if (pattern != pbuf[i])
 
328
                                fprintf(stderr,"Byte %d (%c) differs of pattern(%c)\n",
 
329
                                        i,pbuf[i],pattern);
 
330
                }
 
331
        free(pbuf);
 
332
}
 
333
 
 
334
backblk()
 
335
{
 
336
        char name[80];
 
337
        int ntm;
 
338
        printf("Enter number of blocks to be skipped backward (1): ");
 
339
 
 
340
        gets(name);
 
341
        if ( strlen(name) == 0)
 
342
                ntm=1;
 
343
        else
 
344
                ntm = atoi(name);
 
345
 
 
346
        printf("\nSKIP BACKWARD %d BLOCKS\n",ntm);
 
347
        if (iobsr(fd,ntm) == -1) {
 
348
                printf("Error skipping backward %d blocks, %s\n",
 
349
                        ntm,osmsg());
 
350
                return;
 
351
                }
 
352
        printf("SKIP succeded\n");
 
353
}
 
354
 
 
355
forwblk()
 
356
{
 
357
        char name[80];
 
358
        int ntm;
 
359
        printf("Enter number of blocks to be skipped forward (1): ");
 
360
 
 
361
        gets(name);
 
362
        if ( strlen(name) == 0)
 
363
                ntm=1;
 
364
        else
 
365
                ntm = atoi(name);
 
366
 
 
367
        printf("\nSKIP FORWARD %d BLOCKS\n",ntm);
 
368
        if (iofsr(fd,ntm) == -1) {
 
369
                printf("Error skipping forward %d blocks, %s\n",
 
370
                        ntm,osmsg());
 
371
                return;
 
372
                }
 
373
        printf("SKIP succeded.\n");
 
374
}
 
375
 
 
376
infodev()
 
377
{
 
378
        struct osustat s;
 
379
        int filenum;
 
380
        long blkno;
 
381
        printf("\nRETRIEVE INFO FROM OPENED TAPE\n");
 
382
        if ( ioinfo(fd,&s, &filenum, &blkno) == -1) {
 
383
                printf("Error getting status from device, %s\n",
 
384
                        osmsg());
 
385
                return;
 
386
                }
 
387
        printf("RETRIEVE INFO succeded\n");
 
388
        printf("s.usize= %d\n", s.usize);
 
389
        printf("s.blocksize= %d\n", s.blocksize);
 
390
        printf("s.density= %d\n", s.density);
 
391
        printf("s.isda= %d\n", s.isda);
 
392
        printf("s.istm= %d\n", s.istm);
 
393
        printf("filenum= %d\n", filenum);
 
394
        printf("blkno= %d\n", blkno);
 
395
}
 
396
 
 
397
skipeom()
 
398
{
 
399
        printf("\nSKIP TO END OF MEDIA\n");
 
400
        if (ioeom(fd) == -1) {
 
401
                printf("Error skipping to end of media, %s\n",
 
402
                        osmsg());
 
403
                return;
 
404
                }
 
405
        printf("SKIP EOM succeded\n");
 
406
}
 
407
 
 
408
nothing()
 
409
{
 
410
}
 
411
 
 
412
returnhere()
 
413
{
 
414
        longjmp(env,0);
 
415
}
 
416
 
 
417
quit()
 
418
{
 
419
        ospexit(0);
 
420
}
 
421
 
 
422
struct iolist *findclass(aclass)
 
423
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
424
.PURPOSE Retrieve the class name
 
425
.RETURNS Pointer to found class / NULL
 
426
.REMARKS 
 
427
----------------------------------------------------------------------*/
 
428
        char    *aclass;        /* IN: Class to look for */
 
429
{
 
430
        struct iolist *plist, *iodevr();
 
431
        struct iolist *(*def)();
 
432
        char    *p, *q;
 
433
        
 
434
        /* Follow the linked list of iolist's */
 
435
 
 
436
   for (def = iodevr; def; def = plist->next)
 
437
   {
 
438
        plist = (*def)();
 
439
        for(q=aclass, p=plist->klass; (*p == *q) && (*p); p++, q++) ;
 
440
        if ((*q == '\0') && (*p == '\0'))       break;
 
441
   }
 
442
   if (!def)    /* Class Not Found */
 
443
        plist = NULL_PTR(struct iolist);
 
444
   
 
445
   return(plist);
 
446
}
 
447
 
 
448
int getclass()
 
449
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
450
.PURPOSE Retrieve the class name, to retrieve operators.
 
451
.RETURNS 0 / -1 (class doesn't exist)
 
452
.REMARKS Functions added in FCB
 
453
----------------------------------------------------------------------*/
 
454
{
 
455
        struct  iolist *plist;
 
456
        int     i;
 
457
        OPITEM  *pop;
 
458
        
 
459
   plist = findclass(class);
 
460
   if (!plist)  
 
461
   {
 
462
        oserror = -1;
 
463
        oserrmsg = "Bad DEVCAP cl=";
 
464
        return(-1);
 
465
   }
 
466
   
 
467
   /*
 
468
   ** We have just to insert the functions in the list
 
469
   */
 
470
 
 
471
   for (pop = plist->oplist, i = plist->nop; --i >= 0; pop++)
 
472
        ops[pop->opid] = pop->opf;
 
473
   
 
474
   return(0);
 
475
}
 
476
 
 
477
/*=====================================================================
 
478
                Callable Routines
 
479
 *=====================================================================*/
 
480
 
 
481
char *osuname(f)
 
482
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
483
.PURPOSE Retrieve name of current unit (when f = -1)
 
484
.RETURNS Name / NULL pointer if failed
 
485
----------------------------------------------------------------------*/
 
486
        int f;          /* IN: The unit number  */
 
487
 
488
    if (f != -1)
 
489
        return((char *)0);      /* Bad unit number...   */
 
490
 
 
491
    return(name);
 
492
}
 
493
 
 
494
int osumode(f)
 
495
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
496
.PURPOSE Retrieve opening mode of current unit (when f = -1)
 
497
.RETURNS Mode / -1 when error
 
498
----------------------------------------------------------------------*/
 
499
        int f;          /* IN: The unit number  */
 
500
 
501
    if (f != -1)
 
502
        return(-1);     /* Bad unit number...   */
 
503
 
 
504
    return(mode);
 
505
}
 
506
 
 
507
int osugrep (class_name, item)
 
508
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
509
.PURPOSE Get the item function in a class of devices
 
510
.RETURNS 0 / -1
 
511
----------------------------------------------------------------------*/
 
512
        char    *class_name;    /* IN: Class of Devices (NULL for any) */
 
513
        OPITEM  *item;          /* OUT: op_code + Function Pointer */
 
514
 
515
        struct iolist *plist, *iodevr();
 
516
        struct iolist *(*def)();
 
517
        int     i;
 
518
        OPITEM  *pop;
 
519
        
 
520
                        
 
521
   for (def = iodevr; def; def = plist->next)
 
522
   {
 
523
        if (!class_name)        /* Class not specified: look to next one */
 
524
                plist = (*def)();
 
525
        else    plist = findclass(class_name);
 
526
        if (!plist)             return(-1);
 
527
 
 
528
                /* Retrieve the item in list */
 
529
        
 
530
        for (pop = plist->oplist, i = plist->nop; 
 
531
                (--i >= 0) && (pop->opid != item->opid);  pop++)  ;
 
532
 
 
533
        if (i >= 0)             /* I found the relevant item... */
 
534
        {       item->opf = pop->opf;
 
535
                return(0);
 
536
        }
 
537
        if (class_name)         break;
 
538
        
 
539
   }
 
540
   return(-1);
 
541
}