join.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* join F1 F2 on stuff */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #define F1 0
  7. #define F2 1
  8. #define F0 3
  9. #define NFLD 100 /* max field per line */
  10. #define comp() runecmp(ppi[F1][j1],ppi[F2][j2])
  11. FILE *f[2];
  12. Rune buf[2][BUFSIZ]; /*input lines */
  13. Rune *ppi[2][NFLD+1]; /* pointers to fields in lines */
  14. Rune *s1,*s2;
  15. int j1 = 1; /* join of this field of file 1 */
  16. int j2 = 1; /* join of this field of file 2 */
  17. int olist[2*NFLD]; /* output these fields */
  18. int olistf[2*NFLD]; /* from these files */
  19. int no; /* number of entries in olist */
  20. Rune sep1 = ' '; /* default field separator */
  21. Rune sep2 = '\t';
  22. char *sepstr=" ";
  23. int discard; /* count of truncated lines */
  24. Rune null[BUFSIZ] = L"";
  25. int a1;
  26. int a2;
  27. char *getoptarg(int*, char***);
  28. void output(int, int);
  29. int input(int);
  30. void oparse(char*);
  31. void error(char*, char*);
  32. void seek1(void), seek2(void);
  33. Rune *strtorune(Rune *, char *);
  34. void
  35. main(int argc, char **argv)
  36. {
  37. int i;
  38. while (argc > 1 && argv[1][0] == '-') {
  39. if (argv[1][1] == '\0')
  40. break;
  41. switch (argv[1][1]) {
  42. case '-':
  43. argc--;
  44. argv++;
  45. goto proceed;
  46. case 'a':
  47. switch(*getoptarg(&argc, &argv)) {
  48. case '1':
  49. a1++;
  50. break;
  51. case '2':
  52. a2++;
  53. break;
  54. default:
  55. error("incomplete option -a","");
  56. }
  57. break;
  58. case 'e':
  59. strtorune(null, getoptarg(&argc, &argv));
  60. break;
  61. case 't':
  62. sepstr=getoptarg(&argc, &argv);
  63. chartorune(&sep1, sepstr);
  64. sep2 = sep1;
  65. break;
  66. case 'o':
  67. if(argv[1][2]!=0 ||
  68. argc>2 && strchr(argv[2],',')!=0)
  69. oparse(getoptarg(&argc, &argv));
  70. else for (no = 0; no<2*NFLD && argc>2; no++){
  71. if (argv[2][0] == '1' && argv[2][1] == '.') {
  72. olistf[no] = F1;
  73. olist[no] = atoi(&argv[2][2]);
  74. } else if (argv[2][0] == '2' && argv[2][1] == '.') {
  75. olist[no] = atoi(&argv[2][2]);
  76. olistf[no] = F2;
  77. } else if (argv[2][0] == '0')
  78. olistf[no] = F0;
  79. else
  80. break;
  81. argc--;
  82. argv++;
  83. }
  84. break;
  85. case 'j':
  86. if(argc <= 2)
  87. break;
  88. if (argv[1][2] == '1')
  89. j1 = atoi(argv[2]);
  90. else if (argv[1][2] == '2')
  91. j2 = atoi(argv[2]);
  92. else
  93. j1 = j2 = atoi(argv[2]);
  94. argc--;
  95. argv++;
  96. break;
  97. case '1':
  98. j1 = atoi(getoptarg(&argc, &argv));
  99. break;
  100. case '2':
  101. j2 = atoi(getoptarg(&argc, &argv));
  102. break;
  103. }
  104. argc--;
  105. argv++;
  106. }
  107. proceed:
  108. for (i = 0; i < no; i++)
  109. if (olist[i]-- > NFLD) /* 0 origin */
  110. error("field number too big in -o","");
  111. if (argc != 3)
  112. error("usage: join [-1 x -2 y] [-o list] file1 file2","");
  113. j1--;
  114. j2--; /* everyone else believes in 0 origin */
  115. s1 = ppi[F1][j1];
  116. s2 = ppi[F2][j2];
  117. if (strcmp(argv[1], "-") == 0)
  118. f[F1] = stdin;
  119. else if ((f[F1] = fopen(argv[1], "r")) == 0)
  120. error("can't open %s", argv[1]);
  121. if(strcmp(argv[2], "-") == 0) {
  122. f[F2] = stdin;
  123. } else if ((f[F2] = fopen(argv[2], "r")) == 0)
  124. error("can't open %s", argv[2]);
  125. if(ftell(f[F2]) >= 0)
  126. seek2();
  127. else if(ftell(f[F1]) >= 0)
  128. seek1();
  129. else
  130. error("neither file is randomly accessible","");
  131. if (discard)
  132. error("some input line was truncated", "");
  133. exits("");
  134. }
  135. int runecmp(Rune *a, Rune *b){
  136. while(*a==*b){
  137. if(*a=='\0') return 0;
  138. a++;
  139. b++;
  140. }
  141. if(*a<*b) return -1;
  142. return 1;
  143. }
  144. char *runetostr(char *buf, Rune *r){
  145. char *s;
  146. for(s=buf;*r;r++) s+=runetochar(s, r);
  147. *s='\0';
  148. return buf;
  149. }
  150. Rune *strtorune(Rune *buf, char *s){
  151. Rune *r;
  152. for(r=buf;*s;r++) s+=chartorune(r, s);
  153. *r='\0';
  154. return buf;
  155. }
  156. /* lazy. there ought to be a clean way to combine seek1 & seek2 */
  157. #define get1() n1=input(F1)
  158. #define get2() n2=input(F2)
  159. void
  160. seek2()
  161. {
  162. int n1, n2;
  163. int top2=0;
  164. int bot2 = ftell(f[F2]);
  165. get1();
  166. get2();
  167. while(n1>0 && n2>0 || (a1||a2) && n1+n2>0) {
  168. if(n1>0 && n2>0 && comp()>0 || n1==0) {
  169. if(a2) output(0, n2);
  170. bot2 = ftell(f[F2]);
  171. get2();
  172. } else if(n1>0 && n2>0 && comp()<0 || n2==0) {
  173. if(a1) output(n1, 0);
  174. get1();
  175. } else /*(n1>0 && n2>0 && comp()==0)*/ {
  176. while(n2>0 && comp()==0) {
  177. output(n1, n2);
  178. top2 = ftell(f[F2]);
  179. get2();
  180. }
  181. fseek(f[F2], bot2, 0);
  182. get2();
  183. get1();
  184. for(;;) {
  185. if(n1>0 && n2>0 && comp()==0) {
  186. output(n1, n2);
  187. get2();
  188. } else if(n1>0 && n2>0 && comp()<0 || n2==0) {
  189. fseek(f[F2], bot2, 0);
  190. get2();
  191. get1();
  192. } else /*(n1>0 && n2>0 && comp()>0 || n1==0)*/{
  193. fseek(f[F2], top2, 0);
  194. bot2 = top2;
  195. get2();
  196. break;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. void
  203. seek1()
  204. {
  205. int n1, n2;
  206. int top1=0;
  207. int bot1 = ftell(f[F1]);
  208. get1();
  209. get2();
  210. while(n1>0 && n2>0 || (a1||a2) && n1+n2>0) {
  211. if(n1>0 && n2>0 && comp()>0 || n1==0) {
  212. if(a2) output(0, n2);
  213. get2();
  214. } else if(n1>0 && n2>0 && comp()<0 || n2==0) {
  215. if(a1) output(n1, 0);
  216. bot1 = ftell(f[F1]);
  217. get1();
  218. } else /*(n1>0 && n2>0 && comp()==0)*/ {
  219. while(n2>0 && comp()==0) {
  220. output(n1, n2);
  221. top1 = ftell(f[F1]);
  222. get1();
  223. }
  224. fseek(f[F1], bot1, 0);
  225. get2();
  226. get1();
  227. for(;;) {
  228. if(n1>0 && n2>0 && comp()==0) {
  229. output(n1, n2);
  230. get1();
  231. } else if(n1>0 && n2>0 && comp()>0 || n1==0) {
  232. fseek(f[F1], bot1, 0);
  233. get2();
  234. get1();
  235. } else /*(n1>0 && n2>0 && comp()<0 || n2==0)*/{
  236. fseek(f[F1], top1, 0);
  237. bot1 = top1;
  238. get1();
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. int
  246. input(int n) /* get input line and split into fields */
  247. {
  248. register int i, c;
  249. Rune *bp;
  250. Rune **pp;
  251. char line[BUFSIZ];
  252. bp = buf[n];
  253. pp = ppi[n];
  254. if (fgets(line, BUFSIZ, f[n]) == 0)
  255. return(0);
  256. strtorune(bp, line);
  257. i = 0;
  258. do {
  259. i++;
  260. if (sep1 == ' ') /* strip multiples */
  261. while ((c = *bp) == sep1 || c == sep2)
  262. bp++; /* skip blanks */
  263. *pp++ = bp; /* record beginning */
  264. while ((c = *bp) != sep1 && c != '\n' && c != sep2 && c != '\0')
  265. bp++;
  266. *bp++ = '\0'; /* mark end by overwriting blank */
  267. } while (c != '\n' && c != '\0' && i < NFLD-1);
  268. if (c != '\n')
  269. discard++;
  270. *pp = 0;
  271. return(i);
  272. }
  273. void
  274. output(int on1, int on2) /* print items from olist */
  275. {
  276. int i;
  277. Rune *temp;
  278. char buf[BUFSIZ];
  279. if (no <= 0) { /* default case */
  280. printf("%s", runetostr(buf, on1? ppi[F1][j1]: ppi[F2][j2]));
  281. for (i = 0; i < on1; i++)
  282. if (i != j1)
  283. printf("%s%s", sepstr, runetostr(buf, ppi[F1][i]));
  284. for (i = 0; i < on2; i++)
  285. if (i != j2)
  286. printf("%s%s", sepstr, runetostr(buf, ppi[F2][i]));
  287. printf("\n");
  288. } else {
  289. for (i = 0; i < no; i++) {
  290. if (olistf[i]==F0 && on1>j1)
  291. temp = ppi[F1][j1];
  292. else if (olistf[i]==F0 && on2>j2)
  293. temp = ppi[F2][j2];
  294. else {
  295. temp = ppi[olistf[i]][olist[i]];
  296. if(olistf[i]==F1 && on1<=olist[i] ||
  297. olistf[i]==F2 && on2<=olist[i] ||
  298. *temp==0)
  299. temp = null;
  300. }
  301. printf("%s", runetostr(buf, temp));
  302. if (i == no - 1)
  303. printf("\n");
  304. else
  305. printf("%s", sepstr);
  306. }
  307. }
  308. }
  309. void
  310. error(char *s1, char *s2)
  311. {
  312. fprintf(stderr, "join: ");
  313. fprintf(stderr, s1, s2);
  314. fprintf(stderr, "\n");
  315. exits(s1);
  316. }
  317. char *
  318. getoptarg(int *argcp, char ***argvp)
  319. {
  320. int argc = *argcp;
  321. char **argv = *argvp;
  322. if(argv[1][2] != 0)
  323. return &argv[1][2];
  324. if(argc<=2 || argv[2][0]=='-')
  325. error("incomplete option %s", argv[1]);
  326. *argcp = argc-1;
  327. *argvp = ++argv;
  328. return argv[1];
  329. }
  330. void
  331. oparse(char *s)
  332. {
  333. for (no = 0; no<2*NFLD && *s; no++, s++) {
  334. switch(*s) {
  335. case 0:
  336. return;
  337. case '0':
  338. olistf[no] = F0;
  339. break;
  340. case '1':
  341. case '2':
  342. if(s[1] == '.' && isdigit(s[2])) {
  343. olistf[no] = *s=='1'? F1: F2;
  344. olist[no] = atoi(s += 2);
  345. break;
  346. } /* fall thru */
  347. default:
  348. error("invalid -o list", "");
  349. }
  350. if(s[1] == ',')
  351. s++;
  352. }
  353. }