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