Advertisement
codemonkey

builtins/history.c

Oct 13th, 2011
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.49 KB | None | 0 0
  1. #include "history.h"
  2.  
  3. void h_add(char** param)
  4. {
  5. #ifdef DEBUG
  6.     // Print that we're in the h_add function
  7.     printf("~ Add to command history\n");
  8. #endif
  9.  
  10.     /*
  11.      * Count length of input parameter
  12.      */
  13.     int cmdlen = 0, i;
  14.    
  15.     int paramlen = arraylen((void**)param); // The length of param
  16.  
  17.     for(i = 0; i < paramlen; i++)
  18.     {
  19.         // Increase the length with the length of this word plus a space
  20.         cmdlen += strlen(*(param + i)) + 1;
  21.     }
  22.  
  23.     // Remove the trailing space character
  24.     cmdlen--;
  25.  
  26.     /*
  27.      * Build the command to a string
  28.      */
  29.     char* cmdstring = malloc(sizeof(char) * cmdlen + 1);
  30.     int cmds_index = 0;
  31.  
  32.     // Loop through param
  33.     for(i = 0; i < paramlen; i++)
  34.     {
  35.         int ci;
  36.         for(ci = 0; ci < strlen(*(param + i)); ci++)
  37.         {
  38.             // Copy the character at this index to cmdstring
  39.             *(cmdstring + cmds_index) = *(*(param + i) + ci);
  40.             cmds_index++;
  41.         }
  42.     }
  43.     // Terminate the string
  44.     *(cmdstring + cmds_index) = '\0';
  45.  
  46.     /*
  47.      * Allocate the new meta block
  48.      */
  49.    
  50.     // If a meta block already exists,
  51.     if(h_meta != NULL)
  52.     {
  53.         // Save the previous meta block pointer in a temprary variable
  54.         h_metablock* temp = h_meta;
  55.  
  56.         // Allocate for the new block and set the old one as next
  57.         h_meta = (h_metablock*)malloc(sizeof(h_metablock));
  58.         h_meta->next = temp;
  59.         h_meta->length = cmdlen;
  60.     }
  61.     // Otherwise just allocate
  62.     else
  63.     {
  64.         h_meta = (h_metablock*)malloc(sizeof(h_metablock));
  65.         h_meta->next = NULL;
  66.         h_meta->length = cmdlen;
  67.     }
  68.    
  69.     /*
  70.      * Put the command into data blocks
  71.      */
  72.    
  73.     // The amount of blocks required to store the command. Adding 1 for the
  74.     // nullbyte on cmdlen
  75.     int breq = ((cmdlen + 1) - ((cmdlen + 1) % BLOCK_SIZE)) / BLOCK_SIZE + 1;
  76.    
  77. #ifdef DEBUG
  78.     printf("~\tBlocks required to store command: %d\n", breq);
  79. #endif
  80.  
  81.     // Allocate block indexes for this command
  82.     int* bindexes = h_allocate(breq);
  83.  
  84. #ifdef DEBUG
  85.     // In debug mode, print out the allocated block indexes
  86.     printf("~\tAllocated block indexes: ");
  87.     for(i = 0; i < breq; i++)
  88.     {
  89.         printf(" %d", *(bindexes + i));
  90.     }
  91.     printf("\n");
  92. #endif
  93.  
  94.     // Keep track of how much of the command string had been written to the
  95.     // data blocks
  96.     int cmdsw = 0;
  97.  
  98.     // Save indexes in meta file and write the command text into the blocks
  99.     for(i = 0; i < breq; i++)
  100.     {
  101.         // Save the block index in the meta block
  102.         h_meta->dataindex[i] = bindexes[i];
  103.  
  104.         // Keep track of the data block index we're in
  105.         int bi;
  106.  
  107.         // Keep writing as long as less than the current block has been written
  108.         // and less than the command string length has been written
  109.         for(bi = 0; cmdsw < i * BLOCK_SIZE && cmdsw < cmdlen+1; cmdsw++, bi++)
  110.         {
  111.             // Set the character at this data block position to the current
  112.             // command string character
  113.             h_data[i][bi] = *(cmdstring + cmdsw);
  114.         }
  115.     }
  116.     // Set a -1 to terminate array
  117.     h_meta->dataindex[i] = -1;
  118. }
  119.  
  120. int* h_allocate(int blocks)
  121. {
  122.     // Declare variables for the loop
  123.     int i, found;
  124.  
  125.     // Allocate enough space to hold the needed indices
  126.     int* indices = malloc(sizeof(int) * blocks);
  127.    
  128.     // Set label before the bitmap search to allow retries
  129.     searchbitmap:
  130.  
  131.     // Loop until we're out of bits or we've found all the bits we need
  132.     for(i = 0, found = 0; i < BLOCKS && found < blocks; i++)
  133.     {
  134.         // If this block is available, use if for this command
  135.         if(h_bitmap[i] == 0)
  136.         {
  137.             // Mark this bit as taken
  138.             h_bitmap[i] = 1;
  139.  
  140.             // Set this index in the meta block
  141.             indices[found] = i;
  142.  
  143.             found++;
  144.         }
  145.     }
  146.  
  147.     // Didn't find enough block
  148.     if(found < blocks)
  149.     {
  150.         printf("Couldn't find enough blocks!!!\n");
  151.         exit(0);
  152.     }
  153.  
  154.     return indices;
  155. }
  156.  
  157. // TODO: Finish this function
  158. void h_removeoldtest()
  159. {
  160.     // Create a pointer for the oldest meta block and set it to the newest
  161.     h_metablock* oldest = h_meta;
  162.  
  163.     // Follow the linked list until reaching the end
  164.     while(oldest->next) oldest = oldest->next;
  165.  
  166.  
  167. }
  168.  
  169. // TODO: Finish this function
  170. char* h_meta_getcmd(h_metablock* metablock)
  171. {
  172. #ifdef DEBUG
  173.     // Print out the data grid
  174.     printf("Printing out history data grid here!!\n");
  175. #endif
  176.  
  177.     return NULL;
  178.  
  179.     // Allocate a string of proper size
  180.     char* output = malloc(sizeof(char) * metablock->length + 1);
  181.  
  182.     while(1)
  183.     {
  184.        
  185.     }
  186. }
  187.  
  188. char* h_cmd_index(int const index)
  189. {
  190.     // Fetch the meta block at the input index
  191.     h_metablock* head = h_meta;
  192.     int i;
  193.  
  194.     for(i = 0; i < index; i++)
  195.         head = head->next;
  196.    
  197.     // Fetch the command at this meta block
  198.     char* output = h_meta_getcmd(head);
  199.  
  200.     // And return it
  201.     return output;
  202. }
  203.  
  204. void h_free()
  205. {
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement