PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/usb-modeswitch-1.2.3/dispatcher.c

#
C | 88 lines | 40 code | 13 blank | 35 comment | 6 complexity | e8068df35f4f9e74711f589bd05b9604 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0
  1. /*
  2. * Copyright (c) 2011-2012 Josua Dietze, usb_modeswitch version 1.2.3
  3. * Contains code under
  4. * Copyright (c) 2010 Wojciech A. Koszek <wkoszek@FreeBSD.org>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $Id$
  29. */
  30. #include <assert.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <jim.h>
  35. /* RAW is defined to the complete Tcl code in that file: */
  36. #include "usb_modeswitch.string"
  37. #define MAX_ARGSIZE 1024
  38. int main(int argc, char **argv)
  39. {
  40. int i, retval;
  41. char arg[MAX_ARGSIZE];
  42. char arglist[MAX_ARGSIZE];
  43. Jim_Interp *interp;
  44. interp = NULL;
  45. arglist[0] = '\0';
  46. for (i=1; i<argc; i++) {
  47. if (strlen(argv[i]) > MAX_ARGSIZE-4) {
  48. printf("Argument #%d extends maximum size, skip it\n", i);
  49. continue;
  50. }
  51. if ( (strlen(arglist) + strlen(argv[i])) > MAX_ARGSIZE-4) {
  52. printf("Argument #%d would extend maximum list size, skip it\n", i);
  53. continue;
  54. }
  55. sprintf(arg,"{%s} ",argv[i]);
  56. strncat(arglist,arg,MAX_ARGSIZE-1);
  57. }
  58. char code[sizeof(RAW) + sizeof(arglist) + 28];
  59. sprintf(code, "set argv {%s}\nset argc %d\n", arglist, argc-1);
  60. strncat(code, RAW, sizeof(RAW));
  61. /* Create Jim instance */
  62. interp = Jim_CreateInterp();
  63. assert(interp != NULL && "Could not create interpreter!");
  64. /* Register base commands, actually implementing Tcl */
  65. Jim_RegisterCoreCommands(interp);
  66. /* Initialize any static extensions */
  67. Jim_InitStaticExtensions(interp);
  68. /* Evaluate the script that's now in "code" */
  69. retval = Jim_Eval(interp, code);
  70. if (retval < 0)
  71. printf("Evaluation returned error %d\n", retval);
  72. /* Free the interpreter */
  73. Jim_FreeInterp(interp);
  74. return (EXIT_SUCCESS);
  75. }