]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/subs/m_getfld.c
Create new mh-format function %(ordinal)
[nmh] / docs / historical / mh-jun-1982 / subs / m_getfld.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9 #include <stdio.h>
10
11 int m_fldsz;
12
13 m_getfld(state, name, buf, bufsz, iob)
14 int state, bufsz;
15 char *name, *buf;
16 FILE *iob;
17 {
18 register char *cp;
19 register c;
20
21 while((c = getc(iob)) == '\001' && peekc(iob) == '\001')
22 while(getc(iob) != '\n');
23
24 if(c < 0)
25 return(FILEEOF);
26 m_fldsz = 0;
27
28 switch(state) {
29
30 case FLDEOF:
31 case BODYEOF:
32 case FLD:
33 if(c == '\n' || c == '-')
34 goto body;
35 cp = name;
36 for(;;) {
37 if(c == ':')
38 break;
39 if(cp >= &name[NAMESZ-1]) {
40 *cp = 0;
41 fprintf(stderr, "??Component Name Exceeds %d Chars:\n \"%s\"\n", NAMESZ-1, name);
42 return(LENERR);
43 }
44 if(c == '\n' || c < 0) {
45 *cp = 0;
46 fprintf(stderr, "??%s Encountered While Scanning for a colon:\n \"%s\"\n",
47 (c < 0)? "<eof>":"<end of line>", name);
48 return(FMTERR);
49 }
50 *cp++ = c;
51 *cp = 0;
52 c = getc(iob);
53 }
54
55 case FLDPLUS:
56 cp = buf;
57 for(;;) {
58 if((c = getc(iob)) < 0)
59 return(FLDEOF);
60 *cp++ = c;
61 *cp = 0;
62 m_fldsz++;
63 if(c == '\n')
64 if((c = peekc(iob)) != ' ' && c != '\t')
65 if(c == '\001' || c < 0)
66 return(FLDEOF);
67 else
68 return(FLD);
69 if(cp >= &buf[bufsz-1])
70 return(peekc(iob) < 0? FLDEOF:FLDPLUS);
71 }
72
73 body: if(c == '-')
74 while(getc(iob) != '\n') ;
75 buf[0] = 0;
76 if((c = getc(iob)) == '\001' && peekc(iob) == '\001')
77 return(BODYEOF);
78
79 case BODY:
80 cp = buf; *cp = 0;
81 for(;;) {
82 if(c < 0 || (c == '\001' && peekc(iob) == '\001'))
83 return(BODYEOF);
84 *cp++ = c;
85 *cp = 0;
86 m_fldsz++;
87 if(cp >= &buf[bufsz-1])
88 return(((c=peekc(iob))<0||c=='\001')?
89 BODYEOF: BODY);
90 c = getc(iob);
91 }
92
93 }
94 /*NOTREACHED*/
95 }