input.c 1.7 KB

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