1
0

spout.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <bio.h>
  5. void spout(int, char*);
  6. Biobuf bout;
  7. void
  8. main(int argc, char *argv[])
  9. {
  10. int i, fd;
  11. Binit(&bout, 1, OWRITE);
  12. if(argc == 1)
  13. spout(0, "");
  14. else
  15. for(i=1; i<argc; i++){
  16. fd = open(argv[i], OREAD);
  17. if(fd < 0){
  18. fprint(2, "spell: can't open %s: %r\n", argv[i]);
  19. continue;
  20. }
  21. spout(fd, argv[i]);
  22. close(fd);
  23. }
  24. exits(nil);
  25. }
  26. Biobuf b;
  27. void
  28. spout(int fd, char *name)
  29. {
  30. char *s, *t, *w;
  31. Rune r;
  32. int inword, wordchar;
  33. int n, wn, wid, c, m;
  34. char buf[1024];
  35. Binit(&b, fd, OREAD);
  36. n = 0;
  37. wn = 0;
  38. while((s = Brdline(&b, '\n')) != nil){
  39. if(s[0] == '.')
  40. for(c=0; c<3 && *s>' '; c++){
  41. n++;
  42. s++;
  43. }
  44. inword = 0;
  45. w = s;
  46. t = s;
  47. do{
  48. c = *(uchar*)t;
  49. if(c < Runeself)
  50. wid = 1;
  51. else{
  52. wid = chartorune(&r, t);
  53. c = r;
  54. }
  55. wordchar = 0;
  56. if(isalpha(c))
  57. wordchar = 1;
  58. if(inword && !wordchar){
  59. if(c=='\'' && isalpha(t[1]))
  60. goto Continue;
  61. m = t-w;
  62. if(m > 1){
  63. memmove(buf, w, m);
  64. buf[m] = 0;
  65. Bprint(&bout, "%s:#%d,#%d:%s\n", name, wn, n, buf);
  66. }
  67. inword = 0;
  68. }else if(!inword && wordchar){
  69. wn = n;
  70. w = t;
  71. inword = 1;
  72. }
  73. if(c=='\\' && (isalpha(t[1]) || t[1]=='(')){
  74. switch(t[1]){
  75. case '(':
  76. m = 4;
  77. break;
  78. case 'f':
  79. if(t[2] == '(')
  80. m = 5;
  81. else
  82. m = 3;
  83. break;
  84. case 's':
  85. if(t[2] == '+' || t[2]=='-'){
  86. if(t[3] == '(')
  87. m = 6;
  88. else
  89. m = 4;
  90. }else{
  91. if(t[2] == '(')
  92. m = 5;
  93. else if(t[2]=='1' || t[2]=='2' || t[2]=='3')
  94. m = 4;
  95. else
  96. m = 3;
  97. }
  98. break;
  99. default:
  100. m = 2;
  101. }
  102. while(m-- > 0){
  103. if(*t == '\n')
  104. break;
  105. n++;
  106. t++;
  107. }
  108. continue;
  109. }
  110. Continue:
  111. n++;
  112. t += wid;
  113. }while(c != '\n');
  114. }
  115. Bterm(&b);
  116. }