home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / doc / intlist / int.c < prev    next >
C/C++ Source or Header  |  1993-01-03  |  7KB  |  277 lines

  1. #ifdef      __TURBOC__
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. /*
  6. *    Force the error for non Borland compilers.
  7. *    NOTE:  This may or may not work for you.
  8. */
  9. #endif
  10.  
  11.  
  12.  
  13.  
  14.  
  15. /*
  16. *    8-bit Internationalization Strings.
  17. */
  18. char*     msg_switchars       = "+/-";
  19. char*     msg_cant_open_file  = "Unable to open %s\n";
  20. char*     msg_usage      = /* No separator until the end. */
  21.      "Use int(1) check the return values of any interrupt.\n\n"
  22.      "%s usage:\n"
  23.      "int [h] int=0x10 [{abcd}{xhl}=0xFFFF]\n"
  24.      "where:\n"
  25.      "h\tdisplays this usage.\n"
  26.      "int       \tis the interrupt function to specify.\n"
  27.      "ax, ah, al\tis set to the specified word or byte value in hex.\n"
  28.      "bx, bh, bl\tis set to the specified word or byte value in hex.\n"
  29.      "cx, ch, cl\tis set to the specified word or byte value in hex.\n"
  30.      "dx, dh, dl\tis set to the specified word or byte value in hex.\n";
  31.  
  32. char msg_off_switchar= '-';
  33. char*     msg_help  = "h";
  34.  
  35. char*     msg_int        = "int=";
  36. char*     msg_ax         = "ax=";
  37. char*     msg_ah         = "ah=";
  38. char*     msg_al         = "al=";
  39.  
  40. char*     msg_bx         = "bx=";
  41. char*     msg_bh         = "bh=";
  42. char*     msg_bl         = "bl=";
  43.  
  44. char*     msg_cx         = "cx=";
  45. char*     msg_ch         = "ch=";
  46. char*     msg_cl         = "cl=";
  47.  
  48. char*     msg_dx         = "dx=";
  49. char*     msg_dh         = "dh=";
  50. char*     msg_dl         = "dl=";
  51.  
  52. char*     msg_cs         = "cs=";
  53. char*     msg_ds         = "ds=";
  54. char*     msg_es         = "es=";
  55. char*     msg_ss         = "ss=";
  56.  
  57. char*     msg_si         = "si=";
  58. char*     msg_di         = "di=";
  59.  
  60. char*     msg_bp         = "bp=";
  61. char*     msg_sp         = "sp=";
  62.  
  63. char*     msg_flags[]    =
  64. {
  65.      "Carry Flag Set\t", /* 0x0001 */
  66.      "Parity Flag Set\t",     /* 0x0002 */
  67.      "Auxilliary Flag Set\t",/* 0x0004 */
  68.      "Zero Flag Set\t",  /* 0x0008 */
  69.      "Sign Flag Set\t",  /* 0x0010 */
  70.      "Trap Flag Set\t",  /* 0x0020 */
  71.      "Interrupt Flag Set\t",  /* 0x0040 */
  72.      "Direction Flag Set\t",  /* 0x0080 */
  73.      "Overflow Flag Set\t"    /* 0x0100 */
  74. };
  75.  
  76.  
  77.  
  78.  
  79. /*
  80. *    Global Variables
  81. */
  82. char*     me;
  83. char intrrupt  = 0;
  84. union     REGS regs = {0};
  85. struct    SREGS     sregs     = {0};
  86.  
  87.  
  88. /*----------------------------------------------------------------------
  89. *    usage()   -- Display our usage.
  90. *
  91. */
  92. void usage( void )
  93. {
  94.      printf( msg_usage, me );
  95.      exit( 1 );
  96. }
  97.  
  98.  
  99.  
  100.  
  101. /*----------------------------------------------------------------------
  102. *    display_registers() -- display the current contents of regs & sregs.
  103. *
  104. */
  105. void display_registers( void )
  106. {
  107.      unsigned i;
  108.      unsigned j;
  109.  
  110.      printf( "\n" );
  111.      printf( msg_ax );   printf( "0x%04X\t", regs.x.ax );
  112.      printf( msg_bx );   printf( "0x%04X\t", regs.x.bx );
  113.      printf( msg_cx );   printf( "0x%04X\t", regs.x.cx );
  114.      printf( msg_dx );   printf( "0x%04X\t", regs.x.dx );
  115.      printf( msg_si );   printf( "0x%04X\t", regs.x.si );
  116.      printf( msg_di );   printf( "0x%04X\t", regs.x.di );
  117.      printf( msg_sp );   printf( "0x%04X\t", _SP );
  118.      printf( msg_bp );   printf( "0x%04X\t", _BP );
  119.      printf( msg_cs );   printf( "0x%04X\t", sregs.cs );    
  120.      printf( msg_ds );   printf( "0x%04X\t", sregs.ds );    
  121.      printf( msg_es );   printf( "0x%04X\t", sregs.es );    
  122.      printf( msg_ss );   printf( "0x%04X\t", sregs.ss );    
  123.      printf( "\n" );
  124.  
  125.      for( i=0x100, j=0; i; i>>=1, j++ )
  126.      {
  127.           if  (i & regs.x.flags)
  128.                printf( msg_flags[j] );
  129.      }
  130.      printf( "\n" );
  131. }
  132.  
  133.  
  134.  
  135.  
  136. /*----------------------------------------------------------------------
  137. *    gen_int() -- Generate a generic Interrupt call.
  138. *
  139. */
  140. void gen_int( void )
  141. {
  142.      if  (intrrupt)
  143.      {
  144.           sregs.cs = _CS;
  145.           sregs.ds = _DS;
  146.           sregs.es = _ES;
  147.           sregs.ss = _SS;
  148.           display_registers();
  149.           printf( "INTERRUPT: 0x%02X\n", intrrupt );
  150.           int86x( intrrupt, ®s, ®s, &sregs );
  151.           display_registers();
  152.      }
  153. }
  154.  
  155.  
  156.  
  157.  
  158. /*----------------------------------------------------------------------
  159. *    setregw() -- set the word to be the value of the string 
  160. *                 after the "=" sign.
  161. *
  162. */
  163. void setregw( regw, str )
  164. unsigned* regw;
  165. char*     str;
  166. {
  167.      char*     p    = strchr( str, '=' ) + 1;
  168.      long scratch   = 0L;
  169.  
  170.      if  (p)
  171.      {
  172.           scratch = strtol( p, &p, 0x10 );
  173.           *regw     = (unsigned)(scratch & 0xFFFF);
  174.      }
  175. }
  176.  
  177.  
  178.  
  179.  
  180. /*----------------------------------------------------------------------
  181. *    setregb() -- set the byte to be the value of the string 
  182. *                 after the "=" sign.
  183. *
  184. */
  185. void setregb( regb, str )
  186. char*     regb;
  187. char*     str;
  188. {
  189.      char*     p    = (char*)strchr( str, '=' ) + 1;
  190.      long scratch   = 0L;
  191.  
  192.      if  (p)
  193.      {
  194.           scratch = strtol( p, &p, 0x10 );
  195.           *regb     = (unsigned)(scratch & 0xFFFF);
  196.      }
  197. }
  198.  
  199.  
  200.  
  201.  
  202. /*----------------------------------------------------------------------
  203. *    cmp()          -- handle all partial comparisons for parse_args().
  204. *
  205. */
  206. char cmp( arg, constant )
  207. char*     arg;
  208. char*     constant;
  209. {
  210.      unsigned  arglen    = strlen( arg );
  211.      unsigned  equals    = strlen( strchr( arg, '=' ) );
  212.      unsigned  cmplen    = arglen - equals;
  213.  
  214.      return( (!strncmpi( arg, constant, cmplen )) ? 1 : 0 );
  215. }
  216.  
  217.  
  218.  
  219.  
  220. /*----------------------------------------------------------------------
  221. *    parse_args()   -- simple argument parsing.
  222. *
  223. *    Argument parsing is GNUish.
  224. *         '-' is off
  225. *         '+' is on
  226. *         '/' is DOSishly on.
  227. *
  228. *    First character of arg is expected to be a switchar.
  229. */
  230. void parse_args( arg )
  231. char*     arg;
  232. {
  233.      char j  = ((strchr( msg_switchars, arg[0] ) == NULL) ? 0 : 1);
  234.      char on = ((*arg == msg_off_switchar) ? 0 : 1);
  235.  
  236.      if  (cmp( arg+j, msg_help )) usage();
  237.      if  (cmp( arg+j, msg_int  )) setregw( &intrrupt,  arg );
  238.      if  (cmp( arg+j, msg_ax   )) setregw( ®s.x.ax, arg );
  239.      if  (cmp( arg+j, msg_ah   )) setregb( ®s.h.ah, arg );
  240.      if  (cmp( arg+j, msg_al   )) setregb( ®s.h.al, arg );
  241.      if  (cmp( arg+j, msg_bx   )) setregw( ®s.x.bx, arg );
  242.      if  (cmp( arg+j, msg_bh   )) setregb( ®s.h.bh, arg );
  243.      if  (cmp( arg+j, msg_bl   )) setregb( ®s.h.bl, arg );
  244.      if  (cmp( arg+j, msg_cx   )) setregw( ®s.x.cx, arg );
  245.      if  (cmp( arg+j, msg_ch   )) setregb( ®s.h.ch, arg );
  246.      if  (cmp( arg+j, msg_cl   )) setregb( ®s.h.cl, arg );
  247.      if  (cmp( arg+j, msg_dx   )) setregw( ®s.x.dx, arg );
  248.      if  (cmp( arg+j, msg_dh   )) setregb( ®s.h.dh, arg );
  249.      if  (cmp( arg+j, msg_dl   )) setregb( ®s.h.dl, arg );
  250. }
  251.  
  252.  
  253.  
  254.  
  255. /*----------------------------------------------------------------------
  256. *    main()    -- Handle switches, do the work.
  257. *
  258. */
  259. unsigned  main( argc, argv )
  260. char argc;
  261. char*     argv[];
  262. {
  263.      char i;
  264.  
  265.      me = argv[0];
  266.      for( i=1; i<argc; i++ )
  267.      {
  268.           parse_args( argv[i] );
  269.      }
  270.  
  271.      if  (!intrrupt)
  272.           usage();
  273.  
  274.      gen_int();
  275.      return( 0 );
  276. }
  277.