summaryrefslogtreecommitdiff
path: root/web/funnelAC/sources/option.c
blob: 19d31b6123a9b586ac6999e07eb6b32b2d1cf31e (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
/*##############################################################################

FUNNNELWEB COPYRIGHT
====================
FunnelWeb is a literate-programming macro preprocessor.

Copyright (C) 1992 Ross N. Williams.

   Ross N. Williams
   ross@spam.adelaide.edu.au
   16 Lerwick Avenue, Hazelwood Park 5066, Australia.

This program is free software; you can redistribute it and/or modify
it under the terms of Version 2 of the GNU General Public License as
published by the Free Software Foundation.

This program is distributed WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See Version 2 of the GNU General Public License for more details.

You should have received a copy of Version 2 of the GNU General Public
License along with this program. If not, you can FTP the license from
prep.ai.mit.edu/pub/gnu/COPYING-2 or write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Section 2a of the license requires that all changes to this file be
recorded prominently in this file. Please record all changes here.

Programmers:
   RNW  Ross N. Williams  ross@spam.adelaide.edu.au

Changes:
   07-May-1992  RNW  Program prepared for release under GNU GPL V2.

##############################################################################*/


/******************************************************************************/
/*                                  OPTION.C                                  */
/******************************************************************************/

#include <ctype.h>
#include "style.h"

#include "as.h"
#include "data.h"
#include "machin.h"
#include "misc.h"
#include "option.h"

/******************************************************************************/

LOCAL void wl_caret P_((ulong,void (*)(char *)));
LOCAL void wl_caret(n,p_outf)
/* Writes a caret at position n of an otherwise blank line. */
ulong n;
void  (*p_outf) P_((char *));
{
 as_cold(n<1000,"wl_caret: Argument is out of range.");
 while (--n > 0)
    (*p_outf)(" ");
 (*p_outf)("^\n");
}

/******************************************************************************/

EXPORT void op_ini(p_op)
p_op_t p_op;
{
 p_op->op_f_b=FALSE;
 strcpy(&p_op->op_f_s[0],"");

 p_op->op_j_b=FALSE;
 strcpy(&p_op->op_j_s[0],"");

 strcpy(&p_op->op_i_s[0],"");

 p_op->op_o_b=TRUE;
 strcpy(&p_op->op_o_s[0],"");

 p_op->op_t_b=FALSE;
 strcpy(&p_op->op_t_s[0],"");

 p_op->op_l_b=TRUE;
 strcpy(&p_op->op_l_s[0],"");

 p_op->op_d_b=FALSE;

 p_op->op_c_i=2;

 p_op->op_q_b=FALSE;

 p_op->op_s_b=FALSE;
 p_op->op_s_i=1;

 p_op->op_w_b=FALSE;
 p_op->op_w_i=80;

 p_op->op_x_b=FALSE;
 strcpy(&p_op->op_x_s[0],"");

 p_op->op_k_b=FALSE;

 p_op->op_b1_b=FALSE;
 p_op->op_b2_b=FALSE;
 p_op->op_b3_b=FALSE;
 p_op->op_b4_b=FALSE;
 p_op->op_b5_b=FALSE;
 p_op->op_b6_b=FALSE;
 p_op->op_b7_b=FALSE;
 
 p_op->op_h_b=FALSE;
 strcpy(&p_op->op_h_s[0],"menu");
}

/******************************************************************************/

EXPORT bool op_add(p_op,p_cl,p_outf)
p_op_t p_op;
p_cl_t p_cl;
void   (*p_outf) P_((char *));
{
 op_t  op_save;                  /* Saves *p_op so it can be restored later.  */
 char *comline = (char *) p_cl;  /* Pointer to the command line to be parsed. */
 char *p;                        /* Handy parsing pointer.                    */
 bool seen_error = FALSE;        /* TRUE iff one or more errors seen.         */

#define ECHO_CL {if (!seen_error)\
                   {(*p_outf)(comline);(*p_outf)("\n"); seen_error=TRUE;}}

 /* Save the parameter option structure so that we can restore it */
 /* if an error occurs later on.                                  */
 ASSIGN(op_save,*p_op);

 /* Check to make sure that the command line is not too long. */
 /* This error should really never occur.                     */
 p=comline;
 while (*p++ != EOS)
    if ((p-comline) > COMLINE_FWMAX)
      {
       (*p_outf)("FunnelWeb command line syntax error.\n");
       (*p_outf)("The entire command line is too long.\n");
       sprintf(linet1,"The maximum command line length is %u characters.\n",
              (unsigned) COMLINE_FWMAX);
       (*p_outf)(linet1);
       goto failure;
      }

 /* Check that the command line contains only printables. */
 p=comline;
 while (*p != EOS)
   {
    if (!isascprn(*p))
      {
       (*p_outf)("FunnelWeb command line syntax error.\n");
       (*p_outf)("Command line contains one or more non-printables.\n");
       sprintf(linet1,"The first is at column %u.\n",(unsigned) (1+p-comline));
       (*p_outf)(linet1);
       goto failure;
      }
    p++;
   }

 /* Now parse each option. */
 p=comline;
 while (TRUE)
   {/* This loop processes one command line parameter (option) per iteration. */
    char *p_startopt;    /* Points to the start of the parameter.             */
    bool opsign;           /* The sign of the current option (+=TRUE).          */
    bool opsign_active;    /* TRUE => (+ or -). FALSE => (=).                   */
    char letter;         /* Letter of current option.                         */
    cl_t cl_temp;        /* Parameter string of the current option.           */
    fn_t fn_temp;        /* Parameter string of the current option.           */
    int  i;              /* Handy int used to feed sscanf.                    */

    /* Skip first parameter or rest of previous parameter after syntax error. */
    while ((*p!=' ') && (*p!=EOS)) p++;

    /* Skip blanks between parameters. */
    while (*p==' ') p++;

    /* At this point we are either at the start of a parameter or at EOS.     */
    /* Finish if we are at the EOS.                                           */
    if (*p==EOS) break;

    /* p now points to the next parameter (option). Make a note of its start. */
    p_startopt=p;

    /* This switch statement parses the sign and letter characters.           */
    switch (*p)
      {
       case '-': opsign=FALSE; opsign_active=TRUE;  p++; goto parse_letter;
       case '+': opsign=TRUE;  opsign_active=TRUE;  p++; goto parse_letter;
       case '=':             opsign_active=FALSE; p++; goto parse_letter;
          parse_letter:
          /* We have seen a sign. There should be a following letter. */
          letter = *p;
          if (!isalpha(letter))
            {
     ECHO_CL; wl_caret(p-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("Option letter expected here.\n");
             continue; /* Skip to the next parameter. */
            }
          p++;
          break;
       default: /* If no sign the option is a NAME option and defaults to +F. */
          opsign=TRUE; opsign_active=TRUE; letter='F'; break;
      } /* End switch */

    /* Assert: p points to the parameter string of the option.      */
    /* Copy the parameter string into the holding variable cl_temp. */
    {char *q;
     q = &cl_temp[0];
     while ((*p!=EOS) && (*p!=' '))
        *q++ = *p++;
     *q=EOS;
    }

    /* As COMLINE_FWMAX can be greater than FILENAME_FWMAX, we have to be carefu*/
    /* that the user is not about to blow our filename buffer.                */
    if (strlen(cl_temp) > FILENAME_FWMAX)
      {
       ECHO_CL; wl_caret(p_startopt-comline,p_outf);
       (*p_outf)("FunnelWeb command line syntax error.\n");
       (*p_outf)("Option parameter string is too long.\n");
       sprintf(linet1,"The maximum option parameter length is %u characters.\n",
              (unsigned) FILENAME_FWMAX);
       (*p_outf)(linet1);
       continue; /* Skip to next parameter. */
      }

    /* Now that we know that the parameter is not too big, we can copy it     */
    /* into a temporary variable of filename type.                            */
    strcpy(&fn_temp[0],&cl_temp[0]);

    /* At this point p points to the character following the option that      */
    /* we have just scanned. This is either a blank or an EOS.                */
    /* The current option is held in (opsign,opsign_active,letter,fn_temp).       */
    /* Now we apply the option to the option record.                          */
    switch (toupper(letter))
      {
       case 'F':
          if (opsign_active) p_op->op_f_b=opsign;
          strcpy(&p_op->op_f_s[0],&fn_temp[0]);
          break;
       case 'J':
          if (opsign_active) p_op->op_j_b=opsign;
          strcpy(&p_op->op_j_s[0],&fn_temp[0]);
          break;
       case 'I':
          strcpy(&p_op->op_i_s[0],&fn_temp[0]);
          break;
       case 'O':
          if (opsign_active) p_op->op_o_b=opsign;
          strcpy(&p_op->op_o_s[0],&fn_temp[0]);
          break;
       case 'T':
          if (opsign_active) p_op->op_t_b=opsign;
          strcpy(&p_op->op_t_s[0],&fn_temp[0]);
          break;
       case 'L':
          if (opsign_active) p_op->op_l_b=opsign;
          strcpy(&p_op->op_l_s[0],&fn_temp[0]);
          break;
       case 'D':
          if (opsign_active) p_op->op_d_b=opsign;
          if (strlen(fn_temp)>0)
            {
     ECHO_CL; wl_caret(3+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("The D option does not take an argument.\n");
            }
          break;
       case 'C':
          if ((sscanf(&fn_temp[0],"%d",&i) != 1) || (i<0) || (100<i))
            {
     ECHO_CL; wl_caret(3+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("Numeric argument to C option invalid. It must\n");
             (*p_outf)("be an unsigned decimal integer in the range [0,100].\n");
            }
          else
             p_op->op_c_i=i;
          break;
       case 'Q':
          if (opsign_active) p_op->op_q_b=opsign;
          if (strlen(fn_temp)>0)
            {
     ECHO_CL; wl_caret(3+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("The Q option does not take an argument.\n");
            }
          break;
       case 'S':
          if (opsign_active) p_op->op_s_b=opsign;
          if (strlen(fn_temp)==0) break;
          if ((sscanf(&fn_temp[0],"%d",&i) != 1) || (i<0) || (100<i))
            {
     ECHO_CL; wl_caret(2+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("Numeric argument to S option invalid. It must\n");
             (*p_outf)("be an unsigned decimal integer in the range [0,100].\n");
            }
          else
             p_op->op_s_i=i;
          break;
       case 'W':
          if (opsign_active) p_op->op_w_b=opsign;
          if (strlen(fn_temp)==0) break;
          if ((sscanf(&fn_temp[0],"%d",&i) != 1) || (i<0) || (1000<i))
            {
     ECHO_CL; wl_caret(2+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("Error: Numeric argument to W option invalid. It must\n");
             (*p_outf)("be an unsigned decimal integer in the range [0,1000].\n");
            }
          else
             p_op->op_w_i=i;
          break;
       case 'X':
          if (opsign_active) p_op->op_x_b=opsign;
          strcpy(&p_op->op_x_s[0],&fn_temp[0]);
          break;
       case 'K':
          if (opsign_active) p_op->op_k_b=opsign;
          if (strlen(fn_temp)>0)
            {
     ECHO_CL; wl_caret(3+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("The K option does not take an argument.\n");
            }
          break;
       case 'H':
          if (opsign_active) p_op->op_h_b=opsign;
          if (strlen(&fn_temp[0])>0)
          if (strlen(&fn_temp[0]) > HL_NMLEN)
            {
     ECHO_CL; wl_caret(3+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("The H option string argument is too long.\n");
             (*p_outf)("Try +Hmenu for a list of help message names.\n");
            }
          else
             if (hel_num(&fn_temp[0]) == HL_ERR)
            {
     ECHO_CL; wl_caret(3+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("Unrecognised message name in argument to H option.\n");
             (*p_outf)("Try +Hmenu for a list of help message names.\n");
            }
          else
             strcpy(&p_op->op_h_s[0],&fn_temp[0]);
          break;
       case 'B':
         {
          uword i;
          if (strlen(fn_temp)==0)
            {
     ECHO_CL; wl_caret(3+p_startopt-comline,p_outf);
             (*p_outf)("FunnelWeb command line syntax error.\n");
             (*p_outf)("The B option must be followed by one or more digits.\n");
             (*p_outf)("The digits must be in the range 1..7.\n");
             break;
            }
          for (i=0;i<strlen(fn_temp);i++)
             if (fn_temp[i]<'1' || fn_temp[i]>'7')
               {

ECHO_CL; wl_caret(2+p_startopt-comline+(1+i),p_outf);
                (*p_outf)("FunnelWeb command line syntax error.\n");
                (*p_outf)("Error: Characters in B argument must be '1'..'7'.\n");
                goto endbop;
               }
          if (opsign_active)
             for (i=0;i<strlen(fn_temp);i++)
                switch (fn_temp[i])
                  {
                   case '1': p_op->op_b1_b=opsign; break;
                   case '2': p_op->op_b2_b=opsign; break;
                   case '3': p_op->op_b3_b=opsign; break;
                   case '4': p_op->op_b4_b=opsign; break;
                   case '5': p_op->op_b5_b=opsign; break;
                   case '6': p_op->op_b6_b=opsign; break;
                   case '7': p_op->op_b7_b=opsign; break;
                   default: as_bomb("op_add: b option case defaulted.");
                  }
          endbop:
          break;
         }
       default:
  ECHO_CL; wl_caret(2+p_startopt-comline,p_outf);
          (*p_outf)("FunnelWeb command line syntax error.\n");
          (*p_outf)("Error: Unknown option letter.\n");
          (*p_outf)("Legal option letters are FIOTLCSWPXZ.\n");
          (*p_outf)("(Note: But P is not yet implemented!)\n");
          break;
      }
   } /* End while. */
 if (seen_error) goto failure;
 return TRUE;

 failure: ASSIGN(*p_op,op_save); return FALSE;
}

/******************************************************************************/

EXPORT void op_wri(p_op,p_outf)
p_op_t p_op;
void   (*p_outf) P_((char *));
{
#define STC(BOOLV) ((BOOLV)?'+':'-')
 sprintf(linet1,"   %cB1 %cB2 %cB3 %cB4 %cB5 %cB6 %cB7\n",
 STC(p_op->op_b1_b),
 STC(p_op->op_b2_b),
 STC(p_op->op_b3_b),
 STC(p_op->op_b4_b),
 STC(p_op->op_b5_b),
 STC(p_op->op_b6_b),
 STC(p_op->op_b7_b));(*p_outf)(linet1);
 sprintf(linet1,"   +C%u\n" ,                    (unsigned) p_op->op_c_i );(*p_outf)(linet1);
 sprintf(linet1,"   %cD\n"  , STC(p_op->op_d_b)                          );(*p_outf)(linet1);
 sprintf(linet1,"   %cF%s\n", STC(p_op->op_f_b),            p_op->op_f_s );(*p_outf)(linet1);
 sprintf(linet1,"   %cH%s\n", STC(p_op->op_h_b),            p_op->op_h_s );(*p_outf)(linet1);
 sprintf(linet1,"   +I%s\n" ,                               p_op->op_i_s );(*p_outf)(linet1);
 sprintf(linet1,"   %cJ%s\n", STC(p_op->op_j_b),            p_op->op_j_s );(*p_outf)(linet1);
 sprintf(linet1,"   %cK\n"  , STC(p_op->op_k_b)                          );(*p_outf)(linet1);
 sprintf(linet1,"   %cL%s\n", STC(p_op->op_l_b),            p_op->op_l_s );(*p_outf)(linet1);
 sprintf(linet1,"   %cO%s\n", STC(p_op->op_o_b),            p_op->op_o_s );(*p_outf)(linet1);
 sprintf(linet1,"   %cQ\n"  , STC(p_op->op_q_b)                          );(*p_outf)(linet1);
 sprintf(linet1,"   %cS%u\n", STC(p_op->op_s_b), (unsigned) p_op->op_s_i );(*p_outf)(linet1);
 sprintf(linet1,"   %cT%s\n", STC(p_op->op_t_b),            p_op->op_t_s );(*p_outf)(linet1);
 sprintf(linet1,"   %cW%u\n", STC(p_op->op_w_b), (unsigned) p_op->op_w_i );(*p_outf)(linet1);
 sprintf(linet1,"   %cX%s\n", STC(p_op->op_x_b),            p_op->op_x_s );(*p_outf)(linet1);
}

/******************************************************************************/
/*                              End of OPTION.C                               */
/******************************************************************************/