server/get2.cxx

Go to the documentation of this file.
00001 /**********************************************************************
00002  * Copyright 2002 Jeff Rush <jrush@taupro.com>
00003  * Original Copyright 1979-2002 Udanax.com
00004  *
00005  * This file is part of the Udanax xanalogical storage system.
00006  *
00007  * Udanax is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * Udanax is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with Udanax; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  **********************************************************************/
00021 
00030 /* Modification History:
00031  * $Log: get2.cxx,v $
00032  * Revision 1.17  2004/09/11 15:02:00  jrush
00033  * Fixed Doxygen tags on all Session parameter arguments.
00034  *
00035  * Revision 1.16  2004/09/11 13:59:21  jrush
00036  * Changed all fprintf's to stderr to use the Nana library L() macro.  Also
00037  * removed a 2-3 minor compiler warnings.
00038  *
00039  * Revision 1.15  2004/09/04 16:02:18  jrush
00040  * Added Doxygen headers before each and every function definition.
00041  *
00042  * Revision 1.14  2004/09/04 13:13:55  jrush
00043  * Added Doxygen commenting around functions.
00044  *
00045  * Revision 1.13  2002/05/28 03:58:42  jrush
00046  * Sources modified to comply with GPL licensing.
00047  *
00048  * Revision 1.12  2002/04/16 22:39:50  jrush
00049  * Converted many #defines into enumeration types instead, and adjusted
00050  * function prototypes accordingly.
00051  *
00052  * Revision 1.11  2002/04/12 11:48:50  jrush
00053  * Major reorganization of includes into a single base-include style.
00054  *
00055  * Revision 1.10  2002/04/10 18:01:54  jrush
00056  * Renamed class typeisa to IStreamAddr.
00057  *
00058  * Revision 1.9  2002/04/09 21:45:46  jrush
00059  * Renamed class 'tumbler' to 'Tumbler', for consistency with Python sources,
00060  * and changed typeisa from typedef to a subclass, in preparation for cleaning
00061  * up the type/class tree.
00062  *
00063  * Revision 1.8  2002/04/06 20:42:50  jrush
00064  * Switch from sess->alloc() style to new(sess) Object parameterized allocator.
00065  *
00066  * Revision 1.7  2002/04/06 17:05:57  jrush
00067  * Switched from referring to 'task' for a client connection to 'session',
00068  * and converted the typetask typedef/struct into a Session C++ class.
00069  *
00070  * Revision 1.6  2002/04/06 15:01:45  jrush
00071  * Changed INT to just 'int'.
00072  *
00073  * Revision 1.5  2002/04/02 17:38:46  jrush
00074  * Purely cosmetic.
00075  *
00076  * Revision 1.4  2002/02/14 10:08:25  jrush
00077  * Cleaned up source:
00078  *
00079  * 1. ran thru the indent tool to achieve a standard look,
00080  * 2. added structured comments at top for use with DOxygen reporting
00081  *    as well as CVS keywords,
00082  * 3. centralized prototypes in protos.h, removing incomplete ones,
00083  * 4. cleaned up use of bool/BOOLEAN type to suit C++ type,
00084  * 5. fixed initializer nesting in tumbler constants,
00085  * 6. converted select() int bits into ANSI fd_set type,
00086  * 7. added Makefile.am for use by automake.
00087  *
00088  */
00089 
00090 #include <string.h>
00091 #include <unistd.h>
00092 #include <stdlib.h>
00093 #include <ctype.h>
00094 #include "udanax.h"
00095 
00096 bool  getspecset(Session * sess, typespecset * specsetptr);
00097 void  prompt(Session * sess, char *string);
00098 bool  getisa(Session * sess, IStreamAddr * isaptr);
00099 bool  getvsa(Session * sess, Tumbler * vsaptr);
00100 bool  gettextset(Session * sess, typetextset * textsetptr);
00101 bool  getnumber(Session * sess, int * numptr);
00102 bool  getcutseq(Session * sess, typecutseq * cutseqptr);
00103 bool  getspanset(Session * sess, typespanset * spansetptr, typeitemid id);
00104 bool  getspan(Session * sess, typespan * spanptr, typeitemid id);
00105 bool  gettumbler(Session * sess, Tumbler * tumblerptr);
00106 bool  validaccount(Session * sess, IStreamAddr * accountptr);
00107 void  error(Session * sess, char *string);
00108 bool  gettext(Session * sess, typetext * textptr);
00109 bool  getvspec(Session * sess, typevspec * vspecptr);
00110 bool  validrequest(Session * sess, typerequest request);
00111 
00119     bool
00120 getnum(
00121     Session *sess,    
00122     int     *numptr)
00123 {                                      /* inside temporary */
00124     metachar c;
00125     int num;
00126 
00127     bool flag = false;                 /* should check for minus */
00128     for (num = 0; (c = getc(sess->inp)) != EOF && isdigit(c);) {
00129         num = num * 10 + c - '0';
00130         flag = true;
00131     }
00132 
00133     if (!flag) {
00134         fprintf(sess->errp, "no number\n");
00135 #ifndef DISTRIBUTION
00136         if (c == '?' || c == 'h') {
00137             system("cat /usr3/xu/requests.j");
00138         } else if (c == '!') {
00139             system("csh");
00140         }
00141 #endif
00142     } else {
00143         ungetc(c, sess->inp);
00144         *numptr = num;
00145     }
00146     return flag;
00147 }
00148 
00156     bool
00157 eatchar(
00158     Session *sess,    
00159     char     c)
00160 {
00161     metachar m;
00162 
00163     if ((m = getc(sess->inp)) != c) {
00164         ungetc(m, sess->inp);
00165         return false;
00166     } else
00167         return true;
00168 }
00169 
00177     bool
00178 needchar(
00179     Session *sess,    
00180     char     c)
00181 {
00182     if (!eatchar(sess, c)) {
00183         fprintf(sess->errp, "needed a ");
00184 
00185         if (c == '\n')
00186             fprintf(sess->errp, "newline\n");
00187         else
00188             fprintf(sess->errp, "%c\n", c);
00189 
00190         return false;
00191     }
00192     return true;
00193 }
00194 
00202     bool
00203 getnumber(
00204     Session *sess,   
00205     int     *numptr)
00206 {
00207     return getnum(sess, numptr) && needchar(sess, '\n');
00208 }
00209 
00217     bool
00218 gettumbler(
00219     Session *sess,    
00220     Tumbler *tumblerptr)
00221 {
00222     int i;
00223 
00224     tumblerclear(tumblerptr);
00225     if (eatchar(sess, '-'))
00226         tumblerptr->sign = 1;
00227 
00228     for (i = 0; i < NPLACES; ++i) {
00229         if (!getnum(sess, (int *) &tumblerptr->mantissa[i]))
00230             return false;
00231 
00232         if (tumblerptr->mantissa[i] == 0 && i == 0) {
00233             --tumblerptr->exp;
00234             --i;
00235         }
00236 
00237         if (!eatchar(sess, '.'))
00238             break;
00239     }
00240 
00241     if (eatchar(sess, '.')) {
00242         fprintf(sess->errp, "tumbler overflow\n");
00243         return false;
00244     }
00245 
00246     for (i = 0; i < NPLACES && tumblerptr->mantissa[i] == 0; ++i)
00247         ;
00248 
00249     if (i == NPLACES)
00250         tumblerptr->exp = 0;
00251 
00252     return needchar(sess, '\n');
00253 }
00254 
00262     bool
00263 getbool(
00264     Session *sess,    
00265     bool    *boolptr)
00266 {
00267     prompt(sess, "(y/n) ");
00268     int c = getc(sess->inp);
00269     if (isupper(c))
00270         c = tolower(c);
00271 
00272     eatchar(sess, '\n');
00273 
00274     if (c == 'y') {
00275         *boolptr = true;
00276         return true;
00277 
00278     } else if (c == 'n') {
00279         *boolptr = false;
00280         return true;
00281 
00282     } else {
00283         error(sess, "need 'y' or 'n'");
00284         return false;
00285     }
00286 }
00287 
00295     bool
00296 getisa(
00297     Session     *sess,    
00298     IStreamAddr *isaptr)
00299 {
00300     return gettumbler(sess, isaptr);
00301 }
00302 
00310     bool
00311 getvsa(
00312     Session *sess,    
00313     Tumbler *vsaptr)
00314 {
00315     return gettumbler(sess, vsaptr);
00316 }
00317 
00325     bool
00326 getrequest(
00327     Session     *sess,    
00328     typerequest *requestptr)
00329 {
00330     prompt(sess, "\nrequest? ");
00331     int c = getc(sess->inp);
00332     if ((int) c == EOF) {
00333         L("endfile\n");
00334         sess->inp = stdin;
00335     }                                  /* else if (c == ':') { while ((c = * getc (sess->inp)) != '\n'); * return
00336                                         * (false); } */
00337     ungetc(c, sess->inp);
00338     return getnumber(sess, requestptr) && validrequest(sess, *requestptr);
00339 }
00340 
00348     bool
00349 validrequest(
00350     Session     *sess,    
00351     typerequest  request)
00352 {
00353     if (request >= 0 && request < NREQUESTS && requestfns[request] != NULL)
00354         return true;
00355 
00356     fprintf(sess->errp, "invalid request: %d\n", request);
00357     return false;
00358 }
00359 
00367     bool
00368 validaccount(
00369     Session     *sess,    
00370     IStreamAddr *accountptr)
00371 {
00372     return (true);
00373 }
00374 
00382     bool
00383 getspecset(
00384     Session     *sess,    
00385     typespecset *specsetptr)
00386 {
00387     bool any, type;
00388     typespec *specset;
00389 
00390     for (;;) {
00391         prompt(sess, "any spans or vspecs? ");
00392         if (!getbool(sess, &any))
00393             return false;
00394 
00395         if (!any) {
00396             *specsetptr = NULL;
00397             return true;
00398         }
00399 
00400         prompt(sess, "a span? ");
00401         if (!getbool(sess, &type))
00402             return false;
00403 
00404         if (type) {
00405             specset = (typespec *) new(sess) typespan;
00406 //            specset = (typespec *) sess->alloc(sizeof(typespan));
00407             if (!getspan(sess, (typespan *) specset, ISPANID))
00408                 return false;
00409 
00410         } else {
00411             specset = (typespec *) new(sess) typevspec;
00412 //            specset = (typespec *) sess->alloc(sizeof(typevspec));
00413             if (!getvspec(sess, (typevspec *) specset))
00414                 return false;
00415         }
00416         *specsetptr = specset;
00417         specsetptr = (typespecset *) & (((typevspec *) specset)->next);
00418     }
00419 }
00420 
00428     bool
00429 getvspec(
00430     Session   *sess,    
00431     typevspec *vspecptr)
00432 {
00433     vspecptr->itemid = VSPECID;
00434     vspecptr->next   = NULL;
00435 
00436     prompt(sess, "document=> ");
00437     if (!(getisa(sess, &vspecptr->docisa) && getspanset(sess, &vspecptr->vspanset, VSPANID)))
00438         return false;
00439 
00440     return true;
00441 }
00442 
00450     bool
00451 getspanset(
00452     Session     *sess,    
00453     typespanset *spansetptr,
00454     typeitemid   id)
00455 {
00456     bool any;
00457     typespan *spanset;
00458 
00459     for (;;) {
00460         prompt(sess, "any spans? ");
00461         if (!getbool(sess, &any))
00462             return false;
00463 
00464         if (!any) {
00465             *spansetptr = NULL;
00466             return true;
00467         }
00468 
00469         spanset = new(sess) typespan;
00470 //        spanset = (typespan *) sess->alloc(sizeof(typespan));
00471         if (!getspan(sess, spanset, id))
00472             return false;
00473 
00474         *spansetptr = spanset;
00475         spansetptr  = &spanset->next;
00476     }
00477 }
00478 
00486     bool
00487 getspan(
00488     Session    *sess,    
00489     typespan   *spanptr,
00490     typeitemid  id)
00491 {
00492     prompt(sess, "enter span\n       start=> ");
00493     if (!getisa(sess, (IStreamAddr *) &spanptr->stream))
00494         return false;
00495 
00496     spanptr->itemid = id;
00497 
00498     prompt(sess, "      width=> ");
00499     if (!(getisa(sess, (IStreamAddr *) &spanptr->width)))
00500         return false;
00501 
00502     return true;
00503 }
00504 
00512     bool
00513 gettextset(
00514     Session     *sess,    
00515     typetextset *textsetptr)
00516 {
00517     typetext *textset;
00518 
00519     for (;;) {
00520         textset = new(sess) typetext;
00521 //        textset = (typetext *) sess->alloc(sizeof(typetext));
00522         if (!gettext(sess, textset)) {
00523             *textsetptr = NULL;
00524             break;
00525         }
00526         *textsetptr = textset;
00527         textsetptr  = &textset->next;
00528     }
00529     return true;
00530 }
00531 
00539     bool
00540 gettext(
00541     Session  *sess,    
00542     typetext *textptr)
00543 {
00544     if (!fgets(textptr->string, GRANTEXTLENGTH, sess->inp)) {
00545         textptr->length = 0;
00546         return false;
00547     }
00548 
00549     textptr->length = strlen(textptr->string);
00550     if (textptr->length <= 1)
00551         return false;
00552 
00553 /* remove newlines */
00554 /* 
00555  * if (textptr->string[textptr->length - 1] == '\n')
00556  * textptr->string[--textptr->length] = '\0'; */
00557 
00558     textptr->itemid = TEXTID;
00559     return true;
00560 }
00561 
00569     bool
00570 getcutseq(
00571     Session    *sess,    
00572     typecutseq *cutseqptr)
00573 {
00574     int i;
00575     bool anycuts;
00576 
00577     i = 0;
00578     for (;;) {
00579         prompt(sess, "any cuts? ");
00580         if (!getbool(sess, &anycuts))
00581             return false;
00582 
00583         if (!anycuts)
00584             break;
00585 
00586         prompt(sess, "cut address=> ");
00587         if (!getvsa(sess, &cutseqptr->cutsarray[i]))
00588             return false;
00589 
00590         if (++i > MAXCUTS)
00591             break;
00592 
00593         cutseqptr->numberofcuts = i;
00594     }
00595     return true;
00596 }
00597 
00605     bool
00606 getboolset(
00607     Session         *sess,    
00608     typeboolsetnode **boolsetptr)
00609 {
00610     bool disjunct;
00611     typeboolsetnode *boolset;
00612 
00613     for (;;) {
00614         prompt(sess, "any disjunctions? ");
00615         if (!getbool(sess, &disjunct))
00616             return false;
00617 
00618         if (!disjunct) {
00619             *boolsetptr = NULL;
00620             return true;
00621         }
00622 
00623         boolset = new(sess) typeboolsetnode;
00624 //        boolset = (typeboolsetnode *) sess->alloc(sizeof(typeboolsetnode));
00625 
00626         prompt(sess, "enter disjunction=> ");
00627         if (!getspanset(sess, &boolset->val, ISPANID))
00628             return false;
00629 
00630 /* zzz & zzz */
00631         boolset->itemid = NODEID;
00632         *boolsetptr     = boolset;
00633         boolsetptr      = &boolset->next;
00634     }
00635 }

Generated on Sun Aug 21 04:18:15 2005 for Udanax-Green by doxygen1.3.4