summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libuuconf/tportc.c
blob: 742440845548c74b733551e15706aea3930bfccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* tportc.c
   Handle a Taylor UUCP port command.

   Copyright (C) 1992, 1993 Ian Lance Taylor

   This file is part of the Taylor UUCP uuconf library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucnfi.h"

#if USE_RCS_ID
const char _uuconf_tportc_rcsid[] = "$Id: tportc.c,v 1.14 1995/06/21 19:24:52 ian Rel $";
#endif

#include <errno.h>

static int ipproto_param P((pointer pglobal, int argc, char **argv,
			    pointer pvar, pointer pinfo));
static int ipbaud_range P((pointer pglobal, int argc, char **argv,
			   pointer pvar, pointer pinfo));
static int ipdialer P((pointer pglobal, int argc, char **argv, pointer pvar,
		       pointer pinfo));
static int ipcunknown P((pointer pglobal, int argc, char **argv,
			 pointer pvar, pointer pinfo));

/* The string names of the port types.  This array corresponds to the
   uuconf_porttype enumeration.  */

static const char * const azPtype_names[] =
{
  NULL,
  "stdin",
  "modem",
  "direct",
  "tcp",
  "tli",
  "pipe"
};

#define CPORT_TYPES (sizeof azPtype_names / sizeof azPtype_names[0])

/* The command table for generic port commands.  The "port" and "type"
   commands are handled specially.  */
static const struct cmdtab_offset asPort_cmds[] =
{
  { "protocol", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port, uuconf_zprotocols), NULL },
  { "protocol-parameter", UUCONF_CMDTABTYPE_FN | 0,
      offsetof (struct uuconf_port, uuconf_qproto_params), ipproto_param },
  { "seven-bit", UUCONF_CMDTABTYPE_FN | 2,
      offsetof (struct uuconf_port, uuconf_ireliable), _uuconf_iseven_bit },
  { "reliable", UUCONF_CMDTABTYPE_FN | 2,
      offsetof (struct uuconf_port, uuconf_ireliable), _uuconf_ireliable },
  { "half-duplex", UUCONF_CMDTABTYPE_FN | 2,
      offsetof (struct uuconf_port, uuconf_ireliable),
      _uuconf_ihalf_duplex },
  { "lockname", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port, uuconf_zlockname), NULL },
  { NULL, 0, 0, NULL }
};

#define CPORT_CMDS (sizeof asPort_cmds / sizeof asPort_cmds[0])

/* The stdin port command table.  */
static const struct cmdtab_offset asPstdin_cmds[] =
{
  { NULL, 0, 0, NULL }
};

#define CSTDIN_CMDS (sizeof asPstdin_cmds / sizeof asPstdin_cmds[0])

/* The modem port command table.  */
static const struct cmdtab_offset asPmodem_cmds[] =
{
  { "device", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem.uuconf_zdevice),
      NULL },
  { "baud", UUCONF_CMDTABTYPE_LONG,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem.uuconf_ibaud),
      NULL },
  { "speed", UUCONF_CMDTABTYPE_LONG,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem.uuconf_ibaud),
      NULL },
  { "baud-range", UUCONF_CMDTABTYPE_FN | 3,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem), ipbaud_range },
  { "speed-range", UUCONF_CMDTABTYPE_FN | 3,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem), ipbaud_range },
  { "carrier", UUCONF_CMDTABTYPE_BOOLEAN,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem.uuconf_fcarrier),
      NULL },
  { "hardflow", UUCONF_CMDTABTYPE_BOOLEAN,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem.uuconf_fhardflow),
      NULL },
  { "dial-device", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port,
		uuconf_u.uuconf_smodem.uuconf_zdial_device),
      NULL },
  { "dialer", UUCONF_CMDTABTYPE_FN | 0,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem), ipdialer },
  { "dialer-sequence", UUCONF_CMDTABTYPE_FULLSTRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_smodem.uuconf_pzdialer),
      NULL },
  { NULL, 0, 0, NULL }
};

#define CMODEM_CMDS (sizeof asPmodem_cmds / sizeof asPmodem_cmds[0])

/* The direct port command table.  */
static const struct cmdtab_offset asPdirect_cmds[] =
{
  { "device", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_sdirect.uuconf_zdevice),
      NULL },
  { "baud", UUCONF_CMDTABTYPE_LONG,
      offsetof (struct uuconf_port, uuconf_u.uuconf_sdirect.uuconf_ibaud),
      NULL },
  { "speed", UUCONF_CMDTABTYPE_LONG,
      offsetof (struct uuconf_port, uuconf_u.uuconf_sdirect.uuconf_ibaud),
      NULL },
  { "carrier", UUCONF_CMDTABTYPE_BOOLEAN,
      offsetof (struct uuconf_port, uuconf_u.uuconf_sdirect.uuconf_fcarrier),
      NULL },
  { "hardflow", UUCONF_CMDTABTYPE_BOOLEAN,
      offsetof (struct uuconf_port, uuconf_u.uuconf_sdirect.uuconf_fhardflow),
      NULL },
  { NULL, 0, 0, NULL }
};

#define CDIRECT_CMDS (sizeof asPdirect_cmds / sizeof asPdirect_cmds[0])

/* The TCP port command table.  */
static const struct cmdtab_offset asPtcp_cmds[] =
{
  { "service", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_stcp.uuconf_zport),
      NULL },
  { "dialer-sequence", UUCONF_CMDTABTYPE_FULLSTRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_stcp.uuconf_pzdialer),
      NULL },
  { NULL, 0, 0, NULL }
};

#define CTCP_CMDS (sizeof asPtcp_cmds / sizeof asPtcp_cmds[0])

/* The TLI port command table.  */
static const struct cmdtab_offset asPtli_cmds[] =
{
  { "device", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_stli.uuconf_zdevice),
      NULL },
  { "stream", UUCONF_CMDTABTYPE_BOOLEAN,
      offsetof (struct uuconf_port, uuconf_u.uuconf_stli.uuconf_fstream),
      NULL },
  { "push", UUCONF_CMDTABTYPE_FULLSTRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_stli.uuconf_pzpush),
      NULL },
  { "dialer-sequence", UUCONF_CMDTABTYPE_FULLSTRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_stli.uuconf_pzdialer),
      NULL },
  { "server-address", UUCONF_CMDTABTYPE_STRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_stli.uuconf_zservaddr),
      NULL },
  { NULL, 0, 0, NULL }
};

#define CTLI_CMDS (sizeof asPtli_cmds / sizeof asPtli_cmds[0])

/* The pipe port command table.  */
static const struct cmdtab_offset asPpipe_cmds[] =
{
  { "command", UUCONF_CMDTABTYPE_FULLSTRING,
      offsetof (struct uuconf_port, uuconf_u.uuconf_spipe.uuconf_pzcmd),
      NULL },
  { NULL, 0, 0, NULL}
};

#define CPIPE_CMDS (sizeof asPpipe_cmds / sizeof asPpipe_cmds[0])

#undef max
#define max(i1, i2) ((i1) > (i2) ? (i1) : (i2))
#define CCMDS \
  max (max (max (CPORT_CMDS, CSTDIN_CMDS), CMODEM_CMDS), \
       max (max (CDIRECT_CMDS, CTCP_CMDS), max (CTLI_CMDS, CPIPE_CMDS)))

/* Handle a command passed to a port from a Taylor UUCP configuration
   file.  This can be called when reading either the port file or the
   sys file.  The return value may have UUCONF_CMDTABRET_KEEP set, but
   not UUCONF_CMDTABRET_EXIT.  It assigns values to the elements of
   qport.  The first time this is called, qport->uuconf_zname and
   qport->uuconf_palloc should be set and qport->uuconf_ttype should
   be UUCONF_PORTTYPE_UNKNOWN.  */

int
_uuconf_iport_cmd (qglobal, argc, argv, qport)
     struct sglobal *qglobal;
     int argc;
     char **argv;
     struct uuconf_port *qport;
{
  boolean fgottype;
  const struct cmdtab_offset *qcmds;
  size_t ccmds;
  struct uuconf_cmdtab as[CCMDS];
  int i;
  int iret;

  fgottype = strcasecmp (argv[0], "type") == 0;

  if (fgottype || qport->uuconf_ttype == UUCONF_PORTTYPE_UNKNOWN)
    {
      enum uuconf_porttype ttype;

      /* We either just got a "type" command, or this is an
	 uninitialized port.  If the first command to a port is not
	 "type", it is assumed to be a modem port.  This
	 implementation will actually permit "type" at any point, but
	 will effectively discard any type specific information that
	 appears before the "type" command.  This supports defaults,
	 in that the default may be of a specific type while future
	 ports in the same file may be of other types.  */
      if (! fgottype)
	ttype = UUCONF_PORTTYPE_MODEM;
      else
	{
	  if (argc != 2)
	    return UUCONF_SYNTAX_ERROR;

	  for (i = 0; i < CPORT_TYPES; i++)
	    if (azPtype_names[i] != NULL
		&& strcasecmp (argv[1], azPtype_names[i]) == 0)
	      break;

	  if (i >= CPORT_TYPES)
	    return UUCONF_SYNTAX_ERROR;
	  
	  ttype = (enum uuconf_porttype) i;
	}

      qport->uuconf_ttype = ttype;

      switch (ttype)
	{
	default:
	case UUCONF_PORTTYPE_STDIN:
	  break;
	case UUCONF_PORTTYPE_MODEM:
	  qport->uuconf_u.uuconf_smodem.uuconf_zdevice = NULL;
	  qport->uuconf_u.uuconf_smodem.uuconf_zdial_device = NULL;
	  qport->uuconf_u.uuconf_smodem.uuconf_ibaud = 0L;
	  qport->uuconf_u.uuconf_smodem.uuconf_ilowbaud = 0L;
	  qport->uuconf_u.uuconf_smodem.uuconf_ihighbaud = 0L;
	  qport->uuconf_u.uuconf_smodem.uuconf_fcarrier = TRUE;
	  qport->uuconf_u.uuconf_smodem.uuconf_fhardflow = TRUE;
	  qport->uuconf_u.uuconf_smodem.uuconf_pzdialer = NULL;
	  qport->uuconf_u.uuconf_smodem.uuconf_qdialer = NULL;
	  break;
	case UUCONF_PORTTYPE_DIRECT:
	  qport->uuconf_u.uuconf_sdirect.uuconf_zdevice = NULL;
	  qport->uuconf_u.uuconf_sdirect.uuconf_ibaud = -1;
	  qport->uuconf_u.uuconf_sdirect.uuconf_fcarrier = FALSE;
	  qport->uuconf_u.uuconf_sdirect.uuconf_fhardflow = TRUE;
	  break;
	case UUCONF_PORTTYPE_TCP:
	  qport->uuconf_u.uuconf_stcp.uuconf_zport = (char *) "uucp";
	  qport->uuconf_u.uuconf_stcp.uuconf_pzdialer = NULL;
	  qport->uuconf_ireliable = (UUCONF_RELIABLE_SPECIFIED
				     | UUCONF_RELIABLE_ENDTOEND
				     | UUCONF_RELIABLE_RELIABLE
				     | UUCONF_RELIABLE_EIGHT
				     | UUCONF_RELIABLE_FULLDUPLEX);
	  break;
	case UUCONF_PORTTYPE_TLI:
	  qport->uuconf_u.uuconf_stli.uuconf_zdevice = NULL;
	  qport->uuconf_u.uuconf_stli.uuconf_fstream = FALSE;
	  qport->uuconf_u.uuconf_stli.uuconf_pzpush = NULL;
	  qport->uuconf_u.uuconf_stli.uuconf_pzdialer = NULL;
	  qport->uuconf_u.uuconf_stli.uuconf_zservaddr = NULL;
	  qport->uuconf_ireliable = (UUCONF_RELIABLE_SPECIFIED
				     | UUCONF_RELIABLE_ENDTOEND
				     | UUCONF_RELIABLE_RELIABLE
				     | UUCONF_RELIABLE_EIGHT
				     | UUCONF_RELIABLE_FULLDUPLEX);
	  break;
	case UUCONF_PORTTYPE_PIPE:
	  qport->uuconf_u.uuconf_spipe.uuconf_pzcmd = NULL;
	  break;
	}

      if (fgottype)
	return UUCONF_CMDTABRET_CONTINUE;
    }

  /* See if this command is one of the generic ones.  */
  qcmds = asPort_cmds;
  ccmds = CPORT_CMDS;

  for (i = 0; i < CPORT_CMDS - 1; i++)
    if (strcasecmp (argv[0], asPort_cmds[i].zcmd) == 0)
      break;

  if (i >= CPORT_CMDS - 1)
    {
      /* It's not a generic command, so we must check the type
	 specific commands.  */
      switch (qport->uuconf_ttype)
	{
	case UUCONF_PORTTYPE_STDIN:
	  qcmds = asPstdin_cmds;
	  ccmds = CSTDIN_CMDS;
	  break;
	case UUCONF_PORTTYPE_MODEM:
	  qcmds = asPmodem_cmds;
	  ccmds = CMODEM_CMDS;
	  break;
	case UUCONF_PORTTYPE_DIRECT:
	  qcmds = asPdirect_cmds;
	  ccmds = CDIRECT_CMDS;
	  break;
	case UUCONF_PORTTYPE_TCP:
	  qcmds = asPtcp_cmds;
	  ccmds = CTCP_CMDS;
	  break;
	case UUCONF_PORTTYPE_TLI:
	  qcmds = asPtli_cmds;
	  ccmds = CTLI_CMDS;
	  break;
	case UUCONF_PORTTYPE_PIPE:
	  qcmds = asPpipe_cmds;
	  ccmds = CPIPE_CMDS;
	  break;
	default:
	  return UUCONF_SYNTAX_ERROR;
	}
    }

  /* Copy the command table onto the stack and modify it to point to
     qport.  */
  _uuconf_ucmdtab_base (qcmds, ccmds, (char *) qport, as);

  iret = uuconf_cmd_args ((pointer) qglobal, argc, argv, as,
			  (pointer) qport, ipcunknown, 0,
			  qport->uuconf_palloc);

  return iret &~ UUCONF_CMDTABRET_EXIT;
}

/* Handle the "protocol-parameter" command.  */

static int
ipproto_param (pglobal, argc, argv, pvar, pinfo)
     pointer pglobal;
     int argc;
     char **argv;
     pointer pvar;
     pointer pinfo;
{
  struct sglobal *qglobal = (struct sglobal *) pglobal;
  struct uuconf_proto_param **pqparam = (struct uuconf_proto_param **) pvar;
  struct uuconf_port *qport = (struct uuconf_port *) pinfo;

  return _uuconf_iadd_proto_param (qglobal, argc - 1, argv + 1, pqparam,
				   qport->uuconf_palloc);
}

/* Handle the "baud-range" command.  */

/*ARGSUSED*/
static int
ipbaud_range (pglobal, argc, argv, pvar, pinfo)
     pointer pglobal;
     int argc;
     char **argv;
     pointer pvar;
     pointer pinfo;
{
  struct sglobal *qglobal = (struct sglobal *) pglobal;
  struct uuconf_modem_port *qmodem = (struct uuconf_modem_port *) pvar;
  int iret;

  iret = _uuconf_iint (qglobal, argv[1],
		       (pointer) &qmodem->uuconf_ilowbaud, FALSE);
  if ((iret &~ UUCONF_CMDTABRET_KEEP) != UUCONF_SUCCESS)
    return iret;

  iret |= _uuconf_iint (qglobal, argv[2],
			(pointer) &qmodem->uuconf_ihighbaud, FALSE);

  return iret;
}

/* Handle the "dialer" command.  If there is one argument, this names
   a dialer.  Otherwise, the remaining arguments form a command
   describing the dialer.  */

static int
ipdialer (pglobal, argc, argv, pvar, pinfo)
     pointer pglobal;
     int argc;
     char **argv;
     pointer pvar;
     pointer pinfo;
{
  struct sglobal *qglobal = (struct sglobal *) pglobal;
  struct uuconf_modem_port *qmodem = (struct uuconf_modem_port *) pvar;
  struct uuconf_port *qport = (struct uuconf_port *) pinfo;
  int iret;

  if (argc < 2)
    return UUCONF_SYNTAX_ERROR | UUCONF_CMDTABRET_EXIT;

  if (argc > 2)
    {
      if (qmodem->uuconf_qdialer == NULL)
	{
	  struct uuconf_dialer *qnew;
	  size_t clen;

	  qnew = ((struct uuconf_dialer *)
		  uuconf_malloc (qport->uuconf_palloc,
				  sizeof (struct uuconf_dialer)));
	  if (qnew == NULL)
	    {
	      qglobal->ierrno = errno;
	      return (UUCONF_MALLOC_FAILED
		      | UUCONF_ERROR_ERRNO
		      | UUCONF_CMDTABRET_EXIT);
	    }

	  _uuconf_uclear_dialer (qnew);

	  if (qport->uuconf_zname == NULL)
	    qnew->uuconf_zname = (char *) "default port file dialer";
	  else
	    {
	      clen = strlen (qport->uuconf_zname);
	      qnew->uuconf_zname =
		(char *) uuconf_malloc (qport->uuconf_palloc,
					clen + sizeof " dialer");
	      if (qnew->uuconf_zname == NULL)
		{
		  qglobal->ierrno = errno;
		  return (UUCONF_MALLOC_FAILED
			  | UUCONF_ERROR_ERRNO
			  | UUCONF_CMDTABRET_EXIT);
		}

	      memcpy ((pointer) qnew->uuconf_zname,
		      (pointer) qport->uuconf_zname, clen);
	      memcpy ((pointer) (qnew->uuconf_zname + clen),
		      (pointer) " dialer", sizeof " dialer");
	    }

	  qnew->uuconf_palloc = qport->uuconf_palloc;

	  qmodem->uuconf_qdialer = qnew;
	}

      iret = _uuconf_idialer_cmd (qglobal, argc - 1, argv + 1,
				  qmodem->uuconf_qdialer);
      if ((iret &~ UUCONF_CMDTABRET_KEEP) != UUCONF_SUCCESS)
	iret |= UUCONF_CMDTABRET_EXIT;
      return iret;
    }
  else
    {
      qmodem->uuconf_pzdialer = NULL;
      iret = _uuconf_iadd_string (qglobal, argv[1], TRUE, FALSE,
				  &qmodem->uuconf_pzdialer,
				  qport->uuconf_palloc);
      if (iret != UUCONF_SUCCESS)
	iret |= UUCONF_CMDTABRET_EXIT;
      return iret;
    }
}

/* Give an error for an unknown port command.  */

/*ARGSUSED*/
static int
ipcunknown (pglobal, argc, argv, pvar, pinfo)
     pointer pglobal;
     int argc;
     char **argv;
     pointer pvar;
     pointer pinfo;
{
  return UUCONF_SYNTAX_ERROR | UUCONF_CMDTABRET_EXIT;
}
OpenPOWER on IntegriCloud