home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume2 / basic / part2 / newbs / scon_in.c < prev   
Encoding:
Text File  |  1986-11-30  |  1.4 KB  |  60 lines

  1. /* scon_in() -- read in a string constant using input.
  2.  *    Format of an scon is either a quoted string, or a sequence
  3.  *    of characters ended with a seperator (' ', '\t' or '\n' or ',').
  4.  *
  5.  *    In either mode, you can get funny characters into the string by
  6.  *    "quoting" them with a '\'.
  7.  *
  8.  * scon_in() uses myalloc() to create space to store the string in.
  9.  */
  10. char *scon_in()
  11. {
  12.     register char c,*s;
  13.     static char text [80];
  14.  
  15.     s = &text[0];
  16.  
  17. /* beginning state, skip seperators until something interesting comes along */
  18.  
  19. l1: c=input();
  20.     if(c == '"') goto l2;
  21.     else if(c=='\n' || c=='\0') {
  22.     rdlin(bsin);
  23.     goto l1;
  24.     }
  25.     else if(c==' ' || c=='\t' || c==',') goto l1;
  26.     else goto l3;
  27.  
  28. /* have skipped unwanted material, seen a '"', read in a quoted string */
  29.  
  30. l2: c=input();
  31.     if(c == '\n') {
  32.     fprintf(stderr,"scon_in: unterminated string\n");
  33.     exit(1);
  34.     }
  35.     else if(c == '\\') { *s++ = bslash(bsin); goto l2; }
  36.     else if(c == '"')
  37.     if((c=input()) == '"') {
  38.         *s++ = '"';
  39.         goto l2;
  40.     }
  41.     else goto done;
  42.     else { *s++ = c; goto l2; }
  43.  
  44. /* skipped unwanted, seen something interesting, not '"', gather until sep */
  45.  
  46. l3: *s++ = c;
  47.     c=input();
  48.     if(c == '\\') { c = bslash(bsin); goto l3; }
  49.     else if(c==' ' || c=='\t' || c==',' || c=='\n') goto done;
  50.     else goto l3;
  51.  
  52. /* final state (if machine finished ok.) */
  53.  
  54. done: unput(c);
  55.     *s++ = '\0';
  56.     s=myalloc(strlen(text)+1);
  57.     strcpy(s,text);
  58.     return(s);
  59. }
  60.