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