You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 rivejä
2.1 KiB

  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <pipi.h>
  6. static const char * aliases[] =
  7. {
  8. "-o", "--save",
  9. "--output", "--save",
  10. NULL, NULL,
  11. };
  12. static pipi_command_t const *list;
  13. int main(int argc, char *argv[])
  14. {
  15. pipi_context_t *ctx;
  16. ctx = pipi_create_context();
  17. list = pipi_get_command_list();
  18. while(*++argv)
  19. {
  20. pipi_command_t const *cmd;
  21. char const * const *flag;
  22. char const * arg;
  23. int i;
  24. arg = argv[0];
  25. for(flag = aliases; flag[0]; flag += 2)
  26. if(!strcmp(arg, flag[0]))
  27. arg = flag[1];
  28. if(!strncmp(arg, "--", 2))
  29. {
  30. for(cmd = list; cmd->name; cmd++)
  31. if(!strcmp(arg + 2, cmd->name))
  32. break;
  33. if(!cmd->name)
  34. {
  35. fprintf(stderr, "unknown command %s\n", argv[0]);
  36. return EXIT_FAILURE;
  37. }
  38. for(i = 1; i <= cmd->argc; i++)
  39. if(argv[i] == NULL)
  40. {
  41. fprintf(stderr, "too few arguments for %s\n", argv[0]);
  42. return EXIT_FAILURE;
  43. }
  44. switch(cmd->argc)
  45. {
  46. case 0:
  47. if(pipi_command(ctx, cmd->name) != 0)
  48. {
  49. fprintf(stderr, "command %s failed\n", argv[0]);
  50. return EXIT_FAILURE;
  51. }
  52. break;
  53. case 1:
  54. if(pipi_command(ctx, cmd->name, argv[1]) != 0)
  55. {
  56. fprintf(stderr, "command %s failed\n", argv[0]);
  57. return EXIT_FAILURE;
  58. }
  59. break;
  60. default:
  61. /* Can’t handle that argument count */
  62. return EXIT_FAILURE;
  63. }
  64. argv += cmd->argc;
  65. }
  66. else
  67. {
  68. if(pipi_command(ctx, "load", arg) != 0)
  69. {
  70. fprintf(stderr, "could not load `%s'\n", argv[0]);
  71. return EXIT_FAILURE;
  72. }
  73. }
  74. }
  75. pipi_destroy_context(ctx);
  76. return EXIT_SUCCESS;
  77. }