summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libuuconf/syssub.c
blob: 81b0dd84e65124acdefaef97193dbd4e686e65be (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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* syssub.c
   System information subroutines.

   Copyright (C) 1992, 1993, 1995 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_syssub_rcsid[] = "$FreeBSD$";
#endif

#include <errno.h>

/* This macro operates on every string (char *) field in struct
   uuconf_system.  */
#define SYSTEM_STRINGS(OP) \
  do \
    { \
      OP (uuconf_zname); \
      OP (uuconf_zalternate); \
      OP (uuconf_zdebug); \
      OP (uuconf_zmax_remote_debug); \
      OP (uuconf_zphone); \
      OP (uuconf_zcall_login); \
      OP (uuconf_zcall_password); \
      OP (uuconf_zcalled_login); \
      OP (uuconf_zprotocols); \
      OP (uuconf_zpubdir); \
      OP (uuconf_zlocalname); \
    } \
  while (0)

/* This macro operates on every string array (char **) field in struct
   uuconf_system.  */
#define SYSTEM_STRING_ARRAYS(OP) \
  do \
    { \
      OP (uuconf_pzalias); \
      OP (uuconf_pzlocal_send); \
      OP (uuconf_pzremote_send); \
      OP (uuconf_pzlocal_receive); \
      OP (uuconf_pzremote_receive); \
      OP (uuconf_pzpath); \
      OP (uuconf_pzcmds); \
      OP (uuconf_pzforward_from); \
      OP (uuconf_pzforward_to); \
      OP (uuconf_schat.uuconf_pzchat); \
      OP (uuconf_schat.uuconf_pzprogram); \
      OP (uuconf_schat.uuconf_pzfail); \
      OP (uuconf_scalled_chat.uuconf_pzchat); \
      OP (uuconf_scalled_chat.uuconf_pzprogram); \
      OP (uuconf_scalled_chat.uuconf_pzfail); \
    } \
  while (0)

/* This macro operations on every timespan pointer (struct
   uuconf_timespan *) in struct uuconf_system.  */
#define SYSTEM_TIMESPANS(OP) \
  do \
    { \
      OP (uuconf_qtimegrade); \
      OP (uuconf_qcalltimegrade); \
      OP (uuconf_qcalledtimegrade); \
      OP (uuconf_qcall_local_size); \
      OP (uuconf_qcall_remote_size); \
      OP (uuconf_qcalled_local_size); \
      OP (uuconf_qcalled_remote_size); \
    } \
  while (0)

/* This macro operates on every boolean value (of type int, although
   some type int are not boolean) field in uuconf_system.  */
#define SYSTEM_BOOLEANS(OP) \
  do \
    { \
      OP (uuconf_fcall); \
      OP (uuconf_fcalled); \
      OP (uuconf_fcallback); \
      OP (uuconf_fsequence); \
      OP (uuconf_fsend_request); \
      OP (uuconf_frec_request); \
      OP (uuconf_fcall_transfer); \
      OP (uuconf_fcalled_transfer); \
      OP (uuconf_schat.uuconf_fstrip); \
      OP (uuconf_scalled_chat.uuconf_fstrip); \
    } \
  while (0)

/* This macro operates on every generic integer (type int or long) in
   uuconf_system.  */
#define SYSTEM_INTEGERS(OP) \
  do \
    { \
      OP (uuconf_cmax_retries); \
      OP (uuconf_csuccess_wait); \
      OP (uuconf_ibaud); \
      OP (uuconf_ihighbaud); \
      OP (uuconf_cfree_space); \
      OP (uuconf_schat.uuconf_ctimeout); \
      OP (uuconf_scalled_chat.uuconf_ctimeout); \
    } \
  while (0)

/* There is no macro for uuconf_qalternate, uuconf_zport,
   uuconf_qport, uuconf_qproto_params, or uuconf_palloc.  */

/* Clear the contents of a struct uuconf_system.  */

void
_uuconf_uclear_system (q)
     struct uuconf_system *q;
{
#define CLEAR(x) q->x = (char *) &_uuconf_unset
  SYSTEM_STRINGS (CLEAR);
#undef CLEAR
#define CLEAR(x) q->x = (char **) &_uuconf_unset
  SYSTEM_STRING_ARRAYS (CLEAR);
#undef CLEAR
#define CLEAR(x) q->x = (struct uuconf_timespan *) &_uuconf_unset
  SYSTEM_TIMESPANS (CLEAR);
#undef CLEAR
#define CLEAR(x) q->x = -1
  SYSTEM_BOOLEANS (CLEAR);
  SYSTEM_INTEGERS (CLEAR);
#undef CLEAR
  q->uuconf_qalternate = NULL;
  q->uuconf_zport = (char *) &_uuconf_unset;
  q->uuconf_qport = (struct uuconf_port *) &_uuconf_unset;
  q->uuconf_qproto_params = (struct uuconf_proto_param *) &_uuconf_unset;
  q->uuconf_palloc = NULL;
}

/* Default the contents of one struct uuconf_system to the contents of
   another.  This default alternate by alternate.  Any additional
   alternates in q default to the last alternate of qdefault.  If the
   faddalternates arguments is TRUE, additional alternates or qdefault
   are added to q; these alternates are copies of the first alternate
   of q, and defaults are set from the additional alternates of
   qdefault.  */

int
_uuconf_isystem_default (qglobal, qset, qdefault, faddalternates)
     struct sglobal *qglobal;
     struct uuconf_system *qset;
     struct uuconf_system *qdefault;
     boolean faddalternates;
{
  struct uuconf_system *qalt;

  if (qset->uuconf_palloc != qdefault->uuconf_palloc)
    qset->uuconf_palloc =
      _uuconf_pmalloc_block_merge (qset->uuconf_palloc,
				   qdefault->uuconf_palloc);

  /* If we are adding alternates from the default, make sure we have
     at least as many alternates in qset as we do in qdefault.  Each
     new alternate we create gets initialized to the first alternate
     of the system.  */
  if (faddalternates)
    {
      struct uuconf_system **pq, *qdef;

      for (qdef = qdefault, pq = &qset;
	   qdef != NULL;
	   qdef = qdef->uuconf_qalternate, pq = &(*pq)->uuconf_qalternate)
	{
	  if (*pq == NULL)
	    {
	      *pq = ((struct uuconf_system *)
		     uuconf_malloc (qset->uuconf_palloc,
				    sizeof (struct uuconf_system)));
	      if (*pq == NULL)
		{
		  qglobal->ierrno = errno;
		  return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
		}
	      **pq = *qset;
	      (*pq)->uuconf_qalternate = NULL;
	    }
	}
    }

  for (qalt = qset; qalt != NULL; qalt = qalt->uuconf_qalternate)
    {
#define DEFAULT(x) \
  if (qalt->x == (char *) &_uuconf_unset) qalt->x = qdefault->x
      SYSTEM_STRINGS (DEFAULT);
#undef DEFAULT
#define DEFAULT(x) \
  if (qalt->x == (char **) &_uuconf_unset) qalt->x = qdefault->x
      SYSTEM_STRING_ARRAYS (DEFAULT);
#undef DEFAULT
#define DEFAULT(x) \
  if (qalt->x == (struct uuconf_timespan *) &_uuconf_unset) \
    qalt->x = qdefault->x
      SYSTEM_TIMESPANS (DEFAULT);
#undef DEFAULT
#define DEFAULT(x) if (qalt->x < 0) qalt->x = qdefault->x
      SYSTEM_BOOLEANS (DEFAULT);
      SYSTEM_INTEGERS (DEFAULT);
#undef DEFAULT

      /* We only copy over zport if both zport and qport are NULL,
	 because otherwise a default zport would override a specific
	 qport.  */
      if (qalt->uuconf_zport == (char *) &_uuconf_unset
	  && qalt->uuconf_qport == (struct uuconf_port *) &_uuconf_unset)
	qalt->uuconf_zport = qdefault->uuconf_zport;
      if (qalt->uuconf_qport == (struct uuconf_port *) &_uuconf_unset)
	qalt->uuconf_qport = qdefault->uuconf_qport;

      if (qalt->uuconf_qproto_params
	  == (struct uuconf_proto_param *) &_uuconf_unset)
	qalt->uuconf_qproto_params = qdefault->uuconf_qproto_params;
      else if (qdefault->uuconf_qproto_params != NULL)
	{
	  int cnew, ca;
	  struct uuconf_proto_param *qd, *qa;

	  /* Merge in the default protocol parameters, so that a
	     system with 'g' protocol parameters won't lose the
	     default 'i' protocol parameters.  */
	  ca = 0;
	  cnew = 0;
	  for (qd = qdefault->uuconf_qproto_params;
	       qd->uuconf_bproto != '\0';
	       qd++)
	    {
	      int c;

	      c = 0;
	      for (qa = qalt->uuconf_qproto_params;
		   (qa->uuconf_bproto != '\0'
		    && qa->uuconf_bproto != qd->uuconf_bproto);
		   qa++)
		++c;
	      if (qa->uuconf_bproto == '\0')
		{
		  ++cnew;
		  ca = c;
		}
	    }

	  if (cnew > 0)
	    {
	      struct uuconf_proto_param *qnew;

	      qnew = ((struct uuconf_proto_param *)
		      uuconf_malloc (qset->uuconf_palloc,
				     ((ca + cnew + 1)
				      * sizeof (struct uuconf_proto_param))));
	      if (qnew == NULL)
		{
		  qglobal->ierrno = errno;
		  return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
		}
	      memcpy ((pointer) qnew, (pointer) qalt->uuconf_qproto_params,
		      ca * sizeof (struct uuconf_proto_param));
	      cnew = 0;
	      for (qd = qdefault->uuconf_qproto_params;
		   qd->uuconf_bproto != '\0';
		   qd++)
		{
		  for (qa = qalt->uuconf_qproto_params;
		       (qa->uuconf_bproto != '\0'
			&& qa->uuconf_bproto != qd->uuconf_bproto);
		       qa++)
		    ;
		  if (qa->uuconf_bproto == '\0')
		    {
		      qnew[ca + cnew] = *qd;
		      ++cnew;
		    }
		}
	      qnew[ca + cnew].uuconf_bproto = '\0';
	      uuconf_free (qset->uuconf_palloc, qalt->uuconf_qproto_params);
	      qalt->uuconf_qproto_params = qnew;
	    }
	}

      if (qdefault->uuconf_qalternate != NULL)
	qdefault = qdefault->uuconf_qalternate;
    }

  return UUCONF_SUCCESS;
}

/* Put in the basic defaults.  This ensures that the fields are valid
   on every uuconf_system structure.  */

int
_uuconf_isystem_basic_default (qglobal, q)
     struct sglobal *qglobal;
     register struct uuconf_system *q;
{
  int iret;

  iret = UUCONF_SUCCESS;

  for (; q != NULL && iret == UUCONF_SUCCESS; q = q->uuconf_qalternate)
    {
      /* The default of 26 allowable retries is traditional.  */
      if (q->uuconf_cmax_retries < 0)
	q->uuconf_cmax_retries = 26;
      if (q->uuconf_schat.uuconf_pzchat == (char **) &_uuconf_unset)
	{
	  q->uuconf_schat.uuconf_pzchat = NULL;
	  iret = _uuconf_iadd_string (qglobal, (char *) "\"\"", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "\\r\\c", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "ogin:", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "-BREAK", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "-ogin:", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "-BREAK", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "-ogin:", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "\\L", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "word:", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	  iret = _uuconf_iadd_string (qglobal, (char *) "\\P", FALSE,
				      FALSE,
				      &q->uuconf_schat.uuconf_pzchat,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	}
      if (q->uuconf_schat.uuconf_ctimeout < 0)
	q->uuconf_schat.uuconf_ctimeout = 10;
      if (q->uuconf_schat.uuconf_fstrip < 0)
	q->uuconf_schat.uuconf_fstrip = TRUE;
      if (q->uuconf_scalled_chat.uuconf_ctimeout < 0)
	q->uuconf_scalled_chat.uuconf_ctimeout = 60;
      if (q->uuconf_scalled_chat.uuconf_fstrip < 0)
	q->uuconf_scalled_chat.uuconf_fstrip = TRUE;
      if (q->uuconf_fsend_request < 0)
	q->uuconf_fsend_request = TRUE;
      if (q->uuconf_frec_request < 0)
	q->uuconf_frec_request = TRUE;
      if (q->uuconf_fcall_transfer < 0)
	q->uuconf_fcall_transfer = TRUE;
      if (q->uuconf_fcalled_transfer < 0)
	q->uuconf_fcalled_transfer = TRUE;
      if (q->uuconf_pzlocal_send == (char **) &_uuconf_unset)
	{
	  q->uuconf_pzlocal_send = NULL;
	  iret = _uuconf_iadd_string (qglobal, (char *) ZROOTDIR, FALSE,
				      FALSE, &q->uuconf_pzlocal_send,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	}
      if (q->uuconf_pzremote_send == (char **) &_uuconf_unset)
	{
	  q->uuconf_pzremote_send = NULL;
	  iret = _uuconf_iadd_string (qglobal, (char *) "~", FALSE, FALSE,
				      &q->uuconf_pzremote_send,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	}
      if (q->uuconf_pzlocal_receive == (char **) &_uuconf_unset)
	{
	  q->uuconf_pzlocal_receive = NULL;
	  iret = _uuconf_iadd_string (qglobal, (char *) "~", FALSE, FALSE,
				      &q->uuconf_pzlocal_receive,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	}
      if (q->uuconf_pzremote_receive == (char **) &_uuconf_unset)
	{
	  q->uuconf_pzremote_receive = NULL;
	  iret = _uuconf_iadd_string (qglobal, (char *) "~", FALSE, FALSE,
				      &q->uuconf_pzremote_receive,
				      q->uuconf_palloc);
	  if (iret != UUCONF_SUCCESS)
	    return iret;
	}

      if (q->uuconf_pzpath == (char **) &_uuconf_unset)
	{
	  char *zdup;
	  char **pz;
	  size_t csplit;
	  int c;

	  zdup = (char *) uuconf_malloc (q->uuconf_palloc, sizeof CMDPATH);
	  if (zdup == NULL)
	    {
	      qglobal->ierrno = errno;
	      return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	    }
		  
	  memcpy ((pointer) zdup, (pointer) CMDPATH, sizeof CMDPATH);
	  pz = NULL;
	  csplit = 0;
	  if ((c = _uuconf_istrsplit (zdup, '\0', &pz, &csplit)) < 0)
	    {
	      qglobal->ierrno = errno;
	      return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	    }
	  q->uuconf_pzpath = (char **) uuconf_malloc (q->uuconf_palloc,
						      ((c + 1)
						       * sizeof (char *)));
	  if (q->uuconf_pzpath == NULL)
	    {
	      qglobal->ierrno = errno;
	      return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	    }
	  memcpy ((pointer) q->uuconf_pzpath, (pointer) pz,
		  c * sizeof (char *));
	  q->uuconf_pzpath[c] = NULL;
	  free ((pointer) pz);
	}

      if (q->uuconf_pzcmds == (char **) &_uuconf_unset)
	{
	  q->uuconf_pzcmds = ((char **)
			      uuconf_malloc (q->uuconf_palloc,
					     3 * sizeof (const char *)));
	  if (q->uuconf_pzcmds == NULL)
	    {
	      qglobal->ierrno = errno;
	      return UUCONF_MALLOC_FAILED | UUCONF_ERROR_ERRNO;
	    }
	  q->uuconf_pzcmds[0] = (char *) "rnews";
	  q->uuconf_pzcmds[1] = (char *) "rmail";
	  q->uuconf_pzcmds[2] = NULL;
	}

      if (q->uuconf_cfree_space < 0)
	q->uuconf_cfree_space = DEFAULT_FREE_SPACE;

      if (q->uuconf_zpubdir == (const char *) &_uuconf_unset)
	q->uuconf_zpubdir = qglobal->qprocess->zpubdir;

#define SET(x) if (q->x == (char *) &_uuconf_unset) q->x = NULL
      SYSTEM_STRINGS(SET);
#undef SET
#define SET(x) if (q->x == (char **) &_uuconf_unset) q->x = NULL
      SYSTEM_STRING_ARRAYS(SET);
#undef SET
#define SET(x) \
  if (q->x == (struct uuconf_timespan *) &_uuconf_unset) q->x = NULL
      SYSTEM_TIMESPANS (SET);
#undef SET
#define SET(x) if (q->x < 0) q->x = 0
      SYSTEM_BOOLEANS (SET);
      SYSTEM_INTEGERS (SET);
#undef SET

      if (q->uuconf_zport == (char *) &_uuconf_unset)
	q->uuconf_zport = NULL;
      if (q->uuconf_qport == (struct uuconf_port *) &_uuconf_unset)
	q->uuconf_qport = NULL;
      if (q->uuconf_qproto_params
	  == (struct uuconf_proto_param *) &_uuconf_unset)
	q->uuconf_qproto_params = NULL;
    }

  return iret;
}
OpenPOWER on IntegriCloud