parse


#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct list_entry {
char * irq;
char * name;
unsigned long counter;
unsigned long wake_up_s3;
struct list_entry * next;
} list_entry_t;

static list_entry_t * list_head = NULL;
static unsigned long total_irq = 0;
static unsigned int s3_flag = 0;



void print_result(void)
{
int i = 0;
list_entry_t * p = NULL;

for (i = 0; i < 50; i++)
putchar('=');
printf("\n");
printf("%-16s%-28s%s\n", "IRQ-No", "IRQ-Name", "Counter");
for (p = list_head; p ; p = p->next)
printf("%-16s%-28s%-ld\n", p->irq, p->name, p->counter);

for (i = 0; i < 50; i++)
putchar('=');
printf("\n");
printf("Total Irqs: %ld\n", total_irq);

for (i = 0; i < 50; i++)
putchar('=');
printf("\n\n");

printf("S3 staticsis\n");
for (i = 0; i < 50; i++)
putchar('=');
printf("\n");

printf("%-16s%-28s%s\n", "IRQ-No", "IRQ-Name", "Counter");
for (p = list_head; p ; p = p->next){
if (p->wake_up_s3 != 0)
printf("%-16s%-28s%-ld\n", p->irq, p->name, p->wake_up_s3);
}

}

void free_list(void)
{
list_entry_t * p = NULL;
list_entry_t *temp = NULL;

for (p = list_head; p; ) {
temp = p->next;
free(p);
p = temp;
}


}

void process_line(char *line)
{
char *token = NULL;
char *irq_str = NULL;
char *info_str = NULL;
char *info_str_temp = NULL;
char *name = NULL;
unsigned int name_len = 0;
int ret = 0;
list_entry_t * p = NULL;
list_entry_t * temp = NULL;

token = strtok(line, " ");
while (token = strtok(NULL," ")){

if (strcmp(token, "irq_handler_entry:") == 0) { //Process irq line

// printf("token's str-->%s\n", token);
// printf("token's lenth-->%d\n", strlen(token));
// printf("irq_handler_entry: 's lenth-->%d\n", sizeof("irq_handler_entry:"));
total_irq += 1;


irq_str = strtok(NULL, " ");
irq_str = irq_str + 4;

name = strtok(NULL, " ");
name = name + 5;

if (list_head == NULL ) { //First irq line

list_head = (list_entry_t *)malloc(sizeof(list_entry_t));
list_head->irq = (char *)malloc(strlen(irq_str));
list_head->name = (char *)malloc(strlen(name));
strcpy(list_head->irq, irq_str);
strncpy(list_head->name, name, strlen(name) - 1);
list_head->counter = 1;

list_head->next = NULL;

}
else {

for (p = list_head; p; p = p->next) { //Chec current irq is in already list ?
if (strcmp(p->irq, irq_str) == 0) {
p->counter += 1;
if (s3_flag == 2)
{
printf("IN this irq %d wake up system from s3 state\n", atoi(irq_str));
p->wake_up_s3 += 1;
s3_flag = 0;
}

break;
}
}
if (p == NULL) { //Not exists in list ,allocate a node and insert it to list
p = (list_entry_t *)malloc(sizeof(list_entry_t));
p->irq = (char *)malloc(strlen(irq_str));
p->name = (char *)malloc(strlen(name));
strcpy(p->irq, irq_str);
strncpy(p->name, name, strlen(name) - 1);
p->counter = 1;
p->wake_up_s3 = 0;
p->next = NULL;

temp = list_head;
while (temp->next) //insert list end
temp = temp->next;
temp->next = p;
if (s3_flag == 2)
{
printf(" FISRT this irq %d wake up system from s3 state\n", atoi(irq_str));
p->wake_up_s3 += 1;
s3_flag = 0;
}

} //end if (p == NULL)
} //end else
//record which irq wake up system from s3 state
if (s3_flag == 1) {
printf("set s3_flag --> 2\n");
s3_flag = 2;
}

} //end process irq-related line;

else if (strcmp(token, "bprintf:") == 0) { //Process s0ix/s3
// printf("Find a bprintf line..\n");
info_str = strtok(NULL, " ");
info_str_temp = (char *)malloc(strlen(info_str));
strcpy(info_str_temp, info_str);
// printf("info_str_temp-->%s", info_str_temp);
// printf("info str lenth is -->%d\n", strlen(info_str));
// printf("infor_str_temp lenth is-->%d\n", strlen(info_str_temp));
// printf("s3_exit lenth is --->%d\n", strlen("s3_exit"));
// ret = strncmp(info_str, "s3_exit", strlen(info_str));
// printf("info_str size's is %d", strlen(info_str));
// printf("ret-->%d\n", ret);
if (strncmp(info_str_temp,"s3_exit",strlen(info_str_temp) -1) == 0) {
printf("set s3_flag-->1\n");
s3_flag = 1;

}

}

} //end while
return ;
}


int main(int argc, char *argv[])
{
FILE *fp = NULL;
char *line = NULL;
char *file_name = NULL;
size_t len = 0;
ssize_t read = 0;
int i = 0;
list_entry_t * p =NULL;

if (argc != 2) {
write(2, "Usage: parse + filename.\n");
exit(1);
}
file_name = argv[1];
//Open file
fp = fopen(file_name, "r");
if (fp == NULL)
exit(EXIT_FAILURE);

//Process
while ((read = getline(&line, &len, fp)) != -1) {
process_line(line);
}

//Print result
print_result();

//free allocated memory
free_list();

//Close opened file
fclose(fp);
}

你可能感兴趣的:(list,File,null,System,UP,token)