home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / FIDO / PUP_V2B.ZIP / SET-PUP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-25  |  11.4 KB  |  549 lines

  1. #include <puppy.h>
  2. #include <pupmem.h>
  3. #include <ascii.h>
  4.  
  5. /*
  6.     This sets PUPPY.SYS and creates the message file.
  7.  
  8.     (k) All rights reversed
  9. */
  10.  
  11. int sysfile;            /* PUPPY.SYS global handle */
  12. struct _pup pup;        /* thing we build */
  13. int events;            /* events we find */
  14.  
  15. #define same(s1,s2) (strcmp(s1,s2)==0)
  16.  
  17. int _stack = 5000;
  18.  
  19. main(argc,argv)
  20. int argc;
  21. char **argv;
  22. {
  23. char *p,fn[SS],sw[SS];
  24. int i;
  25. FLAG quit,once,writeout;
  26.  
  27.     printf("Pup configuration, version 2b, 25 Jan 88\r\n");
  28.     printf("Tom Jennings, 164 Shipley\r\n");
  29.     printf("San Francisco CA 94107 USA\r\n");
  30.     printf("(k) all rights reversed\r\n");
  31.  
  32.     sysfile= open("PUPPY.SYS",2);        /* open it for read/write */
  33.     if (sysfile == -1) {
  34.         printf(" * File \"PUPPY.SYS\" doesn't exist, making a new one\r\n");
  35.         sysfile= creat("PUPPY.SYS",2);    /* make a new one, */
  36.         if (sysfile == -1) {
  37.             printf(" * Can't create it!\r\n");
  38.             exit(1);
  39.         }
  40.         pup.top= 0;            /* empty file */
  41.         pup.msgnbr= 1;            /* first message number */
  42.         pup.quote_pos= 0L;        /* not used yet */
  43.         pup.callers= 0L;
  44.  
  45.     } else read(sysfile,&pup,sizeof(struct _pup));    /* read current settings, */
  46.  
  47.     setdefaults();                /* fill in those blanks */
  48.     finit("PUP.SET");
  49.     lseek(sysfile,0L,0);
  50.     write(sysfile,&pup,sizeof(struct _pup));
  51.     close(sysfile);
  52.  
  53.     setmsg();                /* create a message file */
  54.     setclr();                /* create a caller file */
  55.     exit(0);
  56. }
  57.  
  58. /* Create a new caller file. */
  59.  
  60. setclr() {
  61. int f;
  62.  
  63.     f= open("puppy.clr",0);
  64.     if (f != -1) {
  65.         printf("A caller file already exists\r\n");
  66.         close(f);
  67.         return;
  68.     }
  69.     fffill("PUPPY.CLR",(long)(pup.callsize * sizeof(struct _clr)),0);
  70. }
  71. /* Create an empty message file. */
  72.  
  73. setmsg() {
  74. int f;
  75.  
  76.     f= open("puppy.idx",0);
  77.     if (f != -1) {
  78.         close(f);
  79.         printf("A message base already exists\r\n");
  80.         return;
  81.     }
  82.     fffill("PUPPY.IDX",(long)(pup.messages * sizeof(struct _msg)),0);
  83.     fffill("PUPPY.DAT",(long)(pup.messages * pup.msgsize),SUB);
  84. }
  85.  
  86. /* Fill the specified file with junk. */
  87.  
  88. fffill(fname,count,c)
  89. char *fname;        /* filename */
  90. long count;        /* how many bytes to write, */
  91. char c;            /* fill character */
  92. {
  93. char buff[1024];
  94. int f,i,n;
  95.  
  96.     for (i=0; i < sizeof(buff); i++)     /* fill the buffer with nothings */
  97.         buff[i]= c;
  98.  
  99.     printf("Creating file %s\r\n",fname);
  100.     f= creat(fname,1);            /* create the file, */
  101.     if (f == -1) {
  102.         printf("Creation error!\r\n");
  103.         return;
  104.     }
  105.     while (count > 0L) {
  106.         n= (count < sizeof(buff)) ? count : sizeof(buff);
  107.         if (write(f,buff,n) != n) {
  108.             printf("Disk full!\r\n");
  109.             break;
  110.         }
  111.         count -= n;
  112.     }
  113.     close(f);
  114. }
  115. /* Set the defaults to put into PUPPY.SYS. */
  116.  
  117. setdefaults() {
  118. char *p;
  119. int i,i1,i2;
  120. long l1,l2;
  121.  
  122.     l1= pup.callers;            /* preserve these */
  123.     l2= pup.quote_pos;
  124.     i1= pup.top;
  125.     i2= pup.msgnbr;
  126.     p= (char *) &pup;            /* clear out the structure */
  127.     for (i= sizeof(struct _pup); i--;) *p++= NUL;
  128.     pup.top= i1;
  129.     pup.msgnbr= i2;
  130.     pup.callers= l1;            /* restore */
  131.     pup.quote_pos= l2;
  132.  
  133.     pup.messages= 50;            /* size of message base */
  134.     pup.msgsize= 2048;            /* msg size */
  135.  
  136.     pup.callsize= 100;            /* size of caller base */
  137.  
  138.     pup.nlimit= 60;                /* default= 1 hr time limit, */
  139.     pup.klimit= 100;            /* 100K download limit, */
  140.  
  141.     pup.cd_bit= 0x80;            /* CD bit on IBM Async Card CTS */
  142.     pup.maxbaud= 1200;            /* 1200 baud max */
  143.     strcpy(pup.mdmstr,"ATX1E0V0M0S0=0");    /* default modem init */
  144.  
  145.     pup.connects= 1;            /* 1 attempt with connect */
  146.     pup.tries= 10;                /* 10 attempts to dial */
  147. }
  148.  
  149.  
  150. /* Fido initializer */
  151.  
  152. #define KEYLEN 10
  153.  
  154. char keyword[][KEYLEN] = {
  155.  
  156. /* 0 */        "time-limi",
  157.         "k-limit",
  158.  
  159. /* 2 */        "max-baud",
  160.         "cd-bit",
  161.         "modem-str",
  162.  
  163. /* 5 */        "dial-trie",
  164.         "connect-t",
  165.         "io-port",
  166.  
  167. /* 8 */        "node",
  168.         "net",
  169.         "zone",
  170.  
  171. /* 11 */    "file-pref",
  172.  
  173. /* 12 */    "message-t",
  174.         "message-s",
  175.  
  176. /* 14 */    "event",
  177.  
  178. /* 15 */    "topic",
  179.  
  180. /* 16 */    "callers",
  181.  
  182.         ""    /* end of list */
  183. };
  184.  
  185. finit(fn)
  186. char *fn;
  187. {
  188. char *process();
  189.  
  190. int i,f;
  191. char ln[256];        /* LONG raw input line */
  192. char word[sizeof(ln)];    /* word we parse */
  193. char arg[sizeof(ln)];    /* an arg for it */
  194. int value;        /* argument value */
  195. int line;        /* line in file */
  196. FLAG err;        /* error in file */
  197. char *cp;
  198.  
  199.  
  200.     f= open(fn,0);
  201.     if (f == -1) {
  202.         printf(" * Can't find Startup File %s\r\n",fn);
  203.         return(0);
  204.     }
  205.  
  206.     err= 0;
  207.     line= 0;
  208.     while (rline(f,ln,sizeof(ln))) {
  209.         ++line;
  210.  
  211.         clip_cmt(ln);                /* strip off comments */
  212.         cp= skip_delim(ln);            /* skip leading blanks, etc */
  213.         if (*cp == NUL) continue;        /* ignore blank lines */
  214.  
  215.         if (*cp == '*') {            /* label line */
  216.             puts(ln); puts("\r\n");
  217.             continue;
  218.         }
  219.         cpyatm(word,cp);            /* the key word, */
  220.         word[KEYLEN - 1]= NUL;            /* truncate to match */
  221.         cp= next_arg(cp);            /* ptr to rest of line */
  222.         cpyatm(arg,cp);                /* its (default) arg */
  223.  
  224.         if (! *arg) {
  225.             inierr(fn,line,ln,"Incomplete command");
  226.             err= 1;
  227.             continue;
  228.         }
  229.         stolower(word);
  230.         stolower(arg);
  231.  
  232.         value= atoi(arg);            /* atoi() it blindly */
  233.         if (same(arg,"on") || same(arg,"yes"))
  234.             value= 1;            /* else 0 == off == no */
  235.  
  236.         for (i= 0; *keyword[i]; ++i) {        /* find the word, */
  237.             if (same(keyword[i],word)) {    /* if a match, */
  238.                 cp= process(i,arg,cp,value); /* do it, */
  239.                 if (*cp) {
  240.                     inierr(fn,line,ln,cp);
  241.                     err= 1;
  242.                 }
  243.                 break;
  244.             }
  245.         }
  246.         if (! *keyword[i]) {
  247.             inierr(fn,line,ln,"Not a command");
  248.             err= 1;
  249.         }
  250.     }
  251.     close(f);
  252.     return(1);
  253. }
  254. /* Complain about this line. */
  255.  
  256. inierr(fn,lineno,ln,error)
  257. char *fn;
  258. int lineno;
  259. char *ln,*error;
  260. {
  261.     printf("%s in file %s at line %d\r\n",error,fn,lineno);
  262.     printf("  \"%s\"\r\n",ln);
  263. }
  264.  
  265. /* Process the keyword. */
  266.  
  267. char *process(i,arg,cp,value)
  268. int i;            /* keyword table index */
  269. char *arg;        /* next word at cp, for convenience */
  270. char *cp;        /* ptr to line after keyword */
  271. int value;        /* atoi of arg for convenience */
  272. {
  273. char *rp;
  274. char *build_sched();
  275.  
  276.     rp= "";
  277.     switch (i) {
  278.         case 0: pup.nlimit= value; break;
  279.         case 1: pup.klimit= value; break;
  280.  
  281.         case 2: pup.maxbaud= value; break;
  282.         case 3: pup.cd_bit= value; break;
  283.         case 4: stoupper(cp); strcpy(pup.mdmstr,cp); break;
  284.         case 5: pup.tries= value; break;
  285.         case 6: pup.connects= value; break;
  286.         case 7: pup.iodev= value; break;
  287.  
  288.         case 8: pup.id.number= value; break;
  289.         case 9: pup.id.net= value; break;
  290.         case 10: pup.id.zone= atoi(arg); break;
  291.  
  292.         case 11: strcpy(pup.filepref,arg); break;
  293.  
  294.         case 12: pup.messages= value; break;
  295.         case 13: pup.msgsize= value; break;
  296.  
  297.         case 14: if (events < SCHEDS) {
  298.                 rp= build_sched(&pup.sched[events],cp);
  299.                 if (! *rp) ++events;
  300.             } else rp= "Too many EVENTS";
  301.             break;
  302.  
  303.         case 15: for (i= 0; i < 16; i++) {
  304.                 if (! *pup.topic[i].name) {
  305.                     arg[8]= NUL;
  306.                     strcpy(pup.topic[i].name,arg);
  307.                     cp= next_arg(cp);
  308.                     cp[24]= NUL;
  309.                     strcpy(pup.topic[i].desc,cp);
  310.                     break;
  311.                 }
  312.             }
  313.             if (i == 16) rp= "Too many TOPICs";
  314.             break;
  315.  
  316.         case 16: pup.callsize= value; break;
  317.     }
  318.     return(rp);
  319. }
  320.  
  321. /* Fill in the event structure. */
  322.  
  323. char *build_sched(a,cp)
  324. struct _sched *a;
  325. char *cp;
  326. {
  327. char buff[SS],c;
  328. int n,h,m;
  329. long l;
  330.  
  331.     a-> hr= atoi(cp);                /* get start time, */
  332.     if (a-> hr > 23) return("Hour must be 0 to 23");
  333.     while (isdigit(*cp)) ++cp;            /* look for a colon */
  334.     if (*cp == ':') a-> min= atoi(++cp);        /* get mins if so, */
  335.     else a-> min= 0;
  336.     if (a-> min > 59) return("Minute must be 0 to 59");
  337.  
  338.     cp= next_arg(cp);                /* get sched width */
  339.     a-> len= atoi(cp);                /* or is it ERRORLEVEL */
  340.  
  341.     cp= next_arg(cp);                /* do tag */
  342.     a-> tag= toupper(*cp);
  343.     if ((a-> tag < 'A') || (a-> tag > 'X'))
  344.         return("Event types must be A - X");
  345.  
  346.     cp= next_arg(cp);                /* options */
  347.     stolower(cp);                    /* all lower case */
  348.     while (*cp) switch (*cp++) {
  349.         case 'o': a-> bits |= SCHED_OPTIONAL; break;
  350.     }
  351.     return("");
  352. }
  353.  
  354. /* Strip comments from a line of text; truncate it at the semicolon, then work
  355. backwards deleting delimiters. */
  356.  
  357. clip_cmt(cp)
  358. char *cp;
  359. {
  360. char *sp;
  361.  
  362.     sp= cp;                    /* remember where we started */
  363.     while (*cp) {
  364.         if (*cp == ';') {        /* search for a semicolon */
  365.             *cp= NUL;        /* kill it, */
  366.             while (delim(*--cp))     /* kill all delims */
  367.                 *cp= NUL;
  368.             break;
  369.         }
  370.         ++cp;
  371.     }
  372. }
  373.  
  374. /* Read a line of text from the file, null terminate it. Function returns
  375. zero if EOF. Deletes all CRs and Control-Zs from the stream. Lines are
  376. terminated by LFs. */
  377.  
  378. rline(file,buf,len)
  379. int file;
  380. char *buf;
  381. int len;
  382. {
  383. int i;
  384. char notempty,c;
  385.  
  386.     i= 0; notempty= 0;
  387.     --len;                        /* compensate for added NUL */
  388.     while (i < len) {
  389.         if (! read(file,&c,1)) break;        /* stop if empty */
  390.         if (c == 0x1a) continue;        /* totally ignore ^Z, */
  391.         notempty= 1;                /* not empty */
  392.         if (c == '\r') continue;        /* skip CR, */
  393.         if (c == '\r' + 128) continue;        /* skip soft CR, */
  394.         if (c == '\n') break;            /* stop if LF */
  395.         buf[i++]= c;
  396.     }
  397.     buf[i]= '\0';
  398.     return(notempty);
  399. }
  400.  
  401. /* Return the number of args left in the string. */
  402.  
  403. num_args(s)
  404. char *s;
  405. {
  406. int count;
  407.  
  408.     count= 0;
  409.     s= skip_delim(s);            /* skip leading blanks, */
  410.     while (*s) {
  411.         ++count;            /* count one, */
  412.         s= next_arg(s);            /* find next, */
  413.     }
  414.     return(count);
  415. }
  416.  
  417. /* Return a pointer to the next argument in the string. */
  418.  
  419. char *next_arg(s)
  420. char *s;
  421. {
  422.     while ((!delim(*s)) && *s)        /* skip this one, */
  423.         ++s;                /* up to delim, */
  424.     s= skip_delim(s);            /* then skip delims, */
  425.     return(s);
  426. }
  427.  
  428. /* Skip over the leading delimiters in a string. */
  429.  
  430. char *skip_delim(s)
  431. char *s;
  432. {
  433.     while (delim(*s) && *s) {
  434.         ++s;
  435.     }
  436.     return(s);
  437. }
  438.  
  439. /* Copy the string to the destination array, stopping if we find one
  440. of our delimiters. */
  441.  
  442. cpyatm(to,from)
  443. char *to;
  444. char *from;
  445. {
  446.     while ( (!delim(*from)) && *from) 
  447.         *to++= *from++;
  448.     *to= '\0';
  449. }
  450.  
  451. /* Copy the string to the destination array, stopping if we find one
  452. of our delimiters. */
  453.  
  454. cpyarg(to,from)
  455. char *to;
  456. char *from;
  457. {
  458.     while (*from) {
  459.         if (delim(*from)) break;
  460.         *to++= *from++;
  461.     }
  462.     *to= '\0';
  463. }
  464.  
  465. /* Return true if the character is a delimiter. */
  466.  
  467. delim(c)
  468. char c;
  469. {
  470.     switch (c) {
  471.         case ';': return(1);
  472.         case ' ': return(1);
  473.         case ',': return(1);
  474.         case TAB: return(1);
  475.         default: return(0);
  476.     }
  477. }
  478.  
  479. /* Strip the pathname or disk specifier from a filename, return it in a
  480. seperate array. We do this by initially copying the entire name in, then
  481. searching for the colon or slash. Right after the last one we find,
  482. stuff a null, removing the name part. 
  483.  
  484. Also return a pointer to the name part in the input name. */
  485.  
  486. char *strip_path(out,in)
  487. char *out;
  488. char *in;
  489. {
  490. char *name;
  491. char *endpath;
  492.  
  493.     strcpy(out,in);            /* duplicate, for working, */
  494.     name= in;            /* point to name, */
  495.     endpath= out;            /* and end of path part, */
  496.  
  497.     while (*in) {            /* look for slashes or colons, */
  498.         if (*in == ':')    {    /* if a colon, */
  499.             endpath= ++out;    /* point to name, */
  500.             name= ++in;
  501.  
  502.         } else if (*in == '/') {
  503.             endpath= ++out;    /* move the pointer up, */
  504.             name= ++in;
  505.         } else {
  506.             ++in;
  507.             ++out;
  508.         }
  509.     }
  510.     *endpath= '\0';            /* delete the name part, */
  511.     return(name);            /* return ptr to name part. */
  512. }
  513.  
  514. /* Convert a string to lower case. */
  515.  
  516. stolower(s)
  517. char *s;
  518. {
  519.     while (*s) {
  520.         *s= tolower(*s);
  521.         ++s;
  522.     }
  523. }
  524. /* Convert a string to upper case. */
  525.  
  526. stoupper(s)
  527. char *s;
  528. {
  529.     while (*s) {
  530.         *s= toupper(*s);
  531.         ++s;
  532.     }
  533. }
  534.  
  535. /* atoi() function missing from Lattice C. From Kernighan and Richie. */
  536.  
  537. atoi(s)
  538. char *s;
  539. {
  540. int n;
  541.     n= 0;
  542.     while ((*s >= '0') && (*s <= '9')) {
  543.         n *= 10;
  544.         n += *s - '0';
  545.         ++s;
  546.     }
  547.     return(n);
  548. }
  549.