Commit | Line | Data |
---|---|---|
9e8e5516 C |
1 | /* psaptest.c - test out -lpsap */ |
2 | ||
3 | #ifndef lint | |
4 | static char *rcsid = "$Header: /f/osi/psap/RCS/psaptest.c,v 7.2 91/02/22 09:36:40 mrose Interim $"; | |
5 | #endif | |
6 | ||
7 | /* | |
8 | * $Header: /f/osi/psap/RCS/psaptest.c,v 7.2 91/02/22 09:36:40 mrose Interim $ | |
9 | * | |
10 | * | |
11 | * $Log: psaptest.c,v $ | |
12 | * Revision 7.2 91/02/22 09:36:40 mrose | |
13 | * Interim 6.8 | |
14 | * | |
15 | * Revision 7.1 90/08/29 15:06:21 mrose | |
16 | * update | |
17 | * | |
18 | * Revision 7.0 89/11/23 22:13:26 mrose | |
19 | * Release 6.0 | |
20 | * | |
21 | */ | |
22 | ||
23 | /* | |
24 | * NOTICE | |
25 | * | |
26 | * Acquisition, use, and distribution of this module and related | |
27 | * materials are subject to the restrictions of a license agreement. | |
28 | * Consult the Preface in the User's Manual for the full terms of | |
29 | * this agreement. | |
30 | * | |
31 | */ | |
32 | ||
33 | ||
34 | #include <stdio.h> | |
35 | #include "psap.h" | |
36 | #include "tailor.h" | |
37 | #include <sys/stat.h> | |
38 | ||
39 | /* \f */ | |
40 | ||
41 | static enum { | |
42 | ps2pl, ps2ps, pl2pl, pl2ps | |
43 | } mode; | |
44 | ||
45 | /* \f */ | |
46 | ||
47 | /* ARGSUSED */ | |
48 | ||
49 | main (argc, argv, envp) | |
50 | int argc; | |
51 | char **argv, | |
52 | **envp; | |
53 | { | |
54 | char *cp; | |
55 | register PE pe; | |
56 | register PS ps; | |
57 | ||
58 | argc--, argv++; | |
59 | if (argc != 1) { | |
60 | usage: ; | |
61 | fprintf (stderr, "usage: psaptest [ps2pl | ps2ps | pl2pl | pl2ps]\n"); | |
62 | exit (1); | |
63 | } | |
64 | ||
65 | if (strcmp (*argv, "version") == 0) { | |
66 | printf ("%s\n", psapversion); | |
67 | exit (0); | |
68 | } | |
69 | ||
70 | if ((cp = getenv ("PSAPTEST")) && *cp) { | |
71 | psap_log -> ll_events = atoi (cp); | |
72 | psap_log -> ll_stat |= LLOGTTY; | |
73 | } | |
74 | ||
75 | if (strcmp (*argv, "string") == 0) { | |
76 | int onceonly = 1; | |
77 | register PE *pep; | |
78 | ||
79 | if ((pe = pe_alloc (PE_CLASS_UNIV, PE_FORM_CONS, PE_PRIM_OCTS)) | |
80 | == NULLPE) { | |
81 | no_pe: ; | |
82 | fprintf (stderr, "pe_alloc: you lose\n"); | |
83 | exit (1); | |
84 | } | |
85 | pep = &pe -> pe_cons; | |
86 | for (;;) { | |
87 | int i; | |
88 | register PE p; | |
89 | ||
90 | if (onceonly) { | |
91 | struct stat st; | |
92 | ||
93 | if (fstat (fileno (stdin), &st) == NOTOK | |
94 | || (st.st_mode & S_IFMT) != S_IFREG | |
95 | || (i = st.st_size) <= 0) | |
96 | i = BUFSIZ; | |
97 | ||
98 | onceonly = 0; | |
99 | } | |
100 | else | |
101 | i = BUFSIZ; | |
102 | ||
103 | if ((p = pe_alloc (PE_CLASS_UNIV, PE_FORM_PRIM, PE_PRIM_OCTS)) | |
104 | == NULL | |
105 | || (p -> pe_prim = PEDalloc (p -> pe_len = i)) == NULLPED) | |
106 | goto no_pe; | |
107 | ||
108 | switch (i = fread ((char *) p -> pe_prim, sizeof *p -> pe_prim, | |
109 | (int) p -> pe_len, stdin)) { | |
110 | case NOTOK: | |
111 | perror ("fread"); | |
112 | exit (1); | |
113 | ||
114 | case OK: | |
115 | pe_free (p); | |
116 | mode = ps2ps; | |
117 | goto doit; | |
118 | ||
119 | default: | |
120 | p -> pe_len = i; | |
121 | *pep = p, pep = &p -> pe_next; | |
122 | break; | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | if (strcmp (*argv, "binary") == 0) { | |
128 | int i; | |
129 | char buffer[BUFSIZ], | |
130 | packet[BUFSIZ]; | |
131 | ||
132 | while (fgets (buffer, sizeof buffer, stdin)) { | |
133 | if (*buffer == ' ') | |
134 | (void) strncpy (packet, buffer + 1, i = strlen (buffer) - 2); | |
135 | else | |
136 | i = implode ((u_char *) packet, buffer, strlen (buffer) - 1); | |
137 | (void) fwrite (packet, sizeof *packet, i, stdout); | |
138 | } | |
139 | ||
140 | exit (0); | |
141 | } | |
142 | ||
143 | if (strcmp (*argv, "ps2pl") == 0) | |
144 | mode = ps2pl; | |
145 | else | |
146 | if (strcmp (*argv, "ps2ps") == 0) | |
147 | mode = ps2ps; | |
148 | else | |
149 | if (strcmp (*argv, "pl2pl") == 0) | |
150 | mode = pl2pl; | |
151 | else | |
152 | if (strcmp (*argv, "pl2ps") == 0) | |
153 | mode = pl2ps; | |
154 | else | |
155 | goto usage; | |
156 | ||
157 | for (;;) { | |
158 | if ((ps = ps_alloc (std_open)) == NULLPS) { | |
159 | fprintf (stderr, "ps_alloc(stdin): you lose\n"); | |
160 | exit (1); | |
161 | } | |
162 | if (std_setup (ps, stdin) == NOTOK) | |
163 | ps_die (ps, "std_setup(stdin)"); | |
164 | ||
165 | switch (mode) { | |
166 | case ps2pl: | |
167 | case ps2ps: | |
168 | if ((pe = ps2pe (ps)) == NULLPE) | |
169 | if (ps -> ps_errno) | |
170 | ps_die (ps, "ps2pe"); | |
171 | else | |
172 | exit (0); | |
173 | break; | |
174 | ||
175 | case pl2ps: | |
176 | case pl2pl: | |
177 | if ((pe = pl2pe (ps)) == NULLPE) | |
178 | if (ps -> ps_errno) | |
179 | ps_die (ps, "pl2pe"); | |
180 | else | |
181 | exit (0); | |
182 | break; | |
183 | } | |
184 | ||
185 | ps_free (ps); | |
186 | ||
187 | doit: ; | |
188 | if ((ps = ps_alloc (std_open)) == NULLPS) { | |
189 | fprintf (stderr, "ps_alloc(stdout): you lose\n"); | |
190 | exit (1); | |
191 | } | |
192 | if (std_setup (ps, stdout) == NOTOK) | |
193 | ps_die (ps, "std_setup(stdout)"); | |
194 | ||
195 | switch (mode) { | |
196 | case ps2ps: | |
197 | case pl2ps: | |
198 | if (pe2ps (ps, pe) == NOTOK) | |
199 | ps_die (ps, "pe2ps"); | |
200 | break; | |
201 | ||
202 | case pl2pl: | |
203 | case ps2pl: | |
204 | if (pe2pl (ps, pe) == NOTOK) | |
205 | ps_die (ps, "pe2pl"); | |
206 | break; | |
207 | } | |
208 | ||
209 | pe_free (pe); | |
210 | ps_free (ps); | |
211 | } | |
212 | } | |
213 | ||
214 | /* \f ERRORS */ | |
215 | ||
216 | static ps_die (ps, s) | |
217 | register PS ps; | |
218 | register char *s; | |
219 | { | |
220 | fprintf (stderr, "%s: %s\n", s, ps_error (ps -> ps_errno)); | |
221 | exit (1); | |
222 | } |