c-hglib
 All Data Structures Files Functions Variables Typedefs Macros
main.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include <sys/wait.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 
11 #include "client.h"
12 #include "utils.h"
13 
14 #define INIT_REPO "init_test_repo"
15 
16 /****** Convenience functions. *******/
17 
21 void setup_tmp()
22 {
23  system("hg init tmp");
24  chdir("tmp");
25 }
26 
30 void clean_tmp()
31 {
32  chdir("..");
33  system("rm -rf tmp");
34 }
35 
39 void setup_init()
40 {
41  system("mkdir tmp");
42  chdir("tmp");
43 }
44 
49 {
50  chdir(INIT_REPO);
51  system("touch foo ; hg add foo ; hg commit -m foo");
52  system("echo baloo > foo ; hg commit -m 'baloo text'");
53  chdir("..");
54 }
55 
59 void setup_log()
60 {
61  system("touch foo ; hg add foo ; hg commit -m foo");
62  system("echo baloo > foo ; hg commit -m 'baloo text'");
63  system("touch voodoo ; hg add voodoo ; hg commit -m voodoo");
64  system("echo voodoo > voodoo ; hg commit -m 'voodoo text'");
65 }
66 
71 {
72  system("touch foo ; hg add foo ; hg commit -m foo");
73  system("echo baloo > foo ; hg commit -m 'baloo text'");
74  system("hg up 0");
75  system("touch boo ; hg add boo ; hg commit -m boo");
76  system("echo voodoo > boo ; hg commit -m 'voodoo text'");
77 }
78 
83 {
84  system("touch foo ; hg add foo ; hg commit -m foo");
85  system("echo baloo > foo ; hg commit -m 'baloo text'");
86  system("hg up 0");
87  system("echo voodoo > foo ; hg commit -m 'voodoo text'");
88 }
89 
94 {
95  system("hg init export");
96  chdir("export");
97  system("touch foo ; echo baloo > foo; hg add foo ; hg commit -m foo");
98  chdir("..");
99  system("hg init import");
100 }
101 
106 {
107  system("rm -rf export import");
108 }
109 
114 {
115  system("touch 'foo bar'");
116  printf("---- ls command ----\n");
117  system("ls -l");
118 }
119 
120 /******* By hand implementations. ******/
121 
135 {
136  pid_t cpid;
137  int status;
138  hg_handle *handle;
139  char command[50];
140 
141  sprintf(command, "hg init %s", INIT_REPO);
142 
143  if((cpid = fork()) < 0) {
144  printf("Fork failed\n");
145  return NULL;
146 
147  } else if(cpid == 0) {
148  execl("/bin/sh", "sh", "-c", command, NULL);
149  printf("dadads\n\n");
150  } else {
151  waitpid( cpid, &status, 0);
152  }
153 
154  handle = hg_open(INIT_REPO, "");
155 
156  return handle;
157 }
158 
168 {
169  char buff[4096];
170  char *comm[] = {"log", "-v"};
171  int exitcode;
172  int ns;
173 
174  hg_rawcommand(handle, comm, 2);
175 
176  while(hg_channel(handle) != 'r'){
177  while(ns = hg_rawread(handle, buff, 4096), ns > 0){
178  printf("%s", buff);
179  }
180  }
181 
182  exitcode = hg_exitcode(handle);
183  printf("exitcode = %d\n", exitcode);
184 
185  return exitcode;
186 }
187 
198 int hg_import_by_hand(hg_handle *handle, char *import_patch)
199 {
200  char *comm[] = {"import", "-"};
201  char buff[4096];
202  int exitcode = 0;
203  int fd, ns;
204  hg_header header;
205 
206  fd = open(import_patch, O_RDONLY);
207  hg_rawcommand(handle, comm, 2);
208 
209  while(hg_channel(handle) != 'r'){
210  /* outchannels 'o' or 'e'. */
211  while(ns = hg_rawread(handle, buff, 4096), ns > 0){
212  printf("%s", buff);
213  }
214  if(hg_channel(handle) == 'L'){
215  header = hg_head(handle);
216  int length = read(fd, buff, header.length);
217  hg_rawwrite(handle, buff, length);
218  }
219  }
220 
221  exitcode = hg_exitcode(handle);
222  return exitcode;
223 }
224 
229 char prompt_function(char *output)
230 {
231  char option;
232  scanf("\n%c", &option);
233  return option;
234 }
235 
248 int hg_merge_by_hand(hg_handle *handle, char (*prompt)(char *))
249 {
250  char *comm[] = {"merge", "--tool=internal:prompt"};
251  char buff[4096];
252  int exitcode = 0;
253  int ns;
254 
255  hg_rawcommand(handle, comm, 2);
256 
257  while(hg_channel(handle) != 'r'){
258  /* outchannels 'o' or 'e'. */
259  while(ns = hg_rawread(handle, buff, 4096), ns > 0){
260  printf("%s", buff);
261  }
262  if(hg_channel(handle) == 'I'){
263  printf("INPUT\n");
264  }
265  else if(hg_channel(handle) == 'L'){
266  /* In stand of the default prompt_msg, the argument for
267  * prompt function will be the output data received
268  * until this moment.
269  * */
270  char option = (*prompt)(NULL);
271  printf("\noption = %c\n", option);
272  hg_rawwrite(handle, &option, 1);
273  /* TODO: is a small issue, but not for this level.
274  * The second write, must not be necessary.
275  * */
276  hg_rawwrite(handle, &option, 0);
277  }
278  }
279 
280  exitcode = hg_exitcode(handle);
281  printf("exitcode = %d\n", exitcode);
282 
283  return exitcode;
284 }
285 
295 {
296  char *comm[] = {"verify"};
297  char buff[4096];
298  int exitcode = 0;
299  int ns;
300 
301  hg_rawcommand(handle, comm, 1);
302 
303  while(hg_channel(handle) != 'r'){
304  while(ns = hg_rawread(handle, buff, 4096), ns > 0){
305  if(hg_channel(handle) == 'o'){
306  printf("out = %s", buff);
307  }
308  else if(hg_channel(handle) == 'e'){
309  printf("err = %s", buff);
310  }
311  }
312  }
313 
314  exitcode = hg_exitcode(handle);
315  printf("exitcode = %d\n", exitcode);
316 
317  return exitcode;
318 }
319 
329 {
330  char *export_comm[] = {"export", "-r", "0"};
331  char *import_comm[] = {"import", "-"};
332  char ebuff[4096], ibuff[4096];
333  int es, is;
334 
335  hg_rawcommand(ehandle, export_comm, 3);
336  hg_rawcommand(ihandle, import_comm, 2);
337 
338  while(hg_channel(ehandle) != 'r'){
339  while(es = hg_rawread(ehandle, ebuff, 4096), es > 0){
340  while(is = hg_rawread(ihandle, ibuff, 4096), is > 0){
341  printf("%s", ibuff);
342  }
343  if(hg_channel(ihandle) == 'L'){
344  hg_rawwrite(ihandle, ebuff, strlen(ebuff));
345  }
346  }
347  }
348  /* Send a message to ihandle to understand that the import process
349  * was ending*/
350  hg_rawwrite(ihandle, ebuff, 0);
351 
352  while(hg_channel(ihandle) != 'r'){
353  while(is = hg_rawread(ihandle, ibuff, 4096), is > 0){
354  printf("%s", ibuff);
355  }
356  }
357 
358  printf("exitcode for export process is %d \n", hg_exitcode(ehandle));
359  printf("exitcode for import process is %d \n", hg_exitcode(ihandle));
360 
361 }
362 
369 {
370  char buff[4096];
371  char *comm[] = {"add", "foo bar"};
372  int exitcode;
373  int ns;
374 
375  hg_rawcommand(handle, comm, 2);
376 
377  while(hg_channel(handle) != 'r'){
378  while(ns = hg_rawread(handle, buff, 4096), ns > 0){
379  printf("%s", buff);
380  }
381  }
382 
383  exitcode = hg_exitcode(handle);
384  printf("exitcode = %d\n", exitcode);
385 
386  return exitcode;
387 }
388 
394 {
395  printf("Select test case to run:\n");
396  printf("0) init - 'make some commits' & log \n");
397  printf("1) log \n");
398  printf("2) import - from stdin (simulate from a file) \n");
399  printf("3) merge - (solve conflicts from stdin) \n");
400  printf("4) merge - (without conflicts)\n");
401  printf("5) verify \n");
402  printf("6) verify a corrupt repo.\n");
403  printf("7) export-import example.\n");
404  printf("8) add 'filename with space'\n");
405  printf("\n");
406  printf("Your choice: ");
407 }
408 
409 
410 /***** Main function. *******/
414 int main(int argc, char **argv)
415 {
416  int select_case;
417  hg_handle *handle;
418  hg_handle *ehandle, *ihandle;
419 
421  scanf("%d", &select_case);
422  if(select_case < 0 || select_case > 8){
423  printf("Your choice is not an option...\n");
424  return -1;
425  }
426 
427  switch(select_case){
428  case 0:
429  setup_init();
430  handle = hg_init_by_hand();
431  post_init_setup();
432  hg_log_by_hand(handle);
433 
434  hg_close(&handle);
435  clean_tmp();
436  break;
437  case 1:
438  setup_tmp();
439  setup_log();
440  handle = hg_open(NULL, "");
441  hg_log_by_hand(handle);
442 
443  hg_close(&handle);
444  clean_tmp();
445  break;
446  case 2:
448  ihandle = hg_open("import", "");
449 
450  chdir("export");
451  system("hg export -r 0 > r0.diff");
452  chdir("..");
453  hg_import_by_hand(ihandle, "export/r0.diff");
454 
455  printf("\n");
456  hg_log_by_hand(ihandle);
457 
458  hg_close(&ihandle);
460  break;
461  case 3:
462  case 4:
463  setup_tmp();
464  if(select_case == 3)
466  else
467  setup_merge();
468  handle = hg_open(NULL, "");
470  system("hg ci -m 'merge'");
471  system("hg glog");
472 
473  hg_close(&handle);
474  clean_tmp();
475  break;
476  case 5:
477  case 6:
478  setup_tmp();
479  setup_log();
480  handle = hg_open(NULL, "");
481  printf("\n");
482  if(select_case == 6)
483  system("rm .hg/store/data/foo.i");
484  hg_verify_by_hand(handle);
485 
486  hg_close(&handle);
487  clean_tmp();
488  break;
489  case 7:
491  ehandle = hg_open("export", "");
492  ihandle = hg_open("import", "");
493  hg_export_import_by_hand(ehandle, ihandle);
494  hg_close(&ehandle);
495  hg_close(&ihandle);
497  break;
498  case 8:
499  setup_tmp();
501  printf("\n");
502  handle = hg_open(NULL, "");
504  printf("\n---- hs status command ----\n");
505  system("hg status");
506  clean_tmp();
507  break;
508  default:
509  break;
510  }
511 
512  return 0;
513 }