xref: /original-bsd/usr.bin/f77/libI77/wsnmle.c (revision 7cb96e62)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)wsnmle.c	5.1	07/30/85
7  */
8 
9 /*
10  *		name-list write
11  */
12 
13 #include "fio.h"
14 #include "lio.h"
15 #include "nmlio.h"
16 #include <strings.h>
17 
18 int l_write(), t_putc();
19 LOCAL char nml_wrt[] = "namelist write";
20 char namelistkey_ = '&';
21 
22 s_wsne(a) namelist_arglist *a;
23 {
24 	int n, first;
25 	struct namelistentry *entries;
26 	int *dimptr, *spans, ndim, nelem, offset, vlen, vtype, number;
27 	char *nmlist_nm, *cptr;
28 
29 	nmlist_nm = a->namelist->namelistname;
30 	reading = NO;
31 	formatted = NAMELIST;
32 	fmtbuf = "ext namelist io";
33 	if(n=c_le(a,WRITE)) return(n);
34 	putn = t_putc;
35 	line_len = LINE-1;	/* so we can always add a comma */
36 	curunit->uend = NO;
37 	leof = NO;
38 	if(!curunit->uwrt && ! nowwriting(curunit)) err(errflag, errno, nml_wrt)
39 
40 	/* begin line with " &namelistname " */
41 	if(recpos != 0)
42 		PUT('\n')  /* PUT() adds blank */
43 	else
44 		PUT(' ');
45 	PUT(namelistkey_);
46 	while(*nmlist_nm != '\0') PUT(*nmlist_nm++);
47 	PUT(' ');
48 
49 	/* now loop through entries writing them out */
50 	entries = a->namelist->names;
51 	first = 1;
52 	while( entries->varname[0] != 0 )
53 	{
54 		/* write out variable name and '=' */
55 		cptr = entries->varname;
56 		chk_len( strlen(cptr) + 3);
57 		if(first++ != 1) PUT(',');
58 		PUT(' ');
59 		while( *cptr != '\0') PUT(*cptr++);
60 		PUT('=');
61 
62 		/* how many value are there? */
63 		if( (dimptr = entries->dimp) == NULL ) number = 1;
64 		else number = dimptr[1];
65 		/* what is element length? */
66 		vlen = entries->typelen;
67 		/* get type */
68 		vtype = entries->type;
69 
70 		if(n=l_write( &number, entries->varaddr, vlen, vtype ))
71 				err(errflag,n,nml_wrt);
72 		entries++;
73 	}
74 	PUT('\n');
75 	PUT(namelistkey_);
76 	cptr = "end\n";
77 	while(*cptr != '\0') PUT(*cptr++);
78 	return(OK);
79 }
80 
81 LOCAL
82 t_putc(c) char c;
83 {
84 	if(c=='\n') {
85 		recpos=0;
86 	} else if(recpos == 0) {
87 		putc(' ',cf);		/* for namelist,	   */
88 		recpos = 2;		/* never print in column 1 */
89 	} else {
90 		recpos++;
91 	}
92 	putc(c,cf);
93 	return(OK);
94 }
95