Repository navigation
Expand file tree
/
Copy pathparser.c
More file actions
82 lines (74 loc) · 2.34 KB
/
Copy pathparser.c
File metadata and controls
82 lines (74 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "parser.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#define MAX_LINE 1024
#define MAX_TOKENS 64
char **parse_line(char *line, int *bg) {
char **tokens = malloc(MAX_TOKENS * sizeof(char *));
int pos = 0, i = 0;
*bg = 0; // Varsayılan olarak komut foreground çalışır.
while (line[i]) {
// Boşluk karakterlerini atla.
while (line[i] && isspace(line[i])) i++;
if (!line[i])
break;
char *token = malloc(MAX_LINE); // Token için yeterli bellek ayır.
int t = 0;
// Tırnak içi ifade kontrolü: çift veya tek tırnak.
if (line[i] == '"' || line[i] == '\'') {
char quote = line[i++];
while (line[i] && line[i] != quote) {
if (line[i] == '\\' && line[i+1]) {
token[t++] = line[i+1];
i += 2;
} else {
token[t++] = line[i++];
}
}
if (line[i] == quote)
i++; // Kapanış tırnağını atla.
} else {
// Tırnaksız token
while (line[i] && !isspace(line[i])) {
if (line[i] == '\\' && line[i+1]) {
token[t++] = line[i+1];
i += 2;
} else if (line[i] == '"' || line[i] == '\'') {
char quote = line[i++];
while (line[i] && line[i] != quote) {
if (line[i] == '\\' && line[i+1]) {
token[t++] = line[i+1];
i += 2;
} else {
token[t++] = line[i++];
}
}
if (line[i] == quote)
i++;
} else {
token[t++] = line[i++];
}
}
}
token[t] = '\0';
tokens[pos++] = token;
}
tokens[pos] = NULL;
// Eğer son token "&" ise, komut arka planda çalışacak.
if (pos > 0 && strcmp(tokens[pos-1], "&") == 0) {
*bg = 1;
free(tokens[pos-1]);
tokens[pos-1] = NULL;
}
return tokens;
}
void free_tokens(char **tokens) {
int i = 0;
while(tokens[i]) {
free(tokens[i]);
i++;
}
free(tokens);
}