input.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <grass/glocale.h>
  6. #include "local_proto.h"
  7. int input(char *blank1, char *word1, char *blank2, char *word2, char *rest,
  8. FILE *infile)
  9. {
  10. char buf[1024];
  11. char *b, *w1, *w2;
  12. int string_size;
  13. if (isatty(0))
  14. fprintf(stderr, "> ");
  15. *blank1 = *blank2 = 0;
  16. *word1 = *word2 = *rest = 0;
  17. if (!fgets(buf, 1024, infile)) {
  18. *buf = 0;
  19. return 0;
  20. }
  21. /* note ebuf and nbuf in main.c (w1 and w2 here) are only 256 chars, we
  22. check here to make sure we don't move the pointer past sizeof(buf) */
  23. if (strlen(buf) >= 1023)
  24. G_fatal_error(_("One coordinate pair per line, please"));
  25. b = buf;
  26. w1 = word1;
  27. w2 = word2;
  28. while (*b == ' ' || *b == '\t' || *b == ',')
  29. *blank1++ = *b++;
  30. *blank1 = 0;
  31. while (*b != '\n' && *b != ' ' && *b != '\t' && *b != ',')
  32. *word1++ = *b++;
  33. *word1 = 0;
  34. string_size = strlen(w1);
  35. G_debug(5, "strlen w1=%d [%s]", string_size, w1);
  36. if (string_size > 255)
  37. G_fatal_error(_("One coordinate pair per line, please"));
  38. while (*b == ' ' || *b == '\t' || *b == ',')
  39. *blank2++ = *b++;
  40. *blank2 = 0;
  41. while (*b != '\n' && *b != ' ' && *b != '\t' && *b != ',')
  42. *word2++ = *b++;
  43. *word2 = 0;
  44. string_size = strlen(w2);
  45. G_debug(5, "strlen w2=%d [%s]", string_size, w2);
  46. if (string_size > 255)
  47. G_fatal_error(_("One coordinate pair per line, please"));
  48. /* bug? really = and not ==? */
  49. /* not a bug: we are filling "rest" with the remaining fgets buffer.
  50. This goes unused though so could be ripped out if a worry */
  51. while ((*rest++ = *b++)) ;
  52. if (isatty(0) && strcmp("end", w1) == 0 && *w2 == 0)
  53. return 0;
  54. return 1;
  55. }