9wait.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "lib.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "sys9.h"
  6. #include "dir.h"
  7. static char qsep[] = " \t\r\n";
  8. static char*
  9. qtoken(char *s)
  10. {
  11. int quoting;
  12. char *t;
  13. quoting = 0;
  14. t = s; /* s is output string, t is input string */
  15. while(*t!='\0' && (quoting || strchr(qsep, *t)==nil)){
  16. if(*t != '\''){
  17. *s++ = *t++;
  18. continue;
  19. }
  20. /* *t is a quote */
  21. if(!quoting){
  22. quoting = 1;
  23. t++;
  24. continue;
  25. }
  26. /* quoting and we're on a quote */
  27. if(t[1] != '\''){
  28. /* end of quoted section; absorb closing quote */
  29. t++;
  30. quoting = 0;
  31. continue;
  32. }
  33. /* doubled quote; fold one quote into two */
  34. t++;
  35. *s++ = *t++;
  36. }
  37. if(*s != '\0'){
  38. *s = '\0';
  39. if(t == s)
  40. t++;
  41. }
  42. return t;
  43. }
  44. static int
  45. tokenize(char *s, char **args, int maxargs)
  46. {
  47. int nargs;
  48. for(nargs=0; nargs<maxargs; nargs++){
  49. while(*s!='\0' && strchr(qsep, *s)!=nil)
  50. s++;
  51. if(*s == '\0')
  52. break;
  53. args[nargs] = s;
  54. s = qtoken(s);
  55. }
  56. return nargs;
  57. }
  58. Waitmsg*
  59. _WAIT(void)
  60. {
  61. int n, l;
  62. char buf[512], *fld[5];
  63. Waitmsg *w;
  64. n = _AWAIT(buf, sizeof buf-1);
  65. if(n < 0)
  66. return nil;
  67. buf[n] = '\0';
  68. if(tokenize(buf, fld, 5) != 5){
  69. strcpy(buf, "couldn't parse wait message");
  70. _ERRSTR(buf, sizeof buf);
  71. return nil;
  72. }
  73. l = strlen(fld[4])+1;
  74. w = malloc(sizeof(Waitmsg)+l);
  75. if(w == nil)
  76. return nil;
  77. w->pid = atoi(fld[0]);
  78. w->time[0] = atoi(fld[1]);
  79. w->time[1] = atoi(fld[2]);
  80. w->time[2] = atoi(fld[3]);
  81. w->msg = (char*)&w[1];
  82. memmove(w->msg, fld[4], l);
  83. return w;
  84. }