home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / bss / pup.arc / SUPPORT.C < prev    next >
C/C++ Source or Header  |  1987-11-13  |  8KB  |  338 lines

  1. #include <puppy.h>
  2. #include <ascii.h>
  3. #include <pupmem.h>
  4. /*
  5.     Various low level and extremely important support routines.
  6.  
  7. append(fname)    Open or create file fname for appending.
  8.  
  9. logoff(code,disc)
  10.         Terminate this call; disconnect from the modem if
  11.         disc is true; sets code as the DOS ERRORLEVEL for this
  12.         connect. This writes out the caller record as necessary, 
  13.         etc.
  14.  
  15. makefname(buf,name)
  16.         Creates a full pathname from the current (Msg, File,
  17.         Upload, pup.bbs) area. NOTE: If 'ovpath' is not blank,
  18.         it is used instead of the one from the area.
  19.  
  20. rline(f,line,len)
  21.         Read a line of text; a line is defined as characters
  22.         delimited by a CR. See the code for special cases. Returns
  23.         0 if end of file.
  24.  
  25. char *str_node(&node)
  26.         Generates a static string expression of the node, and 
  27.         returns a pointer to it. Through a kludge it can be 
  28.         called up to 4 times without overwriting a previous 
  29.         invokation.
  30.  
  31.  
  32. int num_args(string)    Returns the number of args in the string, seperated
  33.             by delims (see delim(), below). Leading delimiters
  34.             are ignored.
  35.  
  36. char *next_arg(string) Returns a pointer to the next arg, delimited 
  37.             by a delim, skipping over the current arg. Use via
  38.             ptr= nextarg(ptr) to skip to each argument. All 
  39.             switches at the end of the current arg are skipped.
  40.  
  41.  
  42. char *skip_delim(string) Skips leading delims in a string. returns a pointer.
  43.  
  44. cpyarg(to,from)    Copies a string, up to the next delim or switch.
  45.             Leading and trailing delimiters are stripped (from 
  46.             the output string) and a null terminator is added.
  47.  
  48.             after cpyarg()        FROM: foo/b foobar fipple
  49.                         TO: foo
  50.  
  51. char *strip_path(out,in) Copies the disk specifier or pathname to the output
  52.             array, and returns a pointer to the name in the input
  53.             string.    Drive specs are considered a path, and are
  54.             treated as such by DOS. Stripping "a:foo" and
  55.             "bin/foo/framis.asm" result in:
  56.  
  57.                 IN:    "A:"
  58.                 IN:    "bin\foo"
  59.  
  60.                 OUT:    "A:"
  61.                 OUT:    "bin\"
  62.  
  63. stolower(s)    Convert a string to all lower case
  64.  
  65. stoupper(s)    Convert a string to all upper case
  66.  
  67. atoi(s)        Convert a string of digits into a numeric value; stops
  68.         when the first non-digit character is found.
  69. */
  70.  
  71. /* Open a file for appending, creating it if necessary. Return the
  72. open handle, or -1 if error. */
  73.  
  74. append(s)
  75. char *s;
  76. {
  77. int h;
  78. char c;
  79.  
  80.     h= open(s,2);                /* open or create the */
  81.     if (h == -1) h= creat(s,2);        /* file, if opened OK */
  82.     else lseek(h,0L,2);            /* seek to the end */
  83.     return(h);                /* handle or error */
  84. }
  85.  
  86. /* Log a caller off the system; disconnect and force a termination. */
  87.  
  88. logoff(code,disc)
  89. int code,disc;
  90. {
  91. int f;
  92. char buff[SS];
  93. long p;
  94.  
  95.     limit= 0;                /* disable time limits */
  96.     doscode= code;                /* set result code, */
  97.     if (!test && disc) discon();        /* do disconnect, */
  98.     frc_abort();                /* force rturn to main */
  99. }
  100.  
  101. /* Copy the path prefix, append one filename from the input string. */
  102.  
  103. makefname(d,s)
  104. char *d,*s;
  105. {
  106.     strcpy(d,pup.filepref);        /* the path prefix, */
  107.     d= next_arg(d);            /* point to its end, */
  108.     cpyarg(d,s);            /* add one filename */
  109. }
  110.  
  111. /* Display a text file. */
  112.  
  113. dspfile(filename)
  114. char *filename;
  115. {
  116. int f;
  117.  
  118.     f= open(filename,0);
  119.     if (f == -1) return(0);
  120.     dumptext(f);
  121.     close(f);
  122.     return(1);
  123. }
  124.  
  125. /* Output the contents of a text file to the console. The handle of 
  126. an open file is passed. Output ceases when EOF or a Control-Z is found. */
  127.  
  128. dumptext(file)
  129. int file;
  130. {
  131. char lastc,c,buff[512];                /* local buffering */
  132. unsigned index,count;
  133.  
  134.     if (file == -1) return;                /* be serious */
  135.  
  136.     index= count= 0;                /* buffer is empty */
  137.     while (1) {
  138.         if (index >= count) {            /* read some if */
  139.             index= 0;            /* the local buffer */
  140.             count= read(file,buff,sizeof(buff)); /* is empty */
  141.         }
  142.         if (! count) break;            /* stop if empty file */
  143.         if (abort) break;            /* stop if aborted */
  144.  
  145.         c= buff[index++];            /* get a character, */
  146.         if (c == LF) continue;            /* ignore LFs */
  147.         if (c == CR + 128) {            /* special case soft CRs */
  148.             if (lastc == ' ') continue;
  149.             c= ' ';
  150.         }
  151.  
  152.         lastc= c;                /* remember it, */
  153.         if (c == CR) mputs("\r\n");        /* CR becomes CR/LF */
  154.         else fmconout(c);            /* else output text */
  155.         if (c == SUB) break;            /* ^Z is end of file */
  156.     }
  157.     mputs("\r\n");
  158. }
  159.  
  160. /* Write the system file out to disk. */
  161.  
  162. putsys() {
  163.  
  164. int f;
  165.  
  166.     f= open("puppy.sys",2);
  167.     if (f == -1) {
  168.         printf("Can't open PUPPY.SYS!\r\n");
  169.         return;
  170.     }
  171.     write(f,&pup,sizeof(struct _pup));
  172.     close(f);
  173. }
  174.  
  175. /* Read a line of text from the file, null terminate it. Function returns
  176. zero if EOF. Deletes all CRs and Control-Zs from the stream. Lines are
  177. terminated by LFs. */
  178.  
  179. rline(file,buf,len)
  180. int file;
  181. char *buf;
  182. int len;
  183. {
  184. int i;
  185. char notempty,c;
  186.  
  187.     i= 0; notempty= 0;
  188.     --len;                        /* compensate for added NUL */
  189.     while (i < len) {
  190.         if (! read(file,&c,1)) break;        /* stop if empty */
  191.         if (c == 0x1a) continue;        /* totally ignore ^Z, */
  192.         notempty= 1;                /* not empty */
  193.         if (c == '\r') continue;        /* skip CR, */
  194.         if (c == '\r' + 128) continue;        /* skip soft CR, */
  195.         if (c == '\n') break;            /* stop if LF */
  196.         buf[i++]= c;
  197.     }
  198.     buf[i]= '\0';
  199.     return(notempty);
  200. }
  201.  
  202. /* Return the number of args left in the string. */
  203.  
  204. num_args(s)
  205. char *s;
  206. {
  207. int count;
  208.  
  209.     count= 0;
  210.     s= skip_delim(s);            /* skip leading blanks, */
  211.     while (*s) {
  212.         ++count;            /* count one, */
  213.         s= next_arg(s);            /* find next, */
  214.     }
  215.     return(count);
  216. }
  217.  
  218. /* Return a pointer to the next argument in the string. */
  219.  
  220. char *next_arg(s)
  221. char *s;
  222. {
  223.     while ((!delim(*s)) && *s)        /* skip this one, */
  224.         ++s;                /* up to delim, */
  225.     s= skip_delim(s);            /* then skip delims, */
  226.     return(s);
  227. }
  228.  
  229. /* Skip over the leading delimiters in a string. */
  230.  
  231. char *skip_delim(s)
  232. char *s;
  233. {
  234.     while (delim(*s) && *s) {
  235.         ++s;
  236.     }
  237.     return(s);
  238. }
  239.  
  240. /* Copy the string to the destination array, stopping if we find one
  241. of our delimiters. */
  242.  
  243. cpyatm(to,from)
  244. char *to;
  245. char *from;
  246. {
  247.     while ( (!delim(*from)) && *from) 
  248.         *to++= *from++;
  249.     *to= '\0';
  250. }
  251.  
  252. /* Copy the string to the destination array, stopping if we find one
  253. of our delimiters. */
  254.  
  255. cpyarg(to,from)
  256. char *to;
  257. char *from;
  258. {
  259.     while (*from) {
  260.         if (delim(*from)) break;
  261.         *to++= *from++;
  262.     }
  263.     *to= '\0';
  264. }
  265.  
  266. /* Strip the pathname or disk specifier from a filename, return it in a
  267. seperate array. We do this by initially copying the entire name in, then
  268. searching for the colon or slash. Right after the last one we find,
  269. stuff a null, removing the name part. 
  270.  
  271. Also return a pointer to the name part in the input name. */
  272.  
  273. char *strip_path(out,in)
  274. char *out;
  275. char *in;
  276. {
  277. char *name;
  278. char *endpath;
  279.  
  280.     strcpy(out,in);            /* duplicate, for working, */
  281.     name= in;            /* point to name, */
  282.     endpath= out;            /* and end of path part, */
  283.  
  284.     while (*in) {            /* look for slashes or colons, */
  285.         if (*in == ':')    {    /* if a colon, */
  286.             endpath= ++out;    /* point to name, */
  287.             name= ++in;
  288.  
  289.         } else if ((*in == '/') || (*in == '\\')) {
  290.             endpath= ++out;    /* move the pointer up, */
  291.             name= ++in;
  292.         } else {
  293.             ++in;
  294.             ++out;
  295.         }
  296.     }
  297.     *endpath= '\0';            /* delete the name part, */
  298.     return(name);            /* return ptr to name part. */
  299. }
  300.  
  301. /* Convert a string to lower case. */
  302.  
  303. stolower(s)
  304. char *s;
  305. {
  306.     while (*s) {
  307.         *s= tolower(*s);
  308.         ++s;
  309.     }
  310. }
  311.  
  312. /* Convert a string to upper case. */
  313.  
  314. stoupper(s)
  315. char *s;
  316. {
  317.     while (*s) {
  318.         *s= toupper(*s);
  319.         ++s;
  320.     }
  321. }
  322.  
  323. /* atoi() function missing from Lattice C. From Kernighan and Richie. */
  324.  
  325. atoi(s)
  326. char *s;
  327. {
  328. int n;
  329.     n= 0;
  330.     while ((*s >= '0') && (*s <= '9')) {
  331.         n *= 10;
  332.         n += *s - '0';
  333.         ++s;
  334.     }
  335.     return(n);
  336. }
  337.  
  338.