home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / addr / ap_ut.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  9.8 KB  |  488 lines

  1. /* ap_ut.c: address parser utility routines. */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/addr/RCS/ap_ut.c,v 6.0 1991/12/18 20:21:24 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/addr/RCS/ap_ut.c,v 6.0 1991/12/18 20:21:24 jpo Rel $
  9.  *
  10.  * $Log: ap_ut.c,v $
  11.  * Revision 6.0  1991/12/18  20:21:24  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. /*
  19. Standard routines for handling address list element nodes
  20. */
  21.  
  22.  
  23. /*
  24. < 1978  B. Borden       Wrote initial version of parser code
  25. 78-80   D. Crocker      Reworked parser into current form
  26. Apr 81  K. Harrenstein  Hacked for SRI
  27. Jun 81  D. Crocker      Back in the fold.  Finished v7 conversion
  28.             minor cleanups.
  29.             repackaging into more complete set of calls
  30. Jul 81  D. Crocker      ap_free & _alloc check for not 0 or -1
  31.             malloc() error causes jump to ap_init error
  32. */
  33.  
  34.  
  35.  
  36. #include "util.h"
  37. #include "ap.h"
  38.  
  39.  
  40. #ifdef AP_DEBUG
  41. extern char ap_debug;
  42. extern char *typtab[];
  43. #endif
  44.  
  45.  
  46. int                             (*ap_gfunc)(); /* -- ptr to char get fn -- */
  47. int                             ap_peek; /* -- basic parse state info -- */
  48. extern int                      ap_perlev;
  49. extern int                      ap_grplev;
  50. AP_ptr                          ap_pstrt,
  51.                 ap_pcur;
  52.  
  53.  
  54. /* ---------------------  Begin  Routines  -------------------------------- */
  55.  
  56.  
  57.  
  58. AP_ptr ap_alloc()  /* -- create node, return pointer to it -- */
  59. {
  60.     AP_ptr  ap;
  61.  
  62.     ap = (AP_ptr) malloc (sizeof (struct ap_node));
  63.  
  64.     if (ap == BADAP)
  65.         return (BADAP);
  66.  
  67.     ap_ninit (ap);
  68.     return (ap);
  69. }
  70.  
  71.  
  72.  
  73. void ap_ninit (ap)
  74. register AP_ptr         ap;
  75. {
  76.     bzero((char *)ap, sizeof(*ap));
  77.     ap -> ap_obtype         = AP_NIL;
  78.     ap -> ap_ptrtype        = AP_PTR_NIL;
  79. }
  80.  
  81.  
  82.  
  83. void ap_free (ap)
  84. register AP_ptr         ap;
  85. {
  86.     register char   *obvalue;
  87.  
  88.     /* -- get rid of node, if have one -- */
  89.     switch ((int)ap) {
  90.     case OK:
  91.     case NOTOK:
  92.         /* -- nothing to free -- */
  93.         break;
  94.  
  95.     default:
  96.         if (ap->ap_normalised == TRUE) {
  97.             if (ap->ap_localhub)
  98.                 free(ap->ap_localhub);
  99.             if (ap->ap_chankey)
  100.                 free(ap->ap_chankey);
  101.             if (ap->ap_error)
  102.                 free(ap->ap_error);
  103.         }
  104.         /* -- get rid of its data string -- */
  105.         switch ((int)(obvalue = ap -> ap_obvalue)) {
  106.         case OK:
  107.         case NOTOK:
  108.             /* -- nothing to free -- */
  109.             break;
  110.  
  111.         default:
  112.             free (obvalue);
  113.         }
  114.  
  115.         free ((char *) ap);
  116.     }
  117. }
  118.  
  119.  
  120.  
  121. /* -- add data to node at end of chain -- */
  122. void ap_fllnode (ap, obtype, obvalue)
  123. register AP_ptr         ap;
  124. char                    obtype;
  125. register char           *obvalue;
  126. {
  127.  
  128.     ap -> ap_obtype = obtype;
  129.     ap -> ap_obvalue = (obvalue == NULLCP) ? NULLCP : strdup (obvalue);
  130.  
  131. #ifdef AP_DEBUG
  132.     if (ap_debug)
  133.         PP_DBG (("(%s/'%s')", typtab[obtype], obvalue));
  134. #endif
  135. }
  136.  
  137.  
  138.  
  139. AP_ptr ap_new (obtype, obvalue)  /* -- alloc & fill node -- */
  140. char                    obtype;
  141. char                    *obvalue;
  142. {
  143.     register AP_ptr nap;
  144.  
  145.     nap = ap_alloc();
  146.     ap_fllnode (nap, obtype, obvalue);
  147.     return (nap);
  148. }
  149.  
  150.  
  151.  
  152. void ap_insert (cur, ptrtype, new)  /* -- create, fill or insert node in list -- */
  153. register AP_ptr         cur;            /* -- where to insert after -- */
  154. char                    ptrtype;        /* -- more or new address ? -- */
  155. register AP_ptr         new;            /* -- where to insert after -- */
  156. {
  157.  
  158.     /* -- Now copy linkages from current node -- */
  159.  
  160.     new -> ap_ptrtype = cur -> ap_ptrtype;
  161.     new -> ap_next = cur -> ap_next;
  162.  
  163.     /* -- now point current node at inserted node -- */
  164.  
  165.     cur -> ap_ptrtype = ptrtype;
  166.     cur -> ap_next = new;
  167. }
  168.  
  169.  
  170.  
  171. AP_ptr ap_sqinsert (cur, type, new)  /* -- insert in sequence -- */
  172. register AP_ptr         cur;
  173. int                     type;
  174. register AP_ptr         new;
  175. {
  176.     AP_ptr          oldptr;
  177.     int             otype;
  178.  
  179.     switch ((int)new) {
  180.     case OK:
  181.     case NOTOK:
  182.         return (NULLAP);
  183.     }
  184.  
  185.     oldptr = cur -> ap_next;
  186.     otype = cur -> ap_ptrtype;
  187.     cur -> ap_next = new;
  188.     cur -> ap_ptrtype = type;
  189.  
  190.     while (new -> ap_ptrtype != AP_PTR_NIL &&
  191.            new -> ap_next != NULLAP &&
  192.            new -> ap_next -> ap_obtype != AP_NIL)
  193.             new = new -> ap_next;
  194.  
  195.     if (new -> ap_next && new -> ap_next -> ap_obtype == AP_NIL)
  196.         ap_delete (new);
  197.  
  198.     new -> ap_next = oldptr;
  199.     new -> ap_ptrtype = otype;
  200.     return (new);
  201. }
  202.  
  203.  
  204.  
  205. void ap_delete (ap)  /* -- remove next node in sequence -- */
  206. register AP_ptr         ap;
  207. {
  208.     register AP_ptr next;
  209.  
  210.     if (ap != NULLAP && ap -> ap_ptrtype != AP_PTR_NIL 
  211.         && ap -> ap_next != NULLAP) {
  212.         /* -- only if there is something there -- */
  213.         /* -- link around one to be removed -- */
  214.         next = ap -> ap_next;
  215.         ap -> ap_ptrtype = next -> ap_ptrtype;
  216.         ap -> ap_next = next -> ap_next;
  217.         ap_free (next);
  218.     }
  219. }
  220.  
  221.  
  222.  
  223. AP_ptr ap_append (ap, obtype, obvalue)  /* -- alloc, fill or insert node -- */
  224. register AP_ptr         ap;     /* -- node to insert after -- */
  225. char                    obtype;
  226. char                    *obvalue;
  227. {
  228.     register AP_ptr nap;
  229.  
  230.     nap = ap_alloc();
  231.     ap_fllnode (nap, obtype, obvalue);
  232.     ap_insert (ap, AP_PTR_MORE, nap);
  233.     return (nap);
  234. }
  235.  
  236.  
  237.  
  238. AP_ptr ap_add (ap, obtype, obvalue)  /* -- append data to current node -- */
  239. register AP_ptr         ap;
  240. char                    obtype;
  241. register char           *obvalue;
  242. {
  243.     register char       *ovalue;
  244.  
  245.     if (ap -> ap_obtype != obtype)
  246.         return (ap_append (ap, obtype, obvalue));
  247.     else {
  248.         /* -- same type or empty => can append -- */
  249.  
  250.         /* -- no data to add -- */
  251.         if (obvalue == NULLCP)
  252.             return (OK);
  253.  
  254.         if ((ovalue = ap -> ap_obvalue) == NULLCP)
  255.             ap_fllnode (ap, obtype, obvalue);
  256.         else {
  257.             /* -- add to existing data -- */
  258.             ovalue = ap -> ap_obvalue;
  259.             ap -> ap_obvalue = multcat (ovalue, " ",
  260.                             obvalue, NULLCP);
  261.             free (ovalue);
  262.         }
  263.  
  264. #ifdef AP_DEBUG
  265.         if (ap_debug)
  266.             PP_DBG (("(%d/'%s')", obtype, obvalue));
  267. #endif
  268.     }
  269.  
  270.     return (OK);
  271. }
  272.  
  273.  
  274.  
  275. AP_ptr ap_sqdelete (strt_node, end_node)
  276. register AP_ptr         strt_node;
  277. register AP_ptr         end_node;
  278. {
  279.     /* -- remove nodes, through end node -- */
  280.  
  281.     switch ((int)strt_node) {
  282.     case OK:
  283.     case NOTOK:
  284.         return (NULLAP);
  285.     }
  286.  
  287.     while (strt_node -> ap_ptrtype != AP_PTR_NIL) {
  288.         if (strt_node -> ap_next == end_node) {
  289.             /* -- last one requested -- */
  290.             ap_delete (strt_node);
  291.             return (strt_node -> ap_next);
  292.         }
  293.  
  294.         ap_delete (strt_node);
  295.     }
  296.  
  297.     /* -- end of chain -- */
  298.     return (NULLAP);
  299. }
  300.  
  301.  
  302.  
  303. AP_ptr ap_1delete (ap)  /* -- remove all nodes of address to NXT -- */
  304. register AP_ptr         ap;     /* -- starting node -- */
  305. {
  306.     while (ap -> ap_ptrtype != AP_PTR_NIL) {
  307.         if (ap -> ap_ptrtype == AP_PTR_NXT)
  308.             return (ap -> ap_next);
  309.  
  310.         ap_delete (ap);
  311.     }
  312.  
  313.     /* -- end of chain -- */
  314.     return (NULLAP);
  315. }
  316.  
  317.  
  318.  
  319. void ap_sqtfix (strt, end, obtype)  /* -- alter obtype of a node subsequence -- */
  320. register AP_ptr         strt;
  321. register AP_ptr         end;
  322. register char           obtype;
  323. {
  324.     for ( ; ; strt = strt -> ap_next) {
  325.         if (strt -> ap_obtype != AP_COMMENT)
  326.             strt -> ap_obtype = obtype;
  327.  
  328.         if (strt == end || strt -> ap_ptrtype == AP_PTR_NIL)
  329.             break;
  330.     }
  331. }
  332.  
  333.  
  334.  
  335. AP_ptr ap_move (to, from)  /* -- move node after from to be after to -- */
  336. register AP_ptr         to,
  337.             from;
  338. {
  339.     register AP_ptr     nodeptr;
  340.  
  341.     /* -- quiet failure -- */
  342.     if (from -> ap_ptrtype == AP_PTR_NIL || from -> ap_next == NULLAP)
  343.         return (from);
  344.  
  345.     nodeptr = from -> ap_next;
  346.  
  347.     from -> ap_next = nodeptr -> ap_next;
  348.     from -> ap_ptrtype = nodeptr -> ap_ptrtype;
  349.  
  350.     ap_insert (to, AP_PTR_MORE, nodeptr);
  351.  
  352.     /* -- next in chain, now -- */
  353.     return (from);
  354. }
  355.  
  356.  
  357.  
  358. AP_ptr ap_sqmove (to, from, endtype)  /* -- move sequence -- */
  359. register AP_ptr         to,
  360.             from;
  361. register char           endtype;  /* -- copy only COMMENT and this -- */
  362. {
  363.     switch ((int)from) {
  364.     case OK:
  365.     case NOTOK:
  366.         return (NULLAP);
  367.     }
  368.  
  369.     while (from -> ap_ptrtype != AP_PTR_NIL &&
  370.            from -> ap_next != NULLAP) {
  371.  
  372.         if (endtype != (char) AP_NIL)
  373.             if (from -> ap_obtype != AP_COMMENT &&
  374.                 from -> ap_obtype != endtype)
  375.                 break;
  376.         to = ap_move (to, from);
  377.     }
  378.  
  379.     /* -- end of chain -- */
  380.     return (to);
  381. }
  382.  
  383.  
  384.  
  385. void ap_iinit (gfunc)  /* -- input function initialization -- */
  386. int     (*gfunc)();
  387. {
  388.     ap_gfunc = gfunc;             /* -- set character fetch func -- */
  389.     ap_peek = -1;                 /* -- no lex peek char -- */
  390. }
  391.  
  392.  
  393.  
  394. void ap_clear()  /* -- clear out the parser state -- */
  395. {
  396.     ap_grplev = 0;      /* -- zero group nesting depth -- */
  397.     ap_perlev = 0;      /* -- zero <> nesting depth -- */
  398. }
  399.  
  400.  
  401.  
  402. AP_ptr ap_pinit (gfunc)  /* -- init, alloc & set start node -- */
  403. int     (*gfunc)();
  404. {
  405.     ap_iinit (gfunc);
  406.     return (ap_pstrt = ap_pcur = ap_alloc());
  407. }
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414. /*
  415. These echo the basic list manipuation primitives, but use ap_pcur
  416. for the pointer and any insert will cause ap_pcur to be updated
  417. to point to the new node.
  418. */
  419.  
  420.  
  421. void ap_palloc()  /* -- alloc, insert after pcur -- */
  422. {
  423.     ap_pnsrt (ap_alloc(), AP_PTR_MORE);
  424. }
  425.  
  426.  
  427.  
  428. void ap_pfill (obtype, obvalue)  /* -- add data to node at end of chain -- */
  429. char            obtype;
  430. register char   *obvalue;
  431. {
  432.     ap_pcur -> ap_obtype = obtype;
  433.     ap_pcur -> ap_obvalue =
  434.             (obvalue == NULLCP) ? NULLCP : strdup (obvalue);
  435.  
  436. #ifdef AP_DEBUG
  437.     if (ap_debug)
  438.         PP_DBG (("(%s/'%s')", typtab[obtype], obvalue));
  439. #endif
  440. }
  441.  
  442.  
  443.  
  444. void ap_pnsrt (ap, ptrtype)  /* -- add node to end of parse chain -- */
  445. register AP_ptr         ap;
  446. char                    ptrtype;
  447. {
  448.     register AP_ptr     rap_pcur;
  449.  
  450.     if ((rap_pcur = ap_pcur) -> ap_obtype == AP_NIL) {
  451.         /* -- current one can be used -- */
  452.         rap_pcur -> ap_obtype = ap -> ap_obtype;
  453.         rap_pcur -> ap_obvalue = ap -> ap_obvalue;
  454.         ap -> ap_obvalue = NULLCP;
  455.         ap_free (ap);
  456.     }
  457.     else {
  458.         /* -- really do the insert -- */
  459.         rap_pcur -> ap_ptrtype = ptrtype;
  460.         rap_pcur -> ap_next = ap;
  461.         ap_pcur = ap;
  462.     }
  463. }
  464.  
  465.  
  466.  
  467. void ap_pappend (obtype, obvalue)  /* -- alloc, fill, append at end -- */
  468. char    obtype;
  469. char   *obvalue;
  470. {
  471.     ap_palloc();    /* -- will update pcur -- */
  472.     ap_fllnode (ap_pcur, obtype, obvalue);
  473. }
  474.  
  475.  
  476.  
  477. void ap_padd (obtype, obvalue)  /* -- try to append data to current node -- */
  478. char                    obtype;
  479. char                    *obvalue;
  480. {
  481.     register AP_ptr     nap;
  482.  
  483.     nap = ap_add (ap_pcur, obtype, obvalue);
  484.  
  485.     if (nap != OK)                /* -- created new node -- */
  486.         ap_pcur = nap;
  487. }
  488.